cuprated: event-driven block synchronization#584
Open
redsh4de wants to merge 6 commits intoCuprate:mainfrom
Open
cuprated: event-driven block synchronization#584redsh4de wants to merge 6 commits intoCuprate:mainfrom
redsh4de wants to merge 6 commits intoCuprate:mainfrom
Conversation
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.
What
Make blockchain synchronization event-driven instead of polling every 30s. This makes new sync initiation near-instant as it respond to relevant events. Closes #532.
Why
The syncer previously polled every 30 seconds to check if we are behind our peers. This meant up to a 30-second delay before reacting to network's cumulative difficulty changes. Also unnecessary work when nothing had changed
Where
p2p/p2p-core/src/client.rs- newPeerSyncCallbackstructp2p/p2p-core/src/client/handshaker.rs,builder.rs,request_handler.rs,timeout_monitor.rs- plumbingp2p/p2p/src/lib.rs,inbound_server.rs,connection_maintainer.rs- notify syncer on 0 peer -> 1+ peer transitionsconsensus/context/src/lib.rs- add a read function for our cumulative difficulty for use in the callbackbinaries/cuprated/src/main.rs,p2p.rs,p2p/request_handler.rs,blockchain/manager.rs,blockchain/syncer.rs- wire everything together, add loop label for clarity forbreaksHow
Replace the 30-second polling interval with a
PeerSyncCallbackstruct that wraps atokio::sync::Notifywith three waking paths:call): When a peer reports new sync data, wake the syncer only if the peer's cumulative difficulty exceeds ours.wake_unconditionally): When we receive an orphan block, wake the syncer immediately.wake_on_first_peers): When transitioning from 0 to 1+ connected peers, wake the syncer immediately. Uses a guard that the syncer re-arms (wake_on_first_peers_arm) each time it enters theNoPeersstate, so the mechanism works across repeated complete disconnect -> connect cycles.The syncer checks sync status, then either awaits a notification (
Synced/NoPeers) or starts the inner block downloader loop (BehindPeers).After the downloader loop ends, the outer syncer loop re-checks status again before parking. The
synced_notifyfor the Tor P2P zone start is now fired only once on initial sync completion rather than repeatedly.