-
Notifications
You must be signed in to change notification settings - Fork 6
/
node.py
executable file
·1426 lines (1161 loc) · 49.7 KB
/
node.py
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python3
import logging, traceback
import argparse
import yaml
import time
from random import random, randint
from collections import Counter
import json
import sys
import asyncio
import aiohttp
from aiohttp import web
import hashlib
VIEW_SET_INTERVAL = 10
class View:
def __init__(self, view_number, num_nodes):
self._view_number = view_number
self._num_nodes = num_nodes
self._leader = view_number % num_nodes
# Minimum interval to set the view number
self._min_set_interval = VIEW_SET_INTERVAL
self._last_set_time = time.time()
# To encode to json
def get_view(self):
return self._view_number
# Recover from json data.
def set_view(self, view):
'''
Retrun True if successfully update view number
return False otherwise.
'''
if time.time() - self._last_set_time < self._min_set_interval:
return False
self._last_set_time = time.time()
self._view_number = view
self._leader = view % self._num_nodes
return True
def get_leader(self):
return self._leader
class Status:
'''
Record the state for every slot.
'''
PREPARE = 'prepare'
COMMIT = 'commit'
REPLY = "reply"
def __init__(self, f):
self.f = f
self.request = 0
self.prepare_msgs = {}
self.prepare_certificate = None # proposal
self.commit_msgs = {}
# Only means receive more than 2f + 1 commit message,
# but can not commit if there are any bubbles previously.
self.commit_certificate = None # proposal
# Set it to True only after commit
self.is_committed = False
class Certificate:
def __init__(self, view, proposal = 0):
'''
input:
view: object of class View
proposal: proposal in json_data(dict)
'''
self._view = view
self._proposal = proposal
def to_dict(self):
'''
Convert the Certificate to dictionary
'''
return {
'view': self._view.get_view(),
'proposal': self._proposal
}
def dumps_from_dict(self, dictionary):
'''
Update the view from the form after self.to_dict
input:
dictionay = {
'view': self._view.get_view(),
'proposal': self._proposal
}
'''
self._view.set_view(dictionary['view'])
self._proposal = dictionary['proposal']
def get_proposal(self):
return self._proposal
class SequenceElement:
def __init__(self, proposal):
self.proposal = proposal
self.from_nodes = set([])
def _update_sequence(self, msg_type, view, proposal, from_node):
'''
Update the record in the status by message type
input:
msg_type: Status.PREPARE or Status.COMMIT
view: View object of self._follow_view
proposal: proposal in json_data
from_node: The node send given the message.
'''
# The key need to include hash(proposal) in case get different
# preposals from BFT nodes. Need sort key in json.dumps to make
# sure getting the same string. Use hashlib so that we got same
# hash everytime.
hash_object = hashlib.sha256(json.dumps(proposal, sort_keys=True).encode())
key = (view.get_view(), hash_object.digest())
if msg_type == Status.PREPARE:
if key not in self.prepare_msgs:
self.prepare_msgs[key] = self.SequenceElement(proposal)
self.prepare_msgs[key].from_nodes.add(from_node)
elif msg_type == Status.COMMIT:
if key not in self.commit_msgs:
self.commit_msgs[key] = self.SequenceElement(proposal)
self.commit_msgs[key].from_nodes.add(from_node)
def _check_majority(self, msg_type):
'''
Check if receive more than 2f + 1 given type message in the same view.
input:
msg_type: self.PREPARE or self.COMMIT
'''
if msg_type == Status.PREPARE:
if self.prepare_certificate:
return True
for key in self.prepare_msgs:
if len(self.prepare_msgs[key].from_nodes)>= 2 * self.f + 1:
return True
return False
if msg_type == Status.COMMIT:
if self.commit_certificate:
return True
for key in self.commit_msgs:
if len(self.commit_msgs[key].from_nodes) >= 2 * self.f + 1:
return True
return False
class CheckPoint:
'''
Record all the status of the checkpoint for given PBFTHandler.
'''
RECEIVE_CKPT_VOTE = 'receive_ckpt_vote'
def __init__(self, checkpoint_interval, nodes, f, node_index,
lose_rate = 0, network_timeout = 10):
self._checkpoint_interval = checkpoint_interval
self._nodes = nodes
self._f = f
self._node_index = node_index
self._loss_rate = lose_rate
self._log = logging.getLogger(__name__)
# Next slot of the given globally accepted checkpoint.
# For example, the current checkpoint record until slot 99
# next_slot = 100
self.next_slot = 0
# Globally accepted checkpoint
self.checkpoint = []
# Use the hash of the checkpoint to record receive votes for given ckpt.
self._received_votes_by_ckpt = {}
self._session = None
self._network_timeout = network_timeout
self._log.info("---> %d: Create checkpoint.", self._node_index)
# Class to record the status of received checkpoints
class ReceiveVotes:
def __init__(self, ckpt, next_slot):
self.from_nodes = set([])
self.checkpoint = ckpt
self.next_slot = next_slot
def get_commit_upperbound(self):
'''
Return the upperbound that could commit
(return upperbound = true upperbound + 1)
'''
return self.next_slot + 2 * self._checkpoint_interval
def _hash_ckpt(self, ckpt):
'''
input:
ckpt: the checkpoint
output:
The hash of the input checkpoint in the format of
binary string.
'''
hash_object = hashlib.sha256(json.dumps(ckpt, sort_keys=True).encode())
return hash_object.digest()
async def receive_vote(self, ckpt_vote):
'''
Trigger when PBFTHandler receive checkpoint votes.
First, we update the checkpoint status. Second,
update the checkpoint if more than 2f + 1 node
agree with the given checkpoint.
input:
ckpt_vote = {
'node_index': self._node_index
'next_slot': self._next_slot + self._checkpoint_interval
'ckpt': json.dumps(ckpt)
'type': 'vote'
}
'''
self._log.debug("---> %d: Receive checkpoint votes", self._node_index)
ckpt = json.loads(ckpt_vote['ckpt'])
next_slot = ckpt_vote['next_slot']
from_node = ckpt_vote['node_index']
hash_ckpt = self._hash_ckpt(ckpt)
if hash_ckpt not in self._received_votes_by_ckpt:
self._received_votes_by_ckpt[hash_ckpt] = (
CheckPoint.ReceiveVotes(ckpt, next_slot))
status = self._received_votes_by_ckpt[hash_ckpt]
status.from_nodes.add(from_node)
for hash_ckpt in self._received_votes_by_ckpt:
if (self._received_votes_by_ckpt[hash_ckpt].next_slot > self.next_slot and
len(self._received_votes_by_ckpt[hash_ckpt].from_nodes) >= 2 * self._f + 1):
self._log.info("---> %d: Update checkpoint by receiving votes", self._node_index)
self.next_slot = self._received_votes_by_ckpt[hash_ckpt].next_slot
self.checkpoint = self._received_votes_by_ckpt[hash_ckpt].checkpoint
async def propose_vote(self, commit_decisions):
'''
When node the slots of committed message exceed self._next_slot
plus self._checkpoint_interval, propose new checkpoint and
broadcast to every node
input:
commit_decisions: list of tuple: [((client_index, client_seq), data), ... ]
output:
next_slot for the new update and garbage collection of the Status object.
'''
proposed_checkpoint = self.checkpoint + commit_decisions
await self._broadcast_checkpoint(proposed_checkpoint,
'vote', CheckPoint.RECEIVE_CKPT_VOTE)
async def _post(self, nodes, command, json_data):
'''
Broadcast json_data to all node in nodes with given command.
input:
nodes: list of nodes
command: action
json_data: Data in json format.
'''
if not self._session:
timeout = aiohttp.ClientTimeout(self._network_timeout)
self._session = aiohttp.ClientSession(timeout=timeout)
for i, node in enumerate(nodes):
if random() > self._loss_rate:
self._log.debug("make request to %d, %s", i, command)
try:
_ = await self._session.post(
self.make_url(node, command), json=json_data)
except Exception as e:
#resp_list.append((i, e))
self._log.error(e)
pass
@staticmethod
def make_url(node, command):
'''
input:
node: dictionary with key of host(url) and port
command: action
output:
The url to send with given node and action.
'''
return "http://{}:{}/{}".format(node['host'], node['port'], command)
async def _broadcast_checkpoint(self, ckpt, msg_type, command):
json_data = {
'node_index': self._node_index,
'next_slot': self.next_slot + self._checkpoint_interval,
'ckpt': json.dumps(ckpt),
'type': msg_type
}
await self._post(self._nodes, command, json_data)
def get_ckpt_info(self):
'''
Get the checkpoint serialized information.Called
by synchronize function to get the checkpoint
information.
'''
json_data = {
'next_slot': self.next_slot,
'ckpt': json.dumps(self.checkpoint)
}
return json_data
def update_checkpoint(self, json_data):
'''
Update the checkpoint when input checkpoint cover
more slots than current.
input:
json_data = {
'next_slot': self._next_slot
'ckpt': json.dumps(ckpt)
}
'''
self._log.debug("update_checkpoint: next_slot: %d; update_slot: %d"
, self.next_slot, json_data['next_slot'])
if json_data['next_slot'] > self.next_slot:
self._log.info("---> %d: Update checkpoint by synchronization.", self._node_index)
self.next_slot = json_data['next_slot']
self.checkpoint = json.loads(json_data['ckpt'])
async def receive_sync(sync_ckpt):
'''
Trigger when recieve checkpoint synchronization messages.
input:
sync_ckpt = {
'node_index': self._node_index
'next_slot': self._next_slot + self._checkpoint_interval
'ckpt': json.dumps(ckpt)
'type': 'sync'
}
'''
self._log.debug("receive_sync in checkpoint: current next_slot:"
" %d; update to: %d" , self.next_slot, json_data['next_slot'])
if sync_ckpt['next_slot'] > self._next_slot:
self.next_slot = sync_ckpt['next_slot']
self.checkpoint = json.loads(sync_ckpt['ckpt'])
async def garbage_collection(self):
'''
Clean those ReceiveCKPT objects whose next_slot smaller
than or equal to the current.
'''
deletes = []
for hash_ckpt in self._received_votes_by_ckpt:
if self._received_votes_by_ckpt[hash_ckpt].next_slot <= next_slot:
deletes.append(hash_ckpt)
for hash_ckpt in deletes:
del self._received_votes_by_ckpt[hash_ckpt]
class ViewChangeVotes:
"""
Record which nodes vote for the proposed view change.
In addition, store all the information including:
(1)checkpoints who has the largest information(largest
next_slot) (2) prepare certificate with largest for each
slot sent from voted nodes.
"""
def __init__(self, node_index, num_total_nodes):
# Current node index.
self._node_index = node_index
# Total number of node in the system.
self._num_total_nodes = num_total_nodes
# Number of faults tolerand
self._f = (self._num_total_nodes - 1) // 3
# Record the which nodes vote for current view.
self.from_nodes = set([])
# The prepare_certificate with highest view for each slot
self.prepare_certificate_by_slot = {}
self.lastest_checkpoint = None
self._log = logging.getLogger(__name__)
def receive_vote(self, json_data):
'''
Receive the vote message and make the update:
(1) Update the inforamtion in given vote storage -
prepare certificate.(2) update the node in from_nodes.
input:
json_data: the json_data received by view change vote broadcast:
{
"node_index": self._index,
"view_number": self._follow_view.get_view(),
"checkpoint":self._ckpt.get_ckpt_info(),
"prepared_certificates":self.get_prepare_certificates(),
}
'''
update_view = None
prepare_certificates = json_data["prepare_certificates"]
self._log.debug("%d update prepare_certificate for view %d",
self._node_index, json_data['view_number'])
for slot in prepare_certificates:
prepare_certificate = Status.Certificate(View(0, self._num_total_nodes))
prepare_certificate.dumps_from_dict(prepare_certificates[slot])
# Keep the prepare certificate who has the largest view number
if slot not in self.prepare_certificate_by_slot or (
self.prepare_certificate_by_slot[slot]._view.get_view() < (
prepare_certificate._view.get_view())):
self.prepare_certificate_by_slot[slot] = prepare_certificate
self.from_nodes.add(json_data['node_index'])
class Block:
def __init__(self, index, transactions, timestamp, previous_hash):
self.index = index
self.transactions = transactions
self.timestamp = timestamp
self.hash = ''
self.previous_hash = previous_hash
def compute_hash(self):
"""
A function that return the hash of the block contents.
"""
block_string = json.dumps(self.__dict__, sort_keys=True)
return hashlib.sha256(block_string.encode()).hexdigest()
def get_json(self):
return json.dumps(self.__dict__ , indent=4, sort_keys=True)
class Blockchain:
def __init__(self):
self.commit_counter = 0
self.length = 0
self.chain = []
self.create_genesis_block()
def create_genesis_block(self):
"""
A function to generate genesis block and appends it to
the chain. The block has index 0, previous_hash as 0, and
a valid hash.
"""
genesis_block = Block(0, ["Genenesis Block"], 0, "0")
genesis_block.hash = genesis_block.compute_hash()
self.length += 1
self.chain.append(genesis_block)
# @property
def last_block(self):
return self.chain[-1]
def last_block_hash(self):
tail = self.chain[-1]
# print(tail.transactions)
return tail.hash
def update_commit_counter(self):
self.commit_counter += 1
def add_block(self, block):
"""
A function that adds the block to the chain after verification.
Verification includes:
* The previous_hash referred in the block and the hash of latest block
in the chain match.
"""
previous_hash = self.last_block_hash()
if previous_hash != block.previous_hash:
raise Exception('block.previous_hash not equal to last_block_hash')
# print('block.previous_hash not equal to last_block_hash')
return
# else:
# print(str(previous_hash)+' == '+str(block.previous_hash))
block.hash = block.compute_hash()
# print( 'New Hash : '+str(block.hash)+'\n\n')
self.length += 1
self.chain.append(block)
class PBFTHandler:
REQUEST = 'request'
PREPREPARE = 'preprepare'
PREPARE = 'prepare'
COMMIT = 'commit'
REPLY = 'reply'
NO_OP = 'NOP'
RECEIVE_SYNC = 'receive_sync'
RECEIVE_CKPT_VOTE = 'receive_ckpt_vote'
VIEW_CHANGE_REQUEST = 'view_change_request'
VIEW_CHANGE_VOTE = "view_change_vote"
def __init__(self, index, conf):
self._nodes = conf['nodes']
self._node_cnt = len(self._nodes)
self._index = index
# Number of faults tolerant.
self._f = (self._node_cnt - 1) // 3
# leader
self._view = View(0, self._node_cnt)
self._next_propose_slot = 0
self._blockchain = Blockchain()
# tracks if commit_decisions had been commited to blockchain
self.committed_to_blockchain = False
# TODO: Test fixed
if self._index == 0:
self._is_leader = True
else:
self._is_leader = False
# Network simulation
self._loss_rate = conf['loss%'] / 100
# Time configuration
self._network_timeout = conf['misc']['network_timeout']
# Checkpoint
# After finishing committing self._checkpoint_interval slots,
# trigger to propose new checkpoint.
self._checkpoint_interval = conf['ckpt_interval']
self._ckpt = CheckPoint(self._checkpoint_interval, self._nodes,
self._f, self._index, self._loss_rate, self._network_timeout)
# Commit
self._last_commit_slot = -1
# Indicate my current leader.
# TODO: Test fixed
self._leader = 0
# The largest view either promised or accepted
self._follow_view = View(0, self._node_cnt)
# Restore the votes number and information for each view number
self._view_change_votes_by_view_number = {}
# Record all the status of the given slot
# To adjust json key, slot is string integer.
self._status_by_slot = {}
self._sync_interval = conf['sync_interval']
self._session = None
self._log = logging.getLogger(__name__)
@staticmethod
def make_url(node, command):
'''
input:
node: dictionary with key of host(url) and port
command: action
output:
The url to send with given node and action.
'''
return "http://{}:{}/{}".format(node['host'], node['port'], command)
async def _make_requests(self, nodes, command, json_data):
'''
Send json data:
input:
nodes: list of dictionary with key: host, port
command: Command to execute.
json_data: Json data.
output:
list of tuple: (node_index, response)
'''
resp_list = []
for i, node in enumerate(nodes):
if random() > self._loss_rate:
if not self._session:
timeout = aiohttp.ClientTimeout(self._network_timeout)
self._session = aiohttp.ClientSession(timeout=timeout)
self._log.debug("make request to %d, %s", i, command)
try:
resp = await self._session.post(self.make_url(node, command), json=json_data)
resp_list.append((i, resp))
except Exception as e:
#resp_list.append((i, e))
self._log.error(e)
pass
return resp_list
async def _make_response(self, resp):
'''
Drop response by chance, via sleep for sometime.
'''
if random() < self._loss_rate:
await asyncio.sleep(self._network_timeout)
return resp
async def _post(self, nodes, command, json_data):
'''
Broadcast json_data to all node in nodes with given command.
input:
nodes: list of nodes
command: action
json_data: Data in json format.
'''
if not self._session:
timeout = aiohttp.ClientTimeout(self._network_timeout)
self._session = aiohttp.ClientSession(timeout=timeout)
for i, node in enumerate(nodes):
if random() > self._loss_rate:
self._log.debug("make request to %d, %s", i, command)
try:
_ = await self._session.post(self.make_url(node, command), json=json_data)
except Exception as e:
#resp_list.append((i, e))
self._log.error(e)
pass
def _legal_slot(self, slot):
'''
the slot is legal only when it's between upperbound and the lowerbound.
input:
slot: string integer direct get from the json_data proposal key.
output:
boolean to express the result.
'''
if int(slot) < self._ckpt.next_slot or int(slot) >= self._ckpt.get_commit_upperbound():
return False
else:
return True
async def preprepare(self, json_data):
'''
Prepare: Deal with request from the client and broadcast to other replicas.
input:
json_data: Json-transformed web request from client
{
id: (client_id, client_seq),
client_url: "url string"
timestamp:"time"
data: "string"
}
'''
this_slot = str(self._next_propose_slot)
self._next_propose_slot = int(this_slot) + 1
self._log.info("---> %d: on preprepare, propose at slot: %d",
self._index, int(this_slot))
if this_slot not in self._status_by_slot:
self._status_by_slot[this_slot] = Status(self._f)
self._status_by_slot[this_slot].request = json_data
preprepare_msg = {
'leader': self._index,
'view': self._view.get_view(),
'proposal': {
this_slot: json_data
},
'type': 'preprepare'
}
await self._post(self._nodes, PBFTHandler.PREPARE, preprepare_msg)
async def get_request(self, request):
'''
Handle the request from client if leader, otherwise
redirect to the leader.
'''
self._log.info("---> %d: on request", self._index)
if not self._is_leader:
if self._leader != None:
raise web.HTTPTemporaryRedirect(self.make_url(
self._nodes[self._leader], PBFTHandler.REQUEST))
else:
raise web.HTTPServiceUnavailable()
else:
# print(request.headers)
# print(request.__dict__)
json_data = await request.json()
# print("\t\t--->node"+str(self._index)+": on request :")
# print(json_data)
await self.preprepare(json_data)
return web.Response()
async def prepare(self, request):
'''
Once receive preprepare message from client, broadcast
prepare message to all replicas.
input:
request: preprepare message from preprepare:
preprepare_msg = {
'leader': self._index,
'view': self._view.get_view(),
'proposal': {
this_slot: json_data
}
'type': 'preprepare'
}
'''
json_data = await request.json()
if json_data['view'] < self._follow_view.get_view():
# when receive message with view < follow_view, do nothing
return web.Response()
self._log.info("---> %d: receive preprepare msg from %d",
self._index, json_data['leader'])
self._log.info("---> %d: on prepare", self._index)
for slot in json_data['proposal']:
if not self._legal_slot(slot):
continue
if slot not in self._status_by_slot:
self._status_by_slot[slot] = Status(self._f)
prepare_msg = {
'index': self._index,
'view': json_data['view'],
'proposal': {
slot: json_data['proposal'][slot]
},
'type': Status.PREPARE
}
await self._post(self._nodes, PBFTHandler.COMMIT, prepare_msg)
return web.Response()
async def commit(self, request):
'''
Once receive more than 2f + 1 prepare message,
send the commit message.
input:
request: prepare message from prepare:
prepare_msg = {
'index': self._index,
'view': self._n,
'proposal': {
this_slot: json_data
}
'type': 'prepare'
}
'''
json_data = await request.json()
self._log.info("---> %d: receive prepare msg from %d",
self._index, json_data['index'])
# print("\t--->node "+str(self._index)+": receive prepare msg from node "+str(json_data['index']))
# print(json_data)
if json_data['view'] < self._follow_view.get_view():
# when receive message with view < follow_view, do nothing
return web.Response()
self._log.info("---> %d: on commit", self._index)
for slot in json_data['proposal']:
if not self._legal_slot(slot):
continue
if slot not in self._status_by_slot:
self._status_by_slot[slot] = Status(self._f)
status = self._status_by_slot[slot]
view = View(json_data['view'], self._node_cnt)
status._update_sequence(json_data['type'],
view, json_data['proposal'][slot], json_data['index'])
if status._check_majority(json_data['type']):
status.prepare_certificate = Status.Certificate(view,
json_data['proposal'][slot])
commit_msg = {
'index': self._index,
'view': json_data['view'],
'proposal': {
slot: json_data['proposal'][slot]
},
'type': Status.COMMIT
}
await self._post(self._nodes, PBFTHandler.REPLY, commit_msg)
return web.Response()
async def reply(self, request):
'''
Once receive more than 2f + 1 commit message, append the commit
certificate and cannot change anymore. In addition, if there is
no bubbles ahead, commit the given slots and update the last_commit_slot.
input:
request: commit message from commit:
preprepare_msg = {
'index': self._index,
'n': self._n,
'proposal': {
this_slot: json_data
}
'type': 'commit'
}
'''
json_data = await request.json()
self._log.info("---> %d: on reply", self._index)
# print("\t--->node "+str(self._index)+": on reply ")
if json_data['view'] < self._follow_view.get_view():
# when receive message with view < follow_view, do nothing
return web.Response()
self._log.info("---> %d: receive commit msg from %d",
self._index, json_data['index'])
for slot in json_data['proposal']:
if not self._legal_slot(slot):
continue
if slot not in self._status_by_slot:
self._status_by_slot[slot] = Status(self._f)
status = self._status_by_slot[slot]
view = View(json_data['view'], self._node_cnt)
status._update_sequence(json_data['type'],
view, json_data['proposal'][slot], json_data['index'])
# Commit only when no commit certificate and got more than 2f + 1
# commit message.
if not status.commit_certificate and status._check_majority(json_data['type']):
status.commit_certificate = Status.Certificate(view,
json_data['proposal'][slot])
self._log.debug("Add commit certifiacte to slot %d", int(slot))
# Reply only once and only when no bubble ahead
if self._last_commit_slot == int(slot) - 1 and not status.is_committed:
reply_msg = {
'index': self._index,
'view': json_data['view'],
'proposal': json_data['proposal'][slot],
'type': Status.REPLY
}
status.is_committed = True
self._last_commit_slot += 1
# When commit messages fill the next checkpoint,
# propose a new checkpoint.
if (self._last_commit_slot + 1) % self._checkpoint_interval == 0:
await self._ckpt.propose_vote(self.get_commit_decisions())
self._log.info("---> %d: Propose checkpoint with last slot: %d. "
"In addition, current checkpoint's next_slot is: %d",
self._index, self._last_commit_slot, self._ckpt.next_slot)
# Commit!
await self._commit_action()
try:
await self._session.post(
json_data['proposal'][slot]['client_url'], json=reply_msg)
except:
self._log.error("Send message failed to %s",
json_data['proposal'][slot]['client_url'])
pass
else:
self._log.info("%d reply to %s successfully!!",
self._index, json_data['proposal'][slot]['client_url'])
return web.Response()
def get_commit_decisions(self):
'''
Get the commit decision between the next slot of the
current ckpt until last commit slot
output:
commit_decisions: list of tuple: [((client_index, client_seq), data), ... ]
'''
commit_decisions = []
# print(self._ckpt.next_slot, self._last_commit_slot + 1)
for i in range(self._ckpt.next_slot, self._last_commit_slot + 1):
status = self._status_by_slot[str(i)]
proposal = status.commit_certificate._proposal
commit_decisions.append((str(proposal['id']), proposal['data']))
try:
# if self._index == 3:
# print('Node 3 is leader : ', str(self._is_leader))
# print('Node '+str(self._index)+' is leader : ', str(self._is_leader))
if not self.committed_to_blockchain and len(commit_decisions) == self._checkpoint_interval:
self.committed_to_blockchain = True
transactions = commit_decisions
# proposal is the last proposal
# print(proposal['timestamp'])
try:
timestamp = time.asctime( time.localtime( proposal['timestamp']) )
except Exception as e:
self._log.error("received invalid timestamp. replacing with current timestamp")
timestamp = time.asctime( time.localtime( time.time()) )
new_block= Block(self._blockchain.length, commit_decisions, timestamp , self._blockchain.last_block_hash())
self._blockchain.add_block(new_block)
# if self._index == 3:
# print(new_block.get_json())
except Exception as e:
traceback.print_exc()
print(e)
# print(len(self._status_by_slot))
# print(self._ckpt.next_slot, self._last_commit_slot + 1)
# print(len(commit_decisions))
# # print()
# print()
# print('commit_decisions')
# print(commit_decisions)
return commit_decisions
async def _commit_action(self):
'''
Dump the current commit decisions to disk.
'''
# with open("~$node_{}_blockchain.dump".format(self._index), 'w') as f:
dump_data = self._ckpt.checkpoint + self.get_commit_decisions()
# json.dump(dump_data, f)
# try:
with open("~$node_{}.blockchain".format(self._index), 'a') as f:
# f.write(str(dump_data)+'\n\n------------\n\n')
# print('node :' + str(self._index) +' > '+str(self._blockchain.commit_counter)+' : '+str(self._blockchain.length))
for i in range(self._blockchain.commit_counter, self._blockchain.length):
f.write(str(self._blockchain.chain[i].get_json())+'\n------------\n')
self._blockchain.update_commit_counter()
# except Exception as e:
# traceback.print_exc()
# print('for i = ' +str(i))
# print(e)
# pad = ''
# for k in range(self._index):
# pad += '\t'
# print(pad+'node :' + str(self._index) +' : '+str(self._blockchain.length))
async def receive_ckpt_vote(self, request):
'''
Receive the message sent from CheckPoint.propose_vote()
'''
self._log.info("---> %d: receive checkpoint vote.", self._index)
json_data = await request.json()
# print ()
# print ()
# print ('json_data')
# print(json_data)
# print ()
# print ()
# # print ('ckpt')
# # print (ckpt)
# print ()
await self._ckpt.receive_vote(json_data)
return web.Response()
async def receive_sync(self, request):
'''