Update deps to use scale-type-resolver 0.2 (#1565)

* Updates for scale-type-resolver 0.2

* show that we can check the name too now

* Bump versions and fix WrapperKeepOpaque name check
This commit is contained in:
James Wilson
2024-04-30 08:45:39 +01:00
committed by GitHub
parent cdbf025db1
commit bc079e7c98
18 changed files with 247 additions and 252 deletions
Generated
+174 -185
View File
File diff suppressed because it is too large Load Diff
+6 -6
View File
@@ -91,12 +91,12 @@ proc-macro2 = "1.0.81"
quote = "1.0.36"
regex = { version = "1.10.4", default-features = false }
scale-info = { version = "2.11.0", default-features = false }
scale-value = { version = "0.14.1", default-features = false }
scale-bits = { version = "0.5.0", default-features = false }
scale-decode = { version = "0.11.1", default-features = false }
scale-encode = { version = "0.6.0", default-features = false }
scale-typegen = "0.4.2"
scale-typegen-description = "0.4.3"
scale-value = { version = "0.15.0", default-features = false }
scale-bits = { version = "0.6.0", default-features = false }
scale-decode = { version = "0.12.0", default-features = false }
scale-encode = { version = "0.7.0", default-features = false }
scale-typegen = "0.5.0"
scale-typegen-description = "0.5.0"
serde = { version = "1.0.199", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.116", default-features = false }
syn = { version = "2.0.15", features = ["full", "extra-traits"] }
@@ -25,7 +25,7 @@ pub fn explore_constants(
Usage:
subxt explore pallet {pallet_name} constants <CONSTANT>
explore a specific constant of this pallet
{constants}
"}
};
@@ -53,7 +53,7 @@ pub fn explore_constants(
writedoc! {output, "
Description:
{doc_string}
"}?;
}
@@ -64,11 +64,8 @@ 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!(
+5 -5
View File
@@ -57,7 +57,7 @@ pub async fn explore_storage(
Usage:
subxt explore pallet {pallet_name} storage {storage_entry_placeholder}
explore a specific storage entry of this pallet
{storage_entries}
"}
};
@@ -139,7 +139,7 @@ pub async fn explore_storage(
"}?;
} else {
writedoc! {output,"
Can be accessed without providing a {key_value_placeholder}.
"}?;
}
@@ -164,12 +164,12 @@ pub async fn explore_storage(
let value = parse_string_into_scale_value(trailing_args)?;
let value_str = value.indent(4);
writedoc! {output, "
You submitted the following {key_value_placeholder}:
{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]
}
@@ -191,7 +191,7 @@ pub async fn explore_storage(
let value = decoded_value_thunk.to_value()?.to_string().highlight();
writedoc! {output, "
The value of the storage entry is:
{value}
"}?;
+9 -9
View File
@@ -27,7 +27,7 @@ use subxt_metadata::RuntimeApiMetadata;
/// exectute is:
/// false => Show input type description + Example Value
/// true => validate (trailing args + build node connection)
/// validation is:
/// validation is:
/// Err => Show Error
/// Ok => Make a runtime api call witht the provided args.
/// response is:
@@ -51,7 +51,7 @@ pub async fn run<'a>(
Usage:
subxt explore api {api_name} <METHOD>
explore a specific runtime api method
{methods}
"}
};
@@ -63,7 +63,7 @@ pub async fn run<'a>(
writedoc! {output, "
Description:
{doc_string}
"}?;
}
writeln!(output, "{}", usage())?;
@@ -89,7 +89,7 @@ pub async fn run<'a>(
writedoc! {output, "
Description:
{doc_string}
"}?;
}
@@ -116,7 +116,7 @@ pub async fn run<'a>(
formatdoc! {"
The method expects an {input_value_placeholder} with this shape:
{fields_description}
For example you could provide this {input_value_placeholder}:
{fields_example}"}
};
@@ -156,13 +156,13 @@ pub async fn run<'a>(
let value_str = value.indent(4);
// convert to bytes:
writedoc! {output, "
You submitted the following {input_value_placeholder}:
{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())?;
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>>>()?;
@@ -178,7 +178,7 @@ pub async fn run<'a>(
let output_value = output_value.to_value()?.to_string().highlight();
writedoc! {output, "
Returned value:
{output_value}
"}?;
@@ -47,7 +47,7 @@ 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::new(),
)
@@ -146,7 +146,7 @@ impl<'a, T: Config> ExtrinsicSignedExtension<'a, T> {
pub fn value(&self) -> Result<Value<u32>, Error> {
let value = scale_value::scale::decode_as_type(
&mut &self.bytes[..],
&self.ty_id,
self.ty_id,
self.metadata.types(),
)?;
Ok(value)
@@ -163,7 +163,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)
}
}
+6 -6
View File
@@ -194,7 +194,7 @@ 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::new(),
)
@@ -203,7 +203,7 @@ where
scale_decode::visitor::decode_with_visitor(
cursor,
&ids.signature,
ids.signature,
metadata.types(),
scale_decode::visitor::IgnoreVisitor::new(),
)
@@ -212,7 +212,7 @@ where
scale_decode::visitor::decode_with_visitor(
cursor,
&ids.extra,
ids.extra,
metadata.types(),
scale_decode::visitor::IgnoreVisitor::new(),
)
@@ -370,7 +370,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 =
scale_value::scale::decode_as_fields(bytes, &mut fields, self.metadata.types())?;
@@ -388,7 +388,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))
@@ -403,7 +403,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(),
)?;
+2 -2
View File
@@ -67,7 +67,7 @@ impl DecodedValueThunk {
pub fn to_value(&self) -> Result<DecodedValue, scale_decode::Error> {
let val = scale_value::scale::decode_as_type(
&mut &*self.scale_bytes,
&self.type_id,
self.type_id,
self.metadata.types(),
)?;
Ok(val)
@@ -76,7 +76,7 @@ impl DecodedValueThunk {
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(),
)
}
+4 -4
View File
@@ -272,7 +272,7 @@ impl<T: Config> EventDetails<T> {
// Skip over the bytes for this field:
scale_decode::visitor::decode_with_visitor(
input,
&field_metadata.ty.id,
field_metadata.ty.id,
metadata.types(),
scale_decode::visitor::IgnoreVisitor::new(),
)
@@ -373,7 +373,7 @@ impl<T: Config> EventDetails<T> {
.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 =
scale_value::scale::decode_as_fields(bytes, &mut fields, self.metadata.types())?;
@@ -390,7 +390,7 @@ impl<T: Config> EventDetails<T> {
.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))
@@ -407,7 +407,7 @@ impl<T: Config> EventDetails<T> {
let decoded = E::decode_as_type(
&mut &bytes[..],
&self.metadata.outer_enums().event_enum_ty(),
self.metadata.outer_enums().event_enum_ty(),
self.metadata.types(),
)?;
+2 -2
View File
@@ -22,7 +22,7 @@ impl<T: scale_decode::DecodeAsType> DecodeWithMetadata for T {
type_id: u32,
metadata: &Metadata,
) -> Result<T, scale_decode::Error> {
let val = T::decode_as_type(bytes, &type_id, metadata.types())?;
let val = T::decode_as_type(bytes, type_id, metadata.types())?;
Ok(val)
}
}
@@ -46,7 +46,7 @@ impl<T: scale_encode::EncodeAsType> EncodeWithMetadata for T {
metadata: &Metadata,
bytes: &mut Vec<u8>,
) -> Result<(), scale_encode::Error> {
self.encode_as_type_to(&type_id, metadata.types(), bytes)?;
self.encode_as_type_to(type_id, metadata.types(), bytes)?;
Ok(())
}
}
+1 -1
View File
@@ -108,7 +108,7 @@ impl<ArgsData: EncodeAsFields, ReturnTy: DecodeWithMetadata> Payload
.ok_or_else(|| MetadataError::RuntimeMethodNotFound((*self.method_name).to_owned()))?;
let mut fields = api_method
.inputs()
.map(|input| scale_encode::Field::named(&input.ty, &input.name));
.map(|input| scale_encode::Field::named(input.ty, &input.name));
self.args_data
.encode_as_fields_to(&mut fields, metadata.types(), out)?;
+4 -4
View File
@@ -206,7 +206,7 @@ impl<K: ?Sized> StorageKey for StaticStorageKey<K> {
types: &PortableRegistry,
) -> Result<(), Error> {
let (hasher, ty_id) = hashers.next_or_err()?;
let encoded_value = self.bytes.encode_as_type(&ty_id, types)?;
let encoded_value = self.bytes.encode_as_type(ty_id, types)?;
hash_bytes(&encoded_value, hasher, bytes);
Ok(())
}
@@ -245,7 +245,7 @@ impl StorageKey for Vec<scale_value::Value> {
) -> Result<(), Error> {
for value in self.iter() {
let (hasher, ty_id) = hashers.next_or_err()?;
let encoded_value = value.encode_as_type(&ty_id, types)?;
let encoded_value = value.encode_as_type(ty_id, types)?;
hash_bytes(&encoded_value, hasher, bytes);
}
Ok(())
@@ -264,7 +264,7 @@ impl StorageKey for Vec<scale_value::Value> {
match consume_hash_returning_key_bytes(bytes, hasher, ty_id, types)? {
Some(value_bytes) => {
let value =
scale_value::scale::decode_as_type(&mut &*value_bytes, &ty_id, types)?;
scale_value::scale::decode_as_type(&mut &*value_bytes, ty_id, types)?;
result.push(value.remove_context());
}
None => {
@@ -302,7 +302,7 @@ fn consume_hash_returning_key_bytes<'a>(
if hasher.ends_with_key() {
scale_decode::visitor::decode_with_visitor(
bytes,
&ty_id,
ty_id,
types,
IgnoreVisitor::<PortableRegistry>::new(),
)
+1 -1
View File
@@ -152,7 +152,7 @@ impl<CallData: EncodeAsFields> Payload for DefaultPayload<CallData> {
let mut fields = call
.fields
.iter()
.map(|f| scale_encode::Field::new(&f.ty.id, f.name.as_deref()));
.map(|f| scale_encode::Field::new(f.ty.id, f.name.as_deref()));
self.call_data
.encode_as_fields_to(&mut fields, metadata.types(), out)
+2 -2
View File
@@ -156,7 +156,7 @@ impl<Store, Order, R: TypeResolver> scale_decode::Visitor for DecodedBitsVisitor
fn unchecked_decode_as_type<'scale, 'info>(
self,
input: &mut &'scale [u8],
type_id: &R::TypeId,
type_id: R::TypeId,
types: &'info R,
) -> scale_decode::visitor::DecodeAsTypeResult<
Self,
@@ -181,7 +181,7 @@ impl<Store, Order> scale_decode::IntoVisitor for DecodedBits<Store, Order> {
impl<Store, Order> scale_encode::EncodeAsType for DecodedBits<Store, Order> {
fn encode_as_type_to<R: TypeResolver>(
&self,
type_id: &R::TypeId,
type_id: R::TypeId,
types: &R,
out: &mut Vec<u8>,
) -> Result<(), scale_encode::Error> {
+2 -2
View File
@@ -22,7 +22,7 @@ pub struct Static<T>(pub T);
impl<T: Encode> EncodeAsType for Static<T> {
fn encode_as_type_to<R: TypeResolver>(
&self,
_type_id: &R::TypeId,
_type_id: R::TypeId,
_types: &R,
out: &mut Vec<u8>,
) -> Result<(), scale_encode::Error> {
@@ -41,7 +41,7 @@ impl<T: Decode, R: TypeResolver> Visitor for StaticDecodeAsTypeVisitor<T, R> {
fn unchecked_decode_as_type<'scale, 'info>(
self,
input: &mut &'scale [u8],
_type_id: &R::TypeId,
_type_id: R::TypeId,
_types: &'info R,
) -> DecodeAsTypeResult<Self, Result<Self::Value<'scale, 'info>, Self::Error>> {
use scale_decode::{visitor::DecodeError, Error};
+2 -2
View File
@@ -55,7 +55,7 @@ impl<Address, Call, Signature, Extra> scale_encode::EncodeAsType
{
fn encode_as_type_to<R: TypeResolver>(
&self,
type_id: &R::TypeId,
type_id: R::TypeId,
types: &R,
out: &mut Vec<u8>,
) -> Result<(), scale_encode::Error> {
@@ -93,7 +93,7 @@ impl<Address, Call, Signature, Extra, R: TypeResolver> Visitor
fn unchecked_decode_as_type<'scale, 'info>(
self,
input: &mut &'scale [u8],
type_id: &R::TypeId,
type_id: R::TypeId,
types: &'info R,
) -> DecodeAsTypeResult<Self, Result<Self::Value<'scale, 'info>, Self::Error>> {
DecodeAsTypeResult::Decoded(Self::Value::decode_as_type(input, type_id, types))
+18 -9
View File
@@ -72,26 +72,27 @@ impl<T> WrapperKeepOpaque<T> {
impl<T> EncodeAsType for WrapperKeepOpaque<T> {
fn encode_as_type_to<R: TypeResolver>(
&self,
type_id: &R::TypeId,
type_id: R::TypeId,
types: &R,
out: &mut Vec<u8>,
) -> Result<(), scale_encode::Error> {
use scale_encode::error::{Error, ErrorKind, Kind};
let visitor = visitor::new(out, |_, _| {
let ctx = (type_id.clone(), out);
let visitor = visitor::new(ctx, |(type_id, _out), _| {
// Check that the target shape lines up: any other shape but composite is wrong.
Err(Error::new(ErrorKind::WrongShape {
actual: Kind::Struct,
expected_id: format!("{:?}", type_id),
expected_id: format!("{type_id:?}"),
}))
})
.visit_composite(|out, _fields| {
.visit_composite(|(_type_id, out), _path, _fields| {
self.data.encode_to(out);
Ok(())
});
types
.resolve_type(type_id, visitor)
.resolve_type(type_id.clone(), visitor)
.map_err(|_| Error::new(ErrorKind::TypeNotFound(format!("{:?}", type_id))))?
}
}
@@ -105,11 +106,19 @@ impl<T, R: TypeResolver> Visitor for WrapperKeepOpaqueVisitor<T, R> {
fn visit_composite<'scale, 'info>(
self,
value: &mut scale_decode::visitor::types::Composite<'scale, 'info, R>,
_type_id: &R::TypeId,
_type_id: R::TypeId,
) -> Result<Self::Value<'scale, 'info>, Self::Error> {
use scale_decode::error::{Error, ErrorKind};
use scale_decode::visitor::DecodeError;
// TODO: When `scale-type-resolver` [provides struct names](https://github.com/paritytech/scale-type-resolver/issues/4), check that this struct name is `WrapperKeepOpaque`
if value.name() != Some("WrapperKeepOpaque") {
return Err(Error::new(ErrorKind::VisitorDecodeError(
DecodeError::TypeResolvingError(format!(
"Expected a type named 'WrapperKeepOpaque', got: {:?}",
value.name()
)),
)));
}
if value.remaining() != 2 {
return Err(Error::new(ErrorKind::WrongLength {
@@ -193,7 +202,7 @@ mod test {
let (type_id, types) = make_type::<T>();
let scale_codec_encoded = t.encode();
let encode_as_type_encoded = t.encode_as_type(&type_id, &types).unwrap();
let encode_as_type_encoded = t.encode_as_type(type_id, &types).unwrap();
assert_eq!(
scale_codec_encoded, encode_as_type_encoded,
@@ -201,7 +210,7 @@ mod test {
);
let decode_as_type_bytes = &mut &*scale_codec_encoded;
let decoded_as_type = T::decode_as_type(decode_as_type_bytes, &type_id, &types)
let decoded_as_type = T::decode_as_type(decode_as_type_bytes, type_id, &types)
.expect("decode-as-type decodes");
let decode_scale_codec_bytes = &mut &*scale_codec_encoded;
+2 -2
View File
@@ -210,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(),
)?;
@@ -272,7 +272,7 @@ impl DispatchError {
fn unchecked_decode_as_type<'scale, 'info>(
self,
input: &mut &'scale [u8],
_type_id: &R::TypeId,
_type_id: R::TypeId,
_types: &'info R,
) -> DecodeAsTypeResult<Self, Result<Self::Value<'scale, 'info>, Self::Error>>
{