From b9339ca8473374e6f1585548f26f9b6f0f72028e Mon Sep 17 00:00:00 2001 From: jsvisa Date: Wed, 22 Oct 2025 12:15:39 +0800 Subject: [PATCH 1/4] core/state: add debug if accounts mismatch --- core/state/state_sizer_test.go | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/core/state/state_sizer_test.go b/core/state/state_sizer_test.go index cab0c381635a..e14319f2badb 100644 --- a/core/state/state_sizer_test.go +++ b/core/state/state_sizer_test.go @@ -24,6 +24,7 @@ import ( "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/triedb" "github.com/ethereum/go-ethereum/triedb/pathdb" "github.com/holiman/uint256" @@ -119,6 +120,57 @@ func TestSizeTracker(t *testing.T) { } baseline := baselineResult.stat + // DEBUG: Check if baseline found all expected accounts + if baseline.Accounts != 52 { + t.Logf("WARNING: Baseline found %d accounts instead of expected 52", baseline.Accounts) + + // Collect all expected account addresses + expectedAddrs := make(map[common.Address]bool) + expectedAddrs[addr1] = true + expectedAddrs[addr2] = true + expectedAddrs[addr3] = true + for i := 1; i < 50; i++ { + testAddr := common.BigToAddress(uint256.NewInt(uint64(i + 100)).ToBig()) + expectedAddrs[testAddr] = true + } + + // Scan what's actually in the snapshot + actualAddrs := make(map[common.Address]bool) + iter := db.NewIterator(rawdb.SnapshotAccountPrefix, nil) + for iter.Next() { + // Key is: prefix + account hash + if len(iter.Key()) < len(rawdb.SnapshotAccountPrefix)+common.HashLength { + continue + } + accHash := common.BytesToHash(iter.Key()[len(rawdb.SnapshotAccountPrefix):]) + + // Find which address this hash corresponds to + for addr := range expectedAddrs { + if crypto.Keccak256Hash(addr.Bytes()) == accHash { + actualAddrs[addr] = true + break + } + } + } + iter.Release() + + t.Logf("Serial scan found %d accounts in snapshot", len(actualAddrs)) + + // Find missing accounts + for addr := range expectedAddrs { + if !actualAddrs[addr] { + t.Logf("MISSING ACCOUNT: %s (hash: %s)", addr.Hex(), crypto.Keccak256Hash(addr.Bytes()).Hex()) + } + } + + // Find unexpected accounts + for addr := range actualAddrs { + if !expectedAddrs[addr] { + t.Logf("UNEXPECTED ACCOUNT: %s (hash: %s)", addr.Hex(), crypto.Keccak256Hash(addr.Bytes()).Hex()) + } + } + } + // Now start the tracker and notify it of updates that happen AFTER the baseline tracker, err := NewSizeTracker(db, tdb) if err != nil { From 47fc9684a8cca6d3ffcb9112c7535a30686d7170 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Wed, 22 Oct 2025 14:50:00 +0800 Subject: [PATCH 2/4] Revert "core/state: add debug if accounts mismatch" This reverts commit b9339ca8473374e6f1585548f26f9b6f0f72028e. --- core/state/state_sizer_test.go | 52 ---------------------------------- 1 file changed, 52 deletions(-) diff --git a/core/state/state_sizer_test.go b/core/state/state_sizer_test.go index e14319f2badb..cab0c381635a 100644 --- a/core/state/state_sizer_test.go +++ b/core/state/state_sizer_test.go @@ -24,7 +24,6 @@ import ( "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/triedb" "github.com/ethereum/go-ethereum/triedb/pathdb" "github.com/holiman/uint256" @@ -120,57 +119,6 @@ func TestSizeTracker(t *testing.T) { } baseline := baselineResult.stat - // DEBUG: Check if baseline found all expected accounts - if baseline.Accounts != 52 { - t.Logf("WARNING: Baseline found %d accounts instead of expected 52", baseline.Accounts) - - // Collect all expected account addresses - expectedAddrs := make(map[common.Address]bool) - expectedAddrs[addr1] = true - expectedAddrs[addr2] = true - expectedAddrs[addr3] = true - for i := 1; i < 50; i++ { - testAddr := common.BigToAddress(uint256.NewInt(uint64(i + 100)).ToBig()) - expectedAddrs[testAddr] = true - } - - // Scan what's actually in the snapshot - actualAddrs := make(map[common.Address]bool) - iter := db.NewIterator(rawdb.SnapshotAccountPrefix, nil) - for iter.Next() { - // Key is: prefix + account hash - if len(iter.Key()) < len(rawdb.SnapshotAccountPrefix)+common.HashLength { - continue - } - accHash := common.BytesToHash(iter.Key()[len(rawdb.SnapshotAccountPrefix):]) - - // Find which address this hash corresponds to - for addr := range expectedAddrs { - if crypto.Keccak256Hash(addr.Bytes()) == accHash { - actualAddrs[addr] = true - break - } - } - } - iter.Release() - - t.Logf("Serial scan found %d accounts in snapshot", len(actualAddrs)) - - // Find missing accounts - for addr := range expectedAddrs { - if !actualAddrs[addr] { - t.Logf("MISSING ACCOUNT: %s (hash: %s)", addr.Hex(), crypto.Keccak256Hash(addr.Bytes()).Hex()) - } - } - - // Find unexpected accounts - for addr := range actualAddrs { - if !expectedAddrs[addr] { - t.Logf("UNEXPECTED ACCOUNT: %s (hash: %s)", addr.Hex(), crypto.Keccak256Hash(addr.Bytes()).Hex()) - } - } - } - // Now start the tracker and notify it of updates that happen AFTER the baseline tracker, err := NewSizeTracker(db, tdb) if err != nil { From a50f220ab3c2cc6e17352f6004a5125787c1e308 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Wed, 22 Oct 2025 15:10:31 +0800 Subject: [PATCH 3/4] core/state: reopen triedb to ensure buffer flushed --- core/state/state_sizer_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/state/state_sizer_test.go b/core/state/state_sizer_test.go index cab0c381635a..86ee2d85bbdf 100644 --- a/core/state/state_sizer_test.go +++ b/core/state/state_sizer_test.go @@ -94,6 +94,14 @@ func TestSizeTracker(t *testing.T) { } baselineRoot := currentRoot + // Close and reopen the trie database so all async flushes triggered by the + // baseline commits are written before we measure the baseline snapshot. + if err := tdb.Close(); err != nil { + t.Fatalf("Failed to close triedb before baseline measurement: %v", err) + } + tdb = triedb.NewDatabase(db, &triedb.Config{PathDB: pathdb.Defaults}) + sdb = NewDatabase(tdb, nil) + // Wait for snapshot completion for !tdb.SnapshotCompleted() { time.Sleep(100 * time.Millisecond) From 850e04c355f7874bb3d89f5eaed4b322a19620d7 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Wed, 22 Oct 2025 15:11:07 +0800 Subject: [PATCH 4/4] core/state: check account trie nodes and bytes --- core/state/state_sizer_test.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/core/state/state_sizer_test.go b/core/state/state_sizer_test.go index 86ee2d85bbdf..65f652e424c1 100644 --- a/core/state/state_sizer_test.go +++ b/core/state/state_sizer_test.go @@ -223,13 +223,12 @@ func TestSizeTracker(t *testing.T) { if actualStats.ContractCodeBytes != expectedStats.ContractCodeBytes { t.Errorf("Contract code bytes mismatch: expected %d, got %d", expectedStats.ContractCodeBytes, actualStats.ContractCodeBytes) } - // TODO: failed on github actions, need to investigate - // if actualStats.AccountTrienodes != expectedStats.AccountTrienodes { - // t.Errorf("Account trie nodes mismatch: expected %d, got %d", expectedStats.AccountTrienodes, actualStats.AccountTrienodes) - // } - // if actualStats.AccountTrienodeBytes != expectedStats.AccountTrienodeBytes { - // t.Errorf("Account trie node bytes mismatch: expected %d, got %d", expectedStats.AccountTrienodeBytes, actualStats.AccountTrienodeBytes) - // } + if actualStats.AccountTrienodes != expectedStats.AccountTrienodes { + t.Errorf("Account trie nodes mismatch: expected %d, got %d", expectedStats.AccountTrienodes, actualStats.AccountTrienodes) + } + if actualStats.AccountTrienodeBytes != expectedStats.AccountTrienodeBytes { + t.Errorf("Account trie node bytes mismatch: expected %d, got %d", expectedStats.AccountTrienodeBytes, actualStats.AccountTrienodeBytes) + } if actualStats.StorageTrienodes != expectedStats.StorageTrienodes { t.Errorf("Storage trie nodes mismatch: expected %d, got %d", expectedStats.StorageTrienodes, actualStats.StorageTrienodes) }