This repository was archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsharding_protocol.py
56 lines (43 loc) · 1.64 KB
/
sharding_protocol.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
from casper.protocols.sharding.sharding_view import ShardingView
from casper.protocols.sharding.block import Block
from casper.protocols.sharding.sharding_plot_tool import ShardingPlotTool
from casper.protocol import Protocol
class ShardingProtocol(Protocol):
View = ShardingView
Message = Block
PlotTool = ShardingPlotTool
shard_genesis_blocks = dict()
curr_shard_idx = 0
curr_shard_ids = ['']
"""Shard ID's look like this:
''
/ \
'0' '1'
/ \ / \
'00''01''10''11'
Blocks can be merge mined between shards if
there is an edge between shards
That is, for ids shard_1 and shard_2, there can be a merge block if
abs(len(shard_1) - len(shard_2)) = 1 AND
for i in range(min(len(shard_1), len(shard_2))):
shard_1[i] = shard_2[i]
"""
@classmethod
def initial_message(cls, validator):
"""Returns a starting block for a shard"""
shard_id = cls.get_next_shard_id()
estimate = {'prev_blocks': set([None]), 'shard_ids': set([shard_id])}
cls.shard_genesis_blocks[shard_id] = Block(estimate, dict(), validator, -1, 0)
return cls.shard_genesis_blocks['']
@classmethod
def get_next_shard_id(cls):
next_id = cls.curr_shard_ids[cls.curr_shard_idx]
cls.curr_shard_idx += 1
if cls.curr_shard_idx == len(cls.curr_shard_ids):
next_ids = []
for shard_id in cls.curr_shard_ids:
next_ids.append(shard_id + '0')
next_ids.append(shard_id + '1')
cls.curr_shard_idx = 0
cls.curr_shard_ids = next_ids
return next_id