erigon-pulse/core/state/intermediate_hashes.go
Alex Sharov daa359c363
Mgr schedule iterator (#566)
* db based version of PrefixByCumulativeWitnessSize

* db based version of PrefixByCumulativeWitnessSize

* retain all in Trie by default

* fix WitnessLen logic in calcTrie roots

* Rename IntermediateTrieWitnessLenBucket to IntermediateWitnessLenBucket

* handle corner cases in WL

* Use correct incarnation for IH bucket

* use name WitnessSize

* save progress towards db-only witness estimation

* results from trie and from db are still different

* less recursion

* correct incarnation in CumulativeSearch

* reuse results from previous Tick, separate concepts of parent and startKey

* experiment: if not including trie structure to WitnessSize will reduce cumulative error

* tool to generate all IH and tool to calculate assessment of cumulative error

* tool to generate all IH

* Calculate totalWitnessSize based on DB data - then schedule will not overrun state during MGR cycle

* better stats

* Calculate totalWitnessSize based on DB data - then schedule will not overrun state during MGR cycle

* Calculate totalWitnessSize based on DB data - then schedule will not overrun state during MGR cycle

* calculate ticks size distribution

* estimate cumulative error

* fix linter

* resetIH from scratch if needed

* cleanup

* fix test

* fix test
2020-05-28 12:33:05 +01:00

92 lines
2.9 KiB
Go

package state
import (
"encoding/binary"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
"github.com/ledgerwatch/turbo-geth/common/debug"
"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, witnessSize 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)
}
if debug.IsTrackWitnessSizeEnabled() {
lenBytes := make([]byte, 8)
binary.BigEndian.PutUint64(lenBytes, witnessSize)
if err := ih.putter.Put(dbutils.IntermediateWitnessSizeBucket, common.CopyBytes(key), lenBytes); err != nil {
log.Warn("could not put intermediate trie data len", "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)
}
if debug.IsTrackWitnessSizeEnabled() {
if err := ih.deleter.Delete(dbutils.IntermediateWitnessSizeBucket, key); err != nil {
log.Warn("could not delete intermediate trie hash", "err", err)
}
}
}