async def test_weight_proof_extend_new_ses(self, default_1000_blocks): blocks = default_1000_blocks header_cache, height_to_hash, sub_blocks, summaries = await load_blocks_dont_validate( blocks) # delete last summary last_ses_height = sorted(summaries.keys())[-1] last_ses = summaries[last_ses_height] del summaries[last_ses_height] wpf = WeightProofHandler( test_constants, BlockCache(sub_blocks, height_to_hash, header_cache, summaries)) wp = await wpf.get_proof_of_weight(blocks[last_ses_height - 10].header_hash) assert wp is not None wpf = WeightProofHandler( test_constants, BlockCache(sub_blocks, height_to_hash, header_cache)) valid, fork_point = wpf.validate_weight_proof(wp) assert valid assert fork_point == 0 # extend proof with 100 blocks summaries[last_ses_height] = last_ses wpf = WeightProofHandler( test_constants, BlockCache(sub_blocks, height_to_hash, header_cache, summaries)) new_wp = await wpf._extend_proof_of_weight( wp, sub_blocks[blocks[-1].header_hash]) valid, fork_point = wpf.validate_weight_proof(new_wp) assert valid assert fork_point != 0
async def _test_map_summaries(blocks, header_cache, height_to_hash, sub_blocks, summaries): curr = sub_blocks[blocks[-1].header_hash] orig_summaries: Dict[int, SubEpochSummary] = {} while curr.sub_block_height > 0: if curr.sub_epoch_summary_included is not None: orig_summaries[ curr.sub_block_height] = curr.sub_epoch_summary_included # next sub block curr = sub_blocks[curr.prev_hash] wpf = WeightProofHandler( test_constants, BlockCache(sub_blocks, height_to_hash, header_cache, summaries)) wp = await wpf.get_proof_of_weight(blocks[-1].header_hash) assert wp is not None # sub epoch summaries validate hashes summaries, sub_epoch_data_weight = _map_summaries( test_constants.SUB_EPOCH_SUB_BLOCKS, test_constants.GENESIS_SES_HASH, wp.sub_epochs, test_constants.DIFFICULTY_STARTING, ) assert len(summaries) == len(orig_summaries)
async def test_weight_proof10000(self, default_10000_blocks): blocks = default_10000_blocks header_cache, height_to_hash, sub_blocks, summaries = await load_blocks_dont_validate( blocks) wpf = WeightProofHandler( test_constants, BlockCache(sub_blocks, height_to_hash, header_cache, summaries)) wp = await wpf.get_proof_of_weight(blocks[-1].header_hash) assert wp is not None wpf = WeightProofHandler( test_constants, BlockCache(sub_blocks, height_to_hash, header_cache)) valid, fork_point = wpf.validate_weight_proof(wp) assert valid assert fork_point == 0
async def test_weight_proof_from_genesis(self, default_400_blocks): blocks = default_400_blocks header_cache, height_to_hash, sub_blocks, summaries = await load_blocks_dont_validate( blocks) wpf = WeightProofHandler( test_constants, BlockCache(sub_blocks, height_to_hash, header_cache, summaries)) wp = await wpf.get_proof_of_weight(blocks[-1].header_hash) assert wp is not None wp = await wpf.get_proof_of_weight(blocks[-1].header_hash) assert wp is not None
async def test_weight_proof_bad_peak_hash(self, default_1000_blocks): blocks = default_1000_blocks header_cache, height_to_hash, sub_blocks, summaries = await load_blocks_dont_validate( blocks) wpf = WeightProofHandler( test_constants, BlockCache(sub_blocks, height_to_hash, header_cache, summaries), ) wpf.log.setLevel(logging.INFO) initialize_logging("", {"log_stdout": True}, DEFAULT_ROOT_PATH) wp = await wpf.get_proof_of_weight(b"sadgfhjhgdgsfadfgh") assert wp is None
async def test_weight_proof_extend_no_ses(self, default_1000_blocks): blocks = default_1000_blocks header_cache, height_to_hash, sub_blocks, summaries = await load_blocks_dont_validate( blocks) last_ses_height = sorted(summaries.keys())[-1] wpf = WeightProofHandler( test_constants, BlockCache(sub_blocks, height_to_hash, header_cache, summaries)) wp = await wpf.get_proof_of_weight(blocks[last_ses_height].header_hash) assert wp is not None # todo for each sampled sub epoch, validate number of segments wpf = WeightProofHandler( test_constants, BlockCache(sub_blocks, height_to_hash, header_cache)) valid, fork_point = wpf.validate_weight_proof(wp) assert valid assert fork_point == 0 # extend proof with 100 blocks new_wp = await wpf._extend_proof_of_weight( wp, sub_blocks[blocks[-1].header_hash]) valid, fork_point = wpf.validate_weight_proof(new_wp) assert valid assert fork_point == 0
async def test_weight_proof_summaries_1000_blocks(self, default_1000_blocks): blocks = default_1000_blocks header_cache, height_to_hash, sub_blocks, summaries = await load_blocks_dont_validate( blocks) wpf = WeightProofHandler( test_constants, BlockCache(sub_blocks, height_to_hash, header_cache, summaries)) wp = await wpf.get_proof_of_weight(blocks[-1].header_hash) summaries, sub_epoch_data_weight = _map_summaries( wpf.constants.SUB_EPOCH_SUB_BLOCKS, wpf.constants.GENESIS_SES_HASH, wp.sub_epochs, wpf.constants.DIFFICULTY_STARTING, ) assert wpf._validate_summaries_weight(sub_epoch_data_weight, summaries, wp)
async def test_weight_proof_validate_segment(self, default_400_blocks): blocks = default_400_blocks header_cache, height_to_hash, sub_blocks, summaries = await load_blocks_dont_validate( blocks) wpf = WeightProofHandler( test_constants, BlockCache(sub_blocks, height_to_hash, header_cache, summaries)) summaries_list: List[SubEpochSummary] = [] for key in sorted(summaries.keys()): summaries_list.append(summaries[key]) wp = await wpf._create_proof_of_weight(blocks[-1].header_hash) res, _, _, _, _ = wpf._validate_segment_slots( wp.sub_epoch_segments[0], test_constants.SUB_SLOT_ITERS_STARTING, test_constants.DIFFICULTY_STARTING, None, ) assert res
async def test_weight_proof_from_database(self): connection = await aiosqlite.connect("path to db") block_store: BlockStore = await BlockStore.create(connection) sub_blocks, peak = await block_store.get_sub_block_records() sub_height_to_hash = {} sub_epoch_summaries = {} if len(sub_blocks) == 0: return None, None assert peak is not None peak_height = sub_blocks[peak].sub_block_height # Sets the other state variables (peak_height and height_to_hash) curr: SubBlockRecord = sub_blocks[peak] while True: sub_height_to_hash[curr.sub_block_height] = curr.header_hash if curr.sub_epoch_summary_included is not None: sub_epoch_summaries[ curr.sub_block_height] = curr.sub_epoch_summary_included if curr.sub_block_height == 0: break curr = sub_blocks[curr.prev_hash] assert len(sub_height_to_hash) == peak_height + 1 block_cache = BlockCache(sub_blocks, sub_height_to_hash, sub_epoch_summaries=sub_epoch_summaries, block_store=block_store) wpf = WeightProofHandler(DEFAULT_CONSTANTS, block_cache) wp = await wpf._create_proof_of_weight(sub_height_to_hash[peak_height - 1]) valid, fork_point = wpf.validate_weight_proof(wp) await connection.close() assert valid