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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
|
// Copyright 2018 The dexon-consensus Authors
// This file is part of the dexon-consensus library.
//
// The dexon-consensus library is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The dexon-consensus library is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the dexon-consensus library. If not, see
// <http://www.gnu.org/licenses/>.
package test
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net"
"strconv"
"sync"
"time"
"github.com/dexon-foundation/dexon-consensus/common"
"github.com/dexon-foundation/dexon-consensus/core/crypto"
"github.com/dexon-foundation/dexon-consensus/core/types"
typesDKG "github.com/dexon-foundation/dexon-consensus/core/types/dkg"
"github.com/dexon-foundation/dexon-consensus/core/utils"
)
const (
// Count of maximum count of peers to pull votes from.
maxPullingPeerCount = 3
)
// NetworkType is the simulation network type.
type NetworkType string
// NetworkType enums.
const (
NetworkTypeTCP NetworkType = "tcp"
NetworkTypeTCPLocal NetworkType = "tcp-local"
NetworkTypeFake NetworkType = "fake"
)
// NetworkConfig is the configuration for Network module.
type NetworkConfig struct {
Type NetworkType
PeerServer string
PeerPort int
}
// PullRequest is a generic request to pull everything (ex. vote, block...).
type PullRequest struct {
Requester types.NodeID
Type string
Identity interface{}
}
// MarshalJSON implements json.Marshaller.
func (req *PullRequest) MarshalJSON() (b []byte, err error) {
var idAsBytes []byte
// Make sure caller prepare correct identity for pull requests.
switch req.Type {
case "block":
idAsBytes, err = json.Marshal(req.Identity.(common.Hashes))
case "vote":
idAsBytes, err = json.Marshal(req.Identity.(types.Position))
default:
err = fmt.Errorf("unknown ID type for pull request: %v", req.Type)
}
if err != nil {
return
}
b, err = json.Marshal(&struct {
Requester types.NodeID `json:"req"`
Type string `json:"type"`
Identity []byte `json:"id"`
}{req.Requester, req.Type, idAsBytes})
return
}
// UnmarshalJSON iumplements json.Unmarshaller.
func (req *PullRequest) UnmarshalJSON(data []byte) (err error) {
rawReq := &struct {
Requester types.NodeID `json:"req"`
Type string `json:"type"`
Identity []byte `json:"id"`
}{}
if err = json.Unmarshal(data, rawReq); err != nil {
return
}
var ID interface{}
switch rawReq.Type {
case "block":
hashes := common.Hashes{}
if err = json.Unmarshal(rawReq.Identity, &hashes); err != nil {
break
}
ID = hashes
case "vote":
pos := types.Position{}
if err = json.Unmarshal(rawReq.Identity, &pos); err != nil {
break
}
ID = pos
default:
err = fmt.Errorf("unknown pull request type: %v", rawReq.Type)
}
if err != nil {
return
}
req.Requester = rawReq.Requester
req.Type = rawReq.Type
req.Identity = ID
return
}
// Network implements core.Network interface based on TransportClient.
type Network struct {
ID types.NodeID
config NetworkConfig
ctx context.Context
ctxCancel context.CancelFunc
trans TransportClient
fromTransport <-chan *TransportEnvelope
toConsensus chan interface{}
toNode chan interface{}
sentRandomnessLock sync.Mutex
sentRandomness map[common.Hash]struct{}
sentAgreementLock sync.Mutex
sentAgreement map[common.Hash]struct{}
blockCacheLock sync.RWMutex
blockCache map[common.Hash]*types.Block
voteCacheLock sync.RWMutex
voteCache map[types.Position]map[types.VoteHeader]*types.Vote
voteCacheSize int
votePositions []types.Position
stateModule *State
peers map[types.NodeID]struct{}
unreceivedBlocksLock sync.RWMutex
unreceivedBlocks map[common.Hash]chan<- common.Hash
latencyModel LatencyModel
cache *utils.NodeSetCache
notarySetCachesLock sync.Mutex
notarySetCaches map[uint64]map[uint32]map[types.NodeID]struct{}
dkgSetCachesLock sync.Mutex
dkgSetCaches map[uint64]map[types.NodeID]struct{}
}
// NewNetwork setup network stuffs for nodes, which provides an
// implementation of core.Network based on TransportClient.
func NewNetwork(pubKey crypto.PublicKey, latency LatencyModel,
marshaller Marshaller, config NetworkConfig) (n *Network) {
// Construct basic network instance.
n = &Network{
ID: types.NewNodeID(pubKey),
config: config,
toConsensus: make(chan interface{}, 1000),
toNode: make(chan interface{}, 1000),
sentRandomness: make(map[common.Hash]struct{}),
sentAgreement: make(map[common.Hash]struct{}),
blockCache: make(map[common.Hash]*types.Block),
unreceivedBlocks: make(map[common.Hash]chan<- common.Hash),
latencyModel: latency,
notarySetCaches: make(map[uint64]map[uint32]map[types.NodeID]struct{}),
dkgSetCaches: make(map[uint64]map[types.NodeID]struct{}),
voteCache: make(
map[types.Position]map[types.VoteHeader]*types.Vote),
}
n.ctx, n.ctxCancel = context.WithCancel(context.Background())
// Construct transport layer.
switch config.Type {
case NetworkTypeTCPLocal:
n.trans = NewTCPTransportClient(pubKey, latency, marshaller, true)
case NetworkTypeTCP:
n.trans = NewTCPTransportClient(pubKey, latency, marshaller, false)
case NetworkTypeFake:
n.trans = NewFakeTransportClient(pubKey, latency)
default:
panic(fmt.Errorf("unknown network type: %v", config.Type))
}
return
}
// PullBlocks implements core.Network interface.
func (n *Network) PullBlocks(hashes common.Hashes) {
go n.pullBlocksAsync(hashes)
}
// PullVotes implements core.Network interface.
func (n *Network) PullVotes(pos types.Position) {
go n.pullVotesAsync(pos)
}
// BroadcastVote implements core.Network interface.
func (n *Network) BroadcastVote(vote *types.Vote) {
n.broadcastToSet(
n.getNotarySet(vote.Position.Round, vote.Position.ChainID), vote)
n.addVoteToCache(vote)
}
// BroadcastBlock implements core.Network interface.
func (n *Network) BroadcastBlock(block *types.Block) {
// Avoid data race in fake transport.
block = n.cloneForFake(block).(*types.Block)
n.broadcastToSet(
n.getNotarySet(block.Position.Round, block.Position.ChainID), block)
n.addBlockToCache(block)
}
// BroadcastAgreementResult implements core.Network interface.
func (n *Network) BroadcastAgreementResult(
randRequest *types.AgreementResult) {
n.sentAgreementLock.Lock()
defer n.sentAgreementLock.Unlock()
if _, exist := n.sentAgreement[randRequest.BlockHash]; exist {
return
}
if len(n.sentAgreement) > 1000 {
// Randomly drop one entry.
for k := range n.sentAgreement {
delete(n.sentAgreement, k)
break
}
}
n.sentAgreement[randRequest.BlockHash] = struct{}{}
if err := n.trans.Broadcast(randRequest); err != nil {
panic(err)
}
}
// BroadcastRandomnessResult implements core.Network interface.
func (n *Network) BroadcastRandomnessResult(
randResult *types.BlockRandomnessResult) {
n.sentRandomnessLock.Lock()
defer n.sentRandomnessLock.Unlock()
if _, exist := n.sentRandomness[randResult.BlockHash]; exist {
return
}
if len(n.sentRandomness) > 1000 {
// Randomly drop one entry.
for k := range n.sentRandomness {
delete(n.sentRandomness, k)
break
}
}
n.sentRandomness[randResult.BlockHash] = struct{}{}
if err := n.trans.Broadcast(randResult); err != nil {
panic(err)
}
}
// SendDKGPrivateShare implements core.Network interface.
func (n *Network) SendDKGPrivateShare(
recv crypto.PublicKey, prvShare *typesDKG.PrivateShare) {
if err := n.trans.Send(types.NewNodeID(recv), prvShare); err != nil {
panic(err)
}
}
// BroadcastDKGPrivateShare implements core.Network interface.
func (n *Network) BroadcastDKGPrivateShare(
prvShare *typesDKG.PrivateShare) {
n.broadcastToSet(n.getDKGSet(prvShare.Round), prvShare)
}
// BroadcastDKGPartialSignature implements core.Network interface.
func (n *Network) BroadcastDKGPartialSignature(
psig *typesDKG.PartialSignature) {
n.broadcastToSet(n.getDKGSet(psig.Round), psig)
}
// ReceiveChan implements core.Network interface.
func (n *Network) ReceiveChan() <-chan interface{} {
return n.toConsensus
}
// Setup transport layer.
func (n *Network) Setup(serverEndpoint interface{}) (err error) {
// Join the p2p network.
switch n.config.Type {
case NetworkTypeTCP, NetworkTypeTCPLocal:
addr := net.JoinHostPort(
n.config.PeerServer, strconv.Itoa(n.config.PeerPort))
n.fromTransport, err = n.trans.Join(addr)
case NetworkTypeFake:
n.fromTransport, err = n.trans.Join(serverEndpoint)
default:
err = fmt.Errorf("unknown network type: %v", n.config.Type)
}
if err != nil {
return
}
peerKeys := n.trans.Peers()
n.peers = make(map[types.NodeID]struct{})
for _, k := range peerKeys {
n.peers[types.NewNodeID(k)] = struct{}{}
}
return
}
func (n *Network) dispatchMsg(e *TransportEnvelope) {
msg := n.cloneForFake(e.Msg)
switch v := msg.(type) {
case *types.Block:
n.addBlockToCache(v)
// Notify pulling routine about the newly arrived block.
func() {
n.unreceivedBlocksLock.Lock()
defer n.unreceivedBlocksLock.Unlock()
if ch, exists := n.unreceivedBlocks[v.Hash]; exists {
ch <- v.Hash
}
delete(n.unreceivedBlocks, v.Hash)
}()
n.toConsensus <- v
case *types.Vote:
// Add this vote to cache.
n.addVoteToCache(v)
n.toConsensus <- v
case *types.AgreementResult, *types.BlockRandomnessResult,
*typesDKG.PrivateShare, *typesDKG.PartialSignature:
n.toConsensus <- v
case packedStateChanges:
if n.stateModule == nil {
panic(errors.New(
"receive packed state change request without state attached"))
}
if err := n.stateModule.AddRequestsFromOthers([]byte(v)); err != nil {
panic(err)
}
case *PullRequest:
go n.handlePullRequest(v)
default:
n.toNode <- v
}
}
func (n *Network) handlePullRequest(req *PullRequest) {
switch req.Type {
case "block":
hashes := req.Identity.(common.Hashes)
func() {
n.blockCacheLock.Lock()
defer n.blockCacheLock.Unlock()
All:
for _, h := range hashes {
b, exists := n.blockCache[h]
if !exists {
continue
}
select {
case <-n.ctx.Done():
break All
default:
}
if err := n.trans.Send(req.Requester, b); err != nil {
log.Println("unable to send block", req.Requester, err)
}
}
}()
case "vote":
pos := req.Identity.(types.Position)
func() {
n.voteCacheLock.Lock()
defer n.voteCacheLock.Unlock()
if votes, exists := n.voteCache[pos]; exists {
for _, v := range votes {
if err := n.trans.Send(req.Requester, v); err != nil {
log.Println("unable to send vote", req.Requester, err)
}
}
}
}()
default:
panic(fmt.Errorf("unknown type of pull request: %v", req.Type))
}
}
// Run the main loop.
func (n *Network) Run() {
Loop:
for {
select {
case <-n.ctx.Done():
break Loop
default:
}
select {
case <-n.ctx.Done():
break Loop
case e, ok := <-n.fromTransport:
if !ok {
break Loop
}
go n.dispatchMsg(e)
}
}
}
// Close stops the network.
func (n *Network) Close() (err error) {
n.ctxCancel()
close(n.toConsensus)
n.toConsensus = nil
close(n.toNode)
n.toNode = nil
if err = n.trans.Close(); err != nil {
return
}
return
}
// Report exports 'Report' method of TransportClient.
func (n *Network) Report(msg interface{}) error {
return n.trans.Report(msg)
}
// Peers exports 'Peers' method of Transport.
func (n *Network) Peers() []crypto.PublicKey {
return n.trans.Peers()
}
// Broadcast exports 'Broadcast' method of Transport, and would panic when
// error.
func (n *Network) Broadcast(msg interface{}) {
if err := n.trans.Broadcast(msg); err != nil {
panic(err)
}
}
// Send exports 'Send' method of Transport.
func (n *Network) Send(nodeID types.NodeID, msg interface{}) error {
return n.trans.Send(nodeID, msg)
}
// ReceiveChanForNode returns a channel for messages not handled by
// core.Consensus.
func (n *Network) ReceiveChanForNode() <-chan interface{} {
return n.toNode
}
// addStateModule attaches a State instance to this network.
func (n *Network) addStateModule(s *State) {
// This variable should be attached before run, no lock to protect it.
n.stateModule = s
}
// AddNodeSetCache attaches an utils.NodeSetCache to this module. Once attached
// The behavior of Broadcast-X methods would be switched to broadcast to correct
// set of peers, instead of all peers.
func (n *Network) AddNodeSetCache(cache *utils.NodeSetCache) {
// This variable should be attached before run, no lock to protect it.
n.cache = cache
}
func (n *Network) pullBlocksAsync(hashes common.Hashes) {
// Setup notification channels for each block hash.
notYetReceived := make(map[common.Hash]struct{})
ch := make(chan common.Hash, len(hashes))
func() {
n.unreceivedBlocksLock.Lock()
defer n.unreceivedBlocksLock.Unlock()
for _, h := range hashes {
if _, exists := n.unreceivedBlocks[h]; exists {
continue
}
n.unreceivedBlocks[h] = ch
notYetReceived[h] = struct{}{}
}
}()
req := &PullRequest{
Requester: n.ID,
Type: "block",
Identity: hashes,
}
// Randomly pick peers to send pull requests.
Loop:
for nID := range n.peers {
if nID == n.ID {
continue
}
if err := n.trans.Send(nID, req); err != nil {
// Try next peer.
continue
}
select {
case <-n.ctx.Done():
break Loop
case <-time.After(2 * n.latencyModel.Delay()):
// Consume everything in the notification channel.
for {
select {
case h, ok := <-ch:
if !ok {
// This network module is closed.
break Loop
}
delete(notYetReceived, h)
if len(notYetReceived) == 0 {
break Loop
}
default:
continue Loop
}
}
}
}
}
func (n *Network) pullVotesAsync(pos types.Position) {
// Randomly pick several peers to pull votes from.
req := &PullRequest{
Requester: n.ID,
Type: "vote",
Identity: pos,
}
// Get corresponding notary set.
notarySet := n.getNotarySet(pos.Round, pos.ChainID)
// Randomly select one peer from notary set and send a pull request.
sentCount := 0
for nID := range notarySet {
if err := n.trans.Send(nID, req); err != nil {
// Try next peer.
continue
}
sentCount++
if sentCount >= maxPullingPeerCount {
break
}
}
}
func (n *Network) addBlockToCache(b *types.Block) {
n.blockCacheLock.Lock()
defer n.blockCacheLock.Unlock()
if len(n.blockCache) > 1000 {
// Randomly purge one block from cache.
for k := range n.blockCache {
delete(n.blockCache, k)
break
}
}
n.blockCache[b.Hash] = b.Clone()
}
func (n *Network) addVoteToCache(v *types.Vote) {
n.voteCacheLock.Lock()
defer n.voteCacheLock.Unlock()
if n.voteCacheSize >= 128 {
pos := n.votePositions[0]
n.voteCacheSize -= len(n.voteCache[pos])
delete(n.voteCache, pos)
n.votePositions = n.votePositions[1:]
}
if _, exists := n.voteCache[v.Position]; !exists {
n.votePositions = append(n.votePositions, v.Position)
n.voteCache[v.Position] =
make(map[types.VoteHeader]*types.Vote)
}
if _, exists := n.voteCache[v.Position][v.VoteHeader]; exists {
return
}
n.voteCache[v.Position][v.VoteHeader] = v
n.voteCacheSize++
}
func (n *Network) cloneForFake(v interface{}) interface{} {
if n.config.Type != NetworkTypeFake {
return v
}
switch val := v.(type) {
case *types.Block:
return val.Clone()
case *types.BlockRandomnessResult:
// Perform deep copy for randomness result.
return cloneBlockRandomnessResult(val)
}
return v
}
// getNotarySet gets notary set for that (round, chain) from cache.
func (n *Network) getNotarySet(
round uint64, chain uint32) map[types.NodeID]struct{} {
if n.cache == nil {
// Default behavior is to broadcast to all peers, which makes it easier
// to be used in simple test cases.
return n.peers
}
n.notarySetCachesLock.Lock()
defer n.notarySetCachesLock.Unlock()
roundSets, exists := n.notarySetCaches[round]
if !exists {
roundSets = make(map[uint32]map[types.NodeID]struct{})
n.notarySetCaches[round] = roundSets
}
set, exists := roundSets[chain]
if !exists {
var err error
set, err = n.cache.GetNotarySet(round, chain)
if err != nil {
panic(err)
}
roundSets[chain] = set
}
return set
}
// getDKGSet gets DKG set for that round from cache.
func (n *Network) getDKGSet(round uint64) map[types.NodeID]struct{} {
if n.cache == nil {
// Default behavior is to broadcast to all peers, which makes it easier
// to be used in simple test cases.
return n.peers
}
n.dkgSetCachesLock.Lock()
defer n.dkgSetCachesLock.Unlock()
set, exists := n.dkgSetCaches[round]
if !exists {
var err error
set, err = n.cache.GetDKGSet(round)
if err != nil {
panic(err)
}
n.dkgSetCaches[round] = set
}
return set
}
// broadcastToSet broadcast a message to a set of nodes.
func (n *Network) broadcastToSet(
set map[types.NodeID]struct{}, msg interface{}) {
for nID := range set {
if nID == n.ID {
continue
}
if err := n.trans.Send(nID, msg); err != nil {
panic(err)
}
}
}
|