erigon-pulse/turbo/trie/trie_witness.go
Igor Mandrigin adf52465e3
move ./trie to ./turbo/trie (#1114)
Useful for minimizing merge conflicts when rebasing new geth
2020-09-14 11:33:39 +01:00

56 lines
2.0 KiB
Go

package trie
import "errors"
func (t *Trie) ExtractWitness(trace bool, rl RetainDecider) (*Witness, error) {
var rd RetainDecider
if rl != nil {
rd = rl
}
return extractWitnessFromRootNode(t.root, trace, rd)
}
func (t *Trie) ExtractWitnessForPrefix(prefix []byte, trace bool, rl RetainDecider) (*Witness, error) {
foundNode, _, found, _ := t.getNode(prefix, false)
if !found {
return nil, errors.New("no data found for given prefix")
}
return extractWitnessFromRootNode(foundNode, trace, rl)
}
// ExtractWitnesses extracts witnesses for subtries starting from the specified root
// if retainDec param is nil it will make a witness for the full subtrie,
// if retainDec param is set to a RetainList instance, it will make a witness for only the accounts/storages that were actually touched; other paths will be hashed.
func ExtractWitnesses(subTries SubTries, trace bool, retainDec RetainDecider) ([]*Witness, error) {
var witnesses []*Witness
for _, root := range subTries.roots {
builder := NewWitnessBuilder(root, trace)
var limiter *MerklePathLimiter = nil
if retainDec != nil {
hr := newHasher(false)
defer returnHasherToPool(hr)
limiter = &MerklePathLimiter{retainDec, hr.hash}
}
if witness, err := builder.Build(limiter); err == nil {
witnesses = append(witnesses, witness)
} else {
return witnesses, err
}
}
return witnesses, nil
}
// extractWitnessFromRootNode extracts witness for subtrie starting from the specified root
// if retainDec param is nil it will make a witness for the full subtrie,
// if retainDec param is set to a RetainList instance, it will make a witness for only the accounts/storages that were actually touched; other paths will be hashed.
func extractWitnessFromRootNode(root node, trace bool, retainDec RetainDecider) (*Witness, error) {
builder := NewWitnessBuilder(root, trace)
var limiter *MerklePathLimiter = nil
if retainDec != nil {
hr := newHasher(false)
defer returnHasherToPool(hr)
limiter = &MerklePathLimiter{retainDec, hr.hash}
}
return builder.Build(limiter)
}