Skip to content

Commit 48c3f91

Browse files
jsvisarjl493456442
authored andcommitted
test
1 parent 9a3a6c7 commit 48c3f91

File tree

1 file changed

+41
-77
lines changed

1 file changed

+41
-77
lines changed

core/state/state_sizer_test.go

Lines changed: 41 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -40,44 +40,44 @@ func TestSizeTracker(t *testing.T) {
4040
defer pdb.Close()
4141

4242
db := rawdb.NewDatabase(pdb)
43+
defer db.Close()
4344

4445
tdb := triedb.NewDatabase(db, &triedb.Config{PathDB: pathdb.Defaults})
4546
sdb := NewDatabase(tdb, nil)
4647

47-
state, _ := New(types.EmptyRootHash, sdb)
48+
// Generate 50 blocks to establish a baseline
49+
baselineBlockNum := uint64(50)
50+
currentRoot := types.EmptyRootHash
4851

49-
testAddr1 := common.HexToAddress("0x1234567890123456789012345678901234567890")
50-
testAddr2 := common.HexToAddress("0x2345678901234567890123456789012345678901")
51-
testAddr3 := common.HexToAddress("0x3456789012345678901234567890123456789012")
52+
addr1 := common.BytesToAddress([]byte{1, 0, 0, 1})
53+
addr2 := common.BytesToAddress([]byte{1, 0, 0, 2})
54+
addr3 := common.BytesToAddress([]byte{1, 0, 0, 3})
5255

53-
state.AddBalance(testAddr1, uint256.NewInt(1000), tracing.BalanceChangeUnspecified)
54-
state.SetNonce(testAddr1, 1, tracing.NonceChangeUnspecified)
55-
state.SetState(testAddr1, common.HexToHash("0x1111"), common.HexToHash("0xaaaa"))
56-
state.SetState(testAddr1, common.HexToHash("0x2222"), common.HexToHash("0xbbbb"))
56+
// Create initial state with fixed accounts
57+
state, _ := New(currentRoot, sdb)
58+
state.AddBalance(addr1, uint256.NewInt(1000), tracing.BalanceChangeUnspecified)
59+
state.SetNonce(addr1, 1, tracing.NonceChangeUnspecified)
60+
state.SetState(addr1, common.HexToHash("0x1111"), common.HexToHash("0xaaaa"))
61+
state.SetState(addr1, common.HexToHash("0x2222"), common.HexToHash("0xbbbb"))
5762

58-
state.AddBalance(testAddr2, uint256.NewInt(2000), tracing.BalanceChangeUnspecified)
59-
state.SetNonce(testAddr2, 2, tracing.NonceChangeUnspecified)
60-
state.SetCode(testAddr2, []byte{0x60, 0x80, 0x60, 0x40, 0x52})
63+
state.AddBalance(addr2, uint256.NewInt(2000), tracing.BalanceChangeUnspecified)
64+
state.SetNonce(addr2, 2, tracing.NonceChangeUnspecified)
65+
state.SetCode(addr2, []byte{0x60, 0x80, 0x60, 0x40, 0x52})
6166

62-
state.AddBalance(testAddr3, uint256.NewInt(3000), tracing.BalanceChangeUnspecified)
63-
state.SetNonce(testAddr3, 3, tracing.NonceChangeUnspecified)
67+
state.AddBalance(addr3, uint256.NewInt(3000), tracing.BalanceChangeUnspecified)
68+
state.SetNonce(addr3, 3, tracing.NonceChangeUnspecified)
6469

65-
root1, _, err := state.CommitWithUpdate(1, true, false)
70+
currentRoot, _, err = state.CommitWithUpdate(1, true, false)
6671
if err != nil {
6772
t.Fatalf("Failed to commit initial state: %v", err)
6873
}
69-
if err := tdb.Commit(root1, false); err != nil {
70-
t.Fatalf("Failed to commit trie: %v", err)
74+
if err := tdb.Commit(currentRoot, false); err != nil {
75+
t.Fatalf("Failed to commit initial trie: %v", err)
7176
}
7277

73-
// Generate 50 blocks first to establish a baseline
74-
baselineBlockNum := uint64(50)
75-
currentRoot := root1
76-
77-
for i := 0; i < 49; i++ { // blocks 2-50
78-
blockNum := uint64(i + 2)
78+
for i := 1; i < 50; i++ { // blocks 2-50
79+
blockNum := uint64(i + 1)
7980

80-
// Create new state from the previous committed root
8181
newState, err := New(currentRoot, sdb)
8282
if err != nil {
8383
t.Fatalf("Failed to create new state at block %d: %v", blockNum, err)
@@ -88,8 +88,7 @@ func TestSizeTracker(t *testing.T) {
8888
newState.SetNonce(testAddr, uint64(i+10), tracing.NonceChangeUnspecified)
8989

9090
if i%2 == 0 {
91-
newState.SetState(testAddr1, common.BigToHash(uint256.NewInt(uint64(i+0x1000)).ToBig()),
92-
common.BigToHash(uint256.NewInt(uint64(i+0x2000)).ToBig()))
91+
newState.SetState(addr1, common.BigToHash(uint256.NewInt(uint64(i+0x1000)).ToBig()), common.BigToHash(uint256.NewInt(uint64(i+0x2000)).ToBig()))
9392
}
9493

9594
if i%3 == 0 {
@@ -108,7 +107,6 @@ func TestSizeTracker(t *testing.T) {
108107
}
109108

110109
baselineRoot := currentRoot
111-
rawdb.WriteSnapshotRoot(db, baselineRoot)
112110

113111
// Wait for snapshot completion
114112
for !tdb.SnapshotCompleted() {
@@ -142,22 +140,23 @@ func TestSizeTracker(t *testing.T) {
142140
}
143141
defer tracker.Stop()
144142

145-
// Continue from where we left off (block 51+) and track those updates
146143
var trackedUpdates []SizeStats
147144
currentRoot = baselineRoot
148145

149146
// Generate additional blocks beyond the baseline and track them
150147
for i := 49; i < 130; i++ { // blocks 51-132
151148
blockNum := uint64(i + 2)
152-
newState, _ := New(currentRoot, sdb)
149+
newState, err := New(currentRoot, sdb)
150+
if err != nil {
151+
t.Fatalf("Failed to create new state at block %d: %v", blockNum, err)
152+
}
153153

154154
testAddr := common.BigToAddress(uint256.NewInt(uint64(i + 100)).ToBig())
155155
newState.AddBalance(testAddr, uint256.NewInt(uint64((i+1)*1000)), tracing.BalanceChangeUnspecified)
156156
newState.SetNonce(testAddr, uint64(i+10), tracing.NonceChangeUnspecified)
157157

158158
if i%2 == 0 {
159-
newState.SetState(testAddr1, common.BigToHash(uint256.NewInt(uint64(i+0x1000)).ToBig()),
160-
common.BigToHash(uint256.NewInt(uint64(i+0x2000)).ToBig()))
159+
newState.SetState(addr1, common.BigToHash(uint256.NewInt(uint64(i+0x1000)).ToBig()), common.BigToHash(uint256.NewInt(uint64(i+0x2000)).ToBig()))
161160
}
162161

163162
if i%3 == 0 {
@@ -181,10 +180,19 @@ func TestSizeTracker(t *testing.T) {
181180
currentRoot = root
182181
}
183182

184-
// Give the StateTracker time to process all the notifications we sent
185-
time.Sleep(100 * time.Millisecond)
183+
if len(trackedUpdates) != 130-49 {
184+
t.Errorf("Expected %d tracked updates, got %d", 130-49, len(trackedUpdates))
185+
}
186+
187+
finalRoot := rawdb.ReadSnapshotRoot(db)
186188

187-
finalRoot := currentRoot
189+
// Ensure all commits are flushed to disk
190+
if err := tdb.Close(); err != nil {
191+
t.Fatalf("Failed to close triedb: %v", err)
192+
}
193+
194+
// Reopen the database to simulate a restart
195+
tdb = triedb.NewDatabase(db, &triedb.Config{PathDB: pathdb.Defaults})
188196

189197
finalTracker := &SizeTracker{
190198
db: db,
@@ -206,27 +214,6 @@ func TestSizeTracker(t *testing.T) {
206214

207215
actualStats := result.stat
208216

209-
// Now we have a proper test:
210-
// - Baseline measured at block 50 (with snapshot completion)
211-
// - Final state measured at block 132
212-
// - Tracked updates from blocks 51-132 (should show growth)
213-
214-
// Verify that both baseline and final measurements show reasonable data
215-
if baseline.Accounts < 50 {
216-
t.Errorf("Expected baseline to have at least 50 accounts, got %d", baseline.Accounts)
217-
}
218-
if baseline.StorageBytes == 0 {
219-
t.Errorf("Expected baseline to have storage data, got 0 bytes")
220-
}
221-
222-
if actualStats.Accounts <= baseline.Accounts {
223-
t.Errorf("Expected final state to have more accounts than baseline: baseline=%d, final=%d", baseline.Accounts, actualStats.Accounts)
224-
}
225-
226-
if actualStats.StorageBytes <= baseline.StorageBytes {
227-
t.Errorf("Expected final state to have more storage than baseline: baseline=%d, final=%d", baseline.StorageBytes, actualStats.StorageBytes)
228-
}
229-
230217
expectedStats := baseline
231218
for _, diff := range trackedUpdates {
232219
expectedStats = expectedStats.add(diff)
@@ -264,34 +251,11 @@ func TestSizeTracker(t *testing.T) {
264251
t.Errorf("Storage trie node bytes mismatch: expected %d, got %d", expectedStats.StorageTrienodeBytes, actualStats.StorageTrienodeBytes)
265252
}
266253

267-
// Verify reasonable growth occurred
268-
accountGrowth := actualStats.Accounts - baseline.Accounts
269-
storageGrowth := actualStats.Storages - baseline.Storages
270-
codeGrowth := actualStats.ContractCodes - baseline.ContractCodes
271-
272-
if accountGrowth <= 0 {
273-
t.Errorf("Expected account growth, got %d", accountGrowth)
274-
}
275-
if storageGrowth <= 0 {
276-
t.Errorf("Expected storage growth, got %d", storageGrowth)
277-
}
278-
if codeGrowth <= 0 {
279-
t.Errorf("Expected contract code growth, got %d", codeGrowth)
280-
}
281-
282-
// Verify we successfully tracked updates from blocks 51-132
283-
expectedUpdates := 81 // blocks 51-132 (81 blocks)
284-
if len(trackedUpdates) < 70 || len(trackedUpdates) > expectedUpdates {
285-
t.Errorf("Expected 70-%d tracked updates, got %d", expectedUpdates, len(trackedUpdates))
286-
}
287-
288254
t.Logf("Baseline stats: Accounts=%d, AccountBytes=%d, Storages=%d, StorageBytes=%d, ContractCodes=%d",
289255
baseline.Accounts, baseline.AccountBytes, baseline.Storages, baseline.StorageBytes, baseline.ContractCodes)
290256
t.Logf("Expected stats: Accounts=%d, AccountBytes=%d, Storages=%d, StorageBytes=%d, ContractCodes=%d",
291257
expectedStats.Accounts, expectedStats.AccountBytes, expectedStats.Storages, expectedStats.StorageBytes, expectedStats.ContractCodes)
292258
t.Logf("Final stats: Accounts=%d, AccountBytes=%d, Storages=%d, StorageBytes=%d, ContractCodes=%d",
293259
actualStats.Accounts, actualStats.AccountBytes, actualStats.Storages, actualStats.StorageBytes, actualStats.ContractCodes)
294-
t.Logf("Growth: Accounts=+%d, StorageSlots=+%d, ContractCodes=+%d",
295-
accountGrowth, storageGrowth, codeGrowth)
296260
t.Logf("Tracked %d state updates from %d blocks successfully", len(trackedUpdates), 81)
297261
}

0 commit comments

Comments
 (0)