erigon-pulse/core/state/intermediate_hashes.go
Alex Sharov 56022d8d85
- parent node type - doesn't matter for IH - just check that node itself is fullNode or duoNode is enough (#518)
- in unloading - 64 nibbles - it's hex of a.storage, but not a - then 64 nibbles hex must be stored in IH with incarnation
- EmptyRoot can't come to `WillUnloadBranchNode` method - because if account has no storage - then accountNode will have no branches in a.storage, a.storage will equal to `hashNode(EmptyRoot)` - and we don't unload hashNodes (only fullNode/duoNode).
2020-05-06 09:58:37 +01:00

77 lines
2.3 KiB
Go

package state
import (
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
"github.com/ledgerwatch/turbo-geth/common/pool"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/log"
"github.com/ledgerwatch/turbo-geth/metrics"
"github.com/ledgerwatch/turbo-geth/trie"
)
var (
InsertCounter = metrics.NewRegisteredCounter("db/ih/insert", nil)
DeleteCounter = metrics.NewRegisteredCounter("db/ih/delete", nil)
)
const keyBufferSize = 64
type IntermediateHashes struct {
trie.NoopObserver // make sure that we don't need to subscribe to unnecessary methods
putter ethdb.Putter
deleter ethdb.Deleter
}
func NewIntermediateHashes(putter ethdb.Putter, deleter ethdb.Deleter) *IntermediateHashes {
return &IntermediateHashes{putter: putter, deleter: deleter}
}
func (ih *IntermediateHashes) WillUnloadBranchNode(prefixAsNibbles []byte, nodeHash common.Hash, incarnation uint64) {
// only put to bucket prefixes with even number of nibbles
if len(prefixAsNibbles) == 0 || len(prefixAsNibbles)%2 == 1 {
return
}
InsertCounter.Inc(1)
buf := pool.GetBuffer(keyBufferSize)
defer pool.PutBuffer(buf)
trie.CompressNibbles(prefixAsNibbles, &buf.B)
var key []byte
if len(buf.B) >= common.HashLength {
key = dbutils.GenerateCompositeStoragePrefix(buf.B[:common.HashLength], incarnation, buf.B[common.HashLength:])
} else {
key = common.CopyBytes(buf.B)
}
if err := ih.putter.Put(dbutils.IntermediateTrieHashBucket, key, common.CopyBytes(nodeHash[:])); err != nil {
log.Warn("could not put intermediate trie hash", "err", err)
}
}
func (ih *IntermediateHashes) BranchNodeLoaded(prefixAsNibbles []byte, incarnation uint64) {
// only put to bucket prefixes with even number of nibbles
if len(prefixAsNibbles) == 0 || len(prefixAsNibbles)%2 == 1 {
return
}
DeleteCounter.Inc(1)
buf := pool.GetBuffer(keyBufferSize)
defer pool.PutBuffer(buf)
trie.CompressNibbles(prefixAsNibbles, &buf.B)
var key []byte
if len(buf.B) >= common.HashLength {
key = dbutils.GenerateCompositeStoragePrefix(buf.B[:common.HashLength], incarnation, buf.B[common.HashLength:])
} else {
key = common.CopyBytes(buf.B)
}
if err := ih.deleter.Delete(dbutils.IntermediateTrieHashBucket, key); err != nil {
log.Warn("could not delete intermediate trie hash", "err", err)
return
}
}