def _try_import_orphan_blocks(self, parent_root: Hash32) -> None: """ Perform ``chain.import`` on the blocks in ``self.orphan_block_pool`` in breadth-first order, starting from the children of ``parent_root``. """ imported_roots: List[Hash32] = [] imported_roots.append(parent_root) while len(imported_roots) != 0: current_parent_root = SigningRoot(imported_roots.pop()) # Only process the children if the `current_parent_root` is already in db. if not self._is_block_root_in_db(block_root=current_parent_root): continue # If succeeded, handle the orphan blocks which depend on this block. children = self.orphan_block_pool.pop_children(current_parent_root) if len(children) > 0: self.logger.debug( "Blocks=%s match their parent block, parent_root=%s", children, current_parent_root, ) for block in children: try: self.chain.import_block(block) self.logger.debug("Successfully imported block=%s", block) imported_roots.append(block.signing_root) except ValidationError as e: # TODO: Possibly drop all of its descendants in `self.orphan_block_pool`? self.logger.debug( "Fail to import invalid block=%s reason=%s", block, e) # Remove attestations in block that are also in the attestation pool. self.attestation_pool.remove(block.body.attestations)
async def _handle_get_beacon_blocks(self, session: SessionAPI, msg: GetBeaconBlocksMessage) -> None: peer = BCCProxyPeer.from_session(session, self.event_bus, self.broadcast_config) request_id = msg["request_id"] max_blocks = msg["max_blocks"] block_slot_or_root = msg["block_slot_or_root"] try: if isinstance(block_slot_or_root, int): # TODO: pass accurate `block_class: Type[BaseBeaconBlock]` under # per BeaconStateMachine fork start_block = await self.db.coro_get_canonical_block_by_slot( Slot(block_slot_or_root), BeaconBlock, ) elif isinstance(block_slot_or_root, bytes): # TODO: pass accurate `block_class: Type[BaseBeaconBlock]` under # per BeaconStateMachine fork start_block = await self.db.coro_get_block_by_root( SigningRoot(block_slot_or_root), BeaconBlock, ) else: raise TypeError( f"Invariant: unexpected type for 'block_slot_or_root': " f"{type(block_slot_or_root)}") except BlockNotFound: start_block = None if start_block is not None: self.logger.debug2( "%s requested %d blocks starting with %s", peer, max_blocks, start_block, ) blocks = tuple( [b async for b in self._get_blocks(start_block, max_blocks)]) else: self.logger.debug2("%s requested unknown block %s", block_slot_or_root) blocks = () self.logger.debug2("Replying to %s with %d blocks", peer, len(blocks)) peer.sub_proto.send_blocks(blocks, request_id)
from eth.constants import ZERO_HASH32 from eth_typing import BLSPubkey, BLSSignature from eth2.beacon.typing import Epoch, HashTreeRoot, SigningRoot, Timestamp EMPTY_SIGNATURE = BLSSignature(b"\x00" * 96) EMPTY_PUBKEY = BLSPubkey(b"\x00" * 48) GWEI_PER_ETH = 10 ** 9 FAR_FUTURE_EPOCH = Epoch(2 ** 64 - 1) ZERO_SIGNING_ROOT = SigningRoot(ZERO_HASH32) ZERO_HASH_TREE_ROOT = HashTreeRoot(ZERO_HASH32) GENESIS_PARENT_ROOT = ZERO_SIGNING_ROOT ZERO_TIMESTAMP = Timestamp(0) MAX_INDEX_COUNT = 2 ** 40 MAX_RANDOM_BYTE = 2 ** 8 - 1 BASE_REWARDS_PER_EPOCH = 4 DEPOSIT_CONTRACT_TREE_DEPTH = 2 ** 5 SECONDS_PER_DAY = 86400 JUSTIFICATION_BITS_LENGTH = 4
def _get_justified_head( cls, db: DatabaseAPI, block_class: Type[BaseBeaconBlock]) -> BaseBeaconBlock: justified_head_root = cls._get_justified_head_root(db) return cls._get_block_by_root(db, SigningRoot(justified_head_root), block_class)
def _get_canonical_head( cls, db: DatabaseAPI, block_class: Type[BaseBeaconBlock]) -> BaseBeaconBlock: canonical_head_root = cls._get_canonical_head_root(db) return cls._get_block_by_root(db, SigningRoot(canonical_head_root), block_class)
def from_genesis(cls, genesis_state: BeaconState, genesis_block: BaseBeaconBlock) -> BaseScore: score = (Gwei(0), score_block_by_root(SigningRoot(ZERO_HASH32))) return cls(score)
) def sign_transaction(*, message_hash: Hash32, privkey: int, state: BeaconState, slot: Slot, signature_domain: SignatureDomain, slots_per_epoch: int) -> BLSSignature: domain = get_domain( state, signature_domain, slots_per_epoch, message_epoch=compute_epoch_at_slot(slot, slots_per_epoch), ) return bls.sign(message_hash=message_hash, privkey=privkey, domain=domain) SAMPLE_HASH_1 = SigningRoot(Hash32(b"\x11" * 32)) SAMPLE_HASH_2 = Hash32(b"\x22" * 32) def create_block_header_with_signature( state: BeaconState, body_root: Hash32, privkey: int, slots_per_epoch: int, parent_root: SigningRoot = SAMPLE_HASH_1, state_root: Hash32 = SAMPLE_HASH_2, ) -> BeaconBlockHeader: block_header = BeaconBlockHeader( slot=state.slot, parent_root=parent_root, state_root=state_root,