-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat/blocks: add a stream for all blocks #5455
Changes from all commits
0c8037d
6ae48d3
c5e6efd
6edd447
738cfad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,6 +95,7 @@ pub struct Client<B, E, Block, RA> where Block: BlockT { | |
| storage_notifications: Mutex<StorageNotifications<Block>>, | ||
| import_notification_sinks: Mutex<Vec<mpsc::UnboundedSender<BlockImportNotification<Block>>>>, | ||
| finality_notification_sinks: Mutex<Vec<mpsc::UnboundedSender<FinalityNotification<Block>>>>, | ||
| all_blocks_notification_sinks: Mutex<Vec<mpsc::UnboundedSender<AllBlocksNotification<Block>>>>, | ||
| // holds the block hash currently being imported. TODO: replace this with block queue | ||
| importing_block: RwLock<Option<Block::Hash>>, | ||
| block_rules: BlockRules<Block>, | ||
|
|
@@ -279,6 +280,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where | |
| storage_notifications: Default::default(), | ||
| import_notification_sinks: Default::default(), | ||
| finality_notification_sinks: Default::default(), | ||
| all_blocks_notification_sinks: Default::default(), | ||
| importing_block: Default::default(), | ||
| block_rules: BlockRules::new(fork_blocks, bad_blocks), | ||
| execution_extensions, | ||
|
|
@@ -742,21 +744,30 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where | |
|
|
||
| operation.op.insert_aux(aux)?; | ||
|
|
||
|
|
||
| let header = import_headers.into_post(); | ||
|
Contributor
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. Might be worth to construct this lazily for
Contributor
Author
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. Passing less data is imho not possible since
Contributor
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. In case there can be many all-blocks-notification-streams one could pass an We do similar things for the |
||
| let import_summary = ImportSummary { | ||
| hash, | ||
| origin, | ||
| header, | ||
| is_new_best, | ||
| storage_changes, | ||
| retracted, | ||
| }; | ||
|
|
||
| self.notify_any_block_imported( | ||
| &import_summary | ||
| )?; | ||
|
|
||
| if make_notifications { | ||
| if finalized { | ||
| operation.notify_finalized.push(hash); | ||
| } | ||
|
|
||
| operation.notify_imported = Some(ImportSummary { | ||
| hash, | ||
| origin, | ||
| header: import_headers.into_post(), | ||
| is_new_best, | ||
| storage_changes, | ||
| retracted, | ||
| }) | ||
| operation.notify_imported = Some(import_summary); | ||
| } | ||
|
|
||
|
|
||
| Ok(ImportResult::imported(is_new_best)) | ||
| } | ||
|
|
||
|
|
@@ -971,6 +982,20 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where | |
| Ok(()) | ||
| } | ||
|
|
||
| fn notify_any_block_imported(&self, notify_import: &ImportSummary<Block>) -> sp_blockchain::Result<()> { | ||
| let notification = AllBlocksNotification::<Block> { | ||
| hash: notify_import.hash.clone(), | ||
| origin: notify_import.origin.clone(), | ||
| header: notify_import.header.clone(), | ||
| is_new_best: notify_import.is_new_best, | ||
| }; | ||
|
|
||
| self.all_blocks_notification_sinks.lock() | ||
| .retain(|sink| sink.unbounded_send(notification.clone()).is_ok()); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Attempts to revert the chain by `n` blocks guaranteeing that no block is | ||
| /// reverted past the last finalized block. Returns the number of blocks | ||
| /// that were successfully reverted. | ||
|
|
@@ -1775,6 +1800,12 @@ where | |
| stream | ||
| } | ||
|
|
||
| fn all_blocks_notification_stream(&self) -> AllBlocksNotifications<Block> { | ||
| let (sink, stream) = mpsc::unbounded(); | ||
| self.all_blocks_notification_sinks.lock().push(sink); | ||
| stream | ||
| } | ||
|
|
||
| /// Get storage changes event stream. | ||
| fn storage_changes_notification_stream( | ||
| &self, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm quite hesitant on adding another
Unboundedchannel here. This may become very unreliable, imagine a case where:The whole scheme is then useless, cause even when we receive the notification the state is already lost. Also if the node is turned off the whole content of the channel is lost, so we will essentially miss running for some blocks.
If we want to make it super reliable and make sure that EACH and EVERY block is indexed/processed we need to come up with a more sophisticated mechanism.
I imagine something like LMAX-Disruptor scheme, where we:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.