From 7cf13c22f0e468e1023b0cc3fce843e46eba324b Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Wed, 6 Feb 2019 13:31:41 +0100 Subject: [PATCH 1/2] fix up some non-spec things that interfere with state_sim with attestations --- beacon_chain/state_transition.nim | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/beacon_chain/state_transition.nim b/beacon_chain/state_transition.nim index 45a051723..cd257a3bb 100644 --- a/beacon_chain/state_transition.nim +++ b/beacon_chain/state_transition.nim @@ -436,18 +436,6 @@ func lowerThan(candidate, current: Eth2Digest): bool = if v > candidate.data[i]: return true return false -func inclusion_slot(state: BeaconState, v: ValidatorIndex): uint64 = - for a in state.latest_attestations: - if v in get_attestation_participants(state, a.data, a.participation_bitfield): - return a.slot_included - doAssert false # shouldn't happen.. - -func inclusion_distance(state: BeaconState, v: ValidatorIndex): uint64 = - for a in state.latest_attestations: - if v in get_attestation_participants(state, a.data, a.participation_bitfield): - return a.slot_included - a.data.slot - doAssert false # shouldn't happen.. - func processEpoch(state: var BeaconState) = ## https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#per-epoch-processing @@ -486,8 +474,7 @@ func processEpoch(state: var BeaconState) = let previous_epoch_attestations = state.latest_attestations.filterIt( - state.slot <= it.data.slot + 2 * EPOCH_LENGTH and - it.data.slot + EPOCH_LENGTH < state.slot) + previous_epoch == slot_to_epoch(it.data.slot)) let previous_epoch_attester_indices = @@ -649,6 +636,17 @@ func processEpoch(state: var BeaconState) = get_effective_balance(state, index) * epochs_since_finality div INACTIVITY_PENALTY_QUOTIENT div 2 + func inclusion_slot(state: BeaconState, v: ValidatorIndex): uint64 = + for a in previous_epoch_attestations: + if v in get_attestation_participants(state, a.data, a.participation_bitfield): + return a.slot_included + doAssert false # shouldn't happen.. + + func inclusion_distance(state: BeaconState, v: ValidatorIndex): uint64 = + for a in previous_epoch_attestations: + if v in get_attestation_participants(state, a.data, a.participation_bitfield): + return a.slot_included - a.data.slot + doAssert false # shouldn't happen.. block: # Justification and finalization let epochs_since_finality = next_epoch - state.finalized_epoch From 74cb86ec2109c13ced0f8c3c93edaaafbfdf8300 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Wed, 6 Feb 2019 21:37:21 +0100 Subject: [PATCH 2/2] more spec changes --- beacon_chain/beacon_node.nim | 4 ++-- beacon_chain/fork_choice.nim | 10 ++++----- beacon_chain/spec/beaconstate.nim | 2 +- beacon_chain/spec/datatypes.nim | 37 +++++++++++++++++++------------ beacon_chain/state_transition.nim | 2 +- tests/testutil.nim | 2 +- 6 files changed, 33 insertions(+), 24 deletions(-) diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index 61ec2fb58..61ce22edf 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -180,7 +180,7 @@ proc makeAttestation(node: BeaconNode, var attestation = Attestation( data: attestationData, aggregate_signature: validatorSignature, - participation_bitfield: participationBitfield) + aggregation_bitfield: participationBitfield) await node.network.broadcast(topicAttestations, attestation) @@ -384,7 +384,7 @@ proc processBlocks*(node: BeaconNode) = node.network.subscribe(topicAttestations) do (a: Attestation): let participants = get_attestation_participants( - node.beaconState, a.data, a.participation_bitfield). + node.beaconState, a.data, a.aggregation_bitfield). mapIt(shortValidatorKey(node, it)) info "Attestation received", slot = a.data.slot, diff --git a/beacon_chain/fork_choice.nim b/beacon_chain/fork_choice.nim index 2156eaba3..87bae0004 100644 --- a/beacon_chain/fork_choice.nim +++ b/beacon_chain/fork_choice.nim @@ -46,13 +46,13 @@ proc combine*(tgt: var Attestation, src: Attestation, flags: UpdateFlags) = assert tgt.data == src.data - for i in 0 ..< tgt.participation_bitfield.len: + for i in 0 ..< tgt.aggregation_bitfield.len: # TODO: # when BLS signatures are combined, we must ensure that # the same participant key is not included on both sides - tgt.participation_bitfield[i] = - tgt.participation_bitfield[i] or - src.participation_bitfield[i] + tgt.aggregation_bitfield[i] = + tgt.aggregation_bitfield[i] or + src.aggregation_bitfield[i] if skipValidation notin flags: tgt.aggregate_signature.combine(src.aggregate_signature) @@ -194,7 +194,7 @@ func getAttestationVoteCount(pool: AttestationPool, current_slot: int): CountTab for attestation in pool.attestations[slot]: if attestation.isSome: # Increase the block attestation counts by the number of validators aggregated - let voteCount = attestation.get.participation_bitfield.getVoteCount() + let voteCount = attestation.get.aggregation_bitfield.getVoteCount() result.inc(attestation.get.data.beacon_block_root, voteCount) proc lmdGhost*( diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index ed0652ef7..340a50032 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -373,7 +373,7 @@ proc checkAttestation*( let participants = get_attestation_participants( - state, attestation.data, attestation.participation_bitfield) + state, attestation.data, attestation.aggregation_bitfield) group_public_key = bls_aggregate_pubkeys( participants.mapIt(state.validator_registry[it].pubkey)) diff --git a/beacon_chain/spec/datatypes.nim b/beacon_chain/spec/datatypes.nim index 1fa16c91f..2552a1e68 100644 --- a/beacon_chain/spec/datatypes.nim +++ b/beacon_chain/spec/datatypes.nim @@ -15,9 +15,6 @@ # The latest version can be seen here: # https://github.com/ethereum/eth2.0-specs/blob/master/specs/beacon-chain.md # -# How wrong the code is: -# https://github.com/ethereum/eth2.0-specs/compare/8116562049ed80ad1823dd62e98a7483ddf1546c...master -# # These datatypes are used as specifications for serialization - thus should not # be altered outside of what the spec says. Likewise, they should not be made # `ref` - this can be achieved by wrapping them in higher-level @@ -78,6 +75,7 @@ const MAX_WITHDRAWALS_PER_EPOCH* = 4 # withdrawals # Deposit contract + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md DEPOSIT_CONTRACT_TREE_DEPTH* = 2^5 MIN_DEPOSIT_AMOUNT* = 2'u64^0 * 10'u64^9 ##\ @@ -93,6 +91,7 @@ const ## processing is done # Initial values + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#initial-values GENESIS_FORK_VERSION* = 0'u64 GENESIS_SLOT* = 2'u64^63 GENESIS_EPOCH* = GENESIS_SLOT div EPOCH_LENGTH # slot_to_epoch(GENESIS_SLOT) @@ -103,6 +102,7 @@ const BLS_WITHDRAWAL_PREFIX_BYTE* = 0'u8 # Time parameters + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#time-parameters SLOT_DURATION* = 6'u64 ## \ ## TODO consistent time unit across projects, similar to C++ chrono? @@ -127,18 +127,19 @@ const ETH1_DATA_VOTING_PERIOD* = 2'u64^4 ##\ ## epochs (~1.7 hours) - MIN_VALIDATOR_WITHDRAWAL_TIME* = 2'u64^8 ##\ + MIN_VALIDATOR_WITHDRAWAL_EPOCHS* = 2'u64^8 ##\ ## epochs (~27 hours) # State list lengths + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#state-list-lengths LATEST_BLOCK_ROOTS_LENGTH* = 2'u64^13 LATEST_RANDAO_MIXES_LENGTH* = 2'u64^13 LATEST_INDEX_ROOTS_LENGTH* = 2'u64^13 LATEST_PENALIZED_EXIT_LENGTH* = 8192 # epochs # Reward and penalty quotients - # https://github.com/ethereum/eth2.0-specs/blob/dev/specs/core/0_beacon-chain.md#reward-and-penalty-quotients - BASE_REWARD_QUOTIENT* = 2'u64^10 ##\ + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#reward-and-penalty-quotients + BASE_REWARD_QUOTIENT* = 2'u64^5 ##\ ## The `BASE_REWARD_QUOTIENT` parameter dictates the per-epoch reward. It ## corresponds to ~2.54% annual interest assuming 10 million participating ## ETH in every epoch. @@ -147,11 +148,13 @@ const INACTIVITY_PENALTY_QUOTIENT* = 2'u64^24 # Status flags + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#status-flags # Could model this with enum, but following spec closely here INITIATED_EXIT* = 1'u64 WITHDRAWABLE* = 2'u64 # Max operations per block + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#max-operations-per-block MAX_PROPOSER_SLASHINGS* = 2^4 MAX_ATTESTER_SLASHINGS* = 2^0 MAX_ATTESTATIONS* = 2^7 @@ -163,7 +166,7 @@ type SlotNumber* = uint64 EpochNumber* = uint64 - # https://github.com/ethereum/eth2.0-specs/blob/dev/specs/core/0_beacon-chain.md#proposerslashing + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#proposerslashing ProposerSlashing* = object proposer_index*: ValidatorIndex proposal_data_1*: ProposalSignedData @@ -171,38 +174,40 @@ type proposal_data_2*: ProposalSignedData proposal_signature_2*: ValidatorSig - # https://github.com/ethereum/eth2.0-specs/blob/dev/specs/core/0_beacon-chain.md#attesterslashing + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#attesterslashing AttesterSlashing* = object slashable_attestation_1*: SlashableAttestation ## \ ## First batch of votes slashable_attestation_2*: SlashableAttestation ## \ ## Second batch of votes - # https://github.com/ethereum/eth2.0-specs/blob/dev/specs/core/0_beacon-chain.md#slashableattestation + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#slashableattestation SlashableAttestation* = object validator_indices*: seq[uint64] ##\ ## Validator indices - custody_bitfield*: seq[byte] ##\ - ## Custody bitfield - data*: AttestationData ## \ ## Attestation data + custody_bitfield*: seq[byte] ##\ + ## Custody bitfield + aggregate_signature*: ValidatorSig ## \ ## Aggregate signature + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#attestation Attestation* = object - data*: AttestationData - participation_bitfield*: seq[byte] ##\ + aggregation_bitfield*: seq[byte] ##\ ## The attesters that are represented in the aggregate signature - each ## bit represents an index in `ShardCommittee.committee` + data*: AttestationData custody_bitfield*: seq[byte] ##\ ## Custody bitfield aggregate_signature*: ValidatorSig ##\ ## Aggregate signature of the validators in `custody_bitfield` + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#attestationdata AttestationData* = object slot*: uint64 shard*: uint64 @@ -224,10 +229,12 @@ type justified_block_root*: Eth2Digest ##\ ## Hash of last justified beacon block + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#attestationdata AttestationDataAndCustodyBit* = object data*: AttestationData custody_bit: bool + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#deposit Deposit* = object branch*: seq[Eth2Digest] ##\ ## Branch in the deposit tree @@ -238,11 +245,13 @@ type deposit_data*: DepositData ##\ ## Data + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#depositdata DepositData* = object amount*: uint64 ## Value in Gwei timestamp*: uint64 # Timestamp from deposit contract deposit_input*: DepositInput + # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#depositinput DepositInput* = object pubkey*: ValidatorPubKey withdrawal_credentials*: Eth2Digest diff --git a/beacon_chain/state_transition.nim b/beacon_chain/state_transition.nim index cd257a3bb..d7cc03167 100644 --- a/beacon_chain/state_transition.nim +++ b/beacon_chain/state_transition.nim @@ -273,7 +273,7 @@ proc processAttestations( state.latest_attestations.add blck.body.attestations.mapIt( PendingAttestation( data: it.data, - participation_bitfield: it.participation_bitfield, + participation_bitfield: it.aggregation_bitfield, custody_bitfield: it.custody_bitfield, slot_included: state.slot, ) diff --git a/tests/testutil.nim b/tests/testutil.nim index 13c6ab72d..a07f257c2 100644 --- a/tests/testutil.nim +++ b/tests/testutil.nim @@ -212,6 +212,6 @@ proc makeAttestation*( Attestation( data: data, - participation_bitfield: participation_bitfield, + aggregation_bitfield: participation_bitfield, aggregate_signature: sig )