/
root_state.py
402 lines (330 loc) · 15.9 KB
/
root_state.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
import time
from quarkchain.config import NetworkId
from quarkchain.core import RootBlock, MinorBlockHeader
from quarkchain.core import (
calculate_merkle_root,
Serializable,
PrependedSizeListSerializer,
)
from quarkchain.diff import EthDifficultyCalculator
from quarkchain.genesis import GenesisManager
from quarkchain.utils import Logger, check
class LastMinorBlockHeaderList(Serializable):
FIELDS = [("header_list", PrependedSizeListSerializer(4, MinorBlockHeader))]
def __init__(self, header_list):
self.header_list = header_list
class RootDb:
""" Storage for all validated root blocks and minor blocks
On initialization it will try to recover the recent blocks (max_num_blocks_to_recover) of the best chain
from local database with tip referenced by "tipHash".
Note that we only recover the blocks on the best chain than including the forking blocks because
we don't save "tipHash"s for the forks and thus their consistency state is hard to reason about.
For example, a root block might not be received by all the shards when the cluster is down.
Forks can always be downloaded again from peers if they ever became the best chain.
"""
def __init__(self, db, max_num_blocks_to_recover):
# TODO: evict old blocks from memory
self.db = db
self.max_num_blocks_to_recover = max_num_blocks_to_recover
# TODO: May store locally to save memory space (e.g., with LRU cache)
self.m_hash_set = set()
self.r_header_pool = dict()
self.tip_header = None
self.__recover_from_db()
def __recover_from_db(self):
""" Recover the best chain from local database.
"""
Logger.info("Recovering root chain from local database...")
if b"tipHash" not in self.db:
return None
r_hash = self.db.get(b"tipHash")
r_block = RootBlock.deserialize(self.db.get(b"rblock_" + r_hash))
self.tip_header = r_block.header
while len(self.r_header_pool) < self.max_num_blocks_to_recover:
self.r_header_pool[r_hash] = r_block.header
for m_header in r_block.minor_block_header_list:
self.m_hash_set.add(m_header.get_hash())
if r_block.header.height <= 0:
break
r_hash = r_block.header.hash_prev_block
r_block = RootBlock.deserialize(self.db.get(b"rblock_" + r_hash))
def get_tip_header(self):
return self.tip_header
# ------------------------- Root block db operations --------------------------------
def put_root_block(
self, root_block, last_minor_block_header_list, root_block_hash=None
):
if root_block_hash is None:
root_block_hash = root_block.header.get_hash()
last_list = LastMinorBlockHeaderList(header_list=last_minor_block_header_list)
self.db.put(b"rblock_" + root_block_hash, root_block.serialize())
self.db.put(b"lastlist_" + root_block_hash, last_list.serialize())
self.r_header_pool[root_block_hash] = root_block.header
def update_tip_hash(self, block_hash):
self.db.put(b"tipHash", block_hash)
def get_root_block_by_hash(self, h, consistency_check=True):
if consistency_check and h not in self.r_header_pool:
return None
raw_block = self.db.get(b"rblock_" + h, None)
if not raw_block:
return None
return RootBlock.deserialize(raw_block)
def get_root_block_header_by_hash(self, h, consistency_check=True):
header = self.r_header_pool.get(h, None)
if not header and not consistency_check:
block = self.get_root_block_by_hash(h, False)
if block:
header = block.header
return header
def get_root_block_last_minor_block_header_list(self, h):
if h not in self.r_header_pool:
return None
return LastMinorBlockHeaderList.deserialize(
self.db.get(b"lastlist_" + h)
).header_list
def contain_root_block_by_hash(self, h):
return h in self.r_header_pool
def put_root_block_index(self, block):
self.db.put(b"ri_%d" % block.header.height, block.header.get_hash())
def get_root_block_by_height(self, height):
key = b"ri_%d" % height
if key not in self.db:
return None
block_hash = self.db.get(key)
return self.get_root_block_by_hash(block_hash, False)
# ------------------------- Minor block db operations --------------------------------
def contain_minor_block_by_hash(self, h):
return h in self.m_hash_set
def put_minor_block_hash(self, m_hash):
self.db.put(b"mheader_" + m_hash, b"")
self.m_hash_set.add(m_hash)
# ------------------------- Common operations -----------------------------------------
def put(self, key, value):
self.db.put(key, value)
def get(self, key, default=None):
return self.db.get(key, default)
def __getitem__(self, key):
return self[key]
class RootState:
""" State of root
"""
def __init__(self, env, diff_calc=None):
self.env = env
self.diff_calc = (
diff_calc
if diff_calc
else EthDifficultyCalculator(
cutoff=45, diff_factor=2048, minimum_diff=1000000
)
)
self.raw_db = env.db
self.db = RootDb(
self.raw_db, env.quark_chain_config.ROOT.max_root_blocks_in_memory
)
persisted_tip = self.db.get_tip_header()
if persisted_tip:
self.tip = persisted_tip
Logger.info(
"Recovered root state with tip height {}".format(self.tip.height)
)
else:
self.__create_genesis_block()
Logger.info("Created genesis root block")
def __create_genesis_block(self):
genesis_manager = GenesisManager(self.env.quark_chain_config)
genesis_block = genesis_manager.create_root_block()
self.db.put_root_block(genesis_block, [])
self.db.put_root_block_index(genesis_block)
self.tip = genesis_block.header
def get_tip_block(self):
return self.db.get_root_block_by_hash(self.tip.get_hash())
def add_validated_minor_block_hash(self, h):
self.db.put_minor_block_hash(h)
def get_next_block_difficulty(self, create_time=None):
if create_time is None:
create_time = max(self.tip.create_time + 1, int(time.time()))
return self.diff_calc.calculate_diff_with_parent(self.tip, create_time)
def create_block_to_mine(self, m_header_list, address, create_time=None):
if create_time is None:
create_time = max(self.tip.create_time + 1, int(time.time()))
difficulty = self.diff_calc.calculate_diff_with_parent(self.tip, create_time)
block = self.tip.create_block_to_append(
create_time=create_time, address=address, difficulty=difficulty
)
block.minor_block_header_list = m_header_list
coinbase_amount = 0
for header in m_header_list:
coinbase_amount += header.coinbase_amount
coinbase_amount = coinbase_amount // 2
return block.finalize(quarkash=coinbase_amount, coinbase_address=address)
def validate_block_header(self, block_header, block_hash=None):
""" Validate the block header.
"""
if block_header.height < 1:
raise ValueError("unexpected height")
if not self.db.contain_root_block_by_hash(block_header.hash_prev_block):
raise ValueError("previous hash block mismatch")
prev_block_header = self.db.get_root_block_header_by_hash(
block_header.hash_prev_block
)
if prev_block_header.height + 1 != block_header.height:
raise ValueError("incorrect block height")
if block_header.create_time <= prev_block_header.create_time:
raise ValueError(
"incorrect create time tip time {}, new block time {}".format(
block_header.create_time, prev_block_header.create_time
)
)
if block_hash is None:
block_hash = block_header.get_hash()
# Check difficulty
if not self.env.quark_chain_config.SKIP_ROOT_DIFFICULTY_CHECK:
if self.env.quark_chain_config.NETWORK_ID == NetworkId.MAINNET:
diff = self.diff_calc.calculate_diff_with_parent(
prev_block_header, block_header.create_time
)
if diff != block_header.difficulty:
raise ValueError("incorrect difficulty")
metric = diff * int.from_bytes(block_hash, byteorder="big")
if metric >= 2 ** 256:
raise ValueError("insufficient difficulty")
elif (
block_header.coinbase_address.recipient
!= self.env.quark_chain_config.testnet_master_address.recipient
):
raise ValueError("incorrect master to create the block")
return block_hash
def __is_same_chain(self, longer_block_header, shorter_block_header):
if shorter_block_header.height > longer_block_header.height:
return False
header = longer_block_header
for i in range(longer_block_header.height - shorter_block_header.height):
header = self.db.get_root_block_header_by_hash(header.hash_prev_block)
return header == shorter_block_header
def validate_block(self, block, block_hash=None):
if not self.db.contain_root_block_by_hash(block.header.hash_prev_block):
raise ValueError("previous hash block mismatch")
block_hash = self.validate_block_header(block.header, block_hash)
# Check the merkle tree
merkle_hash = calculate_merkle_root(block.minor_block_header_list)
if merkle_hash != block.header.hash_merkle_root:
raise ValueError("incorrect merkle root")
# Check whether all minor blocks are ordered, validated (and linked to previous block)
headers_map = dict() # shard_id -> List[MinorBlockHeader]
shard_id = (
block.minor_block_header_list[0].branch.get_shard_id()
if block.minor_block_header_list
else None
)
for m_header in block.minor_block_header_list:
if not self.db.contain_minor_block_by_hash(m_header.get_hash()):
raise ValueError(
"minor block is not validated. {}-{}".format(
m_header.branch.get_shard_id(), m_header.height
)
)
if m_header.create_time > block.header.create_time:
raise ValueError(
"minor block create time is larger than root block {} > {}".format(
m_header.create_time, block.header.create_time
)
)
if not self.__is_same_chain(
self.db.get_root_block_header_by_hash(block.header.hash_prev_block),
self.db.get_root_block_header_by_hash(m_header.hash_prev_root_block),
):
raise ValueError(
"minor block's prev root block must be in the same chain"
)
if m_header.branch.get_shard_id() < shard_id:
raise ValueError("shard id must be ordered")
elif m_header.branch.get_shard_id() > shard_id:
shard_id = m_header.branch.get_shard_id()
headers_map.setdefault(m_header.branch.get_shard_id(), []).append(m_header)
# TODO: Add coinbase
# check proof of progress
shard_ids_to_check_proof_of_progress = self.env.quark_chain_config.get_initialized_shard_ids_before_root_height(
block.header.height
)
for shard_id in shard_ids_to_check_proof_of_progress:
if (
len(headers_map.get(shard_id, []))
< self.env.quark_chain_config.PROOF_OF_PROGRESS_BLOCKS
):
raise ValueError("fail to prove progress")
# check minor block headers are linked
prev_last_minor_block_header_list = self.db.get_root_block_last_minor_block_header_list(
block.header.hash_prev_block
)
prev_header_map = dict() # shard_id -> MinorBlockHeader or None
for header in prev_last_minor_block_header_list:
prev_header_map[header.branch.get_shard_id()] = header
last_minor_block_header_list = []
for shard_id, headers in headers_map.items():
check(len(headers) > 0)
last_minor_block_header_list.append(headers[-1])
if shard_id not in shard_ids_to_check_proof_of_progress:
raise ValueError(
"found minor block header in root block {} for uninitialized shard {}".format(
block_hash.hex(), shard_id
)
)
prev_header_in_last_root_block = prev_header_map.get(shard_id, None)
if not prev_header_in_last_root_block:
pass
# no header in previous root block then it must start with genesis block
if headers[0].height != 0:
raise ValueError(
"genesis block height is not 0 for shard {} block hash {}".format(
shard_id, headers[0].get_hash().hex()
)
)
else:
headers = [prev_header_in_last_root_block] + headers
for i in range(len(headers) - 1):
if headers[i + 1].hash_prev_minor_block != headers[i].get_hash():
raise ValueError(
"minor block {} does not link to previous block {}".format(
headers[i + 1].get_hash(), headers[i].get_hash()
)
)
return block_hash, last_minor_block_header_list
def __rewrite_block_index_to(self, block):
""" Find the common ancestor in the current chain and rewrite index till block """
while block.header.height >= 0:
orig_block = self.db.get_root_block_by_height(block.header.height)
if orig_block and orig_block.header == block.header:
break
self.db.put_root_block_index(block)
block = self.db.get_root_block_by_hash(block.header.hash_prev_block)
def add_block(self, block, block_hash=None):
""" Add new block.
return True if a longest block is added, False otherwise
There are a couple of optimizations can be done here:
- the root block could only contain minor block header hashes as long as the shards fully validate the headers
- the header (or hashes) are un-ordered as long as they contains valid sub-chains from previous root block
"""
block_hash, last_minor_block_header_list = self.validate_block(
block, block_hash
)
self.db.put_root_block(
block, last_minor_block_header_list, root_block_hash=block_hash
)
if self.tip.height < block.header.height:
self.tip = block.header
self.db.update_tip_hash(block_hash)
self.__rewrite_block_index_to(block)
return True
return False
# -------------------------------- Root block db related operations ------------------------------
def get_root_block_by_hash(self, h):
return self.db.get_root_block_by_hash(h)
def contain_root_block_by_hash(self, h):
return self.db.contain_root_block_by_hash(h)
def get_root_block_header_by_hash(self, h):
return self.db.get_root_block_header_by_hash(h)
def get_root_block_by_height(self, height):
return self.db.get_root_block_by_height(height)
# --------------------------------- Minor block db related operations ----------------------------
def is_minor_block_validated(self, h):
return self.db.contain_minor_block_by_hash(h)