def test_get_attestation_root(valid_chain, genesis_block, genesis_state, config, keymap, min_attestation_inclusion_delay): state_machine = valid_chain.get_state_machine() attestations = create_mock_signed_attestations_at_slot( state=genesis_state, config=config, state_machine=state_machine, attestation_slot=genesis_block.slot, beacon_block_root=genesis_block.signing_root, keymap=keymap, ) block = create_mock_block( state=genesis_state, config=config, state_machine=state_machine, block_class=genesis_block.__class__, parent_block=genesis_block, keymap=keymap, slot=genesis_state.slot + 1, attestations=attestations, ) valid_chain.import_block(block) # Only one attestation in attestations, so just check that one a0 = attestations[0] assert valid_chain.get_attestation_by_root(a0.root) == a0 assert valid_chain.attestation_exists(a0.root) fake_attestation = a0.copy(signature=b'\x78' * 96, ) with pytest.raises(AttestationRootNotFound): valid_chain.get_attestation_by_root(fake_attestation.root) assert not valid_chain.attestation_exists(fake_attestation.root)
def test_process_max_attestations(genesis_state, genesis_block, sample_beacon_block_params, sample_beacon_block_body_params, config, keymap): attestation_slot = config.GENESIS_SLOT current_slot = attestation_slot + config.MIN_ATTESTATION_INCLUSION_DELAY state = genesis_state.copy(slot=current_slot, ) attestations = create_mock_signed_attestations_at_slot( state=state, config=config, attestation_slot=attestation_slot, beacon_block_root=genesis_block.root, keymap=keymap, voted_attesters_ratio=1.0, ) attestations_count = len(attestations) assert attestations_count > 0 block_body = BeaconBlockBody(**sample_beacon_block_body_params).copy( attestations=attestations * (attestations_count // config.MAX_ATTESTATIONS + 1), ) block = SerenityBeaconBlock(**sample_beacon_block_params).copy( slot=current_slot, body=block_body, ) with pytest.raises(ValidationError): process_attestations( state, block, config, )
def test_process_attestations(genesis_state, genesis_block, sample_beacon_block_params, sample_beacon_block_body_params, config, keymap, fixture_sm_class, chaindb, empty_attestation_pool, success): attestation_slot = 0 current_slot = attestation_slot + config.MIN_ATTESTATION_INCLUSION_DELAY state = genesis_state.copy(slot=current_slot, ) attestations = create_mock_signed_attestations_at_slot( state=state, config=config, state_machine=fixture_sm_class( chaindb, empty_attestation_pool, genesis_block.slot, ), attestation_slot=attestation_slot, beacon_block_root=genesis_block.signing_root, keymap=keymap, voted_attesters_ratio=1.0, ) assert len(attestations) > 0 if not success: # create invalid attestation by shard # i.e. wrong parent invalid_attestation_data = attestations[-1].data.copy( crosslink=attestations[-1].data.crosslink.copy( parent_root=Crosslink(shard=333, ).root, )) invalid_attestation = attestations[-1].copy( data=invalid_attestation_data, ) attestations = attestations[:-1] + (invalid_attestation, ) block_body = BeaconBlockBody(**sample_beacon_block_body_params).copy( attestations=attestations, ) block = SerenityBeaconBlock(**sample_beacon_block_params).copy( slot=current_slot, body=block_body, ) if success: new_state = process_attestations( state, block, config, ) assert len(new_state.current_epoch_attestations) == len(attestations) else: with pytest.raises(ValidationError): process_attestations( state, block, config, )
def test_process_attestations(genesis_state, genesis_block, sample_beacon_block_params, sample_beacon_block_body_params, config, keymap, fixture_sm_class, base_db, success): attestation_slot = 0 current_slot = attestation_slot + config.MIN_ATTESTATION_INCLUSION_DELAY state = genesis_state.copy(slot=current_slot, ) attestations = create_mock_signed_attestations_at_slot( state=state, config=config, state_machine=fixture_sm_class( BeaconChainDB(base_db), genesis_block, ), attestation_slot=attestation_slot, beacon_block_root=genesis_block.signed_root, keymap=keymap, voted_attesters_ratio=1.0, ) assert len(attestations) > 0 if not success: # create invalid attestation in the future invalid_attestation_data = attestations[-1].data.copy(slot=state.slot + 10, ) invalid_attestation = attestations[-1].copy( data=invalid_attestation_data, ) attestations = attestations[:-1] + (invalid_attestation, ) block_body = BeaconBlockBody(**sample_beacon_block_body_params).copy( attestations=attestations, ) block = SerenityBeaconBlock(**sample_beacon_block_params).copy( slot=current_slot, body=block_body, ) if success: new_state = process_attestations( state, block, config, ) assert len(new_state.current_epoch_attestations) == len(attestations) else: with pytest.raises(ValidationError): process_attestations( state, block, config, )
def test_process_attestations( genesis_state, genesis_block, sample_beacon_block_params, sample_beacon_block_body_params, config, keymap, fixture_sm_class, chaindb, genesis_fork_choice_context, success, ): attestation_slot = 0 current_slot = attestation_slot + config.MIN_ATTESTATION_INCLUSION_DELAY state = genesis_state.set("slot", current_slot) attestations = create_mock_signed_attestations_at_slot( state=state, config=config, state_machine=fixture_sm_class(chaindb, genesis_fork_choice_context), attestation_slot=attestation_slot, beacon_block_root=genesis_block.message.hash_tree_root, keymap=keymap, voted_attesters_ratio=1.0, ) assert len(attestations) > 0 if not success: # create invalid attestation # i.e. wrong slot invalid_attestation_data = attestations[-1].data.set( "slot", state.slot + 1) invalid_attestation = attestations[-1].set("data", invalid_attestation_data) attestations = attestations[:-1] + (invalid_attestation, ) block_body = BeaconBlockBody.create(**sample_beacon_block_body_params).set( "attestations", attestations) block = SerenityBeaconBlock.create(**sample_beacon_block_params).mset( "slot", current_slot, "body", block_body) if success: new_state = process_attestations(state, block, config) assert len(new_state.current_epoch_attestations) == len(attestations) else: with pytest.raises(ValidationError): process_attestations(state, block, config)
def test_process_attestations(genesis_state, sample_beacon_block_params, sample_beacon_block_body_params, config, keymap, success): attestation_slot = 0 current_slot = attestation_slot + config.MIN_ATTESTATION_INCLUSION_DELAY state = genesis_state.copy(slot=current_slot, ) attestations = create_mock_signed_attestations_at_slot( state, config, attestation_slot, keymap, 1.0, ) assert len(attestations) > 0 if not success: # create invalid attestation in the future invalid_attestation_data = attestations[-1].data.copy(slot=state.slot + 10, ) invalid_attestation = attestations[-1].copy( data=invalid_attestation_data, ) attestations = attestations[:-1] + (invalid_attestation, ) block_body = BeaconBlockBody(**sample_beacon_block_body_params).copy( attestations=attestations, ) block = SerenityBeaconBlock(**sample_beacon_block_params).copy( slot=current_slot, body=block_body, ) if success: new_state = process_attestations( state, block, config, ) assert len(new_state.latest_attestations) == len(attestations) else: with pytest.raises(ValidationError): process_attestations( state, block, config, )
def _build_chain_of_blocks_with_states( chain, state, parent_block, slots, config, keymap, attestation_participation=1.0, eth1_block_hash=ZERO_HASH32, ): blocks = () states = () for slot in range(parent_block.slot + 1, parent_block.slot + 1 + slots): sm = chain.get_state_machine(state.slot) pre_state, _ = sm.apply_state_transition(state, future_slot=slot) proposer_index = get_beacon_proposer_index(pre_state, config) public_key = state.validators[proposer_index].pubkey private_key = keymap[public_key] randao_reveal = generate_randao_reveal(private_key, slot, pre_state, config) attestations = create_mock_signed_attestations_at_slot( state, config, sm, slot - 1, parent_block.hash_tree_root, keymap, voted_attesters_ratio=attestation_participation, ) block = create_block( slot, parent_block.hash_tree_root, randao_reveal, Eth1Data.create(block_hash=eth1_block_hash), attestations, state, sm, private_key, ) parent_block = block.message state, block = sm.apply_state_transition(state, block) blocks += (block, ) states += (state, ) return blocks, states
def test_get_attestation_root(valid_chain, genesis_block, genesis_state, keymap, min_attestation_inclusion_delay): state_machine = valid_chain.get_state_machine() config = state_machine.config attestations = create_mock_signed_attestations_at_slot( state=genesis_state, config=config, state_machine=state_machine, attestation_slot=genesis_block.slot, beacon_block_root=genesis_block.message.hash_tree_root, keymap=keymap, ) state = genesis_state slot = genesis_state.slot + 1 future_state, _ = state_machine.apply_state_transition(state, future_slot=slot) proposer_index = get_beacon_proposer_index(future_state, config) proposer = state.validators[proposer_index].pubkey private_key = keymap[proposer] randao_reveal = generate_randao_reveal(private_key, slot, state, config) block = create_block( slot, genesis_block.message.hash_tree_root, randao_reveal, state.eth1_data, attestations, state, state_machine, private_key, ) valid_chain.import_block(block) # Only one attestation in attestations, so just check that one a0 = attestations[0] assert valid_chain.get_attestation_by_root(a0.hash_tree_root) == a0 assert valid_chain.attestation_exists(a0.hash_tree_root) fake_attestation = a0.set("signature", b"\x78" * 96) with pytest.raises(AttestationRootNotFound): valid_chain.get_attestation_by_root(fake_attestation.hash_tree_root) assert not valid_chain.attestation_exists(fake_attestation.hash_tree_root)
def test_process_max_attestations( genesis_state, genesis_block, sample_beacon_block_params, sample_beacon_block_body_params, config, keymap, fixture_sm_class, chaindb, genesis_fork_choice_context, ): attestation_slot = config.GENESIS_SLOT current_slot = attestation_slot + config.MIN_ATTESTATION_INCLUSION_DELAY state = genesis_state.copy(slot=current_slot) attestations = create_mock_signed_attestations_at_slot( state=state, config=config, state_machine=fixture_sm_class(chaindb, genesis_fork_choice_context), attestation_slot=attestation_slot, beacon_block_root=genesis_block.signing_root, keymap=keymap, voted_attesters_ratio=1.0, ) attestations_count = len(attestations) assert attestations_count > 0 block_body = BeaconBlockBody(**sample_beacon_block_body_params).copy( attestations=attestations * (config.MAX_ATTESTATIONS // attestations_count + 1) ) block = SerenityBeaconBlock(**sample_beacon_block_params).copy( slot=current_slot, body=block_body ) with pytest.raises(ValidationError): process_attestations(state, block, config)
def test_demo(base_db, validator_count, keymap, pubkeys, fork_choice_scoring): bls.use_noop_backend() slots_per_epoch = 8 config = SERENITY_CONFIG._replace( SLOTS_PER_EPOCH=slots_per_epoch, GENESIS_EPOCH=slot_to_epoch(SERENITY_CONFIG.GENESIS_SLOT, slots_per_epoch), TARGET_COMMITTEE_SIZE=3, SHARD_COUNT=2, MIN_ATTESTATION_INCLUSION_DELAY=2, ) override_vector_lengths(config) fixture_sm_class = SerenityStateMachine.configure( __name__='SerenityStateMachineForTesting', config=config, ) genesis_slot = config.GENESIS_SLOT genesis_epoch = config.GENESIS_EPOCH chaindb = BeaconChainDB(base_db, config) attestation_pool = AttestationPool() # TODO(ralexstokes) clean up how the cache is populated for i in range(validator_count): pubkeys[i] genesis_state, genesis_block = create_mock_genesis( num_validators=validator_count, config=config, keymap=keymap, genesis_block_class=SerenityBeaconBlock, ) for i in range(validator_count): assert genesis_state.validators[i].is_active(genesis_slot) chaindb.persist_block(genesis_block, SerenityBeaconBlock, fork_choice_scoring) chaindb.persist_state(genesis_state) state = genesis_state block = genesis_block chain_length = 3 * config.SLOTS_PER_EPOCH blocks = (block, ) attestations_map = {} # Dict[Slot, Sequence[Attestation]] for current_slot in range(genesis_slot + 1, genesis_slot + chain_length + 1): if current_slot > genesis_slot + config.MIN_ATTESTATION_INCLUSION_DELAY: attestations = attestations_map[ current_slot - config.MIN_ATTESTATION_INCLUSION_DELAY] else: attestations = () block = create_mock_block( state=state, config=config, state_machine=fixture_sm_class( chaindb, attestation_pool, blocks[-1].slot, ), block_class=SerenityBeaconBlock, parent_block=block, keymap=keymap, slot=current_slot, attestations=attestations, ) # Get state machine instance sm = fixture_sm_class( chaindb, attestation_pool, blocks[-1].slot, ) state, _ = sm.import_block(block) chaindb.persist_state(state) chaindb.persist_block(block, SerenityBeaconBlock, fork_choice_scoring) blocks += (block, ) # Mock attestations attestation_slot = current_slot attestations = create_mock_signed_attestations_at_slot( state=state, config=config, state_machine=fixture_sm_class( chaindb, attestation_pool, block.slot, ), attestation_slot=attestation_slot, beacon_block_root=block.signing_root, keymap=keymap, voted_attesters_ratio=1.0, ) attestations_map[attestation_slot] = attestations assert state.slot == chain_length + genesis_slot # Justification assertions assert state.current_justified_epoch == genesis_epoch assert state.finalized_epoch == genesis_epoch
def test_demo(base_db, num_validators, config, keymap, fixture_sm_class): genesis_slot = config.GENESIS_SLOT genesis_epoch = config.GENESIS_EPOCH chaindb = BeaconChainDB(base_db) genesis_state, genesis_block = create_mock_genesis( num_validators=num_validators, config=config, keymap=keymap, genesis_block_class=SerenityBeaconBlock, ) for i in range(num_validators): assert genesis_state.validator_registry[i].is_active(genesis_slot) chaindb.persist_block(genesis_block, SerenityBeaconBlock) chaindb.persist_state(genesis_state) state = genesis_state block = genesis_block chain_length = 3 * config.SLOTS_PER_EPOCH blocks = (block, ) attestations_map = {} # Dict[Slot, Sequence[Attestation]] for current_slot in range(genesis_slot + 1, genesis_slot + chain_length + 1): if current_slot > genesis_slot + config.MIN_ATTESTATION_INCLUSION_DELAY: attestations = attestations_map[ current_slot - config.MIN_ATTESTATION_INCLUSION_DELAY] else: attestations = () block = create_mock_block( state=state, config=config, state_machine=fixture_sm_class( chaindb, blocks[-1], ), block_class=SerenityBeaconBlock, parent_block=block, keymap=keymap, slot=current_slot, attestations=attestations, ) # Get state machine instance sm = fixture_sm_class( chaindb, blocks[-1], ) state, _ = sm.import_block(block) chaindb.persist_state(state) chaindb.persist_block(block, SerenityBeaconBlock) blocks += (block, ) # Mock attestations attestation_slot = current_slot attestations = create_mock_signed_attestations_at_slot( state=state, config=config, state_machine=fixture_sm_class( chaindb, block, ), attestation_slot=attestation_slot, beacon_block_root=block.signed_root, keymap=keymap, voted_attesters_ratio=1.0, ) attestations_map[attestation_slot] = attestations assert state.slot == chain_length + genesis_slot assert isinstance(sm.block, SerenityBeaconBlock) # Justification assertions assert state.current_justified_epoch == 2 + genesis_epoch assert state.finalized_epoch == 1 + genesis_epoch
def test_demo(base_db, validator_count, keymap, pubkeys, fork_choice_scoring): bls.use_noop_backend() config = MINIMAL_SERENITY_CONFIG override_lengths(config) fixture_sm_class = SerenityStateMachine.configure( __name__="SerenityStateMachineForTesting", config=config) genesis_slot = config.GENESIS_SLOT genesis_epoch = config.GENESIS_EPOCH chaindb = BeaconChainDB(base_db, config) genesis_state, genesis_block = create_mock_genesis( pubkeys=pubkeys[:validator_count], config=config, keymap=keymap, genesis_block_class=SerenityBeaconBlock, ) for i in range(validator_count): assert genesis_state.validators[i].is_active(genesis_slot) chaindb.persist_block( SerenitySignedBeaconBlock.create(message=genesis_block), SerenitySignedBeaconBlock, fork_choice_scoring, ) chaindb.persist_state(genesis_state) state = genesis_state block = SerenitySignedBeaconBlock.create(message=genesis_block) chain_length = 4 * config.SLOTS_PER_EPOCH blocks = (block, ) attestations_map = {} # Dict[Slot, Sequence[Attestation]] for current_slot in range(genesis_slot + 1, genesis_slot + chain_length + 1): if current_slot > genesis_slot + config.MIN_ATTESTATION_INCLUSION_DELAY: attestations = attestations_map[ current_slot - config.MIN_ATTESTATION_INCLUSION_DELAY] else: attestations = () block = create_mock_block( state=state, config=config, state_machine=fixture_sm_class(chaindb), signed_block_class=SerenitySignedBeaconBlock, parent_block=block, keymap=keymap, slot=current_slot, attestations=attestations, ) # Get state machine instance sm = fixture_sm_class(chaindb) state, _ = sm.import_block(block, state) chaindb.persist_state(state) chaindb.persist_block(block, SerenitySignedBeaconBlock, fork_choice_scoring) blocks += (block, ) # Mock attestations attestation_slot = current_slot attestations = create_mock_signed_attestations_at_slot( state=state, config=config, state_machine=fixture_sm_class(chaindb), attestation_slot=attestation_slot, beacon_block_root=block.signing_root, keymap=keymap, voted_attesters_ratio=1.0, ) attestations_map[attestation_slot] = attestations assert state.slot == chain_length + genesis_slot # Justification assertions # NOTE: why are the number `2` or `3` used in the checks below? # Answer: # "We do not check any justification and finality during epochs 0 or 1. We do check for # justification and finality from epoch 2 onward." # [epoch 0]------[epoch 1]------> # # "In epoch 2, we justify the current epoch. This epoch is in fact justified but we do not # recognize it in the protocol due to an artifact of the construction of the genesis state # (using the `zero` value for `Checkpoint` type)." # [epoch 0]------[epoch 1]------[epoch 2]*------> # []*: checkpoint justified # []**: checkpoint finalized # # "In epoch 3, we have the previous justified checkpoint at the prior current justified # checkpoint (so `GENESIS_EPOCH + 2`) and we justify this current epoch. we check finality here # and see that we finalize the prior justified checkpoint at epoch 2." # [epoch 0]------[epoch 1]------[epoch 2]**------[epoch 3]*------> # # "Given the way we handle epoch processing (i.e. process a given epoch at the start of # the next epoch), we need to transition through `4 * SLOTS_PER_EPOCH` worth of slots to # include the processing of epoch 3." # # source: https://github.com/ethereum/trinity/pull/1214#issuecomment-546184080 # # epoch | prev_justified_checkpoint | cur_justified_checkpoint | finalized_checkpoint # ------|---------------------------|--------------------------|--------------------- # 0 | 0 | 0 | 0 # 1 | 0 | 0 | 0 # 2 | 0 | 0 | 0 # 3 | 0 | 2 | 0 # 4 | 2 | 3 | 2 assert state.previous_justified_checkpoint.epoch == 2 + genesis_epoch assert state.current_justified_checkpoint.epoch == 3 + genesis_epoch assert state.finalized_checkpoint.epoch == 2 + genesis_epoch
def _mk_attestations_from(blocks, states, chain, config, keymap): for block, state in zip(blocks, states): sm = chain.get_state_machine(block.slot) yield from create_mock_signed_attestations_at_slot( state, config, sm, block.slot, block.message.hash_tree_root, keymap)
def test_demo(base_db, keymap): slots_per_epoch = 8 config = SERENITY_CONFIG._replace( SLOTS_PER_EPOCH=slots_per_epoch, GENESIS_EPOCH=slot_to_epoch(SERENITY_CONFIG.GENESIS_SLOT, slots_per_epoch), TARGET_COMMITTEE_SIZE=3, SHARD_COUNT=2, MIN_ATTESTATION_INCLUSION_DELAY=2, ) fixture_sm_class = SerenityStateMachine.configure( __name__='SerenityStateMachineForTesting', config=config, ) num_validators = 40 genesis_slot = config.GENESIS_SLOT genesis_epoch = config.GENESIS_EPOCH chaindb = BeaconChainDB(base_db, config) genesis_state, genesis_block = create_mock_genesis( num_validators=num_validators, config=config, keymap=keymap, genesis_block_class=SerenityBeaconBlock, ) for i in range(num_validators): assert genesis_state.validator_registry[i].is_active(genesis_slot) chaindb.persist_block(genesis_block, SerenityBeaconBlock) chaindb.persist_state(genesis_state) state = genesis_state block = genesis_block chain_length = 3 * config.SLOTS_PER_EPOCH blocks = (block, ) attestations_map = {} # Dict[Slot, Sequence[Attestation]] for current_slot in range(genesis_slot + 1, genesis_slot + chain_length + 1): if current_slot > genesis_slot + config.MIN_ATTESTATION_INCLUSION_DELAY: attestations = attestations_map[ current_slot - config.MIN_ATTESTATION_INCLUSION_DELAY] else: attestations = () block = create_mock_block( state=state, config=config, state_machine=fixture_sm_class( chaindb, blocks[-1], ), block_class=SerenityBeaconBlock, parent_block=block, keymap=keymap, slot=current_slot, attestations=attestations, ) # Get state machine instance sm = fixture_sm_class( chaindb, blocks[-1], ) state, _ = sm.import_block(block) chaindb.persist_state(state) chaindb.persist_block(block, SerenityBeaconBlock) blocks += (block, ) # Mock attestations attestation_slot = current_slot attestations = create_mock_signed_attestations_at_slot( state=state, config=config, state_machine=fixture_sm_class( chaindb, block, ), attestation_slot=attestation_slot, beacon_block_root=block.signing_root, keymap=keymap, voted_attesters_ratio=1.0, ) attestations_map[attestation_slot] = attestations assert state.slot == chain_length + genesis_slot assert isinstance(sm.block, SerenityBeaconBlock) # Justification assertions assert state.current_justified_epoch == 2 + genesis_epoch assert state.finalized_epoch == 1 + genesis_epoch
def test_demo(base_db, num_validators, config, keymap, fixture_sm_class): chaindb = BeaconChainDB(base_db) genesis_state, genesis_block = create_mock_genesis( num_validators=num_validators, config=config, keymap=keymap, genesis_block_class=SerenityBeaconBlock, ) for i in range(num_validators): assert genesis_state.validator_registry[i].is_active(0) chaindb.persist_block(genesis_block, SerenityBeaconBlock) chaindb.persist_state(genesis_state) state = genesis_state current_slot = 1 chain_length = 3 * config.EPOCH_LENGTH attestations = () for current_slot in range(chain_length): # two epochs block = create_mock_block( state=state, config=config, block_class=SerenityBeaconBlock, parent_block=genesis_block, keymap=keymap, slot=current_slot, attestations=attestations, ) block = block.copy( body=block.body.copy( attestations=attestations, ) ) # Get state machine instance sm = fixture_sm_class( chaindb, block, parent_block_class=SerenityBeaconBlock, ) state, _ = sm.import_block(block) # TODO: move to chain level? block = block.copy( state_root=state.root, ) chaindb.persist_state(state) chaindb.persist_block(block, SerenityBeaconBlock) if current_slot > config.MIN_ATTESTATION_INCLUSION_DELAY: attestation_slot = current_slot - config.MIN_ATTESTATION_INCLUSION_DELAY attestations = create_mock_signed_attestations_at_slot( state, config, attestation_slot, keymap, 1.0, ) else: attestations = () assert state.slot == chain_length - 1 assert isinstance(sm.block, SerenityBeaconBlock)