Пример #1
0
def create_mock_genesis_validator_deposits(num_validators=100):
    deposit_timestamp = 0
    proof_of_possession = b'\x33' * 96

    deposit_data_list = []
    for i in range(num_validators):
        pubkey = pubkeys[i]
        privkey = pubkey_to_privkey[pubkey]
        deposit_data = DepositData(
            amount=spec.MAX_DEPOSIT_AMOUNT,
            timestamp=deposit_timestamp,
            deposit_input=DepositInput(
                pubkey=pubkey,
                withdrawal_credentials=privkey.to_bytes(32, byteorder='big'),
                proof_of_possession=proof_of_possession,
            ),
        )
        item = hash(deposit_data.serialize())
        all_deposit_data_leaves.append(item)
        tree = calc_merkle_tree_from_leaves(tuple(all_deposit_data_leaves))
        root = get_merkle_root((tuple(all_deposit_data_leaves)))
        proof = list(get_merkle_proof(tree, item_index=i))
        assert verify_merkle_branch(item, proof,
                                    spec.DEPOSIT_CONTRACT_TREE_DEPTH, i, root)
        deposit_data_list.append(deposit_data)

    genesis_validator_deposits = []
    for i in range(num_validators):
        genesis_validator_deposits.append(
            Deposit(proof=list(get_merkle_proof(tree, item_index=i)),
                    index=i,
                    deposit_data=deposit_data_list[i]))
    return genesis_validator_deposits, root
Пример #2
0
def test_deposit_in_block(state):
    test_state = deepcopy(state)
    test_deposit_data_leaves = deepcopy(all_deposit_data_leaves)
    withdrawal_credentials = b'\x42' * 32
    deposit_timestamp = 1
    proof_of_possession = b'\x44' * 96

    index = len(test_deposit_data_leaves)
    deposit_data = DepositData(
        amount=MAX_DEPOSIT_AMOUNT,
        timestamp=deposit_timestamp,
        deposit_input=DepositInput(
            pubkey=pubkeys[index],
            withdrawal_credentials=withdrawal_credentials,
            proof_of_possession=proof_of_possession,
        ),
    )
    item = hash(deposit_data.serialize())
    test_deposit_data_leaves.append(item)
    tree = calc_merkle_tree_from_leaves(tuple(test_deposit_data_leaves))
    root = get_merkle_root((tuple(test_deposit_data_leaves)))
    proof = list(get_merkle_proof(tree, item_index=index))
    assert verify_merkle_branch(item, proof, DEPOSIT_CONTRACT_TREE_DEPTH,
                                index, root)

    deposit = Deposit(
        proof=list(proof),
        index=index,
        deposit_data=deposit_data,
    )

    test_state.latest_eth1_data.deposit_root = root
    block = construct_empty_block_for_next_slot(test_state)
    block.body.deposits.append(deposit)

    state_transition(test_state, block)
    assert len(
        test_state.validator_registry) == len(state.validator_registry) + 1
    assert len(
        test_state.validator_balances) == len(state.validator_balances) + 1
    assert test_state.validator_registry[index].pubkey == pubkeys[index]
Пример #3
0
def test_deposit_top_up(state):
    pre_state = deepcopy(state)
    test_deposit_data_leaves = deepcopy(all_deposit_data_leaves)

    validator_index = 0
    amount = spec.MAX_DEPOSIT_AMOUNT // 4
    pubkey = pubkeys[validator_index]
    deposit_data = create_deposit_data(pre_state, pubkey, amount)

    merkle_index = len(test_deposit_data_leaves)
    item = hash(deposit_data.serialize())
    test_deposit_data_leaves.append(item)
    tree = calc_merkle_tree_from_leaves(tuple(test_deposit_data_leaves))
    root = get_merkle_root((tuple(test_deposit_data_leaves)))
    proof = list(get_merkle_proof(tree, item_index=merkle_index))
    assert verify_merkle_branch(item, proof, spec.DEPOSIT_CONTRACT_TREE_DEPTH,
                                merkle_index, root)

    deposit = Deposit(
        proof=list(proof),
        index=merkle_index,
        deposit_data=deposit_data,
    )

    pre_state.latest_eth1_data.deposit_root = root
    block = construct_empty_block_for_next_slot(pre_state)
    block.body.deposits.append(deposit)

    pre_balance = pre_state.validator_balances[validator_index]
    post_state = deepcopy(pre_state)
    state_transition(post_state, block)
    assert len(post_state.validator_registry) == len(
        pre_state.validator_registry)
    assert len(post_state.validator_balances) == len(
        pre_state.validator_balances)
    assert post_state.validator_balances[
        validator_index] == pre_balance + amount

    return pre_state, [block], post_state