From bd1cdcd5869007abe7f429aaec07c53974203ec3 Mon Sep 17 00:00:00 2001 From: Yieazy Date: Thu, 26 Oct 2023 15:40:01 +0800 Subject: [PATCH] support evm version: Shanghai! (#85) * chore: tmp * feat: support push0 * feat: supoort shanghai * support evm version: shanghai * Supplant DIFFICULTY opcode with PREVRANDAO * fix test * chore: bump to 0.4.0 * chore: downgrade cita_trie * chore: bump to 0.4.1 * fix: push0 lookup failed * chore: bump to 0.4.2 --- Cargo.toml | 4 +- examples/simplestorage.rs | 13 +-- src/err.rs | 28 ++--- src/evm/common.rs | 2 +- src/evm/err.rs | 26 ++--- src/evm/extmock.rs | 2 +- src/evm/interpreter.rs | 21 ++-- src/evm/opcodes.rs | 21 ++-- src/executive.rs | 29 +++--- src/json_tests/general_state_test.rs | 3 +- src/json_tests/vm_test.rs | 7 +- src/state/account.rs | 35 +++---- src/state/account_db.rs | 18 ++-- src/state/err.rs | 10 +- src/state/state.rs | 146 +++++++++++++++------------ tests/state_misc.rs | 13 +-- tests/state_solidity_test.rs | 27 ++--- 17 files changed, 222 insertions(+), 183 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d85d5e8..a968b87 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cita-vm" -version = "0.3.4" +version = "0.4.2" authors = ["Cryptape Technologies "] edition = "2021" description = "CITA VM" @@ -13,7 +13,7 @@ byteorder = "1.0" cita_trie = "4.0" env_logger = "0.10" ethereum-types = "0.14" -hashbrown = { version = "0.13", features = ["rayon"] } +hashbrown = { version = "0.14", features = ["rayon"] } hasher = { version="0.1" } hex = "0.4" secp256k1 = { package = "libsecp256k1", version = "0.7" } diff --git a/examples/simplestorage.rs b/examples/simplestorage.rs index ab2e1fe..c3259d1 100644 --- a/examples/simplestorage.rs +++ b/examples/simplestorage.rs @@ -1,4 +1,5 @@ use std::cell::RefCell; +use std::str::FromStr; use std::sync::Arc; use ethereum_types::{Address, U256}; @@ -16,13 +17,13 @@ fn main() { 99c66a25d59f0aa78f7ebc40748fa1d1fbc335d8d780f284841b30e0365acd9\ 60029"; state.new_contract( - &Address::from("0xBd770416a3345F91E4B34576cb804a576fa48EB1"), + &Address::from_str("0xBd770416a3345F91E4B34576cb804a576fa48EB1").unwrap(), U256::from(10), U256::from(1), hex::decode(code).unwrap(), ); state.new_contract( - &Address::from("0x1000000000000000000000000000000000000000"), + &Address::from_str("0x1000000000000000000000000000000000000000").unwrap(), U256::from(1_000_000_000_000_000u64), U256::from(1), vec![], @@ -34,8 +35,8 @@ fn main() { let config = cita_vm::Config::default(); let tx = cita_vm::Transaction { - from: Address::from("0x1000000000000000000000000000000000000000"), - to: Some(Address::from("0xBd770416a3345F91E4B34576cb804a576fa48EB1")), + from: Address::from_str("0x1000000000000000000000000000000000000000").unwrap(), + to: Some(Address::from_str("0xBd770416a3345F91E4B34576cb804a576fa48EB1").unwrap()), value: U256::from(0), nonce: U256::from(1), gas_limit: 80000, @@ -53,8 +54,8 @@ fn main() { println!("return={:?}", r); let tx = cita_vm::Transaction { - from: Address::from("0x1000000000000000000000000000000000000000"), - to: Some(Address::from("0xBd770416a3345F91E4B34576cb804a576fa48EB1")), + from: Address::from_str("0x1000000000000000000000000000000000000000").unwrap(), + to: Some(Address::from_str("0xBd770416a3345F91E4B34576cb804a576fa48EB1").unwrap()), value: U256::from(0), nonce: U256::from(2), gas_limit: 80000, diff --git a/src/err.rs b/src/err.rs index 9ac2e99..f845eb6 100644 --- a/src/err.rs +++ b/src/err.rs @@ -26,20 +26,20 @@ impl error::Error for Error {} impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Error::Evm(e) => return write!(f, "{}", e), - Error::Secp256k1(e) => return write!(f, "{:?}", e), - Error::State(e) => return write!(f, "{}", e), - Error::IO(e) => return write!(f, "{:?}", e), - Error::Str(e) => return write!(f, "{:?}", e), - Error::NotEnoughBaseGas => return write!(f, "NotEnoughBaseGas"), - Error::NotEnoughBalance => return write!(f, "NotEnoughBalance"), - Error::InvalidNonce => return write!(f, "InvalidNonce"), - Error::ContractAlreadyExist => return write!(f, "ContractAlreadyExist"), - Error::ExccedMaxCodeSize => return write!(f, "ExccedMaxCodeSize"), - Error::ExccedMaxBlockGasLimit => return write!(f, "ExccedMaxBlockGasLimit"), - Error::ExccedMaxCallDepth => return write!(f, "ExccedMaxCallDepth"), - Error::CreateInStaticCall => return write!(f, "CreateInStaticCall"), - }; + Error::Evm(e) => write!(f, "{}", e), + Error::Secp256k1(e) => write!(f, "{:?}", e), + Error::State(e) => write!(f, "{}", e), + Error::IO(e) => write!(f, "{:?}", e), + Error::Str(e) => write!(f, "{:?}", e), + Error::NotEnoughBaseGas => write!(f, "NotEnoughBaseGas"), + Error::NotEnoughBalance => write!(f, "NotEnoughBalance"), + Error::InvalidNonce => write!(f, "InvalidNonce"), + Error::ContractAlreadyExist => write!(f, "ContractAlreadyExist"), + Error::ExccedMaxCodeSize => write!(f, "ExccedMaxCodeSize"), + Error::ExccedMaxBlockGasLimit => write!(f, "ExccedMaxBlockGasLimit"), + Error::ExccedMaxCallDepth => write!(f, "ExccedMaxCallDepth"), + Error::CreateInStaticCall => write!(f, "CreateInStaticCall"), + } } } diff --git a/src/evm/common.rs b/src/evm/common.rs index f7b0ef5..bee704b 100644 --- a/src/evm/common.rs +++ b/src/evm/common.rs @@ -74,7 +74,7 @@ pub fn rpad(slice: Vec, n: usize) -> Vec { let mut padded: Vec = Vec::with_capacity(n); let mut part1 = slice; padded.append(&mut part1); - let mut part2 = vec![0; n as usize - slice_len]; + let mut part2 = vec![0; n - slice_len]; padded.append(&mut part2); padded } diff --git a/src/evm/err.rs b/src/evm/err.rs index 0d73057..8bdea48 100644 --- a/src/evm/err.rs +++ b/src/evm/err.rs @@ -21,18 +21,18 @@ impl error::Error for Error {} impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Error::OutOfBounds => return write!(f, "OutOfBounds"), - Error::OutOfGas => return write!(f, "OutOfGas"), - Error::OutOfStack => return write!(f, "OutOfStack"), - Error::OutOfCode => return write!(f, "OutOfCode"), - Error::OutOfData => return write!(f, "OutOfData"), - Error::MutableCallInStaticContext => return write!(f, "MutableCallInStaticContext"), - Error::InvalidOpcode => return write!(f, "InvalidOpcode"), - Error::CallError => return write!(f, "CallError"), - Error::ExccedMaxCodeSize => return write!(f, "ExccedMaxCodeSize"), - Error::InvalidJumpDestination => return write!(f, "InvalidJumpDestination"), - Error::Internal(err) => return write!(f, "Internal error {}", err), - Error::StackUnderflow => return write!(f, "StackUnderflow"), - }; + Error::OutOfBounds => write!(f, "OutOfBounds"), + Error::OutOfGas => write!(f, "OutOfGas"), + Error::OutOfStack => write!(f, "OutOfStack"), + Error::OutOfCode => write!(f, "OutOfCode"), + Error::OutOfData => write!(f, "OutOfData"), + Error::MutableCallInStaticContext => write!(f, "MutableCallInStaticContext"), + Error::InvalidOpcode => write!(f, "InvalidOpcode"), + Error::CallError => write!(f, "CallError"), + Error::ExccedMaxCodeSize => write!(f, "ExccedMaxCodeSize"), + Error::InvalidJumpDestination => write!(f, "InvalidJumpDestination"), + Error::Internal(err) => write!(f, "Internal error {}", err), + Error::StackUnderflow => write!(f, "StackUnderflow"), + } } } diff --git a/src/evm/extmock.rs b/src/evm/extmock.rs index 3db63bc..c5bcdd3 100644 --- a/src/evm/extmock.rs +++ b/src/evm/extmock.rs @@ -118,7 +118,7 @@ impl ext::DataProvider for DataProviderMock { let mut it = interpreter::Interpreter::new( interpreter::Context::default(), interpreter::InterpreterConf::default(), - Box::new(DataProviderMock::default()), + Box::::default(), params, ); let data_provider = DataProviderMock { diff --git a/src/evm/interpreter.rs b/src/evm/interpreter.rs index 092e987..6f76ae0 100644 --- a/src/evm/interpreter.rs +++ b/src/evm/interpreter.rs @@ -663,7 +663,7 @@ impl Interpreter { let shift = shift.as_u32() as usize; let mut shifted = value >> shift; if sign { - shifted = shifted | (U256::max_value() << (256 - shift)); + shifted |= U256::max_value() << (256 - shift); } shifted }; @@ -788,7 +788,7 @@ impl Interpreter { opcodes::OpCode::NUMBER => { self.stack.push(self.context.number); } - opcodes::OpCode::DIFFICULTY => { + opcodes::OpCode::PREVRANDAO => { self.stack.push(self.context.difficulty); } opcodes::OpCode::GASLIMIT => { @@ -851,6 +851,9 @@ impl Interpreter { self.stack.push(U256::from(self.gas)); } opcodes::OpCode::JUMPDEST => {} + opcodes::OpCode::PUSH0 => { + self.stack.push(U256::zero()); + } opcodes::OpCode::PUSH1 | opcodes::OpCode::PUSH2 | opcodes::OpCode::PUSH3 @@ -1771,10 +1774,16 @@ mod tests { let mut it = default_interpreter(); it.cfg.eip1283 = true; assert_eq!(it.gas, it.context.gas_limit); - it.data_provider - .set_storage_origin(&it.params.contract.code_address, H256::zero(), H256::from(origin)); - it.data_provider - .set_storage(&it.params.contract.code_address, H256::zero(), H256::from(origin)); + it.data_provider.set_storage_origin( + &it.params.contract.code_address, + H256::zero(), + H256::from_low_u64_be(origin), + ); + it.data_provider.set_storage( + &it.params.contract.code_address, + H256::zero(), + H256::from_low_u64_be(origin), + ); it.params.contract.code_data = hex::decode(code).unwrap(); it.run().unwrap(); assert_eq!(it.gas, it.context.gas_limit - use_gas); diff --git a/src/evm/opcodes.rs b/src/evm/opcodes.rs index e353b02..5a12afe 100644 --- a/src/evm/opcodes.rs +++ b/src/evm/opcodes.rs @@ -49,7 +49,7 @@ pub enum OpCode { COINBASE = 0x41, TIMESTAMP = 0x42, NUMBER = 0x43, - DIFFICULTY = 0x44, + PREVRANDAO = 0x44, GASLIMIT = 0x45, CHAINID = 0x46, SELFBALANCE = 0x47, @@ -66,6 +66,7 @@ pub enum OpCode { MSIZE = 0x59, GAS = 0x5a, JUMPDEST = 0x5b, + PUSH0 = 0x5f, PUSH1 = 0x60, PUSH2 = 0x61, PUSH3 = 0x62, @@ -196,7 +197,7 @@ impl fmt::Display for OpCode { OpCode::COINBASE => write!(f, "COINBASE"), OpCode::TIMESTAMP => write!(f, "TIMESTAMP"), OpCode::NUMBER => write!(f, "NUMBER"), - OpCode::DIFFICULTY => write!(f, "DIFFICULTY"), + OpCode::PREVRANDAO => write!(f, "PREVRANDAO"), OpCode::GASLIMIT => write!(f, "GASLIMIT"), OpCode::CHAINID => write!(f, "CHAINID"), OpCode::SELFBALANCE => write!(f, "SELFBALANCE"), @@ -213,6 +214,7 @@ impl fmt::Display for OpCode { OpCode::MSIZE => write!(f, "MSIZE"), OpCode::GAS => write!(f, "GAS"), OpCode::JUMPDEST => write!(f, "JUMPDEST"), + OpCode::PUSH0 => write!(f, "PUSH0"), OpCode::PUSH1 => write!(f, "PUSH1"), OpCode::PUSH2 => write!(f, "PUSH2"), OpCode::PUSH3 => write!(f, "PUSH3"), @@ -372,7 +374,7 @@ impl OpCode { 0x41 => Some(OpCode::COINBASE), 0x42 => Some(OpCode::TIMESTAMP), 0x43 => Some(OpCode::NUMBER), - 0x44 => Some(OpCode::DIFFICULTY), + 0x44 => Some(OpCode::PREVRANDAO), 0x45 => Some(OpCode::GASLIMIT), 0x46 => Some(OpCode::CHAINID), 0x47 => Some(OpCode::SELFBALANCE), @@ -389,6 +391,7 @@ impl OpCode { 0x59 => Some(OpCode::MSIZE), 0x5a => Some(OpCode::GAS), 0x5b => Some(OpCode::JUMPDEST), + 0x5f => Some(OpCode::PUSH0), 0x60 => Some(OpCode::PUSH1), 0x61 => Some(OpCode::PUSH2), 0x62 => Some(OpCode::PUSH3), @@ -520,7 +523,7 @@ impl OpCode { OpCode::COINBASE => GasPriceTier::Base, OpCode::TIMESTAMP => GasPriceTier::Base, OpCode::NUMBER => GasPriceTier::Base, - OpCode::DIFFICULTY => GasPriceTier::Base, + OpCode::PREVRANDAO => GasPriceTier::Base, OpCode::GASLIMIT => GasPriceTier::Base, OpCode::CHAINID => GasPriceTier::VeryLow, OpCode::SELFBALANCE => GasPriceTier::Low, @@ -537,6 +540,7 @@ impl OpCode { OpCode::MSIZE => GasPriceTier::Base, OpCode::GAS => GasPriceTier::Base, OpCode::JUMPDEST => GasPriceTier::Special, + OpCode::PUSH0 => GasPriceTier::Base, OpCode::PUSH1 => GasPriceTier::VeryLow, OpCode::PUSH2 => GasPriceTier::VeryLow, OpCode::PUSH3 => GasPriceTier::VeryLow, @@ -667,7 +671,7 @@ impl OpCode { OpCode::COINBASE => 0, OpCode::TIMESTAMP => 0, OpCode::NUMBER => 0, - OpCode::DIFFICULTY => 0, + OpCode::PREVRANDAO => 0, OpCode::GASLIMIT => 0, OpCode::CHAINID => 0, OpCode::SELFBALANCE => 0, @@ -684,6 +688,7 @@ impl OpCode { OpCode::MSIZE => 0, OpCode::GAS => 0, OpCode::JUMPDEST => 0, + OpCode::PUSH0 => 0, OpCode::PUSH1 => 0, OpCode::PUSH2 => 0, OpCode::PUSH3 => 0, @@ -814,7 +819,7 @@ impl OpCode { OpCode::COINBASE => 1, OpCode::TIMESTAMP => 1, OpCode::NUMBER => 1, - OpCode::DIFFICULTY => 1, + OpCode::PREVRANDAO => 1, OpCode::GASLIMIT => 1, OpCode::CHAINID => 1, OpCode::SELFBALANCE => 1, @@ -831,6 +836,7 @@ impl OpCode { OpCode::MSIZE => 1, OpCode::GAS => 1, OpCode::JUMPDEST => 0, + OpCode::PUSH0 => 1, OpCode::PUSH1 => 1, OpCode::PUSH2 => 1, OpCode::PUSH3 => 1, @@ -961,7 +967,7 @@ impl OpCode { OpCode::COINBASE => false, OpCode::TIMESTAMP => false, OpCode::NUMBER => false, - OpCode::DIFFICULTY => false, + OpCode::PREVRANDAO => false, OpCode::GASLIMIT => false, OpCode::CHAINID => false, OpCode::SELFBALANCE => false, @@ -978,6 +984,7 @@ impl OpCode { OpCode::MSIZE => false, OpCode::GAS => false, OpCode::JUMPDEST => false, + OpCode::PUSH0 => false, OpCode::PUSH1 => false, OpCode::PUSH2 => false, OpCode::PUSH3 => false, diff --git a/src/executive.rs b/src/executive.rs index 2cbf786..53c9b06 100644 --- a/src/executive.rs +++ b/src/executive.rs @@ -12,7 +12,7 @@ use crate::common; use crate::err; use crate::evm; use crate::native; -use crate::state::{self, State, StateObjectInfo}; +use crate::state::{State, StateObjectInfo}; use hasher::Hasher; /// BlockDataProvider provides functions to get block's hash from chain. @@ -142,7 +142,7 @@ pub fn get_interpreter_conf() -> evm::InterpreterConf { /// /// See: EIP 684 pub fn can_create( - state_provider: Arc>>, + state_provider: Arc>>, address: &Address, ) -> Result { let a = state_provider.borrow_mut().nonce(address)?; @@ -182,7 +182,7 @@ pub fn get_refund(store: Arc>, request: &InterpreterParams, gas_l /// Liquidtion for a transaction. pub fn clear( - state_provider: Arc>>, + state_provider: Arc>>, store: Arc>, request: &InterpreterParams, gas_left: u64, @@ -219,7 +219,7 @@ impl Default for Config { /// Function call_pure enters into the specific contract with no check or checkpoints. fn call_pure( block_provider: Arc, - state_provider: Arc>>, + state_provider: Arc>>, store: Arc>, request: &InterpreterParams, ) -> Result { @@ -257,7 +257,7 @@ fn call_pure( /// Function call enters into the specific contract. fn call( block_provider: Arc, - state_provider: Arc>>, + state_provider: Arc>>, store: Arc>, request: &InterpreterParams, ) -> Result { @@ -296,7 +296,7 @@ fn call( /// Function create creates a new contract. fn create( block_provider: Arc, - state_provider: Arc>>, + state_provider: Arc>>, store: Arc>, request: &InterpreterParams, create_kind: CreateKind, @@ -399,10 +399,7 @@ pub struct Transaction { } /// Reinterpret tx to interpreter params. -fn reinterpret_tx( - tx: Transaction, - state_provider: Arc>>, -) -> InterpreterParams { +fn reinterpret_tx(tx: Transaction, state_provider: Arc>>) -> InterpreterParams { let mut request = InterpreterParams { origin: tx.from, sender: tx.from, @@ -432,12 +429,12 @@ fn reinterpret_tx( /// Execute the transaction from transaction pool pub fn exec( block_provider: Arc, - state_provider: Arc>>, + state_provider: Arc>>, evm_context: evm::Context, config: Config, tx: Transaction, ) -> Result { - let mut request = &mut reinterpret_tx(tx, state_provider.clone()); + let request = &mut reinterpret_tx(tx, state_provider.clone()); // Ensure gas < block_gas_limit /* TODO : this judgement need be reconsider @@ -553,7 +550,7 @@ pub fn exec( #[allow(unused_variables)] pub fn exec_static( block_provider: Arc, - state_provider: Arc>>, + state_provider: Arc>>, evm_context: evm::Context, config: Config, tx: Transaction, @@ -575,12 +572,12 @@ pub fn exec_static( pub struct Executive { pub block_provider: Arc, - pub state_provider: Arc>>, + pub state_provider: Arc>>, pub config: Config, } impl Executive { - pub fn new(block_provider: Arc, state_provider: state::State, config: Config) -> Self { + pub fn new(block_provider: Arc, state_provider: State, config: Config) -> Self { Self { block_provider, state_provider: Arc::new(RefCell::new(state_provider)), @@ -634,7 +631,7 @@ impl Executive { pub fn exec_static( block_provider: Arc, - state_provider: state::State, + state_provider: State, evm_context: evm::Context, config: Config, tx: Transaction, diff --git a/src/json_tests/general_state_test.rs b/src/json_tests/general_state_test.rs index 198003f..455172e 100644 --- a/src/json_tests/general_state_test.rs +++ b/src/json_tests/general_state_test.rs @@ -143,6 +143,7 @@ impl Test { mod tests { use super::*; use std::fs; + use std::str::FromStr; #[test] fn test_json_tests_parse() { @@ -154,7 +155,7 @@ mod tests { let v = &t.0["addmodNonConst"]; assert_eq!( v.env.current_coinbase, - Address::from("0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba") + Address::from_str("0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba").unwrap() ); } } diff --git a/src/json_tests/vm_test.rs b/src/json_tests/vm_test.rs index 98bc32f..53afe3d 100644 --- a/src/json_tests/vm_test.rs +++ b/src/json_tests/vm_test.rs @@ -125,6 +125,7 @@ impl Test { mod tests { use super::*; use std::fs; + use std::str::FromStr; #[test] fn test_json_tests_parse() { @@ -135,16 +136,16 @@ mod tests { let v = &t.0["add0"]; assert_eq!( v.env.current_coinbase, - Address::from("0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba") + Address::from_str("0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba").unwrap() ); assert_eq!( v.exec.address, - Address::from("0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6") + Address::from_str("0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap() ); assert_eq!(v.gas, Some(String::from("0x013874"))); if let Some(data) = &v.post { assert_eq!( - data.0[&Address::from("0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6")].balance, + data.0[&Address::from_str("0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap()].balance, String::from("0x0de0b6b3a7640000") ) } diff --git a/src/state/account.rs b/src/state/account.rs index c7b3ef1..8661968 100644 --- a/src/state/account.rs +++ b/src/state/account.rs @@ -376,6 +376,7 @@ impl StateObject { #[cfg(test)] mod tests { use super::*; + use std::str::FromStr; #[test] fn state_object_new() { @@ -409,10 +410,10 @@ mod tests { assert_eq!(a.code_state, CodeState::Clean); assert_eq!( a.code_hash, - "af231e631776a517ca23125370d542873eca1fb4d613ed9b5d5335a46ae5b7eb".into() + H256::from_str("af231e631776a517ca23125370d542873eca1fb4d613ed9b5d5335a46ae5b7eb").unwrap() ); - let k = a.code_hash.to_vec(); + let k = a.code_hash.0.to_vec(); assert_eq!(db.get(&k).unwrap().unwrap(), vec![0x55, 0x44, 0xffu8]); a.init_code(vec![0x55]); assert_eq!(a.code_state, CodeState::Dirty); @@ -420,10 +421,10 @@ mod tests { a.commit_code(Arc::clone(&db)).unwrap(); assert_eq!( a.code_hash, - "37bf2238b11b68cdc8382cece82651b59d3c3988873b6e0f33d79694aa45f1be".into() + H256::from_str("37bf2238b11b68cdc8382cece82651b59d3c3988873b6e0f33d79694aa45f1be").unwrap() ); - let k = a.code_hash.to_vec(); + let k = a.code_hash.0.to_vec(); assert_eq!(db.get(&k).unwrap().unwrap(), vec![0x55]); } @@ -431,11 +432,11 @@ mod tests { fn state_object_storage_1() { let mut a = StateObject::new(69u8.into(), 0.into()); let db = Arc::new(cita_trie::MemoryDB::new(false)); - a.set_storage(0.into(), 0x1234.into()); + a.set_storage(H256::zero(), H256::from_low_u64_be(0x1234)); a.commit_storage(Arc::clone(&db)).unwrap(); assert_eq!( a.storage_root, - "71623f5ec821de33ad5aa81f8c82f0916c6f60de0a536f8c466d440c56715bd5".into() + H256::from_str("71623f5ec821de33ad5aa81f8c82f0916c6f60de0a536f8c466d440c56715bd5").unwrap() ); } @@ -443,26 +444,26 @@ mod tests { fn state_object_storage_2() { let mut a = StateObject::new(69u8.into(), 0.into()); let db = Arc::new(cita_trie::MemoryDB::new(false)); - a.set_storage(0.into(), 0x1234.into()); + a.set_storage(H256::zero(), H256::from_low_u64_be(0x1234)); a.commit_storage(Arc::clone(&db)).unwrap(); assert_eq!( a.storage_root, //"c57e1afb758b07f8d2c8f13a3b6e44fa5ff94ab266facc5a4fd3f062426e50b2".into() - "71623f5ec821de33ad5aa81f8c82f0916c6f60de0a536f8c466d440c56715bd5".into() + H256::from_str("71623f5ec821de33ad5aa81f8c82f0916c6f60de0a536f8c466d440c56715bd5").unwrap() ); - a.set_storage(1.into(), 0x1234.into()); + a.set_storage(H256::from_low_u64_be(1), H256::from_low_u64_be(0x1234)); a.commit_storage(Arc::clone(&db)).unwrap(); assert_eq!( a.storage_root, //"4e49574efd650366d071855e0a3975123ea9d64cc945e8f5de8c8c517e1b4ca5".into() - "a3db671bd0653a641fb031dccb869982da390eade9e6f993802ed09c4f6b7b2a".into() + H256::from_str("a3db671bd0653a641fb031dccb869982da390eade9e6f993802ed09c4f6b7b2a").unwrap() ); - a.set_storage(1.into(), 0.into()); + a.set_storage(H256::from_low_u64_be(1), H256::zero()); a.commit_storage(Arc::clone(&db)).unwrap(); assert_eq!( a.storage_root, //"c57e1afb758b07f8d2c8f13a3b6e44fa5ff94ab266facc5a4fd3f062426e50b2".into() - "71623f5ec821de33ad5aa81f8c82f0916c6f60de0a536f8c466d440c56715bd5".into() + H256::from_str("71623f5ec821de33ad5aa81f8c82f0916c6f60de0a536f8c466d440c56715bd5").unwrap() ); } @@ -471,7 +472,7 @@ mod tests { let mut a = StateObject::new(69u8.into(), 0.into()); let db = Arc::new(cita_trie::MemoryDB::new(false)); let a_rlp = { - a.set_storage(0x00u64.into(), 0x1234u64.into()); + a.set_storage(H256::zero(), H256::from_low_u64_be(0x1234)); a.commit_storage(Arc::clone(&db)).unwrap(); a.init_code(vec![]); a.commit_code(Arc::clone(&db)).unwrap(); @@ -481,13 +482,13 @@ mod tests { assert_eq!( a.storage_root, //"c57e1afb758b07f8d2c8f13a3b6e44fa5ff94ab266facc5a4fd3f062426e50b2".into() - "71623f5ec821de33ad5aa81f8c82f0916c6f60de0a536f8c466d440c56715bd5".into() + H256::from_str("71623f5ec821de33ad5aa81f8c82f0916c6f60de0a536f8c466d440c56715bd5").unwrap() ); assert_eq!( - a.get_storage(Arc::clone(&db), &0x00u64.into()).unwrap().unwrap(), - 0x1234u64.into() + a.get_storage(Arc::clone(&db), &H256::zero()).unwrap().unwrap(), + H256::from_low_u64_be(0x1234) ); - assert_eq!(a.get_storage(Arc::clone(&db), &0x01u64.into()).unwrap(), None); + assert_eq!(a.get_storage(Arc::clone(&db), &H256::from_low_u64_be(1)).unwrap(), None); } #[test] diff --git a/src/state/account_db.rs b/src/state/account_db.rs index 4cbb487..9c5a9cb 100644 --- a/src/state/account_db.rs +++ b/src/state/account_db.rs @@ -1,10 +1,8 @@ -use std::sync::Arc; - use crate::common::hash::{summary, RLP_NULL}; +use crate::state::Error; use cita_trie::DB; use ethereum_types::{Address, H256}; - -use crate::state::err::Error; +use std::sync::Arc; static NULL_RLP_STATIC: [u8; 1] = [0x80; 1]; @@ -34,8 +32,7 @@ impl AccountDB { impl DB for AccountDB { type Error = Error; - - fn get(&self, key: &[u8]) -> Result>, Self::Error> { + fn get(&self, key: &[u8]) -> Result>, Error> { if H256::from_slice(key) == RLP_NULL { return Ok(Some(NULL_RLP_STATIC.to_vec())); } @@ -46,7 +43,7 @@ impl DB for AccountDB { .map_err(|e| Error::DB(format!("{}", e))) } - fn insert(&self, key: Vec, value: Vec) -> Result<(), Self::Error> { + fn insert(&self, key: Vec, value: Vec) -> Result<(), Error> { if H256::from_slice(key.as_slice()) == RLP_NULL { return Ok(()); } @@ -56,7 +53,7 @@ impl DB for AccountDB { .map_err(|e| Error::DB(format!("{}", e))) } - fn contains(&self, key: &[u8]) -> Result { + fn contains(&self, key: &[u8]) -> Result { if H256::from_slice(key) == RLP_NULL { return Ok(true); } @@ -66,7 +63,7 @@ impl DB for AccountDB { .map_err(|e| Error::DB(format!("{}", e))) } - fn remove(&self, key: &[u8]) -> Result<(), Self::Error> { + fn remove(&self, key: &[u8]) -> Result<(), Error> { if H256::from_slice(key) == RLP_NULL { return Ok(()); } @@ -76,7 +73,7 @@ impl DB for AccountDB { .map_err(|e| Error::DB(format!("{}", e))) } - fn flush(&self) -> Result<(), Self::Error> { + fn flush(&self) -> Result<(), Error> { self.db.flush().map_err(|e| Error::DB(format!("{}", e))) } } @@ -85,6 +82,7 @@ impl DB for AccountDB { mod test_account_db { use super::*; use cita_trie::MemoryDB; + use cita_trie::DB; #[test] fn test_accdb_get() { diff --git a/src/state/err.rs b/src/state/err.rs index 4a3ccc5..bab581a 100644 --- a/src/state/err.rs +++ b/src/state/err.rs @@ -11,11 +11,11 @@ impl std::error::Error for Error {} impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { - Error::Trie(e) => return write!(f, "state trie: {}", e), - Error::RLP(e) => return write!(f, "state rlp: {}", e), - Error::DB(e) => return write!(f, "state db: {}", e), - Error::NotFound => return write!(f, "state: not found"), - Error::BalanceError => return write!(f, "state: balance error"), + Error::Trie(e) => write!(f, "state trie: {}", e), + Error::RLP(e) => write!(f, "state rlp: {}", e), + Error::DB(e) => write!(f, "state db: {}", e), + Error::NotFound => write!(f, "state: not found"), + Error::BalanceError => write!(f, "state: balance error"), } } } diff --git a/src/state/state.rs b/src/state/state.rs index 2c7c878..7ca36c2 100644 --- a/src/state/state.rs +++ b/src/state/state.rs @@ -1,21 +1,17 @@ -use std::cell::RefCell; -use std::sync::Arc; - -use cita_trie::DB; -use cita_trie::{PatriciaTrie, Trie}; -use ethereum_types::{Address, H256, U256}; -use hashbrown::hash_map::Entry; -use hashbrown::{HashMap, HashSet}; -use log::debug; -use rayon::prelude::{IntoParallelRefMutIterator, ParallelIterator}; -//use std::str::FromStr; - use crate::common; use crate::common::hash; use crate::state::account::StateObject; use crate::state::account_db::AccountDB; use crate::state::err::Error; use crate::state::object_entry::{ObjectStatus, StateObjectEntry}; +use cita_trie::{PatriciaTrie, Trie, DB}; +use ethereum_types::{Address, H256, U256}; +use hashbrown::hash_map::Entry; +use hashbrown::{HashMap, HashSet}; +use log::debug; +use rayon::prelude::{IntoParallelRefMutIterator, ParallelIterator}; +use std::cell::RefCell; +use std::sync::Arc; const PREFIX_LEN: usize = 12; const LATEST_ERA_KEY: [u8; PREFIX_LEN] = [b'l', b'a', b's', b't', 0, 0, 0, 0, 0, 0, 0, 0]; @@ -298,7 +294,6 @@ impl State { pub fn commit(&mut self) -> Result<(), Error> { assert!(self.checkpoints.borrow().is_empty()); // Firstly, update account storage tree - let db = Arc::clone(&self.db); self.cache .borrow_mut() .par_iter_mut() @@ -309,7 +304,7 @@ impl State { if let Some(ref mut state_object) = entry.state_object { // When operate on account element, AccountDB should be used - let accdb = Arc::new(AccountDB::new(*address, Arc::clone(&db))); + let accdb = Arc::new(AccountDB::new(*address, self.db.clone())); state_object.commit_storage(Arc::clone(&accdb))?; state_object.commit_code(Arc::clone(&accdb))?; state_object.commit_abi(Arc::clone(&accdb))?; @@ -325,7 +320,7 @@ impl State { .cache .borrow_mut() .par_iter_mut() - .filter(|&(_, ref a)| a.is_dirty()) + .filter(|(_, a)| a.is_dirty()) .map(|(address, entry)| { entry.status = ObjectStatus::Committed; match entry.state_object { @@ -478,6 +473,7 @@ impl StateObjectInfo for State { mod tests { use super::*; use cita_trie::MemoryDB; + use std::str::FromStr; use std::sync::Arc; fn get_temp_state() -> State { @@ -494,14 +490,14 @@ mod tests { assert_eq!(state.code(&a).unwrap(), vec![1, 2, 3]); assert_eq!( state.code_hash(&a).unwrap(), - "0xf1885eda54b7a053318cd41e2093220dab15d65381b1157a3633a83bfd5c9239".into() + H256::from_str("0xf1885eda54b7a053318cd41e2093220dab15d65381b1157a3633a83bfd5c9239").unwrap() ); assert_eq!(state.code_size(&a).unwrap(), 3); state.commit().unwrap(); assert_eq!(state.code(&a).unwrap(), vec![1, 2, 3]); assert_eq!( state.code_hash(&a).unwrap(), - "0xf1885eda54b7a053318cd41e2093220dab15d65381b1157a3633a83bfd5c9239".into() + H256::from_str("0xf1885eda54b7a053318cd41e2093220dab15d65381b1157a3633a83bfd5c9239").unwrap() ); assert_eq!(state.code_size(&a).unwrap(), 3); (state.root, state.db) @@ -511,7 +507,7 @@ mod tests { assert_eq!(state.code(&a).unwrap(), vec![1, 2, 3]); assert_eq!( state.code_hash(&a).unwrap(), - "0xf1885eda54b7a053318cd41e2093220dab15d65381b1157a3633a83bfd5c9239".into() + H256::from_str("0xf1885eda54b7a053318cd41e2093220dab15d65381b1157a3633a83bfd5c9239").unwrap() ); assert_eq!(state.code_size(&a).unwrap(), 3); } @@ -525,14 +521,14 @@ mod tests { assert_eq!(state.abi(&a).unwrap(), vec![1, 2, 3]); assert_eq!( state.abi_hash(&a).unwrap(), - "0xf1885eda54b7a053318cd41e2093220dab15d65381b1157a3633a83bfd5c9239".into() + H256::from_str("0xf1885eda54b7a053318cd41e2093220dab15d65381b1157a3633a83bfd5c9239").unwrap() ); assert_eq!(state.abi_size(&a).unwrap(), 3); state.commit().unwrap(); assert_eq!(state.abi(&a).unwrap(), vec![1, 2, 3]); assert_eq!( state.abi_hash(&a).unwrap(), - "0xf1885eda54b7a053318cd41e2093220dab15d65381b1157a3633a83bfd5c9239".into() + H256::from_str("0xf1885eda54b7a053318cd41e2093220dab15d65381b1157a3633a83bfd5c9239").unwrap() ); assert_eq!(state.abi_size(&a).unwrap(), 3); (state.root, state.db) @@ -542,7 +538,7 @@ mod tests { assert_eq!(state.abi(&a).unwrap(), vec![1, 2, 3]); assert_eq!( state.abi_hash(&a).unwrap(), - "0xf1885eda54b7a053318cd41e2093220dab15d65381b1157a3633a83bfd5c9239".into() + H256::from_str("0xf1885eda54b7a053318cd41e2093220dab15d65381b1157a3633a83bfd5c9239").unwrap() ); assert_eq!(state.abi_size(&a).unwrap(), 3); } @@ -553,7 +549,7 @@ mod tests { let (root, db) = { let mut state = get_temp_state(); state - .set_storage(&a, H256::from(&U256::from(1u64)), H256::from(&U256::from(69u64))) + .set_storage(&a, H256::from_low_u64_be(1), H256::from_low_u64_be(69)) .unwrap(); state.commit().unwrap(); (state.root, state.db) @@ -561,8 +557,8 @@ mod tests { let mut state = State::from_existing(db, root).unwrap(); assert_eq!( - state.get_storage(&a, &H256::from(&U256::from(1u64))).unwrap(), - H256::from(&U256::from(69u64)) + state.get_storage(&a, &H256::from_low_u64_be(1u64)).unwrap(), + H256::from_low_u64_be(69u64) ); } @@ -627,7 +623,7 @@ mod tests { fn alter_balance() { let mut state = get_temp_state(); let a = Address::zero(); - let b: Address = 1u64.into(); + let b = Address::from_low_u64_be(1); state.add_balance(&a, U256::from(69u64)).unwrap(); assert_eq!(state.balance(&a).unwrap(), U256::from(69u64)); @@ -681,7 +677,7 @@ mod tests { state.commit().unwrap(); assert_eq!( state.root, - "530acecc6ec873396bb3e90b6578161f9688ed7eeeb93d6fba5684895a93b78a".into() + H256::from_str("530acecc6ec873396bb3e90b6578161f9688ed7eeeb93d6fba5684895a93b78a").unwrap() ); } @@ -721,12 +717,12 @@ mod tests { fn checkpoint_revert_to_get_storage() { let mut state = get_temp_state(); let a = Address::zero(); - let k = H256::from(U256::from(0)); + let k = H256::zero(); state.checkpoint(); state.checkpoint(); - state.set_storage(&a, k, H256::from(1u64)).unwrap(); - assert_eq!(state.get_storage(&a, &k).unwrap(), H256::from(1u64)); + state.set_storage(&a, k, H256::from_low_u64_be(1)).unwrap(); + assert_eq!(state.get_storage(&a, &k).unwrap(), H256::from_low_u64_be(1)); state.revert_checkpoint(); assert!(state.get_storage(&a, &k).unwrap().is_zero()); } @@ -735,21 +731,21 @@ mod tests { fn checkpoint_kill_account() { let mut state = get_temp_state(); let a = Address::zero(); - let k = H256::from(U256::from(0)); + let k = H256::zero(); state.checkpoint(); - state.set_storage(&a, k, H256::from(U256::from(1))).unwrap(); + state.set_storage(&a, k, H256::from_low_u64_be(1)).unwrap(); state.checkpoint(); state.kill_contract(&a); assert!(state.get_storage(&a, &k).unwrap().is_zero()); state.revert_checkpoint(); - assert_eq!(state.get_storage(&a, &k).unwrap(), H256::from(U256::from(1))); + assert_eq!(state.get_storage(&a, &k).unwrap(), H256::from_low_u64_be(1)); } #[test] fn checkpoint_create_contract_fail() { let mut state = get_temp_state(); let orig_root = state.root; - let a: Address = 1000.into(); + let a = Address::from_low_u64_be(1000); state.checkpoint(); // c1 state.new_contract(&a, U256::zero(), U256::zero(), vec![]); @@ -766,25 +762,25 @@ mod tests { #[test] fn create_contract_fail_previous_storage() { let mut state = get_temp_state(); - let a: Address = 1000.into(); - let k = H256::from(U256::from(0)); + let a = Address::from_low_u64_be(1000); + let k = H256::zero(); - state.set_storage(&a, k, H256::from(U256::from(0xffff))).unwrap(); + state.set_storage(&a, k, H256::from_low_u64_be(0xffff)).unwrap(); state.commit().unwrap(); state.clear(); let orig_root = state.root; - assert_eq!(state.get_storage(&a, &k).unwrap(), H256::from(U256::from(0xffff))); + assert_eq!(state.get_storage(&a, &k).unwrap(), H256::from_low_u64_be(0xffff)); state.clear(); state.checkpoint(); // c1 state.new_contract(&a, U256::zero(), U256::zero(), vec![]); state.checkpoint(); // c2 - state.set_storage(&a, k, H256::from(U256::from(2))).unwrap(); + state.set_storage(&a, k, H256::from_low_u64_be(2)).unwrap(); state.revert_checkpoint(); // revert to c2 - assert_eq!(state.get_storage(&a, &k).unwrap(), H256::from(U256::from(0))); + assert_eq!(state.get_storage(&a, &k).unwrap(), H256::from_low_u64_be(0)); state.revert_checkpoint(); // revert to c1 - assert_eq!(state.get_storage(&a, &k).unwrap(), H256::from(U256::from(0xffff))); + assert_eq!(state.get_storage(&a, &k).unwrap(), H256::from_low_u64_be(0xffff)); state.commit().unwrap(); assert_eq!(orig_root, state.root); @@ -793,14 +789,19 @@ mod tests { #[test] fn checkpoint_chores() { let mut state = get_temp_state(); - let a: Address = 1000.into(); - let b: Address = 2000.into(); + let a = Address::from_low_u64_be(1000); + let b = Address::from_low_u64_be(2000); state.new_contract(&a, 5.into(), 0.into(), vec![10u8, 20, 30, 40, 50]); state.add_balance(&a, 5.into()).unwrap(); - state.set_storage(&a, 10.into(), 10.into()).unwrap(); + state + .set_storage(&a, H256::from_low_u64_be(10), H256::from_low_u64_be(10)) + .unwrap(); assert_eq!(state.code(&a).unwrap(), vec![10u8, 20, 30, 40, 50]); assert_eq!(state.balance(&a).unwrap(), 10.into()); - assert_eq!(state.get_storage(&a, &10.into()).unwrap(), 10.into()); + assert_eq!( + state.get_storage(&a, &H256::from_low_u64_be(10)).unwrap(), + H256::from_low_u64_be(10) + ); state.commit().unwrap(); let orig_root = state.root; @@ -820,21 +821,37 @@ mod tests { state.checkpoint(); // c1 state.sub_balance(&a, 2.into()).unwrap(); - state.set_storage(&a, 20.into(), 20.into()).unwrap(); + state + .set_storage(&a, H256::from_low_u64_be(20), H256::from_low_u64_be(20)) + .unwrap(); assert_eq!(state.balance(&a).unwrap(), 8.into()); - assert_eq!(state.get_storage(&a, &10.into()).unwrap(), 10.into()); - assert_eq!(state.get_storage(&a, &20.into()).unwrap(), 20.into()); + assert_eq!( + state.get_storage(&a, &H256::from_low_u64_be(10)).unwrap(), + H256::from_low_u64_be(10) + ); + assert_eq!( + state.get_storage(&a, &H256::from_low_u64_be(20)).unwrap(), + H256::from_low_u64_be(20) + ); state.checkpoint(); // c2 state.new_contract(&b, 30.into(), 0.into(), vec![]); - state.set_storage(&a, 10.into(), 15.into()).unwrap(); + state + .set_storage(&a, H256::from_low_u64_be(10), H256::from_low_u64_be(10)) + .unwrap(); assert_eq!(state.balance(&b).unwrap(), 30.into()); assert!(state.code(&b).unwrap().is_empty()); state.revert_checkpoint(); // revert c2 assert_eq!(state.balance(&a).unwrap(), 8.into()); - assert_eq!(state.get_storage(&a, &10.into()).unwrap(), 10.into()); - assert_eq!(state.get_storage(&a, &20.into()).unwrap(), 20.into()); + assert_eq!( + state.get_storage(&a, &H256::from_low_u64_be(10)).unwrap(), + H256::from_low_u64_be(10) + ); + assert_eq!( + state.get_storage(&a, &H256::from_low_u64_be(20)).unwrap(), + H256::from_low_u64_be(20) + ); assert_eq!(state.balance(&b).unwrap(), 0.into()); assert!(state.code(&b).unwrap().is_empty()); assert_eq!(state.exist(&b).unwrap(), false); @@ -842,7 +859,10 @@ mod tests { state.revert_checkpoint(); // revert c1 assert_eq!(state.code(&a).unwrap(), vec![10u8, 20, 30, 40, 50]); assert_eq!(state.balance(&a).unwrap(), 10.into()); - assert_eq!(state.get_storage(&a, &10.into()).unwrap(), 10.into()); + assert_eq!( + state.get_storage(&a, &H256::from_low_u64_be(10)).unwrap(), + H256::from_low_u64_be(10) + ); state.commit().unwrap(); assert_eq!(orig_root, state.root); @@ -851,8 +871,8 @@ mod tests { #[test] fn get_account_proof() { let mut state = get_temp_state(); - let a: Address = 1000.into(); - let b: Address = 2000.into(); + let a = Address::from_low_u64_be(1000); + let b = Address::from_low_u64_be(2000); state.new_contract(&a, 5.into(), 0.into(), vec![10u8, 20, 30, 40, 50]); state.commit().unwrap(); @@ -872,28 +892,30 @@ mod tests { #[test] fn get_storage_proof() { let mut state = get_temp_state(); - let a: Address = 1000.into(); - let b: Address = 2000.into(); - let c: Address = 3000.into(); + let a = Address::from_low_u64_be(1000); + let b = Address::from_low_u64_be(2000); + let c = Address::from_low_u64_be(3000); state.new_contract(&a, 5.into(), 0.into(), vec![10u8, 20, 30, 40, 50]); - state.set_storage(&a, 10.into(), 10.into()).unwrap(); + state + .set_storage(&a, H256::from_low_u64_be(10), H256::from_low_u64_be(15)) + .unwrap(); state.new_contract(&b, 5.into(), 0.into(), vec![10u8, 20, 30, 40, 50]); state.commit().unwrap(); // account not exist - let proof = state.get_storage_proof(&c, &10.into()).unwrap(); + let proof = state.get_storage_proof(&c, &H256::from_low_u64_be(10)).unwrap(); assert_eq!(proof.len(), 0); // account who has empty storage trie - let proof = state.get_storage_proof(&b, &10.into()).unwrap(); + let proof = state.get_storage_proof(&b, &H256::from_low_u64_be(10)).unwrap(); assert_eq!(proof.len(), 0); // account and storage key exists - let proof1 = state.get_storage_proof(&a, &10.into()).unwrap(); + let proof1 = state.get_storage_proof(&a, &H256::from_low_u64_be(10)).unwrap(); assert_eq!(proof1.len(), 1); // account exists but storage key not exist - let proof2 = state.get_storage_proof(&a, &20.into()).unwrap(); + let proof2 = state.get_storage_proof(&a, &H256::from_low_u64_be(20)).unwrap(); assert_eq!(proof2.len(), 1); assert_eq!(proof1, proof2); @@ -911,6 +933,6 @@ mod tests { #[cfg(feature = "sm3hash")] let expected = "995b949869f80fa1465a9d8b6fa759ec65c3020d59c2624662bdff059bdf19b3"; - assert_eq!(state.root, expected.into()); + assert_eq!(state.root, H256::from_str(expected).unwrap()); } } diff --git a/tests/state_misc.rs b/tests/state_misc.rs index b2ac26a..07f69b2 100644 --- a/tests/state_misc.rs +++ b/tests/state_misc.rs @@ -1,4 +1,5 @@ use std::cell::RefCell; +use std::str::FromStr; use std::sync::Arc; use ethereum_types::{Address, U256}; @@ -11,13 +12,13 @@ fn test_state_misc00() { let mut state = cita_vm::state::State::new(db.clone()).unwrap(); state.new_contract( - &Address::from("0x2000000000000000000000000000000000000000"), + &Address::from_str("0x2000000000000000000000000000000000000000").unwrap(), U256::from(100_000), U256::from(1), hex::decode("").unwrap(), ); state.new_contract( - &Address::from("0x1000000000000000000000000000000000000000"), + &Address::from_str("0x1000000000000000000000000000000000000000").unwrap(), U256::from(200_000), U256::from(1), vec![], @@ -31,8 +32,8 @@ fn test_state_misc00() { let config = cita_vm::Config::default(); let tx = cita_vm::Transaction { - from: Address::from("0x1000000000000000000000000000000000000000"), - to: Some(Address::from("0x2000000000000000000000000000000000000000")), + from: Address::from_str("0x1000000000000000000000000000000000000000").unwrap(), + to: Some(Address::from_str("0x2000000000000000000000000000000000000000").unwrap()), value: U256::from(5), nonce: U256::from(1), gas_limit: 80000, @@ -51,13 +52,13 @@ fn test_state_misc00() { assert_eq!( state_data_provider .borrow_mut() - .balance(&Address::from("0x2000000000000000000000000000000000000000")) + .balance(&Address::from_str("0x2000000000000000000000000000000000000000").unwrap()) .unwrap(), U256::from(100_005) ); let mut ur_state = cita_vm::state::State::from_existing(db, root0).unwrap(); let b = ur_state - .balance(&Address::from("0x2000000000000000000000000000000000000000")) + .balance(&Address::from_str("0x2000000000000000000000000000000000000000").unwrap()) .unwrap(); assert_eq!(b, U256::from(100_000)); } diff --git a/tests/state_solidity_test.rs b/tests/state_solidity_test.rs index 13c49cd..94113ca 100644 --- a/tests/state_solidity_test.rs +++ b/tests/state_solidity_test.rs @@ -1,6 +1,7 @@ use ethereum_types::{Address, H256, U256}; use log::debug; use std::cell::RefCell; +use std::str::FromStr; use std::sync::Arc; #[test] @@ -17,13 +18,13 @@ fn test_solidity_simplestorage() { 99c66a25d59f0aa78f7ebc40748fa1d1fbc335d8d780f284841b30e0365acd9\ 60029"; state.new_contract( - &Address::from("0xBd770416a3345F91E4B34576cb804a576fa48EB1"), + &Address::from_str("0xBd770416a3345F91E4B34576cb804a576fa48EB1").unwrap(), U256::from(10), U256::from(1), hex::decode(code).unwrap(), ); state.new_contract( - &Address::from("0x1000000000000000000000000000000000000000"), + &Address::from_str("0x1000000000000000000000000000000000000000").unwrap(), U256::from(1_000_000_000_000_000u64), U256::from(1), vec![], @@ -36,8 +37,8 @@ fn test_solidity_simplestorage() { // SimpleStorage.set(42) let tx = cita_vm::Transaction { - from: Address::from("0x1000000000000000000000000000000000000000"), - to: Some(Address::from("0xBd770416a3345F91E4B34576cb804a576fa48EB1")), + from: Address::from_str("0x1000000000000000000000000000000000000000").unwrap(), + to: Some(Address::from_str("0xBd770416a3345F91E4B34576cb804a576fa48EB1").unwrap()), value: U256::from(0), nonce: U256::from(1), gas_limit: 80000, @@ -55,8 +56,8 @@ fn test_solidity_simplestorage() { // Send transaction: SimpleStorage.get() => 42 let tx = cita_vm::Transaction { - from: Address::from("0x1000000000000000000000000000000000000000"), - to: Some(Address::from("0xBd770416a3345F91E4B34576cb804a576fa48EB1")), + from: Address::from_str("0x1000000000000000000000000000000000000000").unwrap(), + to: Some(Address::from_str("0xBd770416a3345F91E4B34576cb804a576fa48EB1").unwrap()), value: U256::from(0), nonce: U256::from(2), gas_limit: 80000, @@ -83,8 +84,8 @@ fn test_solidity_simplestorage() { // Eth call: SimpleStorage.get() => 42 let tx = cita_vm::Transaction { - from: Address::from("0x1000000000000000000000000000000000000001"), // Omited in most cases. - to: Some(Address::from("0xBd770416a3345F91E4B34576cb804a576fa48EB1")), // tx.to shouldn't be none. + from: Address::from_str("0x1000000000000000000000000000000000000001").unwrap(), // Omited in most cases. + to: Some(Address::from_str("0xBd770416a3345F91E4B34576cb804a576fa48EB1").unwrap()), // tx.to shouldn't be none. value: U256::from(0), // tx.value must be 0. This is due to solidity's check. nonce: U256::from(123_456), // tx.nonce is just omited. gas_limit: 80000, // Give me a large enougth value plz. @@ -109,8 +110,8 @@ fn test_solidity_erc20() { let _ = env_logger::builder().is_test(true).try_init(); let db = Arc::new(cita_vm::state::MemoryDB::new(false)); let mut state = cita_vm::state::State::new(db).unwrap(); - let address0 = Address::from("0x1000000000000000000000000000000000000000"); - let address1 = Address::from("0x1000000000000000000000000000000000000001"); + let address0 = Address::from_str("0x1000000000000000000000000000000000000000").unwrap(); + let address1 = Address::from_str("0x1000000000000000000000000000000000000001").unwrap(); state.new_contract(&address0, U256::from(100_000_000_000_000_000u64), U256::from(1), vec![]); state.new_contract(&address1, U256::from(100_000_000_000_000_000u64), U256::from(1), vec![]); @@ -254,7 +255,7 @@ fn test_solidity_erc20() { let config = cita_vm::Config::default(); let tx = cita_vm::Transaction { - from: Address::from("0x1000000000000000000000000000000000000000"), + from: Address::from_str("0x1000000000000000000000000000000000000000").unwrap(), to: None, value: U256::from(0), nonce: U256::from(1), @@ -331,11 +332,11 @@ fn test_solidity_erc20() { assert_eq!(addr, &contract); assert_eq!( topics[1], - H256::from("0x0000000000000000000000001000000000000000000000000000000000000000") + H256::from_str("0x0000000000000000000000001000000000000000000000000000000000000000").unwrap() ); assert_eq!( topics[2], - H256::from("0x0000000000000000000000001000000000000000000000000000000000000001") + H256::from_str("0x0000000000000000000000001000000000000000000000000000000000000001").unwrap() ); assert_eq!( data,