|
| 1 | +// Copyright 2025 The go-ethereum Authors |
| 2 | +// This file is part of the go-ethereum library. |
| 3 | +// |
| 4 | +// The go-ethereum library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// The go-ethereum library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package state |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/ethereum/go-ethereum/common" |
| 24 | + "github.com/ethereum/go-ethereum/core/rawdb" |
| 25 | + "github.com/ethereum/go-ethereum/core/tracing" |
| 26 | + "github.com/ethereum/go-ethereum/core/types" |
| 27 | + "github.com/ethereum/go-ethereum/ethdb/pebble" |
| 28 | + "github.com/ethereum/go-ethereum/triedb" |
| 29 | + "github.com/ethereum/go-ethereum/triedb/pathdb" |
| 30 | + "github.com/holiman/uint256" |
| 31 | +) |
| 32 | + |
| 33 | +func TestSizeTracker(t *testing.T) { |
| 34 | + tempDir := t.TempDir() |
| 35 | + |
| 36 | + pdb, err := pebble.New(tempDir, 0, 0, "", false) |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("Failed to create pebble database: %v", err) |
| 39 | + } |
| 40 | + defer pdb.Close() |
| 41 | + |
| 42 | + db := rawdb.NewDatabase(pdb) |
| 43 | + |
| 44 | + tdb := triedb.NewDatabase(db, &triedb.Config{PathDB: pathdb.Defaults}) |
| 45 | + sdb := NewDatabase(tdb, nil) |
| 46 | + |
| 47 | + state, _ := New(types.EmptyRootHash, sdb) |
| 48 | + |
| 49 | + testAddr1 := common.HexToAddress("0x1234567890123456789012345678901234567890") |
| 50 | + testAddr2 := common.HexToAddress("0x2345678901234567890123456789012345678901") |
| 51 | + testAddr3 := common.HexToAddress("0x3456789012345678901234567890123456789012") |
| 52 | + |
| 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")) |
| 57 | + |
| 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}) |
| 61 | + |
| 62 | + state.AddBalance(testAddr3, uint256.NewInt(3000), tracing.BalanceChangeUnspecified) |
| 63 | + state.SetNonce(testAddr3, 3, tracing.NonceChangeUnspecified) |
| 64 | + |
| 65 | + root1, _, err := state.CommitWithUpdate(1, true, false) |
| 66 | + if err != nil { |
| 67 | + t.Fatalf("Failed to commit initial state: %v", err) |
| 68 | + } |
| 69 | + if err := tdb.Commit(root1, false); err != nil { |
| 70 | + t.Fatalf("Failed to commit trie: %v", err) |
| 71 | + } |
| 72 | + |
| 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) |
| 79 | + |
| 80 | + // Create new state from the previous committed root |
| 81 | + newState, err := New(currentRoot, sdb) |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("Failed to create new state at block %d: %v", blockNum, err) |
| 84 | + } |
| 85 | + |
| 86 | + testAddr := common.BigToAddress(uint256.NewInt(uint64(i + 100)).ToBig()) |
| 87 | + newState.AddBalance(testAddr, uint256.NewInt(uint64((i+1)*1000)), tracing.BalanceChangeUnspecified) |
| 88 | + newState.SetNonce(testAddr, uint64(i+10), tracing.NonceChangeUnspecified) |
| 89 | + |
| 90 | + 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())) |
| 93 | + } |
| 94 | + |
| 95 | + if i%3 == 0 { |
| 96 | + newState.SetCode(testAddr, []byte{byte(i), 0x60, 0x80, byte(i + 1), 0x52}) |
| 97 | + } |
| 98 | + |
| 99 | + root, _, err := newState.CommitWithUpdate(blockNum, true, false) |
| 100 | + if err != nil { |
| 101 | + t.Fatalf("Failed to commit state at block %d: %v", blockNum, err) |
| 102 | + } |
| 103 | + if err := tdb.Commit(root, false); err != nil { |
| 104 | + t.Fatalf("Failed to commit trie at block %d: %v", blockNum, err) |
| 105 | + } |
| 106 | + |
| 107 | + currentRoot = root |
| 108 | + } |
| 109 | + |
| 110 | + baselineRoot := currentRoot |
| 111 | + rawdb.WriteSnapshotRoot(db, baselineRoot) |
| 112 | + |
| 113 | + // Wait for snapshot completion |
| 114 | + for !tdb.SnapshotCompleted() { |
| 115 | + time.Sleep(100 * time.Millisecond) |
| 116 | + } |
| 117 | + |
| 118 | + // Calculate baseline from the intermediate persisted state |
| 119 | + baselineTracker := &SizeTracker{ |
| 120 | + db: db, |
| 121 | + triedb: tdb, |
| 122 | + abort: make(chan struct{}), |
| 123 | + } |
| 124 | + |
| 125 | + done := make(chan buildResult) |
| 126 | + go baselineTracker.build(baselineRoot, baselineBlockNum, done) |
| 127 | + var baselineResult buildResult |
| 128 | + select { |
| 129 | + case baselineResult = <-done: |
| 130 | + if baselineResult.err != nil { |
| 131 | + t.Fatalf("Failed to get baseline stats: %v", baselineResult.err) |
| 132 | + } |
| 133 | + case <-time.After(30 * time.Second): |
| 134 | + t.Fatal("Timeout waiting for baseline stats") |
| 135 | + } |
| 136 | + baseline := baselineResult.stat |
| 137 | + |
| 138 | + // Now start the tracker and notify it of updates that happen AFTER the baseline |
| 139 | + tracker, err := NewSizeTracker(db, tdb) |
| 140 | + if err != nil { |
| 141 | + t.Fatalf("Failed to create size tracker: %v", err) |
| 142 | + } |
| 143 | + defer tracker.Stop() |
| 144 | + |
| 145 | + // Continue from where we left off (block 51+) and track those updates |
| 146 | + var trackedUpdates []SizeStats |
| 147 | + currentRoot = baselineRoot |
| 148 | + |
| 149 | + // Generate additional blocks beyond the baseline and track them |
| 150 | + for i := 49; i < 130; i++ { // blocks 51-132 |
| 151 | + blockNum := uint64(i + 2) |
| 152 | + newState, _ := New(currentRoot, sdb) |
| 153 | + |
| 154 | + testAddr := common.BigToAddress(uint256.NewInt(uint64(i + 100)).ToBig()) |
| 155 | + newState.AddBalance(testAddr, uint256.NewInt(uint64((i+1)*1000)), tracing.BalanceChangeUnspecified) |
| 156 | + newState.SetNonce(testAddr, uint64(i+10), tracing.NonceChangeUnspecified) |
| 157 | + |
| 158 | + 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())) |
| 161 | + } |
| 162 | + |
| 163 | + if i%3 == 0 { |
| 164 | + newState.SetCode(testAddr, []byte{byte(i), 0x60, 0x80, byte(i + 1), 0x52}) |
| 165 | + } |
| 166 | + |
| 167 | + root, update, err := newState.CommitWithUpdate(blockNum, true, false) |
| 168 | + if err != nil { |
| 169 | + t.Fatalf("Failed to commit state at block %d: %v", blockNum, err) |
| 170 | + } |
| 171 | + if err := tdb.Commit(root, false); err != nil { |
| 172 | + t.Fatalf("Failed to commit trie at block %d: %v", blockNum, err) |
| 173 | + } |
| 174 | + |
| 175 | + diff, err := calSizeStats(update) |
| 176 | + if err != nil { |
| 177 | + t.Fatalf("Failed to calculate size stats for block %d: %v", blockNum, err) |
| 178 | + } |
| 179 | + trackedUpdates = append(trackedUpdates, diff) |
| 180 | + tracker.Notify(update) |
| 181 | + currentRoot = root |
| 182 | + } |
| 183 | + |
| 184 | + // Give the StateTracker time to process all the notifications we sent |
| 185 | + time.Sleep(100 * time.Millisecond) |
| 186 | + |
| 187 | + finalRoot := currentRoot |
| 188 | + |
| 189 | + finalTracker := &SizeTracker{ |
| 190 | + db: db, |
| 191 | + triedb: tdb, |
| 192 | + abort: make(chan struct{}), |
| 193 | + } |
| 194 | + |
| 195 | + finalDone := make(chan buildResult) |
| 196 | + go finalTracker.build(finalRoot, uint64(132), finalDone) |
| 197 | + var result buildResult |
| 198 | + select { |
| 199 | + case result = <-finalDone: |
| 200 | + if result.err != nil { |
| 201 | + t.Fatalf("Failed to build final stats: %v", result.err) |
| 202 | + } |
| 203 | + case <-time.After(30 * time.Second): |
| 204 | + t.Fatal("Timeout waiting for final stats") |
| 205 | + } |
| 206 | + |
| 207 | + actualStats := result.stat |
| 208 | + |
| 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 | + |
| 230 | + expectedStats := baseline |
| 231 | + for _, diff := range trackedUpdates { |
| 232 | + expectedStats = expectedStats.add(diff) |
| 233 | + } |
| 234 | + |
| 235 | + // The final measured stats should match our calculated expected stats exactly |
| 236 | + if actualStats.Accounts != expectedStats.Accounts { |
| 237 | + t.Errorf("Account count mismatch: baseline(%d) + tracked_changes = %d, but final_measurement = %d", baseline.Accounts, expectedStats.Accounts, actualStats.Accounts) |
| 238 | + } |
| 239 | + if actualStats.AccountBytes != expectedStats.AccountBytes { |
| 240 | + t.Errorf("Account bytes mismatch: expected %d, got %d", expectedStats.AccountBytes, actualStats.AccountBytes) |
| 241 | + } |
| 242 | + if actualStats.Storages != expectedStats.Storages { |
| 243 | + t.Errorf("Storage count mismatch: baseline(%d) + tracked_changes = %d, but final_measurement = %d", baseline.Storages, expectedStats.Storages, actualStats.Storages) |
| 244 | + } |
| 245 | + if actualStats.StorageBytes != expectedStats.StorageBytes { |
| 246 | + t.Errorf("Storage bytes mismatch: expected %d, got %d", expectedStats.StorageBytes, actualStats.StorageBytes) |
| 247 | + } |
| 248 | + if actualStats.ContractCodes != expectedStats.ContractCodes { |
| 249 | + t.Errorf("Contract code count mismatch: baseline(%d) + tracked_changes = %d, but final_measurement = %d", baseline.ContractCodes, expectedStats.ContractCodes, actualStats.ContractCodes) |
| 250 | + } |
| 251 | + if actualStats.ContractCodeBytes != expectedStats.ContractCodeBytes { |
| 252 | + t.Errorf("Contract code bytes mismatch: expected %d, got %d", expectedStats.ContractCodeBytes, actualStats.ContractCodeBytes) |
| 253 | + } |
| 254 | + if actualStats.AccountTrienodes != expectedStats.AccountTrienodes { |
| 255 | + t.Errorf("Account trie nodes mismatch: expected %d, got %d", expectedStats.AccountTrienodes, actualStats.AccountTrienodes) |
| 256 | + } |
| 257 | + if actualStats.AccountTrienodeBytes != expectedStats.AccountTrienodeBytes { |
| 258 | + t.Errorf("Account trie node bytes mismatch: expected %d, got %d", expectedStats.AccountTrienodeBytes, actualStats.AccountTrienodeBytes) |
| 259 | + } |
| 260 | + if actualStats.StorageTrienodes != expectedStats.StorageTrienodes { |
| 261 | + t.Errorf("Storage trie nodes mismatch: expected %d, got %d", expectedStats.StorageTrienodes, actualStats.StorageTrienodes) |
| 262 | + } |
| 263 | + if actualStats.StorageTrienodeBytes != expectedStats.StorageTrienodeBytes { |
| 264 | + t.Errorf("Storage trie node bytes mismatch: expected %d, got %d", expectedStats.StorageTrienodeBytes, actualStats.StorageTrienodeBytes) |
| 265 | + } |
| 266 | + |
| 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 | + |
| 288 | + t.Logf("Baseline stats: Accounts=%d, AccountBytes=%d, Storages=%d, StorageBytes=%d, ContractCodes=%d", |
| 289 | + baseline.Accounts, baseline.AccountBytes, baseline.Storages, baseline.StorageBytes, baseline.ContractCodes) |
| 290 | + t.Logf("Expected stats: Accounts=%d, AccountBytes=%d, Storages=%d, StorageBytes=%d, ContractCodes=%d", |
| 291 | + expectedStats.Accounts, expectedStats.AccountBytes, expectedStats.Storages, expectedStats.StorageBytes, expectedStats.ContractCodes) |
| 292 | + t.Logf("Final stats: Accounts=%d, AccountBytes=%d, Storages=%d, StorageBytes=%d, ContractCodes=%d", |
| 293 | + actualStats.Accounts, actualStats.AccountBytes, actualStats.Storages, actualStats.StorageBytes, actualStats.ContractCodes) |
| 294 | + t.Logf("Growth: Accounts=+%d, StorageSlots=+%d, ContractCodes=+%d", |
| 295 | + accountGrowth, storageGrowth, codeGrowth) |
| 296 | + t.Logf("Tracked %d state updates from %d blocks successfully", len(trackedUpdates), 81) |
| 297 | +} |
0 commit comments