Created
April 14, 2020 09:10
-
-
Save adria0/83e1014959b2715fd5f98aba51f55b53 to your computer and use it in GitHub Desktop.
openethereum vs go-ethereum state calc snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type stateTest struct { | |
db ethdb.Database | |
state *StateDB | |
} | |
func newStateTest() *stateTest { | |
db := rawdb.NewMemoryDatabase() | |
sdb, _ := New(common.Hash{}, NewDatabase(db), nil) | |
return &stateTest{db: db, state: sdb} | |
} | |
assert := func(cond bool, err string) { | |
if !cond { | |
panic(err) | |
} | |
} | |
type AccountData struct { | |
address common.Address | |
balance *big.Int | |
nonce uint64 | |
code []byte | |
} | |
accounts := []AccountData{} | |
bytes, err := ioutil.ReadFile("/tmp/test_account") | |
assert(err == nil, "open file") | |
s := newStateTest() | |
for _, line := range strings.Split(string(bytes), "\n") { | |
line := strings.TrimSpace(line) | |
if len(line) > 0 { | |
addrNonceBalCode := strings.Split(line, " ") | |
fmt.Println("adding ", addrNonceBalCode) | |
assert(len(addrNonceBalCode) == 4, "bad len ") | |
assert(strings.HasPrefix(addrNonceBalCode[0], "0x"), "bad addr") | |
assert(strings.HasPrefix(addrNonceBalCode[3], "0x"), "bad code") | |
addr := common.HexToAddress(addrNonceBalCode[0]) | |
state := s.state.GetOrNewStateObject(addr) | |
nonce, err := strconv.Atoi(addrNonceBalCode[1]) | |
assert(err == nil, "bad nonce") | |
state.SetNonce(uint64(nonce)) | |
balance := new(big.Int) | |
balance.SetString(addrNonceBalCode[2], 10) | |
state.SetBalance(balance) | |
code := []byte{} | |
code = common.Hex2Bytes(addrNonceBalCode[3][2:]) | |
state.SetCode(crypto.Keccak256Hash(code), code) | |
accounts = append(accounts, AccountData{ | |
address: addr, | |
balance: balance, | |
nonce: uint64(nonce), | |
code: code, | |
}) | |
s.state.updateStateObject(state) | |
} | |
} | |
for _, acc := range accounts { | |
state := s.state.GetOrNewStateObject(acc.address) | |
state.SetNonce(uint64(acc.nonce)) | |
state.SetBalance(acc.balance) | |
state.SetCode(crypto.Keccak256Hash(acc.code), acc.code) | |
s.state.updateStateObject(state) | |
} | |
root, err := s.state.Commit(false) | |
assert(err == nil, "commit") | |
t.Fatalf("root=%v", root.Hex()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
println!("*********************"); | |
let db = test_helpers::new_db(); | |
let journal_db = journaldb::new(db.key_value().clone(), journaldb::Algorithm::Archive, ::db::COL_STATE); | |
let state_db = StateDB::new(journal_db, 1000000); | |
let mut state : account_state::State<StateDB> = account_state::State::new(state_db, U256::zero(), Default::default()); | |
let text = std::fs::read_to_string("/tmp/test_account").expect("cannot read file"); | |
for line in text.lines() { | |
let line = line.trim(); | |
if line.len() == 0 { | |
continue; | |
} | |
let acc_nonce_bal_code = line.split(" ").collect::<Vec<_>>(); | |
println!("adding {:?}",acc_nonce_bal_code); | |
if acc_nonce_bal_code.len() !=4 { | |
panic!("bad line"); | |
} | |
let acc = Address::from_str(&acc_nonce_bal_code[0][2..]).unwrap(); | |
let nonce = u64::from_str_radix(acc_nonce_bal_code[1],10).unwrap(); | |
(0..nonce).for_each(|_| state.inc_nonce(&acc).unwrap()); | |
let bal = U256::from_dec_str(acc_nonce_bal_code[2]).unwrap(); | |
state.add_balance(&acc, &bal, CleanupMode::ForceCreate).unwrap(); | |
if acc_nonce_bal_code.len() > 3 { | |
let code = (&acc_nonce_bal_code[3][2..]).from_hex().unwrap(); | |
state.reset_code(&acc, code).unwrap(); | |
} | |
} | |
state.commit().unwrap(); | |
println!("root={}",state.root()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0x0000000000000000000000000000000000000001 0 0 0x | |
0x0000000000000000000000000000000000000002 0 0 0x | |
0x0000000000000000000000000000000000000003 0 0 0x | |
0x0000000000000000000000000000000000000004 0 0 0x | |
0x0000000000000000000000000000000000000005 0 0 0x | |
0x0000000000000000000000000000000000000006 0 0 0x | |
0x0000000000000000000000000000000000000007 0 0 0x | |
0x0000000000000000000000000000000000000008 0 0 0x | |
0x1000000000000000000000000000000000000000 0 0 0x60006000600060006000365af1 | |
0x2000000000000000000000000000000000000000 0 0 0x6000600060006000365af4 | |
0x3000000000000000000000000000000000000000 0 0 0x60006000600060006000365af2 | |
0x4000000000000000000000000000000000000000 0 0 0x6000600060006000365afa | |
0x68795C4AA09D6f4Ed3E5DeDDf8c2AD3049A601da 0 2000000000000116977 0x | |
0xa94f5374Fce5edBC8E2a8697C15331677e6EbF0B 2 999999999999883023 0x | |
0xb94f5374fCe5EDbc8E2A8697c15331677E6EBf0B 0 0 0x60006000602035600060006000355af2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment