2020-01-15 13:56:50 +00:00
|
|
|
package trie
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2020-01-15 13:56:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HashNodeFunc func(node, bool, []byte) (int, error)
|
2020-01-24 10:58:01 +00:00
|
|
|
|
|
|
|
type MerklePathLimiter struct {
|
2020-05-12 14:24:43 +00:00
|
|
|
RetainDecider RetainDecider
|
2020-05-15 08:58:36 +00:00
|
|
|
HashFunc HashNodeFunc
|
2020-01-24 10:58:01 +00:00
|
|
|
}
|
2020-01-15 13:56:50 +00:00
|
|
|
|
|
|
|
type WitnessBuilder struct {
|
2020-01-24 10:58:01 +00:00
|
|
|
root node
|
2020-01-15 13:56:50 +00:00
|
|
|
trace bool
|
|
|
|
operands []WitnessOperator
|
|
|
|
}
|
|
|
|
|
2020-05-12 07:22:45 +00:00
|
|
|
func NewWitnessBuilder(root node, trace bool) *WitnessBuilder {
|
2020-01-15 13:56:50 +00:00
|
|
|
return &WitnessBuilder{
|
2020-01-24 10:58:01 +00:00
|
|
|
root: root,
|
2020-01-15 13:56:50 +00:00
|
|
|
trace: trace,
|
|
|
|
operands: make([]WitnessOperator, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 10:58:01 +00:00
|
|
|
func (b *WitnessBuilder) Build(limiter *MerklePathLimiter) (*Witness, error) {
|
|
|
|
err := b.makeBlockWitness(b.root, []byte{}, limiter, true)
|
2020-01-15 13:56:50 +00:00
|
|
|
witness := NewWitness(b.operands)
|
|
|
|
b.operands = nil
|
|
|
|
return witness, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *WitnessBuilder) addLeafOp(key []byte, value []byte) error {
|
|
|
|
if b.trace {
|
|
|
|
fmt.Printf("LEAF_VALUE: k %x v:%x\n", key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
var op OperatorLeafValue
|
|
|
|
|
|
|
|
op.Key = make([]byte, len(key))
|
2021-07-17 02:09:56 +00:00
|
|
|
copy(op.Key, key)
|
2020-01-15 13:56:50 +00:00
|
|
|
if value != nil {
|
|
|
|
op.Value = make([]byte, len(value))
|
2021-07-17 02:09:56 +00:00
|
|
|
copy(op.Value, value)
|
2020-01-15 13:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.operands = append(b.operands, &op)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-07 11:24:55 +00:00
|
|
|
func (b *WitnessBuilder) addAccountLeafOp(key []byte, accountNode *accountNode, codeSize int) error {
|
2020-01-15 13:56:50 +00:00
|
|
|
if b.trace {
|
|
|
|
fmt.Printf("LEAF_ACCOUNT: k %x acc:%v\n", key, accountNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var op OperatorLeafAccount
|
|
|
|
op.Key = make([]byte, len(key))
|
2021-07-17 02:09:56 +00:00
|
|
|
copy(op.Key, key)
|
2020-01-15 13:56:50 +00:00
|
|
|
|
|
|
|
op.Nonce = accountNode.Nonce
|
|
|
|
op.Balance = big.NewInt(0)
|
|
|
|
op.Balance.SetBytes(accountNode.Balance.Bytes())
|
2020-01-15 14:12:33 +00:00
|
|
|
|
2020-01-15 13:56:50 +00:00
|
|
|
if !accountNode.IsEmptyRoot() || !accountNode.IsEmptyCodeHash() {
|
|
|
|
op.HasCode = true
|
|
|
|
op.HasStorage = true
|
|
|
|
}
|
|
|
|
|
2020-07-07 11:24:55 +00:00
|
|
|
op.CodeSize = uint64(codeSize)
|
|
|
|
|
2020-01-15 13:56:50 +00:00
|
|
|
b.operands = append(b.operands, &op)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *WitnessBuilder) addExtensionOp(key []byte) error {
|
|
|
|
if b.trace {
|
|
|
|
fmt.Printf("EXTENSION: k %x\n", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
var op OperatorExtension
|
|
|
|
op.Key = make([]byte, len(key))
|
2021-07-17 02:09:56 +00:00
|
|
|
copy(op.Key, key)
|
2020-01-15 13:56:50 +00:00
|
|
|
|
|
|
|
b.operands = append(b.operands, &op)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 10:58:01 +00:00
|
|
|
func (b *WitnessBuilder) makeHashNode(n node, force bool, hashNodeFunc HashNodeFunc) (hashNode, error) {
|
2020-01-15 13:56:50 +00:00
|
|
|
switch n := n.(type) {
|
|
|
|
case hashNode:
|
2020-01-24 10:58:01 +00:00
|
|
|
return n, nil
|
2020-01-15 13:56:50 +00:00
|
|
|
default:
|
2023-01-13 18:12:18 +00:00
|
|
|
var hash libcommon.Hash
|
2020-01-15 13:56:50 +00:00
|
|
|
if _, err := hashNodeFunc(n, force, hash[:]); err != nil {
|
2020-05-15 08:58:36 +00:00
|
|
|
return hashNode{}, err
|
2020-01-15 13:56:50 +00:00
|
|
|
}
|
2020-07-05 06:17:27 +00:00
|
|
|
return hashNode{hash: hash[:]}, nil
|
2020-01-15 13:56:50 +00:00
|
|
|
}
|
2020-01-24 10:58:01 +00:00
|
|
|
}
|
2020-01-15 13:56:50 +00:00
|
|
|
|
2020-01-24 10:58:01 +00:00
|
|
|
func (b *WitnessBuilder) addHashOp(n hashNode) error {
|
2020-01-15 13:56:50 +00:00
|
|
|
if b.trace {
|
2020-05-15 08:58:36 +00:00
|
|
|
fmt.Printf("HASH: type: %T v %s\n", n, n)
|
2020-01-15 13:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var op OperatorHash
|
2023-01-13 18:12:18 +00:00
|
|
|
op.Hash = libcommon.BytesToHash(n.hash)
|
2020-01-15 13:56:50 +00:00
|
|
|
|
|
|
|
b.operands = append(b.operands, &op)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *WitnessBuilder) addBranchOp(mask uint32) error {
|
|
|
|
if b.trace {
|
|
|
|
fmt.Printf("BRANCH: mask=%b\n", mask)
|
|
|
|
}
|
|
|
|
|
|
|
|
var op OperatorBranch
|
|
|
|
op.Mask = mask
|
|
|
|
|
|
|
|
b.operands = append(b.operands, &op)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *WitnessBuilder) addCodeOp(code []byte) error {
|
|
|
|
if b.trace {
|
|
|
|
fmt.Printf("CODE: len=%d\n", len(code))
|
|
|
|
}
|
|
|
|
|
|
|
|
var op OperatorCode
|
|
|
|
op.Code = make([]byte, len(code))
|
|
|
|
copy(op.Code, code)
|
|
|
|
|
|
|
|
b.operands = append(b.operands, &op)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *WitnessBuilder) addEmptyRoot() error {
|
|
|
|
if b.trace {
|
|
|
|
fmt.Printf("EMPTY ROOT\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.operands = append(b.operands, &OperatorEmptyRoot{})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-07 11:24:55 +00:00
|
|
|
func (b *WitnessBuilder) processAccountCode(n *accountNode, retainDec RetainDecider) (int, error) {
|
2020-01-15 13:56:50 +00:00
|
|
|
if n.IsEmptyRoot() && n.IsEmptyCodeHash() {
|
2020-07-07 11:24:55 +00:00
|
|
|
return 0, nil
|
2020-01-15 13:56:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 14:24:43 +00:00
|
|
|
if n.code == nil || (retainDec != nil && !retainDec.IsCodeTouched(n.CodeHash)) {
|
2020-07-07 11:24:55 +00:00
|
|
|
codeSize := n.codeSize
|
|
|
|
if n.code != nil {
|
|
|
|
codeSize = len(n.code)
|
|
|
|
}
|
|
|
|
return codeSize, b.addHashOp(hashNode{hash: n.CodeHash[:]})
|
2020-01-15 13:56:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 11:24:55 +00:00
|
|
|
return len(n.code), b.addCodeOp(n.code)
|
2020-01-15 13:56:50 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 10:58:01 +00:00
|
|
|
func (b *WitnessBuilder) processAccountStorage(n *accountNode, hex []byte, limiter *MerklePathLimiter) error {
|
2020-01-15 13:56:50 +00:00
|
|
|
if n.IsEmptyRoot() && n.IsEmptyCodeHash() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.storage == nil {
|
|
|
|
return b.addEmptyRoot()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we substitute rs parameter for storageRs, because it needs to become the default
|
2020-01-24 10:58:01 +00:00
|
|
|
return b.makeBlockWitness(n.storage, hex, limiter, true)
|
2020-01-15 13:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *WitnessBuilder) makeBlockWitness(
|
2020-01-24 10:58:01 +00:00
|
|
|
nd node, hex []byte, limiter *MerklePathLimiter, force bool) error {
|
2020-01-15 13:56:50 +00:00
|
|
|
|
|
|
|
processAccountNode := func(key []byte, storageKey []byte, n *accountNode) error {
|
2020-05-12 14:24:43 +00:00
|
|
|
var retainDec RetainDecider
|
2020-04-08 12:59:30 +00:00
|
|
|
if limiter != nil {
|
2020-05-12 14:24:43 +00:00
|
|
|
retainDec = limiter.RetainDecider
|
2020-04-08 12:59:30 +00:00
|
|
|
}
|
2023-03-25 05:13:27 +00:00
|
|
|
var codeSize int
|
2020-07-07 11:24:55 +00:00
|
|
|
var err error
|
|
|
|
if codeSize, err = b.processAccountCode(n, retainDec); err != nil {
|
2020-01-15 13:56:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-01-24 10:58:01 +00:00
|
|
|
if err := b.processAccountStorage(n, storageKey, limiter); err != nil {
|
2020-01-15 13:56:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-07-07 11:24:55 +00:00
|
|
|
return b.addAccountLeafOp(key, n, codeSize)
|
2020-01-15 13:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch n := nd.(type) {
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
case valueNode:
|
|
|
|
return b.addLeafOp(hex, n)
|
|
|
|
case *accountNode:
|
|
|
|
return processAccountNode(hex, hex, n)
|
|
|
|
case *shortNode:
|
|
|
|
h := n.Key
|
|
|
|
// Remove terminator
|
|
|
|
if h[len(h)-1] == 16 {
|
|
|
|
h = h[:len(h)-1]
|
|
|
|
}
|
|
|
|
hexVal := concat(hex, h...)
|
|
|
|
switch v := n.Val.(type) {
|
|
|
|
case valueNode:
|
|
|
|
return b.addLeafOp(n.Key, v[:])
|
|
|
|
case *accountNode:
|
|
|
|
return processAccountNode(n.Key, hexVal, v)
|
|
|
|
default:
|
2020-01-24 10:58:01 +00:00
|
|
|
if err := b.makeBlockWitness(n.Val, hexVal, limiter, false); err != nil {
|
2020-01-15 13:56:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.addExtensionOp(n.Key)
|
|
|
|
}
|
|
|
|
case *duoNode:
|
2020-05-12 14:24:43 +00:00
|
|
|
hashOnly := limiter != nil && !limiter.RetainDecider.Retain(hex) // Save this because rl can move on to other keys during the recursive invocation
|
2020-01-15 13:56:50 +00:00
|
|
|
if b.trace {
|
2020-05-12 14:24:43 +00:00
|
|
|
fmt.Printf("b.retainDec.Retain(%x) -> %v\n", hex, !hashOnly)
|
2020-01-15 13:56:50 +00:00
|
|
|
}
|
|
|
|
if hashOnly {
|
2020-01-24 10:58:01 +00:00
|
|
|
hn, err := b.makeHashNode(n, force, limiter.HashFunc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.addHashOp(hn)
|
2020-01-15 13:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i1, i2 := n.childrenIdx()
|
|
|
|
|
2020-01-24 10:58:01 +00:00
|
|
|
if err := b.makeBlockWitness(n.child1, expandKeyHex(hex, i1), limiter, false); err != nil {
|
2020-01-15 13:56:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-01-24 10:58:01 +00:00
|
|
|
if err := b.makeBlockWitness(n.child2, expandKeyHex(hex, i2), limiter, false); err != nil {
|
2020-01-15 13:56:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.addBranchOp(n.mask)
|
|
|
|
|
|
|
|
case *fullNode:
|
2020-05-12 14:24:43 +00:00
|
|
|
hashOnly := limiter != nil && !limiter.RetainDecider.Retain(hex) // Save this because rs can move on to other keys during the recursive invocation
|
2020-01-15 13:56:50 +00:00
|
|
|
if hashOnly {
|
2020-01-24 10:58:01 +00:00
|
|
|
hn, err := b.makeHashNode(n, force, limiter.HashFunc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.addHashOp(hn)
|
2020-01-15 13:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var mask uint32
|
|
|
|
for i, child := range n.Children {
|
|
|
|
if child != nil {
|
2020-01-24 10:58:01 +00:00
|
|
|
if err := b.makeBlockWitness(child, expandKeyHex(hex, byte(i)), limiter, false); err != nil {
|
2020-01-15 13:56:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-08-10 12:04:13 +00:00
|
|
|
mask |= uint32(1) << uint(i)
|
2020-01-15 13:56:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return b.addBranchOp(mask)
|
|
|
|
|
|
|
|
case hashNode:
|
2020-05-12 14:24:43 +00:00
|
|
|
hashOnly := limiter == nil || !limiter.RetainDecider.Retain(hex)
|
2020-01-15 13:56:50 +00:00
|
|
|
if hashOnly {
|
2020-01-24 10:58:01 +00:00
|
|
|
return b.addHashOp(n)
|
2020-01-15 13:56:50 +00:00
|
|
|
}
|
2020-05-15 08:58:36 +00:00
|
|
|
return fmt.Errorf("unexpected hashNode: %s, at hex: %x, (%d), hashOnly: %t", n, hex, len(hex), hashOnly)
|
2020-01-15 13:56:50 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected node type: %T", nd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandKeyHex(hex []byte, nibble byte) []byte {
|
|
|
|
result := make([]byte, len(hex)+1)
|
|
|
|
copy(result, hex)
|
|
|
|
result[len(hex)] = nibble
|
|
|
|
return result
|
|
|
|
}
|