2020-01-15 13:56:50 +00:00
package trie
2020-03-25 15:40:30 +00:00
import "errors"
2020-05-12 14:24:43 +00:00
func ( t * Trie ) ExtractWitness ( trace bool , rl RetainDecider ) ( * Witness , error ) {
var rd RetainDecider
if rl != nil {
rd = rl
2020-04-08 12:59:30 +00:00
}
2020-05-12 14:24:43 +00:00
return extractWitnessFromRootNode ( t . root , trace , rd )
2020-01-24 10:58:01 +00:00
}
2020-05-12 14:24:43 +00:00
func ( t * Trie ) ExtractWitnessForPrefix ( prefix [ ] byte , trace bool , rl RetainDecider ) ( * Witness , error ) {
2020-04-16 07:42:25 +00:00
foundNode , _ , found , _ := t . getNode ( prefix , false )
2020-03-25 15:40:30 +00:00
if ! found {
return nil , errors . New ( "no data found for given prefix" )
}
2020-05-12 14:24:43 +00:00
return extractWitnessFromRootNode ( foundNode , trace , rl )
2020-03-25 15:40:30 +00:00
}
2020-05-12 07:22:45 +00:00
// ExtractWitnesses extracts witnesses for subtries starting from the specified root
2020-05-12 14:24:43 +00:00
// 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 ) {
2020-05-12 07:22:45 +00:00
var witnesses [ ] * Witness
for _ , root := range subTries . roots {
builder := NewWitnessBuilder ( root , trace )
var limiter * MerklePathLimiter = nil
2020-05-12 14:24:43 +00:00
if retainDec != nil {
2020-05-12 07:22:45 +00:00
hr := newHasher ( false )
defer returnHasherToPool ( hr )
2020-05-12 14:24:43 +00:00
limiter = & MerklePathLimiter { retainDec , hr . hash }
2020-05-12 07:22:45 +00:00
}
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
2020-05-12 14:24:43 +00:00
// 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 ) {
2020-05-12 07:22:45 +00:00
builder := NewWitnessBuilder ( root , trace )
2020-04-08 12:59:30 +00:00
var limiter * MerklePathLimiter = nil
2020-05-12 14:24:43 +00:00
if retainDec != nil {
2020-01-24 10:58:01 +00:00
hr := newHasher ( false )
defer returnHasherToPool ( hr )
2020-05-12 14:24:43 +00:00
limiter = & MerklePathLimiter { retainDec , hr . hash }
2020-01-24 10:58:01 +00:00
}
return builder . Build ( limiter )
2020-01-15 13:56:50 +00:00
}