-
Notifications
You must be signed in to change notification settings - Fork 282
Extend the new api.blocks() to be the primary way to subscribe and fetch blocks/extrinsics/events
#691
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
Merged
Merged
Extend the new api.blocks() to be the primary way to subscribe and fetch blocks/extrinsics/events
#691
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f719231
First pass adding functions to get blocks and extrinsics
jsdw 727f8fc
Merge branch 'master' into jsdw-blocks
jsdw 8b1285f
cargo fmt and cache block events
jsdw 51af76c
prefix block hash with 0x
jsdw 036d333
pin streams for better ergonomics and add an example of subscribing t…
jsdw da74c7b
remove unused var
jsdw fea170c
standardise on _all, _best and _finalized for different block header …
jsdw ba85a92
WIP center subscribing around blocks
jsdw c9080f1
Remove the event filtering/subscribing stuff
jsdw 16dd212
clippy
jsdw f967ec9
we need tokio, silly clippy
jsdw 85ad19c
add extrinsic_index() call
jsdw 0326e63
Update subxt/src/blocks/block_types.rs
jsdw cda4dd0
Merge branch 'master' into jsdw-blocks
jsdw 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
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 |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| // Copyright 2019-2022 Parity Technologies (UK) Ltd. | ||
| // This file is dual-licensed as Apache-2.0 or GPL-3.0. | ||
| // see LICENSE for license details. | ||
|
|
||
| use crate::{ | ||
| Config, | ||
| error::{ | ||
| Error, | ||
| }, | ||
| client::{ | ||
| OfflineClientT, | ||
| OnlineClientT, | ||
| }, | ||
| rpc::{ | ||
| ChainBlockResponse, | ||
| }, | ||
| events | ||
| }; | ||
| use derivative::Derivative; | ||
| use sp_runtime::traits::Hash; | ||
|
|
||
| /// A representation of a block from which you can obtain details | ||
| /// including the block header, extrinsics and events for the block. | ||
| pub struct Block<T: Config, C> { | ||
| hash: T::Hash, | ||
| details: ChainBlockResponse<T>, | ||
| client: C, | ||
| } | ||
|
|
||
| impl <T, C> Block<T, C> | ||
| where | ||
| T: Config, | ||
| C: OfflineClientT<T> | ||
| { | ||
| pub (crate) fn new( | ||
| hash: T::Hash, | ||
| details: ChainBlockResponse<T>, | ||
| client: C | ||
| ) -> Self { | ||
| Block { | ||
| hash, details, client | ||
| } | ||
| } | ||
|
|
||
| /// Return the block hash. | ||
| pub fn hash(&self) -> T::Hash { | ||
| self.hash | ||
| } | ||
|
|
||
| /// Return the block header. | ||
| pub fn header(&self) -> &T::Header { | ||
| &self.details.block.header | ||
| } | ||
|
|
||
| /// Returns an iterator over the extrinsics in the block. | ||
| pub fn extrinsics<'a>(&'a self) -> impl Iterator<Item = Extrinsic<'a, T, C>> { | ||
| self.details.block.extrinsics.iter().enumerate().map(|(idx, e)| { | ||
| Extrinsic { | ||
| index: idx as u32, | ||
| bytes: &e.0, | ||
| client: self.client.clone(), | ||
| block_hash: self.hash, | ||
| _marker: std::marker::PhantomData | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// A single extrinsic in a block. | ||
| pub struct Extrinsic<'a, T: Config, C> { | ||
| index: u32, | ||
| bytes: &'a [u8], | ||
| client: C, | ||
| block_hash: T::Hash, | ||
| _marker: std::marker::PhantomData<T> | ||
| } | ||
|
|
||
| impl <'a, T, C> Extrinsic<'a, T, C> | ||
| where | ||
| T: Config, | ||
| C: OfflineClientT<T> | ||
| { | ||
| /// The index of the extrinsic in the block. | ||
| pub fn index(&self) -> u32 { | ||
| self.index | ||
| } | ||
|
|
||
| /// The bytes of the extrinsic. | ||
| pub fn bytes(&self) -> &'a [u8] { | ||
| &self.bytes | ||
| } | ||
| } | ||
|
|
||
| impl <'a, T, C> Extrinsic<'a, T, C> | ||
| where | ||
| T: Config, | ||
| C: OnlineClientT<T> | ||
| { | ||
| /// The events associated with the extrinsic. | ||
| pub async fn events(&self) -> Result<ExtrinsicEvents<T>, Error> { | ||
| let ext_hash = T::Hashing::hash_of(&self.bytes); | ||
| let events = events::EventsClient::new(self.client.clone()) | ||
| .at(Some(self.block_hash)) | ||
| .await?; | ||
|
|
||
| Ok(ExtrinsicEvents::new(ext_hash, self.index, events)) | ||
| } | ||
| } | ||
|
|
||
| /// The events associated with a given extrinsic. | ||
| #[derive(Derivative)] | ||
| #[derivative(Debug(bound = ""))] | ||
| pub struct ExtrinsicEvents<T: Config> { | ||
| // The hash of the extrinsic (handy to expose here because | ||
| // this type is returned from TxProgress things in the most | ||
| // basic flows, so it's the only place people can access it | ||
| // without complicating things for themselves). | ||
| ext_hash: T::Hash, | ||
| // The index of the extrinsic: | ||
| idx: u32, | ||
jsdw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // All of the events in the block: | ||
| events: events::Events<T> | ||
| } | ||
|
|
||
| impl<T: Config> ExtrinsicEvents<T> { | ||
| pub (crate) fn new( | ||
| ext_hash: T::Hash, | ||
| idx: u32, | ||
| events: events::Events<T> | ||
| ) -> Self { | ||
| Self { ext_hash, idx, events } | ||
| } | ||
|
|
||
| /// Return the hash of the block that the extrinsic is in. | ||
| pub fn block_hash(&self) -> T::Hash { | ||
| self.events.block_hash() | ||
| } | ||
|
|
||
| /// Return the hash of the extrinsic. | ||
| pub fn extrinsic_hash(&self) -> T::Hash { | ||
| self.ext_hash | ||
| } | ||
|
|
||
| /// Return all of the events in the block that the extrinsic is in. | ||
| pub fn all_events_in_block(&self) -> &events::Events<T> { | ||
| &self.events | ||
| } | ||
|
|
||
| /// Iterate over all of the raw events associated with this transaction. | ||
| /// | ||
| /// This works in the same way that [`events::Events::iter()`] does, with the | ||
| /// exception that it filters out events not related to the submitted extrinsic. | ||
| pub fn iter(&self) -> impl Iterator<Item = Result<events::EventDetails, Error>> + '_ { | ||
| self.events.iter().filter(|ev| { | ||
| ev.as_ref() | ||
| .map(|ev| ev.phase() == events::Phase::ApplyExtrinsic(self.idx)) | ||
| .unwrap_or(true) // Keep any errors. | ||
| }) | ||
| } | ||
|
|
||
| /// Find all of the transaction events matching the event type provided as a generic parameter. | ||
| /// | ||
| /// This works in the same way that [`events::Events::find()`] does, with the | ||
| /// exception that it filters out events not related to the submitted extrinsic. | ||
| pub fn find<Ev: events::StaticEvent>(&self) -> impl Iterator<Item = Result<Ev, Error>> + '_ { | ||
| self.iter().filter_map(|ev| { | ||
| ev.and_then(|ev| ev.as_event::<Ev>().map_err(Into::into)) | ||
| .transpose() | ||
| }) | ||
| } | ||
|
|
||
| /// Iterate through the transaction events using metadata to dynamically decode and skip | ||
| /// them, and return the first event found which decodes to the provided `Ev` type. | ||
| /// | ||
| /// This works in the same way that [`events::Events::find_first()`] does, with the | ||
| /// exception that it ignores events not related to the submitted extrinsic. | ||
| pub fn find_first<Ev: events::StaticEvent>(&self) -> Result<Option<Ev>, Error> { | ||
| self.find::<Ev>().next().transpose() | ||
| } | ||
|
|
||
| /// Find an event in those associated with this transaction. Returns true if it was found. | ||
| /// | ||
| /// This works in the same way that [`events::Events::has()`] does, with the | ||
| /// exception that it ignores events not related to the submitted extrinsic. | ||
| pub fn has<Ev: events::StaticEvent>(&self) -> Result<bool, Error> { | ||
| Ok(self.find::<Ev>().next().transpose()?.is_some()) | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.