def from_genesis( cls, db: AtomicDatabaseAPI, genesis_state: BeaconState, signed_block_class: Type[BaseSignedBeaconBlock], genesis_fork_choice_scoring: BaseForkChoiceScoring, ) -> "BeaconChainDB": chain_db = cls(db) genesis_block = get_genesis_block(genesis_state.hash_tree_root, signed_block_class.block_class) genesis_root = genesis_block.hash_tree_root chain_db.persist_state(genesis_state) chain_db.update_head_state(genesis_state.slot, genesis_state.hash_tree_root) chain_db.persist_block( signed_block_class.create(message=genesis_block), signed_block_class, genesis_fork_choice_scoring, ) # NOTE: this should happen after persisting the state and block for now... chain_db._handle_exceptional_justification_and_finality(genesis_root) return chain_db
def create_mock_genesis( pubkeys: Sequence[BLSPubkey], config: Eth2Config, keymap: Dict[BLSPubkey, int], genesis_block_class: Type[BaseBeaconBlock], genesis_time: Timestamp = ZERO_TIMESTAMP, ) -> Tuple[BeaconState, BaseBeaconBlock]: genesis_deposits, deposit_root = create_mock_deposits_and_root( pubkeys=pubkeys, keymap=keymap, config=config) genesis_eth1_data = Eth1Data( deposit_root=deposit_root, deposit_count=len(genesis_deposits), block_hash=ZERO_HASH32, ) state = initialize_beacon_state_from_eth1( eth1_block_hash=genesis_eth1_data.block_hash, eth1_timestamp=genesis_time, deposits=genesis_deposits, config=config, ) block = get_genesis_block(genesis_state_root=state.hash_tree_root, block_class=genesis_block_class) assert len(state.validators) == len(pubkeys) return state, block
def get_genesis_block(self) -> BaseBeaconBlock: chain_class = self.beacon_chain_class genesis_state_machine_class = chain_class.get_genesis_state_machine_class() return get_genesis_block( genesis_state_root=self._genesis_state.hash_tree_root, block_class=genesis_state_machine_class.block_class, )
def create_mock_genesis( num_validators: int, config: Eth2Config, keymap: Dict[BLSPubkey, int], genesis_block_class: Type[BaseBeaconBlock], genesis_time: Timestamp = ZERO_TIMESTAMP ) -> Tuple[BeaconState, BaseBeaconBlock]: assert num_validators <= len(keymap) genesis_deposits, deposit_root = create_mock_deposits_and_root( pubkeys=list(keymap)[:num_validators], keymap=keymap, config=config, ) genesis_eth1_data = Eth1Data( deposit_root=deposit_root, deposit_count=len(genesis_deposits), block_hash=ZERO_HASH32, ) state = get_genesis_beacon_state( genesis_deposits=genesis_deposits, genesis_time=genesis_time, genesis_eth1_data=genesis_eth1_data, config=config, ) block = get_genesis_block( genesis_state_root=state.root, block_class=genesis_block_class, ) assert len(state.validators) == num_validators return state, block
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
def test_get_genesis_block(): genesis_state_root = b"\x10" * 32 genesis_slot = 0 genesis_block = get_genesis_block(genesis_state_root, BeaconBlock) assert genesis_block.slot == genesis_slot assert genesis_block.parent_root == ZERO_HASH32 assert genesis_block.state_root == genesis_state_root assert genesis_block.body.is_empty
def test_get_genesis_block(): genesis_state_root = b'\x10' * 32 genesis_slot = 0 genesis_block = get_genesis_block(genesis_state_root, BeaconBlock) assert genesis_block.slot == genesis_slot assert genesis_block.parent_root == ZERO_HASH32 assert genesis_block.state_root == genesis_state_root assert genesis_block.signature == EMPTY_SIGNATURE assert genesis_block.body.is_empty
def from_genesis(cls, genesis_state: BeaconState, config: Eth2Config, block_sink: BlockSink) -> "LMDGHOSTForkChoice": # NOTE: patch up genesis state to reflect the genesis block as an initial checkpoint # this only has to be patched once at genesis genesis_block = get_genesis_block(genesis_state.hash_tree_root, BeaconBlock) genesis_block_node = BlockNode(genesis_block.slot, default_root, genesis_block) return cls(genesis_block_node, genesis_state, config, block_sink)
async def test_sync_from_old_head(request, event_loop, event_bus, genesis_state): genesis_block = get_genesis_block(genesis_state.hash_tree_root, BeaconBlock) alice_branch = (genesis_block, ) + BeaconBlockFactory.create_branch( length=49, root=genesis_block) bob_branch = alice_branch + BeaconBlockFactory.create_branch( length=50, root=alice_branch[-1]) async with get_sync_setup(request, event_loop, event_bus, genesis_state, alice_branch, bob_branch) as (alice, bob): assert_synced(alice, bob, bob_branch)
async def test_reorg_sync(request, event_loop, event_bus, genesis_state): genesis_block = get_genesis_block(genesis_state.hash_tree_root, BeaconBlock) alice_branch = (genesis_block, ) + BeaconBlockFactory.create_branch( length=49, root=genesis_block, state_root=b"\x11" * 32) bob_branch = (genesis_block, ) + BeaconBlockFactory.create_branch( length=99, root=genesis_block, state_root=b"\x22" * 32) async with get_sync_setup(request, event_loop, event_bus, genesis_state, alice_branch, bob_branch) as (alice, bob): assert_synced(alice, bob, bob_branch)
async def test_sync_skipped_slots(request, event_loop, event_bus, genesis_state): genesis_block = get_genesis_block(genesis_state.hash_tree_root, BeaconBlock) alice_branch = (genesis_block, ) + BeaconBlockFactory.create_branch( length=0, root=genesis_block) bob_branch = (genesis_block, ) + BeaconBlockFactory.create_branch_by_slots( slots=tuple(range(4, 99)), root=genesis_block) assert bob_branch[0].slot == 0 assert bob_branch[1].slot == 4 async with get_sync_setup(request, event_loop, event_bus, genesis_state, alice_branch, bob_branch) as (alice, bob): assert_synced(alice, bob, bob_branch)
def initialize_chain(self, base_db: BaseAtomicDB) -> 'BeaconChain': chain_class = self.beacon_chain_class state = self.genesis_data.state block = get_genesis_block( genesis_state_root=state.root, block_class=chain_class.get_genesis_state_machine_class().block_class, ) return chain_class.from_genesis( base_db=base_db, genesis_state=state, genesis_block=block, genesis_config=self.genesis_config, )
def initialize_chain(self, base_db: AtomicDatabaseAPI) -> 'BaseBeaconChain': chain_class = self.beacon_chain_class state = self._genesis_state genesis_state_machine_class = chain_class.get_genesis_state_machine_class() block = get_genesis_block( genesis_state_root=state.hash_tree_root, block_class=genesis_state_machine_class.block_class, ) return chain_class.from_genesis( base_db=base_db, genesis_state=state, genesis_block=block, genesis_config=self._eth2_config, )
def initialize_chain(self, base_db: BaseAtomicDB) -> 'BeaconChain': config = self.eth2_config chain_class = self.beacon_chain_class state_machine_class = self.state_machine_class state = self.genesis_data.state block = get_genesis_block( genesis_state_root=state.root, genesis_slot=config. GENESIS_SLOT, # FIXME: Shouldn't access GENESIS_SLOT from a particular state machine configs. # noqa: E501 block_class=state_machine_class.block_class, ) return chain_class.from_genesis( base_db=base_db, genesis_state=state, genesis_block=block, config=config, )
async def test_sync_when_already_at_best_head(request, event_loop, event_bus, genesis_state): genesis_block = get_genesis_block(genesis_state.hash_tree_root, BeaconBlock) alice_branch = (genesis_block, ) + BeaconBlockFactory.create_branch( length=99, root=genesis_block, state_root=b"\x11" * 32) bob_branch = (genesis_block, ) + BeaconBlockFactory.create_branch( length=50, root=genesis_block, state_root=b"\x22" * 32) async with get_sync_setup(request, event_loop, event_bus, genesis_state, alice_branch, bob_branch) as (alice, bob): alice_head = alice.chain.get_canonical_head() assert alice_head.slot == 99 for correct_block in alice_branch: slot = correct_block.slot alice_block = alice.chain.get_canonical_block_by_slot(slot) assert alice_block == correct_block
def from_genesis( cls, db: AtomicDatabaseAPI, genesis_state: BeaconState, signed_block_class: Type[BaseSignedBeaconBlock], ) -> "BeaconChainDB": chain_db = cls(db) genesis_block = get_genesis_block(genesis_state.hash_tree_root, signed_block_class.block_class) chain_db.persist_block( signed_block_class.create(message=genesis_block)) chain_db.persist_state(genesis_state) chain_db.mark_canonical_block(genesis_block) chain_db.mark_finalized_head(genesis_block) return chain_db
def genesis_block(genesis_state): return get_genesis_block( genesis_state.hash_tree_root, SerenityBeaconBlock, )
def genesis_block(genesis_state, genesis_slot): return get_genesis_block( genesis_state.root, genesis_slot, SerenityBeaconBlock, )
def genesis_block(genesis_state, sample_signature): return SerenitySignedBeaconBlock.create( message=get_genesis_block(genesis_state.hash_tree_root, SerenityBeaconBlock), signature=sample_signature, )
def genesis_block(genesis_state): return get_genesis_block(genesis_state.hash_tree_root, BeaconBlock)