@@ -22,6 +22,7 @@ import (
22
22
23
23
"github.com/ethereum/go-ethereum/common"
24
24
"github.com/ethereum/go-ethereum/core/rawdb"
25
+ "github.com/ethereum/go-ethereum/crypto"
25
26
"github.com/ethereum/go-ethereum/ethdb"
26
27
"github.com/ethereum/go-ethereum/log"
27
28
"github.com/ethereum/go-ethereum/metrics"
@@ -228,42 +229,90 @@ func (g *stateSizeGenerator) persistMetrics() {
228
229
229
230
// updateMetrics updates metrics based on state changes
230
231
func (g * stateSizeGenerator ) updateMetrics (update * stateUpdate ) {
231
- var diff StateSizeMetrics
232
+ var (
233
+ accountBytes , storageBytes , nodeBytes , codeBytes int
234
+ accountCount , storageCount , nodeCount , codeCount int
235
+ )
232
236
233
- // Calculate account changes
234
- for _ , data := range update .accounts {
235
- if len (data ) > 0 {
236
- diff .AccountCount ++
237
- diff .AccountBytes += uint64 (common .HashLength + len (data ))
237
+ for addr , oldValue := range update .accountsOrigin {
238
+ addrHash := crypto .Keccak256Hash (addr .Bytes ())
239
+ newValue , exists := update .accounts [addrHash ]
240
+ if ! exists {
241
+ log .Warn ("State update missing account" , "address" , addr )
242
+ continue
243
+ }
244
+ if len (newValue ) == 0 {
245
+ accountCount -= 1
246
+ accountBytes -= common .HashLength
238
247
}
248
+ if len (oldValue ) == 0 {
249
+ accountCount += 1
250
+ accountBytes += common .HashLength
251
+ }
252
+ accountBytes += len (newValue ) - len (oldValue )
239
253
}
240
254
241
- // Calculate storage changes
242
- for _ , slots := range update .storages {
243
- for _ , data := range slots {
244
- if len (data ) > 0 {
245
- diff .StorageCount ++
246
- diff .StorageBytes += uint64 (2 * common .HashLength + len (data ))
255
+ for addr , slots := range update .storagesOrigin {
256
+ addrHash := crypto .Keccak256Hash (addr .Bytes ())
257
+ subset , exists := update .storages [addrHash ]
258
+ if ! exists {
259
+ log .Warn ("State update missing storage" , "address" , addr )
260
+ continue
261
+ }
262
+ for key , oldValue := range slots {
263
+ var (
264
+ exists bool
265
+ newValue []byte
266
+ )
267
+ if update .rawStorageKey {
268
+ newValue , exists = subset [crypto .Keccak256Hash (key .Bytes ())]
269
+ } else {
270
+ newValue , exists = subset [key ]
271
+ }
272
+ if ! exists {
273
+ log .Warn ("State update missing storage slot" , "address" , addr , "key" , key )
274
+ continue
247
275
}
276
+ if len (newValue ) == 0 {
277
+ storageCount -= 1
278
+ storageBytes -= common .HashLength
279
+ }
280
+ if len (oldValue ) == 0 {
281
+ storageCount += 1
282
+ storageBytes += common .HashLength
283
+ }
284
+ storageBytes += len (newValue ) - len (oldValue )
248
285
}
249
286
}
250
-
251
- // Calculate trie node changes
252
- for _ , nodeSet := range update .nodes .Sets {
253
- for _ , node := range nodeSet .Nodes {
254
- diff .TrieNodeCount ++
255
- diff .TrieNodeBytes += uint64 (len (node .Blob ))
287
+ for _ , subset := range update .nodes .Sets {
288
+ for path , n := range subset .Nodes {
289
+ if len (n .Blob ) == 0 {
290
+ nodeCount -= 1
291
+ nodeBytes -= len (path ) + common .HashLength
292
+ }
293
+ prev , ok := subset .Origins [path ]
294
+ if ok {
295
+ nodeCount += 1
296
+ nodeBytes += len (path ) + common .HashLength
297
+ }
298
+ nodeBytes += len (n .Blob ) - len (prev )
256
299
}
257
300
}
301
+ for _ , code := range update .codes {
302
+ codeCount += 1
303
+ codeBytes += len (code .blob ) + common .HashLength // no deduplication
304
+ }
258
305
259
306
// Update local metrics
260
307
g .metricsLock .Lock ()
261
308
g .metrics .Root = update .root
262
- g .metrics .AccountCount += diff .AccountCount
263
- g .metrics .AccountBytes += diff .AccountBytes
264
- g .metrics .StorageCount += diff .StorageCount
265
- g .metrics .StorageBytes += diff .StorageBytes
266
- g .metrics .TrieNodeCount += diff .TrieNodeCount
267
- g .metrics .TrieNodeBytes += diff .TrieNodeBytes
309
+ g .metrics .AccountCount += uint64 (accountCount )
310
+ g .metrics .AccountBytes += uint64 (accountBytes )
311
+ g .metrics .StorageCount += uint64 (storageCount )
312
+ g .metrics .StorageBytes += uint64 (storageBytes )
313
+ g .metrics .TrieNodeCount += uint64 (nodeCount )
314
+ g .metrics .TrieNodeBytes += uint64 (nodeBytes )
315
+ g .metrics .ContractCount += uint64 (codeCount )
316
+ g .metrics .ContractBytes += uint64 (codeBytes )
268
317
g .metricsLock .Unlock ()
269
318
}
0 commit comments