use forked `getAttestationsForBlock` everywhere (#2937)

There are a number of locations in the code that get attestations on a
forked beacon state. For attestation pools test, a convenience wrapper
was available to reduce clutter. This patch integrates that wrapper into
the core component so that it can also take advantage of the wrapper.
This commit is contained in:
Etan Kissling 2021-10-01 03:29:32 +02:00 committed by GitHub
parent 6fc1511866
commit b217150f1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 55 deletions

View File

@ -591,6 +591,12 @@ proc getAttestationsForBlock*(pool: var AttestationPool,
res
proc getAttestationsForBlock*(pool: var AttestationPool,
state: ForkedHashedBeaconState,
cache: var StateCache): seq[Attestation] =
withState(state):
pool.getAttestationsForBlock(state, cache)
func bestValidation(aggregates: openArray[Validation]): (int, int) =
# Look for best validation based on number of votes in the aggregate
doAssert aggregates.len() > 0,

View File

@ -432,35 +432,26 @@ proc makeBeaconBlockForHeadAndSlot*(node: BeaconNode,
doAssert v.addr == addr proposalStateAddr.data
assign(proposalStateAddr[], poolPtr.headState)
template makeBeaconBlock(kind: untyped): untyped =
makeBeaconBlock(
node.dag.cfg,
stateData.data,
validator_index,
head.root,
randao_reveal,
eth1Proposal.vote,
graffiti,
node.attestationPool[].getAttestationsForBlock(
stateData.data.`hbs kind`, cache),
eth1Proposal.deposits,
node.exitPool[].getProposerSlashingsForBlock(),
node.exitPool[].getAttesterSlashingsForBlock(),
node.exitPool[].getVoluntaryExitsForBlock(),
sync_aggregate,
default(ExecutionPayload),
restore,
cache)
return if slot.epoch < node.dag.cfg.ALTAIR_FORK_EPOCH:
let sync_aggregate = SyncAggregate.init()
makeBeaconBlock(phase0)
elif slot.epoch < node.dag.cfg.MERGE_FORK_EPOCH:
let sync_aggregate = node.sync_committee_msg_pool[].produceSyncAggregate(head.root)
makeBeaconBlock(altair)
else:
let sync_aggregate = node.sync_committee_msg_pool[].produceSyncAggregate(head.root)
makeBeaconBlock(merge)
return makeBeaconBlock(
node.dag.cfg,
stateData.data,
validator_index,
head.root,
randao_reveal,
eth1Proposal.vote,
graffiti,
node.attestationPool[].getAttestationsForBlock(stateData.data, cache),
eth1Proposal.deposits,
node.exitPool[].getProposerSlashingsForBlock(),
node.exitPool[].getAttesterSlashingsForBlock(),
node.exitPool[].getVoluntaryExitsForBlock(),
if slot.epoch < node.dag.cfg.ALTAIR_FORK_EPOCH:
SyncAggregate(sync_committee_signature: ValidatorSig.infinity)
else:
node.sync_committee_msg_pool[].produceSyncAggregate(head.root),
default(merge.ExecutionPayload),
restore,
cache)
proc proposeSignedBlock*(node: BeaconNode,
head: BlockRef,

View File

@ -263,7 +263,7 @@ cli do(slots = SLOTS_PER_EPOCH * 6,
slot).toValidatorSig(),
eth1ProposalData.vote,
default(GraffitiBytes),
attPool.getAttestationsForTestBlock(stateData, cache),
attPool.getAttestationsForBlock(stateData.data, cache),
eth1ProposalData.deposits,
@[],
@[],

View File

@ -104,7 +104,7 @@ suite "Attestation pool processing" & preset():
getStateField(state.data, slot) + MIN_ATTESTATION_INCLUSION_DELAY, cache,
rewards, {})
let attestations = pool[].getAttestationsForTestBlock(state[], cache)
let attestations = pool[].getAttestationsForBlock(state.data, cache)
check:
attestations.len == 1
@ -126,14 +126,14 @@ suite "Attestation pool processing" & preset():
check:
# shouldn't include already-included attestations
pool[].getAttestationsForTestBlock(state[], cache) == []
pool[].getAttestationsForBlock(state.data, cache) == []
pool[].addAttestation(
att1, @[bc1[0]], att1.loadSig, att1.data.slot)
check:
# but new ones should go in
pool[].getAttestationsForTestBlock(state[], cache).len() == 1
pool[].getAttestationsForBlock(state.data, cache).len() == 1
let
att2 = makeAttestation(state[].data, root1, bc1[1], cache)
@ -141,7 +141,7 @@ suite "Attestation pool processing" & preset():
att2, @[bc1[1]], att2.loadSig, att2.data.slot)
let
combined = pool[].getAttestationsForTestBlock(state[], cache)
combined = pool[].getAttestationsForBlock(state.data, cache)
check:
# New attestations should be combined with old attestations
@ -153,7 +153,7 @@ suite "Attestation pool processing" & preset():
check:
# readding the combined attestation shouldn't have an effect
pool[].getAttestationsForTestBlock(state[], cache).len() == 1
pool[].getAttestationsForBlock(state.data, cache).len() == 1
let
# Someone votes for a different root
@ -164,7 +164,7 @@ suite "Attestation pool processing" & preset():
check:
# We should now get both attestations for the block, but the aggregate
# should be the one with the most votes
pool[].getAttestationsForTestBlock(state[], cache).len() == 2
pool[].getAttestationsForBlock(state.data, cache).len() == 2
pool[].getAggregatedAttestation(2.Slot, 0.CommitteeIndex).
get().aggregation_bits.countOnes() == 2
pool[].getAggregatedAttestation(2.Slot, hash_tree_root(att2.data)).
@ -203,7 +203,7 @@ suite "Attestation pool processing" & preset():
rewards, {})
check:
pool[].getAttestationsForTestBlock(state[], cache).len() == 2
pool[].getAttestationsForBlock(state.data, cache).len() == 2
# Can get either aggregate here, random!
pool[].getAggregatedAttestation(1.Slot, 0.CommitteeIndex).isSome()
@ -211,7 +211,7 @@ suite "Attestation pool processing" & preset():
pool[].addAttestation(att3, @[bc0[3]], att3.loadSig, att3.data.slot)
block:
let attestations = pool[].getAttestationsForTestBlock(state[], cache)
let attestations = pool[].getAttestationsForBlock(state.data, cache)
check:
attestations.len() == 2
attestations[0].aggregation_bits.countOnes() == 3
@ -223,7 +223,7 @@ suite "Attestation pool processing" & preset():
pool[].addAttestation(att0x, @[bc0[0]], att0x.loadSig, att0x.data.slot)
block:
let attestations = pool[].getAttestationsForTestBlock(state[], cache)
let attestations = pool[].getAttestationsForBlock(state.data, cache)
check:
attestations.len() == 1
attestations[0].aggregation_bits.countOnes() == 4
@ -253,7 +253,7 @@ suite "Attestation pool processing" & preset():
"6*SLOTS_PER_EPOCH validators > 128 mainnet MAX_ATTESTATIONS"
check:
# Fill block with attestations
pool[].getAttestationsForTestBlock(state[], cache).lenu64() ==
pool[].getAttestationsForBlock(state.data, cache).lenu64() ==
MAX_ATTESTATIONS
pool[].getAggregatedAttestation(
getStateField(state.data, slot) - 1, 0.CommitteeIndex).isSome()
@ -286,7 +286,7 @@ suite "Attestation pool processing" & preset():
defaultRuntimeConfig, state.data,
MIN_ATTESTATION_INCLUSION_DELAY.Slot + 1, cache, rewards, {})
let attestations = pool[].getAttestationsForTestBlock(state[], cache)
let attestations = pool[].getAttestationsForBlock(state.data, cache)
check:
attestations.len == 1
@ -312,7 +312,7 @@ suite "Attestation pool processing" & preset():
defaultRuntimeConfig, state.data,
MIN_ATTESTATION_INCLUSION_DELAY.Slot + 1, cache, rewards, {})
let attestations = pool[].getAttestationsForTestBlock(state[], cache)
let attestations = pool[].getAttestationsForBlock(state.data, cache)
check:
attestations.len == 1
@ -341,7 +341,7 @@ suite "Attestation pool processing" & preset():
defaultRuntimeConfig, state.data,
MIN_ATTESTATION_INCLUSION_DELAY.Slot + 1, cache, rewards, {})
let attestations = pool[].getAttestationsForTestBlock(state[], cache)
let attestations = pool[].getAttestationsForBlock(state.data, cache)
check:
attestations.len == 1
@ -369,7 +369,7 @@ suite "Attestation pool processing" & preset():
defaultRuntimeConfig, state.data,
MIN_ATTESTATION_INCLUSION_DELAY.Slot + 1, cache, rewards, {})
let attestations = pool[].getAttestationsForTestBlock(state[], cache)
let attestations = pool[].getAttestationsForBlock(state.data, cache)
check:
attestations.len == 1

View File

@ -8,11 +8,9 @@
import
chronicles,
options, stew/endians2,
../beacon_chain/[beacon_node_types],
../beacon_chain/validators/validator_pool,
../beacon_chain/spec/datatypes/merge,
../beacon_chain/spec/[helpers, signatures, state_transition, forks],
../beacon_chain/consensus_object_pools/attestation_pool
../beacon_chain/spec/[helpers, signatures, state_transition, forks]
type
MockPrivKeysT = object
@ -318,11 +316,3 @@ iterator makeTestBlocks*(
state, parent_root, cache, attestations = attestations, cfg = cfg)
yield blck
parent_root = blck.root
proc getAttestationsForTestBlock*(
pool: var AttestationPool, stateData: StateData, cache: var StateCache):
seq[Attestation] =
case stateData.data.beaconStateFork:
of forkPhase0: pool.getAttestationsForBlock(stateData.data.hbsPhase0, cache)
of forkAltair: pool.getAttestationsForBlock(stateData.data.hbsAltair, cache)
of forkMerge: pool.getAttestationsForBlock(stateData.data.hbsMerge, cache)