2019-03-18 22:20:24 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
|
|
|
|
from py_ecc import bls
|
|
|
|
|
2019-04-03 03:18:17 +00:00
|
|
|
import eth2spec.phase0.spec as spec
|
2019-04-10 12:34:42 +00:00
|
|
|
from eth2spec.utils.minimal_ssz import signing_root
|
2019-04-03 03:18:17 +00:00
|
|
|
from eth2spec.phase0.spec import (
|
2019-03-19 03:46:32 +00:00
|
|
|
# constants
|
2019-03-21 23:08:54 +00:00
|
|
|
ZERO_HASH,
|
2019-05-05 11:10:39 +00:00
|
|
|
MAX_EPOCHS_PER_CROSSLINK,
|
2019-03-19 03:46:32 +00:00
|
|
|
# SSZ
|
2019-03-26 17:27:07 +00:00
|
|
|
Attestation,
|
2019-03-18 22:20:24 +00:00
|
|
|
AttestationData,
|
2019-03-26 17:27:07 +00:00
|
|
|
AttestationDataAndCustodyBit,
|
2019-04-02 02:50:06 +00:00
|
|
|
AttesterSlashing,
|
2019-04-19 08:26:54 +00:00
|
|
|
BeaconBlock,
|
2019-03-21 23:08:54 +00:00
|
|
|
BeaconBlockHeader,
|
2019-05-05 11:10:39 +00:00
|
|
|
Crosslink,
|
2019-03-18 22:20:24 +00:00
|
|
|
Deposit,
|
|
|
|
DepositData,
|
|
|
|
Eth1Data,
|
2019-03-21 23:08:54 +00:00
|
|
|
ProposerSlashing,
|
2019-04-17 18:31:00 +00:00
|
|
|
Transfer,
|
2019-03-19 21:49:01 +00:00
|
|
|
VoluntaryExit,
|
2019-03-19 03:46:32 +00:00
|
|
|
# functions
|
2019-04-02 02:50:06 +00:00
|
|
|
convert_to_indexed,
|
2019-05-21 17:30:38 +00:00
|
|
|
bls_domain,
|
2019-03-21 23:08:54 +00:00
|
|
|
get_active_validator_indices,
|
2019-04-18 04:43:24 +00:00
|
|
|
get_attesting_indices,
|
2019-03-18 22:20:24 +00:00
|
|
|
get_block_root,
|
2019-04-24 05:17:25 +00:00
|
|
|
get_block_root_at_slot,
|
2019-04-30 18:55:14 +00:00
|
|
|
get_crosslink_committee,
|
2019-03-18 22:20:24 +00:00
|
|
|
get_current_epoch,
|
|
|
|
get_domain,
|
|
|
|
get_epoch_start_slot,
|
|
|
|
get_genesis_beacon_state,
|
2019-04-12 13:17:57 +00:00
|
|
|
get_previous_epoch,
|
2019-04-14 09:46:21 +00:00
|
|
|
get_shard_delta,
|
2019-04-07 07:55:38 +00:00
|
|
|
hash_tree_root,
|
2019-03-27 14:31:56 +00:00
|
|
|
slot_to_epoch,
|
2019-05-01 12:14:10 +00:00
|
|
|
state_transition,
|
2019-03-18 22:20:24 +00:00
|
|
|
verify_merkle_branch,
|
|
|
|
hash,
|
|
|
|
)
|
2019-04-03 03:18:17 +00:00
|
|
|
from eth2spec.utils.merkle_minimal import (
|
2019-03-18 22:20:24 +00:00
|
|
|
calc_merkle_tree_from_leaves,
|
|
|
|
get_merkle_proof,
|
|
|
|
get_merkle_root,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-04-24 18:31:27 +00:00
|
|
|
privkeys = [i + 1 for i in range(1024)]
|
2019-03-25 17:25:33 +00:00
|
|
|
pubkeys = [bls.privtopub(privkey) for privkey in privkeys]
|
|
|
|
pubkey_to_privkey = {pubkey: privkey for privkey, pubkey in zip(privkeys, pubkeys)}
|
2019-03-18 22:20:24 +00:00
|
|
|
|
|
|
|
|
2019-05-01 12:14:10 +00:00
|
|
|
def advance_slot(state) -> None:
|
|
|
|
state.slot += 1
|
|
|
|
|
|
|
|
|
2019-04-20 05:17:33 +00:00
|
|
|
def get_balance(state, index):
|
|
|
|
return state.balances[index]
|
2019-03-18 22:20:24 +00:00
|
|
|
|
2019-04-24 05:17:25 +00:00
|
|
|
|
2019-04-12 13:17:57 +00:00
|
|
|
def set_bitfield_bit(bitfield, i):
|
|
|
|
"""
|
|
|
|
Set the bit in ``bitfield`` at position ``i`` to ``1``.
|
|
|
|
"""
|
|
|
|
byte_index = i // 8
|
|
|
|
bit_index = i % 8
|
|
|
|
return (
|
|
|
|
bitfield[:byte_index] +
|
|
|
|
bytes([bitfield[byte_index] | (1 << bit_index)]) +
|
|
|
|
bitfield[byte_index+1:]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-03-25 17:25:33 +00:00
|
|
|
def create_mock_genesis_validator_deposits(num_validators, deposit_data_leaves=None):
|
|
|
|
if not deposit_data_leaves:
|
|
|
|
deposit_data_leaves = []
|
2019-04-03 00:22:14 +00:00
|
|
|
signature = b'\x33' * 96
|
2019-03-18 22:20:24 +00:00
|
|
|
|
|
|
|
deposit_data_list = []
|
|
|
|
for i in range(num_validators):
|
2019-03-25 17:25:33 +00:00
|
|
|
pubkey = pubkeys[i]
|
2019-03-18 22:20:24 +00:00
|
|
|
deposit_data = DepositData(
|
2019-03-26 13:38:51 +00:00
|
|
|
pubkey=pubkey,
|
|
|
|
# insecurely use pubkey as withdrawal key as well
|
|
|
|
withdrawal_credentials=spec.BLS_WITHDRAWAL_PREFIX_BYTE + hash(pubkey)[1:],
|
2019-04-16 06:16:13 +00:00
|
|
|
amount=spec.MAX_EFFECTIVE_BALANCE,
|
2019-04-03 00:22:14 +00:00
|
|
|
signature=signature,
|
2019-03-18 22:20:24 +00:00
|
|
|
)
|
2019-04-14 22:29:08 +00:00
|
|
|
item = deposit_data.hash_tree_root()
|
2019-03-18 22:20:24 +00:00
|
|
|
deposit_data_leaves.append(item)
|
|
|
|
tree = calc_merkle_tree_from_leaves(tuple(deposit_data_leaves))
|
|
|
|
root = get_merkle_root((tuple(deposit_data_leaves)))
|
|
|
|
proof = list(get_merkle_proof(tree, item_index=i))
|
|
|
|
assert verify_merkle_branch(item, proof, spec.DEPOSIT_CONTRACT_TREE_DEPTH, i, root)
|
|
|
|
deposit_data_list.append(deposit_data)
|
|
|
|
|
|
|
|
genesis_validator_deposits = []
|
|
|
|
for i in range(num_validators):
|
|
|
|
genesis_validator_deposits.append(Deposit(
|
|
|
|
proof=list(get_merkle_proof(tree, item_index=i)),
|
|
|
|
index=i,
|
2019-03-26 13:38:51 +00:00
|
|
|
data=deposit_data_list[i]
|
2019-03-18 22:20:24 +00:00
|
|
|
))
|
|
|
|
return genesis_validator_deposits, root
|
|
|
|
|
|
|
|
|
2019-03-25 17:25:33 +00:00
|
|
|
def create_genesis_state(num_validators, deposit_data_leaves=None):
|
2019-03-19 23:10:36 +00:00
|
|
|
initial_deposits, deposit_root = create_mock_genesis_validator_deposits(
|
|
|
|
num_validators,
|
|
|
|
deposit_data_leaves,
|
|
|
|
)
|
2019-03-18 22:20:24 +00:00
|
|
|
return get_genesis_beacon_state(
|
|
|
|
initial_deposits,
|
|
|
|
genesis_time=0,
|
|
|
|
genesis_eth1_data=Eth1Data(
|
|
|
|
deposit_root=deposit_root,
|
2019-03-19 23:10:36 +00:00
|
|
|
deposit_count=len(initial_deposits),
|
2019-03-18 22:20:24 +00:00
|
|
|
block_hash=spec.ZERO_HASH,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2019-03-19 03:39:19 +00:00
|
|
|
|
2019-03-18 22:20:24 +00:00
|
|
|
def build_empty_block_for_next_slot(state):
|
2019-04-19 08:26:54 +00:00
|
|
|
empty_block = BeaconBlock()
|
2019-03-18 22:20:24 +00:00
|
|
|
empty_block.slot = state.slot + 1
|
2019-04-22 07:46:13 +00:00
|
|
|
empty_block.body.eth1_data.deposit_count = state.deposit_index
|
2019-03-18 22:20:24 +00:00
|
|
|
previous_block_header = deepcopy(state.latest_block_header)
|
|
|
|
if previous_block_header.state_root == spec.ZERO_HASH:
|
|
|
|
previous_block_header.state_root = state.hash_tree_root()
|
2019-05-06 19:49:46 +00:00
|
|
|
empty_block.parent_root = signing_root(previous_block_header)
|
2019-03-18 22:20:24 +00:00
|
|
|
return empty_block
|
|
|
|
|
|
|
|
|
|
|
|
def build_deposit_data(state, pubkey, privkey, amount):
|
2019-03-26 13:38:51 +00:00
|
|
|
deposit_data = DepositData(
|
2019-03-18 22:20:24 +00:00
|
|
|
pubkey=pubkey,
|
2019-03-19 15:56:04 +00:00
|
|
|
# insecurely use pubkey as withdrawal key as well
|
|
|
|
withdrawal_credentials=spec.BLS_WITHDRAWAL_PREFIX_BYTE + hash(pubkey)[1:],
|
2019-03-26 13:38:51 +00:00
|
|
|
amount=amount,
|
2019-03-18 22:20:24 +00:00
|
|
|
)
|
2019-04-03 00:22:14 +00:00
|
|
|
signature = bls.sign(
|
2019-04-08 01:51:13 +00:00
|
|
|
message_hash=signing_root(deposit_data),
|
2019-03-18 22:20:24 +00:00
|
|
|
privkey=privkey,
|
2019-05-21 17:30:38 +00:00
|
|
|
domain=bls_domain(
|
2019-03-18 22:20:24 +00:00
|
|
|
spec.DOMAIN_DEPOSIT,
|
|
|
|
)
|
|
|
|
)
|
2019-04-03 00:22:14 +00:00
|
|
|
deposit_data.signature = signature
|
2019-03-18 22:20:24 +00:00
|
|
|
return deposit_data
|
|
|
|
|
|
|
|
|
|
|
|
def build_attestation_data(state, slot, shard):
|
|
|
|
assert state.slot >= slot
|
|
|
|
|
2019-04-12 13:17:57 +00:00
|
|
|
if slot == state.slot:
|
2019-05-06 19:49:46 +00:00
|
|
|
block_root = build_empty_block_for_next_slot(state).parent_root
|
2019-04-12 13:17:57 +00:00
|
|
|
else:
|
2019-04-24 05:17:25 +00:00
|
|
|
block_root = get_block_root_at_slot(state, slot)
|
2019-03-18 22:20:24 +00:00
|
|
|
|
2019-04-12 13:17:57 +00:00
|
|
|
current_epoch_start_slot = get_epoch_start_slot(get_current_epoch(state))
|
|
|
|
if slot < current_epoch_start_slot:
|
2019-04-24 05:17:25 +00:00
|
|
|
epoch_boundary_root = get_block_root(state, get_previous_epoch(state))
|
2019-04-12 13:17:57 +00:00
|
|
|
elif slot == current_epoch_start_slot:
|
2019-03-18 22:20:24 +00:00
|
|
|
epoch_boundary_root = block_root
|
|
|
|
else:
|
2019-04-24 05:17:25 +00:00
|
|
|
epoch_boundary_root = get_block_root(state, get_current_epoch(state))
|
2019-03-18 22:20:24 +00:00
|
|
|
|
2019-04-12 13:17:57 +00:00
|
|
|
if slot < current_epoch_start_slot:
|
|
|
|
justified_epoch = state.previous_justified_epoch
|
2019-03-18 22:20:24 +00:00
|
|
|
justified_block_root = state.previous_justified_root
|
|
|
|
else:
|
2019-04-12 13:17:57 +00:00
|
|
|
justified_epoch = state.current_justified_epoch
|
2019-03-18 22:20:24 +00:00
|
|
|
justified_block_root = state.current_justified_root
|
|
|
|
|
2019-04-02 05:00:36 +00:00
|
|
|
crosslinks = state.current_crosslinks if slot_to_epoch(slot) == get_current_epoch(state) else state.previous_crosslinks
|
2019-05-19 21:47:59 +00:00
|
|
|
parent_crosslink = crosslinks[shard]
|
2019-03-18 22:20:24 +00:00
|
|
|
return AttestationData(
|
|
|
|
beacon_block_root=block_root,
|
2019-04-12 13:17:57 +00:00
|
|
|
source_epoch=justified_epoch,
|
2019-03-18 22:20:24 +00:00
|
|
|
source_root=justified_block_root,
|
2019-04-30 18:55:14 +00:00
|
|
|
target_epoch=slot_to_epoch(slot),
|
2019-03-18 22:20:24 +00:00
|
|
|
target_root=epoch_boundary_root,
|
2019-05-05 11:10:39 +00:00
|
|
|
crosslink=Crosslink(
|
|
|
|
shard=shard,
|
2019-05-19 21:47:59 +00:00
|
|
|
start_epoch=parent_crosslink.end_epoch,
|
|
|
|
end_epoch=min(slot_to_epoch(slot), parent_crosslink.end_epoch + MAX_EPOCHS_PER_CROSSLINK),
|
2019-05-06 17:26:14 +00:00
|
|
|
data_root=spec.ZERO_HASH,
|
2019-05-19 21:47:59 +00:00
|
|
|
parent_root=hash_tree_root(parent_crosslink),
|
2019-05-05 11:10:39 +00:00
|
|
|
),
|
2019-03-18 22:20:24 +00:00
|
|
|
)
|
2019-03-19 21:49:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def build_voluntary_exit(state, epoch, validator_index, privkey):
|
|
|
|
voluntary_exit = VoluntaryExit(
|
|
|
|
epoch=epoch,
|
|
|
|
validator_index=validator_index,
|
|
|
|
)
|
|
|
|
voluntary_exit.signature = bls.sign(
|
2019-04-08 01:51:13 +00:00
|
|
|
message_hash=signing_root(voluntary_exit),
|
2019-03-19 21:49:01 +00:00
|
|
|
privkey=privkey,
|
|
|
|
domain=get_domain(
|
2019-04-03 00:22:14 +00:00
|
|
|
state=state,
|
2019-03-19 21:49:01 +00:00
|
|
|
domain_type=spec.DOMAIN_VOLUNTARY_EXIT,
|
2019-04-03 00:22:14 +00:00
|
|
|
message_epoch=epoch,
|
2019-03-19 21:49:01 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return voluntary_exit
|
2019-03-19 23:10:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def build_deposit(state,
|
|
|
|
deposit_data_leaves,
|
|
|
|
pubkey,
|
|
|
|
privkey,
|
|
|
|
amount):
|
|
|
|
deposit_data = build_deposit_data(state, pubkey, privkey, amount)
|
|
|
|
|
2019-04-14 22:29:08 +00:00
|
|
|
item = deposit_data.hash_tree_root()
|
2019-03-19 23:10:36 +00:00
|
|
|
index = len(deposit_data_leaves)
|
|
|
|
deposit_data_leaves.append(item)
|
|
|
|
tree = calc_merkle_tree_from_leaves(tuple(deposit_data_leaves))
|
|
|
|
root = get_merkle_root((tuple(deposit_data_leaves)))
|
|
|
|
proof = list(get_merkle_proof(tree, item_index=index))
|
|
|
|
assert verify_merkle_branch(item, proof, spec.DEPOSIT_CONTRACT_TREE_DEPTH, index, root)
|
|
|
|
|
|
|
|
deposit = Deposit(
|
|
|
|
proof=list(proof),
|
|
|
|
index=index,
|
2019-03-26 13:38:51 +00:00
|
|
|
data=deposit_data,
|
2019-03-19 23:10:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return deposit, root, deposit_data_leaves
|
2019-03-21 23:08:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_valid_proposer_slashing(state):
|
|
|
|
current_epoch = get_current_epoch(state)
|
2019-04-14 07:52:50 +00:00
|
|
|
validator_index = get_active_validator_indices(state, current_epoch)[-1]
|
2019-03-21 23:08:54 +00:00
|
|
|
privkey = pubkey_to_privkey[state.validator_registry[validator_index].pubkey]
|
|
|
|
slot = state.slot
|
|
|
|
|
|
|
|
header_1 = BeaconBlockHeader(
|
|
|
|
slot=slot,
|
2019-05-06 19:49:46 +00:00
|
|
|
parent_root=ZERO_HASH,
|
2019-03-21 23:08:54 +00:00
|
|
|
state_root=ZERO_HASH,
|
2019-05-06 19:49:46 +00:00
|
|
|
body_root=ZERO_HASH,
|
2019-03-21 23:08:54 +00:00
|
|
|
)
|
|
|
|
header_2 = deepcopy(header_1)
|
2019-05-06 19:49:46 +00:00
|
|
|
header_2.parent_root = b'\x02' * 32
|
2019-03-21 23:08:54 +00:00
|
|
|
header_2.slot = slot + 1
|
|
|
|
|
|
|
|
domain = get_domain(
|
2019-04-03 00:22:14 +00:00
|
|
|
state=state,
|
|
|
|
domain_type=spec.DOMAIN_BEACON_PROPOSER,
|
2019-03-21 23:08:54 +00:00
|
|
|
)
|
|
|
|
header_1.signature = bls.sign(
|
2019-04-08 01:51:13 +00:00
|
|
|
message_hash=signing_root(header_1),
|
2019-03-21 23:08:54 +00:00
|
|
|
privkey=privkey,
|
|
|
|
domain=domain,
|
|
|
|
)
|
|
|
|
header_2.signature = bls.sign(
|
2019-04-08 01:51:13 +00:00
|
|
|
message_hash=signing_root(header_2),
|
2019-03-21 23:08:54 +00:00
|
|
|
privkey=privkey,
|
|
|
|
domain=domain,
|
|
|
|
)
|
|
|
|
|
|
|
|
return ProposerSlashing(
|
|
|
|
proposer_index=validator_index,
|
|
|
|
header_1=header_1,
|
|
|
|
header_2=header_2,
|
|
|
|
)
|
2019-03-26 17:27:07 +00:00
|
|
|
|
|
|
|
|
2019-04-02 02:50:06 +00:00
|
|
|
def get_valid_attester_slashing(state):
|
|
|
|
attestation_1 = get_valid_attestation(state)
|
|
|
|
attestation_2 = deepcopy(attestation_1)
|
2019-04-06 15:09:07 +00:00
|
|
|
attestation_2.data.target_root = b'\x01' * 32
|
2019-04-02 02:50:06 +00:00
|
|
|
|
|
|
|
return AttesterSlashing(
|
|
|
|
attestation_1=convert_to_indexed(state, attestation_1),
|
|
|
|
attestation_2=convert_to_indexed(state, attestation_2),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-03-26 17:27:07 +00:00
|
|
|
def get_valid_attestation(state, slot=None):
|
|
|
|
if slot is None:
|
|
|
|
slot = state.slot
|
2019-04-14 09:46:21 +00:00
|
|
|
|
|
|
|
if slot_to_epoch(slot) == get_current_epoch(state):
|
|
|
|
shard = (state.latest_start_shard + slot) % spec.SLOTS_PER_EPOCH
|
|
|
|
else:
|
|
|
|
previous_shard_delta = get_shard_delta(state, get_previous_epoch(state))
|
|
|
|
shard = (state.latest_start_shard - previous_shard_delta + slot) % spec.SHARD_COUNT
|
|
|
|
|
2019-03-26 17:27:07 +00:00
|
|
|
attestation_data = build_attestation_data(state, slot, shard)
|
|
|
|
|
2019-05-05 11:10:39 +00:00
|
|
|
crosslink_committee = get_crosslink_committee(state, attestation_data.target_epoch, attestation_data.crosslink.shard)
|
2019-03-26 17:27:07 +00:00
|
|
|
|
|
|
|
committee_size = len(crosslink_committee)
|
|
|
|
bitfield_length = (committee_size + 7) // 8
|
2019-03-27 14:31:56 +00:00
|
|
|
aggregation_bitfield = b'\xC0' + b'\x00' * (bitfield_length - 1)
|
2019-03-26 17:27:07 +00:00
|
|
|
custody_bitfield = b'\x00' * bitfield_length
|
|
|
|
attestation = Attestation(
|
|
|
|
aggregation_bitfield=aggregation_bitfield,
|
|
|
|
data=attestation_data,
|
|
|
|
custody_bitfield=custody_bitfield,
|
|
|
|
)
|
2019-04-18 04:43:24 +00:00
|
|
|
participants = get_attesting_indices(
|
2019-03-26 17:27:07 +00:00
|
|
|
state,
|
|
|
|
attestation.data,
|
|
|
|
attestation.aggregation_bitfield,
|
|
|
|
)
|
2019-03-27 14:31:56 +00:00
|
|
|
assert len(participants) == 2
|
|
|
|
|
|
|
|
signatures = []
|
|
|
|
for validator_index in participants:
|
|
|
|
privkey = privkeys[validator_index]
|
|
|
|
signatures.append(
|
|
|
|
get_attestation_signature(
|
|
|
|
state,
|
|
|
|
attestation.data,
|
|
|
|
privkey
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
attestation.aggregation_signature = bls.aggregate_signatures(signatures)
|
|
|
|
return attestation
|
2019-03-26 17:27:07 +00:00
|
|
|
|
|
|
|
|
2019-04-17 18:31:00 +00:00
|
|
|
def get_valid_transfer(state, slot=None, sender_index=None, amount=None, fee=None):
|
|
|
|
if slot is None:
|
|
|
|
slot = state.slot
|
|
|
|
current_epoch = get_current_epoch(state)
|
|
|
|
if sender_index is None:
|
|
|
|
sender_index = get_active_validator_indices(state, current_epoch)[-1]
|
|
|
|
recipient_index = get_active_validator_indices(state, current_epoch)[0]
|
|
|
|
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
|
|
|
|
|
|
|
|
transfer = Transfer(
|
|
|
|
sender=sender_index,
|
|
|
|
recipient=recipient_index,
|
|
|
|
amount=amount,
|
|
|
|
fee=fee,
|
|
|
|
slot=slot,
|
|
|
|
pubkey=transfer_pubkey,
|
2019-04-23 11:39:19 +00:00
|
|
|
signature=ZERO_HASH,
|
2019-04-17 18:31:00 +00:00
|
|
|
)
|
|
|
|
transfer.signature = bls.sign(
|
|
|
|
message_hash=signing_root(transfer),
|
|
|
|
privkey=transfer_privkey,
|
|
|
|
domain=get_domain(
|
2019-04-19 06:50:48 +00:00
|
|
|
state=state,
|
2019-04-17 18:31:00 +00:00
|
|
|
domain_type=spec.DOMAIN_TRANSFER,
|
2019-04-19 06:50:48 +00:00
|
|
|
message_epoch=get_current_epoch(state),
|
2019-04-17 18:31:00 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# ensure withdrawal_credentials reproducable
|
|
|
|
state.validator_registry[transfer.sender].withdrawal_credentials = (
|
|
|
|
spec.BLS_WITHDRAWAL_PREFIX_BYTE + spec.hash(transfer.pubkey)[1:]
|
|
|
|
)
|
|
|
|
|
|
|
|
return transfer
|
|
|
|
|
|
|
|
|
2019-03-27 14:31:56 +00:00
|
|
|
def get_attestation_signature(state, attestation_data, privkey, custody_bit=0b0):
|
2019-03-26 17:27:07 +00:00
|
|
|
message_hash = AttestationDataAndCustodyBit(
|
2019-03-27 14:31:56 +00:00
|
|
|
data=attestation_data,
|
|
|
|
custody_bit=custody_bit,
|
2019-03-26 17:27:07 +00:00
|
|
|
).hash_tree_root()
|
|
|
|
|
2019-03-27 14:31:56 +00:00
|
|
|
return bls.sign(
|
2019-03-26 17:27:07 +00:00
|
|
|
message_hash=message_hash,
|
|
|
|
privkey=privkey,
|
|
|
|
domain=get_domain(
|
2019-04-03 00:22:14 +00:00
|
|
|
state=state,
|
2019-03-26 17:27:07 +00:00
|
|
|
domain_type=spec.DOMAIN_ATTESTATION,
|
2019-04-30 18:55:14 +00:00
|
|
|
message_epoch=attestation_data.target_epoch,
|
2019-03-26 17:27:07 +00:00
|
|
|
)
|
|
|
|
)
|
2019-04-17 02:32:50 +00:00
|
|
|
|
|
|
|
|
2019-04-12 13:17:57 +00:00
|
|
|
def fill_aggregate_attestation(state, attestation):
|
2019-05-05 11:10:39 +00:00
|
|
|
crosslink_committee = get_crosslink_committee(state, attestation.data.target_epoch, attestation.data.crosslink.shard)
|
2019-04-12 13:17:57 +00:00
|
|
|
for i in range(len(crosslink_committee)):
|
|
|
|
attestation.aggregation_bitfield = set_bitfield_bit(attestation.aggregation_bitfield, i)
|
|
|
|
|
|
|
|
|
2019-04-03 05:56:13 +00:00
|
|
|
def add_attestation_to_state(state, attestation, slot):
|
|
|
|
block = build_empty_block_for_next_slot(state)
|
|
|
|
block.slot = slot
|
|
|
|
block.body.attestations.append(attestation)
|
|
|
|
state_transition(state, block)
|
|
|
|
|
|
|
|
|
2019-04-17 02:32:50 +00:00
|
|
|
def next_slot(state):
|
2019-04-26 14:27:07 +00:00
|
|
|
"""
|
|
|
|
Transition to the next slot via an empty block.
|
|
|
|
Return the empty block that triggered the transition.
|
|
|
|
"""
|
2019-04-17 02:32:50 +00:00
|
|
|
block = build_empty_block_for_next_slot(state)
|
|
|
|
state_transition(state, block)
|
2019-04-26 14:27:07 +00:00
|
|
|
return block
|
2019-04-17 18:31:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def next_epoch(state):
|
2019-04-26 14:27:07 +00:00
|
|
|
"""
|
|
|
|
Transition to the start slot of the next epoch via an empty block.
|
|
|
|
Return the empty block that triggered the transition.
|
|
|
|
"""
|
2019-04-17 18:31:00 +00:00
|
|
|
block = build_empty_block_for_next_slot(state)
|
|
|
|
block.slot += spec.SLOTS_PER_EPOCH - (state.slot % spec.SLOTS_PER_EPOCH)
|
|
|
|
state_transition(state, block)
|
2019-04-26 14:27:07 +00:00
|
|
|
return block
|
2019-05-01 09:06:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_state_root(state, slot) -> bytes:
|
|
|
|
"""
|
|
|
|
Return the state root at a recent ``slot``.
|
|
|
|
"""
|
|
|
|
assert slot < state.slot <= slot + spec.SLOTS_PER_HISTORICAL_ROOT
|
|
|
|
return state.latest_state_roots[slot % spec.SLOTS_PER_HISTORICAL_ROOT]
|