erigon-pulse/core/state/intermediate_hashes.go
Alex Sharov c68f00045a
Revert "remove tombstones" (#430)
* Revert "remove tombstones (#426)"

This reverts commit aa6bab40e8.

* tombstones don't hide storage or account anymore

* auto-format code by prettier (similar to gofmt)

* wow, it works.....

* small simplification, but need make it more clear

* rebase to master

* rebase to master

* rebase to master

* re-run ci

* clean test files
2020-04-08 12:45:51 +01:00

139 lines
3.6 KiB
Go

package state
import (
"bytes"
"fmt"
"github.com/ledgerwatch/bolt"
"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/trie"
)
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) {
// only put to bucket prefixes with even number of nibbles
if len(prefixAsNibbles) == 0 || len(prefixAsNibbles)%2 == 1 {
return
}
key := pool.GetBuffer(keyBufferSize)
defer pool.PutBuffer(key)
trie.CompressNibbles(prefixAsNibbles, &key.B)
if err := ih.putter.Put(dbutils.IntermediateTrieHashBucket, common.CopyBytes(key.B), common.CopyBytes(nodeHash[:])); err != nil {
log.Warn("could not put intermediate trie hash", "err", err)
}
}
func (ih *IntermediateHashes) BranchNodeLoaded(prefixAsNibbles []byte) {
// only put to bucket prefixes with even number of nibbles
if len(prefixAsNibbles) == 0 || len(prefixAsNibbles)%2 == 1 {
return
}
key := pool.GetBuffer(keyBufferSize)
defer pool.PutBuffer(key)
trie.CompressNibbles(prefixAsNibbles, &key.B)
if err := ih.deleter.Delete(dbutils.IntermediateTrieHashBucket, common.CopyBytes(key.B)); err != nil {
log.Warn("could not delete intermediate trie hash", "err", err)
return
}
}
// PutTombstoneForDeletedAccount - placing tombstone only if given account has storage in database
func PutTombstoneForDeletedAccount(db ethdb.Database, addrHash []byte) error {
if len(addrHash) != common.HashLength {
return nil
}
hasIH := false
if err := db.Walk(dbutils.IntermediateTrieHashBucket, addrHash, 32*8, func(_ []byte, _ []byte) (bool, error) {
hasIH = true
return false, nil
}); err != nil {
return err
}
if !hasIH {
return nil
}
return db.Put(dbutils.IntermediateTrieHashBucket, common.CopyBytes(addrHash), []byte{})
}
func ClearTombstonesForNewStorage(db ethdb.MinDatabase, storageKeyNoInc []byte) error {
var boltDb *bolt.DB
if hasKV, ok := db.(ethdb.HasKV); ok {
boltDb = hasKV.KV()
} else {
return fmt.Errorf("only Bolt supported yet, given: %T", db)
}
var toPut [][]byte
toDelete := map[string]struct{}{}
if err := boltDb.View(func(tx *bolt.Tx) error {
c := tx.Bucket(dbutils.IntermediateTrieHashBucket).Cursor()
i := common.HashLength
var k, v []byte
for ; i < len(storageKeyNoInc); i++ {
k, v = c.Seek(storageKeyNoInc[:i])
if k == nil {
return nil
}
isTombstone := v != nil && len(v) == 0
if isTombstone && bytes.HasPrefix(storageKeyNoInc, k) {
break
}
}
for ; bytes.HasPrefix(k, storageKeyNoInc[:i]); k, v = c.Next() {
isTombstone := v != nil && len(v) == 0
if isTombstone && bytes.HasPrefix(storageKeyNoInc, k) {
toDelete[string(k)] = struct{}{}
} else {
for j := i; j < len(k); j++ {
if storageKeyNoInc[j] != k[j] {
toPut = append(toPut, common.CopyBytes(k[:j+1]))
break
}
}
}
}
return nil
}); err != nil {
return err
}
for _, k := range toPut {
if err := db.Put(dbutils.IntermediateTrieHashBucket, k, []byte{}); err != nil {
return err
}
}
for k := range toDelete {
if err := db.Delete(dbutils.IntermediateTrieHashBucket, []byte(k)); err != nil {
return err
}
}
return nil
}