示例#1
0
def valid_topup():
    new_dep, state = build_deposit_for_index(10, 3)
    yield 'description', 'valid deposit to top-up existing validator'
    yield 'pre', encode(state, spec.BeaconState)
    yield 'deposit', encode(new_dep, spec.Deposit)
    spec.process_deposit(state, new_dep)
    yield 'post', encode(state, spec.BeaconState)
示例#2
0
def test_wrong_index(state):
    pre_state = deepcopy(state)
    deposit_data_leaves = [ZERO_HASH] * len(pre_state.validator_registry)

    index = len(deposit_data_leaves)
    pubkey = pubkeys[index]
    privkey = privkeys[index]
    deposit, root, deposit_data_leaves = build_deposit(
        pre_state,
        deposit_data_leaves,
        pubkey,
        privkey,
        spec.MAX_EFFECTIVE_BALANCE,
    )

    # mess up deposit_index
    deposit.index = pre_state.deposit_index + 1

    pre_state.latest_eth1_data.deposit_root = root
    pre_state.latest_eth1_data.deposit_count = len(deposit_data_leaves)

    post_state = deepcopy(pre_state)

    with pytest.raises(AssertionError):
        process_deposit(post_state, deposit)

    return pre_state, deposit, None
示例#3
0
def valid_deposit():
    new_dep, state = build_deposit_for_index(10, 10)
    yield 'description', 'valid deposit to add new validator'
    yield 'pre', encode(state, spec.BeaconState)
    yield 'deposit', encode(new_dep, spec.Deposit)
    spec.process_deposit(state, new_dep)
    yield 'post', encode(state, spec.BeaconState)
示例#4
0
def test_success_top_up(state):
    pre_state = deepcopy(state)
    deposit_data_leaves = [ZERO_HASH] * len(pre_state.validator_registry)

    validator_index = 0
    amount = spec.MAX_EFFECTIVE_BALANCE // 4
    pubkey = pubkeys[validator_index]
    privkey = privkeys[validator_index]
    deposit, root, deposit_data_leaves = build_deposit(
        pre_state,
        deposit_data_leaves,
        pubkey,
        privkey,
        amount,
    )

    pre_state.latest_eth1_data.deposit_root = root
    pre_state.latest_eth1_data.deposit_count = len(deposit_data_leaves)
    pre_balance = get_balance(pre_state, validator_index)

    post_state = deepcopy(pre_state)

    process_deposit(post_state, deposit)

    assert len(post_state.validator_registry) == len(state.validator_registry)
    assert len(post_state.balances) == len(state.balances)
    assert post_state.deposit_index == post_state.latest_eth1_data.deposit_count
    assert get_balance(post_state, validator_index) == pre_balance + amount

    return pre_state, deposit, post_state
示例#5
0
def test_success(state):
    pre_state = deepcopy(state)
    # fill previous deposits with zero-hash
    deposit_data_leaves = [ZERO_HASH] * len(pre_state.validator_registry)

    index = len(deposit_data_leaves)
    pubkey = pubkeys[index]
    privkey = privkeys[index]
    deposit, root, deposit_data_leaves = build_deposit(
        pre_state,
        deposit_data_leaves,
        pubkey,
        privkey,
        spec.MAX_EFFECTIVE_BALANCE,
    )

    pre_state.latest_eth1_data.deposit_root = root
    pre_state.latest_eth1_data.deposit_count = len(deposit_data_leaves)

    post_state = deepcopy(pre_state)

    process_deposit(post_state, deposit)

    assert len(
        post_state.validator_registry) == len(state.validator_registry) + 1
    assert len(post_state.balances) == len(state.balances) + 1
    assert post_state.validator_registry[index].pubkey == pubkeys[index]
    assert get_balance(post_state, index) == spec.MAX_EFFECTIVE_BALANCE
    assert post_state.deposit_index == post_state.latest_eth1_data.deposit_count

    return pre_state, deposit, post_state
示例#6
0
def FuzzerRunOne(input_data: bytes) -> typing.Optional[bytes]:
    test_case = translate_value(deposit_sedes.deserialize(input_data),
                                DepositTestCase)

    try:
        # modifies state in place
        spec.process_deposit(state=test_case.pre, deposit=test_case.deposit)
        return serialize(test_case.pre)
    except (AssertionError, IndexError):
        return None
def run_deposit_processing(state,
                           deposit,
                           validator_index,
                           valid=True,
                           effective=True):
    """
    Run ``process_deposit``, yielding:
      - pre-state ('pre')
      - deposit ('deposit')
      - post-state ('post').
    If ``valid == False``, run expecting ``AssertionError``
    """
    pre_validator_count = len(state.validator_registry)
    pre_balance = 0
    if validator_index < pre_validator_count:
        pre_balance = get_balance(state, validator_index)
    else:
        # if it is a new validator, it should be right at the end of the current registry.
        assert validator_index == pre_validator_count

    yield 'pre', state
    yield 'deposit', deposit

    if not valid:
        expect_assertion_error(lambda: process_deposit(state, deposit))
        yield 'post', None
        return

    process_deposit(state, deposit)

    yield 'post', state

    if not effective:
        assert len(state.validator_registry) == pre_validator_count
        assert len(state.balances) == pre_validator_count
        if validator_index < pre_validator_count:
            assert get_balance(state, validator_index) == pre_balance
    else:
        if validator_index < pre_validator_count:
            # top-up
            assert len(state.validator_registry) == pre_validator_count
            assert len(state.balances) == pre_validator_count
        else:
            # new validator
            assert len(state.validator_registry) == pre_validator_count + 1
            assert len(state.balances) == pre_validator_count + 1
        assert get_balance(
            state, validator_index) == pre_balance + deposit.data.amount

    assert state.deposit_index == state.latest_eth1_data.deposit_count
示例#8
0
def invalid_deposit_proof():
    new_dep, state = build_deposit_for_index(10, 10)
    # Make deposit proof invalid (at bottom of proof)
    new_dep.proof[-1] = spec.ZERO_HASH

    yield 'description', 'invalid deposit proof'
    yield 'pre', encode(state, spec.BeaconState)
    yield 'deposit', encode(new_dep, spec.Deposit)
    try:
        spec.process_deposit(state, new_dep)
    except AssertionError:
        # expected
        yield 'post', None
        return
    raise Exception('invalid_deposit_index has unexpectedly allowed deposit')
示例#9
0
def invalid_deposit_index():
    new_dep, state = build_deposit_for_index(10, 10)
    # Mess up deposit index, 1 too small
    state.deposit_index = 9

    yield 'description', 'invalid deposit index'
    yield 'pre', encode(state, spec.BeaconState)
    yield 'deposit', encode(new_dep, spec.Deposit)
    try:
        spec.process_deposit(state, new_dep)
    except AssertionError:
        # expected
        yield 'post', None
        return
    raise Exception('invalid_deposit_index has unexpectedly allowed deposit')