Skip to content
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

scale-type-resolver integration #1460

Merged
merged 13 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ proc-macro2 = "1.0.78"
quote = "1.0.35"
regex = "1.10.3"
scale-info = { version = "2.10.0", default-features = false }
scale-value = "0.13.0"
scale-bits = "0.4.0"
scale-decode = "0.10.0"
scale-encode = "0.5.0"
scale-value = "0.14.0"
scale-bits = "0.5.0"
scale-decode = "0.11.1"
scale-encode = "0.6.0"
serde = { version = "1.0.197" }
serde_json = { version = "1.0.114" }
syn = { version = "2.0.15", features = ["full", "extra-traits"] }
Expand All @@ -98,8 +98,8 @@ url = "2.5.0"
wabt = "0.10.0"
wasm-bindgen-test = "0.3.24"
which = "5.0.0"
scale-typegen-description = "0.1.0"
scale-typegen = "0.1.1"
scale-typegen-description = "0.1.2"
scale-typegen = "0.1.2"
strip-ansi-escapes = "0.2.0"

# Light client support:
Expand Down
7 changes: 5 additions & 2 deletions cli/src/commands/explore/pallets/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ pub fn explore_constants(
.highlight();

// value
let value =
scale_value::scale::decode_as_type(&mut constant.value(), constant.ty(), metadata.types())?;
let value = scale_value::scale::decode_as_type(
&mut constant.value(),
&constant.ty(),
metadata.types(),
)?;
let value = format_scale_value(&value).indent(4);

writedoc!(
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/explore/pallets/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub async fn explore_storage(
{value_str}
"}?;

let key_bytes = value.encode_as_type(type_id, metadata.types())?;
let key_bytes = value.encode_as_type(&type_id, metadata.types())?;
let bytes_composite = Value::from_bytes(key_bytes);
vec![bytes_composite]
}
Expand Down
5 changes: 2 additions & 3 deletions cli/src/commands/explore/runtime_apis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,8 @@ pub async fn run<'a>(
{value_str}
"}?;
// encode, then decode. This ensures that the scale value is of the correct shape for the param:
let bytes = value.encode_as_type(ty.ty, metadata.types())?;
let value = Value::decode_as_type(&mut &bytes[..], ty.ty, metadata.types())?
.map_context(|_| ());
let bytes = value.encode_as_type(&ty.ty, metadata.types())?;
let value = Value::decode_as_type(&mut &bytes[..], &ty.ty, metadata.types())?;
Ok(value)
})
.collect::<color_eyre::Result<Vec<Value>>>()?;
Expand Down
2 changes: 1 addition & 1 deletion subxt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,4 @@ features = ["default", "substrate-compat", "unstable-light-client"]
rustdoc-args = ["--cfg", "docsrs"]

[package.metadata.playground]
features = ["default", "substrate-compat", "unstable-light-client"]
features = ["default", "substrate-compat", "unstable-light-client"]
38 changes: 21 additions & 17 deletions subxt/src/blocks/extrinsic_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,27 +245,27 @@ where
// Skip over the address, signature and extra fields.
scale_decode::visitor::decode_with_visitor(
cursor,
ids.address,
&ids.address,
metadata.types(),
scale_decode::visitor::IgnoreVisitor,
scale_decode::visitor::IgnoreVisitor::new(),
)
.map_err(scale_decode::Error::from)?;
let address_end_idx = bytes.len() - cursor.len();

scale_decode::visitor::decode_with_visitor(
cursor,
ids.signature,
&ids.signature,
metadata.types(),
scale_decode::visitor::IgnoreVisitor,
scale_decode::visitor::IgnoreVisitor::new(),
)
.map_err(scale_decode::Error::from)?;
let signature_end_idx = bytes.len() - cursor.len();

scale_decode::visitor::decode_with_visitor(
cursor,
ids.extra,
&ids.extra,
metadata.types(),
scale_decode::visitor::IgnoreVisitor,
scale_decode::visitor::IgnoreVisitor::new(),
)
.map_err(scale_decode::Error::from)?;
let extra_end_idx = bytes.len() - cursor.len();
Expand Down Expand Up @@ -420,18 +420,16 @@ where

/// Decode and provide the extrinsic fields back in the form of a [`scale_value::Composite`]
/// type which represents the named or unnamed fields that were present in the extrinsic.
pub fn field_values(
&self,
) -> Result<scale_value::Composite<scale_value::scale::TypeId>, Error> {
jsdw marked this conversation as resolved.
Show resolved Hide resolved
pub fn field_values(&self) -> Result<scale_value::Composite<()>, Error> {
let bytes = &mut self.field_bytes();
let extrinsic_metadata = self.extrinsic_metadata()?;

let mut fields = extrinsic_metadata
.variant
.fields
.iter()
.map(|f| scale_decode::Field::new(f.ty.id, f.name.as_deref()));
let decoded = <scale_value::Composite<scale_value::scale::TypeId>>::decode_as_fields(
.map(|f| scale_decode::Field::new(&f.ty.id, f.name.as_deref()));
let decoded = <scale_value::Composite<()>>::decode_as_fields(
bytes,
&mut fields,
self.metadata.types(),
Expand All @@ -451,7 +449,7 @@ where
.variant
.fields
.iter()
.map(|f| scale_decode::Field::new(f.ty.id, f.name.as_deref()));
.map(|f| scale_decode::Field::new(&f.ty.id, f.name.as_deref()));
let decoded =
E::decode_as_fields(&mut self.field_bytes(), &mut fields, self.metadata.types())?;
Ok(Some(decoded))
Expand All @@ -466,7 +464,7 @@ where
pub fn as_root_extrinsic<E: DecodeAsType>(&self) -> Result<E, Error> {
let decoded = E::decode_as_type(
&mut &self.call_bytes()[..],
self.metadata.outer_enums().call_enum_ty(),
&self.metadata.outer_enums().call_enum_ty(),
self.metadata.types(),
)?;

Expand Down Expand Up @@ -651,9 +649,9 @@ impl<'a, T: Config> ExtrinsicSignedExtensions<'a, T> {
let cursor = &mut &bytes[byte_start_idx..];
if let Err(err) = scale_decode::visitor::decode_with_visitor(
cursor,
ty_id,
&ty_id,
metadata.types(),
scale_decode::visitor::IgnoreVisitor,
scale_decode::visitor::IgnoreVisitor::new(),
)
.map_err(|e| Error::Decode(e.into()))
{
Expand Down Expand Up @@ -748,7 +746,13 @@ impl<'a, T: Config> ExtrinsicSignedExtension<'a, T> {

/// Signed Extension as a [`scale_value::Value`]
pub fn value(&self) -> Result<DecodedValue, Error> {
self.as_type()
let value = scale_value::scale::decode_as_type(
&mut &self.bytes[..],
&self.ty_id,
self.metadata.types(),
)
.map_err(|e| Error::Decode(e.into()))?;
Ok(value)
}

/// Decodes the bytes of this Signed Extension into its associated `Decoded` type.
Expand All @@ -762,7 +766,7 @@ impl<'a, T: Config> ExtrinsicSignedExtension<'a, T> {
}

fn as_type<E: DecodeAsType>(&self) -> Result<E, Error> {
let value = E::decode_as_type(&mut &self.bytes[..], self.ty_id, self.metadata.types())?;
let value = E::decode_as_type(&mut &self.bytes[..], &self.ty_id, self.metadata.types())?;
Ok(value)
}
}
Expand Down
11 changes: 6 additions & 5 deletions subxt/src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub use scale_value::{At, Value};
/// regarding what type was used to decode each part of it. This implements
/// [`crate::metadata::DecodeWithMetadata`], and is used as a return type
/// for dynamic requests.
pub type DecodedValue = scale_value::Value<scale_value::scale::TypeId>;
pub type DecodedValue = scale_value::Value<u32>;

// Submit dynamic transactions.
pub use crate::tx::dynamic as tx;
Expand Down Expand Up @@ -68,18 +68,19 @@ impl DecodedValueThunk {
}
/// Decode the SCALE encoded storage entry into a dynamic [`DecodedValue`] type.
pub fn to_value(&self) -> Result<DecodedValue, Error> {
let val = DecodedValue::decode_as_type(
let val = scale_value::scale::decode_as_type(
&mut &*self.scale_bytes,
self.type_id,
&self.type_id,
self.metadata.types(),
)?;
)
.map_err(|e| Error::Decode(e.into()))?;
Ok(val)
}
/// decode the `DecodedValueThunk` into a concrete type.
pub fn as_type<T: DecodeAsType>(&self) -> Result<T, scale_decode::Error> {
T::decode_as_type(
&mut &self.scale_bytes[..],
self.type_id,
&self.type_id,
self.metadata.types(),
)
}
Expand Down
24 changes: 14 additions & 10 deletions subxt/src/error/dispatch_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

use crate::metadata::{DecodeWithMetadata, Metadata};
use core::fmt::Debug;
use scale_decode::{visitor::DecodeAsTypeResult, DecodeAsType};
use std::borrow::Cow;
use scale_decode::{visitor::DecodeAsTypeResult, DecodeAsType, TypeResolver};

use std::{borrow::Cow, marker::PhantomData};

use super::{Error, MetadataError};

Expand Down Expand Up @@ -209,7 +210,7 @@ impl ModuleError {
pub fn as_root_error<E: DecodeAsType>(&self) -> Result<E, Error> {
let decoded = E::decode_as_type(
&mut &self.bytes[..],
self.metadata.outer_enums().error_enum_ty(),
&self.metadata.outer_enums().error_enum_ty(),
self.metadata.types(),
)?;

Expand Down Expand Up @@ -262,24 +263,27 @@ impl DispatchError {
// a legacy format of 2 bytes, or a newer format of 5 bytes. So, just grab the bytes
// out when decoding to manually work with them.
struct DecodedModuleErrorBytes(Vec<u8>);
struct DecodedModuleErrorBytesVisitor;
impl scale_decode::Visitor for DecodedModuleErrorBytesVisitor {
struct DecodedModuleErrorBytesVisitor<R: TypeResolver>(PhantomData<R>);
impl<R: TypeResolver> scale_decode::Visitor for DecodedModuleErrorBytesVisitor<R> {
type Error = scale_decode::Error;
type Value<'scale, 'info> = DecodedModuleErrorBytes;
type TypeResolver = R;

fn unchecked_decode_as_type<'scale, 'info>(
self,
input: &mut &'scale [u8],
_type_id: scale_decode::visitor::TypeId,
_types: &'info scale_info::PortableRegistry,
_type_id: &R::TypeId,
_types: &'info R,
) -> DecodeAsTypeResult<Self, Result<Self::Value<'scale, 'info>, Self::Error>>
{
DecodeAsTypeResult::Decoded(Ok(DecodedModuleErrorBytes(input.to_vec())))
}
}

impl scale_decode::IntoVisitor for DecodedModuleErrorBytes {
type Visitor = DecodedModuleErrorBytesVisitor;
fn into_visitor() -> Self::Visitor {
DecodedModuleErrorBytesVisitor
type AnyVisitor<R: TypeResolver> = DecodedModuleErrorBytesVisitor<R>;
fn into_visitor<R: TypeResolver>() -> DecodedModuleErrorBytesVisitor<R> {
DecodedModuleErrorBytesVisitor(PhantomData)
}
}

Expand Down
Loading
Loading