예제 #1
0
 async def _post_block_proposal(self, request: web.Request) -> web.Response:
     block_data = await request.json()
     block = from_formatted_dict(block_data, BeaconBlock)
     self.logger.info("broadcasting block with root %s",
                      humanize_hash(block.hash_tree_root))
     # TODO the actual brodcast
     return web.Response()
예제 #2
0
    def from_genesis_config(cls, config_profile: str) -> 'BeaconChainConfig':
        """
        Construct an instance of ``cls`` reading the genesis configuration
        data under the local data directory.
        """
        if config_profile == "mainnet":
            beacon_chain_class = BeaconChain
        else:
            beacon_chain_class = SkeletonLakeChain

        try:
            with open(_get_eth2_genesis_config_file_path(
                    config_profile)) as config_file:
                genesis_config = json.load(config_file)
        except FileNotFoundError as e:
            raise Exception("unable to load genesis config: %s", e)

        eth2_config = Eth2Config.from_formatted_dict(
            genesis_config["eth2_config"])
        # NOTE: have to ``override_lengths`` before we can parse the ``BeaconState``
        override_lengths(eth2_config)

        genesis_state = from_formatted_dict(genesis_config["genesis_state"],
                                            BeaconState)
        genesis_validator_key_map = load_genesis_key_map(
            genesis_config["genesis_validator_key_pairs"])
        return cls(genesis_state,
                   eth2_config,
                   genesis_validator_key_map,
                   beacon_chain_class=beacon_chain_class)
예제 #3
0
 async def _post_attestation(self, request: web.Request) -> web.Response:
     attestation_data = await request.json()
     attestation = from_formatted_dict(attestation_data, Attestation)
     self.logger.info(
         "broadcasting attestation with root %s",
         humanize_hash(attestation.hash_tree_root),
     )
     # TODO the actual brodcast
     return web.Response()
예제 #4
0
async def _get_block_proposal_from_beacon_node(
        session: Session, url: str, slot: Slot,
        randao_reveal: BLSSignature) -> BeaconBlock:
    block_proposal_response = (await session.get(url,
                                                 params={
                                                     "slot":
                                                     slot,
                                                     "randao_reveal":
                                                     encode_hex(randao_reveal)
                                                 })).json()
    return from_formatted_dict(block_proposal_response, BeaconBlock)
예제 #5
0
async def _get_block_proposal_from_beacon_node(
        session: Session, url: str, slot: Slot,
        randao_reveal: BLSSignature) -> Optional[BeaconBlock]:
    block_proposal_response = await _get_json(
        session, url, {
            "slot": slot,
            "randao_reveal": encode_hex(randao_reveal)
        })
    try:
        return from_formatted_dict(block_proposal_response, BeaconBlock)
    except Exception as e:
        logger.exception(e)
        return None
예제 #6
0
파일: genesis.py 프로젝트: nondejus/trinity
def update_genesis_config_with_time(config: Dict[str, Any],
                                    genesis_time: Timestamp) -> Dict[str, Any]:
    config_profile = config["profile"]
    eth2_config = _get_eth2_config(config_profile)
    override_lengths(eth2_config)

    existing_state = from_formatted_dict(config["genesis_state"], BeaconState)

    genesis_state = existing_state.set("genesis_time", genesis_time)

    updates = {
        "genesis_state_root": encode_hex(genesis_state.hash_tree_root),
        "genesis_state": to_formatted_dict(genesis_state),
    }
    return {**config, **updates}
예제 #7
0
async def _get_attestation_from_beacon_node(
    session: Session,
    url: str,
    public_key: BLSPubkey,
    slot: Slot,
    committee_index: CommitteeIndex,
) -> Attestation:
    attestation_response = (await session.get(
        url,
        params={
            "validator_pubkey": encode_hex(public_key),
            "slot": slot,
            "committee_index": committee_index,
        },
    )).json()
    return from_formatted_dict(attestation_response, Attestation)
예제 #8
0
def main_validator() -> None:
    logger = _setup_logging()

    parser = parse_cli_args()
    arguments = parser.parse_args()
    trinity_config = load_trinity_config_from_parser_args(
        parser, arguments, APP_IDENTIFIER_VALIDATOR_CLIENT, (ValidatorClientAppConfig,)
    )

    # NOTE: we do not want the rest of the functionality in
    # ``trinity.bootstrap.ensure_data_dir_is_initialized
    create_dir_if_missing(trinity_config.data_dir)
    validator_client_app_config = trinity_config.get_app_config(
        ValidatorClientAppConfig
    )
    root_dir = validator_client_app_config.root_dir
    create_dir_if_missing(root_dir)

    try:
        genesis_config = _load_genesis_config_at(
            validator_client_app_config.genesis_config_path
        )
    except FileNotFoundError:
        genesis_time = Timestamp(int(time.time()))
        genesis_config = generate_genesis_config("minimal", genesis_time)

    eth2_config = Eth2Config.from_formatted_dict(genesis_config["eth2_config"])
    override_lengths(eth2_config)
    key_pairs = load_genesis_key_map(genesis_config["genesis_validator_key_pairs"])
    genesis_state = from_formatted_dict(genesis_config["genesis_state"], BeaconState)

    slots_per_epoch = Slot(eth2_config.SLOTS_PER_EPOCH)
    seconds_per_slot = eth2_config.SECONDS_PER_SLOT
    genesis_time = genesis_state.genesis_time

    config = Config(
        key_pairs=key_pairs,
        root_data_dir=root_dir,
        slots_per_epoch=slots_per_epoch,
        seconds_per_slot=seconds_per_slot,
        genesis_time=genesis_time,
    )
    # NOTE: we deviate from the multiprocess-driven Component-based
    # application machinery here until said machinery is more stable
    # with respect to boot, shutdown and general concurrent control.
    trio.run(arguments.func, logger, config, arguments)
예제 #9
0
    def from_genesis_config(cls) -> 'BeaconChainConfig':
        """
        Construct an instance of ``cls`` reading the genesis configuration
        data under the local data directory.
        """
        with open(_get_eth2_genesis_config_file_path()) as config_file:
            config = json.load(config_file)
            genesis_eth2_config = deserialize_eth2_config(
                config["eth2_config"])
            # NOTE: have to ``override_lengths`` before we can parse the ``BeaconState``
            override_lengths(genesis_eth2_config)

            genesis_state = from_formatted_dict(config["genesis_state"],
                                                BeaconState)
            genesis_validator_key_map = load_genesis_key_map(
                config["genesis_validator_key_pairs"])
            return cls(genesis_state, genesis_eth2_config,
                       genesis_validator_key_map)
예제 #10
0
    def from_genesis_config(cls) -> 'BeaconChainConfig':
        """
        Construct an instance of ``cls`` reading the genesis configuration
        data under the local data directory.
        """
        try:
            with open(_get_eth2_genesis_config_file_path()) as config_file:
                genesis_config = json.load(config_file)
        except FileNotFoundError:
            genesis_config = generate_genesis_config("minimal")

        eth2_config = Eth2Config.from_formatted_dict(genesis_config["eth2_config"])
        # NOTE: have to ``override_lengths`` before we can parse the ``BeaconState``
        override_lengths(eth2_config)

        genesis_state = from_formatted_dict(genesis_config["genesis_state"], BeaconState)
        genesis_validator_key_map = load_genesis_key_map(
            genesis_config["genesis_validator_key_pairs"]
        )
        return cls(genesis_state, eth2_config, genesis_validator_key_map)
예제 #11
0
async def _get_attestation_from_beacon_node(
    session: Session,
    url: str,
    public_key: BLSPubkey,
    slot: Slot,
    committee_index: CommitteeIndex,
) -> Optional[Attestation]:
    attestation_response = await _get_json(
        session,
        url,
        {
            "validator_pubkey": encode_hex(public_key),
            "slot": slot,
            "committee_index": committee_index,
        },
    )
    try:
        return from_formatted_dict(attestation_response, Attestation)
    except Exception as e:
        logger.exception(e)
        return None
예제 #12
0
async def _post_attestation(context: Context, request: Request) -> Response:
    attestation = from_formatted_dict(request, Attestation)
    return await context.broadcast_attestation(attestation)
예제 #13
0
async def _post_block_proposal(context: Context, request: Request) -> Response:
    block = from_formatted_dict(request, SignedBeaconBlock)
    return await context.broadcast_block(block)