コード例 #1
0
async def libp2p_node(chain, event_bus):
    key_pair = create_new_key_pair()
    libp2p_node = Node(
        key_pair=key_pair,
        listen_ip="0.0.0.0",
        listen_port=40000,
        preferred_nodes=(),
        chain=chain,
        subnets=(),
        event_bus=event_bus,
    )
    asyncio.ensure_future(libp2p_node.run())
    await asyncio.sleep(0.01)
    return libp2p_node
コード例 #2
0
    def do_start(self) -> None:
        trinity_config = self.boot_info.trinity_config
        key_pair = self._load_or_create_node_key()
        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
        )

        # 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,
        )

        receive_server = BCCReceiveServer(
            chain=chain,
            p2p_node=libp2p_node,
            topic_msg_queues=libp2p_node.pubsub.my_topics,
            cancel_token=libp2p_node.cancel_token,
        )

        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:
            try:
                validator_index = cast(ValidatorIndex, registry_pubkeys.index(pubkey))
            except ValueError:
                self.logger.error(f'Could not find pubkey {pubkey.hex()} in genesis state')
                raise
            validator_privkeys[validator_index] = validator_keymap[pubkey]

        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=receive_server.get_ready_attestations,
        )

        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,
        )

        syncer = BeaconChainSyncer(
            chain_db=AsyncBeaconChainDB(
                base_db,
                chain_config.genesis_config,
            ),
            peer_pool=libp2p_node.handshaked_peers,
            block_importer=SyncBlockImporter(chain),
            genesis_config=chain_config.genesis_config,
            token=libp2p_node.cancel_token,

        )

        asyncio.ensure_future(exit_with_services(
            self._event_bus_service,
            libp2p_node,
            receive_server,
            slot_ticker,
            validator,
            syncer,
        ))
        asyncio.ensure_future(libp2p_node.run())
        asyncio.ensure_future(receive_server.run())
        asyncio.ensure_future(slot_ticker.run())
        asyncio.ensure_future(validator.run())
        asyncio.ensure_future(syncer.run())
コード例 #3
0
    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())
コード例 #4
0
    def do_start(self) -> None:
        trinity_config = self.boot_info.trinity_config
        beacon_app_config = trinity_config.get_app_config(BeaconAppConfig)
        db_manager = create_db_consumer_manager(
            trinity_config.database_ipc_path)
        base_db = db_manager.get_db()  # type: ignore
        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)

        if self.boot_info.args.beacon_nodekey:
            privkey = PrivateKey(
                bytes.fromhex(self.boot_info.args.beacon_nodekey))
        else:
            privkey = ecies.generate_privkey()

        # TODO: Handle `bootstrap_nodes`.
        libp2p_node = Node(
            privkey=privkey,
            listen_ip="127.0.0.1",  # FIXME: Should be configurable
            listen_port=self.boot_info.args.port,
            security_protocol_ops={
                SECURITY_PROTOCOL_ID: InsecureTransport("plaintext")
            },
            muxer_protocol_ids=(MULTIPLEXING_PROTOCOL_ID, ),
            preferred_nodes=trinity_config.preferred_nodes,
        )

        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())