@@ -27,6 +27,33 @@ import (
2727 "github.com/ethereum/go-ethereum/ethdb/memorydb"
2828)
2929
30+ func copyDestructs (destructs map [common.Hash ]struct {}) map [common.Hash ]struct {} {
31+ copy := make (map [common.Hash ]struct {})
32+ for hash := range destructs {
33+ copy [hash ] = struct {}{}
34+ }
35+ return copy
36+ }
37+
38+ func copyAccounts (accounts map [common.Hash ][]byte ) map [common.Hash ][]byte {
39+ copy := make (map [common.Hash ][]byte )
40+ for hash , blob := range accounts {
41+ copy [hash ] = blob
42+ }
43+ return copy
44+ }
45+
46+ func copyStorage (storage map [common.Hash ]map [common.Hash ][]byte ) map [common.Hash ]map [common.Hash ][]byte {
47+ copy := make (map [common.Hash ]map [common.Hash ][]byte )
48+ for accHash , slots := range storage {
49+ copy [accHash ] = make (map [common.Hash ][]byte )
50+ for slotHash , blob := range slots {
51+ copy [accHash ][slotHash ] = blob
52+ }
53+ }
54+ return copy
55+ }
56+
3057// TestMergeBasics tests some simple merges
3158func TestMergeBasics (t * testing.T ) {
3259 var (
@@ -52,41 +79,41 @@ func TestMergeBasics(t *testing.T) {
5279 }
5380 }
5481 // Add some (identical) layers on top
55- parent := newDiffLayer (emptyLayer (), common.Hash {}, destructs , accounts , storage )
56- child := newDiffLayer (parent , common.Hash {}, destructs , accounts , storage )
57- child = newDiffLayer (child , common.Hash {}, destructs , accounts , storage )
58- child = newDiffLayer (child , common.Hash {}, destructs , accounts , storage )
59- child = newDiffLayer (child , common.Hash {}, destructs , accounts , storage )
82+ parent := newDiffLayer (emptyLayer (), common.Hash {}, copyDestructs ( destructs ), copyAccounts ( accounts ), copyStorage ( storage ) )
83+ child := newDiffLayer (parent , common.Hash {}, copyDestructs ( destructs ), copyAccounts ( accounts ), copyStorage ( storage ) )
84+ child = newDiffLayer (child , common.Hash {}, copyDestructs ( destructs ), copyAccounts ( accounts ), copyStorage ( storage ) )
85+ child = newDiffLayer (child , common.Hash {}, copyDestructs ( destructs ), copyAccounts ( accounts ), copyStorage ( storage ) )
86+ child = newDiffLayer (child , common.Hash {}, copyDestructs ( destructs ), copyAccounts ( accounts ), copyStorage ( storage ) )
6087 // And flatten
6188 merged := (child .flatten ()).(* diffLayer )
6289
6390 { // Check account lists
64- if got , exp := len (merged .accountList ), 0 ; got != exp {
65- t .Errorf ("accountList wrong, got %v exp %v" , got , exp )
91+ if have , want := len (merged .accountList ), 0 ; have != want {
92+ t .Errorf ("accountList wrong: have %v, want %v" , have , want )
6693 }
67- if got , exp := len (merged .AccountList ()), len (accounts ); got != exp {
68- t .Errorf ("AccountList() wrong, got %v exp %v" , got , exp )
94+ if have , want := len (merged .AccountList ()), len (accounts ); have != want {
95+ t .Errorf ("AccountList() wrong: have %v, want %v" , have , want )
6996 }
70- if got , exp := len (merged .accountList ), len (accounts ); got != exp {
71- t .Errorf ("accountList [2] wrong, got %v exp %v" , got , exp )
97+ if have , want := len (merged .accountList ), len (accounts ); have != want {
98+ t .Errorf ("accountList [2] wrong: have %v, want %v" , have , want )
7299 }
73100 }
74101 { // Check account drops
75- if got , exp := len (merged .destructSet ), len (destructs ); got != exp {
76- t .Errorf ("accountDrop wrong, got %v exp %v" , got , exp )
102+ if have , want := len (merged .destructSet ), len (destructs ); have != want {
103+ t .Errorf ("accountDrop wrong: have %v, want %v" , have , want )
77104 }
78105 }
79106 { // Check storage lists
80107 i := 0
81108 for aHash , sMap := range storage {
82- if got , exp := len (merged .storageList ), i ; got != exp {
83- t .Errorf ("[1] storageList wrong, got %v exp %v" , got , exp )
109+ if have , want := len (merged .storageList ), i ; have != want {
110+ t .Errorf ("[1] storageList wrong: have %v, want %v" , have , want )
84111 }
85- if got , exp := len (merged .StorageList (aHash )), len (sMap ); got != exp {
86- t .Errorf ("[2] StorageList() wrong, got %v exp %v" , got , exp )
112+ if have , want := len (merged .StorageList (aHash )), len (sMap ); have != want {
113+ t .Errorf ("[2] StorageList() wrong: have %v, want %v" , have , want )
87114 }
88- if got , exp := len (merged .storageList [aHash ]), len (sMap ); got != exp {
89- t .Errorf ("storageList wrong, got %v exp %v" , got , exp )
115+ if have , want := len (merged .storageList [aHash ]), len (sMap ); have != want {
116+ t .Errorf ("storageList wrong: have %v, want %v" , have , want )
90117 }
91118 i ++
92119 }
@@ -160,8 +187,8 @@ func TestMergeDelete(t *testing.T) {
160187 }
161188 // If we add more granular metering of memory, we can enable this again,
162189 // but it's not implemented for now
163- //if got, exp := merged.memory, child.memory; got != exp {
164- // t.Errorf("mem wrong, got %d, exp %d", got, exp )
190+ //if have, want := merged.memory, child.memory; have != want {
191+ // t.Errorf("mem wrong: have %d, want %d", have, want )
165192 //}
166193}
167194
@@ -197,9 +224,9 @@ func TestInsertAndMerge(t *testing.T) {
197224 // And flatten
198225 merged := (child .flatten ()).(* diffLayer )
199226 { // Check that slot value is present
200- got , _ := merged .Storage (acc , slot )
201- if exp := []byte {0x01 }; ! bytes .Equal (got , exp ) {
202- t .Errorf ("merged slot value wrong, got %x, exp %x" , got , exp )
227+ have , _ := merged .Storage (acc , slot )
228+ if want := []byte {0x01 }; ! bytes .Equal (have , want ) {
229+ t .Errorf ("merged slot value wrong: have %x, want %x" , have , want )
203230 }
204231 }
205232}
0 commit comments