eth2.0-specs/test_libs/pyspec/eth2spec/test/helpers/transfers.py

54 lines
1.7 KiB
Python
Raw Normal View History

2019-05-15 16:36:32 +00:00
from eth2spec.test.helpers.keys import pubkeys, privkeys
from eth2spec.test.helpers.state import get_balance
from eth2spec.utils.bls import bls_sign
2019-05-31 23:51:09 +00:00
from eth2spec.utils.ssz.ssz_impl import signing_root
2019-05-15 16:36:32 +00:00
def get_valid_transfer(spec, state, slot=None, sender_index=None,
recipient_index=None, amount=None, fee=None, signed=False):
2019-05-15 16:36:32 +00:00
if slot is None:
slot = state.slot
2019-05-30 20:57:18 +00:00
current_epoch = spec.get_current_epoch(state)
2019-05-15 16:36:32 +00:00
if sender_index is None:
2019-05-30 20:57:18 +00:00
sender_index = spec.get_active_validator_indices(state, current_epoch)[-1]
if recipient_index is None:
recipient_index = spec.get_active_validator_indices(state, current_epoch)[0]
2019-05-15 16:36:32 +00:00
transfer_pubkey = pubkeys[-1]
transfer_privkey = privkeys[-1]
if fee is None:
fee = get_balance(state, sender_index) // 32
if amount is None:
amount = get_balance(state, sender_index) - fee
2019-05-30 20:57:18 +00:00
transfer = spec.Transfer(
2019-05-15 16:36:32 +00:00
sender=sender_index,
recipient=recipient_index,
amount=amount,
fee=fee,
slot=slot,
pubkey=transfer_pubkey,
)
2019-05-15 17:31:02 +00:00
if signed:
2019-05-30 20:57:18 +00:00
sign_transfer(spec, state, transfer, transfer_privkey)
2019-05-15 17:31:02 +00:00
# ensure withdrawal_credentials reproducible
2019-06-09 19:41:21 +00:00
state.validators[transfer.sender].withdrawal_credentials = (
spec.BLS_WITHDRAWAL_PREFIX + spec.hash(transfer.pubkey)[1:]
2019-05-15 17:31:02 +00:00
)
return transfer
2019-05-30 20:57:18 +00:00
def sign_transfer(spec, state, transfer, privkey):
2019-05-15 16:36:32 +00:00
transfer.signature = bls_sign(
message_hash=signing_root(transfer),
2019-05-15 17:31:02 +00:00
privkey=privkey,
2019-05-30 20:57:18 +00:00
domain=spec.get_domain(
2019-05-15 16:36:32 +00:00
state=state,
domain_type=spec.DOMAIN_TRANSFER,
2019-05-30 20:57:18 +00:00
message_epoch=spec.get_current_epoch(state),
2019-05-15 16:36:32 +00:00
)
)
return transfer