From 224c98a094a5d537fe05377955d8f30868cb6ba7 Mon Sep 17 00:00:00 2001 From: protolambda Date: Thu, 20 Jun 2019 20:55:17 +0200 Subject: [PATCH] last() method, no negative index lookups --- test_libs/pyspec/eth2spec/test/helpers/custody.py | 2 +- .../eth2spec/test/helpers/proposer_slashings.py | 2 +- .../pyspec/eth2spec/test/helpers/transfers.py | 2 +- .../block_processing/test_process_transfer.py | 14 +++++++------- .../test_process_voluntary_exit.py | 2 +- .../pyspec/eth2spec/test/sanity/test_blocks.py | 6 +++--- test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py | 4 ++++ 7 files changed, 18 insertions(+), 14 deletions(-) diff --git a/test_libs/pyspec/eth2spec/test/helpers/custody.py b/test_libs/pyspec/eth2spec/test/helpers/custody.py index b49a6be1f..681add457 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/custody.py +++ b/test_libs/pyspec/eth2spec/test/helpers/custody.py @@ -4,7 +4,7 @@ from eth2spec.utils.bls import bls_sign def get_valid_early_derived_secret_reveal(spec, state, epoch=None): current_epoch = spec.get_current_epoch(state) - revealed_index = spec.get_active_validator_indices(state, current_epoch)[-1] + revealed_index = spec.get_active_validator_indices(state, current_epoch).last() masker_index = spec.get_active_validator_indices(state, current_epoch)[0] if epoch is None: diff --git a/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py b/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py index d5b7f7b7f..7b6f71374 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py +++ b/test_libs/pyspec/eth2spec/test/helpers/proposer_slashings.py @@ -6,7 +6,7 @@ from eth2spec.test.helpers.keys import pubkey_to_privkey def get_valid_proposer_slashing(spec, state, signed_1=False, signed_2=False): current_epoch = spec.get_current_epoch(state) - validator_index = spec.get_active_validator_indices(state, current_epoch)[-1] + validator_index = spec.get_active_validator_indices(state, current_epoch).last() privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] slot = state.slot diff --git a/test_libs/pyspec/eth2spec/test/helpers/transfers.py b/test_libs/pyspec/eth2spec/test/helpers/transfers.py index acc6a35c5..215cd6fcb 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/transfers.py +++ b/test_libs/pyspec/eth2spec/test/helpers/transfers.py @@ -9,7 +9,7 @@ def get_valid_transfer(spec, state, slot=None, sender_index=None, amount=None, f 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] + sender_index = spec.get_active_validator_indices(state, current_epoch).last() 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 e9d282b3a..2bed94dc8 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 @@ -63,7 +63,7 @@ def test_success_withdrawable(spec, state): @with_all_phases @spec_state_test def test_success_active_above_max_effective(spec, state): - sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1] + sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state)).last() state.balances[sender_index] = spec.MAX_EFFECTIVE_BALANCE + 1 transfer = get_valid_transfer(spec, state, sender_index=sender_index, amount=1, fee=0, signed=True) @@ -73,7 +73,7 @@ def test_success_active_above_max_effective(spec, state): @with_all_phases @spec_state_test def test_success_active_above_max_effective_fee(spec, state): - sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1] + sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state)).last() state.balances[sender_index] = spec.MAX_EFFECTIVE_BALANCE + 1 transfer = get_valid_transfer(spec, state, sender_index=sender_index, amount=0, fee=1, signed=True) @@ -94,7 +94,7 @@ def test_invalid_signature(spec, state): @with_all_phases @spec_state_test def test_active_but_transfer_past_effective_balance(spec, state): - sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1] + sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state)).last() amount = spec.MAX_EFFECTIVE_BALANCE // 32 state.balances[sender_index] = spec.MAX_EFFECTIVE_BALANCE transfer = get_valid_transfer(spec, state, sender_index=sender_index, amount=amount, fee=0, signed=True) @@ -115,7 +115,7 @@ def test_incorrect_slot(spec, state): @with_all_phases @spec_state_test def test_insufficient_balance_for_fee(spec, state): - sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1] + sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state)).last() state.balances[sender_index] = spec.MAX_EFFECTIVE_BALANCE transfer = get_valid_transfer(spec, state, sender_index=sender_index, amount=0, fee=1, signed=True) @@ -128,7 +128,7 @@ def test_insufficient_balance_for_fee(spec, state): @with_all_phases @spec_state_test def test_insufficient_balance(spec, state): - sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1] + sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state)).last() state.balances[sender_index] = spec.MAX_EFFECTIVE_BALANCE transfer = get_valid_transfer(spec, state, sender_index=sender_index, amount=1, fee=0, signed=True) @@ -141,7 +141,7 @@ def test_insufficient_balance(spec, state): @with_all_phases @spec_state_test def test_no_dust_sender(spec, state): - sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1] + sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state)).last() balance = state.balances[sender_index] transfer = get_valid_transfer( spec, @@ -161,7 +161,7 @@ def test_no_dust_sender(spec, state): @with_all_phases @spec_state_test def test_no_dust_recipient(spec, state): - sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1] + sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state)).last() state.balances[sender_index] = spec.MAX_EFFECTIVE_BALANCE + 1 transfer = get_valid_transfer(spec, state, sender_index=sender_index, amount=1, fee=0, signed=True) state.balances[transfer.recipient] = 0 diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_voluntary_exit.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_voluntary_exit.py index 33cacc4e2..b9f7d6098 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_voluntary_exit.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_voluntary_exit.py @@ -93,7 +93,7 @@ def test_success_exit_queue(spec, state): continue # exit an additional validator - validator_index = spec.get_active_validator_indices(state, current_epoch)[-1] + validator_index = spec.get_active_validator_indices(state, current_epoch).last() privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] voluntary_exit = build_voluntary_exit( spec, diff --git a/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py b/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py index f8590005e..c86394a0b 100644 --- a/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py +++ b/test_libs/pyspec/eth2spec/test/sanity/test_blocks.py @@ -269,7 +269,7 @@ def test_voluntary_exit(spec, state): validator_index = spec.get_active_validator_indices( state, spec.get_current_epoch(state) - )[-1] + ).last() # move state forward PERSISTENT_COMMITTEE_PERIOD epochs to allow for exit state.slot += spec.PERSISTENT_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH @@ -315,7 +315,7 @@ def test_voluntary_exit(spec, state): # overwrite default 0 to test # spec.MAX_TRANSFERS = 1 - # sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1] + # sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state)).last() # amount = get_balance(state, sender_index) # transfer = get_valid_transfer(spec, state, state.slot + 1, sender_index, amount, signed=True) @@ -347,7 +347,7 @@ def test_voluntary_exit(spec, state): @spec_state_test def test_balance_driven_status_transitions(spec, state): current_epoch = spec.get_current_epoch(state) - validator_index = spec.get_active_validator_indices(state, current_epoch)[-1] + validator_index = spec.get_active_validator_indices(state, current_epoch).last() assert state.validators[validator_index].exit_epoch == spec.FAR_FUTURE_EPOCH diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py index bd7fe0950..e5f9f66a8 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py @@ -312,6 +312,10 @@ class BaseList(list, Elements): def __iter__(self) -> Iterator[SSZValue]: return super().__iter__() + def last(self): + # be explict about getting the last item, for the non-python readers, and negative-index safety + return self[len(self)-1] + class List(BaseList):