def test_chaindb_get_score(chaindb, sample_beacon_block_params): genesis = BaseBeaconBlock(**sample_beacon_block_params).copy( parent_root=GENESIS_PARENT_HASH, slot=0, ) chaindb.persist_block(genesis) genesis_score_key = SchemaV1.make_block_root_to_score_lookup_key( genesis.root) genesis_score = rlp.decode(chaindb.db.get(genesis_score_key), sedes=rlp.sedes.big_endian_int) assert genesis_score == 0 assert chaindb.get_score(genesis.root) == 0 block1 = BaseBeaconBlock(**sample_beacon_block_params).copy( parent_root=genesis.root, slot=1, ) chaindb.persist_block(block1) block1_score_key = SchemaV1.make_block_root_to_score_lookup_key( block1.root) block1_score = rlp.decode(chaindb.db.get(block1_score_key), sedes=rlp.sedes.big_endian_int) assert block1_score == 1 assert chaindb.get_score(block1.root) == 1
def test_update_attestations(sample_attestation_record_params, sample_beacon_block_params): block = BaseBeaconBlock(**sample_beacon_block_params) attestations = block.attestations attestations = list(attestations) attestations.append(AttestationRecord(**sample_attestation_record_params)) block2 = block.copy(attestations=attestations) assert block2.num_attestations == 1
def test_validate_proposer_signature( proposer_privkey, proposer_pubkey, is_valid_signature, sample_beacon_block_params, sample_beacon_state_params, sample_shard_committee_params, beacon_chain_shard_number, epoch_length): state = BeaconState(**sample_beacon_state_params).copy( validator_registry=[ mock_validator_record(proposer_pubkey) for _ in range(10) ], shard_committees_at_slots=get_sample_shard_committees_at_slots( num_slot=128, num_shard_committee_per_slot=10, sample_shard_committee_params=sample_shard_committee_params, ), ) default_block = BaseBeaconBlock(**sample_beacon_block_params) empty_signature_block_root = default_block.block_without_signature_root proposal_root = ProposalSignedData( state.slot, beacon_chain_shard_number, empty_signature_block_root, ).root proposed_block = BaseBeaconBlock(**sample_beacon_block_params).copy( signature=bls.sign( message=proposal_root, privkey=proposer_privkey, domain=SignatureDomain.DOMAIN_PROPOSAL, ), ) if is_valid_signature: validate_proposer_signature( state, proposed_block, beacon_chain_shard_number, epoch_length, ) else: with pytest.raises(ValidationError): validate_proposer_signature( state, proposed_block, beacon_chain_shard_number, epoch_length, )
def test_update_attestations(sample_attestation_params, sample_beacon_block_params): block = BaseBeaconBlock(**sample_beacon_block_params) attestations = block.body.attestations attestations = list(attestations) attestations.append(Attestation(**sample_attestation_params)) body2 = block.body.copy( attestations=attestations ) block2 = block.copy( body=body2 ) assert block2.num_attestations == 1
def process_block( cls, crystallized_state: CrystallizedState, active_state: ActiveState, block: BaseBeaconBlock, chaindb: BaseBeaconChainDB, is_validating_signatures: bool = True ) -> Tuple[BaseBeaconBlock, CrystallizedState, ActiveState]: """ Process ``block`` and return the new crystallized state and active state. """ # Process per block state changes (ActiveState) processing_active_state = cls.compute_per_block_transition( crystallized_state, active_state, block, chaindb, is_validating_signatures=is_validating_signatures, ) # Process per cycle state changes (CrystallizedState and ActiveState) processed_crystallized_state, processed_active_state = cls.compute_cycle_transitions( crystallized_state, processing_active_state, block, ) # Return the copy result_block = block.copy() return result_block, processed_crystallized_state, processed_active_state
def test_demo(base_db, sample_beacon_block_params, sample_beacon_state_params, fixture_sm_class): chaindb = BeaconChainDB(base_db) block = BaseBeaconBlock(**sample_beacon_block_params) state = BeaconState(**sample_beacon_state_params) sm = fixture_sm_class(chaindb, block, state) result_state, result_block = sm.import_block(block) assert state.slot == 0 assert result_state.slot == sm.config.ZERO_BALANCE_VALIDATOR_TTL
def get_genesis_block(active_state_root: Hash32, crystallized_state_root: Hash32) -> BaseBeaconBlock: return BaseBeaconBlock( parent_hash=ZERO_HASH32, slot_number=0, randao_reveal=ZERO_HASH32, attestations=[], pow_chain_ref=ZERO_HASH32, active_state_root=active_state_root, crystallized_state_root=crystallized_state_root, )
def create_test_block(parent=None, **kwargs): defaults = { "slot": 0, "parent_root": ZERO_HASH32, "state_root": ZERO_HASH32, # note: not the actual genesis state root "randao_reveal": ZERO_HASH32, "candidate_pow_receipt_root": ZERO_HASH32, "signature": (0, 0), "body": empty_body() } if parent is not None: kwargs["parent_root"] = parent.root kwargs["slot"] = parent.slot + 1 return BaseBeaconBlock(**merge(defaults, kwargs))
def get_fresh_chain_db(): db = AtomicDB() genesis_block = BaseBeaconBlock( slot=0, randao_reveal=ZERO_HASH32, candidate_pow_receipt_root=ZERO_HASH32, ancestor_hashes=[ZERO_HASH32] * 32, state_root=ZERO_HASH32, # note: not the actual genesis state root attestations=[], specials=[], proposer_signature=None, ) chain_db = BeaconChainDB(db) chain_db.persist_block(genesis_block) return chain_db
def create_test_block(parent=None, **kwargs): defaults = { "slot": 0, "randao_reveal": ZERO_HASH32, "candidate_pow_receipt_root": ZERO_HASH32, "ancestor_hashes": [ZERO_HASH32] * 32, "state_root": ZERO_HASH32, # note: not the actual genesis state root "attestations": [], "specials": [], "proposer_signature": None, } if parent is not None: kwargs["ancestor_hashes"] = [parent.hash] + [ZERO_HASH32] * 31 kwargs["slot"] = parent.slot + 1 return BaseBeaconBlock(**merge(defaults, kwargs))
async def test_send_single_block(request, event_loop): alice, bob = await get_directly_linked_peers(request, event_loop) msg_buffer = MsgBuffer() bob.add_subscriber(msg_buffer) block = BaseBeaconBlock( slot=1, parent_root=ZERO_HASH32, state_root=ZERO_HASH32, randao_reveal=ZERO_HASH32, candidate_pow_receipt_root=ZERO_HASH32, signature=(0, 0), body=empty_body(), ) alice.sub_proto.send_blocks((block, )) message = await msg_buffer.msg_queue.get() assert isinstance(message.command, BeaconBlocks) assert message.payload == (block, )
async def test_send_single_block(request, event_loop): alice, bob = await get_directly_linked_peers(request, event_loop) msg_buffer = MsgBuffer() bob.add_subscriber(msg_buffer) block = BaseBeaconBlock( slot=1, randao_reveal=ZERO_HASH32, candidate_pow_receipt_root=ZERO_HASH32, ancestor_hashes=[ZERO_HASH32] * 32, state_root=ZERO_HASH32, attestations=[], specials=[], proposer_signature=None, ) alice.sub_proto.send_blocks((block, )) message = await msg_buffer.msg_queue.get() assert isinstance(message.command, BeaconBlocks) assert message.payload == (block, )
def test_hash(sample_block_params): block = BaseBeaconBlock(**sample_block_params) assert block.hash == blake(rlp.encode(block))
def test_defaults(param, default_value, sample_block_params): del sample_block_params[param] block = BaseBeaconBlock(**sample_block_params) assert getattr(block, param) == default_value
def test_parent_hash(sample_beacon_block_params, ancestor_hashes, parent_hash): block = BaseBeaconBlock(**sample_beacon_block_params).copy( ancestor_hashes=ancestor_hashes, ) assert block.parent_hash == parent_hash
def test_defaults(sample_beacon_block_params): block = BaseBeaconBlock(**sample_beacon_block_params) assert block.slot == sample_beacon_block_params['slot']
def block(request, sample_beacon_block_params): return BaseBeaconBlock(**sample_beacon_block_params).copy( parent_root=GENESIS_PARENT_HASH, slot=request.param, )
def block(request, sample_beacon_block_params): return BaseBeaconBlock(**sample_beacon_block_params).copy( ancestor_hashes=(GENESIS_PARENT_HASH, ), slot=request.param, )
def block(request, sample_block_params): return BaseBeaconBlock(**sample_block_params).copy( parent_hash=GENESIS_PARENT_HASH, slot_number=request.param, )
def sample_block(sample_beacon_block_params): return BaseBeaconBlock(**sample_beacon_block_params)