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 _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 _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)
def test_get_genesis_beacon_state(validator_count, pubkeys, genesis_epoch, genesis_slot, shard_count, slots_per_historical_root, epochs_per_slashed_balances_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( deposit_count=len(genesis_deposits), deposit_root=deposit_root, block_hash=ZERO_HASH32, ) genesis_time = 10 state = get_genesis_beacon_state( genesis_deposits=genesis_deposits, genesis_time=genesis_time, genesis_eth1_data=genesis_eth1_data, config=config, ) # Versioning assert state.slot == genesis_slot assert state.genesis_time == genesis_time assert state.fork == Fork() # History assert state.latest_block_header == BeaconBlockHeader( body_root=BeaconBlockBody().root, ) assert len(state.block_roots) == slots_per_historical_root assert state.block_roots == (ZERO_HASH32, ) * slots_per_historical_root assert len(state.state_roots) == slots_per_historical_root assert 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 state.start_shard == 0 assert len(state.randao_mixes) == epochs_per_historical_vector assert state.randao_mixes == (ZERO_HASH32, ) * epochs_per_historical_vector assert len(state.active_index_roots) == epochs_per_historical_vector # Slashings assert len(state.slashed_balances) == epochs_per_slashed_balances_vector assert state.slashed_balances == ( Gwei(0), ) * epochs_per_slashed_balances_vector # Attestations assert len(state.previous_epoch_attestations) == 0 assert len(state.current_epoch_attestations) == 0 # Crosslinks assert len(state.current_crosslinks) == shard_count assert state.current_crosslinks == (Crosslink(), ) * shard_count assert len(state.previous_crosslinks) == shard_count assert state.previous_crosslinks == (Crosslink(), ) * shard_count # Justification assert state.previous_justified_epoch == genesis_epoch assert state.previous_justified_root == ZERO_HASH32 assert state.current_justified_epoch == genesis_epoch assert state.current_justified_root == ZERO_HASH32 assert state.justification_bitfield == 0 # Finalization assert state.finalized_epoch == genesis_epoch assert state.finalized_root == ZERO_HASH32 for i in range(len(genesis_deposits)): assert state.validators[i].is_active(genesis_epoch)