/
master.py
1367 lines (1173 loc) · 50.1 KB
/
master.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
import argparse
import asyncio
import ipaddress
import random
import time
from collections import deque
from threading import Thread
from typing import Optional, List, Union, Dict
import psutil
# absl has to be imported before Logger (Logger changes default logger by logging.setLoggerClass(SLogger))
from absl import logging as GLOG
from absl import flags
import sys
from quarkchain.cluster.miner import Miner
from quarkchain.cluster.p2p_commands import (
CommandOp,
Direction,
GetRootBlockHeaderListRequest,
GetRootBlockListRequest,
)
from quarkchain.cluster.protocol import (
ClusterMetadata,
ClusterConnection,
P2PConnection,
ROOT_BRANCH,
NULL_CONNECTION,
)
from quarkchain.cluster.root_state import RootState
from quarkchain.cluster.rpc import (
AddMinorBlockHeaderResponse,
GetEcoInfoListRequest,
GetNextBlockToMineRequest,
GetUnconfirmedHeadersRequest,
GetAccountDataRequest,
AddTransactionRequest,
AddRootBlockRequest,
AddMinorBlockRequest,
CreateClusterPeerConnectionRequest,
DestroyClusterPeerConnectionCommand,
SyncMinorBlockListRequest,
GetMinorBlockRequest,
GetTransactionRequest,
ArtificialTxConfig,
MineRequest,
GenTxRequest,
GetLogResponse,
GetLogRequest,
ShardStats,
EstimateGasRequest,
GetStorageRequest,
GetCodeRequest,
GasPriceRequest,
)
from quarkchain.cluster.rpc import (
ConnectToSlavesRequest,
ClusterOp,
CLUSTER_OP_SERIALIZER_MAP,
ExecuteTransactionRequest,
Ping,
GetTransactionReceiptRequest,
GetTransactionListByAddressRequest,
)
from quarkchain.cluster.simple_network import SimpleNetwork
from quarkchain.env import DEFAULT_ENV
from quarkchain.core import Branch, ShardMask, Log, Address
from quarkchain.core import Transaction
from quarkchain.db import PersistentDb
from quarkchain.p2p.p2p_network import P2PNetwork, devp2p_app
from quarkchain.utils import set_logging_level, Logger, check
from quarkchain.cluster.cluster_config import ClusterConfig
FLAGS = flags.FLAGS
class SyncTask:
""" Given a header and a peer, the task will synchronize the local state
including root chain and shards with the peer up to the height of the header.
"""
def __init__(self, header, peer):
self.header = header
self.peer = peer
self.master_server = peer.master_server
self.root_state = peer.root_state
self.max_staleness = (
self.root_state.env.quark_chain_config.ROOT.MAX_STALE_ROOT_BLOCK_HEIGHT_DIFF
)
async def sync(self):
try:
await self.__run_sync()
except Exception as e:
Logger.log_exception()
self.peer.close_with_error(str(e))
async def __run_sync(self):
"""raise on any error so that sync() will close peer connection"""
if self.__has_block_hash(self.header.get_hash()):
return
# descending height
block_header_chain = [self.header]
while not self.__has_block_hash(block_header_chain[-1].hash_prev_block):
block_hash = block_header_chain[-1].hash_prev_block
height = block_header_chain[-1].height - 1
# abort if we have to download super old blocks
if self.root_state.tip.height - height > self.max_staleness:
Logger.warning(
"[R] abort syncing due to forking at super old block {} << {}".format(
height, self.root_state.tip.height
)
)
return
Logger.info(
"[R] downloading block header list from {} {}".format(
height, block_hash.hex()
)
)
block_header_list = await self.__download_block_headers(block_hash)
Logger.info(
"[R] downloaded headers from peer".format(len(block_header_list))
)
if not self.__validate_block_headers(block_header_list):
# TODO: tag bad peer
raise RuntimeError("Bad peer sending discontinuing block headers")
for header in block_header_list:
if self.__has_block_hash(header.get_hash()):
break
block_header_chain.append(header)
block_header_chain.reverse()
while len(block_header_chain) > 0:
Logger.info(
"[R] syncing from {} {}".format(
block_header_chain[0].height, block_header_chain[0].get_hash().hex()
)
)
block_chain = await self.__download_blocks(block_header_chain[:100])
Logger.info("[R] downloaded {} blocks from peer".format(len(block_chain)))
if len(block_chain) != len(block_header_chain[:100]):
# TODO: tag bad peer
raise RuntimeError("Bad peer missing blocks for headers they have")
for block in block_chain:
await self.__add_block(block)
block_header_chain.pop(0)
def __has_block_hash(self, block_hash):
return self.root_state.contain_root_block_by_hash(block_hash)
def __validate_block_headers(self, block_header_list):
# TODO: check difficulty and other stuff?
for i in range(len(block_header_list) - 1):
block, prev = block_header_list[i : i + 2]
if block.height != prev.height + 1:
return False
if block.hash_prev_block != prev.get_hash():
return False
return True
async def __download_block_headers(self, block_hash):
request = GetRootBlockHeaderListRequest(
block_hash=block_hash, limit=100, direction=Direction.GENESIS
)
op, resp, rpc_id = await self.peer.write_rpc_request(
CommandOp.GET_ROOT_BLOCK_HEADER_LIST_REQUEST, request
)
return resp.block_header_list
async def __download_blocks(self, block_header_list):
block_hash_list = [b.get_hash() for b in block_header_list]
op, resp, rpc_id = await self.peer.write_rpc_request(
CommandOp.GET_ROOT_BLOCK_LIST_REQUEST,
GetRootBlockListRequest(block_hash_list),
)
return resp.root_block_list
async def __add_block(self, root_block):
start = time.time()
await self.__sync_minor_blocks(root_block.minor_block_header_list)
await self.master_server.add_root_block(root_block)
elapse = time.time() - start
Logger.info(
"[R] syncing root block {} {} took {:.2f} seconds".format(
root_block.header.height, root_block.header.get_hash().hex(), elapse
)
)
async def __sync_minor_blocks(self, minor_block_header_list):
minor_block_download_map = dict()
for m_block_header in minor_block_header_list:
m_block_hash = m_block_header.get_hash()
if not self.root_state.is_minor_block_validated(m_block_hash):
minor_block_download_map.setdefault(m_block_header.branch, []).append(
m_block_hash
)
future_list = []
for branch, m_block_hash_list in minor_block_download_map.items():
slave_conn = self.master_server.get_slave_connection(branch=branch)
future = slave_conn.write_rpc_request(
op=ClusterOp.SYNC_MINOR_BLOCK_LIST_REQUEST,
cmd=SyncMinorBlockListRequest(
m_block_hash_list, branch, self.peer.get_cluster_peer_id()
),
)
future_list.append(future)
result_list = await asyncio.gather(*future_list)
for result in result_list:
if result is Exception:
raise RuntimeError(
"Unable to download minor blocks from root block with exception {}".format(
result
)
)
_, result, _ = result
if result.error_code != 0:
raise RuntimeError("Unable to download minor blocks from root block")
for m_header in minor_block_header_list:
self.root_state.add_validated_minor_block_hash(m_header.get_hash())
class Synchronizer:
""" Buffer the headers received from peer and sync one by one """
def __init__(self):
self.queue = deque()
self.running = False
def add_task(self, header, peer):
self.queue.append((header, peer))
if not self.running:
self.running = True
asyncio.ensure_future(self.__run())
async def __run(self):
while len(self.queue) > 0:
header, peer = self.queue.popleft()
task = SyncTask(header, peer)
await task.sync()
self.running = False
class SlaveConnection(ClusterConnection):
OP_NONRPC_MAP = {}
def __init__(
self, env, reader, writer, master_server, slave_id, shard_mask_list, name=None
):
super().__init__(
env,
reader,
writer,
CLUSTER_OP_SERIALIZER_MAP,
self.OP_NONRPC_MAP,
OP_RPC_MAP,
name=name,
)
self.master_server = master_server
self.id = slave_id
self.shard_mask_list = shard_mask_list
check(len(shard_mask_list) > 0)
asyncio.ensure_future(self.active_and_loop_forever())
def get_connection_to_forward(self, metadata):
""" Override ProxyConnection.get_connection_to_forward()
Forward traffic from slave to peer
"""
if metadata.cluster_peer_id == 0:
return None
peer = self.master_server.get_peer(metadata.cluster_peer_id)
if peer is None:
return NULL_CONNECTION
return peer
def validate_connection(self, connection):
return connection == NULL_CONNECTION or isinstance(connection, P2PConnection)
def has_shard(self, shard_id):
for shard_mask in self.shard_mask_list:
if shard_mask.contain_shard_id(shard_id):
return True
return False
def has_overlap(self, shard_mask):
for local_shard_mask in self.shard_mask_list:
if local_shard_mask.has_overlap(shard_mask):
return True
return False
async def send_ping(self, initialize_shard_state=False):
root_block = (
self.master_server.root_state.get_tip_block()
if initialize_shard_state
else None
)
req = Ping("", [], root_block)
op, resp, rpc_id = await self.write_rpc_request(
op=ClusterOp.PING,
cmd=req,
metadata=ClusterMetadata(branch=ROOT_BRANCH, cluster_peer_id=0),
)
return (resp.id, resp.shard_mask_list)
async def send_connect_to_slaves(self, slave_info_list):
""" Make slave connect to other slaves.
Returns True on success
"""
req = ConnectToSlavesRequest(slave_info_list)
op, resp, rpc_id = await self.write_rpc_request(
ClusterOp.CONNECT_TO_SLAVES_REQUEST, req
)
check(len(resp.result_list) == len(slave_info_list))
for i, result in enumerate(resp.result_list):
if len(result) > 0:
Logger.info(
"Slave {} failed to connect to {} with error {}".format(
self.id, slave_info_list[i].id, result
)
)
return False
Logger.info("Slave {} connected to other slaves successfully".format(self.id))
return True
def close(self):
Logger.info("Lost connection with slave {}".format(self.id))
super().close()
self.master_server.shutdown()
def close_with_error(self, error):
Logger.info("Closing connection with slave {}".format(self.id))
return super().close_with_error(error)
async def add_transaction(self, tx):
request = AddTransactionRequest(tx)
_, resp, _ = await self.write_rpc_request(
ClusterOp.ADD_TRANSACTION_REQUEST, request
)
return resp.error_code == 0
async def execute_transaction(
self, tx: Transaction, from_address, block_height: Optional[int]
):
request = ExecuteTransactionRequest(tx, from_address, block_height)
_, resp, _ = await self.write_rpc_request(
ClusterOp.EXECUTE_TRANSACTION_REQUEST, request
)
return resp.result if resp.error_code == 0 else None
async def get_minor_block_by_hash(self, block_hash, branch):
request = GetMinorBlockRequest(branch, minor_block_hash=block_hash)
_, resp, _ = await self.write_rpc_request(
ClusterOp.GET_MINOR_BLOCK_REQUEST, request
)
if resp.error_code != 0:
return None
return resp.minor_block
async def get_minor_block_by_height(self, height, branch):
request = GetMinorBlockRequest(branch, height=height)
_, resp, _ = await self.write_rpc_request(
ClusterOp.GET_MINOR_BLOCK_REQUEST, request
)
if resp.error_code != 0:
return None
return resp.minor_block
async def get_transaction_by_hash(self, tx_hash, branch):
request = GetTransactionRequest(tx_hash, branch)
_, resp, _ = await self.write_rpc_request(
ClusterOp.GET_TRANSACTION_REQUEST, request
)
if resp.error_code != 0:
return None, None
return resp.minor_block, resp.index
async def get_transaction_receipt(self, tx_hash, branch):
request = GetTransactionReceiptRequest(tx_hash, branch)
_, resp, _ = await self.write_rpc_request(
ClusterOp.GET_TRANSACTION_RECEIPT_REQUEST, request
)
if resp.error_code != 0:
return None
return resp.minor_block, resp.index, resp.receipt
async def get_transactions_by_address(self, address, start, limit):
request = GetTransactionListByAddressRequest(address, start, limit)
_, resp, _ = await self.write_rpc_request(
ClusterOp.GET_TRANSACTION_LIST_BY_ADDRESS_REQUEST, request
)
if resp.error_code != 0:
return None
return resp.tx_list, resp.next
async def get_logs(
self,
branch: Branch,
addresses: List[Address],
topics: List[List[bytes]],
start_block: int,
end_block: int,
) -> Optional[List[Log]]:
request = GetLogRequest(branch, addresses, topics, start_block, end_block)
_, resp, _ = await self.write_rpc_request(
ClusterOp.GET_LOG_REQUEST, request
) # type: GetLogResponse
return resp.logs if resp.error_code == 0 else None
async def estimate_gas(
self, tx: Transaction, from_address: Address
) -> Optional[int]:
request = EstimateGasRequest(tx, from_address)
_, resp, _ = await self.write_rpc_request(
ClusterOp.ESTIMATE_GAS_REQUEST, request
)
return resp.result if resp.error_code == 0 else None
async def get_storage_at(
self, address: Address, key: int, block_height: Optional[int]
) -> Optional[bytes]:
request = GetStorageRequest(address, key, block_height)
_, resp, _ = await self.write_rpc_request(
ClusterOp.GET_STORAGE_REQUEST, request
)
return resp.result if resp.error_code == 0 else None
async def get_code(
self, address: Address, block_height: Optional[int]
) -> Optional[bytes]:
request = GetCodeRequest(address, block_height)
_, resp, _ = await self.write_rpc_request(ClusterOp.GET_CODE_REQUEST, request)
return resp.result if resp.error_code == 0 else None
async def gas_price(self, branch: Branch) -> Optional[int]:
request = GasPriceRequest(branch)
_, resp, _ = await self.write_rpc_request(ClusterOp.GAS_PRICE_REQUEST, request)
return resp.result if resp.error_code == 0 else None
# RPC handlers
async def handle_add_minor_block_header_request(self, req):
self.master_server.root_state.add_validated_minor_block_hash(
req.minor_block_header.get_hash()
)
self.master_server.update_shard_stats(req.shard_stats)
self.master_server.update_tx_count_history(
req.tx_count, req.x_shard_tx_count, req.minor_block_header.create_time
)
return AddMinorBlockHeaderResponse(
error_code=0,
artificial_tx_config=self.master_server.get_artificial_tx_config(),
)
OP_RPC_MAP = {
ClusterOp.ADD_MINOR_BLOCK_HEADER_REQUEST: (
ClusterOp.ADD_MINOR_BLOCK_HEADER_RESPONSE,
SlaveConnection.handle_add_minor_block_header_request,
)
}
class MasterServer:
""" Master node in a cluster
It does two things to initialize the cluster:
1. Setup connection with all the slaves in ClusterConfig
2. Make slaves connect to each other
"""
def __init__(self, env, root_state, name="master"):
self.loop = asyncio.get_event_loop()
self.env = env
self.root_state = root_state
self.network = None # will be set by SimpleNetwork
self.cluster_config = env.cluster_config
# branch value -> a list of slave running the shard
self.branch_to_slaves = dict() # type: Dict[int, List[SlaveConnection]]
self.slave_pool = set()
self.cluster_active_future = self.loop.create_future()
self.shutdown_future = self.loop.create_future()
self.name = name
self.artificial_tx_config = ArtificialTxConfig(
target_root_block_time=self.env.quark_chain_config.ROOT.CONSENSUS_CONFIG.TARGET_BLOCK_TIME,
target_minor_block_time=self.env.quark_chain_config.SHARD_LIST[
0
].CONSENSUS_CONFIG.TARGET_BLOCK_TIME,
)
self.synchronizer = Synchronizer()
self.branch_to_shard_stats = dict() # type: Dict[int, ShardStats]
# (epoch in minute, tx_count in the minute)
self.tx_count_history = deque()
self.__init_root_miner()
def __init_root_miner(self):
miner_address = self.env.quark_chain_config.testnet_master_address
async def __create_block():
while True:
is_root, block = await self.get_next_block_to_mine(
address=miner_address, shard_mask_value=0, prefer_root=True
)
if is_root:
return block
await asyncio.sleep(1)
async def __add_block(block):
# Root block should include latest minor block headers while it's being mined
# This is a hack to get the latest minor block included since testnet does not check difficulty
# TODO: fix this as it will break real PoW
block = await __create_block()
await self.add_root_block(block)
def __get_target_block_time():
return self.get_artificial_tx_config().target_root_block_time
self.root_miner = Miner(
self.env.quark_chain_config.ROOT.CONSENSUS_TYPE,
__create_block,
__add_block,
__get_target_block_time,
self.env,
)
def __get_shard_size(self):
# TODO: replace it with dynamic size
return self.env.quark_chain_config.SHARD_SIZE
def get_shard_size(self):
return self.__get_shard_size()
def get_artificial_tx_config(self):
return self.artificial_tx_config
def __has_all_shards(self):
""" Returns True if all the shards have been run by at least one node """
return len(self.branch_to_slaves) == self.__get_shard_size() and all(
[len(slaves) > 0 for _, slaves in self.branch_to_slaves.items()]
)
async def __connect(self, ip, port):
""" Retries until success """
Logger.info("Trying to connect {}:{}".format(ip, port))
while True:
try:
reader, writer = await asyncio.open_connection(ip, port, loop=self.loop)
break
except Exception as e:
Logger.info("Failed to connect {} {}: {}".format(ip, port, e))
await asyncio.sleep(
self.env.cluster_config.MASTER.MASTER_TO_SLAVE_CONNECT_RETRY_DELAY
)
Logger.info("Connected to {}:{}".format(ip, port))
return (reader, writer)
async def __connect_to_slaves(self):
""" Master connects to all the slaves """
futures = []
slaves = []
for slave_info in self.cluster_config.get_slave_info_list():
ip = str(ipaddress.ip_address(slave_info.ip))
reader, writer = await self.__connect(ip, slave_info.port)
slave = SlaveConnection(
self.env,
reader,
writer,
self,
slave_info.id,
slave_info.shard_mask_list,
name="{}_slave_{}".format(self.name, slave_info.id),
)
await slave.wait_until_active()
futures.append(slave.send_ping())
slaves.append(slave)
results = await asyncio.gather(*futures)
for slave, result in zip(slaves, results):
# Verify the slave does have the same id and shard mask list as the config file
id, shard_mask_list = result
if id != slave.id:
Logger.error(
"Slave id does not match. expect {} got {}".format(slave.id, id)
)
self.shutdown()
if shard_mask_list != slave.shard_mask_list:
Logger.error(
"Slave {} shard mask list does not match. expect {} got {}".format(
slave.id, slave.shard_mask_list, shard_mask_list
)
)
self.shutdown()
self.slave_pool.add(slave)
for shard_id in range(self.__get_shard_size()):
branch = Branch.create(self.__get_shard_size(), shard_id)
if slave.has_shard(shard_id):
self.branch_to_slaves.setdefault(branch.value, []).append(slave)
async def __setup_slave_to_slave_connections(self):
""" Make slaves connect to other slaves.
Retries until success.
"""
for slave in self.slave_pool:
await slave.wait_until_active()
success = await slave.send_connect_to_slaves(
self.cluster_config.get_slave_info_list()
)
if not success:
self.shutdown()
async def __init_shards(self):
futures = []
for slave in self.slave_pool:
futures.append(slave.send_ping(initialize_shard_state=True))
await asyncio.gather(*futures)
async def __send_mining_config_to_slaves(self, mining):
futures = []
for slave in self.slave_pool:
request = MineRequest(self.get_artificial_tx_config(), mining)
futures.append(slave.write_rpc_request(ClusterOp.MINE_REQUEST, request))
responses = await asyncio.gather(*futures)
check(all([resp.error_code == 0 for _, resp, _ in responses]))
async def start_mining(self):
await self.__send_mining_config_to_slaves(True)
self.root_miner.enable()
self.root_miner.mine_new_block_async()
Logger.warning(
"Mining started with root block time {} s, minor block time {} s".format(
self.get_artificial_tx_config().target_root_block_time,
self.get_artificial_tx_config().target_minor_block_time,
)
)
async def stop_mining(self):
await self.__send_mining_config_to_slaves(False)
self.root_miner.disable()
Logger.warning("Mining stopped")
def get_slave_connection(self, branch):
# TODO: Support forwarding to multiple connections (for replication)
check(len(self.branch_to_slaves[branch.value]) > 0)
return self.branch_to_slaves[branch.value][0]
def __log_summary(self):
for branch_value, slaves in self.branch_to_slaves.items():
Logger.info(
"[{}] is run by slave {}".format(
Branch(branch_value).get_shard_id(), [s.id for s in slaves]
)
)
async def __init_cluster(self):
await self.__connect_to_slaves()
self.__log_summary()
if not self.__has_all_shards():
Logger.error("Missing some shards. Check cluster config file!")
return
await self.__setup_slave_to_slave_connections()
await self.__init_shards()
self.cluster_active_future.set_result(None)
def start(self):
self.loop.create_task(self.__init_cluster())
def start_and_loop(self):
self.start()
try:
self.loop.run_until_complete(self.shutdown_future)
except KeyboardInterrupt:
pass
def wait_until_cluster_active(self):
# Wait until cluster is ready
self.loop.run_until_complete(self.cluster_active_future)
def shutdown(self):
# TODO: May set exception and disconnect all slaves
if not self.shutdown_future.done():
self.shutdown_future.set_result(None)
if not self.cluster_active_future.done():
self.cluster_active_future.set_exception(
RuntimeError("failed to start the cluster")
)
def get_shutdown_future(self):
return self.shutdown_future
async def __create_root_block_to_mine_or_fallback_to_minor_block(self, address):
""" Try to create a root block to mine or fallback to create minor block if failed proof-of-progress """
futures = []
for slave in self.slave_pool:
request = GetUnconfirmedHeadersRequest()
futures.append(
slave.write_rpc_request(
ClusterOp.GET_UNCONFIRMED_HEADERS_REQUEST, request
)
)
responses = await asyncio.gather(*futures)
# Slaves may run multiple copies of the same branch
# branch_value -> HeaderList
shard_id_to_header_list = dict()
for response in responses:
_, response, _ = response
if response.error_code != 0:
return (None, None)
for headers_info in response.headers_info_list:
if headers_info.branch.get_shard_size() != self.__get_shard_size():
Logger.error(
"Expect shard size {} got {}".format(
self.__get_shard_size(),
headers_info.branch.get_shard_size(),
)
)
return (None, None)
height = 0
for header in headers_info.header_list:
# check headers are ordered by height
check(height == 0 or height + 1 == header.height)
height = header.height
# Filter out the ones unknown to the master
if not self.root_state.is_minor_block_validated(header.get_hash()):
break
shard_id_to_header_list.setdefault(
headers_info.branch.get_shard_id(), []
).append(header)
header_list = []
# check proof of progress
shard_ids_to_check = self.env.quark_chain_config.get_initialized_shard_ids_before_root_height(
self.root_state.tip.height + 1
)
for shard_id in shard_ids_to_check:
headers = shard_id_to_header_list.get(shard_id, [])
header_list.extend(headers)
if len(headers) < self.env.quark_chain_config.PROOF_OF_PROGRESS_BLOCKS:
# Fallback to create minor block
block = await self.__get_minor_block_to_mine(
Branch.create(self.__get_shard_size(), shard_id), address
)
return (None, None) if not block else (False, block)
return (True, self.root_state.create_block_to_mine(header_list, address))
async def __get_minor_block_to_mine(self, branch, address):
request = GetNextBlockToMineRequest(
branch=branch,
address=address.address_in_branch(branch),
artificial_tx_config=self.get_artificial_tx_config(),
)
slave = self.get_slave_connection(branch)
_, response, _ = await slave.write_rpc_request(
ClusterOp.GET_NEXT_BLOCK_TO_MINE_REQUEST, request
)
return response.block if response.error_code == 0 else None
async def get_next_block_to_mine(
self, address, shard_mask_value=0, prefer_root=False, randomize_output=True
):
""" Returns (is_root_block, block)
shard_mask_value = 0 means considering root chain and all the shards
"""
# Mining old blocks is useless
if self.synchronizer.running:
return None, None
if prefer_root and shard_mask_value == 0:
return await self.__create_root_block_to_mine_or_fallback_to_minor_block(
address
)
shard_mask = None if shard_mask_value == 0 else ShardMask(shard_mask_value)
futures = []
# Collect EcoInfo from shards
for slave in self.slave_pool:
if shard_mask and not slave.has_overlap(shard_mask):
continue
request = GetEcoInfoListRequest()
futures.append(
slave.write_rpc_request(ClusterOp.GET_ECO_INFO_LIST_REQUEST, request)
)
responses = await asyncio.gather(*futures)
# Slaves may run multiple copies of the same branch
# We only need one EcoInfo per branch
# branch_value -> EcoInfo
branch_value_to_eco_info = dict()
for response in responses:
_, response, _ = response
if response.error_code != 0:
return (None, None)
for eco_info in response.eco_info_list:
branch_value_to_eco_info[eco_info.branch.value] = eco_info
root_coinbase_amount = 0
for branch_value, eco_info in branch_value_to_eco_info.items():
root_coinbase_amount += eco_info.unconfirmed_headers_coinbase_amount
root_coinbase_amount = root_coinbase_amount // 2
branch_value_with_max_eco = 0 if shard_mask is None else None
max_eco = root_coinbase_amount / self.root_state.get_next_block_difficulty()
dup_eco_count = 1
block_height = 0
for branch_value, eco_info in branch_value_to_eco_info.items():
if shard_mask and not shard_mask.contain_branch(Branch(branch_value)):
continue
# TODO: Obtain block reward and tx fee
eco = eco_info.coinbase_amount / eco_info.difficulty
if (
branch_value_with_max_eco is None
or eco > max_eco
or (
eco == max_eco
and branch_value_with_max_eco > 0
and block_height > eco_info.height
)
):
branch_value_with_max_eco = branch_value
max_eco = eco
dup_eco_count = 1
block_height = eco_info.height
elif eco == max_eco and randomize_output:
# The current block with max eco has smaller height, mine the block first
# This should be only used during bootstrap.
if branch_value_with_max_eco > 0 and block_height < eco_info.height:
continue
dup_eco_count += 1
if random.random() < 1 / dup_eco_count:
branch_value_with_max_eco = branch_value
max_eco = eco
if branch_value_with_max_eco == 0:
return await self.__create_root_block_to_mine_or_fallback_to_minor_block(
address
)
block = await self.__get_minor_block_to_mine(
Branch(branch_value_with_max_eco), address
)
return (None, None) if not block else (False, block)
async def get_account_data(self, address: Address):
""" Returns a dict where key is Branch and value is AccountBranchData """
futures = []
for slave in self.slave_pool:
request = GetAccountDataRequest(address)
futures.append(
slave.write_rpc_request(ClusterOp.GET_ACCOUNT_DATA_REQUEST, request)
)
responses = await asyncio.gather(*futures)
# Slaves may run multiple copies of the same branch
# We only need one AccountBranchData per branch
branch_to_account_branch_data = dict()
for response in responses:
_, response, _ = response
check(response.error_code == 0)
for account_branch_data in response.account_branch_data_list:
branch_to_account_branch_data[
account_branch_data.branch
] = account_branch_data
check(len(branch_to_account_branch_data) == len(self.env.quark_chain_config.get_genesis_shard_ids()))
return branch_to_account_branch_data
async def get_primary_account_data(
self, address: Address, block_height: Optional[int] = None
):
# TODO: Only query the shard who has the address
shard_id = address.get_shard_id(self.__get_shard_size())
branch = Branch.create(self.__get_shard_size(), shard_id)
slaves = self.branch_to_slaves.get(branch.value, None)
if not slaves:
return None
slave = slaves[0]
request = GetAccountDataRequest(address, block_height)
_, resp, _ = await slave.write_rpc_request(
ClusterOp.GET_ACCOUNT_DATA_REQUEST, request
)
for account_branch_data in resp.account_branch_data_list:
if account_branch_data.branch == branch:
return account_branch_data
return None
async def add_transaction(self, tx, from_peer=None):
""" Add transaction to the cluster and broadcast to peers """
evm_tx = tx.code.get_evm_transaction()
evm_tx.set_shard_size(self.__get_shard_size())
branch = Branch.create(self.__get_shard_size(), evm_tx.from_shard_id())
if branch.value not in self.branch_to_slaves:
return False
futures = []
for slave in self.branch_to_slaves[branch.value]:
futures.append(slave.add_transaction(tx))
success = all(await asyncio.gather(*futures))
if not success:
return False
if self.network is not None:
for peer in self.network.iterate_peers():
if peer == from_peer:
continue
try:
peer.send_transaction(tx)
except Exception:
Logger.log_exception()
return True
async def execute_transaction(
self, tx: Transaction, from_address, block_height: Optional[int]
) -> Optional[bytes]:
""" Execute transaction without persistence """
evm_tx = tx.code.get_evm_transaction()
evm_tx.set_shard_size(self.__get_shard_size())
branch = Branch.create(self.__get_shard_size(), evm_tx.from_shard_id())
if branch.value not in self.branch_to_slaves:
return None
futures = []
for slave in self.branch_to_slaves[branch.value]:
futures.append(slave.execute_transaction(tx, from_address, block_height))
responses = await asyncio.gather(*futures)
# failed response will return as None
success = all(r is not None for r in responses) and len(set(responses)) == 1
if not success:
return None
check(len(responses) >= 1)
return responses[0]
def handle_new_root_block_header(self, header, peer):
self.synchronizer.add_task(header, peer)
async def add_root_block(self, r_block):
""" Add root block locally and broadcast root block to all shards and .
All update root block should be done in serial to avoid inconsistent global root block state.
"""
self.root_state.validate_block(r_block) # throw exception if failed
update_tip = False
try:
update_tip = self.root_state.add_block(r_block)
success = True
except ValueError:
Logger.log_exception()
success = False
try:
if update_tip and self.network is not None:
for peer in self.network.iterate_peers():
peer.send_updated_tip()
except Exception:
pass
if success:
future_list = self.broadcast_rpc(
op=ClusterOp.ADD_ROOT_BLOCK_REQUEST,
req=AddRootBlockRequest(r_block, False),
)
result_list = await asyncio.gather(*future_list)
check(all([resp.error_code == 0 for _, resp, _ in result_list]))
self.root_miner.mine_new_block_async()
async def add_raw_minor_block(self, branch, block_data):
if branch.value not in self.branch_to_slaves:
return False
request = AddMinorBlockRequest(block_data)
# TODO: support multiple slaves running the same shard
_, resp, _ = await self.get_slave_connection(branch).write_rpc_request(
ClusterOp.ADD_MINOR_BLOCK_REQUEST, request