11package state
22
33import (
4+ "github.com/crytic/medusa/chain/state/cache"
45 "testing"
56
67 "github.com/crytic/medusa-geth/common"
7- "github.com/crytic/medusa-geth/core/rawdb"
88 gethstate "github.com/crytic/medusa-geth/core/state"
99 "github.com/crytic/medusa-geth/core/tracing"
1010 "github.com/crytic/medusa-geth/core/types"
11- "github.com/crytic/medusa-geth/triedb"
12- "github.com/crytic/medusa/chain/state/cache"
1311 types2 "github.com/crytic/medusa/chain/types"
1412 "github.com/holiman/uint256"
1513 "github.com/stretchr/testify/assert"
1614)
1715
18- /* TestForkedStateDB provides unit testing for medusa-geth's ForkedStateDb */
16+ // TestForkedStateDB provides unit testing for medusa-geth's ForkedStateDb
1917func TestForkedStateDB (t * testing.T ) {
2018 fixture := newPrePopulatedBackendFixture ()
2119 factory := NewForkedStateFactory (fixture .Backend )
2220
23- db := rawdb .NewMemoryDatabase ()
24- tdb := triedb .NewDatabase (db , nil )
25- cachingDb := gethstate .NewDatabaseWithNodeDB (db , tdb )
21+ cachingDb := gethstate .NewDatabaseForTesting ()
2622
2723 stateDb1 , err := factory .New (types .EmptyRootHash , cachingDb )
2824 assert .NoError (t , err )
2925 genesisSnap := stateDb1 .Snapshot ()
3026
31- /* ensure the statedb is hitting the backend */
27+ // ensure the statedb is hitting the backend
3228 assert .True (t , stateDb1 .Exist (fixture .StateObjectContractAddress ))
3329 assert .True (t , stateDb1 .Exist (fixture .StateObjectEOAAddress ))
3430 assert .False (t , stateDb1 .Exist (fixture .StateObjectEmptyAddress ))
3531
3632 fixture .verifyAgainstState (t , stateDb1 )
3733
38- /* write some new data and make sure it's readable */
34+ // write some new data and make sure it's readable
3935 newAccount := common .BytesToAddress ([]byte {1 , 2 , 3 , 4 , 5 , 6 })
4036 newAccountData := cache.StateObject {
4137 Balance : uint256 .NewInt (5 ),
4238 Nonce : 99 ,
4339 Code : []byte {1 , 2 , 3 },
4440 }
4541
46- stateDb1 .SetNonce (newAccount , newAccountData .Nonce )
42+ stateDb1 .SetNonce (newAccount , newAccountData .Nonce , tracing . NonceChangeUnspecified )
4743 assert .True (t , stateDb1 .Exist (newAccount ))
4844 stateDb1 .SetCode (newAccount , newAccountData .Code )
4945 stateDb1 .SetBalance (newAccount , newAccountData .Balance , tracing .BalanceChangeUnspecified )
5046 checkAccountAgainstFixture (t , stateDb1 , newAccount , newAccountData )
5147
52- /* roll back to snapshot, ensure fork data still queryable and newly added data was purged */
48+ // roll back to snapshot, ensure fork data still queryable and newly added data was purged
5349 stateDb1 .Snapshot ()
5450 stateDb1 .RevertToSnapshot (genesisSnap )
5551 fixture .verifyAgainstState (t , stateDb1 )
5652 assert .False (t , stateDb1 .Exist (newAccount ))
5753
58- /* now we want to test to verify our fork-populated data is being persisted */
59- root , err := stateDb1 .Commit (1 , true )
54+ // now we want to test to verify our fork-populated data is being persisted
55+ root , err := stateDb1 .Commit (1 , true , true )
6056 assert .NoError (t , err )
6157 stateDb2 , err := factory .New (root , cachingDb )
6258 assert .NoError (t , err )
6359
6460 fixture .verifyAgainstState (t , stateDb2 )
6561}
6662
67- /*
68- TestForkedStateFactory verifies the various independence/shared properties of each forkedStateDB created by the
69- factory. This is because the underlying RPC/caching layer is shared between all TestChain instances globally,
70- but this sharing relationship should not cause state to leak from one forkedStateDb to another.
71- */
63+ // TestForkedStateFactory verifies the various independence/shared properties of each forkedStateDB created by the
64+ // factory. This is because the underlying RPC/caching layer is shared between all TestChain instances globally,
65+ // but this sharing relationship should not cause state to leak from one forkedStateDb to another.
7266func TestForkedStateFactory (t * testing.T ) {
7367 fixture := newPrePopulatedBackendFixture ()
7468 factory := NewForkedStateFactory (fixture .Backend )
@@ -81,7 +75,7 @@ func TestForkedStateFactory(t *testing.T) {
8175 assert .NoError (t , err )
8276 stateDb2 .Snapshot ()
8377
84- /* naive check to ensure they're both pulling from the same remote */
78+ // naive check to ensure they're both pulling from the same remote
8579 fixture .verifyAgainstState (t , stateDb1 )
8680 fixture .verifyAgainstState (t , stateDb2 )
8781
@@ -91,7 +85,7 @@ func TestForkedStateFactory(t *testing.T) {
9185 stateDb1 .RevertToSnapshot (0 )
9286 stateDb2 .RevertToSnapshot (0 )
9387
94- /* now we'll mutate a cold account in one stateDB and ensure the mutation doesn't propagate */
88+ // now we'll mutate a cold account in one stateDB and ensure the mutation doesn't propagate
9589 valueAdded := uint256 .NewInt (100 )
9690 expectedSum := uint256 .NewInt (0 ).Add (fixture .StateObjectEOA .Balance , valueAdded )
9791 stateDb1 .AddBalance (fixture .StateObjectEOAAddress , valueAdded , tracing .BalanceChangeUnspecified )
@@ -108,10 +102,8 @@ func TestForkedStateFactory(t *testing.T) {
108102 bal = stateDb3 .GetBalance (fixture .StateObjectEOAAddress )
109103 assert .Equal (t , bal , fixture .StateObjectEOA .Balance )
110104
111- /*
112- now we'll emulate one stateDB obtaining a new piece of data from RPC and ensuring the other stateDB loads
113- the same data
114- */
105+ // now we'll emulate one stateDB obtaining a new piece of data from RPC and ensuring the other stateDB loads
106+ // the same data
115107 newAccount := common .BytesToAddress ([]byte {1 , 2 , 3 , 4 , 5 , 6 })
116108 slotKey := common .BytesToHash ([]byte {5 , 5 , 5 , 5 , 5 , 5 , 5 })
117109 slotData := common .BytesToHash ([]byte {6 , 6 , 6 , 6 , 6 , 6 , 6 })
@@ -127,10 +119,8 @@ func TestForkedStateFactory(t *testing.T) {
127119 assert .EqualValues (t , slotData , data )
128120}
129121
130- /*
131- TestEmptyBackendFactoryDifferential tests the differential properties between a stateDB using an empty forked backend
132- versus directly using geth's statedb.
133- */
122+ // TestEmptyBackendFactoryDifferential tests the differential properties between a stateDB using an empty forked backend
123+ // versus directly using geth's statedb.
134124func TestEmptyBackendFactoryDifferential (t * testing.T ) {
135125 gethFactory := & gethStateFactory {}
136126 unbackedFactory := NewUnbackedStateFactory ()
@@ -141,14 +131,14 @@ func TestEmptyBackendFactoryDifferential(t *testing.T) {
141131 unbackedStateDb , err := createEmptyStateDb (unbackedFactory )
142132 assert .NoError (t , err )
143133
144- /* start with existence/empty of an existing object. should be identical. */
134+ // start with existence/empty of an existing object. should be identical.
145135 addr := common .BytesToAddress ([]byte {1 })
146- gethStateDb .SetNonce (addr , 5 )
147- unbackedStateDb .SetNonce (addr , 5 )
136+ gethStateDb .SetNonce (addr , 5 , tracing . NonceChangeUnspecified )
137+ unbackedStateDb .SetNonce (addr , 5 , tracing . NonceChangeUnspecified )
148138 assert .EqualValues (t , gethStateDb .Exist (addr ), unbackedStateDb .Exist (addr ))
149139 assert .EqualValues (t , gethStateDb .Empty (addr ), unbackedStateDb .Empty (addr ))
150140
151- /* existence/empty of a non-existing object, should be identical. */
141+ // existence/empty of a non-existing object, should be identical.
152142 nonExistentStateObjAddr := common .BytesToAddress ([]byte {5 , 5 , 5 , 5 , 5 })
153143 assert .EqualValues (t , gethStateDb .Exist (nonExistentStateObjAddr ), unbackedStateDb .Exist (nonExistentStateObjAddr ))
154144 assert .EqualValues (t , gethStateDb .Empty (nonExistentStateObjAddr ), unbackedStateDb .Empty (nonExistentStateObjAddr ))
@@ -158,19 +148,17 @@ func TestEmptyBackendFactoryDifferential(t *testing.T) {
158148 gethStateDb .SetBalance (emptyStateObjectAddr , value , tracing .BalanceChangeUnspecified )
159149 unbackedStateDb .SetBalance (emptyStateObjectAddr , value , tracing .BalanceChangeUnspecified )
160150
161- /* existence/empty of an empty object, should be identical. */
151+ // existence/empty of an empty object, should be identical.
162152 gethStateDb .SubBalance (emptyStateObjectAddr , value , tracing .BalanceChangeUnspecified )
163153 unbackedStateDb .SubBalance (emptyStateObjectAddr , value , tracing .BalanceChangeUnspecified )
164154 assert .EqualValues (t , gethStateDb .Exist (emptyStateObjectAddr ), unbackedStateDb .Exist (emptyStateObjectAddr ))
165155 assert .EqualValues (t , gethStateDb .Empty (emptyStateObjectAddr ), unbackedStateDb .Empty (emptyStateObjectAddr ))
166156}
167157
168- /*
169- TestForkedBackendDifferential tests the differential properties between a stateDB using a forked backend
170- versus directly using geth's statedb. Consider this test a canonical definition of how our forked stateDB acts
171- differently from geth's.
172- Good place for future fuzz testing if we run into issues.
173- */
158+ // TestForkedBackendDifferential tests the differential properties between a stateDB using a forked backend
159+ // versus directly using geth's statedb. Consider this test a canonical definition of how our forked stateDB acts
160+ // differently from geth's.
161+ // Good place for future fuzz testing if we run into issues.
174162func TestForkedBackendDifferential (t * testing.T ) {
175163 fixture := newPrePopulatedBackendFixture ()
176164 factory := NewForkedStateFactory (fixture .Backend )
@@ -187,14 +175,14 @@ func TestForkedBackendDifferential(t *testing.T) {
187175 fixture .StateObjectContractAddress ,
188176 fixture .StateObjectContract .Balance ,
189177 tracing .BalanceChangeUnspecified )
190- gethStateDb .SetNonce (fixture .StateObjectContractAddress , fixture .StateObjectContract .Nonce )
178+ gethStateDb .SetNonce (fixture .StateObjectContractAddress , fixture .StateObjectContract .Nonce , tracing . NonceChangeUnspecified )
191179 gethStateDb .SetCode (fixture .StateObjectContractAddress , fixture .StateObjectContract .Code )
192180 // eoa
193181 gethStateDb .SetBalance (
194182 fixture .StateObjectEOAAddress ,
195183 fixture .StateObjectEOA .Balance ,
196184 tracing .BalanceChangeUnspecified )
197- gethStateDb .SetNonce (fixture .StateObjectEOAAddress , fixture .StateObjectEOA .Nonce )
185+ gethStateDb .SetNonce (fixture .StateObjectEOAAddress , fixture .StateObjectEOA .Nonce , tracing . NonceChangeUnspecified )
198186 // do not set the empty account. On a live geth node, the empty account will be pruned.
199187
200188 // check exist/empty equivalence for the contract account
@@ -232,9 +220,7 @@ func TestForkedBackendDifferential(t *testing.T) {
232220
233221// createEmptyStateDb creates an empty stateDB using the provided factory. Intended for tests only.
234222func createEmptyStateDb (factory MedusaStateFactory ) (types2.MedusaStateDB , error ) {
235- db := rawdb .NewMemoryDatabase ()
236- tdb := triedb .NewDatabase (db , nil )
237- cachingDb := gethstate .NewDatabaseWithNodeDB (db , tdb )
223+ cachingDb := gethstate .NewDatabaseForTesting ()
238224 return factory .New (types .EmptyRootHash , cachingDb )
239225}
240226
@@ -243,5 +229,5 @@ func createEmptyStateDb(factory MedusaStateFactory) (types2.MedusaStateDB, error
243229type gethStateFactory struct {}
244230
245231func (f * gethStateFactory ) New (root common.Hash , db gethstate.Database ) (types2.MedusaStateDB , error ) {
246- return gethstate .New (root , db , nil )
232+ return gethstate .New (root , db )
247233}
0 commit comments