-
Notifications
You must be signed in to change notification settings - Fork 300
add payload attestation pool #7730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d68a43b
add payload attestation pool
Tomi-3-0 abf5e3b
fix for get_ptc iterator
Tomi-3-0 a97f4ad
install payload attestation message handler
Tomi-3-0 1b97ece
replace iterator with direct check
Tomi-3-0 cf11566
simplify table lookup
Tomi-3-0 a389c2b
merge unstable
Tomi-3-0 948a57d
Merge branch 'unstable' of github.com:status-im/nimbus-eth2 into ZZO
Tomi-3-0 a2bb3e4
update payload attestation pool test
Tomi-3-0 d237ce6
use updated get_ptc iterator
Tomi-3-0 54a291b
address reviews
Tomi-3-0 d63e7e8
add comment to issue
Tomi-3-0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
beacon_chain/consensus_object_pools/payload_attestation_pool.nim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| # beacon_chain | ||
| # Copyright (c) 2025 Status Research & Development GmbH | ||
| # Licensed and distributed under either of | ||
| # * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). | ||
| # * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). | ||
| # at your option. This file may not be copied, modified, or distributed except according to those terms. | ||
|
|
||
| {.push raises: [], gcsafe.} | ||
|
|
||
| import | ||
| # Status libraries | ||
| metrics, | ||
| chronicles, | ||
| # Internal | ||
| ../spec/[eth2_merkleization, forks, validator], | ||
| "."/[spec_cache, blockchain_dag], | ||
| ../beacon_clock | ||
|
|
||
| from ../spec/beaconstate import get_ptc | ||
|
|
||
| logScope: topics = "payattpool" | ||
|
|
||
| declareGauge payload_attestation_pool_block_packing_time, | ||
| "Time it took to create list of payload attestations for block" | ||
|
|
||
| type | ||
| PayloadAttestationEntry* = object | ||
| data*: PayloadAttestationData | ||
| messages*: Table[ValidatorIndex, PayloadAttestationMessage] | ||
| aggregated*: Opt[PayloadAttestation] | ||
|
|
||
| PayloadAttestationPool* = object | ||
| dag*: ChainDAGRef | ||
| attestations*: Table[Slot, Table[Eth2Digest, PayloadAttestationEntry]] | ||
|
|
||
| func init*(T: type PayloadAttestationPool, dag: ChainDAGRef): T = | ||
| T(dag: dag) | ||
|
|
||
| func pruneOldEntries(pool: var PayloadAttestationPool, wallTime: BeaconTime) = | ||
| let current_slot = wallTime.slotOrZero(pool.dag.timeParams) | ||
|
|
||
| # keep only recent slots - since payload attestations | ||
| # are only valid for 1 slot | ||
| var slotsToRemove: seq[Slot] | ||
| for slot in pool.attestations.keys: | ||
| if slot + 2 < current_slot: | ||
| slotsToRemove.add(slot) | ||
|
|
||
| for slot in slotsToRemove: | ||
| pool.attestations.del(slot) | ||
|
|
||
| proc addPayloadAttestation*( | ||
| pool: var PayloadAttestationPool, message: PayloadAttestationMessage, | ||
| wallTime: BeaconTime): bool = | ||
| let | ||
| slot = message.data.slot | ||
| beacon_block_root = message.data.beacon_block_root | ||
tersec marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| validator_index = message.validator_index | ||
|
|
||
| pool.pruneOldEntries(wallTime) | ||
|
|
||
| # create an entry for this block and slot | ||
| let | ||
| entry = addr pool.attestations.mgetOrPut(slot).mgetOrPut( | ||
| beacon_block_root, PayloadAttestationEntry(data: message.data)) | ||
|
|
||
| # Check for duplicate | ||
| let vidx = ValidatorIndex(validator_index) | ||
| if vidx in entry[].messages: | ||
| return false | ||
|
|
||
| entry[].messages[vidx] = message | ||
|
|
||
| entry[].aggregated = Opt.none(PayloadAttestation) | ||
|
|
||
| true | ||
|
|
||
| proc aggregateMessages( | ||
tersec marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pool: PayloadAttestationPool, slot: Slot, | ||
| entry: var PayloadAttestationEntry, cache: var StateCache | ||
| ): Opt[PayloadAttestation] = | ||
|
|
||
| if entry.messages.len == 0: | ||
| return Opt.none(PayloadAttestation) | ||
|
|
||
| withState(pool.dag.headState): | ||
| when consensusFork >= ConsensusFork.Gloas: | ||
| var | ||
| aggregation_bits: BitArray[int(PTC_SIZE)] | ||
| signatures: seq[CookedSig] | ||
| ptc_index = 0 | ||
|
|
||
| for ptc_validator_index in get_ptc(forkyState.data, slot, cache): | ||
| entry.messages.withValue(ptc_validator_index, message): | ||
| let cookedSig = message[].signature.load().valueOr: | ||
| continue | ||
| aggregation_bits[ptc_index] = true | ||
| signatures.add(cookedSig) | ||
| ptc_index += 1 | ||
|
|
||
| if signatures.len == 0: | ||
| return Opt.none(PayloadAttestation) | ||
|
|
||
| var aggregated_signature = AggregateSignature.init(signatures[0]) | ||
| for i in 1..<signatures.len: | ||
| aggregated_signature.aggregate(signatures[i]) | ||
|
|
||
| Opt.some(PayloadAttestation( | ||
| aggregation_bits: aggregation_bits, | ||
| data: entry.data, | ||
| signature: aggregated_signature.finish().toValidatorSig() | ||
| )) | ||
| else: | ||
| Opt.none(PayloadAttestation) | ||
|
|
||
| proc getAggregatedPayloadAttestation*( | ||
tersec marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pool: var PayloadAttestationPool, slot: Slot, | ||
| beacon_block_root: Eth2Digest, cache: var StateCache | ||
| ): Opt[PayloadAttestation] = | ||
| ## Get aggregated payload attestation for a specific block and slot | ||
|
|
||
| pool.attestations.withValue(slot, slotEntries): | ||
| slotEntries[].withValue(beacon_block_root, entry): | ||
| if entry[].aggregated.isNone(): | ||
| entry[].aggregated = pool.aggregateMessages(slot, entry[], cache) | ||
| return entry[].aggregated | ||
|
|
||
| Opt.none(PayloadAttestation) | ||
|
|
||
| proc getPayloadAttestationsForBlock*( | ||
| pool: var PayloadAttestationPool, target_slot: Slot, | ||
| cache: var StateCache): seq[PayloadAttestation] = | ||
| ## Get payload attestations to include in a block for a target slot | ||
| let startPackingTick = Moment.now() | ||
|
|
||
| if target_slot == 0: | ||
| return @[] | ||
|
|
||
| let attestation_slot = target_slot - 1 | ||
|
|
||
| if attestation_slot notin pool.attestations: | ||
tersec marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return @[] | ||
|
|
||
| var | ||
| payload_attestations: seq[PayloadAttestation] | ||
| totalCandidates = 0 | ||
|
|
||
| pool.attestations.withValue(attestation_slot, slotEntries): | ||
| for beacon_block_root, entry in slotEntries[]: | ||
| totalCandidates += 1 | ||
| let aggregated = | ||
| pool.getAggregatedPayloadAttestation( | ||
| attestation_slot, beacon_block_root, cache) | ||
| if aggregated.isSome(): | ||
| payload_attestations.add(aggregated.get()) | ||
| if payload_attestations.len >= MAX_PAYLOAD_ATTESTATIONS.int: | ||
| break | ||
|
|
||
| let packingDur = Moment.now() - startPackingTick | ||
|
|
||
| debug "Packed payload attestations for block", | ||
| target_slot = target_slot, attestation_slot = attestation_slot, | ||
| packingDur = packingDur, totalCandidates = totalCandidates, | ||
| payload_attestations = payload_attestations.len() | ||
|
|
||
| payload_attestation_pool_block_packing_time.set(packingDur.toFloatSeconds()) | ||
|
|
||
| payload_attestations | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.