def generate_genesis_config( config_profile: Literal["minimal", "mainnet"], genesis_time: Optional[Timestamp] = None, ) -> Dict[str, Any]: eth2_config = _get_eth2_config(config_profile) override_lengths(eth2_config) validator_count = eth2_config.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT validator_key_pairs = create_key_pairs_for(validator_count) deposits = create_genesis_deposits_from( validator_key_pairs, withdrawal_credentials_provider=mk_withdrawal_credentials_from( eth2_config.BLS_WITHDRAWAL_PREFIX.to_bytes(1, byteorder="little") ), amount_provider=lambda _public_key: eth2_config.MAX_EFFECTIVE_BALANCE, ) eth1_block_hash = ZERO_HASH32 eth1_timestamp = eth2_config.MIN_GENESIS_TIME initial_state = initialize_beacon_state_from_eth1( eth1_block_hash=eth1_block_hash, eth1_timestamp=Timestamp(eth1_timestamp), deposits=deposits, config=eth2_config, ) if genesis_time: initial_state.set("genesis_time", genesis_time) return { "eth2_config": eth2_config.to_formatted_dict(), "genesis_validator_key_pairs": mk_genesis_key_map( validator_key_pairs, initial_state ), "genesis_state": to_formatted_dict(initial_state), }
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 genesis_config_with_validators( config_profile: Literal["minimal", "mainnet"], genesis_time: Timestamp = None) -> Dict[str, Any]: eth2_config = _get_eth2_config(config_profile) override_lengths(eth2_config) validator_count = eth2_config.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT validator_key_pairs = create_key_pairs_for(validator_count) deposits = create_genesis_deposits_from( validator_key_pairs, withdrawal_credentials_provider=mk_withdrawal_credentials_from( eth2_config.BLS_WITHDRAWAL_PREFIX), amount_provider=lambda _public_key: eth2_config.MAX_EFFECTIVE_BALANCE, ) eth1_block_hash = ZERO_HASH32 eth1_timestamp = eth2_config.MIN_GENESIS_TIME genesis_state = initialize_beacon_state_from_eth1( eth1_block_hash=eth1_block_hash, eth1_timestamp=Timestamp(eth1_timestamp), deposits=deposits, config=eth2_config, ) if genesis_time: genesis_state = genesis_state.set("genesis_time", genesis_time) key_pairs = mk_genesis_key_map(validator_key_pairs, genesis_state) return _create_genesis_config(config_profile, eth2_config, genesis_state, key_pairs)
def _main(): config_type = Minimal config = _load_config(config_type) override_lengths(config) key_set = load_yaml_at(KEY_DIR / KEY_SET_FILE) pubkeys, privkeys, withdrawal_credentials = create_keypair_and_mock_withdraw_credentials( config, key_set) keymap = {pubkey: privkey for pubkey, privkey in zip(pubkeys, privkeys)} deposits, _ = create_mock_deposits_and_root(pubkeys, keymap, config, withdrawal_credentials) eth1_block_hash = b"\x42" * 32 # NOTE: this timestamp is a placeholder eth1_timestamp = 10000 state = initialize_beacon_state_from_eth1( eth1_block_hash=eth1_block_hash, eth1_timestamp=eth1_timestamp, deposits=deposits, config=config, ) genesis_time = int(time.time()) print(f"creating genesis at time {genesis_time}") genesis_state = state.copy(genesis_time=genesis_time) print(genesis_state.hash_tree_root.hex()) genesis_file_path = RESOURCE_DIR / GENESIS_FILE output_file = open(genesis_file_path, "wb") output_file.write(ssz.encode(genesis_state)) output_file.close() print(f"genesis is saved in {genesis_file_path}")
def test_genesis_initialization_fixture(config, test_case): result_state = initialize_beacon_state_from_eth1( eth1_block_hash=test_case.eth1_block_hash, eth1_timestamp=test_case.eth1_timestamp, deposits=test_case.deposits, config=config, ) validate_state(test_case.state, result_state)
def run_with(_cls, inputs: Tuple[Hash32, Timestamp, Tuple[Deposit, ...]], config: Eth2Config) -> BeaconState: eth1_block_hash, eth1_timestamp, deposits = inputs return initialize_beacon_state_from_eth1( eth1_block_hash=eth1_block_hash, eth1_timestamp=eth1_timestamp, deposits=deposits, config=config, )
def _main(): config_type = Minimal config = _load_config(config_type) override_lengths(config) key_set = load_yaml_at(KEY_DIR / KEY_SET_FILE) pubkeys = () privkeys = () withdrawal_credentials = () keymap = {} for key_pair in key_set: pubkey = decode_hex(key_pair["pubkey"]) privkey = int.from_bytes(decode_hex(key_pair["privkey"]), "big") withdrawal_credential = ( config.BLS_WITHDRAWAL_PREFIX.to_bytes(1, byteorder="big") + hash_eth2(pubkey)[1:]) pubkeys += (pubkey, ) privkeys += (privkey, ) withdrawal_credentials += (withdrawal_credential, ) keymap[pubkey] = privkey deposits, _ = create_mock_deposits_and_root(pubkeys, keymap, config, withdrawal_credentials) eth1_block_hash = b"\x42" * 32 # NOTE: this timestamp is a placeholder eth1_timestamp = 10000 state = initialize_beacon_state_from_eth1( eth1_block_hash=eth1_block_hash, eth1_timestamp=eth1_timestamp, deposits=deposits, config=config, ) genesis_time = int(time.time()) print(f"creating genesis at time {genesis_time}") genesis_state = state.copy(genesis_time=genesis_time) print(genesis_state.hash_tree_root.hex()) genesis_file_path = RESOURCE_DIR / GENESIS_FILE output_file = open(genesis_file_path, "wb") output_file.write(ssz.encode(genesis_state)) output_file.close() print(f"genesis is saved in {genesis_file_path}")
def _generate_network_as_json(cls, args: Namespace, trinity_config: TrinityConfig) -> None: config = _get_eth2_config(args.config_profile) validator_count = config.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT output_file_path = (args.output if args.output else _get_network_config_path_from()) cls.logger.info( "generating a configuration file at '%s'" " for %d validators and the genesis state containing them...", output_file_path, validator_count, ) validator_key_pairs = create_key_pairs_for(validator_count) deposits = create_genesis_deposits_from( validator_key_pairs, withdrawal_credentials_provider=mk_withdrawal_credentials_from( config.BLS_WITHDRAWAL_PREFIX.to_bytes(1, byteorder="little")), amount_provider=lambda _public_key: config.MAX_EFFECTIVE_BALANCE, ) eth1_block_hash = ZERO_HASH32 eth1_timestamp = config.MIN_GENESIS_TIME genesis_state = initialize_beacon_state_from_eth1( eth1_block_hash=eth1_block_hash, eth1_timestamp=Timestamp(eth1_timestamp), deposits=deposits, config=config, ) output = { "eth2_config": serialize(config), "genesis_validator_key_pairs": mk_genesis_key_map(validator_key_pairs, genesis_state), "genesis_state": to_formatted_dict(genesis_state), } cls.logger.info( "configuration generated; genesis state has root %s", humanize_hash(genesis_state.hash_tree_root), ) output_file_path.parent.mkdir(parents=True, exist_ok=True) with open(output_file_path, "w") as output_file: output_file.write(json.dumps(output))
def create_mock_genesis_state_from_validators( genesis_time: Timestamp, genesis_eth1_data: Eth1Data, genesis_validators: Sequence[Validator], genesis_balances: Sequence[Gwei], config: Eth2Config, ) -> BeaconState: """ Produce a valid genesis state without creating the corresponding deposits. Compare with ``eth2.beacon.genesis.get_genesis_beacon_state``. """ # NOTE: does not handle nondistinct pubkeys at the moment _check_distinct_pubkeys(genesis_validators) _check_no_missing_balances(genesis_validators, genesis_balances) _check_sufficient_balance(genesis_balances, config.MAX_EFFECTIVE_BALANCE) _check_activated_validators(genesis_validators, config.GENESIS_EPOCH) _check_correct_eth1_data(genesis_eth1_data, genesis_validators) empty_state = initialize_beacon_state_from_eth1( eth1_block_hash=genesis_eth1_data.block_hash, eth1_timestamp=genesis_time, deposits=tuple(), config=config, ) eth1_data = empty_state.eth1_data.set( "deposit_count", empty_state.eth1_data.deposit_count + len(genesis_validators) ) state_with_validators = empty_state.mset( "eth1_deposit_index", empty_state.eth1_deposit_index + len(genesis_validators), "eth1_data", eth1_data, "validators", genesis_validators, "balances", genesis_balances, ) return state_with_validators
def _main(): config_type = Minimal config = _load_config(config_type) override_lengths(config) key_set = load_yaml_at(ROOT_DIR / KEY_SET_FILE) pubkeys = () privkeys = () withdrawal_credentials = () keymap = {} for key_pair in key_set: pubkey = key_pair["pubkey"].to_bytes(48, byteorder="big") privkey = key_pair["privkey"] withdrawal_credential = ( config.BLS_WITHDRAWAL_PREFIX.to_bytes(1, byteorder="big") + hash_eth2(pubkey)[1:]) pubkeys += (pubkey, ) privkeys += (privkey, ) withdrawal_credentials += (withdrawal_credential, ) keymap[pubkey] = privkey deposits, _ = create_mock_deposits_and_root(pubkeys, keymap, config, withdrawal_credentials) eth1_block_hash = b"\x42" * 32 # NOTE: this timestamp is a placeholder eth1_timestamp = 10000 state = initialize_beacon_state_from_eth1( eth1_block_hash=eth1_block_hash, eth1_timestamp=eth1_timestamp, deposits=deposits, config=config, ) genesis_time = int(time.time()) print(f"creating genesis at time {genesis_time}") genesis_state = state.copy(genesis_time=genesis_time) print(genesis_state.hash_tree_root.hex())
def test_get_genesis_beacon_state( validator_count, pubkeys, genesis_epoch, genesis_slot, max_committees_per_slot, slots_per_historical_root, epochs_per_slashings_vector, epochs_per_historical_vector, config, keymap, ): genesis_deposits, deposit_root = create_mock_deposits_and_root( pubkeys=pubkeys[:validator_count], keymap=keymap, config=config) genesis_eth1_data = Eth1Data.create( deposit_count=len(genesis_deposits), deposit_root=deposit_root, block_hash=ZERO_HASH32, ) eth1_timestamp = 10 eth1_block_hash = genesis_eth1_data.block_hash state = initialize_beacon_state_from_eth1( eth1_block_hash=eth1_block_hash, eth1_timestamp=eth1_timestamp, deposits=genesis_deposits, config=config, ) # Versioning assert state.slot == genesis_slot assert state.genesis_time == _genesis_time_from_eth1_timestamp( eth1_timestamp) assert state.fork == Fork.create() # History assert state.latest_block_header == BeaconBlockHeader.create( body_root=BeaconBlockBody.create().hash_tree_root) assert len(state.block_roots) == slots_per_historical_root assert tuple( state.block_roots) == (ZERO_HASH32, ) * slots_per_historical_root assert len(state.state_roots) == slots_per_historical_root assert tuple( state.block_roots) == (ZERO_HASH32, ) * slots_per_historical_root assert len(state.historical_roots) == 0 # Ethereum 1.0 chain data assert state.eth1_data == genesis_eth1_data assert len(state.eth1_data_votes) == 0 assert state.eth1_deposit_index == len(genesis_deposits) # Validator registry assert len(state.validators) == validator_count assert len(state.balances) == validator_count # Shuffling assert len(state.randao_mixes) == epochs_per_historical_vector assert (tuple(state.randao_mixes) == (eth1_block_hash, ) * epochs_per_historical_vector) # Slashings assert len(state.slashings) == epochs_per_slashings_vector assert tuple(state.slashings) == (Gwei(0), ) * epochs_per_slashings_vector # Attestations assert len(state.previous_epoch_attestations) == 0 assert len(state.current_epoch_attestations) == 0 # Justification assert state.previous_justified_checkpoint.epoch == genesis_epoch assert state.previous_justified_checkpoint.root == ZERO_HASH32 assert state.current_justified_checkpoint.epoch == genesis_epoch assert state.current_justified_checkpoint.root == ZERO_HASH32 assert state.justification_bits == (False, ) * JUSTIFICATION_BITS_LENGTH # Finalization assert state.finalized_checkpoint.epoch == genesis_epoch assert state.finalized_checkpoint.root == ZERO_HASH32 for i in range(len(genesis_deposits)): assert state.validators[i].is_active(genesis_epoch)