forked from tranvictor/ethashproof
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dag_merkle_proof.go
34 lines (30 loc) · 1.09 KB
/
dag_merkle_proof.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
package ethashproof
import (
"fmt"
"os"
"github.com/tranvictor/ethashproof/ethash"
"github.com/tranvictor/ethashproof/mtree"
)
func CalculateProof(blockno uint64, index uint32, cache *DatasetMerkleTreeCache) (mtree.Word, []mtree.Hash, error) {
dt := mtree.NewSHA256DagTree()
fullSize := ethash.DAGSize(blockno)
fullSizeIn128Resolution := fullSize / 128
branchDepth := len(fmt.Sprintf("%b", fullSizeIn128Resolution-1))
dt.RegisterStoredLevel(uint32(uint64(branchDepth)-CACHE_LEVEL), uint32(0))
liveLevel := uint64(branchDepth) - CACHE_LEVEL
subtreeStart := index >> liveLevel << liveLevel
dt.RegisterIndex(index - subtreeStart)
path := ethash.PathToDAG(uint64(blockno/30000), ethash.DefaultDir)
f, err := os.Open(path)
if err != nil {
return mtree.Word{}, []mtree.Hash{}, err
}
defer f.Close()
processDuringRead(f, int(subtreeStart), uint32(1<<(uint64(branchDepth)-CACHE_LEVEL)), dt)
dt.Finalize()
element := dt.AllDAGElements()[0]
proof := dt.ProofsForRegisteredIndices()[0]
cacheIndex := index >> liveLevel
proof = append(proof, cache.Proofs[cacheIndex]...)
return element, proof, nil
}