Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
change shard_block_root to crosslink_data_root (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
ralexstokes authored and hwwhww committed Feb 27, 2019
1 parent 4a0cb07 commit cdba2e9
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 68 deletions.
14 changes: 7 additions & 7 deletions eth2/beacon/epoch_processing_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,19 @@ def get_winning_root(
committee_config: CommitteeConfig) -> Tuple[Hash32, Gwei]:
winning_root = None
winning_root_balance: Gwei = Gwei(0)
shard_block_roots = set(
crosslink_data_roots = set(
[
a.data.shard_block_root for a in attestations
a.data.crosslink_data_root for a in attestations
if a.data.shard == shard
]
)
for shard_block_root in shard_block_roots:
for crosslink_data_root in crosslink_data_roots:
attesting_validator_indices = get_attester_indices_from_attesttion(
state=state,
attestations=[
a
for a in attestations
if a.data.shard == shard and a.data.shard_block_root == shard_block_root
if a.data.shard == shard and a.data.crosslink_data_root == crosslink_data_root
],
committee_config=committee_config,
)
Expand All @@ -126,11 +126,11 @@ def get_winning_root(
max_deposit_amount,
)
if total_attesting_balance > winning_root_balance:
winning_root = shard_block_root
winning_root = crosslink_data_root
winning_root_balance = total_attesting_balance
elif total_attesting_balance == winning_root_balance and winning_root_balance > 0:
if shard_block_root < winning_root:
winning_root = shard_block_root
if crosslink_data_root < winning_root:
winning_root = crosslink_data_root

if winning_root is None:
raise NoWinningRootError
Expand Down
2 changes: 1 addition & 1 deletion eth2/beacon/on_genesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def get_genesis_beacon_state(*,

# Recent state
latest_crosslinks=(
(CrosslinkRecord(epoch=genesis_epoch, shard_block_root=ZERO_HASH32),) * shard_count
(CrosslinkRecord(epoch=genesis_epoch, crosslink_data_root=ZERO_HASH32),) * shard_count
),
latest_block_roots=(ZERO_HASH32,) * latest_block_roots_length,
latest_active_index_roots=(ZERO_HASH32,) * latest_active_index_roots_length,
Expand Down
28 changes: 14 additions & 14 deletions eth2/beacon/state_machines/forks/serenity/block_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ def validate_attestation(state: BeaconState,

validate_attestation_latest_crosslink_root(
attestation.data,
latest_crosslink_root=state.latest_crosslinks[attestation.data.shard].shard_block_root,
latest_crosslink_root=state.latest_crosslinks[attestation.data.shard].crosslink_data_root,
)

validate_attestation_shard_block_root(attestation.data)
validate_attestation_crosslink_data_root(attestation.data)

validate_attestation_aggregate_signature(
state,
Expand Down Expand Up @@ -360,41 +360,41 @@ def validate_attestation_justified_block_root(attestation_data: AttestationData,
def validate_attestation_latest_crosslink_root(attestation_data: AttestationData,
latest_crosslink_root: Hash32) -> None:
"""
Validate that either the attestation ``latest_crosslink_root`` or ``shard_block_root``
Validate that either the attestation ``latest_crosslink_root`` or ``crosslink_data_root``
field of ``attestation_data`` is the provided ``latest_crosslink_root``.
Raise ``ValidationError`` if it's invalid.
"""
acceptable_shard_block_roots = {
acceptable_crosslink_data_roots = {
attestation_data.latest_crosslink_root,
attestation_data.shard_block_root,
attestation_data.crosslink_data_root,
}
if latest_crosslink_root not in acceptable_shard_block_roots:
if latest_crosslink_root not in acceptable_crosslink_data_roots:
raise ValidationError(
"Neither the attestation ``latest_crosslink_root`` nor the attestation "
"``shard_block_root`` are equal to the ``latest_crosslink_root``.\n"
"``crosslink_data_root`` are equal to the ``latest_crosslink_root``.\n"
"\tFound: %s and %s, Expected %s" %
(
attestation_data.latest_crosslink_root,
attestation_data.shard_block_root,
attestation_data.crosslink_data_root,
latest_crosslink_root,
)
)


def validate_attestation_shard_block_root(attestation_data: AttestationData) -> None:
def validate_attestation_crosslink_data_root(attestation_data: AttestationData) -> None:
"""
Validate ``shard_block_root`` field of `attestation_data`.
Validate ``crosslink_data_root`` field of `attestation_data`.
Raise ``ValidationError`` if it's invalid.
Note: This is the Phase 0 version of ``shard_block_root`` validation.
Note: This is the Phase 0 version of ``crosslink_data_root`` validation.
This is a built-in stub and will be changed in phase 1.
"""
if attestation_data.shard_block_root != ZERO_HASH32:
if attestation_data.crosslink_data_root != ZERO_HASH32:
raise ValidationError(
"Attestation ``shard_block_root`` is not ZERO_HASH32.\n"
"Attestation ``crosslink_data_root`` is not ZERO_HASH32.\n"
"\tFound: %s, Expected %s" %
(
attestation_data.shard_block_root,
attestation_data.crosslink_data_root,
ZERO_HASH32,
)
)
Expand Down
4 changes: 2 additions & 2 deletions eth2/beacon/state_machines/forks/serenity/epoch_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def process_crosslinks(state: BeaconState, config: BeaconConfig) -> BeaconState:
shard,
CrosslinkRecord(
epoch=state.current_epoch(config.SLOTS_PER_EPOCH),
shard_block_root=winning_root,
crosslink_data_root=winning_root,
),
)
else:
Expand Down Expand Up @@ -663,7 +663,7 @@ def _process_rewards_and_penalties_for_crosslinks(
attestations=(
a
for a in filtered_attestations
if a.data.shard == shard and a.data.shard_block_root == winning_root
if a.data.shard == shard and a.data.crosslink_data_root == winning_root
),
committee_config=CommitteeConfig(config),
)
Expand Down
4 changes: 2 additions & 2 deletions eth2/beacon/tools/builder/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,14 @@ def create_mock_signed_attestations_at_slot(
committee, shard = crosslink_committee

num_voted_attesters = int(len(committee) * voted_attesters_ratio)
latest_crosslink_root = state.latest_crosslinks[shard].shard_block_root
latest_crosslink_root = state.latest_crosslinks[shard].crosslink_data_root

attestation_data = AttestationData(
slot=attestation_slot,
shard=shard,
beacon_block_root=beacon_block_root,
epoch_boundary_root=epoch_boundary_root,
shard_block_root=ZERO_HASH32,
crosslink_data_root=ZERO_HASH32,
latest_crosslink_root=latest_crosslink_root,
justified_epoch=state.justified_epoch,
justified_block_root=justified_block_root,
Expand Down
6 changes: 3 additions & 3 deletions eth2/beacon/types/attestation_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AttestationData(ssz.Serializable):
# Hash of the ancestor at the epoch boundary
('epoch_boundary_root', bytes32),
# Shard block root being attested to
('shard_block_root', bytes32),
('crosslink_data_root', bytes32),
# Last crosslink hash
('latest_crosslink_root', bytes32),
# epoch of the last justified beacon block
Expand All @@ -41,7 +41,7 @@ def __init__(self,
shard: Shard,
beacon_block_root: Hash32,
epoch_boundary_root: Hash32,
shard_block_root: Hash32,
crosslink_data_root: Hash32,
latest_crosslink_root: Hash32,
justified_epoch: Epoch,
justified_block_root: Hash32) -> None:
Expand All @@ -50,7 +50,7 @@ def __init__(self,
shard,
beacon_block_root,
epoch_boundary_root,
shard_block_root,
crosslink_data_root,
latest_crosslink_root,
justified_epoch,
justified_block_root,
Expand Down
6 changes: 3 additions & 3 deletions eth2/beacon/types/crosslink_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class CrosslinkRecord(ssz.Serializable):
# Epoch during which crosslink was added
('epoch', uint64),
# Shard chain block root
('shard_block_root', bytes32),
('crosslink_data_root', bytes32),
]

def __init__(self,
epoch: Epoch,
shard_block_root: Hash32) -> None:
crosslink_data_root: Hash32) -> None:

super().__init__(
epoch=epoch,
shard_block_root=shard_block_root,
crosslink_data_root=crosslink_data_root,
)
7 changes: 6 additions & 1 deletion eth2/beacon/types/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,12 @@ def create_filled_state(cls,

# Recent state
latest_crosslinks=(
(CrosslinkRecord(epoch=genesis_epoch, shard_block_root=ZERO_HASH32),) * shard_count
(
CrosslinkRecord(
epoch=genesis_epoch,
crosslink_data_root=ZERO_HASH32,
),
) * shard_count
),
latest_block_roots=(ZERO_HASH32,) * latest_block_roots_length,
latest_active_index_roots=(ZERO_HASH32,) * latest_active_index_roots_length,
Expand Down
4 changes: 2 additions & 2 deletions tests/core/p2p-proto/bcc/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ async def test_send_single_attestation(request, event_loop):
shard=1,
beacon_block_root=ZERO_HASH32,
epoch_boundary_root=ZERO_HASH32,
shard_block_root=ZERO_HASH32,
crosslink_data_root=ZERO_HASH32,
latest_crosslink_root=ZERO_HASH32,
justified_epoch=SERENITY_CONFIG.GENESIS_EPOCH,
justified_block_root=ZERO_HASH32,
Expand All @@ -193,7 +193,7 @@ async def test_send_multiple_attestations(request, event_loop):
shard=shard,
beacon_block_root=ZERO_HASH32,
epoch_boundary_root=ZERO_HASH32,
shard_block_root=ZERO_HASH32,
crosslink_data_root=ZERO_HASH32,
latest_crosslink_root=ZERO_HASH32,
justified_epoch=SERENITY_CONFIG.GENESIS_EPOCH,
justified_block_root=ZERO_HASH32,
Expand Down
6 changes: 3 additions & 3 deletions tests/eth2/beacon/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def sample_attestation_data_params():
'shard': 12,
'beacon_block_root': b'\x11' * 32,
'epoch_boundary_root': b'\x22' * 32,
'shard_block_root': b'\x33' * 32,
'crosslink_data_root': b'\x33' * 32,
'latest_crosslink_root': b'\x44' * 32,
'justified_epoch': 0,
'justified_block_root': b'\x55' * 32,
Expand Down Expand Up @@ -198,7 +198,7 @@ def sample_eth1_data_vote_params(sample_eth1_data_params):
def sample_crosslink_record_params():
return {
'epoch': 0,
'shard_block_root': b'\x43' * 32,
'crosslink_data_root': b'\x43' * 32,
}


Expand Down Expand Up @@ -586,7 +586,7 @@ def genesis_state(filled_beacon_state,
latest_crosslinks=tuple(
CrosslinkRecord(
epoch=genesis_epoch,
shard_block_root=ZERO_HASH32,
crosslink_data_root=ZERO_HASH32,
)
for _ in range(shard_count)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
validate_attestation_latest_crosslink_root,
validate_attestation_justified_block_root,
validate_attestation_justified_epoch,
validate_attestation_shard_block_root,
validate_attestation_crosslink_data_root,
validate_attestation_slot,
)
from eth2.beacon.tools.builder.validator import (
Expand Down Expand Up @@ -166,7 +166,7 @@ def test_validate_attestation_justified_block_root(sample_attestation_data_param
@pytest.mark.parametrize(
(
'attestation_latest_crosslink_root,'
'attestation_shard_block_root,'
'attestation_crosslink_data_root,'
'latest_crosslink_root,'
'is_valid,'
),
Expand All @@ -180,14 +180,14 @@ def test_validate_attestation_justified_block_root(sample_attestation_data_param
)
def test_validate_attestation_latest_crosslink_root(sample_attestation_data_params,
attestation_latest_crosslink_root,
attestation_shard_block_root,
attestation_crosslink_data_root,
latest_crosslink_root,
is_valid):
sample_attestation_data_params['latest_crosslink_root'] = attestation_latest_crosslink_root
sample_attestation_data_params['shard_block_root'] = attestation_shard_block_root
sample_attestation_data_params['crosslink_data_root'] = attestation_crosslink_data_root
attestation_data = AttestationData(**sample_attestation_data_params).copy(
latest_crosslink_root=attestation_latest_crosslink_root,
shard_block_root=attestation_shard_block_root,
crosslink_data_root=attestation_crosslink_data_root,
)

if is_valid:
Expand All @@ -205,7 +205,7 @@ def test_validate_attestation_latest_crosslink_root(sample_attestation_data_para

@pytest.mark.parametrize(
(
'attestation_shard_block_root,'
'attestation_crosslink_data_root,'
'is_valid,'
),
[
Expand All @@ -214,20 +214,20 @@ def test_validate_attestation_latest_crosslink_root(sample_attestation_data_para
(b'\x66' * 32, False),
]
)
def test_validate_attestation_shard_block_root(sample_attestation_data_params,
attestation_shard_block_root,
is_valid):
def test_validate_attestation_crosslink_data_root(sample_attestation_data_params,
attestation_crosslink_data_root,
is_valid):
attestation_data = AttestationData(**sample_attestation_data_params).copy(
shard_block_root=attestation_shard_block_root,
crosslink_data_root=attestation_crosslink_data_root,
)

if is_valid:
validate_attestation_shard_block_root(
validate_attestation_crosslink_data_root(
attestation_data,
)
else:
with pytest.raises(ValidationError):
validate_attestation_shard_block_root(
validate_attestation_crosslink_data_root(
attestation_data,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,11 @@ def test_process_crosslinks(
sample_attestation_data_params,
sample_attestation_params):
shard = 1
shard_block_root = hash_eth2(b'shard_block_root')
crosslink_data_root = hash_eth2(b'crosslink_data_root')
current_slot = config.SLOTS_PER_EPOCH * 2 - 1

genesis_crosslinks = tuple([
CrosslinkRecord(epoch=config.GENESIS_EPOCH, shard_block_root=ZERO_HASH32)
CrosslinkRecord(epoch=config.GENESIS_EPOCH, crosslink_data_root=ZERO_HASH32)
for _ in range(shard_count)
])
state = n_validators_state.copy(
Expand Down Expand Up @@ -334,7 +334,7 @@ def test_process_crosslinks(
data=AttestationData(**sample_attestation_data_params).copy(
slot=slot_in_cur_epoch,
shard=shard,
shard_block_root=shard_block_root,
crosslink_data_root=crosslink_data_root,
),
aggregation_bitfield=aggregation_bitfield,
)
Expand All @@ -344,18 +344,18 @@ def test_process_crosslinks(
latest_attestations=cur_epoch_attestations,
)
assert (state.latest_crosslinks[shard].epoch == config.GENESIS_EPOCH and
state.latest_crosslinks[shard].shard_block_root == ZERO_HASH32)
state.latest_crosslinks[shard].crosslink_data_root == ZERO_HASH32)

new_state = process_crosslinks(state, config)
crosslink_record = new_state.latest_crosslinks[shard]
if success_crosslink_in_cur_epoch:
attestation = cur_epoch_attestations[0]
assert (crosslink_record.epoch == slot_to_epoch(current_slot, slots_per_epoch) and
crosslink_record.shard_block_root == attestation.data.shard_block_root and
attestation.data.shard_block_root == shard_block_root)
crosslink_record.crosslink_data_root == attestation.data.crosslink_data_root and
attestation.data.crosslink_data_root == crosslink_data_root)
else:
assert (crosslink_record.epoch == config.GENESIS_EPOCH and
crosslink_record.shard_block_root == ZERO_HASH32)
crosslink_record.crosslink_data_root == ZERO_HASH32)


#
Expand Down Expand Up @@ -739,13 +739,13 @@ def test_process_rewards_and_penalties_for_crosslinks(
committee, shard = prev_epoch_crosslink_committees[i]
# Randomly sample `num_attesting_validators` validators
# from the committee to attest in this slot.
shard_block_root_attesting_validators = random.sample(
crosslink_data_root_attesting_validators = random.sample(
committee,
num_attesting_validators,
)
each_slot_attestion_validators_list.append(shard_block_root_attesting_validators)
each_slot_attestion_validators_list.append(crosslink_data_root_attesting_validators)
participants_bitfield = get_empty_bitfield(target_committee_size)
for index in shard_block_root_attesting_validators:
for index in crosslink_data_root_attesting_validators:
participants_bitfield = set_voted(participants_bitfield, committee.index(index))
data_slot = i + previous_epoch * slots_per_epoch
previous_epoch_attestations.append(
Expand Down Expand Up @@ -885,7 +885,7 @@ def test_check_if_update_validator_registry(genesis_state,
if has_crosslink:
crosslink = CrosslinkRecord(
epoch=crosslink_epoch,
shard_block_root=ZERO_HASH32,
crosslink_data_root=ZERO_HASH32,
)
latest_crosslinks = state.latest_crosslinks
for shard in range(config.SHARD_COUNT):
Expand Down
Loading

0 comments on commit cdba2e9

Please sign in to comment.