forked from gtfierro/wave
-
Notifications
You must be signed in to change notification settings - Fork 2
/
storage.go
300 lines (274 loc) · 6.41 KB
/
storage.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"context"
"crypto/ecdsa"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"sync"
"github.com/gogo/protobuf/proto"
"github.com/google/trillian"
"github.com/immesys/wave/iapi"
"github.com/immesys/wave/storage/simplehttp"
)
const logServer = "localhost:8092"
const mapServer = "localhost:8090"
var PublicKey string
var PrivateKey string
var PrivateKeyUnpacked *ecdsa.PrivateKey
var TreeID_Op int64
var TreeID_Map int64
var TreeID_Root int64
type PromiseObject struct {
Promise *simplehttp.MergePromise
Seals []*simplehttp.V1CertifierSeal
Key []byte
Value []byte
}
var promisemu sync.Mutex
var promises map[string]*PromiseObject
func initstorage() {
optreeid := os.Getenv("VLDM_TREE_OPERATIONS")
maptreeid := os.Getenv("VLDM_TREE_MAP")
roottreeid := os.Getenv("VLDM_TREE_ROOT")
if optreeid == "" || maptreeid == "" || roottreeid == "" {
fmt.Printf("Missing tree ids\n")
os.Exit(1)
}
v, err := strconv.ParseInt(optreeid, 10, 64)
if err != nil {
fmt.Printf("could not parse tree id:%v\n", err)
}
TreeID_Op = v
v, err = strconv.ParseInt(roottreeid, 10, 64)
if err != nil {
fmt.Printf("could not parse tree id:%v\n", err)
}
TreeID_Root = v
v, err = strconv.ParseInt(maptreeid, 10, 64)
if err != nil {
fmt.Printf("could not parse tree id:%v\n", err)
}
TreeID_Map = v
pub, err := ioutil.ReadFile("vldm_public.pem")
if err != nil {
fmt.Printf("could not read public key: %v\n", err)
os.Exit(1)
}
priv, err := ioutil.ReadFile("vldm_private.pem")
if err != nil {
fmt.Printf("could not read private key: %v\n", err)
os.Exit(1)
}
PublicKey = string(pub)
PrivateKey = string(priv)
pk, err := ParsePrivateKey(priv)
if err != nil {
fmt.Printf("could not parse private key\n", err)
}
PrivateKeyUnpacked = pk
queueindexes = make(map[string]int64)
promises = make(map[string]*PromiseObject)
}
type GetMapKeyResponse struct {
Unmerged bool
//If unmerged
MergePromise *simplehttp.MergePromise
//If merged
SignedMapRoot []byte
MapInclusion []byte
SignedLogRoot []byte
LogInclusion []byte
Value []byte
LogConsistency [][]byte
}
func GetPromise(key []byte) *PromiseObject {
promisemu.Lock()
p := promises[string(key)]
promisemu.Unlock()
return p
}
var ErrMapRootTooOld = errors.New("No recent map roots certified by the given ids found")
func GetMapKeyValue(key []byte, trustedSize int64) (*GetMapKeyResponse, error) {
dbSMR := DB.GetLatestMapRoot()
checkPromise := func() *GetMapKeyResponse {
pv := GetPromise(key)
if pv != nil {
realValue, err := DB.RetrieveObject(pv.Value)
if err != nil {
panic(err)
}
return &GetMapKeyResponse{
Unmerged: true,
MergePromise: pv.Promise,
Value: realValue,
}
}
return nil
}
if dbSMR == nil {
p := checkPromise()
if p == nil {
return nil, ErrMapRootTooOld
}
return p, nil
}
resp2, err := vmap.GetLeavesByRevision(context.Background(), &trillian.GetMapLeavesByRevisionRequest{
MapId: TreeID_Map,
Index: [][]byte{key},
Revision: int64(dbSMR.Revision),
})
if err != nil {
panic(err)
}
if resp2.MapLeafInclusion[0].Leaf.LeafValue == nil {
//check for promise rather
p := checkPromise()
if p != nil {
return p, nil
}
}
//Get consistency proof if required
var consistency [][]byte
if dbSMR.LogSize > trustedSize && trustedSize != 0 {
cp, err := logclient.GetConsistencyProof(context.Background(), &trillian.GetConsistencyProofRequest{
LogId: TreeID_Root,
FirstTreeSize: trustedSize,
SecondTreeSize: dbSMR.LogSize,
})
if err != nil {
panic(err)
}
consistency = cp.Proof.Hashes
}
inclusion, err := proto.Marshal(resp2.MapLeafInclusion[0])
if err != nil {
panic(err)
}
smr, err := proto.Marshal(resp2.MapRoot)
if err != nil {
panic(err)
}
if resp2.MapLeafInclusion[0].Leaf.LeafValue == nil {
return &GetMapKeyResponse{
SignedMapRoot: smr,
MapInclusion: inclusion,
SignedLogRoot: dbSMR.LogSignedRoot,
LogInclusion: dbSMR.LogInclusion,
LogConsistency: consistency,
}, nil
} else {
realValue, err := DB.RetrieveObject(resp2.MapLeafInclusion[0].Leaf.LeafValue)
if err != nil {
return nil, err
}
return &GetMapKeyResponse{
SignedMapRoot: smr,
MapInclusion: inclusion,
Value: realValue,
SignedLogRoot: dbSMR.LogSignedRoot,
LogInclusion: dbSMR.LogInclusion,
LogConsistency: consistency,
}, nil
}
}
func InsertKeyValue(key []byte, value []byte) (*simplehttp.MergePromise, error) {
//TODO check for existing merge promise for this value
hi := iapi.KECCAK256.Instance(value)
hasharr := hi.Value()
mp, err := MakeMergePromise(key, hasharr, PrivateKeyUnpacked)
if err != nil {
return nil, err
}
po := &PromiseObject{
Promise: mp,
Key: key,
Value: hasharr,
}
promisemu.Lock()
promises[string(key)] = po
promisemu.Unlock()
poj, err := json.Marshal(po)
if err != nil {
fmt.Printf("failed to marshal promise object: %v\n", err)
}
ctx := context.Background()
llf := &trillian.LogLeaf{
LeafValue: poj,
}
_, err = logclient.QueueLeaf(ctx, &trillian.QueueLeafRequest{
LogId: TreeID_Op,
Leaf: llf,
})
if err != nil {
panic(err)
}
err = DB.InsertObject(hasharr, value)
if err != nil {
return nil, err
}
return mp, nil
}
func Exists(queueid []byte, index int64) bool {
tohash := make([]byte, 40)
copy(tohash[:32], queueid)
binary.LittleEndian.PutUint64(tohash[32:], uint64(index+1))
hi := iapi.KECCAK256.Instance(tohash)
hiarr := hi.Value()
resp2, err := vmap.GetLeaves(context.Background(), &trillian.GetMapLeavesRequest{
MapId: TreeID_Map,
Index: [][]byte{hiarr},
})
if err != nil {
panic(err)
}
return resp2.MapLeafInclusion[0].Leaf.LeafValue != nil
}
var queueindexes map[string]int64
var queuemu sync.Mutex
func GetIndex(queueid []byte) int64 {
queuemu.Lock()
idx, ok := queueindexes[string(queueid)]
queuemu.Unlock()
if ok {
return idx
}
if !Exists(queueid, 0) {
return -1
}
start := int64(128)
for Exists(queueid, start) {
start <<= 4
}
pivot := start / 2
interval := start / 4
for {
if Exists(queueid, pivot) {
if interval == 0 {
break
}
pivot += interval
interval /= 2
} else {
if interval == 0 {
pivot--
break
}
pivot -= interval
interval /= 2
}
}
queuemu.Lock()
queueindexes[string(queueid)] = pivot
queuemu.Unlock()
return pivot
}
func SetIndex(queueid []byte, id int64) {
queuemu.Lock()
queueindexes[string(queueid)] = id
queuemu.Unlock()
}