@@ -50,3 +50,71 @@ func TestUpdateLeaks(t *testing.T) {
5050 t .Errorf ("State leaked into database: %x -> %x" , key , value )
5151 }
5252}
53+
54+ // Tests that no intermediate state of an object is stored into the database,
55+ // only the one right before the commit.
56+ func TestIntermediateLeaks (t * testing.T ) {
57+ // Create two state databases, one transitioning to the final state, the other final from the beginning
58+ transDb , _ := ethdb .NewMemDatabase ()
59+ finalDb , _ := ethdb .NewMemDatabase ()
60+ transState , _ := New (common.Hash {}, transDb )
61+ finalState , _ := New (common.Hash {}, finalDb )
62+
63+ // Update the states with some objects
64+ for i := byte (0 ); i < 255 ; i ++ {
65+ // Create a new state object with some data into the transition database
66+ obj := transState .GetOrNewStateObject (common .BytesToAddress ([]byte {i }))
67+ obj .SetBalance (big .NewInt (int64 (11 * i )))
68+ obj .SetNonce (uint64 (42 * i ))
69+ if i % 2 == 0 {
70+ obj .SetState (common .BytesToHash ([]byte {i , i , i , 0 }), common .BytesToHash ([]byte {i , i , i , i , 0 }))
71+ }
72+ if i % 3 == 0 {
73+ obj .SetCode ([]byte {i , i , i , i , i , 0 })
74+ }
75+ transState .UpdateStateObject (obj )
76+
77+ // Overwrite all the data with new values in the transition database
78+ obj .SetBalance (big .NewInt (int64 (11 * i + 1 )))
79+ obj .SetNonce (uint64 (42 * i + 1 ))
80+ if i % 2 == 0 {
81+ obj .SetState (common .BytesToHash ([]byte {i , i , i , 0 }), common.Hash {})
82+ obj .SetState (common .BytesToHash ([]byte {i , i , i , 1 }), common .BytesToHash ([]byte {i , i , i , i , 1 }))
83+ }
84+ if i % 3 == 0 {
85+ obj .SetCode ([]byte {i , i , i , i , i , 1 })
86+ }
87+ transState .UpdateStateObject (obj )
88+
89+ // Create the final state object directly in the final database
90+ obj = finalState .GetOrNewStateObject (common .BytesToAddress ([]byte {i }))
91+ obj .SetBalance (big .NewInt (int64 (11 * i + 1 )))
92+ obj .SetNonce (uint64 (42 * i + 1 ))
93+ if i % 2 == 0 {
94+ obj .SetState (common .BytesToHash ([]byte {i , i , i , 1 }), common .BytesToHash ([]byte {i , i , i , i , 1 }))
95+ }
96+ if i % 3 == 0 {
97+ obj .SetCode ([]byte {i , i , i , i , i , 1 })
98+ }
99+ finalState .UpdateStateObject (obj )
100+ }
101+ if _ , err := transState .Commit (); err != nil {
102+ t .Fatalf ("failed to commit transition state: %v" , err )
103+ }
104+ if _ , err := finalState .Commit (); err != nil {
105+ t .Fatalf ("failed to commit final state: %v" , err )
106+ }
107+ // Cross check the databases to ensure they are the same
108+ for _ , key := range finalDb .Keys () {
109+ if _ , err := transDb .Get (key ); err != nil {
110+ val , _ := finalDb .Get (key )
111+ t .Errorf ("entry missing from the transition database: %x -> %x" , key , val )
112+ }
113+ }
114+ for _ , key := range transDb .Keys () {
115+ if _ , err := finalDb .Get (key ); err != nil {
116+ val , _ := transDb .Get (key )
117+ t .Errorf ("extra entry in the transition database: %x -> %x" , key , val )
118+ }
119+ }
120+ }
0 commit comments