From e79b47e3c3a24d54f341d3defb691707c0acfb73 Mon Sep 17 00:00:00 2001 From: protolambda Date: Sat, 22 Jun 2019 03:54:07 +0200 Subject: [PATCH] non-existent transfer participants tests --- .../pyspec/eth2spec/test/helpers/transfers.py | 6 ++-- .../block_processing/test_process_transfer.py | 34 +++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/test_libs/pyspec/eth2spec/test/helpers/transfers.py b/test_libs/pyspec/eth2spec/test/helpers/transfers.py index acc6a35c5..fa01a3088 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/transfers.py +++ b/test_libs/pyspec/eth2spec/test/helpers/transfers.py @@ -4,13 +4,15 @@ from eth2spec.utils.bls import bls_sign from eth2spec.utils.ssz.ssz_impl import signing_root -def get_valid_transfer(spec, state, slot=None, sender_index=None, amount=None, fee=None, signed=False): +def get_valid_transfer(spec, state, slot=None, sender_index=None, + recipient_index=None, amount=None, fee=None, signed=False): if slot is None: slot = state.slot current_epoch = spec.get_current_epoch(state) if sender_index is None: sender_index = spec.get_active_validator_indices(state, current_epoch)[-1] - recipient_index = spec.get_active_validator_indices(state, current_epoch)[0] + if recipient_index is None: + recipient_index = spec.get_active_validator_indices(state, current_epoch)[0] transfer_pubkey = pubkeys[-1] transfer_privkey = privkeys[-1] diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_transfer.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_transfer.py index 45c161214..ad7e6ff60 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_transfer.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_transfer.py @@ -1,7 +1,7 @@ from eth2spec.test.context import spec_state_test, expect_assertion_error, always_bls, with_all_phases from eth2spec.test.helpers.state import next_epoch from eth2spec.test.helpers.block import apply_empty_block -from eth2spec.test.helpers.transfers import get_valid_transfer +from eth2spec.test.helpers.transfers import get_valid_transfer, sign_transfer def run_transfer_processing(spec, state, transfer, valid=True): @@ -13,11 +13,6 @@ def run_transfer_processing(spec, state, transfer, valid=True): If ``valid == False``, run expecting ``AssertionError`` """ - proposer_index = spec.get_beacon_proposer_index(state) - pre_transfer_sender_balance = state.balances[transfer.sender] - pre_transfer_recipient_balance = state.balances[transfer.recipient] - pre_transfer_proposer_balance = state.balances[proposer_index] - yield 'pre', state yield 'transfer', transfer @@ -26,6 +21,11 @@ def run_transfer_processing(spec, state, transfer, valid=True): yield 'post', None return + proposer_index = spec.get_beacon_proposer_index(state) + pre_transfer_sender_balance = state.balances[transfer.sender] + pre_transfer_recipient_balance = state.balances[transfer.recipient] + pre_transfer_proposer_balance = state.balances[proposer_index] + spec.process_transfer(state, transfer) yield 'post', state @@ -306,6 +306,28 @@ def test_no_dust_recipient(spec, state): yield from run_transfer_processing(spec, state, transfer, False) +@with_all_phases +@spec_state_test +def test_non_existent_sender(spec, state): + sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1] + transfer = get_valid_transfer(spec, state, sender_index=sender_index, amount=1, fee=0) + transfer.sender = len(state.validators) + sign_transfer(spec, state, transfer, 42) # mostly valid signature, but sender won't exist, use bogus key. + + yield from run_transfer_processing(spec, state, transfer, False) + + +@with_all_phases +@spec_state_test +def test_non_existent_recipient(spec, state): + sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1] + state.balances[sender_index] = spec.MAX_EFFECTIVE_BALANCE + 1 + transfer = get_valid_transfer(spec, state, sender_index=sender_index, + recipient_index=len(state.validators), amount=1, fee=0, signed=True) + + yield from run_transfer_processing(spec, state, transfer, False) + + @with_all_phases @spec_state_test def test_invalid_pubkey(spec, state):