refactor(sync): use deque(maxlen=N) for pending attestation buffers - #746
Merged
Conversation
Both pending attestation queues previously appended into a list and then sliced back to MAX_PENDING_ATTESTATIONS when the size exceeded the cap. Switching to collections.deque(maxlen=N) lets the standard library do the bookkeeping: each append drops the oldest entry atomically when the deque is full, so the buffer never exceeds the cap even momentarily. Same items kept and same items dropped (drop-oldest, keep-newest, insertion order preserved). The replay drain now snapshots via list(...) and clears the deque in place rather than reassigning a new list, keeping the field a deque after the drain. Five test assertions wrap the deque in list(...) for the equality comparisons that previously matched against literal lists. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The previous drain pattern snapshotted via list(...) then cleared the deque in place to avoid breaking the field's deque typing. Replacing the field with a fresh empty deque (mirroring the original list pattern of self.x = []) is shorter and keeps the deque type without the list conversion. Behaviour unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Both pending attestation queues in
SyncServicepreviously appended into alistand then sliced back toMAX_PENDING_ATTESTATIONSwhenever the size exceeded the cap. Switching tocollections.deque(maxlen=N)lets the standard library do the bookkeeping: eachappenddrops the oldest entry atomically when the deque is full, so the buffer never exceeds the cap even momentarily.Behaviour
Identical: drop-oldest, keep-newest, insertion order preserved.
append(new)grows to MAX+1 → slice[-MAX:]drops index 0.append(new)atomically drops index 0 and pushes new on the right.Same items kept, same items dropped, same final ordering.
Replay drain
pending = self._pending; self._pending = []would have replaced the deque with a plain list. Switched topending = list(self._pending); self._pending.clear()so the field stays a deque after the drain. Failed retries re-append into the empty deque just like before.Tests
Five equality assertions wrap the deque in
list(...)becausedeque == listisFalsein Python. The trim-bound tests (test_pending_attestations_trimmed_to_max,test_pending_aggregated_trimmed_to_max) — which feedMAX + 50items and assert the final length is exactlyMAXwith the last item being the newest — pass without modification.Test plan
ruff check— clean.uv run pytest tests/lean_spec/subspecs/sync/test_service.py— 34 tests pass.🤖 Generated with Claude Code