/
merklecachedtree.go
57 lines (46 loc) · 1.6 KB
/
merklecachedtree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2019 DxChain, All rights reserved.
// Use of this source code is governed by an Apache
// License 2.0 that can be found in the LICENSE file.
package merkle
import (
"errors"
"hash"
)
//CachedTree will store the leaf's data data instead of hash
type CachedTree struct {
subTreeCachedHeight uint64
confirmStorageProofIndex uint64
Tree
}
// NewCachedTree return a cachedTree
func NewCachedTree(h hash.Hash, height uint64) *CachedTree {
return &CachedTree{
subTreeCachedHeight: height,
Tree: Tree{
hash: h,
usedAsCached: true,
},
}
}
// SetStorageProofIndex must be called on an empty tree.
func (ct *CachedTree) SetStorageProofIndex(i uint64) error {
if ct.top != nil {
return errors.New("must be called on an empty tree")
}
ct.confirmStorageProofIndex = i
return ct.Tree.SetStorageProofIndex(i / (1 << ct.subTreeCachedHeight))
}
// ProofList construct a storage proof result set for
// the cached tree that has established the storage proof index
func (ct *CachedTree) ProofList(cachedTreeProofList [][]byte) (merkleRoot []byte, proofList [][]byte, storageProofIndex uint64, numLeaves uint64) {
cachedSubtree := uint64(1) << ct.subTreeCachedHeight
numLeaves = cachedSubtree * ct.leafIndex
//get storage proof from merkle tree
merkleRoot, merkleTreeProofList, _, _ := ct.Tree.ProofList()
if len(merkleTreeProofList) < 1 {
return merkleRoot, nil, ct.confirmStorageProofIndex, numLeaves
}
//we already have the data from storageProofIndex
proofList = append(cachedTreeProofList, merkleTreeProofList[1:]...)
return merkleRoot, proofList, ct.confirmStorageProofIndex, numLeaves
}