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

Make payment_queryInfo work no matter the type of Balance #2914

Merged
merged 4 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions bin/wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Changed

- The [`payment_queryInfo`] JSON-RPC function now works with runtimes that have defined the type of `Balance` to be less than 16 bytes. ([#2914](https://github.com/paritytech/smoldot/pull/2914))
tomaka marked this conversation as resolved.
Show resolved Hide resolved

## 0.7.3 - 2022-10-19

### Changed
Expand Down
41 changes: 39 additions & 2 deletions src/json_rpc/payment_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,45 @@ fn nom_decode_payment_info<'a, E: nom::error::ParseError<&'a [u8]>>(
2 => Some(methods::DispatchClass::Mandatory),
_ => None,
}),
// TODO: this is actually of type `Balance`; figure out how to find that type
nom::number::complete::le_u128,
|bytes| {
// The exact format here is the SCALE encoding of the type `Balance`.
// Normally, determining the actual type of `Balance` would require parsing the
// metadata provided by the runtime. However, this is a pretty difficult to
// implement and CPU-heavy. Instead, given that there is no other field after
// the balance, we simply parse all the remaining bytes.
// Because the SCALE encoding of a number is the number in little endian format,
// we decode the bytes in little endian format in a way that works no matter the
// number of bytes.
// If a field was to be added after the balance, this code would need to be
// modified.
// TODO: must make sure that TransactionPaymentApi is at version 1, see https://github.com/paritytech/smoldot/issues/949
let mut num = 0u128;
let mut shift = 0u32;
for byte in <[u8]>::iter(bytes) {
let shifted =
u128::from(*byte)
.checked_mul(1 << shift)
.ok_or(nom::Err::Error(nom::error::make_error(
bytes,
nom::error::ErrorKind::Digit,
)))?;
num =
num.checked_add(shifted)
.ok_or(nom::Err::Error(nom::error::make_error(
bytes,
nom::error::ErrorKind::Digit,
)))?;
shift =
shift
.checked_add(16)
.ok_or(nom::Err::Error(nom::error::make_error(
bytes,
nom::error::ErrorKind::Digit,
)))?;
}

Ok((&[][..], num))
},
)),
|(weight, class, partial_fee)| methods::RuntimeDispatchInfo {
weight,
Expand Down