diff --git a/cmd/evm/internal/t8ntool/transition.go b/cmd/evm/internal/t8ntool/transition.go index 89b703d3b805..98b3078c5d6c 100644 --- a/cmd/evm/internal/t8ntool/transition.go +++ b/cmd/evm/internal/t8ntool/transition.go @@ -613,10 +613,10 @@ func genBinTrieFromAlloc(alloc core.GenesisAlloc, db database.NodeDatabase, grou } } account := &types.StateAccount{ - Balance: uint256.MustFromBig(acc.Balance), - Nonce: acc.Nonce, - CodeHash: crypto.Keccak256Hash(acc.Code).Bytes(), - Root: common.Hash{}, + MntBalances: types.NewQKCTokenBalances(uint256.MustFromBig(acc.Balance)), + Nonce: acc.Nonce, + CodeHash: crypto.Keccak256Hash(acc.Code).Bytes(), + Root: common.Hash{}, } err := bt.UpdateAccount(addr, account, len(acc.Code)) if err != nil { diff --git a/cmd/geth/bintrie_convert_test.go b/cmd/geth/bintrie_convert_test.go index 32e8c7e55bfd..80d20388d5c8 100644 --- a/cmd/geth/bintrie_convert_test.go +++ b/cmd/geth/bintrie_convert_test.go @@ -114,8 +114,8 @@ func TestBintrieConvert(t *testing.T) { t.Errorf("account1 nonce: got %d, want 5", acc1.Nonce) } wantBal1 := uint256.NewInt(1000000) - if acc1.Balance.Cmp(wantBal1) != 0 { - t.Errorf("account1 balance: got %s, want %s", acc1.Balance, wantBal1) + if balance := acc1.GetBalance(); balance.Cmp(wantBal1) != 0 { + t.Errorf("account1 balance: got %s, want %s", balance, wantBal1) } acc2, err := bt2.GetAccount(addr2) @@ -129,8 +129,8 @@ func TestBintrieConvert(t *testing.T) { t.Errorf("account2 nonce: got %d, want 10", acc2.Nonce) } wantBal2 := uint256.NewInt(2000000) - if acc2.Balance.Cmp(wantBal2) != 0 { - t.Errorf("account2 balance: got %s, want %s", acc2.Balance, wantBal2) + if balance := acc2.GetBalance(); balance.Cmp(wantBal2) != 0 { + t.Errorf("account2 balance: got %s, want %s", balance, wantBal2) } treeKey1 := bintrie.GetBinaryTreeKeyStorageSlot(addr2, slotKey1[:]) @@ -222,8 +222,8 @@ func TestBintrieConvertDeleteSource(t *testing.T) { t.Fatal("account not found after MPT deletion") } wantBal := uint256.NewInt(1000000) - if acc.Balance.Cmp(wantBal) != 0 { - t.Errorf("balance after deletion: got %s, want %s", acc.Balance, wantBal) + if balance := acc.GetBalance(); balance.Cmp(wantBal) != 0 { + t.Errorf("balance after deletion: got %s, want %s", balance, wantBal) } destTriedb.Close() } diff --git a/cmd/geth/snapshot.go b/cmd/geth/snapshot.go index d168ee1d7dc0..3126712c11ca 100644 --- a/cmd/geth/snapshot.go +++ b/cmd/geth/snapshot.go @@ -717,7 +717,7 @@ func dumpState(ctx *cli.Context) error { return err } da := &state.DumpAccount{ - Balance: account.Balance.String(), + Balance: account.GetBalance().String(), Nonce: account.Nonce, Root: account.Root.Bytes(), CodeHash: account.CodeHash, diff --git a/core/state/database_history.go b/core/state/database_history.go index fbf4ab5f9c70..98832509f819 100644 --- a/core/state/database_history.go +++ b/core/state/database_history.go @@ -56,10 +56,10 @@ func (r *historicStateReader) Account(addr common.Address) (*types.StateAccount, return nil, nil } acct := &types.StateAccount{ - Nonce: account.Nonce, - Balance: account.Balance, - CodeHash: account.CodeHash, - Root: common.BytesToHash(account.Root), + Nonce: account.Nonce, + MntBalances: types.NewQKCTokenBalances(account.Balance), + CodeHash: account.CodeHash, + Root: common.BytesToHash(account.Root), } if len(acct.CodeHash) == 0 { acct.CodeHash = types.EmptyCodeHash.Bytes() diff --git a/core/state/database_iterator_test.go b/core/state/database_iterator_test.go index 8313f864038c..6be47480b996 100644 --- a/core/state/database_iterator_test.go +++ b/core/state/database_iterator_test.go @@ -102,8 +102,9 @@ func testAccountIterator(t *testing.T, scheme string) { if got.Nonce != acc.nonce { t.Fatalf("(%s) nonce %x: got %d, want %d", scheme, hash, got.Nonce, acc.nonce) } - if got.Balance.Cmp(acc.balance) != 0 { - t.Fatalf("(%s) balance %x: got %v, want %v", scheme, hash, got.Balance, acc.balance) + balance := got.GetBalance() + if balance.Cmp(acc.balance) != 0 { + t.Fatalf("(%s) balance %x: got %v, want %v", scheme, hash, balance, acc.balance) } // Verify address preimage resolution. addr, err := acctIt.Address() diff --git a/core/state/dump.go b/core/state/dump.go index cbf53de05333..3d1c875d0a23 100644 --- a/core/state/dump.go +++ b/core/state/dump.go @@ -151,7 +151,7 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey [] } var ( account = DumpAccount{ - Balance: data.Balance.String(), + Balance: data.GetBalance().String(), Nonce: data.Nonce, Root: data.Root[:], CodeHash: data.CodeHash, diff --git a/core/state/reader.go b/core/state/reader.go index be07cec0f97f..b6ab71027587 100644 --- a/core/state/reader.go +++ b/core/state/reader.go @@ -106,10 +106,10 @@ func (r *flatReader) Account(addr common.Address) (*types.StateAccount, error) { return nil, nil } acct := &types.StateAccount{ - Nonce: account.Nonce, - Balance: account.Balance, - CodeHash: account.CodeHash, - Root: common.BytesToHash(account.Root), + Nonce: account.Nonce, + MntBalances: types.NewQKCTokenBalances(account.Balance), + CodeHash: account.CodeHash, + Root: common.BytesToHash(account.Root), } if len(acct.CodeHash) == 0 { acct.CodeHash = types.EmptyCodeHash.Bytes() diff --git a/core/state/snapshot/generate_test.go b/core/state/snapshot/generate_test.go index 7fb4c152dca9..3f2377472815 100644 --- a/core/state/snapshot/generate_test.go +++ b/core/state/snapshot/generate_test.go @@ -54,9 +54,9 @@ func testGeneration(t *testing.T, scheme string) { var helper = newHelper(scheme) stRoot := helper.makeStorageTrie("", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, false) - helper.addTrieAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) - helper.addTrieAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(2), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) - helper.addTrieAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(3), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(2)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(3)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.makeStorageTrie("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) helper.makeStorageTrie("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) @@ -93,16 +93,16 @@ func testGenerateExistentState(t *testing.T, scheme string) { var helper = newHelper(scheme) stRoot := helper.makeStorageTrie("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addTrieAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) - helper.addSnapAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addSnapAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}) - helper.addTrieAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(2), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) - helper.addSnapAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(2), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(2)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addSnapAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(2)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) stRoot = helper.makeStorageTrie("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addTrieAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(3), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) - helper.addSnapAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(3), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(3)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addSnapAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(3)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}) root, snap := helper.CommitAndGenerate() @@ -274,28 +274,28 @@ func testGenerateExistentStateWithWrongStorage(t *testing.T, scheme string) { helper := newHelper(scheme) // Account one, empty root but non-empty database - helper.addAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}) // Account two, non empty root but empty database stRoot := helper.makeStorageTrie("acc-2", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // Miss slots { // Account three, non empty root but misses slots in the beginning helper.makeStorageTrie("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-3", []string{"key-2", "key-3"}, []string{"val-2", "val-3"}) // Account four, non empty root but misses slots in the middle helper.makeStorageTrie("acc-4", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-4", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-4", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-4", []string{"key-1", "key-3"}, []string{"val-1", "val-3"}) // Account five, non empty root but misses slots in the end helper.makeStorageTrie("acc-5", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-5", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-5", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-5", []string{"key-1", "key-2"}, []string{"val-1", "val-2"}) } @@ -303,22 +303,22 @@ func testGenerateExistentStateWithWrongStorage(t *testing.T, scheme string) { { // Account six, non empty root but wrong slots in the beginning helper.makeStorageTrie("acc-6", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-6", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-6", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-6", []string{"key-1", "key-2", "key-3"}, []string{"badval-1", "val-2", "val-3"}) // Account seven, non empty root but wrong slots in the middle helper.makeStorageTrie("acc-7", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-7", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-7", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-7", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "badval-2", "val-3"}) // Account eight, non empty root but wrong slots in the end helper.makeStorageTrie("acc-8", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-8", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-8", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-8", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "badval-3"}) // Account 9, non empty root but rotated slots helper.makeStorageTrie("acc-9", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-9", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-9", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-9", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-3", "val-2"}) } @@ -326,17 +326,17 @@ func testGenerateExistentStateWithWrongStorage(t *testing.T, scheme string) { { // Account 10, non empty root but extra slots in the beginning helper.makeStorageTrie("acc-10", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-10", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-10", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-10", []string{"key-0", "key-1", "key-2", "key-3"}, []string{"val-0", "val-1", "val-2", "val-3"}) // Account 11, non empty root but extra slots in the middle helper.makeStorageTrie("acc-11", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-11", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-11", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-11", []string{"key-1", "key-2", "key-2-1", "key-3"}, []string{"val-1", "val-2", "val-2-1", "val-3"}) // Account 12, non empty root but extra slots in the end helper.makeStorageTrie("acc-12", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-12", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-12", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-12", []string{"key-1", "key-2", "key-3", "key-4"}, []string{"val-1", "val-2", "val-3", "val-4"}) } @@ -381,25 +381,25 @@ func testGenerateExistentStateWithWrongAccounts(t *testing.T, scheme string) { // Missing accounts, only in the trie { - helper.addTrieAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // Beginning - helper.addTrieAccount("acc-4", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // Middle - helper.addTrieAccount("acc-6", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // End + helper.addTrieAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // Beginning + helper.addTrieAccount("acc-4", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // Middle + helper.addTrieAccount("acc-6", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // End } // Wrong accounts { - helper.addTrieAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) - helper.addSnapAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: common.Hex2Bytes("0x1234")}) + helper.addTrieAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addSnapAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: common.Hex2Bytes("0x1234")}) - helper.addTrieAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) - helper.addSnapAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addSnapAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) } // Extra accounts, only in the snap { - helper.addSnapAccount("acc-0", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // before the beginning - helper.addSnapAccount("acc-5", &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: common.Hex2Bytes("0x1234")}) // Middle - helper.addSnapAccount("acc-7", &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // after the end + helper.addSnapAccount("acc-0", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // before the beginning + helper.addSnapAccount("acc-5", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: common.Hex2Bytes("0x1234")}) // Middle + helper.addSnapAccount("acc-7", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // after the end } root, snap := helper.CommitAndGenerate() @@ -433,9 +433,9 @@ func testGenerateCorruptAccountTrie(t *testing.T, scheme string) { // without any storage slots to keep the test smaller. helper := newHelper(scheme) - helper.addTrieAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0xc7a30f39aff471c95d8a837497ad0e49b65be475cc0953540f80cfcdbdcd9074 - helper.addTrieAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(2), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7 - helper.addTrieAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(3), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x19ead688e907b0fab07176120dceec244a72aff2f0aa51e8b827584e378772f4 + helper.addTrieAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0xc7a30f39aff471c95d8a837497ad0e49b65be475cc0953540f80cfcdbdcd9074 + helper.addTrieAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(2)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7 + helper.addTrieAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(3)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x19ead688e907b0fab07176120dceec244a72aff2f0aa51e8b827584e378772f4 root := helper.Commit() // Root: 0xa04693ea110a31037fb5ee814308a6f1d76bdab0b11676bdf4541d2de55ba978 @@ -477,11 +477,11 @@ func testGenerateMissingStorageTrie(t *testing.T, scheme string) { acc3 = hashData([]byte("acc-3")) helper = newHelper(scheme) ) - stRoot := helper.makeStorageTrie("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) // 0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67 - helper.addTrieAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e - helper.addTrieAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(2), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7 + stRoot := helper.makeStorageTrie("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) // 0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67 + helper.addTrieAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e + helper.addTrieAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(2)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7 stRoot = helper.makeStorageTrie("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addTrieAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(3), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2 + helper.addTrieAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(3)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2 root := helper.Commit() @@ -517,11 +517,11 @@ func testGenerateCorruptStorageTrie(t *testing.T, scheme string) { // two of which also has the same 3-slot storage trie attached. helper := newHelper(scheme) - stRoot := helper.makeStorageTrie("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) // 0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67 - helper.addTrieAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e - helper.addTrieAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(2), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7 + stRoot := helper.makeStorageTrie("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) // 0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67 + helper.addTrieAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e + helper.addTrieAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(2)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7 stRoot = helper.makeStorageTrie("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addTrieAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(3), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2 + helper.addTrieAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(3)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2 root := helper.Commit() @@ -561,7 +561,7 @@ func testGenerateWithExtraAccounts(t *testing.T, scheme string) { []string{"val-1", "val-2", "val-3", "val-4", "val-5"}, true, ) - acc := &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()} + acc := &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()} val, _ := rlp.EncodeToBytes(acc) helper.accTrie.MustUpdate([]byte("acc-1"), val) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e @@ -581,7 +581,7 @@ func testGenerateWithExtraAccounts(t *testing.T, scheme string) { []string{"val-1", "val-2", "val-3", "val-4", "val-5"}, true, ) - acc := &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()} + acc := &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()} val, _ := rlp.EncodeToBytes(acc) key := hashData([]byte("acc-2")) rawdb.WriteAccountSnapshot(helper.diskdb, key, val) @@ -637,7 +637,7 @@ func testGenerateWithManyExtraAccounts(t *testing.T, scheme string) { []string{"val-1", "val-2", "val-3"}, true, ) - acc := &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()} + acc := &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()} val, _ := rlp.EncodeToBytes(acc) helper.accTrie.MustUpdate([]byte("acc-1"), val) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e @@ -651,7 +651,7 @@ func testGenerateWithManyExtraAccounts(t *testing.T, scheme string) { { // 100 accounts exist only in snapshot for i := 0; i < 1000; i++ { - acc := &types.StateAccount{Balance: uint256.NewInt(uint64(i)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()} + acc := &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(uint64(i))), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()} val, _ := rlp.EncodeToBytes(acc) key := hashData(fmt.Appendf(nil, "acc-%d", i)) rawdb.WriteAccountSnapshot(helper.diskdb, key, val) @@ -693,7 +693,7 @@ func testGenerateWithExtraBeforeAndAfter(t *testing.T, scheme string) { } helper := newHelper(scheme) { - acc := &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()} + acc := &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()} val, _ := rlp.EncodeToBytes(acc) helper.accTrie.MustUpdate(common.HexToHash("0x03").Bytes(), val) helper.accTrie.MustUpdate(common.HexToHash("0x07").Bytes(), val) @@ -735,7 +735,7 @@ func testGenerateWithMalformedSnapdata(t *testing.T, scheme string) { } helper := newHelper(scheme) { - acc := &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()} + acc := &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()} val, _ := rlp.EncodeToBytes(acc) helper.accTrie.MustUpdate(common.HexToHash("0x03").Bytes(), val) @@ -779,7 +779,7 @@ func testGenerateFromEmptySnap(t *testing.T, scheme string) { for i := 0; i < 400; i++ { stRoot := helper.makeStorageTrie(fmt.Sprintf("acc-%d", i), []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) helper.addTrieAccount(fmt.Sprintf("acc-%d", i), - &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) } root, snap := helper.CommitAndGenerate() t.Logf("Root: %#x\n", root) // Root: 0x6f7af6d2e1a1bf2b84a3beb3f8b64388465fbc1e274ca5d5d3fc787ca78f59e4 @@ -821,7 +821,7 @@ func testGenerateWithIncompleteStorage(t *testing.T, scheme string) { for i := 0; i < 8; i++ { accKey := fmt.Sprintf("acc-%d", i) stRoot := helper.makeStorageTrie(accKey, stKeys, stVals, true) - helper.addAccount(accKey, &types.StateAccount{Balance: uint256.NewInt(uint64(i)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount(accKey, &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(uint64(i))), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) var moddedKeys []string var moddedVals []string for ii := 0; ii < 8; ii++ { @@ -918,11 +918,11 @@ func testGenerateCompleteSnapshotWithDanglingStorage(t *testing.T, scheme string var helper = newHelper(scheme) stRoot := helper.makeStorageTrie("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) - helper.addAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) helper.makeStorageTrie("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}) helper.addSnapStorage("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}) @@ -958,11 +958,11 @@ func testGenerateBrokenSnapshotWithDanglingStorage(t *testing.T, scheme string) var helper = newHelper(scheme) stRoot := helper.makeStorageTrie("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addTrieAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) - helper.addTrieAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(2), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(2)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) helper.makeStorageTrie("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addTrieAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(3), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(3)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) populateDangling(helper.diskdb) diff --git a/core/state/snapshot/snapshot_test.go b/core/state/snapshot/snapshot_test.go index 34ef61e8d00a..b606d5268687 100644 --- a/core/state/snapshot/snapshot_test.go +++ b/core/state/snapshot/snapshot_test.go @@ -43,11 +43,11 @@ func randomHash() common.Hash { // randomAccount generates a random account and returns it RLP encoded. func randomAccount() []byte { - a := &types.StateAccount{ - Balance: uint256.NewInt(rand.Uint64()), - Nonce: rand.Uint64(), - Root: randomHash(), - CodeHash: types.EmptyCodeHash[:], + a := types.StateAccount{ + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(rand.Uint64())), + Nonce: rand.Uint64(), + Root: randomHash(), + CodeHash: types.EmptyCodeHash[:], } data, _ := rlp.EncodeToBytes(a) return data diff --git a/core/state/snapshot/utils.go b/core/state/snapshot/utils.go index c35c82f67a44..628cbfb4764b 100644 --- a/core/state/snapshot/utils.go +++ b/core/state/snapshot/utils.go @@ -103,7 +103,7 @@ func CheckJournalAccount(db ethdb.KeyValueStore, hash common.Hash) error { panic(err) } fmt.Printf("\taccount.nonce: %d\n", account.Nonce) - fmt.Printf("\taccount.balance: %x\n", account.Balance) + fmt.Printf("\taccount.balance: %x\n", account.GetBalance()) fmt.Printf("\taccount.root: %x\n", account.Root) fmt.Printf("\taccount.codehash: %x\n", account.CodeHash) } @@ -133,7 +133,7 @@ func CheckJournalAccount(db ethdb.KeyValueStore, hash common.Hash) error { panic(err) } fmt.Printf("\taccount.nonce: %d\n", account.Nonce) - fmt.Printf("\taccount.balance: %x\n", account.Balance) + fmt.Printf("\taccount.balance: %x\n", account.GetBalance()) fmt.Printf("\taccount.root: %x\n", account.Root) fmt.Printf("\taccount.codehash: %x\n", account.CodeHash) } diff --git a/core/state/state_object.go b/core/state/state_object.go index 8e72486825e2..c4c4676653cf 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -89,7 +89,7 @@ type stateObject struct { // empty returns whether the account is considered empty. func (s *stateObject) empty() bool { - return s.data.Nonce == 0 && s.data.Balance.IsZero() && bytes.Equal(s.data.CodeHash, types.EmptyCodeHash.Bytes()) + return s.data.Nonce == 0 && s.data.GetBalance().IsZero() && bytes.Equal(s.data.CodeHash, types.EmptyCodeHash.Bytes()) } // newObject creates a state object. @@ -489,14 +489,14 @@ func (s *stateObject) AddBalance(amount *uint256.Int) uint256.Int { // SetBalance sets the balance for the object, and returns the previous balance. func (s *stateObject) SetBalance(amount *uint256.Int) uint256.Int { - prev := *s.data.Balance - s.db.journal.balanceChange(s.address, s.data.Balance) + prev := *s.data.GetBalance() + s.db.journal.balanceChange(s.address, s.data.GetBalance()) s.setBalance(amount) return prev } func (s *stateObject) setBalance(amount *uint256.Int) { - s.data.Balance = amount + s.data.SetBalance(amount) } func (s *stateObject) deepCopy(db *StateDB) *stateObject { @@ -613,7 +613,7 @@ func (s *stateObject) CodeHash() []byte { } func (s *stateObject) Balance() *uint256.Int { - return s.data.Balance + return s.data.GetBalance() } func (s *stateObject) Nonce() uint64 { diff --git a/core/types/gen_account_rlp.go b/core/types/gen_account_rlp.go deleted file mode 100644 index 8b424493afb8..000000000000 --- a/core/types/gen_account_rlp.go +++ /dev/null @@ -1,21 +0,0 @@ -// Code generated by rlpgen. DO NOT EDIT. - -package types - -import "github.com/ethereum/go-ethereum/rlp" -import "io" - -func (obj *StateAccount) EncodeRLP(_w io.Writer) error { - w := rlp.NewEncoderBuffer(_w) - _tmp0 := w.List() - w.WriteUint64(obj.Nonce) - if obj.Balance == nil { - w.Write(rlp.EmptyString) - } else { - w.WriteUint256(obj.Balance) - } - w.WriteBytes(obj.Root[:]) - w.WriteBytes(obj.CodeHash) - w.ListEnd(_tmp0) - return w.Flush() -} diff --git a/core/types/state_account.go b/core/types/state_account.go index 52ef843b3527..caed8f96f650 100644 --- a/core/types/state_account.go +++ b/core/types/state_account.go @@ -20,47 +20,52 @@ import ( "bytes" "github.com/ethereum/go-ethereum/common" + qkccommon "github.com/ethereum/go-ethereum/qkc/common" "github.com/ethereum/go-ethereum/rlp" "github.com/holiman/uint256" ) -//go:generate go run ../../rlp/rlpgen -type StateAccount -out gen_account_rlp.go - -// StateAccount is the Ethereum consensus representation of accounts. -// These objects are stored in the main account trie. +// StateAccount is the QuarkChain consensus representation of accounts. +// It uses the codec in state_account_qkc.go because the standard four-field +// Ethereum codec cannot encode its token and shard fields. type StateAccount struct { - Nonce uint64 - Balance *uint256.Int - Root common.Hash // merkle root of the storage trie - CodeHash []byte + Nonce uint64 + MntBalances *qkccommon.TokenBalances // QKC and MNT balances. + Root common.Hash // merkle root of the storage trie + CodeHash []byte + FullShardKey uint32 } // NewEmptyStateAccount constructs an empty state account. func NewEmptyStateAccount() *StateAccount { return &StateAccount{ - Balance: new(uint256.Int), - Root: EmptyRootHash, - CodeHash: EmptyCodeHash.Bytes(), + MntBalances: qkccommon.NewEmptyTokenBalances(), + Root: EmptyRootHash, + CodeHash: EmptyCodeHash.Bytes(), } } // Copy returns a deep-copied state account object. func (acct *StateAccount) Copy() *StateAccount { - var balance *uint256.Int - if acct.Balance != nil { - balance = new(uint256.Int).Set(acct.Balance) + var mntBalances *qkccommon.TokenBalances + if acct.MntBalances != nil { + mntBalances = acct.MntBalances.Copy() } return &StateAccount{ - Nonce: acct.Nonce, - Balance: balance, - Root: acct.Root, - CodeHash: common.CopyBytes(acct.CodeHash), + Nonce: acct.Nonce, + MntBalances: mntBalances, + Root: acct.Root, + CodeHash: common.CopyBytes(acct.CodeHash), + FullShardKey: acct.FullShardKey, } } -// SlimAccount is a modified version of an Account, where the root is replaced -// with a byte slice. This format can be used to represent full-consensus format -// or slim format which replaces the empty root and code hash as nil byte slice. +// SlimAccount is retained unchanged for the inherited snapshot and pathdb code. +// Goshard currently supports neither snap sync nor the snapshot database flat +// reader, so this representation intentionally carries only the QKC balance. +// Converting between SlimAccount and StateAccount therefore loses MNT balances +// and FullShardKey. This is a known issue which is ignored until those modes are +// supported; they must not be enabled before the conversion becomes lossless. type SlimAccount struct { Nonce uint64 Balance *uint256.Int @@ -72,7 +77,7 @@ type SlimAccount struct { func SlimAccountRLP(account StateAccount) []byte { slim := SlimAccount{ Nonce: account.Nonce, - Balance: account.Balance, + Balance: account.GetBalance(), } if account.Root != EmptyRootHash { slim.Root = account.Root[:] @@ -94,8 +99,10 @@ func FullAccount(data []byte) (*StateAccount, error) { if err := rlp.DecodeBytes(data, &slim); err != nil { return nil, err } - var account StateAccount - account.Nonce, account.Balance = slim.Nonce, slim.Balance + account := StateAccount{ + Nonce: slim.Nonce, + MntBalances: NewQKCTokenBalances(slim.Balance), + } // Interpret the storage root and code hash in slim format. if len(slim.Root) == 0 { diff --git a/core/types/state_account_qkc.go b/core/types/state_account_qkc.go new file mode 100644 index 000000000000..0e27a069ff86 --- /dev/null +++ b/core/types/state_account_qkc.go @@ -0,0 +1,97 @@ +// Copyright 2026-2027, QuarkChain. + +package types + +import ( + "errors" + "io" + + "github.com/ethereum/go-ethereum/common" + qkccommon "github.com/ethereum/go-ethereum/qkc/common" + "github.com/ethereum/go-ethereum/rlp" + "github.com/holiman/uint256" +) + +// qkcAccountRLP matches pyquarkchain's six-field _Account encoding. +type qkcAccountRLP struct { + Nonce uint64 + TokenBal []byte + Root common.Hash + CodeHash []byte + FullShardKey qkccommon.Uint32 + Optional []byte +} + +// GetMntBalance returns the balance of tokenID in the account's unified balance map. +func (acct *StateAccount) GetMntBalance(tokenID uint64) *uint256.Int { + if acct.MntBalances == nil { + return new(uint256.Int) + } + return acct.MntBalances.GetTokenBalance(tokenID) +} + +// GetBalance returns the account's QKC balance. +func (acct *StateAccount) GetBalance() *uint256.Int { + return acct.GetMntBalance(qkccommon.DefaultTokenID) +} + +// SetBalance sets the account's QKC balance. +func (acct *StateAccount) SetBalance(balance *uint256.Int) { + if acct.MntBalances == nil { + acct.MntBalances = qkccommon.NewEmptyTokenBalances() + } + acct.MntBalances.SetValue(balance, qkccommon.DefaultTokenID) +} + +// NewQKCTokenBalances creates an account balance map with a QKC balance. +func NewQKCTokenBalances(balance *uint256.Int) *qkccommon.TokenBalances { + balances := qkccommon.NewEmptyTokenBalances() + if balance != nil { + balances.SetValue(balance, qkccommon.DefaultTokenID) + } + return balances +} + +// EncodeRLP implements pyquarkchain's account encoding. +func (acct *StateAccount) EncodeRLP(w io.Writer) error { + balances := acct.MntBalances + if balances == nil { + balances = qkccommon.NewEmptyTokenBalances() + } + tokenBal, err := balances.SerializeToBytes() + if err != nil { + return err + } + return rlp.Encode(w, &qkcAccountRLP{ + Nonce: acct.Nonce, + TokenBal: tokenBal, + Root: acct.Root, + CodeHash: acct.CodeHash, + FullShardKey: qkccommon.Uint32(acct.FullShardKey), + }) +} + +// DecodeRLP implements pyquarkchain's account decoding. +func (acct *StateAccount) DecodeRLP(s *rlp.Stream) error { + raw, err := s.Raw() + if err != nil { + return err + } + var wire qkcAccountRLP + if err := rlp.DecodeBytes(raw, &wire); err != nil { + return err + } + if len(wire.Optional) != 0 { + return errors.New("unsupported non-empty QuarkChain account optional field") + } + acct.Nonce = wire.Nonce + acct.Root = wire.Root + acct.CodeHash = wire.CodeHash + acct.FullShardKey = uint32(wire.FullShardKey) + balances, err := qkccommon.NewTokenBalances(wire.TokenBal) + if err != nil { + return err + } + acct.MntBalances = balances + return nil +} diff --git a/core/types/state_account_test.go b/core/types/state_account_test.go new file mode 100644 index 000000000000..bcec1e6557bc --- /dev/null +++ b/core/types/state_account_test.go @@ -0,0 +1,180 @@ +// Copyright 2026-2027, QuarkChain. + +package types + +import ( + "encoding/hex" + "testing" + + "github.com/ethereum/go-ethereum/common" + qkccommon "github.com/ethereum/go-ethereum/qkc/common" + "github.com/ethereum/go-ethereum/rlp" + "github.com/holiman/uint256" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +var ( + pyqkcAccountQKC = decodeStateAccountHex("f853018900c7c6828bb08203e8a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470840000000180") + pyqkcAccountMNT = decodeStateAccountHex("f858058e00ccc4648201f4c6828bb08207d0a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470840000000180") + pyqkcAccountZero = decodeStateAccountHex("f84a8080a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470840000000180") +) + +func decodeStateAccountHex(input string) []byte { + data, err := hex.DecodeString(input) + if err != nil { + panic(err) + } + return data +} + +func TestStateAccountPyquarkchainEncoding(t *testing.T) { + tests := []struct { + name string + acct StateAccount + want []byte + }{ + { + name: "QKC balance", + acct: StateAccount{ + Nonce: 1, + MntBalances: qkccommon.NewTokenBalancesWithMap(map[uint64]*uint256.Int{ + qkccommon.DefaultTokenID: uint256.NewInt(1000), + }), + Root: EmptyRootHash, CodeHash: EmptyCodeHash[:], FullShardKey: 1, + }, + want: pyqkcAccountQKC, + }, + { + name: "QKC and MNT balances", + acct: StateAccount{ + Nonce: 5, + MntBalances: qkccommon.NewTokenBalancesWithMap(map[uint64]*uint256.Int{ + 100: uint256.NewInt(500), + qkccommon.DefaultTokenID: uint256.NewInt(2000), + }), + Root: EmptyRootHash, + CodeHash: EmptyCodeHash[:], + FullShardKey: 1, + }, + want: pyqkcAccountMNT, + }, + { + name: "zero account", + acct: StateAccount{MntBalances: qkccommon.NewEmptyTokenBalances(), Root: EmptyRootHash, CodeHash: EmptyCodeHash[:], FullShardKey: 1}, + want: pyqkcAccountZero, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got, err := rlp.EncodeToBytes(&test.acct) + require.NoError(t, err) + assert.Equal(t, test.want, got) + }) + } +} + +func TestStateAccountPyquarkchainDecoding(t *testing.T) { + tests := []struct { + name string + input []byte + nonce uint64 + balances map[uint64]*uint256.Int + }{ + {name: "QKC balance", input: pyqkcAccountQKC, nonce: 1, balances: map[uint64]*uint256.Int{qkccommon.DefaultTokenID: uint256.NewInt(1000)}}, + {name: "QKC and MNT balances", input: pyqkcAccountMNT, nonce: 5, balances: map[uint64]*uint256.Int{100: uint256.NewInt(500), qkccommon.DefaultTokenID: uint256.NewInt(2000)}}, + {name: "zero account", input: pyqkcAccountZero, balances: map[uint64]*uint256.Int{}}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + var acct StateAccount + require.NoError(t, rlp.DecodeBytes(test.input, &acct)) + assert.Equal(t, test.nonce, acct.Nonce) + assert.Equal(t, EmptyRootHash, acct.Root) + assert.Equal(t, EmptyCodeHash[:], acct.CodeHash) + assert.Equal(t, uint32(1), acct.FullShardKey) + if assert.NotNil(t, acct.MntBalances) { + assert.Equal(t, test.balances, acct.MntBalances.GetBalanceMap()) + } + }) + } +} + +func TestStateAccountExplicitZeroBalanceEncoding(t *testing.T) { + acct := NewEmptyStateAccount() + encoded, err := rlp.EncodeToBytes(acct) + require.NoError(t, err) + var wire qkcAccountRLP + require.NoError(t, rlp.DecodeBytes(encoded, &wire)) + assert.Empty(t, wire.TokenBal) + + previous := acct.MntBalances.GetTokenBalance(qkccommon.DefaultTokenID) + acct.MntBalances.SetValue(uint256.NewInt(1), qkccommon.DefaultTokenID) + acct.MntBalances.SetValue(previous, qkccommon.DefaultTokenID) + encoded, err = rlp.EncodeToBytes(acct) + require.NoError(t, err) + require.NoError(t, rlp.DecodeBytes(encoded, &wire)) + assert.Equal(t, []byte{0x00, 0xc0}, wire.TokenBal) +} + +func TestStateAccountCopyMntBalances(t *testing.T) { + original := StateAccount{ + MntBalances: qkccommon.NewTokenBalancesWithMap(map[uint64]*uint256.Int{ + 100: uint256.NewInt(2), + qkccommon.DefaultTokenID: uint256.NewInt(1), + }), + FullShardKey: 3, + } + copied := original.Copy() + copied.MntBalances.SetValue(uint256.NewInt(4), 100) + copied.MntBalances.SetValue(uint256.NewInt(5), qkccommon.DefaultTokenID) + + assert.Equal(t, uint256.NewInt(2), original.MntBalances.GetTokenBalance(100)) + assert.Equal(t, uint256.NewInt(4), copied.MntBalances.GetTokenBalance(100)) + assert.Equal(t, uint256.NewInt(1), original.MntBalances.GetTokenBalance(qkccommon.DefaultTokenID)) + assert.Equal(t, uint256.NewInt(5), copied.MntBalances.GetTokenBalance(qkccommon.DefaultTokenID)) + assert.Equal(t, original.FullShardKey, copied.FullShardKey) +} + +func TestStateAccountBalance(t *testing.T) { + acct := StateAccount{MntBalances: NewQKCTokenBalances(uint256.NewInt(42))} + if got := acct.GetBalance(); got.Cmp(uint256.NewInt(42)) != 0 { + t.Fatalf("QKC balance mismatch: have %v, want 42", got) + } + acct.SetBalance(uint256.NewInt(43)) + if got := acct.GetBalance(); got.Cmp(uint256.NewInt(43)) != 0 { + t.Fatalf("updated QKC balance mismatch: have %v, want 43", got) + } + acct.SetBalance(nil) + if got := acct.GetBalance(); !got.IsZero() { + t.Fatalf("nil QKC balance should set zero, have %v", got) + } + newAcct := &StateAccount{} + newAcct.SetBalance(uint256.NewInt(7)) + if got := newAcct.GetBalance(); got.Cmp(uint256.NewInt(7)) != 0 { + t.Fatalf("QKC balance on nil map mismatch: have %v, want 7", got) + } + if got := (&StateAccount{}).GetBalance(); !got.IsZero() { + t.Fatalf("nil balance map should return zero, have %v", got) + } +} + +func TestStateAccountRejectsUnsupportedQKCEncoding(t *testing.T) { + tests := []struct { + name string + wire qkcAccountRLP + want string + }{ + {name: "token trie", wire: qkcAccountRLP{TokenBal: append([]byte{1}, make([]byte, common.HashLength)...), Root: EmptyRootHash}, want: "trie"}, + {name: "unknown token encoding", wire: qkcAccountRLP{TokenBal: []byte{2}, Root: EmptyRootHash}, want: "enum byte"}, + {name: "optional field", wire: qkcAccountRLP{Root: EmptyRootHash, Optional: []byte{1}}, want: "optional field"}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + encoded, err := rlp.EncodeToBytes(&test.wire) + require.NoError(t, err) + var acct StateAccount + assert.ErrorContains(t, rlp.DecodeBytes(encoded, &acct), test.want) + }) + } +} diff --git a/eth/protocols/snap/sync_test.go b/eth/protocols/snap/sync_test.go index c506488e91c1..a60b3908c06f 100644 --- a/eth/protocols/snap/sync_test.go +++ b/eth/protocols/snap/sync_test.go @@ -1500,10 +1500,10 @@ func makeAccountTrieNoStorage(n int, scheme string) (string, *trie.Trie, []*kv) ) for i := uint64(1); i <= uint64(n); i++ { value, _ := rlp.EncodeToBytes(&types.StateAccount{ - Nonce: i, - Balance: uint256.NewInt(i), - Root: types.EmptyRootHash, - CodeHash: getCodeHash(i), + Nonce: i, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(i)), + Root: types.EmptyRootHash, + CodeHash: getCodeHash(i), }) key := key32(i) elem := &kv{key, value} @@ -1551,10 +1551,10 @@ func makeBoundaryAccountTrie(scheme string, n int) (string, *trie.Trie, []*kv) { // Fill boundary accounts for i := 0; i < len(boundaries); i++ { value, _ := rlp.EncodeToBytes(&types.StateAccount{ - Nonce: uint64(0), - Balance: uint256.NewInt(uint64(i)), - Root: types.EmptyRootHash, - CodeHash: getCodeHash(uint64(i)), + Nonce: 0, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(uint64(i))), + Root: types.EmptyRootHash, + CodeHash: getCodeHash(uint64(i)), }) elem := &kv{boundaries[i].Bytes(), value} accTrie.MustUpdate(elem.k, elem.v) @@ -1563,10 +1563,10 @@ func makeBoundaryAccountTrie(scheme string, n int) (string, *trie.Trie, []*kv) { // Fill other accounts if required for i := uint64(1); i <= uint64(n); i++ { value, _ := rlp.EncodeToBytes(&types.StateAccount{ - Nonce: i, - Balance: uint256.NewInt(i), - Root: types.EmptyRootHash, - CodeHash: getCodeHash(i), + Nonce: i, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(i)), + Root: types.EmptyRootHash, + CodeHash: getCodeHash(i), }) elem := &kv{key32(i), value} accTrie.MustUpdate(elem.k, elem.v) @@ -1607,10 +1607,10 @@ func makeAccountTrieWithStorageWithUniqueStorage(scheme string, accounts, slots nodes.Merge(stNodes) value, _ := rlp.EncodeToBytes(&types.StateAccount{ - Nonce: i, - Balance: uint256.NewInt(i), - Root: stRoot, - CodeHash: codehash, + Nonce: i, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(i)), + Root: stRoot, + CodeHash: codehash, }) elem := &kv{key, value} accTrie.MustUpdate(elem.k, elem.v) @@ -1673,10 +1673,10 @@ func makeAccountTrieWithStorage(scheme string, accounts, slots int, code, bounda nodes.Merge(stNodes) value, _ := rlp.EncodeToBytes(&types.StateAccount{ - Nonce: i, - Balance: uint256.NewInt(i), - Root: stRoot, - CodeHash: codehash, + Nonce: i, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(i)), + Root: stRoot, + CodeHash: codehash, }) elem := &kv{key, value} accTrie.MustUpdate(elem.k, elem.v) diff --git a/trie/bintrie/trie.go b/trie/bintrie/trie.go index e3436e3df1bf..7cc3b4ad78ae 100644 --- a/trie/bintrie/trie.go +++ b/trie/bintrie/trie.go @@ -219,7 +219,7 @@ func (t *BinaryTrie) GetAccount(addr common.Address) (*types.StateAccount, error acc.Nonce = binary.BigEndian.Uint64(values[BasicDataLeafKey][BasicDataNonceOffset:]) var balance [16]byte copy(balance[:], values[BasicDataLeafKey][BasicDataBalanceOffset:]) - acc.Balance = new(uint256.Int).SetBytes(balance[:]) + acc.SetBalance(new(uint256.Int).SetBytes(balance[:])) acc.CodeHash = values[CodeHashLeafKey] return acc, nil @@ -245,7 +245,7 @@ func (t *BinaryTrie) UpdateAccount(addr common.Address, acc *types.StateAccount, // Because the balance is a max of 16 bytes, truncate // the extra values. This happens in devmode, where // 0xff**HashSize is allocated to the developer account. - balanceBytes := acc.Balance.Bytes() + balanceBytes := acc.GetBalance().Bytes() // TODO: reduce the size of the allocation in devmode, then panic instead // of truncating. if len(balanceBytes) > 16 { diff --git a/trie/bintrie/trie_test.go b/trie/bintrie/trie_test.go index 8b7d9e46d607..22b29cf4eb02 100644 --- a/trie/bintrie/trie_test.go +++ b/trie/bintrie/trie_test.go @@ -196,9 +196,9 @@ func TestStorageRoundTrip(t *testing.T) { // Create an account first so the root becomes an InternalNode, // which is the realistic state when storage operations happen. acc := &types.StateAccount{ - Nonce: 1, - Balance: uint256.NewInt(1000), - CodeHash: common.HexToHash("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").Bytes(), + Nonce: 1, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1000)), + CodeHash: common.HexToHash("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").Bytes(), } if err := tr.UpdateAccount(addr, acc, 0); err != nil { t.Fatalf("UpdateAccount error: %v", err) @@ -265,9 +265,9 @@ func newEmptyTestTrie(t *testing.T) *BinaryTrie { // zeroed out because the bintrie has no per-account storage root. func makeAccount(nonce uint64, balance uint64, codeHash common.Hash) *types.StateAccount { return &types.StateAccount{ - Nonce: nonce, - Balance: uint256.NewInt(balance), - CodeHash: codeHash.Bytes(), + Nonce: nonce, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(balance)), + CodeHash: codeHash.Bytes(), } } @@ -295,8 +295,8 @@ func TestDeleteAccountRoundTrip(t *testing.T) { if got.Nonce != 42 { t.Fatalf("Nonce: got %d, want 42", got.Nonce) } - if got.Balance.Uint64() != 1000 { - t.Fatalf("Balance: got %s, want 1000", got.Balance) + if balance := got.GetBalance(); balance.Uint64() != 1000 { + t.Fatalf("Balance: got %s, want 1000", balance) } if !bytes.Equal(got.CodeHash, codeHash[:]) { t.Fatalf("CodeHash: got %x, want %x", got.CodeHash, codeHash) @@ -375,8 +375,8 @@ func TestDeleteAccountPreservesOtherAccounts(t *testing.T) { if got.Nonce != 2 { t.Fatalf("Account B Nonce: got %d, want 2", got.Nonce) } - if got.Balance.Uint64() != 200 { - t.Fatalf("Account B Balance: got %s, want 200", got.Balance) + if balance := got.GetBalance(); balance.Uint64() != 200 { + t.Fatalf("Account B Balance: got %s, want 200", balance) } if !bytes.Equal(got.CodeHash, codeHashB[:]) { t.Fatalf("Account B CodeHash: got %x, want %x", got.CodeHash, codeHashB) @@ -415,8 +415,8 @@ func TestDeleteAccountThenRecreate(t *testing.T) { if got.Nonce != 7 { t.Fatalf("Nonce: got %d, want 7", got.Nonce) } - if got.Balance.Uint64() != 9999 { - t.Fatalf("Balance: got %s, want 9999", got.Balance) + if balance := got.GetBalance(); balance.Uint64() != 9999 { + t.Fatalf("Balance: got %s, want 9999", balance) } if !bytes.Equal(got.CodeHash, codeHash2[:]) { t.Fatalf("CodeHash: got %x, want %x", got.CodeHash, codeHash2) @@ -612,9 +612,9 @@ func testAccount(t *testing.T, addr common.Address, nonce uint64, balance uint64 tracer: trie.NewPrevalueTracer(), } acc := &types.StateAccount{ - Nonce: nonce, - Balance: uint256.NewInt(balance), - CodeHash: types.EmptyCodeHash[:], + Nonce: nonce, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(balance)), + CodeHash: types.EmptyCodeHash[:], } if err := tr.UpdateAccount(addr, acc, 0); err != nil { t.Fatalf("UpdateAccount error: %v", err) @@ -642,7 +642,7 @@ func TestGetAccountNonMembershipStemRoot(t *testing.T) { t.Fatalf("GetAccount error: %v", err) } if got != nil { - t.Fatalf("expected nil for non-existent account, got nonce=%d balance=%s", got.Nonce, got.Balance) + t.Fatalf("expected nil for non-existent account, got nonce=%d balance=%s", got.Nonce, got.GetBalance()) } // Original account must still be retrievable. @@ -672,9 +672,9 @@ func TestGetAccountNonMembershipInternalRoot(t *testing.T) { addr2 := common.HexToAddress("0x9999999999999999999999999999999999999999") for _, addr := range []common.Address{addr1, addr2} { acc := &types.StateAccount{ - Nonce: 1, - Balance: uint256.NewInt(1), - CodeHash: types.EmptyCodeHash[:], + Nonce: 1, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), + CodeHash: types.EmptyCodeHash[:], } if err := tr.UpdateAccount(addr, acc, 0); err != nil { t.Fatalf("UpdateAccount error: %v", err) @@ -731,9 +731,9 @@ func TestGetStorageNonMembershipInternalRoot(t *testing.T) { addr := common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678") acc := &types.StateAccount{ - Nonce: 1, - Balance: uint256.NewInt(1000), - CodeHash: types.EmptyCodeHash[:], + Nonce: 1, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1000)), + CodeHash: types.EmptyCodeHash[:], } if err := tr.UpdateAccount(addr, acc, 0); err != nil { t.Fatalf("UpdateAccount error: %v", err) diff --git a/trie/inspect_test.go b/trie/inspect_test.go index c07904c52d8b..f37733f15c85 100644 --- a/trie/inspect_test.go +++ b/trie/inspect_test.go @@ -182,10 +182,10 @@ func TestInspectContract(t *testing.T) { // Build the account trie with the contract account. account := types.StateAccount{ - Nonce: 1, - Balance: uint256.NewInt(1000), - Root: storageRoot, - CodeHash: crypto.Keccak256(nil), + Nonce: 1, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1000)), + Root: storageRoot, + CodeHash: crypto.Keccak256(nil), } accountRLP, err := rlp.EncodeToBytes(&account) if err != nil { @@ -245,10 +245,10 @@ func makeAccountsWithStorage(db *testDb, size int, storage bool) (addresses [][2 random.Read(balanceBytes) balance := new(uint256.Int).SetBytes(balanceBytes) data, _ := rlp.EncodeToBytes(&types.StateAccount{ - Nonce: nonce, - Balance: balance, - Root: root, - CodeHash: code, + Nonce: nonce, + MntBalances: types.NewQKCTokenBalances(balance), + Root: root, + CodeHash: code, }) accounts[i] = data } diff --git a/trie/trie_test.go b/trie/trie_test.go index 3661933e2281..9cc983fe96b5 100644 --- a/trie/trie_test.go +++ b/trie/trie_test.go @@ -803,10 +803,10 @@ func makeAccounts(size int) (addresses [][20]byte, accounts [][]byte) { random.Read(balanceBytes) balance := new(uint256.Int).SetBytes(balanceBytes) data, _ := rlp.EncodeToBytes(&types.StateAccount{ - Nonce: nonce, - Balance: balance, - Root: root, - CodeHash: code, + Nonce: nonce, + MntBalances: types.NewQKCTokenBalances(balance), + Root: root, + CodeHash: code, }) accounts[i] = data } diff --git a/triedb/generate_test.go b/triedb/generate_test.go index 42bccd9aa393..9c03d582e434 100644 --- a/triedb/generate_test.go +++ b/triedb/generate_test.go @@ -86,19 +86,19 @@ func TestGenerateTrieAccountsOnly(t *testing.T) { { hash: common.HexToHash("0x01"), account: types.StateAccount{ - Nonce: 1, - Balance: uint256.NewInt(100), - Root: types.EmptyRootHash, - CodeHash: types.EmptyCodeHash.Bytes(), + Nonce: 1, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(100)), + Root: types.EmptyRootHash, + CodeHash: types.EmptyCodeHash.Bytes(), }, }, { hash: common.HexToHash("0x02"), account: types.StateAccount{ - Nonce: 2, - Balance: uint256.NewInt(200), - Root: types.EmptyRootHash, - CodeHash: types.EmptyCodeHash.Bytes(), + Nonce: 2, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(200)), + Root: types.EmptyRootHash, + CodeHash: types.EmptyCodeHash.Bytes(), }, }, } @@ -125,20 +125,20 @@ func TestGenerateTrieWithStorage(t *testing.T) { { hash: common.HexToHash("0x01"), account: types.StateAccount{ - Nonce: 1, - Balance: uint256.NewInt(100), - Root: storageRoot, - CodeHash: types.EmptyCodeHash.Bytes(), + Nonce: 1, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(100)), + Root: storageRoot, + CodeHash: types.EmptyCodeHash.Bytes(), }, storage: slots, }, { hash: common.HexToHash("0x02"), account: types.StateAccount{ - Nonce: 0, - Balance: uint256.NewInt(50), - Root: types.EmptyRootHash, - CodeHash: types.EmptyCodeHash.Bytes(), + Nonce: 0, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(50)), + Root: types.EmptyRootHash, + CodeHash: types.EmptyCodeHash.Bytes(), }, }, } @@ -163,10 +163,10 @@ func TestGenerateTrieRootMismatch(t *testing.T) { db := rawdb.NewMemoryDatabase() acct := types.StateAccount{ - Nonce: 1, - Balance: uint256.NewInt(100), - Root: types.EmptyRootHash, - CodeHash: types.EmptyCodeHash.Bytes(), + Nonce: 1, + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(100)), + Root: types.EmptyRootHash, + CodeHash: types.EmptyCodeHash.Bytes(), } rawdb.WriteAccountSnapshot(db, common.HexToHash("0x01"), types.SlimAccountRLP(acct)) diff --git a/triedb/pathdb/database_test.go b/triedb/pathdb/database_test.go index 41212dc9d079..1ff968c70027 100644 --- a/triedb/pathdb/database_test.go +++ b/triedb/pathdb/database_test.go @@ -66,10 +66,10 @@ func updateTrie(db *Database, stateRoot common.Hash, addrHash common.Hash, root func generateAccount(storageRoot common.Hash) types.StateAccount { return types.StateAccount{ - Nonce: uint64(rand.Intn(100)), - Balance: uint256.NewInt(rand.Uint64()), - CodeHash: testrand.Bytes(32), - Root: storageRoot, + Nonce: uint64(rand.Intn(100)), + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(rand.Uint64())), + CodeHash: testrand.Bytes(32), + Root: storageRoot, } } diff --git a/triedb/pathdb/generate_test.go b/triedb/pathdb/generate_test.go index f38a1ed7c43a..1a0b16a63331 100644 --- a/triedb/pathdb/generate_test.go +++ b/triedb/pathdb/generate_test.go @@ -139,9 +139,9 @@ func TestGeneration(t *testing.T) { helper := newGenTester() stRoot := helper.makeStorageTrie("", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, false) - helper.addTrieAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) - helper.addTrieAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(2), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) - helper.addTrieAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(3), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(2)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(3)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.makeStorageTrie("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) helper.makeStorageTrie("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) @@ -185,28 +185,28 @@ func TestGenerateExistentStateWithWrongStorage(t *testing.T) { helper := newGenTester() // Account one, empty storage trie root but non-empty flat states - helper.addAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}) // Account two, non-empty storage trie root but empty flat states stRoot := helper.makeStorageTrie("acc-2", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // Miss slots { // Account three, non-empty root but misses slots in the beginning helper.makeStorageTrie("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-3", []string{"key-2", "key-3"}, []string{"val-2", "val-3"}) // Account four, non-empty root but misses slots in the middle helper.makeStorageTrie("acc-4", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-4", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-4", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-4", []string{"key-1", "key-3"}, []string{"val-1", "val-3"}) // Account five, non-empty root but misses slots in the end helper.makeStorageTrie("acc-5", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-5", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-5", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-5", []string{"key-1", "key-2"}, []string{"val-1", "val-2"}) } @@ -214,22 +214,22 @@ func TestGenerateExistentStateWithWrongStorage(t *testing.T) { { // Account six, non-empty root but wrong slots in the beginning helper.makeStorageTrie("acc-6", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-6", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-6", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-6", []string{"key-1", "key-2", "key-3"}, []string{"badval-1", "val-2", "val-3"}) // Account seven, non-empty root but wrong slots in the middle helper.makeStorageTrie("acc-7", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-7", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-7", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-7", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "badval-2", "val-3"}) // Account eight, non-empty root but wrong slots in the end helper.makeStorageTrie("acc-8", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-8", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-8", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-8", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "badval-3"}) // Account 9, non-empty root but rotated slots helper.makeStorageTrie("acc-9", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-9", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-9", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-9", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-3", "val-2"}) } @@ -237,17 +237,17 @@ func TestGenerateExistentStateWithWrongStorage(t *testing.T) { { // Account 10, non-empty root but extra slots in the beginning helper.makeStorageTrie("acc-10", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-10", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-10", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-10", []string{"key-0", "key-1", "key-2", "key-3"}, []string{"val-0", "val-1", "val-2", "val-3"}) // Account 11, non-empty root but extra slots in the middle helper.makeStorageTrie("acc-11", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-11", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-11", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-11", []string{"key-1", "key-2", "key-2-1", "key-3"}, []string{"val-1", "val-2", "val-2-1", "val-3"}) // Account 12, non-empty root but extra slots in the end helper.makeStorageTrie("acc-12", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-12", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-12", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-12", []string{"key-1", "key-2", "key-3", "key-4"}, []string{"val-1", "val-2", "val-3", "val-4"}) } @@ -285,25 +285,25 @@ func TestGenerateExistentStateWithWrongAccounts(t *testing.T) { // Missing accounts, only in the trie { - helper.addTrieAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // Beginning - helper.addTrieAccount("acc-4", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // Middle - helper.addTrieAccount("acc-6", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // End + helper.addTrieAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // Beginning + helper.addTrieAccount("acc-4", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // Middle + helper.addTrieAccount("acc-6", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // End } // Wrong accounts { - helper.addTrieAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) - helper.addSnapAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: common.Hex2Bytes("0x1234")}) + helper.addTrieAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addSnapAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: common.Hex2Bytes("0x1234")}) - helper.addTrieAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) - helper.addSnapAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addSnapAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) } // Extra accounts, only in the snap { - helper.addSnapAccount("acc-0", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // before the beginning - helper.addSnapAccount("acc-5", &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: common.Hex2Bytes("0x1234")}) // Middle - helper.addSnapAccount("acc-7", &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // after the end + helper.addSnapAccount("acc-0", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // before the beginning + helper.addSnapAccount("acc-5", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: common.Hex2Bytes("0x1234")}) // Middle + helper.addSnapAccount("acc-7", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // after the end } root, dl := helper.CommitAndGenerate() @@ -325,9 +325,9 @@ func TestGenerateExistentStateWithWrongAccounts(t *testing.T) { func TestGenerateCorruptAccountTrie(t *testing.T) { helper := newGenTester() - helper.addTrieAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0xc7a30f39aff471c95d8a837497ad0e49b65be475cc0953540f80cfcdbdcd9074 - helper.addTrieAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(2), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7 - helper.addTrieAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(3), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x19ead688e907b0fab07176120dceec244a72aff2f0aa51e8b827584e378772f4 + helper.addTrieAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0xc7a30f39aff471c95d8a837497ad0e49b65be475cc0953540f80cfcdbdcd9074 + helper.addTrieAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(2)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7 + helper.addTrieAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(3)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x19ead688e907b0fab07176120dceec244a72aff2f0aa51e8b827584e378772f4 root := helper.Commit() // Root: 0xa04693ea110a31037fb5ee814308a6f1d76bdab0b11676bdf4541d2de55ba978 @@ -358,11 +358,11 @@ func TestGenerateMissingStorageTrie(t *testing.T) { acc3 = hashData([]byte("acc-3")) helper = newGenTester() ) - stRoot := helper.makeStorageTrie("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) // 0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67 - helper.addTrieAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e - helper.addTrieAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(2), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7 + stRoot := helper.makeStorageTrie("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) // 0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67 + helper.addTrieAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e + helper.addTrieAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(2)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7 stRoot = helper.makeStorageTrie("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addTrieAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(3), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2 + helper.addTrieAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(3)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2 root := helper.Commit() @@ -387,11 +387,11 @@ func TestGenerateMissingStorageTrie(t *testing.T) { func TestGenerateCorruptStorageTrie(t *testing.T) { helper := newGenTester() - stRoot := helper.makeStorageTrie("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) // 0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67 - helper.addTrieAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e - helper.addTrieAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(2), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7 + stRoot := helper.makeStorageTrie("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) // 0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67 + helper.addTrieAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e + helper.addTrieAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(2)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7 stRoot = helper.makeStorageTrie("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addTrieAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(3), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2 + helper.addTrieAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(3)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2 root := helper.Commit() @@ -431,7 +431,7 @@ func TestGenerateWithExtraAccounts(t *testing.T) { []string{"val-1", "val-2", "val-3", "val-4", "val-5"}, true, ) - acc := &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()} + acc := &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()} val, _ := rlp.EncodeToBytes(acc) helper.acctTrie.MustUpdate(hashData([]byte("acc-1")).Bytes(), val) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e @@ -450,7 +450,7 @@ func TestGenerateWithExtraAccounts(t *testing.T) { []string{"val-1", "val-2", "val-3", "val-4", "val-5"}, true, ) - acc = &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()} + acc = &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()} val, _ = rlp.EncodeToBytes(acc) key = hashData([]byte("acc-2")) rawdb.WriteAccountSnapshot(helper.diskdb, key, val) @@ -493,7 +493,7 @@ func TestGenerateWithManyExtraAccounts(t *testing.T) { []string{"val-1", "val-2", "val-3"}, true, ) - acc := &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()} + acc := &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()} val, _ := rlp.EncodeToBytes(acc) helper.acctTrie.MustUpdate(hashData([]byte("acc-1")).Bytes(), val) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e @@ -506,7 +506,7 @@ func TestGenerateWithManyExtraAccounts(t *testing.T) { // 100 accounts exist only in snapshot for i := 0; i < 1000; i++ { - acc := &types.StateAccount{Balance: uint256.NewInt(uint64(i)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()} + acc := &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(uint64(i))), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()} val, _ := rlp.EncodeToBytes(acc) key := hashData([]byte(fmt.Sprintf("acc-%d", i))) rawdb.WriteAccountSnapshot(helper.diskdb, key, val) @@ -530,7 +530,7 @@ func TestGenerateWithManyExtraAccounts(t *testing.T) { func TestGenerateWithExtraBeforeAndAfter(t *testing.T) { helper := newGenTester() - acc := &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()} + acc := &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()} val, _ := rlp.EncodeToBytes(acc) acctHashA := hashData([]byte("acc-1")) @@ -564,7 +564,7 @@ func TestGenerateWithMalformedStateData(t *testing.T) { helper := newGenTester() acctHash := hashData([]byte("acc")) - acc := &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()} + acc := &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()} val, _ := rlp.EncodeToBytes(acc) helper.acctTrie.MustUpdate(acctHash.Bytes(), val) @@ -595,7 +595,7 @@ func TestGenerateFromEmptySnap(t *testing.T) { for i := 0; i < 400; i++ { stRoot := helper.makeStorageTrie(fmt.Sprintf("acc-%d", i), []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addTrieAccount(fmt.Sprintf("acc-%d", i), &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount(fmt.Sprintf("acc-%d", i), &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) } root, snap := helper.CommitAndGenerate() t.Logf("Root: %#x\n", root) // Root: 0x6f7af6d2e1a1bf2b84a3beb3f8b64388465fbc1e274ca5d5d3fc787ca78f59e4 @@ -625,7 +625,7 @@ func TestGenerateWithIncompleteStorage(t *testing.T) { for i := 0; i < 8; i++ { accKey := fmt.Sprintf("acc-%d", i) stRoot := helper.makeStorageTrie(accKey, stKeys, stVals, true) - helper.addAccount(accKey, &types.StateAccount{Balance: uint256.NewInt(uint64(i)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount(accKey, &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(uint64(i))), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) var moddedKeys []string var moddedVals []string for ii := 0; ii < 8; ii++ { @@ -713,11 +713,11 @@ func TestGenerateCompleteSnapshotWithDanglingStorage(t *testing.T) { var helper = newGenTester() stRoot := helper.makeStorageTrie("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) - helper.addAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(1), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) helper.makeStorageTrie("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) helper.addSnapStorage("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}) helper.addSnapStorage("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}) @@ -743,11 +743,11 @@ func TestGenerateBrokenSnapshotWithDanglingStorage(t *testing.T) { var helper = newGenTester() stRoot := helper.makeStorageTrie("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addTrieAccount("acc-1", &types.StateAccount{Balance: uint256.NewInt(1), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) - helper.addTrieAccount("acc-2", &types.StateAccount{Balance: uint256.NewInt(2), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-1", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(1)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-2", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(2)), Root: types.EmptyRootHash, CodeHash: types.EmptyCodeHash.Bytes()}) helper.makeStorageTrie("acc-3", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"}, true) - helper.addTrieAccount("acc-3", &types.StateAccount{Balance: uint256.NewInt(3), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) + helper.addTrieAccount("acc-3", &types.StateAccount{MntBalances: types.NewQKCTokenBalances(uint256.NewInt(3)), Root: stRoot, CodeHash: types.EmptyCodeHash.Bytes()}) populateDangling(helper.diskdb) diff --git a/triedb/pathdb/iterator_test.go b/triedb/pathdb/iterator_test.go index 191c2fadf593..aa4fa712a172 100644 --- a/triedb/pathdb/iterator_test.go +++ b/triedb/pathdb/iterator_test.go @@ -71,10 +71,10 @@ func verifyIterator(t *testing.T, expCount int, it Iterator, verify verifyConten // randomAccount generates a random account and returns it RLP encoded. func randomAccount() []byte { a := &types.StateAccount{ - Balance: uint256.NewInt(rand.Uint64()), - Nonce: rand.Uint64(), - Root: testrand.Hash(), - CodeHash: types.EmptyCodeHash[:], + MntBalances: types.NewQKCTokenBalances(uint256.NewInt(rand.Uint64())), + Nonce: rand.Uint64(), + Root: testrand.Hash(), + CodeHash: types.EmptyCodeHash[:], } data, _ := rlp.EncodeToBytes(a) return data