def test_higher_slot_fork_choice_scoring(sample_beacon_block_params, slot): block = BeaconBlock.create(**sample_beacon_block_params).set("slot", slot) expected_score = HigherSlotScore(slot) scoring = HigherSlotScoring() score = scoring.score(block) assert score == expected_score
def test_higher_slot_fork_choice_scoring(slot): block = BeaconBlock.create(slot=slot) expected_score = HigherSlotScore(slot) scoring = HigherSlotScoring() score = scoring.score(block) assert score == expected_score
def import_block( self, block: BaseSignedBeaconBlock ) -> Tuple[BaseSignedBeaconBlock, Tuple[BaseSignedBeaconBlock, ...], Tuple[ BaseSignedBeaconBlock, ...], ]: new_blocks, old_blocks = self._chain_db.persist_block( block, SignedBeaconBlock, HigherSlotScoring()) return None, new_blocks, old_blocks
async def get_fake_chain() -> FakeChain: genesis_config = Eth2GenesisConfig(MINIMAL_SERENITY_CONFIG) chain_db = AsyncBeaconChainDBFactory(genesis_config=genesis_config) genesis_block = SignedBeaconBlockFactory() chain_db.persist_block(genesis_block, SerenitySignedBeaconBlock, HigherSlotScoring()) return FakeChain(base_db=chain_db.db, genesis_config=genesis_config)
def _create( cls, model_class: Type[BaseBeaconChain], *args: Any, **kwargs: Any ) -> BaseBeaconChain: """ Create a BeaconChain according to the factory definition. NOTE: clients of this class may provide a ``branch`` keyword in the ``kwargs`` to construct a chain with a ``Collection[BaseSignedBeaconBlock]``. This ``branch`` is NOT assumed to have been constructed according to the full set of validity rules, e.g. lacking a proper signature so the ``perform_validation`` option to ``import_block`` is disabled. """ override_lengths(cls.config) if "num_validators" in kwargs: num_validators = kwargs["num_validators"] else: num_validators = cls.num_validators if kwargs["genesis_state"] is None: keymap = mk_keymap_of_size(num_validators) genesis_state, genesis_block = create_mock_genesis( config=cls.config, pubkeys=tuple(keymap.keys()), keymap=keymap, genesis_block_class=SerenityBeaconBlock, genesis_time=Timestamp(int(time.time())), ) elif kwargs["genesis_block"] is None: genesis_state = kwargs["genesis_state"] genesis_block = get_genesis_block( genesis_state.hash_tree_root, SerenityBeaconBlock ) else: genesis_state = kwargs["genesis_state"] genesis_block = kwargs["genesis_block"] db = kwargs.pop("db", AtomicDB()) genesis_config = model_class.get_genesis_state_machine_class().config chain = model_class.from_genesis( base_db=db, genesis_state=genesis_state, genesis_block=genesis_block, genesis_config=genesis_config, ) if kwargs["branch"] is not None: branch = kwargs["branch"] for block in branch: if block.is_genesis: continue # NOTE: ideally we use the ``import_block`` method # on ``chain`` but for the time being we skip some # validation corresponding to assumptions made in clients of # this class. A future refactoring should use the external API. chain.chaindb.persist_block( block, SerenitySignedBeaconBlock, HigherSlotScoring() ) return chain
async def test_json_rpc_http_server(aiohttp_raw_server, aiohttp_client, event_bus, base_db, ipc_path): manager = DBManager(base_db) with manager.run(ipc_path): # Set chaindb override_lengths(SERENITY_CONFIG) db = DBClient.connect(ipc_path) genesis_config = SERENITY_CONFIG chaindb = AsyncBeaconChainDB(db, genesis_config) fork_choice_scoring = HigherSlotScoring() genesis_state, genesis_block = create_mock_genesis( pubkeys=(), config=SERENITY_CONFIG, keymap=dict(), genesis_block_class=BeaconBlock, genesis_time=0, ) chaindb.persist_state(genesis_state) chaindb.persist_block( SignedBeaconBlock.create(message=genesis_block), SignedBeaconBlock, fork_choice_scoring, ) try: rpc = RPCServer(initialize_beacon_modules(chaindb, event_bus), chaindb, event_bus) raw_server = await aiohttp_raw_server( RPCHandler.handle(rpc.execute)) client = await aiohttp_client(raw_server) request_id = 1 request_data = { "jsonrpc": "2.0", "method": "beacon_head", "params": [], "id": request_id, } response = await client.post("/", json=request_data) response_data = await response.json() assert response_data["id"] == request_id result = response_data["result"] assert result["slot"] == 0 assert decode_hex( result["block_root"]) == genesis_block.hash_tree_root assert decode_hex( result["state_root"]) == genesis_state.hash_tree_root except KeyboardInterrupt: pass finally: await raw_server.close() db.close()
def import_block( self, block: BaseBeaconBlock, perform_validation: bool = True ) -> Tuple[ BaseBeaconBlock, Tuple[BaseBeaconBlock, ...], Tuple[BaseBeaconBlock, ...] ]: """ Remove the logics about `state`, because we only need to check a block's parent in `ReceiveServer`. """ try: self.get_block_by_root(block.parent_root) except BlockNotFound: raise ValidationError (new_canonical_blocks, old_canonical_blocks) = self.chaindb.persist_block( block, block.__class__, HigherSlotScoring() ) return block, new_canonical_blocks, old_canonical_blocks
def get_fork_choice_scoring(self) -> BaseForkChoiceScoring: return HigherSlotScoring()
def fork_choice_scoring(): return HigherSlotScoring()