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

Commit

Permalink
Remove placeholder (#250)
Browse files Browse the repository at this point in the history
* Update the type of `ValidatorIndex` from `uint24` to `uint64`

* Update `SlashableAttestation` and `verify_slashable_attestation`

* Remove placeholders

* Remove `latest_vdf_outputs`, `persistent_committees`, `persistent_committee_reassignments`, and `custody_challenges` from `BeaconState`
* Remove `custody_reseeds`, `custody_challenges`,  and `custody_responses` from `BaseBeaconBlock`

* Remove `custody_commitment`

* Remove `vote_count` (we can just use `len(slashable_attestation.validator_indices)`)

* Fix `verify_bitfield`

* Rename `verify_slashable_attestation` to `validate_slashable_attestation` to imply that it's not as same as the spec `verify_slashable_attestation`

* Add more test cases for `test_validate_slashable_attestation`

* Add tests for new SlashableAttestation properties

* `verifiy_bitfield` ->  `validate_bitfield`

* Add tests for `validate_bitfield`

* Refactor

* Apply suggestions from code review

Co-Authored-By: hwwhww <hwwang156@gmail.com>

* PR feedback, thanks Nic!
  • Loading branch information
hwwhww committed Feb 7, 2019
1 parent 320162d commit dec97c0
Show file tree
Hide file tree
Showing 30 changed files with 314 additions and 357 deletions.
5 changes: 0 additions & 5 deletions eth2/beacon/deposit_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ def validate_proof_of_possession(state: BeaconState,
proof_of_possession: BLSSignature,
withdrawal_credentials: Hash32,
randao_commitment: Hash32,
custody_commitment: Hash32,
epoch_length: int) -> None:
deposit_input = DepositInput(
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
custody_commitment=custody_commitment,
proof_of_possession=EMPTY_SIGNATURE,
)

Expand Down Expand Up @@ -79,7 +77,6 @@ def process_deposit(*,
proof_of_possession: BLSSignature,
withdrawal_credentials: Hash32,
randao_commitment: Hash32,
custody_commitment: Hash32,
epoch_length: int) -> BeaconState:
"""
Process a deposit from Ethereum 1.0.
Expand All @@ -90,7 +87,6 @@ def process_deposit(*,
proof_of_possession=proof_of_possession,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
custody_commitment=custody_commitment,
epoch_length=epoch_length,
)

Expand All @@ -100,7 +96,6 @@ def process_deposit(*,
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
custody_commitment=custody_commitment,
)

# Note: In phase 2 registry indices that has been withdrawn for a long time
Expand Down
72 changes: 44 additions & 28 deletions eth2/beacon/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
)

from eth2._utils.bitfield import (
get_bitfield_length,
has_voted,
)
import eth2._utils.bls as bls
Expand Down Expand Up @@ -48,6 +47,7 @@
ValidatorIndex,
)
from eth2.beacon.validation import (
validate_bitfield,
validate_epoch_for_active_index_root,
validate_epoch_for_active_randao_mix,
validate_epoch_for_current_epoch,
Expand Down Expand Up @@ -383,13 +383,13 @@ def _get_committee_for_shard(
@to_tuple
def get_attestation_participants(state: 'BeaconState',
attestation_data: 'AttestationData',
aggregation_bitfield: Bitfield,
bitfield: Bitfield,
genesis_epoch: EpochNumber,
epoch_length: int,
target_committee_size: int,
shard_count: int) -> Iterable[ValidatorIndex]:
"""
Return the participant indices at for the ``attestation_data`` and ``aggregation_bitfield``.
Return the participant indices at for the ``attestation_data`` and ``bitfield``.
"""
# Find the committee in the list with the desired shard
crosslink_committees = get_crosslink_committees_at_slot(
Expand Down Expand Up @@ -420,16 +420,11 @@ def get_attestation_participants(state: 'BeaconState',
)
)

committee_size = len(committee)
if len(aggregation_bitfield) != get_bitfield_length(committee_size):
raise ValidationError(
f"Invalid bitfield length,"
f"\texpected: {get_bitfield_length(committee_size)}, found: {len(aggregation_bitfield)}"
)
validate_bitfield(bitfield, len(committee))

# Find the participating attesters in the committee
for bitfield_index, validator_index in enumerate(committee):
if has_voted(aggregation_bitfield, bitfield_index):
if has_voted(bitfield, bitfield_index):
yield validator_index


Expand Down Expand Up @@ -618,24 +613,14 @@ def generate_aggregate_pubkeys(
Compute the aggregate pubkey we expect based on
the proof-of-custody indices found in the ``slashable_attestation``.
"""
custody_bit_0_indices = slashable_attestation.custody_bit_0_indices
custody_bit_1_indices = slashable_attestation.custody_bit_1_indices
all_indices = (custody_bit_0_indices, custody_bit_1_indices)
all_indices = slashable_attestation.custody_bit_indices
get_pubkeys = functools.partial(get_pubkey_for_indices, validators)
return map(
bls.aggregate_pubkeys,
map(get_pubkeys, all_indices),
)


def verify_vote_count(slashable_attestation: 'SlashableAttestation',
max_indices_per_slashable_vote: int) -> bool:
"""
Ensure we have no more than ``max_indices_per_slashable_vote`` in the ``slashable_attestation``.
"""
return slashable_attestation.vote_count <= max_indices_per_slashable_vote


def verify_slashable_attestation_signature(state: 'BeaconState',
slashable_attestation: 'SlashableAttestation',
epoch_length: int) -> bool:
Expand All @@ -662,20 +647,51 @@ def verify_slashable_attestation_signature(state: 'BeaconState',
)


def verify_slashable_attestation(state: 'BeaconState',
slashable_attestation: 'SlashableAttestation',
max_indices_per_slashable_vote: int,
epoch_length: int) -> bool:
def validate_slashable_attestation(state: 'BeaconState',
slashable_attestation: 'SlashableAttestation',
max_indices_per_slashable_vote: int,
epoch_length: int) -> None:
"""
Verify validity of ``slashable_attestation`` fields.
Ensure that the ``slashable_attestation`` is properly assembled and contains the signature
we expect from the validators we expect. Otherwise, return False as
the ``slashable_attestation`` is invalid.
"""
return (
verify_vote_count(slashable_attestation, max_indices_per_slashable_vote) and
verify_slashable_attestation_signature(state, slashable_attestation, epoch_length)
# [TO BE REMOVED IN PHASE 1]
if not slashable_attestation.is_custody_bitfield_empty:
raise ValidationError(
"`slashable_attestation.custody_bitfield` is not empty."
)

if len(slashable_attestation.validator_indices) == 0:
raise ValidationError(
"`slashable_attestation.validator_indices` is empty."
)

if not slashable_attestation.is_validator_indices_ascending:
raise ValidationError(
"`slashable_attestation.validator_indices` "
f"({slashable_attestation.validator_indices}) "
"is not ordered in ascending."
)

validate_bitfield(
slashable_attestation.custody_bitfield,
len(slashable_attestation.validator_indices),
)

if len(slashable_attestation.validator_indices) > max_indices_per_slashable_vote:
raise ValidationError(
f"`len(slashable_attestation.validator_indices)` "
f"({len(slashable_attestation.validator_indices)}) greater than"
f"MAX_INDICES_PER_SLASHABLE_VOTE ({max_indices_per_slashable_vote})"
)

if not verify_slashable_attestation_signature(state, slashable_attestation, epoch_length):
raise ValidationError(
f"slashable_attestation.signature error"
)


def is_double_vote(attestation_data_1: 'AttestationData',
attestation_data_2: 'AttestationData',
Expand Down
10 changes: 0 additions & 10 deletions eth2/beacon/on_startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,13 @@ def get_initial_beacon_state(*,

# Randomness and committees
latest_randao_mixes=tuple(ZERO_HASH32 for _ in range(latest_randao_mixes_length)),
latest_vdf_outputs=tuple(
ZERO_HASH32 for _ in range(latest_randao_mixes_length // epoch_length)
),
# TODO Remove `persistent_committees`, `persistent_committee_reassignments`
persistent_committees=(),
persistent_committee_reassignments=(),
previous_epoch_start_shard=genesis_start_shard,
current_epoch_start_shard=genesis_start_shard,
previous_calculation_epoch=genesis_epoch,
current_calculation_epoch=genesis_epoch,
previous_epoch_seed=ZERO_HASH32,
current_epoch_seed=ZERO_HASH32,

# Custody challenges
custody_challenges=(),

# Finality
previous_justified_epoch=genesis_epoch,
justified_epoch=genesis_epoch,
Expand Down Expand Up @@ -145,7 +136,6 @@ def get_initial_beacon_state(*,
proof_of_possession=deposit.deposit_data.deposit_input.proof_of_possession,
withdrawal_credentials=deposit.deposit_data.deposit_input.withdrawal_credentials,
randao_commitment=deposit.deposit_data.deposit_input.randao_commitment,
custody_commitment=deposit.deposit_data.deposit_input.custody_commitment,
epoch_length=epoch_length,
)

Expand Down
1 change: 0 additions & 1 deletion eth2/beacon/sedes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@


hash32 = Binary.fixed_length(32)
uint24 = BigEndianInt(24)
uint64 = BigEndianInt(64)
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def validate_attestation_aggregate_signature(state: BeaconState,
participant_indices = get_attestation_participants(
state=state,
attestation_data=attestation.data,
aggregation_bitfield=attestation.aggregation_bitfield,
bitfield=attestation.aggregation_bitfield,
genesis_epoch=genesis_epoch,
epoch_length=epoch_length,
target_committee_size=target_committee_size,
Expand Down
3 changes: 0 additions & 3 deletions eth2/beacon/tools/builder/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def create_mock_initial_validator_deposits(
# Mock data
withdrawal_credentials = b'\x22' * 32
randao_commitment = b'\x33' * 32
custody_commitment = b'\x44' * 32
deposit_timestamp = 0
fork = Fork(
previous_version=config.GENESIS_FORK_VERSION,
Expand All @@ -59,13 +58,11 @@ def create_mock_initial_validator_deposits(
pubkey=pubkeys[i],
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
custody_commitment=custody_commitment,
proof_of_possession=sign_proof_of_possession(
deposit_input=DepositInput(
pubkey=pubkeys[i],
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
custody_commitment=custody_commitment,
),
privkey=keymap[pubkeys[i]],
fork=fork,
Expand Down
24 changes: 0 additions & 24 deletions eth2/beacon/types/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@


from .attestations import Attestation
from .custody_challenges import CustodyChallenge
from .custody_reseeds import CustodyReseed
from .custody_responses import CustodyResponse
from .attester_slashings import AttesterSlashing
from .deposits import Deposit
from .eth1_data import Eth1Data
Expand All @@ -61,9 +58,6 @@ class BeaconBlockBody(rlp.Serializable):
('proposer_slashings', CountableList(ProposerSlashing)),
('attester_slashings', CountableList(AttesterSlashing)),
('attestations', CountableList(Attestation)),
('custody_reseeds', CountableList(CustodyReseed)),
('custody_challenges', CountableList(CustodyChallenge)),
('custody_responses', CountableList(CustodyResponse)),
('deposits', CountableList(Deposit)),
('exits', CountableList(Exit)),
]
Expand All @@ -72,18 +66,12 @@ def __init__(self,
proposer_slashings: Sequence[ProposerSlashing],
attester_slashings: Sequence[AttesterSlashing],
attestations: Sequence[Attestation],
custody_reseeds: Sequence[CustodyReseed],
custody_challenges: Sequence[CustodyResponse],
custody_responses: Sequence[CustodyResponse],
deposits: Sequence[Deposit],
exits: Sequence[Exit])-> None:
super().__init__(
proposer_slashings=proposer_slashings,
attester_slashings=attester_slashings,
attestations=attestations,
custody_reseeds=custody_reseeds,
custody_challenges=custody_challenges,
custody_responses=custody_responses,
deposits=deposits,
exits=exits,
)
Expand All @@ -94,9 +82,6 @@ def create_empty_body(cls) -> 'BeaconBlockBody':
proposer_slashings=(),
attester_slashings=(),
attestations=(),
custody_reseeds=(),
custody_challenges=(),
custody_responses=(),
deposits=(),
exits=(),
)
Expand All @@ -107,9 +92,6 @@ def is_empty(self) -> bool:
self.proposer_slashings == () and
self.attester_slashings == () and
self.attestations == () and
self.custody_reseeds == () and
self.custody_challenges == () and
self.custody_responses == () and
self.deposits == () and
self.exits == ()
)
Expand All @@ -121,9 +103,6 @@ def cast_block_body(cls,
proposer_slashings=body.proposer_slashings,
attester_slashings=body.attester_slashings,
attestations=body.attestations,
custody_reseeds=body.custody_reseeds,
custody_challenges=body.custody_challenges,
custody_responses=body.custody_responses,
deposits=body.deposits,
exits=body.exits,
)
Expand Down Expand Up @@ -217,9 +196,6 @@ def from_root(cls, root: Hash32, chaindb: 'BaseBeaconChainDB') -> 'BeaconBlock':
proposer_slashings=block.body.proposer_slashings,
attester_slashings=block.body.attester_slashings,
attestations=block.body.attestations,
custody_reseeds=block.body.custody_reseeds,
custody_challenges=block.body.custody_challenges,
custody_responses=block.body.custody_responses,
deposits=block.body.deposits,
exits=block.body.exits,
)
Expand Down
5 changes: 0 additions & 5 deletions eth2/beacon/types/custody_challenges.py

This file was deleted.

5 changes: 0 additions & 5 deletions eth2/beacon/types/custody_reseeds.py

This file was deleted.

5 changes: 0 additions & 5 deletions eth2/beacon/types/custody_responses.py

This file was deleted.

4 changes: 0 additions & 4 deletions eth2/beacon/types/deposit_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class DepositInput(rlp.Serializable):
('withdrawal_credentials', hash32),
# Initial RANDAO commitment
('randao_commitment', hash32),
# Initial proof of custody commitment
('custody_commitment', hash32),
# BLS proof of possession (a BLS signature)
('proof_of_possession', binary),
]
Expand All @@ -38,13 +36,11 @@ def __init__(self,
pubkey: BLSPubkey,
withdrawal_credentials: Hash32,
randao_commitment: Hash32,
custody_commitment: Hash32,
proof_of_possession: BLSSignature=EMPTY_SIGNATURE) -> None:
super().__init__(
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
custody_commitment=custody_commitment,
proof_of_possession=proof_of_possession,
)

Expand Down
3 changes: 1 addition & 2 deletions eth2/beacon/types/exits.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
binary,
)
from eth2.beacon.sedes import (
uint24,
uint64,
)
from eth2.beacon.typing import (
Expand All @@ -22,7 +21,7 @@ class Exit(rlp.Serializable):
# Minimum epoch for processing exit
('epoch', uint64),
# Index of the exiting validator
('validator_index', uint24),
('validator_index', uint64),
# Validator signature
('signature', binary),
]
Expand Down
4 changes: 2 additions & 2 deletions eth2/beacon/types/proposer_slashings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
binary,
)
from eth2.beacon.sedes import (
uint24,
uint64,
)
from .proposal_signed_data import ProposalSignedData
from eth2.beacon.typing import (
Expand All @@ -16,7 +16,7 @@
class ProposerSlashing(rlp.Serializable):
fields = [
# Proposer index
('proposer_index', uint24),
('proposer_index', uint64),
# First proposal data
('proposal_data_1', ProposalSignedData),
# First proposal signature
Expand Down
Loading

0 comments on commit dec97c0

Please sign in to comment.