Skip to content
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

fix: clone attestations for block inclusion #6174

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,16 @@ export class AggregatedAttestationPool {
}
}

return attestationsByScore
.sort((a, b) => b.score - a.score)
.slice(0, MAX_ATTESTATIONS)
.map((attestation) => attestation.attestation);
const sortedAttestationsByScore = attestationsByScore.sort((a, b) => b.score - a.score);
const attestationsForBlock: phase0.Attestation[] = [];
for (const [i, attestationWithScore] of sortedAttestationsByScore.entries()) {
if (i >= MAX_ATTESTATIONS) {
break;
}
// attestations could be modified in this op pool, so we need to clone for block
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one! I'm wondering if this could be tested somehow?

Also immutable data structure (à la immutable.js) could be considered if the cloning is identified as a perf issue (here or elsewhere).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one! I'm wondering if this could be tested somehow?

testing this requires setting up the pool and add attestations and produce blocks at the same time, I think it's not worth it. We do have a lot of unit tests for this file https://github.com/ChainSafe/lodestar/blob/unstable/packages/beacon-node/test/unit/chain/opPools/aggregatedAttestationPool.test.ts

Also immutable data structure (à la immutable.js) could be considered if the cloning is identified as a perf issue (here or elsewhere).

we only need to do the clone in this specific scenario, we do use immutable.js in this PR #6042 where it's really needed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!

Also might be clearer to just add the cloning part in the existing map, as map already returns a new array anyway. A matter of taste probably.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also might be clearer to just add the cloning part in the existing map

Yes I agree there. No need to be so much more verbose with named intermediate variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clone() made the code a little bit slower, so I just want to use a single for loop to hopefully keep the same performance. There was already an issue with producing slow beacon block body #5793

attestationsForBlock.push(ssz.phase0.Attestation.clone(attestationWithScore.attestation));
}
return attestationsForBlock;
}

/**
Expand Down
Loading