2019-09-27 04:02:16 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
|
2019-07-29 13:47:35 +00:00
|
|
|
from eth2spec.test.helpers.keys import privkeys
|
|
|
|
from eth2spec.utils.bls import (
|
|
|
|
bls_sign,
|
|
|
|
only_with_bls,
|
|
|
|
)
|
|
|
|
from eth2spec.utils.ssz.ssz_impl import (
|
|
|
|
signing_root,
|
|
|
|
)
|
|
|
|
|
2019-08-01 06:19:08 +00:00
|
|
|
from .attestations import (
|
|
|
|
sign_shard_attestation,
|
|
|
|
)
|
|
|
|
|
2019-07-29 13:47:35 +00:00
|
|
|
|
|
|
|
@only_with_bls()
|
2019-09-30 03:58:05 +00:00
|
|
|
def sign_shard_block(spec, beacon_state, shard_state, block, proposer_index=None):
|
2019-07-29 13:47:35 +00:00
|
|
|
if proposer_index is None:
|
2019-09-30 03:58:05 +00:00
|
|
|
proposer_index = spec.get_shard_proposer_index(beacon_state, shard_state.shard, block.slot)
|
2019-07-29 13:47:35 +00:00
|
|
|
|
|
|
|
privkey = privkeys[proposer_index]
|
|
|
|
|
2019-09-27 04:02:16 +00:00
|
|
|
block.signature = bls_sign(
|
2019-07-29 13:47:35 +00:00
|
|
|
message_hash=signing_root(block),
|
|
|
|
privkey=privkey,
|
|
|
|
domain=spec.get_domain(
|
2019-09-27 04:02:16 +00:00
|
|
|
beacon_state,
|
2019-07-29 13:47:35 +00:00
|
|
|
spec.DOMAIN_SHARD_PROPOSER,
|
2019-09-27 04:02:16 +00:00
|
|
|
spec.compute_epoch_of_shard_slot(block.slot),
|
2019-07-29 13:47:35 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-08-01 06:19:08 +00:00
|
|
|
def build_empty_shard_block(spec,
|
|
|
|
beacon_state,
|
2019-09-27 04:02:16 +00:00
|
|
|
shard_state,
|
2019-08-01 06:19:08 +00:00
|
|
|
slot,
|
|
|
|
signed=False,
|
|
|
|
full_attestation=False):
|
2019-07-29 13:47:35 +00:00
|
|
|
if slot is None:
|
2019-08-01 06:19:08 +00:00
|
|
|
slot = shard_state.slot
|
|
|
|
|
2019-10-04 22:22:20 +00:00
|
|
|
previous_beacon_header = deepcopy(beacon_state.latest_block_header)
|
|
|
|
if previous_beacon_header.state_root == spec.Bytes32():
|
|
|
|
previous_beacon_header.state_root = beacon_state.hash_tree_root()
|
|
|
|
beacon_block_root = spec.signing_root(previous_beacon_header)
|
2019-09-27 04:02:16 +00:00
|
|
|
|
|
|
|
previous_block_header = deepcopy(shard_state.latest_block_header)
|
2019-10-04 22:22:20 +00:00
|
|
|
if previous_block_header.state_root == spec.Bytes32():
|
2019-09-27 04:02:16 +00:00
|
|
|
previous_block_header.state_root = shard_state.hash_tree_root()
|
|
|
|
parent_root = signing_root(previous_block_header)
|
|
|
|
|
2019-07-29 13:47:35 +00:00
|
|
|
block = spec.ShardBlock(
|
2019-09-27 04:02:16 +00:00
|
|
|
shard=shard_state.shard,
|
|
|
|
slot=slot,
|
|
|
|
beacon_block_root=beacon_block_root,
|
|
|
|
parent_root=parent_root,
|
|
|
|
block_size_sum=shard_state.block_size_sum + spec.SHARD_HEADER_SIZE,
|
2019-07-29 13:47:35 +00:00
|
|
|
)
|
|
|
|
|
2019-08-01 06:19:08 +00:00
|
|
|
if full_attestation:
|
2019-09-27 04:02:16 +00:00
|
|
|
shard_committee = spec.get_shard_committee(beacon_state, shard_state.shard, block.slot)
|
|
|
|
block.aggregation_bits = list(
|
|
|
|
(True,) * len(shard_committee) +
|
|
|
|
(False,) * (spec.MAX_PERIOD_COMMITTEE_SIZE * 2 - len(shard_committee))
|
2019-08-01 06:19:08 +00:00
|
|
|
)
|
2019-10-04 21:56:54 +00:00
|
|
|
else:
|
|
|
|
shard_committee = []
|
|
|
|
|
|
|
|
block.attestations = sign_shard_attestation(
|
|
|
|
spec,
|
|
|
|
beacon_state,
|
|
|
|
shard_state,
|
|
|
|
block,
|
|
|
|
participants=shard_committee,
|
|
|
|
)
|
2019-08-01 06:19:08 +00:00
|
|
|
|
2019-07-29 13:47:35 +00:00
|
|
|
if signed:
|
2019-09-30 03:58:05 +00:00
|
|
|
sign_shard_block(spec, beacon_state, shard_state, block)
|
2019-07-29 13:47:35 +00:00
|
|
|
|
|
|
|
return block
|