def test_chaindb_get_score(chaindb, fixture_sm_class, genesis_block, fork_choice_scoring): chaindb.persist_block(genesis_block, genesis_block.__class__, fork_choice_scoring) genesis_score_key = SchemaV1.make_block_root_to_score_lookup_key( genesis_block.message.hash_tree_root) genesis_score_data = chaindb.db.get(genesis_score_key) genesis_score_class = fork_choice_scoring.get_score_class() genesis_score = genesis_score_class.deserialize(genesis_score_data) expected_genesis_score = fork_choice_scoring.score(genesis_block.message) assert genesis_score == expected_genesis_score assert (chaindb.get_score(genesis_block.message.hash_tree_root, genesis_score_class) == expected_genesis_score) block1 = SignedBeaconBlock.from_parent(genesis_block, FromBlockParams(slot=1)) chaindb.persist_block(block1, block1.__class__, fork_choice_scoring) block1_score_key = SchemaV1.make_block_root_to_score_lookup_key( block1.message.hash_tree_root) block1_score_data = chaindb.db.get(block1_score_key) block1_score = genesis_score_class.deserialize(block1_score_data) expected_block1_score = fork_choice_scoring.score(block1.message) assert block1_score == expected_block1_score assert (chaindb.get_score(block1.message.hash_tree_root, genesis_score_class) == expected_block1_score)
def block_with_attestation(chaindb, genesis_block, sample_attestation_params, fork_choice_scoring): sample_attestation = Attestation.create(**sample_attestation_params) chaindb.persist_block(genesis_block, genesis_block.__class__, fork_choice_scoring) block1 = SignedBeaconBlock.from_parent( genesis_block, FromBlockParams(slot=1)).transform( ("message", "body", "attestations"), (sample_attestation, )) return block1, sample_attestation
def test_chaindb_get_canonical_head(chaindb, block, fork_choice_scoring): chaindb.persist_block(block, block.__class__, fork_choice_scoring) canonical_head_root = chaindb.get_canonical_head_root() assert canonical_head_root == block.message.hash_tree_root result_block = chaindb.get_canonical_head(block.__class__) assert result_block == block block_2 = SignedBeaconBlock.from_parent( block, FromBlockParams(slot=block.message.slot + 1)) chaindb.persist_block(block_2, block_2.__class__, fork_choice_scoring) result_block = chaindb.get_canonical_head(block.__class__) assert result_block == block_2 block_3 = SignedBeaconBlock.from_parent( block_2, FromBlockParams(slot=block_2.message.slot + 1)) chaindb.persist_block(block_3, block_3.__class__, fork_choice_scoring) result_block = chaindb.get_canonical_head(block.__class__) assert result_block == block_3
def test_chaindb_get_finalized_head( chaindb_at_genesis, genesis_block, genesis_state, sample_beacon_block_params, fork_choice_scoring, ): chaindb = chaindb_at_genesis block = SignedBeaconBlock.from_parent(genesis_block, FromBlockParams(slot=1)) assert chaindb.get_finalized_head(genesis_block.__class__) == genesis_block assert chaindb.get_justified_head(genesis_block.__class__) == genesis_block state_with_finalized_block = genesis_state.set( "finalized_checkpoint", Checkpoint.create(root=block.signing_root)) chaindb.persist_state(state_with_finalized_block) chaindb.persist_block(block, SignedBeaconBlock, fork_choice_scoring) assert (chaindb.get_finalized_head(SignedBeaconBlock).signing_root == block.signing_root) assert chaindb.get_justified_head(genesis_block.__class__) == genesis_block
def test_chaindb_get_justified_head( chaindb_at_genesis, genesis_block, genesis_state, sample_beacon_block_params, fork_choice_scoring, config, ): chaindb = chaindb_at_genesis block = SignedBeaconBlock.from_parent(genesis_block, FromBlockParams(slot=1)) assert chaindb.get_finalized_head(genesis_block.__class__) == genesis_block assert chaindb.get_justified_head(genesis_block.__class__) == genesis_block # test that there is only one justified head per epoch state_with_bad_epoch = genesis_state.set( "current_justified_checkpoint", Checkpoint.create(root=block.message.hash_tree_root, epoch=config.GENESIS_EPOCH), ) chaindb.persist_state(state_with_bad_epoch) chaindb.persist_block(block, SignedBeaconBlock, fork_choice_scoring) assert chaindb.get_finalized_head(genesis_block.__class__) == genesis_block assert chaindb.get_justified_head(genesis_block.__class__) == genesis_block # test that the we can update justified head if we satisfy the invariants state_with_justified_block = genesis_state.set( "current_justified_checkpoint", Checkpoint.create(root=block.message.hash_tree_root, epoch=config.GENESIS_EPOCH + 1), ) chaindb.persist_state(state_with_justified_block) assert chaindb.get_finalized_head(genesis_block.__class__) == genesis_block assert (chaindb.get_justified_head(SignedBeaconBlock).message. hash_tree_root == block.message.hash_tree_root)
def block(request): return SignedBeaconBlock.create(message=BeaconBlock.create( parent_root=GENESIS_PARENT_HASH, slot=request.param))