-
Notifications
You must be signed in to change notification settings - Fork 258
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
Make subxt-core ready for publishing #1508
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
adf1f0d
Move Extrinsic decoding things to subxt_core and various tidy-ups
jsdw 5076651
A couple more fixes and fmt
jsdw 49268fd
first pass moving tx logic to subxt_core
jsdw b571bb1
cargo fmt
jsdw 6f54031
fix wasm example
jsdw b9b63b4
clippy
jsdw 1ba85f5
more clippy
jsdw 65345a7
WIP Adding examples and such
jsdw d22338a
Move storage functionality more fully to subxt_core and nice examples…
jsdw 0a33d38
Add example for events
jsdw b3c782b
consistify how addresses/payloads are exposed in subxt-core and add r…
jsdw 4ef4585
Add runtime API core example
jsdw a164951
fmt
jsdw 191237d
remove scale-info patch
jsdw e9e59d3
Add a little to the top level docs
jsdw 42fc42a
Merge branch 'master' into jsdw-subxt-core-polish
jsdw e8a96ac
swap args around
jsdw 44b91e6
clippy
jsdw 53b7d8e
Merge branch 'master' into jsdw-subxt-core-polish
jsdw 6e42e91
cargo fmt and fix wasm-example
jsdw 2d98704
doc fixes
jsdw c2987f2
no-std-ise new subxt-core additions
jsdw 8b8bfec
alloc, not core
jsdw b518286
more no-std fixes
jsdw 50db863
A couple more fixes
jsdw 75eb502
Merge branch 'master' into jsdw-subxt-core-polish
jsdw 1c6b55f
Add back extrinsic decode test
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file contains 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 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 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 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 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 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 |
---|---|---|
|
@@ -224,7 +224,16 @@ impl CodegenBuilder { | |
} | ||
|
||
/// Set the path to the `subxt` crate. By default, we expect it to be at `::subxt::ext::subxt_core`. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if the path provided is not an absolute path. | ||
pub fn set_subxt_crate_path(&mut self, crate_path: syn::Path) { | ||
if absolute_path(crate_path.clone()).is_err() { | ||
// Throw an error here, because otherwise we end up with a harder to comprehend error when | ||
// substitute types don't begin with an absolute path. | ||
panic!("The provided crate path must be an absolute path, ie prefixed with '::' or 'crate'"); | ||
} | ||
Comment on lines
+232
to
+236
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. We shouldn't need to clone crate_path here but need to change scale-typegen to avoid. |
||
self.crate_path = crate_path; | ||
} | ||
|
||
|
This file contains 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 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,169 @@ | ||
// Copyright 2019-2024 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::signed_extensions::{ | ||
ChargeAssetTxPayment, ChargeTransactionPayment, CheckNonce, | ||
}; | ||
use crate::config::SignedExtension; | ||
use crate::dynamic::Value; | ||
use crate::{config::Config, error::Error, Metadata}; | ||
use scale_decode::DecodeAsType; | ||
|
||
/// The signed extensions of an extrinsic. | ||
#[derive(Debug, Clone)] | ||
pub struct ExtrinsicSignedExtensions<'a, T: Config> { | ||
bytes: &'a [u8], | ||
metadata: &'a Metadata, | ||
_marker: core::marker::PhantomData<T>, | ||
} | ||
|
||
impl<'a, T: Config> ExtrinsicSignedExtensions<'a, T> { | ||
pub(crate) fn new(bytes: &'a [u8], metadata: &'a Metadata) -> Self { | ||
Self { | ||
bytes, | ||
metadata, | ||
_marker: core::marker::PhantomData, | ||
} | ||
} | ||
|
||
/// Returns an iterator over each of the signed extension details of the extrinsic. | ||
/// If the decoding of any signed extension fails, an error item is yielded and the iterator stops. | ||
pub fn iter(&self) -> impl Iterator<Item = Result<ExtrinsicSignedExtension<T>, Error>> { | ||
let signed_extension_types = self.metadata.extrinsic().signed_extensions(); | ||
let num_signed_extensions = signed_extension_types.len(); | ||
let bytes = self.bytes; | ||
let mut index = 0; | ||
let mut byte_start_idx = 0; | ||
let metadata = &self.metadata; | ||
|
||
core::iter::from_fn(move || { | ||
if index == num_signed_extensions { | ||
return None; | ||
} | ||
|
||
let extension = &signed_extension_types[index]; | ||
let ty_id = extension.extra_ty(); | ||
let cursor = &mut &bytes[byte_start_idx..]; | ||
if let Err(err) = scale_decode::visitor::decode_with_visitor( | ||
cursor, | ||
&ty_id, | ||
metadata.types(), | ||
scale_decode::visitor::IgnoreVisitor::new(), | ||
) | ||
.map_err(|e| Error::Decode(e.into())) | ||
{ | ||
index = num_signed_extensions; // (such that None is returned in next iteration) | ||
return Some(Err(err)); | ||
} | ||
let byte_end_idx = bytes.len() - cursor.len(); | ||
let bytes = &bytes[byte_start_idx..byte_end_idx]; | ||
byte_start_idx = byte_end_idx; | ||
index += 1; | ||
Some(Ok(ExtrinsicSignedExtension { | ||
bytes, | ||
ty_id, | ||
identifier: extension.identifier(), | ||
metadata, | ||
_marker: core::marker::PhantomData, | ||
})) | ||
}) | ||
} | ||
|
||
/// Searches through all signed extensions to find a specific one. | ||
/// If the Signed Extension is not found `Ok(None)` is returned. | ||
/// If the Signed Extension is found but decoding failed `Err(_)` is returned. | ||
pub fn find<S: SignedExtension<T>>(&self) -> Result<Option<S::Decoded>, Error> { | ||
for ext in self.iter() { | ||
// If we encounter an error while iterating, we won't get any more results | ||
// back, so just return that error as we won't find the signed ext anyway. | ||
let ext = ext?; | ||
match ext.as_signed_extension::<S>() { | ||
// We found a match; return it: | ||
Ok(Some(e)) => return Ok(Some(e)), | ||
// No error, but no match either; next! | ||
Ok(None) => continue, | ||
// Error? return it | ||
Err(e) => return Err(e), | ||
} | ||
} | ||
Ok(None) | ||
} | ||
|
||
/// The tip of an extrinsic, extracted from the ChargeTransactionPayment or ChargeAssetTxPayment | ||
/// signed extension, depending on which is present. | ||
/// | ||
/// Returns `None` if `tip` was not found or decoding failed. | ||
pub fn tip(&self) -> Option<u128> { | ||
// Note: the overhead of iterating multiple time should be negligible. | ||
self.find::<ChargeTransactionPayment>() | ||
.ok() | ||
.flatten() | ||
.map(|e| e.tip()) | ||
.or_else(|| { | ||
self.find::<ChargeAssetTxPayment<T>>() | ||
.ok() | ||
.flatten() | ||
.map(|e| e.tip()) | ||
}) | ||
} | ||
|
||
/// The nonce of the account that submitted the extrinsic, extracted from the CheckNonce signed extension. | ||
/// | ||
/// Returns `None` if `nonce` was not found or decoding failed. | ||
pub fn nonce(&self) -> Option<u64> { | ||
self.find::<CheckNonce>().ok()? | ||
} | ||
} | ||
|
||
/// A single signed extension | ||
#[derive(Debug, Clone)] | ||
pub struct ExtrinsicSignedExtension<'a, T: Config> { | ||
bytes: &'a [u8], | ||
ty_id: u32, | ||
identifier: &'a str, | ||
metadata: &'a Metadata, | ||
_marker: core::marker::PhantomData<T>, | ||
} | ||
|
||
impl<'a, T: Config> ExtrinsicSignedExtension<'a, T> { | ||
/// The bytes representing this signed extension. | ||
pub fn bytes(&self) -> &'a [u8] { | ||
self.bytes | ||
} | ||
|
||
/// The name of the signed extension. | ||
pub fn name(&self) -> &'a str { | ||
self.identifier | ||
} | ||
|
||
/// The type id of the signed extension. | ||
pub fn type_id(&self) -> u32 { | ||
self.ty_id | ||
} | ||
|
||
/// Signed Extension as a [`scale_value::Value`] | ||
pub fn value(&self) -> Result<Value<u32>, Error> { | ||
let value = scale_value::scale::decode_as_type( | ||
&mut &self.bytes[..], | ||
&self.ty_id, | ||
self.metadata.types(), | ||
)?; | ||
Ok(value) | ||
} | ||
|
||
/// Decodes the bytes of this Signed Extension into its associated `Decoded` type. | ||
/// Returns `Ok(None)` if the data we have doesn't match the Signed Extension we're asking to | ||
/// decode with. | ||
pub fn as_signed_extension<S: SignedExtension<T>>(&self) -> Result<Option<S::Decoded>, Error> { | ||
if !S::matches(self.identifier, self.ty_id, self.metadata.types()) { | ||
return Ok(None); | ||
} | ||
self.as_type::<S::Decoded>().map(Some) | ||
} | ||
|
||
fn as_type<E: DecodeAsType>(&self) -> Result<E, Error> { | ||
let value = E::decode_as_type(&mut &self.bytes[..], &self.ty_id, self.metadata.types())?; | ||
Ok(value) | ||
} | ||
} |
Oops, something went wrong.
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.
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 reverted to just a struct so that the args (of the same type) were named
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.
This is more specific, I like it