2019-03-19 23:10:36 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
import pytest
|
|
|
|
|
2019-04-03 03:18:17 +00:00
|
|
|
import eth2spec.phase0.spec as spec
|
2019-03-19 23:10:36 +00:00
|
|
|
|
2019-04-03 03:18:17 +00:00
|
|
|
from eth2spec.phase0.spec import (
|
2019-03-25 17:25:33 +00:00
|
|
|
ZERO_HASH,
|
2019-03-19 23:10:36 +00:00
|
|
|
process_deposit,
|
|
|
|
)
|
2019-04-17 07:47:56 +00:00
|
|
|
from tests.helpers import (
|
2019-04-20 05:17:33 +00:00
|
|
|
get_balance,
|
2019-03-19 23:10:36 +00:00
|
|
|
build_deposit,
|
2019-03-25 17:25:33 +00:00
|
|
|
privkeys,
|
|
|
|
pubkeys,
|
2019-03-19 23:10:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-04-13 23:13:53 +00:00
|
|
|
# mark entire file as 'deposits'
|
|
|
|
pytestmark = pytest.mark.deposits
|
2019-03-19 23:10:36 +00:00
|
|
|
|
|
|
|
|
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,
|
2019-04-16 06:16:13 +00:00
|
|
|
spec.MAX_EFFECTIVE_BALANCE,
|
2019-03-19 23:10:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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-04-16 06:16:13 +00:00
|
|
|
assert get_balance(post_state, index) == spec.MAX_EFFECTIVE_BALANCE
|
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
|
2019-04-16 06:16:13 +00:00
|
|
|
amount = spec.MAX_EFFECTIVE_BALANCE // 4
|
2019-03-19 23:10:36 +00:00
|
|
|
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,
|
2019-04-16 06:16:13 +00:00
|
|
|
spec.MAX_EFFECTIVE_BALANCE,
|
2019-03-19 23:10:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# 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,
|
2019-04-16 06:16:13 +00:00
|
|
|
spec.MAX_EFFECTIVE_BALANCE,
|
2019-03-19 23:10:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# 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
|