-
Notifications
You must be signed in to change notification settings - Fork 283
/
DirectoryService.cpp
828 lines (729 loc) · 37.8 KB
/
DirectoryService.cpp
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/**
* Copyright (c) 2018 Zilliqa
* This source code is being disclosed to you solely for the purpose of your participation in
* testing Zilliqa. You may view, compile and run the code for that purpose and pursuant to
* the protocols and algorithms that are programmed into, and intended by, the code. You may
* not do anything else with the code without express permission from Zilliqa Research Pte. Ltd.,
* including modifying or publishing the code (or any part of it), and developing or forming
* another public or private blockchain network. This source code is provided ‘as is’ and no
* warranties are given as to title or non-infringement, merchantability or fitness for purpose
* and, to the extent permitted by law, all liability for your use of the code is disclaimed.
* Some programs in this code are governed by the GNU General Public License v3.0 (available at
* https://www.gnu.org/licenses/gpl-3.0.en.html) (‘GPLv3’). The programs that are governed by
* GPLv3.0 are those programs that are located in the folders src/depends and tests/depends
* and which include a reference to GPLv3 in their program files.
**/
#include <algorithm>
#include <thread>
#include <chrono>
#include "DirectoryService.h"
#include "common/Constants.h"
#include "common/Messages.h"
#include "common/Serializable.h"
#include "depends/common/RLP.h"
#include "depends/libTrie/TrieDB.h"
#include "depends/libTrie/TrieHash.h"
#include "depends/libDatabase/MemoryDB.h"
#include "libCrypto/Sha2.h"
#include "libMediator/Mediator.h"
#include "libNetwork/P2PComm.h"
#include "libUtils/DataConversion.h"
#include "libUtils/DetachedFunction.h"
#include "libUtils/Logger.h"
#include "libUtils/SanityChecks.h"
#include "libUtils/TxnRootComputation.h"
using namespace std;
using namespace boost::multiprecision;
DirectoryService::DirectoryService(Mediator & mediator) : m_mediator(mediator)
{
#ifndef IS_LOOKUP_NODE
SetState(POW1_SUBMISSION);
#endif // IS_LOOKUP_NODE
m_mode = IDLE;
m_requesting_last_ds_block = false;
m_consensusLeaderID = 0;
m_consensusID = 1;
}
DirectoryService::~DirectoryService()
{
}
#ifndef IS_LOOKUP_NODE
bool DirectoryService::CheckState(Action action)
{
if(m_mode == Mode::IDLE)
{
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: I am a non-DS node now. Why am I getting this message?");
return false;
}
bool result = true;
switch(action)
{
case PROCESS_POW1SUBMISSION:
switch(m_state)
{
case POW1_SUBMISSION:
break;
case DSBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW1SUBMISSION but already in DSBLOCK_CONSENSUS_PREP");
result = false;
break;
case DSBLOCK_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW1SUBMISSION but already in DSBLOCK_CONSENSUS");
result = false;
break;
case POW2_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW1SUBMISSION but already in POW2_SUBMISSION");
result = false;
break;
case SHARDING_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW1SUBMISSION but already in SHARDING_CONSENSUS_PREP");
result = false;
break;
case SHARDING_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW1SUBMISSION but already in SHARDING_CONSENSUS");
result = false;
break;
case MICROBLOCK_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW1SUBMISSION but already in MICROBLOCK_SUBMISSION");
result = false;
break;
case FINALBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW1SUBMISSION but already in FINALBLOCK_CONSENSUS_PREP");
result = false;
break;
case FINALBLOCK_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW1SUBMISSION but already in FINALBLOCK_CONSENSUS");
result = false;
break;
case ERROR:
default:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Unrecognized or error state");
result = false;
break;
}
break;
case VERIFYPOW1:
switch(m_state)
{
case POW1_SUBMISSION:
break;
case DSBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW1 but already in DSBLOCK_CONSENSUS_PREP");
result = false;
break;
case DSBLOCK_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW1 but already in DSBLOCK_CONSENSUS");
result = false;
break;
case POW2_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW1 but already in POW2_SUBMISSION");
result = false;
break;
case SHARDING_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW1 but already in SHARDING_CONSENSUS_PREP");
result = false;
break;
case SHARDING_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW1 but already in SHARDING_CONSENSUS");
result = false;
break;
case MICROBLOCK_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW1 but already in MICROBLOCK_SUBMISSION");
result = false;
break;
case FINALBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW1 but already in FINALBLOCK_CONSENSUS_PREP");
result = false;
break;
case FINALBLOCK_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW1 but already in FINALBLOCK_CONSENSUS");
result = false;
break;
case ERROR:
default:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Unrecognized or error state");
result = false;
break;
}
break;
case PROCESS_DSBLOCKCONSENSUS:
switch(m_state)
{
case POW1_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_DSBLOCKCONSENSUS but already in POW1_SUBMISSION");
result = false;
break;
case DSBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_DSBLOCKCONSENSUS but already in DSBLOCK_CONSENSUS_PREP");
result = false;
break;
case DSBLOCK_CONSENSUS:
break;
case POW2_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_DSBLOCKCONSENSUS but already in POW2_SUBMISSION");
result = false;
break;
case SHARDING_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_DSBLOCKCONSENSUS but already in SHARDING_CONSENSUS_PREP");
result = false;
break;
case SHARDING_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_DSBLOCKCONSENSUS but already in SHARDING_CONSENSUS");
result = false;
break;
case MICROBLOCK_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_DSBLOCKCONSENSUS but already in MICROBLOCK_SUBMISSION");
result = false;
break;
case FINALBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_DSBLOCKCONSENSUS but already in FINALBLOCK_CONSENSUS_PREP");
result = false;
break;
case FINALBLOCK_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_DSBLOCKCONSENSUS but already in FINALBLOCK_CONSENSUS");
result = false;
break;
case ERROR:
default:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Unrecognized or error state");
result = false;
break;
}
break;
case PROCESS_POW2SUBMISSION:
switch(m_state)
{
case POW1_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW2SUBMISSION but already in POW1_SUBMISSION");
result = false;
break;
case DSBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW2SUBMISSION but already in DSBLOCK_CONSENSUS_PREP");
result = false;
break;
case DSBLOCK_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW2SUBMISSION but already in DSBLOCK_CONSENSUS");
result = false;
break;
case POW2_SUBMISSION:
break;
case SHARDING_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW2SUBMISSION but already in SHARDING_CONSENSUS_PREP");
result = false;
break;
case SHARDING_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW2SUBMISSION but already in SHARDING_CONSENSUS");
result = false;
break;
case MICROBLOCK_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW2SUBMISSION but already in MICROBLOCK_SUBMISSION");
result = false;
break;
case FINALBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW2SUBMISSION but already in FINALBLOCK_CONSENSUS_PREP");
result = false;
break;
case FINALBLOCK_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_POW2SUBMISSION but already in FINALBLOCK_CONSENSUS");
result = false;
break;
case ERROR:
default:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Unrecognized or error state");
result = false;
break;
}
break;
case VERIFYPOW2:
switch(m_state)
{
case POW1_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW2 but already in POW1_SUBMISSION");
result = false;
break;
case DSBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW2 but already in DSBLOCK_CONSENSUS_PREP");
result = false;
break;
case DSBLOCK_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW2 but already in DSBLOCK_CONSENSUS");
result = false;
break;
case POW2_SUBMISSION:
break;
case SHARDING_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW2 but already in SHARDING_CONSENSUS_PREP");
result = false;
break;
case SHARDING_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW2 but already in SHARDING_CONSENSUS");
result = false;
break;
case MICROBLOCK_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW2 but already in MICROBLOCK_SUBMISSION");
result = false;
break;
case FINALBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW2 but already in FINALBLOCK_CONSENSUS_PREP");
result = false;
break;
case FINALBLOCK_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing VERIFYPOW2 but already in FINALBLOCK_CONSENSUS");
result = false;
break;
case ERROR:
default:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Unrecognized or error state");
result = false;
break;
}
break;
case PROCESS_SHARDINGCONSENSUS:
switch(m_state)
{
case POW1_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_SHARDINGCONSENSUS but already in POW1_SUBMISSION");
result = false;
break;
case DSBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_SHARDINGCONSENSUS but already in DSBLOCK_CONSENSUS_PREP");
result = false;
break;
case DSBLOCK_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_SHARDINGCONSENSUS but already in DSBLOCK_CONSENSUS");
result = false;
break;
case POW2_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_SHARDINGCONSENSUS but already in POW2_SUBMISSION");
result = false;
break;
case SHARDING_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_SHARDINGCONSENSUS but already in SHARDING_CONSENSUS_PREP");
result = false;
break;
case SHARDING_CONSENSUS:
break;
case MICROBLOCK_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_SHARDINGCONSENSUS but already in MICROBLOCK_SUBMISSION");
result = false;
break;
case FINALBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_SHARDINGCONSENSUS but already in FINALBLOCK_CONSENSUS_PREP");
result = false;
break;
case FINALBLOCK_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_SHARDINGCONSENSUS but already in FINALBLOCK_CONSENSUS");
result = false;
break;
case ERROR:
default:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Unrecognized or error state");
result = false;
break;
}
break;
case PROCESS_MICROBLOCKSUBMISSION:
switch(m_state)
{
case POW1_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_MICROBLOCKSUBMISSION but already in POW1_SUBMISSION");
result = false;
break;
case DSBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_MICROBLOCKSUBMISSION but already in DSBLOCK_CONSENSUS_PREP");
result = false;
break;
case DSBLOCK_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_MICROBLOCKSUBMISSION but already in DSBLOCK_CONSENSUS");
result = false;
break;
case POW2_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_MICROBLOCKSUBMISSION but already in POW2_SUBMISSION");
result = false;
break;
case SHARDING_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_MICROBLOCKSUBMISSION but already in SHARDING_CONSENSUS_PREP");
result = false;
break;
case SHARDING_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_MICROBLOCKSUBMISSION but already in SHARDING_CONSENSUS");
result = false;
break;
case MICROBLOCK_SUBMISSION:
break;
case FINALBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_MICROBLOCKSUBMISSION but already in FINALBLOCK_CONSENSUS_PREP");
result = false;
break;
case FINALBLOCK_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_MICROBLOCKSUBMISSION but already in FINALBLOCK_CONSENSUS");
result = false;
break;
case ERROR:
default:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Unrecognized or error state");
result = false;
break;
}
break;
case PROCESS_FINALBLOCKCONSENSUS:
switch(m_state)
{
case POW1_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_FINALBLOCKCONSENSUS but already in POW1_SUBMISSION");
result = false;
break;
case DSBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_FINALBLOCKCONSENSUS but already in DSBLOCK_CONSENSUS_PREP");
result = false;
break;
case DSBLOCK_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_FINALBLOCKCONSENSUS but already in DSBLOCK_CONSENSUS");
result = false;
break;
case POW2_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_FINALBLOCKCONSENSUS but already in POW2_SUBMISSION");
result = false;
break;
case SHARDING_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_FINALBLOCKCONSENSUS but already in SHARDING_CONSENSUS_PREP");
result = false;
break;
case SHARDING_CONSENSUS:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_FINALBLOCKCONSENSUS but already in SHARDING_CONSENSUS");
result = false;
break;
case MICROBLOCK_SUBMISSION:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_FINALBLOCKCONSENSUS but already in MICROBLOCK_SUBMISSION");
result = false;
break;
case FINALBLOCK_CONSENSUS_PREP:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Doing PROCESS_FINALBLOCKCONSENSUS but already in FINALBLOCK_CONSENSUS_PREP");
result = false;
break;
case FINALBLOCK_CONSENSUS:
break;
case ERROR:
default:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Unrecognized or error state");
result = false;
break;
}
break;
default:
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: Unrecognized action");
result = false;
break;
}
return result;
}
#endif // IS_LOOKUP_NODE
bool DirectoryService::ProcessSetPrimary(const vector<unsigned char> & message, unsigned int offset, const Peer & from)
{
#ifndef IS_LOOKUP_NODE
// Note: This function should only be invoked during bootstrap sequence
// Message = [Primary node IP] [Primary node port]
LOG_MARKER();
Peer primary(message, offset);
if (primary == m_mediator.m_selfPeer)
{
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "I am the DS committee leader");
m_mode = PRIMARY_DS;
}
else
{
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "I am a DS committee backup. " << m_mediator.m_selfPeer.GetPrintableIPAddress() << ":" << m_mediator.m_selfPeer.m_listenPortHost );
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Current DS committee leader is " << primary.GetPrintableIPAddress() << " at port " << primary.m_listenPortHost)
m_mode = BACKUP_DS;
}
// For now, we assume the following when ProcessSetPrimary() is called:
// 1. All peers in the peer list are my fellow DS committee members for this first epoch
// 2. The list of DS nodes is sorted by PubKey, including my own
// 3. The peer with the smallest PubKey is also the first leader assigned in this call to ProcessSetPrimary()
// Let's notify lookup node of the DS committee during bootstrap
// TODO: Refactor this code
if (primary == m_mediator.m_selfPeer)
{
PeerStore & dsstore = PeerStore::GetStore();
dsstore.AddPeer(m_mediator.m_selfKey.second, m_mediator.m_selfPeer); // Add myself, but with dummy IP info
vector<PubKey> dsPub = dsstore.GetAllKeys();
m_mediator.m_DSCommitteePubKeys.resize(dsPub.size());
copy(dsPub.begin(), dsPub.end(), m_mediator.m_DSCommitteePubKeys.begin()); // These are the sorted PubKeys
vector<Peer> dsPeer = dsstore.GetAllPeers();
m_mediator.m_DSCommitteeNetworkInfo.resize(dsPeer.size());
copy(dsPeer.begin(), dsPeer.end(), m_mediator.m_DSCommitteeNetworkInfo.begin()); // This will be sorted by PubKey
// Message = [numDSPeers][DSPeer][DSPeer]... numDSPeers times
vector<unsigned char> setDSBootstrapNodeMessage = { MessageType::LOOKUP,
LookupInstructionType::SETDSINFOFROMSEED};
unsigned int curr_offset = MessageOffset::BODY;
Serializable::SetNumber<uint32_t>(setDSBootstrapNodeMessage, curr_offset, dsPeer.size(), sizeof(uint32_t));
curr_offset += sizeof(uint32_t);
for (unsigned int i = 0; i < dsPeer.size(); i++)
{
// PubKey
curr_offset += dsPub.at(i).Serialize(setDSBootstrapNodeMessage, curr_offset);
// Peer
curr_offset += dsPeer.at(i).Serialize(setDSBootstrapNodeMessage, curr_offset);
}
m_mediator.m_lookup->SendMessageToLookupNodes(setDSBootstrapNodeMessage);
}
PeerStore & peerstore = PeerStore::GetStore();
peerstore.AddPeer(m_mediator.m_selfKey.second, Peer()); // Add myself, but with dummy IP info
vector<Peer> tmp1 = peerstore.GetAllPeers();
m_mediator.m_DSCommitteeNetworkInfo.resize(tmp1.size());
copy(tmp1.begin(), tmp1.end(), m_mediator.m_DSCommitteeNetworkInfo.begin()); // This will be sorted by PubKey
vector<PubKey> tmp2 = peerstore.GetAllKeys();
m_mediator.m_DSCommitteePubKeys.resize(tmp2.size());
copy(tmp2.begin(), tmp2.end(), m_mediator.m_DSCommitteePubKeys.begin()); // These are the sorted PubKeys
peerstore.RemovePeer(m_mediator.m_selfKey.second); // Remove myself
// Now I need to find my index in the sorted list (this will be my ID for the consensus)
m_consensusMyID = 0;
for (auto i = m_mediator.m_DSCommitteePubKeys.begin(); i != m_mediator.m_DSCommitteePubKeys.end(); i++)
{
if (*i == m_mediator.m_selfKey.second)
{
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "My node ID for this PoW1 consensus is " << m_consensusMyID);
break;
}
m_consensusMyID++;
}
m_consensusLeaderID = 0;
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "START OF EPOCH 0");
#ifdef STAT_TEST
if (primary == m_mediator.m_selfPeer)
{
LOG_STATE("[IDENT][" << std::setw(15) << std::left << m_mediator.m_selfPeer.GetPrintableIPAddress() << "][0 ] DSLD");
}
else
{
LOG_STATE("[IDENT][" << std::setw(15) << std::left << m_mediator.m_selfPeer.GetPrintableIPAddress() << "][" << std::setw(6) << std::left << m_consensusMyID << "] DSBK");
}
#endif // STAT_TEST
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Waiting " << POW1_WINDOW_IN_SECONDS << " seconds, accepting PoW1 submissions...");
this_thread::sleep_for(chrono::seconds(POW1_WINDOW_IN_SECONDS));
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Starting consensus on ds block");
RunConsensusOnDSBlock();
#endif // IS_LOOKUP_NODE
return true;
}
#ifndef IS_LOOKUP_NODE
bool DirectoryService::CheckWhetherDSBlockIsFresh(const uint256_t dsblock_num)
{
// uint256_t latest_block_num_in_blockchain = m_mediator.m_dsBlockChain.GetLastBlock().GetHeader().GetBlockNum();
uint256_t latest_block_num_in_blockchain = m_mediator.m_dsBlockChain.GetBlockCount();
if (dsblock_num < latest_block_num_in_blockchain)
{
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error: We are processing duplicated blocks");
return false;
}
else if (dsblock_num > latest_block_num_in_blockchain)
{
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Warning: We are missing of some DS blocks. Cur: " << dsblock_num << ". New: " <<
latest_block_num_in_blockchain);
// Todo: handle missing DS blocks.
return false;
}
return true;
}
void DirectoryService::SetState(DirState state)
{
m_state = state;
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "DS State is now " << m_state);
}
vector<Peer> DirectoryService::GetBroadcastList(unsigned char ins_type, const Peer & broadcast_originator)
{
LOG_MARKER();
// Regardless of the instruction type, right now all our "broadcasts" are just redundant multicasts from DS nodes to non-DS nodes
return vector<Peer>();
}
void DirectoryService::RequestAllPoWConn()
{
LOG_MARKER();
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "I am requeesting AllPowConn");
// message: [listening port]
// In this implementation, we are only requesting it from ds leader only.
vector<unsigned char> requestAllPoWConnMsg = { MessageType::DIRECTORY, DSInstructionType::AllPoWConnRequest};
unsigned int cur_offset = MessageOffset::BODY;
Serializable::SetNumber<uint32_t>(requestAllPoWConnMsg, cur_offset, m_mediator.m_selfPeer.m_listenPortHost, sizeof(uint32_t));
cur_offset += sizeof(uint32_t);
P2PComm::GetInstance().SendMessage(m_mediator.m_DSCommitteeNetworkInfo.front(), requestAllPoWConnMsg);
// TODO: Request from a total of 20 ds members
}
#endif // IS_LOOKUP_NODE
// Current this is only used by ds. But ideally, 20 ds nodes should
bool DirectoryService::ProcessAllPoWConnRequest(const vector<unsigned char> & message, unsigned int offset, const Peer & from)
{
LOG_MARKER();
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "I am sending AllPowConn to requester");
uint32_t requesterListeningPort = Serializable::GetNumber<uint32_t>(message, offset, sizeof(uint32_t));
// Contruct the message and send to the requester
// Message: [size of m_allPowConn] [pub key, peer][pub key, peer] ....
vector<unsigned char> allPowConnMsg = { MessageType::DIRECTORY, DSInstructionType::AllPoWConnResponse};
unsigned int cur_offset = MessageOffset::BODY;
Serializable::SetNumber<uint32_t>(allPowConnMsg, cur_offset, m_allPoWConns.size(), sizeof(uint32_t));
cur_offset += sizeof(uint32_t);
unsigned int offset_to_increment;
for (auto & kv : m_allPoWConns)
{
if (kv.first == m_mediator.m_selfKey.second)
{
m_mediator.m_selfKey.second.Serialize(allPowConnMsg, cur_offset);
cur_offset += PUB_KEY_SIZE;
offset_to_increment = m_mediator.m_selfPeer.Serialize(allPowConnMsg, cur_offset);
cur_offset += offset_to_increment;
}
else
{
kv.first.Serialize(allPowConnMsg, cur_offset);
cur_offset += PUB_KEY_SIZE;
offset_to_increment = kv.second.Serialize(allPowConnMsg, cur_offset);
cur_offset += offset_to_increment;
}
}
Peer peer(from.m_ipAddress, requesterListeningPort);
P2PComm::GetInstance().SendMessage(peer, allPowConnMsg);
return true;
}
bool DirectoryService::ProcessAllPoWConnResponse(const vector<unsigned char> & message, unsigned int offset, const Peer & from)
{
LOG_MARKER();
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Updating AllPowConn");
unsigned int cur_offset = offset;
// 32-byte block number
uint32_t sizeeOfAllPowConn = Serializable::GetNumber<uint32_t>(message, cur_offset, sizeof(uint32_t));
cur_offset += sizeof(uint32_t);
std::map<PubKey, Peer> allPowConn;
for (uint32_t i = 0; i < sizeeOfAllPowConn; i++)
{
PubKey key(message, cur_offset);
cur_offset += PUB_KEY_SIZE;
Peer peer(message, cur_offset);
cur_offset += IP_SIZE + PORT_SIZE;
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "updating = " << peer.GetPrintableIPAddress() << ":" << peer.m_listenPortHost);
if (m_allPoWConns.find(key) == m_allPoWConns.end())
{
m_allPoWConns.insert(make_pair(key, peer));
}
}
{
std::unique_lock<std::mutex> lk(m_MutexCVAllPowConn);
m_hasAllPoWconns = true;
}
cv_allPowConns.notify_all();
return true;
}
#ifndef IS_LOOKUP_NODE
void DirectoryService::LastDSBlockRequest()
{
LOG_MARKER();
if (m_requesting_last_ds_block)
{
// Already requesting for last ds block. Should re-request again.
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "DEBUG: I am already waiting for the last ds block from ds leader.");
}
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "DEBUG: I am requesting the last ds block from ds leader.");
// message: [listening port]
// In this implementation, we are only requesting it from ds leader only.
vector<unsigned char> requestAllPoWConnMsg = { MessageType::DIRECTORY, DSInstructionType::LastDSBlockRequest};
unsigned int cur_offset = MessageOffset::BODY;
Serializable::SetNumber<uint32_t>(requestAllPoWConnMsg, cur_offset, m_mediator.m_selfPeer.m_listenPortHost, sizeof(uint32_t));
cur_offset += sizeof(uint32_t);
P2PComm::GetInstance().SendMessage(m_mediator.m_DSCommitteeNetworkInfo.front(), requestAllPoWConnMsg);
// TODO: Request from a total of 20 ds members
}
bool DirectoryService::ProcessLastDSBlockRequest(const vector<unsigned char> & message, unsigned int offset, const Peer & from)
{
LOG_MARKER();
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "DEBUG: I am sending the last ds block to the requester.");
// Deserialize the message and get the port
uint32_t requesterListeningPort = Serializable::GetNumber<uint32_t>(message, offset, sizeof(uint32_t));
// Craft the last block message
vector<unsigned char> lastDSBlockMsg = { MessageType::DIRECTORY, DSInstructionType::LastDSBlockResponse};
unsigned int cur_offset = MessageOffset::BODY;
m_mediator.m_dsBlockChain.GetLastBlock().Serialize(lastDSBlockMsg, cur_offset);
Peer peer(from.m_ipAddress, requesterListeningPort);
P2PComm::GetInstance().SendMessage(peer, lastDSBlockMsg);
return true;
}
bool DirectoryService::ProcessLastDSBlockResponse(const vector<unsigned char> & message, unsigned int offset, const Peer & from)
{
LOG_MARKER();
if (m_state != PROCESS_DSBLOCKCONSENSUS and m_requesting_last_ds_block)
{
// This recovery stage is meant for nodes that may get stuck in ds block consensus only.
// Only proceed if I still need the last ds block
return false;
}
// TODO: Should check whether ds block chain contain this block or not.
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "DEBUG: I received the last ds block from ds leader.");
m_requesting_last_ds_block = false;
unsigned int cur_offset = offset;
DSBlock dsblock(message, cur_offset);
int result = m_mediator.m_dsBlockChain.AddBlock(dsblock);
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Storing DS Block Number: "<< dsblock.GetHeader().GetBlockNum() <<
" with Nonce: "<< dsblock.GetHeader().GetNonce() <<
", Difficulty: "<< dsblock.GetHeader().GetDifficulty() <<
", Timestamp: "<< dsblock.GetHeader().GetTimestamp());
if (result == -1)
{
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Error. We failed to add dsblock to dsblockchain.");
return false;
}
vector<unsigned char> serializedDSBlock;
dsblock.Serialize(serializedDSBlock, 0);
BlockStorage::GetBlockStorage().PutDSBlock(dsblock.GetHeader().GetBlockNum(), serializedDSBlock);
SetState(POW2_SUBMISSION);
ScheduleShardingConsensus(BACKUP_POW2_WINDOW_IN_SECONDS - BUFFER_TIME_BEFORE_DS_BLOCK_REQUEST);
return true;
}
#endif // IS_LOOKUP_NODE
bool DirectoryService::Execute(const vector<unsigned char> & message, unsigned int offset, const Peer & from)
{
LOG_MARKER();
bool result = false;
typedef bool(DirectoryService::*InstructionHandler)(const vector<unsigned char> &, unsigned int, const Peer &);
#ifndef IS_LOOKUP_NODE
InstructionHandler ins_handlers[] =
{
&DirectoryService::ProcessSetPrimary,
&DirectoryService::ProcessPoW1Submission,
&DirectoryService::ProcessDSBlockConsensus,
&DirectoryService::ProcessPoW2Submission,
&DirectoryService::ProcessShardingConsensus,
&DirectoryService::ProcessMicroblockSubmission,
&DirectoryService::ProcessFinalBlockConsensus,
&DirectoryService::ProcessAllPoWConnRequest,
&DirectoryService::ProcessAllPoWConnResponse,
&DirectoryService::ProcessLastDSBlockRequest,
&DirectoryService::ProcessLastDSBlockResponse
};
#else
InstructionHandler ins_handlers[] =
{
&DirectoryService::ProcessSetPrimary,
&DirectoryService::ProcessPoW1Submission,
&DirectoryService::ProcessDSBlockConsensus,
&DirectoryService::ProcessPoW2Submission,
&DirectoryService::ProcessShardingConsensus,
&DirectoryService::ProcessMicroblockSubmission,
&DirectoryService::ProcessFinalBlockConsensus,
&DirectoryService::ProcessAllPoWConnRequest,
&DirectoryService::ProcessAllPoWConnResponse
};
#endif // IS_LOOKUP_NODE
const unsigned char ins_byte = message.at(offset);
const unsigned int ins_handlers_count = sizeof(ins_handlers) / sizeof(InstructionHandler);
if (ins_byte < ins_handlers_count)
{
result = (this->*ins_handlers[ins_byte])(message, offset + 1, from);
if (result == false)
{
// To-do: Error recovery
}
}
else
{
LOG_MESSAGE2(to_string(m_mediator.m_currentEpochNum).c_str(), "Unknown instruction byte " << hex << (unsigned int)ins_byte);
}
return result;
}