forked from holochain/holochain-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht.go
358 lines (323 loc) · 8.58 KB
/
dht.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright (C) 2013-2017, The MetaCurrency Project (Eric Harris-Braun, Arthur Brock, et. al.)
// Use of this source code is governed by GPLv3 found in the LICENSE file
//----------------------------------------------------------------------------------------
// DHT implements the distributed hash table
package holochain
import (
"errors"
"fmt"
q "github.com/golang-collections/go-datastructures/queue"
peer "github.com/libp2p/go-libp2p-peer"
"github.com/tidwall/buntdb"
"strings"
)
var ErrDHTExpectedHashInBody error = errors.New("expected hash")
var ErrDHTExpectedMetaInBody error = errors.New("expected meta struct")
// DHT struct holds the data necessary to run the distributed hash table
type DHT struct {
h *Holochain // pointer to the holochain this DHT is part of
Queue q.Queue // a queue for incoming puts
db *buntdb.DB
}
// Meta holds data that can be associated with a hash
// @todo, we should also be storing the meta-data source
type Meta struct {
H Hash // hash of meta-data associated
T string // meta-data type identifier
V []byte // meta-data
}
// Meta holds a putMeta request
type MetaReq struct {
O Hash // original data on which to put the meta
M Hash // hash of the meta-data
T string // meta type
}
// MetaQuery holds a getMeta query
type MetaQuery struct {
H Hash
T string
// order
// filter, etc
}
// NewDHT creates a new DHT structure
func NewDHT(h *Holochain) *DHT {
dht := DHT{
h: h,
}
db, err := buntdb.Open(h.path + "/dht.db")
if err != nil {
panic(err)
}
db.CreateIndex("meta", "meta:*", buntdb.IndexString)
dht.db = db
return &dht
}
// put stores a value to the DHT store
// N.B. This call assumes that the value has already been validated
func (dht *DHT) put(key Hash, src peer.ID, value []byte) (err error) {
k := key.String()
err = dht.db.Update(func(tx *buntdb.Tx) error {
_, _, err := tx.Set("entry:"+k, string(value), nil)
if err != nil {
return err
}
_, _, err = tx.Set("src:"+k, peer.IDB58Encode(src), nil)
return err
})
// dht.store[k] = value
// dht.sources[k] = src
return
}
// exists checks for the existence of the hash in the store
func (dht *DHT) exists(key Hash) (err error) {
err = dht.db.View(func(tx *buntdb.Tx) error {
_, err := tx.Get("entry:" + key.String())
if err == buntdb.ErrNotFound {
err = ErrHashNotFound
}
return err
})
return
}
// returns the source of a given hash
func (dht *DHT) source(key Hash) (id peer.ID, err error) {
err = dht.db.View(func(tx *buntdb.Tx) error {
val, err := tx.Get("src:" + key.String())
if err == buntdb.ErrNotFound {
err = ErrHashNotFound
}
if err == nil {
id, err = peer.IDB58Decode(val)
}
return err
})
return
}
// get retrieves a value from the DHT store
func (dht *DHT) get(key Hash) (data []byte, err error) {
err = dht.db.View(func(tx *buntdb.Tx) error {
val, err := tx.Get("entry:" + key.String())
if err == buntdb.ErrNotFound {
err = ErrHashNotFound
}
if err == nil {
data = []byte(val)
}
return err
})
return
}
// putMeta associates a value with a stored hash
// N.B. this function assumes that the data associated has been properly retrieved
// and validated from the cource chain
func (dht *DHT) putMeta(key Hash, metaKey Hash, metaType string, entry Entry) (err error) {
k := key.String()
err = dht.db.Update(func(tx *buntdb.Tx) error {
_, err := tx.Get("entry:" + k)
if err == buntdb.ErrNotFound {
return ErrHashNotFound
}
mk := metaKey.String()
var b []byte
b, err = entry.Marshal()
if err == nil {
_, _, err = tx.Set("meta:"+k+":"+mk+":"+metaType, string(b), nil)
}
return err
})
return
}
func filter(ss []Meta, test func(*Meta) bool) (ret []Meta) {
for _, s := range ss {
if test(&s) {
ret = append(ret, s)
}
}
return
}
// getMeta retrieves values associated with hashes
func (dht *DHT) getMeta(key Hash, metaType string) (results []Entry, err error) {
k := key.String()
err = dht.db.View(func(tx *buntdb.Tx) error {
_, err := tx.Get("entry:" + k)
if err == buntdb.ErrNotFound {
return ErrHashNotFound
}
results = make([]Entry, 0)
err = tx.Ascend("meta", func(key, value string) bool {
x := strings.Split(key, ":")
if string(x[1]) == k && string(x[3]) == metaType {
var entry GobEntry
err := entry.Unmarshal([]byte(value))
if err != nil {
return false
}
results = append(results, &entry)
}
return true
})
if len(results) == 0 {
err = fmt.Errorf("No values for %s", metaType)
}
return err
})
return
}
// SendPut initiates publishing a particular Hash to the DHT.
// This command only sends the hash, because the expectation is that DHT nodes will start to
// communicate back to Source node (the node that makes this call) to get the data for validation
func (dht *DHT) SendPut(key Hash) (err error) {
n, err := dht.FindNodeForHash(key)
if err != nil {
return
}
_, err = dht.send(n.HashAddr, PUT_REQUEST, key)
return
}
// SendGet initiates retrieving a value from the DHT
func (dht *DHT) SendGet(key Hash) (response interface{}, err error) {
n, err := dht.FindNodeForHash(key)
if err != nil {
return
}
response, err = dht.send(n.HashAddr, GET_REQUEST, key)
return
}
// SendPutMeta initiates associating Meta data with particular Hash on the DHT.
// This command assumes that the data has been committed to your local chain, and the hash of that
// data is what get's sent in the MetaReq
func (dht *DHT) SendPutMeta(req MetaReq) (err error) {
n, err := dht.FindNodeForHash(req.O)
if err != nil {
return
}
_, err = dht.send(n.HashAddr, PUTMETA_REQUEST, req)
return
}
// SendGetMeta initiates retrieving meta data from the DHT
func (dht *DHT) SendGetMeta(query MetaQuery) (response interface{}, err error) {
n, err := dht.FindNodeForHash(query.H)
if err != nil {
return
}
response, err = dht.send(n.HashAddr, GETMETA_REQUEST, query)
return
}
// Send sends a message to the node
func (dht *DHT) send(to peer.ID, t MsgType, body interface{}) (response interface{}, err error) {
return dht.h.Send(DHTProtocol, to, t, body, DHTReceiver)
}
// FindNodeForHash gets the nearest node to the neighborhood of the hash
func (dht *DHT) FindNodeForHash(key Hash) (n *Node, err error) {
// for now, the node it returns is self!
pid, err := peer.IDFromPrivateKey(dht.h.Agent().PrivKey())
if err != nil {
return
}
var node Node
node.HashAddr = pid
n = &node
return
}
func (dht *DHT) handlePutReqs() (err error) {
x, err := dht.Queue.Get(10)
if err == nil {
for _, r := range x {
m := r.(*Message)
from := r.(*Message).From
switch t := m.Body.(type) {
case Hash:
log.Debugf("handling put: %v", m)
var r interface{}
r, err = dht.h.Send(SourceProtocol, from, SRC_VALIDATE, t, SrcReceiver)
if err != nil {
return
}
// @TODO do the validation here!!!
entry := r.(Entry)
b, err := entry.Marshal()
if err == nil {
err = dht.put(t, from, b)
}
case MetaReq:
log.Debugf("handling putmeta: %v", m)
var r interface{}
r, err = dht.h.Send(SourceProtocol, from, SRC_VALIDATE, t.M, SrcReceiver)
if err != nil {
return
}
// @TODO do the validation here!!!
err = dht.putMeta(t.O, t.M, t.T, r.(Entry))
default:
err = errors.New("unexpected body type in handlePutReqs")
}
}
}
return
}
// DHTReceiver handles messages on the dht protocol
func DHTReceiver(h *Holochain, m *Message) (response interface{}, err error) {
switch m.Type {
case PUT_REQUEST:
switch m.Body.(type) {
case Hash:
err = h.dht.Queue.Put(m)
if err == nil {
response = "queued"
}
default:
err = ErrDHTExpectedHashInBody
}
return
case GET_REQUEST:
switch t := m.Body.(type) {
case Hash:
var b []byte
b, err = h.dht.get(t)
if err == nil {
var e GobEntry
err = e.Unmarshal(b)
if err == nil {
response = &e
}
}
default:
err = ErrDHTExpectedHashInBody
}
return
case PUTMETA_REQUEST:
switch t := m.Body.(type) {
case MetaReq:
err = h.dht.exists(t.O)
if err == nil {
err = h.dht.Queue.Put(m)
if err == nil {
response = "queued"
}
}
default:
err = ErrDHTExpectedMetaInBody
}
case GETMETA_REQUEST:
switch t := m.Body.(type) {
case MetaQuery:
response, err = h.dht.getMeta(t.H, t.T)
default:
err = ErrDHTExpectedMetaInBody
}
default:
err = fmt.Errorf("message type %d not in holochain-dht protocol", int(m.Type))
}
return
}
// StartDHT initiates listening for DHT protocol messages on the node
func (dht *DHT) StartDHT() (err error) {
err = dht.h.node.StartProtocol(dht.h, DHTProtocol, DHTReceiver)
if err == nil {
e := dht.h.BSget()
if e != nil {
log.Infof("error in BSget: %s", e.Error())
}
}
return
}