last() method, no negative index lookups
This commit is contained in:
parent
f157745248
commit
224c98a094
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
||||
|
|
Loading…
Reference in New Issue