Skip to content

Commit 4da9e8a

Browse files
committed
state update
Signed-off-by: jsvisa <[email protected]>
1 parent ab70c17 commit 4da9e8a

File tree

1 file changed

+73
-24
lines changed

1 file changed

+73
-24
lines changed

core/state/statesize.go

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/ethereum/go-ethereum/common"
2424
"github.com/ethereum/go-ethereum/core/rawdb"
25+
"github.com/ethereum/go-ethereum/crypto"
2526
"github.com/ethereum/go-ethereum/ethdb"
2627
"github.com/ethereum/go-ethereum/log"
2728
"github.com/ethereum/go-ethereum/metrics"
@@ -228,42 +229,90 @@ func (g *stateSizeGenerator) persistMetrics() {
228229

229230
// updateMetrics updates metrics based on state changes
230231
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+
)
232236

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
238247
}
248+
if len(oldValue) == 0 {
249+
accountCount += 1
250+
accountBytes += common.HashLength
251+
}
252+
accountBytes += len(newValue) - len(oldValue)
239253
}
240254

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
247275
}
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)
248285
}
249286
}
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)
256299
}
257300
}
301+
for _, code := range update.codes {
302+
codeCount += 1
303+
codeBytes += len(code.blob) + common.HashLength // no deduplication
304+
}
258305

259306
// Update local metrics
260307
g.metricsLock.Lock()
261308
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)
268317
g.metricsLock.Unlock()
269318
}

0 commit comments

Comments
 (0)