def create_mock_deposits_and_root( pubkeys: Sequence[BLSPubkey], keymap: Dict[BLSPubkey, int], config: Eth2Config, withdrawal_credentials: Sequence[Hash32] = None, leaves: Sequence[Hash32] = None, ) -> Tuple[Tuple[Deposit, ...], Hash32]: """ Creates as many new deposits as there are keys in ``pubkeys``. Optionally provide corresponding ``withdrawal_credentials`` to include those. Optionally provide the prefix in the sequence of leaves leading up to the new deposits made by this function to get the correct updated root. If ``leaves`` is empty, this function simulates the genesis deposit tree calculation. """ if not withdrawal_credentials: withdrawal_credentials = tuple( Hash32(b"\x22" * 32) for _ in range(len(pubkeys))) else: assert len(withdrawal_credentials) == len(pubkeys) if not leaves: leaves = tuple() deposit_datas = tuple() # type: Tuple[DepositData, ...] deposit_data_leaves = cast(Tuple[Hash32, ...], leaves) # type: Tuple[Hash32, ...] for key, credentials in zip(pubkeys, withdrawal_credentials): privkey = keymap[key] deposit_data = create_mock_deposit_data( config=config, pubkey=key, privkey=privkey, withdrawal_credentials=credentials, ) item = deposit_data.hash_tree_root deposit_data_leaves += (item, ) deposit_datas += (deposit_data, ) deposits: Tuple[Deposit, ...] = tuple() for index, data in enumerate(deposit_datas): length_mix_in = Hash32((index + 1).to_bytes(32, byteorder="little")) tree = calc_merkle_tree_from_leaves(deposit_data_leaves[:index + 1]) deposit = Deposit( proof=(get_merkle_proof(tree, item_index=index) + (length_mix_in, )), data=data, ) deposits += (deposit, ) if len(deposit_data_leaves) > 0: tree_root = get_root(tree) return deposits, hash_eth2(tree_root + length_mix_in) else: return tuple(), ZERO_HASH32
def make_deposit_proof( list_deposit_data: Sequence[DepositData], deposit_tree: MerkleTree, deposit_tree_root: Hash32, deposit_index: int, ) -> Tuple[Hash32, ...]: length_mix_in = Hash32(len(list_deposit_data).to_bytes(32, byteorder="little")) merkle_proof = get_merkle_proof(deposit_tree, deposit_index) merkle_proof_with_mix_in = merkle_proof + (length_mix_in,) return merkle_proof_with_mix_in
def create_mock_deposits_and_root( pubkeys: Sequence[BLSPubkey], keymap: Dict[BLSPubkey, int], config: Eth2Config, withdrawal_credentials: Sequence[Hash32] = None, leaves: Sequence[Hash32] = None) -> Tuple[Tuple[Deposit, ...], Hash32]: """ Creates as many new deposits as there are keys in ``pubkeys``. Optionally provide corresponding ``withdrawal_credentials`` to include those. Optionally provide the prefix in the sequence of leaves leading up to the new deposits made by this function to get the correct updated root. If ``leaves`` is empty, this function simulates the genesis deposit tree calculation. """ if not withdrawal_credentials: withdrawal_credentials = tuple( Hash32(b'\x22' * 32) for _ in range(len(pubkeys))) else: assert len(withdrawal_credentials) == len(pubkeys) if not leaves: leaves = tuple() deposit_datas = tuple() # type: Tuple[DepositData, ...] deposit_data_leaves = cast(Tuple[Hash32, ...], leaves) # type: Tuple[Hash32, ...] for key, credentials in zip(pubkeys, withdrawal_credentials): privkey = keymap[key] deposit_data = create_mock_deposit_data( config=config, pubkey=key, privkey=privkey, withdrawal_credentials=credentials, ) item = deposit_data.root deposit_data_leaves += (item, ) deposit_datas += (deposit_data, ) tree = calc_merkle_tree_from_leaves(deposit_data_leaves) deposits = tuple( Deposit( proof=get_merkle_proof(tree, item_index=i), data=data, ) for i, data in enumerate(deposit_datas)) return deposits, get_root(tree)
def create_mock_genesis_validator_deposits_and_root( num_validators: int, config: Eth2Config, pubkeys: Sequence[BLSPubkey], keymap: Dict[BLSPubkey, int]) -> Tuple[Tuple[Deposit, ...], Hash32]: # Mock data withdrawal_credentials = Hash32(b'\x22' * 32) fork = Fork( previous_version=config.GENESIS_FORK_VERSION.to_bytes(4, 'little'), current_version=config.GENESIS_FORK_VERSION.to_bytes(4, 'little'), epoch=config.GENESIS_EPOCH, ) deposit_data_array = tuple() # type: Tuple[DepositData, ...] deposit_data_leaves = tuple() # type: Tuple[Hash32, ...] for i in range(num_validators): deposit_data = create_mock_deposit_data( config=config, pubkeys=pubkeys, keymap=keymap, validator_index=ValidatorIndex(i), withdrawal_credentials=withdrawal_credentials, fork=fork, ) item = hash_eth2(ssz.encode(deposit_data)) deposit_data_leaves += (item,) deposit_data_array += (deposit_data,) tree = calc_merkle_tree_from_leaves(deposit_data_leaves) root = get_merkle_root(deposit_data_leaves) genesis_validator_deposits = tuple( Deposit( proof=get_merkle_proof(tree, item_index=i), index=i, deposit_data=deposit_data_array[i], ) for i in range(num_validators) ) return genesis_validator_deposits, root
def create_mock_deposit(config, sample_beacon_state_params, keymap, pubkeys, withdrawal_credentials, validator_index): state = BeaconState(**sample_beacon_state_params).copy( slot=1, validator_registry=(), ) fork = Fork( previous_version=config.GENESIS_FORK_VERSION.to_bytes(4, 'little'), current_version=config.GENESIS_FORK_VERSION.to_bytes(4, 'little'), epoch=config.GENESIS_EPOCH, ) deposit_data = create_mock_deposit_data( config=config, pubkeys=pubkeys, keymap=keymap, validator_index=validator_index, withdrawal_credentials=withdrawal_credentials, fork=fork, ) item = hash_eth2(ssz.encode(deposit_data)) test_deposit_data_leaves = (item, ) tree = calc_merkle_tree_from_leaves(test_deposit_data_leaves) root = get_merkle_root(test_deposit_data_leaves) proof = list(get_merkle_proof(tree, item_index=validator_index)) state = state.copy(latest_eth1_data=state.latest_eth1_data.copy( deposit_root=root, ), ) deposit = Deposit( proof=proof, index=validator_index, deposit_data=deposit_data, ) return state, deposit
def create_deposit_proof(tree: MerkleTree, index: int, leaf_count: int) -> Tuple[Hash32, ...]: length_mix_in = Hash32(leaf_count.to_bytes(32, "little")) proof = get_merkle_proof(tree, index) return proof + (length_mix_in, )