-
Notifications
You must be signed in to change notification settings - Fork 3
/
node.go
486 lines (399 loc) · 12.4 KB
/
node.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package service
import (
"context"
"fmt"
"io/ioutil"
"sync"
"time"
"github.com/ipfs/go-datastore"
ipns "github.com/ipfs/go-ipns"
"github.com/libp2p/go-libp2p"
circuit "github.com/libp2p/go-libp2p-circuit"
connmgr "github.com/libp2p/go-libp2p-connmgr"
crypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
dht "github.com/libp2p/go-libp2p-kad-dht"
dhtopts "github.com/libp2p/go-libp2p-kad-dht/opts"
pubsub "github.com/libp2p/go-libp2p-pubsub"
record "github.com/libp2p/go-libp2p-record"
msdnDiscovery "github.com/libp2p/go-libp2p/p2p/discovery"
"github.com/manifoldco/promptui"
ma "github.com/multiformats/go-multiaddr"
localContext "github.com/unitychain/zkvote-node/zkvote/model/context"
"github.com/unitychain/zkvote-node/zkvote/service/manager"
"github.com/unitychain/zkvote-node/zkvote/service/store"
"github.com/unitychain/zkvote-node/zkvote/service/utils"
)
// node client version
const DB_PEER_ID = "peerID"
// Node ...
type Node struct {
*localContext.Context
*manager.Manager
dht *dht.IpfsDHT
pubsub *pubsub.PubSub
db datastore.Batching
mdnsPeers map[peer.ID]peer.AddrInfo
streams chan network.Stream
}
func loadPrivateKey(ds datastore.Batching) (crypto.PrivKey, error) {
var prvKey crypto.PrivKey
var err error
tmpStore, _ := store.NewStore(nil, ds)
strKey, _ := tmpStore.GetLocal(DB_PEER_ID)
if 0 == len(strKey) {
utils.LogInfof("Generate a new private key!")
prvKey, _, _ = crypto.GenerateKeyPair(crypto.ECDSA, 0)
// priv, _, _ := crypto.GenerateKeyPair(crypto.Ed25519, 0)
b, _ := prvKey.Raw()
tmpStore.PutLocal(DB_PEER_ID, utils.Remove0x(utils.GetHexStringFromBytes(b)))
} else {
prvKey, err = crypto.UnmarshalECDSAPrivateKey(utils.GetBytesFromHexString(strKey))
if err != nil {
return nil, fmt.Errorf("unmarshal private key error, %v", err)
}
}
b, _ := prvKey.GetPublic().Bytes()
utils.LogInfof("peer pub key: %v", utils.GetHexStringFromBytes(b))
return prvKey, err
}
// NewNode create a new node with its implemented protocols
func NewNode(ctx context.Context, ds datastore.Batching, relay bool, bucketSize int) (*Node, error) {
cmgr := connmgr.NewConnManager(1500, 2000, time.Minute)
// Ignoring most errors for brevity
// See echo example for more details and better implementation
prvKey, err := loadPrivateKey(ds)
if err != nil {
panic(err)
}
// listen, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port))
opts := []libp2p.Option{libp2p.ConnectionManager(cmgr), libp2p.Identity(prvKey)}
if relay {
opts = append(opts, libp2p.EnableRelay(circuit.OptHop))
}
host, err := libp2p.New(context.Background(), opts...)
if err != nil {
panic(err)
}
d2, err := dht.New(context.Background(), host, dhtopts.BucketSize(bucketSize), dhtopts.Datastore(ds), dhtopts.Validator(record.NamespacedValidator{
"pk": record.PublicKeyValidator{},
"ipns": ipns.Validator{KeyBook: host.Peerstore()},
}))
_ = d2
// Use an empty validator here for simplicity
// CAUTION! Use d2 will cause a "stream reset" error!
d1, err := dht.New(context.Background(), host, dhtopts.BucketSize(bucketSize), dhtopts.Datastore(ds), dhtopts.Validator(store.NodeValidator{}))
if err != nil {
panic(err)
}
// Pubsub
ps, err := pubsub.NewGossipSub(ctx, host)
if err != nil {
panic(err)
}
node := &Node{
dht: d1,
pubsub: ps,
db: ds,
mdnsPeers: make(map[peer.ID]peer.AddrInfo),
streams: make(chan network.Stream, 128),
}
s, _ := store.NewStore(d1, ds)
cache, _ := store.NewCache()
node.Context = localContext.NewContext(new(sync.RWMutex), host, s, cache, &ctx)
vkData, err := ioutil.ReadFile("./snark/verification_key.json")
if err != nil {
panic(err)
}
node.Manager, _ = manager.NewManager(ps, d1, node.Context, string(vkData))
mdns, err := msdnDiscovery.NewMdnsService(ctx, host, time.Second*5, "")
if err != nil {
panic(err)
}
mdns.RegisterNotifee(node)
return node, nil
}
// HandlePeerFound msdn handler
func (node *Node) HandlePeerFound(pi peer.AddrInfo) {
node.Mutex.Lock()
node.mdnsPeers[pi.ID] = pi
node.Mutex.Unlock()
if err := node.Context.Host.Connect(*node.Ctx, pi); err != nil {
fmt.Printf("failed to connect to mDNS peer: %s\n", err)
}
}
// Info ...
func (node *Node) Info() error {
// 0b. Let's get a sense of what those defaults are. What transports are we
// listening on? Each transport will have a multiaddr. If you run this
// multiple times, you will get different port numbers. Note how we listen
// on all interfaces by default.
// fmt.Println("My addresses:")
// for _, a := range node.h.Addrs() {
// fmt.Printf("\t%s\n", a)
// }
fmt.Println()
fmt.Println("My peer ID:")
fmt.Printf("\t%s\n", node.Host.ID())
fmt.Println()
fmt.Println("My identified multiaddrs:")
for _, a := range node.Host.Addrs() {
fmt.Printf("\t%s/p2p/%s\n", a, node.Host.ID())
}
// What protocols are added by default?
// fmt.Println()
// fmt.Println("Protocols:")
// for _, p := range node.h.Mux().Protocols() {
// fmt.Printf("\t%s\n", p)
// }
// What peers do we have in our peerstore? (hint: we've connected to nobody so far).
fmt.Println()
fmt.Println("Peers in peerstore:")
for _, p := range node.Host.Peerstore().PeersWithAddrs() {
fmt.Printf("\t%s\n", p)
}
fmt.Println(len(node.Host.Peerstore().PeersWithAddrs()))
// DHT routing table
fmt.Println("DHT Routing table:")
node.dht.RoutingTable().Print()
// Connections
fmt.Println("Connections:")
fmt.Println(len(node.Host.Network().Conns()))
fmt.Println("Created subjectHashHex:")
for sh := range node.Cache.GetCreatedSubjects() {
fmt.Println(sh)
}
fmt.Println("Collected subjects:")
fmt.Println(node.Cache.GetCollectedSubjects())
fmt.Println("Subcribed topics:")
fmt.Println(node.pubsub.GetTopics())
fmt.Println("Identity Index:")
fmt.Println(node.GetIdentityIndex())
fmt.Println("Voter Identity Merkle Tree Contents:")
fmt.Println(node.GetVoterIdentities())
fmt.Println("Ballot Map:")
fmt.Println(node.GetBallotMaps())
return nil
}
// DHTBootstrap ...
func (node *Node) DHTBootstrap(seeds ...ma.Multiaddr) error {
fmt.Println("Will bootstrap for 30 seconds...")
ctx, cancel := context.WithTimeout(*node.Ctx, 30*time.Second)
defer cancel()
var wg sync.WaitGroup
wg.Add(len(seeds))
for _, ma := range seeds {
ai, err := peer.AddrInfoFromP2pAddr(ma)
if err != nil {
return err
}
go func(ai peer.AddrInfo) {
defer wg.Done()
fmt.Printf("Connecting to peer: %s\n", ai)
if err := node.Host.Connect(ctx, ai); err != nil {
fmt.Printf("Failed while connecting to peer: %s; %s\n", ai, err)
} else {
fmt.Printf("Succeeded while connecting to peer: %s\n", ai)
}
}(*ai)
}
wg.Wait()
// if err := node.dht.BootstrapRandom(ctx); err != nil && err != context.DeadlineExceeded {
// return fmt.Errorf("failed while bootstrapping DHT: %w", err)
// }
if err := node.dht.Bootstrap(ctx); err != nil && err != context.DeadlineExceeded {
return fmt.Errorf("failed while bootstrapping DHT: %w", err)
}
fmt.Println("bootstrap OK! Routing table:")
node.dht.RoutingTable().Print()
return nil
}
// // Authenticate incoming p2p message
// // message: a protobufs go data object
// // data: common p2p message data
// func (n *Node) authenticateMessage(message proto.Message, data *p2p.MessageData) bool {
// // store a temp ref to signature and remove it from message data
// // sign is a string to allow easy reset to zero-value (empty string)
// sign := data.Sign
// data.Sign = nil
// // marshall data without the signature to protobufs3 binary format
// bin, err := proto.Marshal(message)
// if err != nil {
// log.Println(err, "failed to marshal pb message")
// return false
// }
// // restore sig in message data (for possible future use)
// data.Sign = sign
// // restore peer id binary format from base58 encoded node id data
// peerId, err := peer.IDB58Decode(data.NodeId)
// if err != nil {
// log.Println(err, "Failed to decode node id from base58")
// return false
// }
// // verify the data was authored by the signing peer identified by the public key
// // and signature included in the message
// return n.verifyData(bin, []byte(sign), peerId, data.NodePubKey)
// }
// // sign an outgoing p2p message payload
// func (n *Node) signProtoMessage(message proto.Message) ([]byte, error) {
// data, err := proto.Marshal(message)
// if err != nil {
// return nil, err
// }
// return n.signData(data)
// }
// // sign binary data using the local node's private key
// func (n *Node) signData(data []byte) ([]byte, error) {
// key := n.Peerstore().PrivKey(n.ID())
// res, err := key.Sign(data)
// return res, err
// }
// Verify incoming p2p message data integrity
// data: data to verify
// signature: author signature provided in the message payload
// peerId: author peer id from the message payload
// pubKeyData: author public key from the message payload
// func (n *Node) verifyData(data []byte, signature []byte, peerId peer.ID, pubKeyData []byte) bool {
// key, err := crypto.UnmarshalPublicKey(pubKeyData)
// if err != nil {
// log.Println(err, "Failed to extract key from message key data")
// return false
// }
// // extract node id from the provided public key
// idFromKey, err := peer.IDFromPublicKey(key)
// if err != nil {
// log.Println(err, "Failed to extract peer id from public key")
// return false
// }
// // verify that message author node id matches the provided node public key
// if idFromKey != peerId {
// log.Println(err, "Node id and provided public key mismatch")
// return false
// }
// res, err := key.Verify(data, signature)
// if err != nil {
// log.Println(err, "Error authenticating data")
// return false
// }
// return res
// }
// Run interactive commands
func (node *Node) Run() {
commands := []struct {
name string
exec func() error
}{
{"My info", node.handleMyInfo},
{"Manager: Propose a subject", node.handlePropose},
{"Manager: Join a subject", node.handleJoin},
{"Manager: Find topic providers", node.handleFindProposers},
{"Manager: Collect all topics", node.handleCollect},
// {"Manager: Sync identity index", node.handleSyncIdentityIndex},
{"Store: Put DHT", node.handlePutDHT},
{"Store: Get DHT", node.handleGetDHT},
{"Store: Put Local", node.handlePutLocal},
{"Store: Get Local", node.handleGetLocal},
{"DHT: Bootstrap (all seeds)", node.handleDHTBootstrap},
}
var str []string
for _, c := range commands {
str = append(str, c.name)
}
for {
sel := promptui.Select{
Label: "What do you want to do?",
Items: str,
Size: 1000,
}
fmt.Println()
i, _, err := sel.Run()
if err != nil {
panic(err)
}
if err := commands[i].exec(); err != nil {
fmt.Printf("command failed: %s\n", err)
}
}
}
func (node *Node) handleMyInfo() error {
return node.Info()
}
func (node *Node) handleDHTBootstrap() error {
return node.DHTBootstrap(dht.DefaultBootstrapPeers...)
}
func (node *Node) handlePutDHT() error {
return node.Store.PutDHT()
}
func (node *Node) handlePutLocal() error {
// return node.Store.PutLocal()
return nil
}
func (node *Node) handleGetDHT() error {
return node.Store.GetDHT()
}
func (node *Node) handleGetLocal() error {
// return node.Store.GetLocal()
return nil
}
func (node *Node) handlePropose() error {
p := promptui.Prompt{
Label: "Subject title",
}
title, err := p.Run()
if err != nil {
return err
}
p = promptui.Prompt{
Label: "Subject description",
}
description, err := p.Run()
if err != nil {
return err
}
return node.Propose(title, description, "")
}
func (node *Node) handleJoin() error {
p := promptui.Prompt{
Label: "Subject hash hex",
}
subjectHashHex, err := p.Run()
if err != nil {
return err
}
p = promptui.Prompt{
Label: "Identity commitment hex",
}
identityCommitmentHex, err := p.Run()
if err != nil {
return err
}
return node.Join(subjectHashHex, identityCommitmentHex)
}
// func (node *Node) handleSyncIdentityIndex() error {
// return node.SyncIdentityIndex()
// }
// func (node *Node) handlePrintInboundMessages() error {
// return node.PrintInboundMessages()
// }
func (node *Node) handleFindProposers() error {
peers, err := node.FindProposers()
if err != nil {
fmt.Println(err)
}
for p := range peers {
fmt.Println("found peer", p)
node.SetProvider(p.ID, "")
}
return nil
}
func (node *Node) handleCollect() error {
// subjects, err := node.Collect()
// if err != nil {
// fmt.Println(err)
// }
// for subject := range subjects {
// fmt.Println("Collect: ", subject)
// }
return nil
}