trie: create gentrie type

This commit is contained in:
Martin Holst Swende 2023-10-09 08:15:14 +02:00
parent ed698c2d23
commit 1e837e21f9
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0

View file

@ -395,7 +395,7 @@ func VerifyRangeProofWithStack(rootHash common.Hash, firstKey []byte, keys [][]b
return len(hps) > 0, nil return len(hps) > 0, nil
} }
// wrarpWriteFunction returns a NodeWriteFunc which filters away writes that are // wrapWriteFunction returns a NodeWriteFunc which filters away writes that are
// on the boundary: parents of first/last. // on the boundary: parents of first/last.
func wrapWriteFunction(origin, last []byte, w NodeWriteFunc) NodeWriteFunc { func wrapWriteFunction(origin, last []byte, w NodeWriteFunc) NodeWriteFunc {
if w == nil { if w == nil {
@ -415,3 +415,58 @@ func wrapWriteFunction(origin, last []byte, w NodeWriteFunc) NodeWriteFunc {
w(path, hash, blob) w(path, hash, blob)
} }
} }
type GenerativeTrie struct {
proofsSet bool
owner common.Hash
writeFn NodeWriteFunc
stack *StackTrie
rightHandBorder []byte
}
func NewGentrieWithOwner(origin, end, owner common.Hash, writeFn NodeWriteFunc) *GenerativeTrie {
// Wrap the write function
var originBorder = keybytesToHex(origin[:])
var g = &GenerativeTrie{}
wrapper := func(origin common.Hash, path []byte, hash common.Hash, blob []byte) {
if bytes.HasPrefix(originBorder, path) {
return
}
if bytes.HasPrefix(g.rightHandBorder, path) {
return
}
writeFn(origin, path, hash, blob)
}
return &GenerativeTrie{writeFn: wrapper}
}
func (g *GenerativeTrie) AddProof(rootHash common.Hash, origin []byte, proof ethdb.KeyValueReader) {
if g.stack == nil {
g.proofsSet = true
stack, err := newStackTrieFromProof(rootHash, origin[:], proof, g.writeFn)
if err != nil {
panic(err)
}
g.stack = stack
g.stack.owner = g.owner
}
}
func (g *GenerativeTrie) UpdateAll(keys []common.Hash, values [][]byte) error {
for i := 0; i < len(keys); i++ {
g.stack.Update(keys[i][:], values[i])
}
last := keys[len(keys)-1]
g.rightHandBorder = last[:]
return nil
}
// Update inserts a (key, value) pair into the stack trie.
func (g *GenerativeTrie) Update(key, value []byte) error {
return g.stack.Update(key, value)
}
func (g *GenerativeTrie) Commit() (common.Hash, error) {
return g.stack.Commit()
}