def _load_or_create_node_key(self) -> KeyPair: if self.boot_info.args.beacon_nodekey: privkey = Secp256k1PrivateKey.new( decode_hex(self.boot_info.args.beacon_nodekey) ) key_pair = KeyPair(private_key=privkey, public_key=privkey.get_public_key()) return key_pair else: config = self.boot_info.trinity_config beacon_nodekey_path = f"{config.nodekey_path}-beacon" if os.path.isfile(beacon_nodekey_path): with open(beacon_nodekey_path, "rb") as f: key_data = f.read() private_key = Secp256k1PrivateKey.new(key_data) key_pair = KeyPair( private_key=private_key, public_key=private_key.get_public_key() ) return key_pair else: key_pair = create_new_key_pair() private_key_bytes = key_pair.private_key.to_bytes() with open(beacon_nodekey_path, "wb") as f: f.write(private_key_bytes) return key_pair
def __init__( self, name: str, node_privkey: str, port: int, rpcport: Optional[int] = None, preferred_nodes: Optional[Tuple["Node", ...]] = None, ) -> None: self.name = name self.node_privkey = Secp256k1PrivateKey.new( bytes.fromhex(node_privkey)) self.port = port if preferred_nodes is None: preferred_nodes = [] self.preferred_nodes = preferred_nodes self.rpcport = rpcport self.tasks = [] self.start_time = time.monotonic() self.logs_expected = {} self.logs_expected["stdout"] = set() self.logs_expected["stderr"] = set() # TODO: Add other logging messages in our beacon node to indicate # that the beacon node is successfully bootstrapped. # self.add_log("stderr", SERVER_RUNNING) self.has_log_happened = defaultdict(lambda: False)
def do_start(self) -> None: trinity_config = self.boot_info.trinity_config beacon_app_config = trinity_config.get_app_config(BeaconAppConfig) base_db = DBClient.connect(trinity_config.database_ipc_path) chain_config = beacon_app_config.get_chain_config() attestation_pool = AttestationPool() chain = chain_config.beacon_chain_class(base_db, attestation_pool, chain_config.genesis_config) key_pair: KeyPair if self.boot_info.args.beacon_nodekey: privkey = Secp256k1PrivateKey( bytes.fromhex(self.boot_info.args.beacon_nodekey)) key_pair = KeyPair(private_key=privkey, public_key=privkey.get_public_key()) else: key_pair = create_new_key_pair() # TODO: Handle `bootstrap_nodes`. libp2p_node = Node( key_pair=key_pair, listen_ip="127.0.0.1", # FIXME: Should be configurable listen_port=self.boot_info.args.port, preferred_nodes=trinity_config.preferred_nodes, chain=chain, ) state = chain.get_state_by_slot( chain_config.genesis_config.GENESIS_SLOT) registry_pubkeys = [v_record.pubkey for v_record in state.validators] validator_privkeys = {} validator_keymap = chain_config.genesis_data.validator_keymap for pubkey in validator_keymap: validator_index = cast(ValidatorIndex, registry_pubkeys.index(pubkey)) validator_privkeys[validator_index] = validator_keymap[pubkey] def fake_get_ready_attestations_fn() -> Tuple[Attestation, ...]: return tuple() validator = Validator( chain=chain, p2p_node=libp2p_node, validator_privkeys=validator_privkeys, event_bus=self.event_bus, token=libp2p_node.cancel_token, get_ready_attestations_fn= fake_get_ready_attestations_fn, # FIXME: BCCReceiveServer.get_ready_attestations # noqa: E501 ) slot_ticker = SlotTicker( genesis_slot=chain_config.genesis_config.GENESIS_SLOT, genesis_time=chain_config.genesis_data.genesis_time, seconds_per_slot=chain_config.genesis_config.SECONDS_PER_SLOT, event_bus=self.event_bus, token=libp2p_node.cancel_token, ) asyncio.ensure_future( exit_with_services( self._event_bus_service, libp2p_node, slot_ticker, validator, )) asyncio.ensure_future(libp2p_node.run()) asyncio.ensure_future(slot_ticker.run()) asyncio.ensure_future(validator.run())