last() method, no negative index lookups

This commit is contained in:
protolambda 2019-06-20 20:55:17 +02:00
parent f157745248
commit 224c98a094
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
7 changed files with 18 additions and 14 deletions

View File

@ -4,7 +4,7 @@ from eth2spec.utils.bls import bls_sign
def get_valid_early_derived_secret_reveal(spec, state, epoch=None): def get_valid_early_derived_secret_reveal(spec, state, epoch=None):
current_epoch = spec.get_current_epoch(state) 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] masker_index = spec.get_active_validator_indices(state, current_epoch)[0]
if epoch is None: if epoch is None:

View File

@ -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): def get_valid_proposer_slashing(spec, state, signed_1=False, signed_2=False):
current_epoch = spec.get_current_epoch(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()
privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] privkey = pubkey_to_privkey[state.validators[validator_index].pubkey]
slot = state.slot slot = state.slot

View File

@ -9,7 +9,7 @@ def get_valid_transfer(spec, state, slot=None, sender_index=None, amount=None, f
slot = state.slot slot = state.slot
current_epoch = spec.get_current_epoch(state) current_epoch = spec.get_current_epoch(state)
if sender_index is None: 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] recipient_index = spec.get_active_validator_indices(state, current_epoch)[0]
transfer_pubkey = pubkeys[-1] transfer_pubkey = pubkeys[-1]
transfer_privkey = privkeys[-1] transfer_privkey = privkeys[-1]

View File

@ -63,7 +63,7 @@ def test_success_withdrawable(spec, state):
@with_all_phases @with_all_phases
@spec_state_test @spec_state_test
def test_success_active_above_max_effective(spec, state): 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 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) 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 @with_all_phases
@spec_state_test @spec_state_test
def test_success_active_above_max_effective_fee(spec, state): 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 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) 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 @with_all_phases
@spec_state_test @spec_state_test
def test_active_but_transfer_past_effective_balance(spec, state): 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 amount = spec.MAX_EFFECTIVE_BALANCE // 32
state.balances[sender_index] = spec.MAX_EFFECTIVE_BALANCE state.balances[sender_index] = spec.MAX_EFFECTIVE_BALANCE
transfer = get_valid_transfer(spec, state, sender_index=sender_index, amount=amount, fee=0, signed=True) 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 @with_all_phases
@spec_state_test @spec_state_test
def test_insufficient_balance_for_fee(spec, state): 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 state.balances[sender_index] = spec.MAX_EFFECTIVE_BALANCE
transfer = get_valid_transfer(spec, state, sender_index=sender_index, amount=0, fee=1, signed=True) 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 @with_all_phases
@spec_state_test @spec_state_test
def test_insufficient_balance(spec, state): 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 state.balances[sender_index] = spec.MAX_EFFECTIVE_BALANCE
transfer = get_valid_transfer(spec, state, sender_index=sender_index, amount=1, fee=0, signed=True) 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 @with_all_phases
@spec_state_test @spec_state_test
def test_no_dust_sender(spec, state): 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] balance = state.balances[sender_index]
transfer = get_valid_transfer( transfer = get_valid_transfer(
spec, spec,
@ -161,7 +161,7 @@ def test_no_dust_sender(spec, state):
@with_all_phases @with_all_phases
@spec_state_test @spec_state_test
def test_no_dust_recipient(spec, state): 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 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) transfer = get_valid_transfer(spec, state, sender_index=sender_index, amount=1, fee=0, signed=True)
state.balances[transfer.recipient] = 0 state.balances[transfer.recipient] = 0

View File

@ -93,7 +93,7 @@ def test_success_exit_queue(spec, state):
continue continue
# exit an additional validator # 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] privkey = pubkey_to_privkey[state.validators[validator_index].pubkey]
voluntary_exit = build_voluntary_exit( voluntary_exit = build_voluntary_exit(
spec, spec,

View File

@ -269,7 +269,7 @@ def test_voluntary_exit(spec, state):
validator_index = spec.get_active_validator_indices( validator_index = spec.get_active_validator_indices(
state, state,
spec.get_current_epoch(state) spec.get_current_epoch(state)
)[-1] ).last()
# move state forward PERSISTENT_COMMITTEE_PERIOD epochs to allow for exit # move state forward PERSISTENT_COMMITTEE_PERIOD epochs to allow for exit
state.slot += spec.PERSISTENT_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH 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 # overwrite default 0 to test
# spec.MAX_TRANSFERS = 1 # 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) # amount = get_balance(state, sender_index)
# transfer = get_valid_transfer(spec, state, state.slot + 1, sender_index, amount, signed=True) # 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 @spec_state_test
def test_balance_driven_status_transitions(spec, state): def test_balance_driven_status_transitions(spec, state):
current_epoch = spec.get_current_epoch(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 assert state.validators[validator_index].exit_epoch == spec.FAR_FUTURE_EPOCH

View File

@ -312,6 +312,10 @@ class BaseList(list, Elements):
def __iter__(self) -> Iterator[SSZValue]: def __iter__(self) -> Iterator[SSZValue]:
return super().__iter__() 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): class List(BaseList):