2020-04-08 05:00:31 +00:00
|
|
|
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"
|
2020-04-11 07:22:23 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/metrics"
|
2020-04-08 05:00:31 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/trie"
|
|
|
|
)
|
|
|
|
|
2020-04-11 07:22:23 +00:00
|
|
|
var (
|
2020-04-16 07:42:25 +00:00
|
|
|
InsertCounter = metrics.NewRegisteredCounter("db/ih/insert", nil)
|
|
|
|
DeleteCounter = metrics.NewRegisteredCounter("db/ih/delete", nil)
|
2020-04-11 07:22:23 +00:00
|
|
|
)
|
|
|
|
|
2020-04-08 05:00:31 +00:00
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2020-04-16 07:42:25 +00:00
|
|
|
func (ih *IntermediateHashes) WillUnloadBranchNode(prefixAsNibbles []byte, nodeHash common.Hash, incarnation uint64) {
|
2020-04-08 05:00:31 +00:00
|
|
|
// only put to bucket prefixes with even number of nibbles
|
|
|
|
if len(prefixAsNibbles) == 0 || len(prefixAsNibbles)%2 == 1 {
|
|
|
|
return
|
|
|
|
}
|
2020-04-18 20:09:44 +00:00
|
|
|
|
2020-04-11 07:22:23 +00:00
|
|
|
InsertCounter.Inc(1)
|
2020-04-08 05:00:31 +00:00
|
|
|
|
2020-04-16 07:42:25 +00:00
|
|
|
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 {
|
2020-04-08 05:00:31 +00:00
|
|
|
log.Warn("could not put intermediate trie hash", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 07:42:25 +00:00
|
|
|
func (ih *IntermediateHashes) BranchNodeLoaded(prefixAsNibbles []byte, incarnation uint64) {
|
2020-04-08 05:00:31 +00:00
|
|
|
// only put to bucket prefixes with even number of nibbles
|
|
|
|
if len(prefixAsNibbles) == 0 || len(prefixAsNibbles)%2 == 1 {
|
|
|
|
return
|
|
|
|
}
|
2020-04-11 07:22:23 +00:00
|
|
|
DeleteCounter.Inc(1)
|
2020-04-08 05:00:31 +00:00
|
|
|
|
2020-04-16 07:42:25 +00:00
|
|
|
buf := pool.GetBuffer(keyBufferSize)
|
|
|
|
defer pool.PutBuffer(buf)
|
|
|
|
trie.CompressNibbles(prefixAsNibbles, &buf.B)
|
2020-04-15 17:30:58 +00:00
|
|
|
|
2020-04-16 07:42:25 +00:00
|
|
|
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)
|
2020-04-08 11:45:51 +00:00
|
|
|
}
|
2020-04-15 17:30:58 +00:00
|
|
|
|
2020-04-16 07:42:25 +00:00
|
|
|
if err := ih.deleter.Delete(dbutils.IntermediateTrieHashBucket, key); err != nil {
|
|
|
|
log.Warn("could not delete intermediate trie hash", "err", err)
|
|
|
|
return
|
2020-04-15 17:30:58 +00:00
|
|
|
}
|
2020-04-08 11:45:51 +00:00
|
|
|
}
|