2019-03-19 23:10:36 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
import build.phase0.spec as spec
|
|
|
|
|
|
|
|
from build.phase0.spec import (
|
2019-03-21 15:37:06 +00:00
|
|
|
get_balance,
|
2019-03-25 17:25:33 +00:00
|
|
|
ZERO_HASH,
|
2019-03-19 23:10:36 +00:00
|
|
|
process_deposit,
|
|
|
|
)
|
|
|
|
from tests.phase0.helpers import (
|
|
|
|
build_deposit,
|
2019-03-25 17:25:33 +00:00
|
|
|
privkeys,
|
|
|
|
pubkeys,
|
2019-03-19 23:10:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# mark entire file as 'voluntary_exits'
|
|
|
|
pytestmark = pytest.mark.voluntary_exits
|
|
|
|
|
|
|
|
|
2019-03-25 17:25:33 +00:00
|
|
|
def test_success(state):
|
2019-03-19 23:10:36 +00:00
|
|
|
pre_state = deepcopy(state)
|
2019-03-25 17:25:33 +00:00
|
|
|
# fill previous deposits with zero-hash
|
|
|
|
deposit_data_leaves = [ZERO_HASH] * len(pre_state.validator_registry)
|
2019-03-19 23:10:36 +00:00
|
|
|
|
|
|
|
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_DEPOSIT_AMOUNT,
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2019-03-21 15:37:06 +00:00
|
|
|
assert len(post_state.balances) == len(state.balances) + 1
|
2019-03-19 23:10:36 +00:00
|
|
|
assert post_state.validator_registry[index].pubkey == pubkeys[index]
|
2019-03-21 15:37:06 +00:00
|
|
|
assert get_balance(post_state, index) == spec.MAX_DEPOSIT_AMOUNT
|
2019-03-19 23:10:36 +00:00
|
|
|
assert post_state.deposit_index == post_state.latest_eth1_data.deposit_count
|
|
|
|
|
|
|
|
return pre_state, deposit, post_state
|
|
|
|
|
|
|
|
|
2019-03-25 17:25:33 +00:00
|
|
|
def test_success_top_up(state):
|
2019-03-19 23:10:36 +00:00
|
|
|
pre_state = deepcopy(state)
|
2019-03-25 17:25:33 +00:00
|
|
|
deposit_data_leaves = [ZERO_HASH] * len(pre_state.validator_registry)
|
2019-03-19 23:10:36 +00:00
|
|
|
|
|
|
|
validator_index = 0
|
|
|
|
amount = spec.MAX_DEPOSIT_AMOUNT // 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)
|
2019-03-21 15:37:06 +00:00
|
|
|
pre_balance = get_balance(pre_state, validator_index)
|
2019-03-19 23:10:36 +00:00
|
|
|
|
|
|
|
post_state = deepcopy(pre_state)
|
|
|
|
|
|
|
|
process_deposit(post_state, deposit)
|
|
|
|
|
|
|
|
assert len(post_state.validator_registry) == len(state.validator_registry)
|
2019-03-21 15:37:06 +00:00
|
|
|
assert len(post_state.balances) == len(state.balances)
|
2019-03-19 23:10:36 +00:00
|
|
|
assert post_state.deposit_index == post_state.latest_eth1_data.deposit_count
|
2019-03-21 15:37:06 +00:00
|
|
|
assert get_balance(post_state, validator_index) == pre_balance + amount
|
2019-03-19 23:10:36 +00:00
|
|
|
|
|
|
|
return pre_state, deposit, post_state
|
|
|
|
|
|
|
|
|
2019-03-25 17:25:33 +00:00
|
|
|
def test_wrong_index(state):
|
2019-03-19 23:10:36 +00:00
|
|
|
pre_state = deepcopy(state)
|
2019-03-25 17:25:33 +00:00
|
|
|
deposit_data_leaves = [ZERO_HASH] * len(pre_state.validator_registry)
|
2019-03-19 23:10:36 +00:00
|
|
|
|
|
|
|
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_DEPOSIT_AMOUNT,
|
|
|
|
)
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
2019-03-25 17:25:33 +00:00
|
|
|
def test_bad_merkle_proof(state):
|
2019-03-19 23:10:36 +00:00
|
|
|
pre_state = deepcopy(state)
|
2019-03-25 17:25:33 +00:00
|
|
|
deposit_data_leaves = [ZERO_HASH] * len(pre_state.validator_registry)
|
2019-03-19 23:10:36 +00:00
|
|
|
|
|
|
|
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_DEPOSIT_AMOUNT,
|
|
|
|
)
|
|
|
|
|
|
|
|
# mess up merkle branch
|
|
|
|
deposit.proof[-1] = spec.ZERO_HASH
|
|
|
|
|
|
|
|
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
|