From efa11fe534f50c3f5bd5d579dbadf4924da8dfda Mon Sep 17 00:00:00 2001 From: Delweng Zheng Date: Sat, 29 Sep 2018 17:25:17 +0800 Subject: [PATCH] core/statedb: add testcase for copystorage --- core/state/statedb_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index cbd5bc75e7..33cc5120ee 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -435,3 +435,41 @@ func TestCopyOfCopy(t *testing.T) { t.Fatalf("2nd copy fail, expected 42, got %v", got) } } + +// TestCopyStorage tests that the preimages of an account's storage should be saved to trie between +// the copy of StateDB +// See https://github.com/ethereum/go-ethereum/pull/17785 +func TestCopyStorage(t *testing.T) { + db := NewDatabase(ethdb.NewMemDatabase()) + state1, _ := New(common.Hash{}, db) + + stateobjaddr := common.HexToAddress("zwb") + storage := map[common.Hash]common.Hash{ + common.BytesToHash([]byte{0}): common.BytesToHash([]byte{100}), + common.BytesToHash([]byte{1}): common.BytesToHash([]byte{101}), + common.BytesToHash([]byte{2}): common.BytesToHash([]byte{102}), + } + + for k, v := range storage { + state1.SetState(stateobjaddr, k, v) + } + + // DO what as the miner does + state1.Finalise(false) + state2 := state1.Copy() + state2.IntermediateRoot(false) + state2.Commit(false) + + dump := state2.RawDump() + + addr := strings.TrimPrefix(stateobjaddr.Hex(), "0x") + gotStorage := dump.Accounts[addr].Storage + if len(gotStorage) != len(storage) { + t.Fatalf("the number of storage not the same, expected(%d), got(%d)", len(storage), len(gotStorage)) + } + for k, v := range gotStorage { + if want, ok := storage[common.HexToHash(k)]; !ok || want != common.HexToHash(v) { + t.Fatalf("dump storage mismatched:\ngot: %s\nwant: %s\n", v, want) + } + } +}