Add extra checks (#1443)

* Add extra checks

* Fix test

* Fix in a more secure way

* Add a redundant check just in case
This commit is contained in:
Mariano Sorgente 2021-03-24 02:28:58 +09:00 committed by Justin England
parent 96618c24c3
commit 641852ce76
4 changed files with 20 additions and 7 deletions

View file

@ -888,10 +888,13 @@ def validate_finished_header_block(
log.error(f"INVALID WEIGHT: {header_block} {prev_b} {expected_difficulty}")
return None, ValidationError(Err.INVALID_WEIGHT)
else:
# 27b. Check genesis block height, weight, and prev block hash
if header_block.height != uint32(0):
return None, ValidationError(Err.INVALID_HEIGHT)
if header_block.weight != constants.DIFFICULTY_STARTING:
return None, ValidationError(Err.INVALID_WEIGHT)
if header_block.prev_header_hash != constants.GENESIS_CHALLENGE:
return None, ValidationError(Err.INVALID_PREV_BLOCK_HASH)
# RC vdf challenge is taken from more recent of (slot start, prev_block)
if genesis_block:

View file

@ -186,6 +186,9 @@ class Blockchain(BlockchainInterface):
None,
)
if not genesis and (self.block_record(block.prev_header_hash).height + 1) != block.height:
return ReceiveBlockResult.INVALID_BLOCK, Err.INVALID_HEIGHT, None
if pre_validation_result is None:
if block.height == 0:
prev_b: Optional[BlockRecord] = None

View file

@ -140,14 +140,15 @@ async def pre_validate_blocks_multiprocessing(
diff_ssis: List[Tuple[uint64, uint64]] = []
for block in blocks:
if block.height != 0 and prev_b is None:
prev_b = block_records.block_record(block.prev_header_hash)
if block.height != 0:
assert block_records.contains_block(block.prev_header_hash)
if prev_b is None:
prev_b = block_records.block_record(block.prev_header_hash)
sub_slot_iters, difficulty = get_next_sub_slot_iters_and_difficulty(
constants, len(block.finished_sub_slots) > 0, prev_b, block_records
)
if block.reward_chain_block.signage_point_index >= constants.NUM_SPS_SUB_SLOT:
log.warning(f"Block: {block.reward_chain_block}")
overflow = is_overflow_block(constants, block.reward_chain_block.signage_point_index)
challenge = get_block_challenge(constants, block, BlockCache(recent_blocks), prev_b is None, overflow, False)
if block.reward_chain_block.challenge_chain_sp_vdf is None:
@ -178,9 +179,14 @@ async def pre_validate_blocks_multiprocessing(
block,
None,
)
recent_blocks[block_rec.header_hash] = block_rec
recent_blocks_compressed[block_rec.header_hash] = block_rec
block_records.add_block_record(block_rec) # Temporarily add block to dict
# Makes sure to not override the valid blocks already in block_records
if not block_records.contains_block(block_rec.header_hash):
block_records.add_block_record(block_rec) # Temporarily add block to dict
recent_blocks[block_rec.header_hash] = block_rec
recent_blocks_compressed[block_rec.header_hash] = block_rec
else:
recent_blocks[block_rec.header_hash] = block_records.block_record(block_rec.header_hash)
recent_blocks_compressed[block_rec.header_hash] = block_records.block_record(block_rec.header_hash)
prev_b = block_rec
diff_ssis.append((difficulty, sub_slot_iters))

View file

@ -721,6 +721,7 @@ class FullNode:
)
return False, advanced_peak, fork_height
for i, block in enumerate(blocks_to_validate):
assert pre_validation_results[i].required_iters is not None
(result, error, fork_height,) = await self.blockchain.receive_block(
block, pre_validation_results[i], None if advanced_peak else fork_point