mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 21:56:43 +00:00
state update
Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
parent
b34e366bb4
commit
d0b42a90e5
1 changed files with 73 additions and 24 deletions
|
|
@ -22,6 +22,7 @@ import (
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/ethdb"
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/metrics"
|
"github.com/ethereum/go-ethereum/metrics"
|
||||||
|
|
@ -228,42 +229,90 @@ func (g *stateSizeGenerator) persistMetrics() {
|
||||||
|
|
||||||
// updateMetrics updates metrics based on state changes
|
// updateMetrics updates metrics based on state changes
|
||||||
func (g *stateSizeGenerator) updateMetrics(update *stateUpdate) {
|
func (g *stateSizeGenerator) updateMetrics(update *stateUpdate) {
|
||||||
var diff StateSizeMetrics
|
var (
|
||||||
|
accountBytes, storageBytes, nodeBytes, codeBytes int
|
||||||
|
accountCount, storageCount, nodeCount, codeCount int
|
||||||
|
)
|
||||||
|
|
||||||
// Calculate account changes
|
for addr, oldValue := range update.accountsOrigin {
|
||||||
for _, data := range update.accounts {
|
addrHash := crypto.Keccak256Hash(addr.Bytes())
|
||||||
if len(data) > 0 {
|
newValue, exists := update.accounts[addrHash]
|
||||||
diff.AccountCount++
|
if !exists {
|
||||||
diff.AccountBytes += uint64(common.HashLength + len(data))
|
log.Warn("State update missing account", "address", addr)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
if len(newValue) == 0 {
|
||||||
|
accountCount -= 1
|
||||||
|
accountBytes -= common.HashLength
|
||||||
|
}
|
||||||
|
if len(oldValue) == 0 {
|
||||||
|
accountCount += 1
|
||||||
|
accountBytes += common.HashLength
|
||||||
|
}
|
||||||
|
accountBytes += len(newValue) - len(oldValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate storage changes
|
for addr, slots := range update.storagesOrigin {
|
||||||
for _, slots := range update.storages {
|
addrHash := crypto.Keccak256Hash(addr.Bytes())
|
||||||
for _, data := range slots {
|
subset, exists := update.storages[addrHash]
|
||||||
if len(data) > 0 {
|
if !exists {
|
||||||
diff.StorageCount++
|
log.Warn("State update missing storage", "address", addr)
|
||||||
diff.StorageBytes += uint64(2*common.HashLength + len(data))
|
continue
|
||||||
|
}
|
||||||
|
for key, oldValue := range slots {
|
||||||
|
var (
|
||||||
|
exists bool
|
||||||
|
newValue []byte
|
||||||
|
)
|
||||||
|
if update.rawStorageKey {
|
||||||
|
newValue, exists = subset[crypto.Keccak256Hash(key.Bytes())]
|
||||||
|
} else {
|
||||||
|
newValue, exists = subset[key]
|
||||||
}
|
}
|
||||||
|
if !exists {
|
||||||
|
log.Warn("State update missing storage slot", "address", addr, "key", key)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(newValue) == 0 {
|
||||||
|
storageCount -= 1
|
||||||
|
storageBytes -= common.HashLength
|
||||||
|
}
|
||||||
|
if len(oldValue) == 0 {
|
||||||
|
storageCount += 1
|
||||||
|
storageBytes += common.HashLength
|
||||||
|
}
|
||||||
|
storageBytes += len(newValue) - len(oldValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, subset := range update.nodes.Sets {
|
||||||
// Calculate trie node changes
|
for path, n := range subset.Nodes {
|
||||||
for _, nodeSet := range update.nodes.Sets {
|
if len(n.Blob) == 0 {
|
||||||
for _, node := range nodeSet.Nodes {
|
nodeCount -= 1
|
||||||
diff.TrieNodeCount++
|
nodeBytes -= len(path) + common.HashLength
|
||||||
diff.TrieNodeBytes += uint64(len(node.Blob))
|
}
|
||||||
|
prev, ok := subset.Origins[path]
|
||||||
|
if ok {
|
||||||
|
nodeCount += 1
|
||||||
|
nodeBytes += len(path) + common.HashLength
|
||||||
|
}
|
||||||
|
nodeBytes += len(n.Blob) - len(prev)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, code := range update.codes {
|
||||||
|
codeCount += 1
|
||||||
|
codeBytes += len(code.blob) + common.HashLength // no deduplication
|
||||||
|
}
|
||||||
|
|
||||||
// Update local metrics
|
// Update local metrics
|
||||||
g.metricsLock.Lock()
|
g.metricsLock.Lock()
|
||||||
g.metrics.Root = update.root
|
g.metrics.Root = update.root
|
||||||
g.metrics.AccountCount += diff.AccountCount
|
g.metrics.AccountCount += uint64(accountCount)
|
||||||
g.metrics.AccountBytes += diff.AccountBytes
|
g.metrics.AccountBytes += uint64(accountBytes)
|
||||||
g.metrics.StorageCount += diff.StorageCount
|
g.metrics.StorageCount += uint64(storageCount)
|
||||||
g.metrics.StorageBytes += diff.StorageBytes
|
g.metrics.StorageBytes += uint64(storageBytes)
|
||||||
g.metrics.TrieNodeCount += diff.TrieNodeCount
|
g.metrics.TrieNodeCount += uint64(nodeCount)
|
||||||
g.metrics.TrieNodeBytes += diff.TrieNodeBytes
|
g.metrics.TrieNodeBytes += uint64(nodeBytes)
|
||||||
|
g.metrics.ContractCount += uint64(codeCount)
|
||||||
|
g.metrics.ContractBytes += uint64(codeBytes)
|
||||||
g.metricsLock.Unlock()
|
g.metricsLock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue