2020-05-24 11:23:29 +00:00
|
|
|
package mpt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-10-06 13:37:23 +00:00
|
|
|
"errors"
|
2020-05-24 11:23:29 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetProof returns a proof that the key belongs to t.
|
|
|
|
// The proof consists of serialized nodes occurring on the path from the root to the leaf of key.
|
2020-05-24 11:23:29 +00:00
|
|
|
func (t *Trie) GetProof(key []byte) ([][]byte, error) {
|
|
|
|
var proof [][]byte
|
2021-10-06 13:37:23 +00:00
|
|
|
if len(key) > MaxKeyLength {
|
|
|
|
return nil, errors.New("key is too big")
|
|
|
|
}
|
2020-05-24 11:23:29 +00:00
|
|
|
path := toNibbles(key)
|
|
|
|
r, err := t.getProof(t.root, path, &proof)
|
|
|
|
if err != nil {
|
|
|
|
return proof, err
|
|
|
|
}
|
|
|
|
t.root = r
|
|
|
|
return proof, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trie) getProof(curr Node, path []byte, proofs *[][]byte) (Node, error) {
|
|
|
|
switch n := curr.(type) {
|
|
|
|
case *LeafNode:
|
|
|
|
if len(path) == 0 {
|
2024-03-04 18:09:36 +00:00
|
|
|
*proofs = append(*proofs, bytes.Clone(n.Bytes()))
|
2020-05-24 11:23:29 +00:00
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
case *BranchNode:
|
2024-03-04 18:09:36 +00:00
|
|
|
*proofs = append(*proofs, bytes.Clone(n.Bytes()))
|
2020-05-24 11:23:29 +00:00
|
|
|
i, path := splitPath(path)
|
|
|
|
r, err := t.getProof(n.Children[i], path, proofs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
n.Children[i] = r
|
|
|
|
return n, nil
|
|
|
|
case *ExtensionNode:
|
|
|
|
if bytes.HasPrefix(path, n.key) {
|
2024-03-04 18:09:36 +00:00
|
|
|
*proofs = append(*proofs, bytes.Clone(n.Bytes()))
|
2020-05-24 11:23:29 +00:00
|
|
|
r, err := t.getProof(n.next, path[len(n.key):], proofs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
n.next = r
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
case *HashNode:
|
2021-08-03 14:10:46 +00:00
|
|
|
r, err := t.getFromStore(n.Hash())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-05-24 11:23:29 +00:00
|
|
|
}
|
2021-08-03 14:10:46 +00:00
|
|
|
return t.getProof(r, path, proofs)
|
2020-05-24 11:23:29 +00:00
|
|
|
}
|
|
|
|
return nil, ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyProof verifies that path indeed belongs to a MPT with the specified root hash.
|
2022-04-20 18:30:09 +00:00
|
|
|
// It also returns the value for the key.
|
2020-05-24 11:23:29 +00:00
|
|
|
func VerifyProof(rh util.Uint256, key []byte, proofs [][]byte) ([]byte, bool) {
|
|
|
|
path := toNibbles(key)
|
2022-01-28 08:56:33 +00:00
|
|
|
tr := NewTrie(NewHashNode(rh), ModeAll, storage.NewMemCachedStore(storage.NewMemoryStore()))
|
2020-05-24 11:23:29 +00:00
|
|
|
for i := range proofs {
|
|
|
|
h := hash.DoubleSha256(proofs[i])
|
2022-02-16 14:48:15 +00:00
|
|
|
tr.Store.Put(makeStorageKey(h), proofs[i])
|
2020-05-24 11:23:29 +00:00
|
|
|
}
|
2021-10-07 13:56:27 +00:00
|
|
|
_, leaf, _, err := tr.getWithPath(tr.root, path, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2024-03-04 18:09:36 +00:00
|
|
|
return bytes.Clone(leaf.(*LeafNode).value), true
|
2020-05-24 11:23:29 +00:00
|
|
|
}
|