@@ -26,12 +26,10 @@ import (
2626 "time"
2727
2828 "github.com/ethereum/go-ethereum/common"
29- "github.com/ethereum/go-ethereum/common/hexutil"
3029 "github.com/ethereum/go-ethereum/core/rawdb"
3130 "github.com/ethereum/go-ethereum/crypto"
3231 "github.com/ethereum/go-ethereum/ethdb"
3332 "github.com/ethereum/go-ethereum/log"
34- "github.com/ethereum/go-ethereum/metrics"
3533 "github.com/ethereum/go-ethereum/triedb"
3634 "golang.org/x/sync/errgroup"
3735)
@@ -40,25 +38,10 @@ const (
4038 statEvictThreshold = 128 // the depth of statistic to be preserved
4139)
4240
43- // Metrics for uploading the state statistics.
44- var (
45- blockInfoGauge = metrics .NewRegisteredGaugeInfo ("state/size/block" , nil )
46- accountsGauge = metrics .NewRegisteredGauge ("state/size/account/count" , nil )
47- accountBytesGauge = metrics .NewRegisteredGauge ("state/size/account/bytes" , nil )
48- storagesGauge = metrics .NewRegisteredGauge ("state/size/storage/count" , nil )
49- storageBytesGauge = metrics .NewRegisteredGauge ("state/size/storage/bytes" , nil )
50- accountTrienodesGauge = metrics .NewRegisteredGauge ("state/size/trienode/account/count" , nil )
51- accountTrienodeBytesGauge = metrics .NewRegisteredGauge ("state/size/trienode/account/bytes" , nil )
52- storageTrienodesGauge = metrics .NewRegisteredGauge ("state/size/trienode/storage/count" , nil )
53- storageTrienodeBytesGauge = metrics .NewRegisteredGauge ("state/size/trienode/storage/bytes" , nil )
54- contractCodesGauge = metrics .NewRegisteredGauge ("state/size/contractcode/count" , nil )
55- contractCodeBytesGauge = metrics .NewRegisteredGauge ("state/size/contractcode/bytes" , nil )
56- )
57-
5841// Database key scheme for states.
5942var (
6043 accountKeySize = int64 (len (rawdb .SnapshotAccountPrefix ) + common .HashLength )
61- storageKeySize = int64 (len (rawdb .SnapshotStoragePrefix ) + common .HashLength + common . HashLength )
44+ storageKeySize = int64 (len (rawdb .SnapshotStoragePrefix ) + common .HashLength * 2 )
6245 accountTrienodePrefixSize = int64 (len (rawdb .TrieNodeAccountPrefix ))
6346 storageTrienodePrefixSize = int64 (len (rawdb .TrieNodeStoragePrefix ) + common .HashLength )
6447 codeKeySize = int64 (len (rawdb .CodePrefix ) + common .HashLength )
@@ -246,6 +229,9 @@ type SizeTracker struct {
246229 abort chan struct {}
247230 aborted chan struct {}
248231 updateCh chan * stateUpdate
232+
233+ mu sync.RWMutex
234+ latestStats * SizeStats
249235}
250236
251237// NewSizeTracker creates a new state size tracker and starts it automatically
@@ -313,7 +299,12 @@ func (t *SizeTracker) run() {
313299 }
314300 stat := base .add (diff )
315301 stats [u .root ] = stat
316- t .upload (stat )
302+ log .Info ("Update state size" , "number" , stat .BlockNumber , "root" , stat .StateRoot , "stat" , stat )
303+
304+ // Update latest stats
305+ t .mu .Lock ()
306+ t .latestStats = & stat
307+ t .mu .Unlock ()
317308
318309 heap .Push (& h , stats [u .root ])
319310 for u .blockNumber - h [0 ].BlockNumber > statEvictThreshold {
@@ -415,6 +406,13 @@ wait:
415406 if err := apply (result .root , result .stat ); err != nil {
416407 return nil , err
417408 }
409+
410+ // Set initial latest stats
411+ stats [result .root ] = result .stat
412+ t .mu .Lock ()
413+ t .latestStats = & result .stat
414+ t .mu .Unlock ()
415+
418416 log .Info ("Measured persistent state size" , "root" , result .root , "number" , result .blockNumber , "stat" , result .stat , "elapsed" , common .PrettyDuration (result .elapsed ))
419417 return stats , nil
420418
@@ -600,20 +598,9 @@ func (t *SizeTracker) iterateTableParallel(closed chan struct{}, prefix []byte,
600598 return totalCount , totalBytes , nil
601599}
602600
603- func (t * SizeTracker ) upload (stats SizeStats ) {
604- log .Debug ("Uploading state size" , "number" , stats .BlockNumber , "root" , stats .StateRoot , "stat" , stats )
605- blockInfoGauge .Update (metrics.GaugeInfoValue {
606- "number" : hexutil .Uint64 (stats .BlockNumber ).String (),
607- "hash" : stats .StateRoot .Hex (),
608- })
609- accountsGauge .Update (stats .Accounts )
610- accountBytesGauge .Update (stats .AccountBytes )
611- storagesGauge .Update (stats .Storages )
612- storageBytesGauge .Update (stats .StorageBytes )
613- accountTrienodesGauge .Update (stats .AccountTrienodes )
614- accountTrienodeBytesGauge .Update (stats .AccountTrienodeBytes )
615- storageTrienodesGauge .Update (stats .StorageTrienodes )
616- storageTrienodeBytesGauge .Update (stats .StorageTrienodeBytes )
617- contractCodesGauge .Update (stats .ContractCodes )
618- contractCodeBytesGauge .Update (stats .ContractCodeBytes )
601+ // GetLatestStats returns the latest state size statistics, or nil if not available
602+ func (t * SizeTracker ) GetLatestStats () * SizeStats {
603+ t .mu .RLock ()
604+ defer t .mu .RUnlock ()
605+ return t .latestStats
619606}
0 commit comments