-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[Feat] Announce new blocks via IPC #3002
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
Open
ljedrz
wants to merge
6
commits into
ProvableHQ:staging
Choose a base branch
from
ljedrz:feat/announce_blocks_via_ipc
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
755f54d
feat: announce new blocks via IPC (feature-gated)
ljedrz 15c0771
feat: handle IPC stream restarts
ljedrz 3c1ab9c
tweak: apply review comments
ljedrz 5f3da5f
Update synthesizer/src/vm/helpers/sequential_op.rs
ljedrz 43ce45b
fix: correct a mismatched bracket
ljedrz 6cfa2f2
chore: adjust the lockfile
ljedrz 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
|---|---|---|
|
|
@@ -16,8 +16,12 @@ | |
| use crate::vm::*; | ||
| use console::network::prelude::Network; | ||
|
|
||
| #[cfg(feature = "announce-blocks")] | ||
| use interprocess::local_socket::{self, Stream, prelude::*}; | ||
| use std::{fmt, thread}; | ||
| use tokio::sync::oneshot; | ||
| #[cfg(feature = "announce-blocks")] | ||
| use tracing::*; | ||
|
|
||
| impl<N: Network, C: ConsensusStorage<N>> VM<N, C> { | ||
| /// Launches a thread dedicated to the sequential processing of storage-related | ||
|
|
@@ -26,6 +30,9 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> { | |
| &self, | ||
| request_rx: mpsc::Receiver<SequentialOperationRequest<N>>, | ||
| ) -> thread::JoinHandle<()> { | ||
| #[cfg(feature = "announce-blocks")] | ||
| let mut stream = start_block_announcement_stream(); | ||
|
|
||
| // Spawn a dedicated thread. | ||
| let vm = self.clone(); | ||
| thread::spawn(move || { | ||
|
|
@@ -37,7 +44,17 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> { | |
| // Perform the queued operation. | ||
| let ret = match op { | ||
| SequentialOperation::AddNextBlock(block) => { | ||
| #[cfg(feature = "announce-blocks")] | ||
| let ipc_payload = (block.height(), bincode::serialize(&block)); | ||
| let ret = vm.add_next_block_inner(block); | ||
| #[cfg(feature = "announce-blocks")] | ||
| if ret.is_ok() { | ||
| if let Err(e) = announce_block(&mut stream, ipc_payload) { | ||
| error!("IPC error: {e}"); | ||
| // Attempt to restart the stream. | ||
| stream = start_block_announcement_stream(); | ||
| } | ||
| } | ||
| SequentialOperationResult::AddNextBlock(ret) | ||
| } | ||
| SequentialOperation::AtomicSpeculate(a, b, c, d, e, f) => { | ||
|
|
@@ -138,3 +155,46 @@ pub enum SequentialOperationResult<N: Network> { | |
| )>, | ||
| ), | ||
| } | ||
|
|
||
| #[cfg(feature = "announce-blocks")] | ||
| fn start_block_announcement_stream() -> Option<Stream> { | ||
| let path = std::env::var("BLOCK_ANNOUNCE_PATH") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side note: We should probably have a unified source that documenst all the environment variables that are used. |
||
| .map_err(|_| { | ||
| warn!("BLOCK_ANNOUNCE_PATH env variable must be set in order to publish blocks via IPC"); | ||
| }) | ||
| .ok()? | ||
| .to_fs_name::<local_socket::GenericFilePath>() | ||
| .expect("Invalid path provided as the BLOCK_ANNOUNCE_PATH"); | ||
|
|
||
| match Stream::connect(path) { | ||
| Ok(stream) => { | ||
| debug!("Successfully (re)started the IPC stream for block announcements"); | ||
| Some(stream) | ||
| } | ||
| Err(e) => { | ||
| warn!("Couldn't (re)start the IPC stream for block announcements: {e}"); | ||
| None | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "announce-blocks")] | ||
| fn announce_block( | ||
| stream: &mut Option<Stream>, | ||
| payload: (u32, Result<Vec<u8>, Box<bincode::ErrorKind>>), | ||
| ) -> Result<bool> { | ||
| if let Some(stream) = stream { | ||
| let (block_height, serialized_block) = payload; | ||
| let block_bytes = serialized_block?; | ||
| debug!("Announcing block {block_height} to the IPC stream"); | ||
| let payload_size = u32::try_from(std::mem::size_of::<u32>() + block_bytes.len()).unwrap(); // Safe - blocks are smaller than 4GiB. | ||
| stream.write_all(&payload_size.to_le_bytes())?; | ||
| stream.write_all(&block_height.to_le_bytes())?; | ||
| stream.write_all(&block_bytes)?; | ||
|
|
||
| Ok(true) | ||
| } else { | ||
| *stream = start_block_announcement_stream(); | ||
| if stream.is_some() { announce_block(stream, payload) } else { Ok(false) } | ||
| } | ||
| } | ||
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.