Skip to content

Commit

Permalink
Update scale-encode, scale-decode and scale-value to latest (#991)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsdw authored and tadeohepperle committed Jun 1, 2023
1 parent 2e41f49 commit 8dc3421
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 44 deletions.
24 changes: 14 additions & 10 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ proc-macro2 = "1.0.59"
quote = "1.0.28"
regex = "1.8.3"
scale-info = "2.7.0"
scale-value = "0.7.0"
scale-value = "0.10.0"
scale-bits = "0.3"
scale-decode = "0.5.0"
scale-encode = "0.1.0"
scale-decode = "0.7.0"
scale-encode = "0.3.0"
serde = { version = "1.0.163" }
serde_json = { version = "1.0.96" }
syn = { version = "2.0.15", features = ["full", "extra-traits"] }
Expand Down
19 changes: 13 additions & 6 deletions subxt/src/blocks/extrinsic_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,14 @@ where
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(
bytes,
&extrinsic_metadata.variant.fields,
&mut fields,
self.metadata.types(),
)?;

Expand All @@ -398,11 +403,13 @@ where
if extrinsic_metadata.pallet.name() == E::PALLET
&& extrinsic_metadata.variant.name == E::CALL
{
let decoded = E::decode_as_fields(
&mut self.field_bytes(),
&extrinsic_metadata.variant.fields,
self.metadata.types(),
)?;
let mut fields = extrinsic_metadata
.variant
.fields
.iter()
.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))
} else {
Ok(None)
Expand Down
20 changes: 14 additions & 6 deletions subxt/src/events/events_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,16 @@ impl<T: Config> EventDetails<T> {
let bytes = &mut self.field_bytes();
let event_metadata = self.event_metadata();

let mut fields = event_metadata
.variant
.fields
.iter()
.map(|f| scale_decode::Field::new(f.ty.id, f.name.as_deref()));

use scale_decode::DecodeAsFields;
let decoded = <scale_value::Composite<scale_value::scale::TypeId>>::decode_as_fields(
bytes,
&event_metadata.variant.fields,
&mut fields,
self.metadata.types(),
)?;

Expand All @@ -352,11 +358,13 @@ impl<T: Config> EventDetails<T> {
pub fn as_event<E: StaticEvent>(&self) -> Result<Option<E>, Error> {
let ev_metadata = self.event_metadata();
if ev_metadata.pallet.name() == E::PALLET && ev_metadata.variant.name == E::EVENT {
let decoded = E::decode_as_fields(
&mut self.field_bytes(),
&ev_metadata.variant.fields,
self.metadata.types(),
)?;
let mut fields = ev_metadata
.variant
.fields
.iter()
.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))
} else {
Ok(None)
Expand Down
21 changes: 3 additions & 18 deletions subxt/src/runtime_api/runtime_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,12 @@ impl<ArgsData: EncodeAsFields, ReturnTy: DecodeWithMetadata> RuntimeApiPayload
.runtime_api_trait_by_name_err(&self.trait_name)?
.method_by_name(&self.method_name)
.ok_or_else(|| MetadataError::RuntimeMethodNotFound((*self.method_name).to_owned()))?;

// TODO [jsdw]: This isn't good. The options are:
// 1. Tweak the Metadata to store things in this way (not great, but
// it's what we did previously).
// 2. Tweak scale-encode's methods to accept iterators over
// { name: Option<&str>, type_id: u32 }, because they shouldn't
// need to allocate anyway. I'd like us to do this, and then we can
// remove allocations from this code and probably a couple of other
// places.
let fields: Vec<_> = api_method
let mut fields = api_method
.inputs()
.map(|input| scale_info::Field {
name: Some(input.name.to_owned()),
ty: input.ty.into(),
type_name: None,
docs: Default::default(),
})
.collect();
.map(|input| scale_encode::Field::named(input.ty, &input.name));

self.args_data
.encode_as_fields_to(&fields, metadata.types(), out)?;
.encode_as_fields_to(&mut fields, metadata.types(), out)?;
Ok(())
}

Expand Down
7 changes: 6 additions & 1 deletion subxt/src/tx/tx_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,13 @@ impl<CallData: EncodeAsFields> TxPayload for Payload<CallData> {
pallet_index.encode_to(out);
call_index.encode_to(out);

let mut fields = call
.fields
.iter()
.map(|f| scale_encode::Field::new(f.ty.id, f.name.as_deref()));

self.call_data
.encode_as_fields_to(&call.fields, metadata.types(), out)?;
.encode_as_fields_to(&mut fields, metadata.types(), out)?;
Ok(())
}

Expand Down

0 comments on commit 8dc3421

Please sign in to comment.