-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
149 lines (127 loc) · 3.44 KB
/
wrapper.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Package arbotree provides the functions for creating and managing an arbo
// merkletree adapted to the CensusTree interface
package arbotree
import (
"bytes"
"encoding/hex"
"fmt"
"path/filepath"
"strings"
"sync/atomic"
"time"
"github.com/arnaucube/arbo"
"github.com/iden3/go-merkletree/db/leveldb"
"go.vocdoni.io/dvote/censustree"
)
type Tree struct {
Tree *arbo.Tree
public uint32
lastAccessUnix int64 // a unix timestamp, used via sync/atomic
}
func (t *Tree) Init(name, storageDir string) error {
dbDir := filepath.Join(storageDir, "arbotree.db."+strings.TrimSpace(name))
storage, err := leveldb.NewLevelDbStorage(dbDir, false) // TODO TMP
if err != nil {
return err
}
mt, err := arbo.NewTree(storage, 140, arbo.HashFunctionBlake2b) // TODO here the hash function would depend on the usage
if err != nil {
return err
}
t.Tree = mt
return nil
}
func (t *Tree) MaxKeySize() int {
return t.Tree.HashFunction().Len()
}
func (t *Tree) LastAccess() int64 {
return atomic.LoadInt64(&t.lastAccessUnix)
}
func (t *Tree) updateAccessTime() {
atomic.StoreInt64(&t.lastAccessUnix, time.Now().Unix())
}
// Publish makes a merkle tree available for queries.
// Application layer should check IsPublish() before considering the Tree available.
func (t *Tree) Publish() {
atomic.StoreUint32(&t.public, 1)
}
// UnPublish makes a merkle tree not available for queries
func (t *Tree) UnPublish() {
atomic.StoreUint32(&t.public, 0)
}
// IsPublic returns true if the tree is available
func (t *Tree) IsPublic() bool {
return atomic.LoadUint32(&t.public) == 1
}
func (t *Tree) Add(index, value []byte) error {
t.updateAccessTime()
return t.Tree.Add(index, value)
}
func (t *Tree) AddBatch(indexes, values [][]byte) ([]int, error) {
t.updateAccessTime()
return t.Tree.AddBatch(indexes, values)
}
func (t *Tree) GenProof(index, value []byte) ([]byte, error) {
t.updateAccessTime()
v, siblings, err := t.Tree.GenProof(index)
if err != nil {
return nil, err
}
if !bytes.Equal(v, value) {
return nil, fmt.Errorf("value does not match %s!=%s", hex.EncodeToString(v), hex.EncodeToString(value))
}
return siblings, nil
}
func (t *Tree) CheckProof(index, value, root, mproof []byte) (bool, error) {
t.updateAccessTime()
if root == nil {
root = t.Root()
}
return arbo.CheckProof(t.Tree.HashFunction(), index, value, root, mproof)
}
func (t *Tree) Root() []byte {
t.updateAccessTime()
return t.Tree.Root()
}
func (t *Tree) Dump(root []byte) ([]byte, error) {
return t.Tree.Dump() // TODO pass root once arbo is updated
}
func (t *Tree) DumpPlain(root []byte) ([][]byte, [][]byte, error) {
t.updateAccessTime()
var indexes, values [][]byte
// TODO pass root once arbo is updated
err := t.Tree.Iterate(func(k, v []byte) {
if v[0] != arbo.PrefixValueLeaf {
return
}
leafK, leafV := arbo.ReadLeafValue(v)
indexes = append(indexes, leafK)
values = append(values, leafV)
})
return indexes, values, err
}
func (t *Tree) ImportDump(data []byte) error {
t.updateAccessTime()
return t.Tree.ImportDump(data)
}
func (t *Tree) Size(root []byte) (int64, error) {
count := 0
err := t.Tree.Iterate(func(k, v []byte) {
if v[0] != arbo.PrefixValueLeaf {
return
}
count++
})
return int64(count), err
}
func (t *Tree) Snapshot(root []byte) (censustree.Tree, error) {
// TODO
return t, nil
}
func (t *Tree) HashExists(hash []byte) (bool, error) {
_, _, err := t.Tree.Get(hash)
if err != nil {
return false, err
}
return true, nil
}