cuprated: add node event streaming API (Node::events())#10
Open
orrinfrazier wants to merge 1 commit into
Open
Conversation
Add a NodeEvent enum and Node::events() returning a subscribable
NodeEventListener so embedders can observe node activity without polling.
- New events module: NodeEvent { NewBlock, Reorg }, a pub(crate)
NodeEventSender wrapping tokio::sync::broadcast (cap 256), and a public
NodeEventListener (recv/try_recv/resubscribe).
- Sender threaded through LaunchContext into the BlockchainManager;
Node::events() returns a fresh forward-looking listener.
- NewBlock is published from add_valid_block_to_main_chain only for live
BlockSource::Incoming blocks (peer relay or RPC submit_block); Reorg is
published from try_do_reorg on success. Batch-sync and reorg re-applies
do not emit NewBlock.
Implements the event-streaming item of the Cuprate#616 tracking issue and the
Cuprate#554 proposal.
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.
Closes #20
Summary
NodeEventenum andNode::events()returning a subscribableNodeEventListener, so embedders of thecupratedlibrary can observe node activity (new blocks, reorgs) without polling.cupratedas a library Cuprate/cuprate#554 API proposal — the gap left afterNode::launch()(cuprated: embeddable node library withNode::launch()Cuprate/cuprate#592), where embedders held service handles but had no push-based notifications.Changes
Piece 1 — event types + Node plumbing (
events.rs,lib.rs)eventsmodule:NodeEvent { NewBlock { height, hash }, Reorg { split_height, new_top_hash, new_chain_height } }; apub(crate) NodeEventSenderwrappingtokio::sync::broadcast(capacity 256); a publicNodeEventListener(recv/try_recv/resubscribe).launch(), stored inLaunchContext, andNode::events()returns a fresh, forward-looking listener.Piece 2 — manager publishes events (
manager.rs,manager/handler.rs)BlockchainManagergains anode_eventssender (clone of the launch sender — single shared channel).NewBlockis published fromadd_valid_block_to_main_chainonly for liveBlockSource::Incomingblocks (peer relay or RPCsubmit_block) — mirroring the existing peer-broadcast filter.Reorgis published fromtry_do_reorgon the success arm.Design decisions
broadcast, notwatch— discrete events with multiple independent consumers;watchwould drop blocks.NewBlockexcludes batch-sync and reorg re-applies — initial-sync progress is already observable via the existingnode.syncerhandle (target_height()/wait_for_synced()), and a reorg surfaces as a singleReorgevent. This keeps the stream low-noise and non-overlapping.SyncProgress/Synced(already pollable viaSyncerHandle) andPeerConnected/Disconnected(no clean source exists incuprated— would requirecuprate-p2pchanges). Good follow-ups once there's demand.Testing
mock_managerharness): adding a live block emits exactly oneNewBlock{ height: 1, hash }; a reorg emits aReorg{ split_height: 2, .. }and noNewBlockfor re-applied blocks.cargo clippy --package cuprated --all-targets -- -D warningsandcargo fmt --checkclean. No new dependencies (tokioalready present).How to test
Reviewer notes
Reviewed via 3-way cross-family consensus (Claude + Gemini + Codex) with an opus adjudicator: 0 blockers. One low doc nit was accepted and applied (the
NewBlockdoc now notesIncomingincludes locally-submitted RPC blocks, not only peer relay). One finding (a claim thatresubscribe()inherits lag state) was rejected — tokio'sresubscribeis forward-looking from the current tail.Related: Cuprate#616 (tracking), Cuprate#554 (API proposal), Cuprate#592 (merged launch).