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

chain_getBlock extrinsics encoding #1024

Merged
Merged
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions subxt/src/tx/tx_progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

//! Types representing extrinsics/transactions that have been submitted to a node.

use codec::Decode;
use codec::{Compact, Decode, Encode};
use std::task::Poll;

use crate::{
Expand Down Expand Up @@ -389,10 +389,11 @@ impl<T: Config, C: OnlineClientT<T>> TxInBlock<T, C> {
.iter()
.position(|ext| {
use crate::config::Hasher;
let Ok(ext_without_prefix) = <Vec<u8>>::decode(&mut &ext.0[..]) else {
let Ok(compact) = <Compact<u32>>::decode(&mut &ext.0[..]) else {
return false;
};
Copy link
Collaborator

@jsdw jsdw Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just because size_hint is only a "hint" at the size by the docs, I'd prefer that we have a function, something like this:

/// Decode the compact prefix, returning the remainder of the
/// bytes and the decoded value. 
fn strip_compact_prefix(bytes: &[u8]) -> Result<(u64, &[u8]), codec::Error>
    let cursor = &mut &*bytes;
    let val = <Compact<u64>>::decode(cursor)?;
    Ok((val, *cursor))
}

This decodes the value, consuming any bytes used and then we return the value + rest of the bytes (so we are guaranteed to have stripped the right number off the front).

Then in your code you can:

let Ok((compact, rest)) = strip_compact_prefix(&ext.0) else {
    return false;
}
let hash = T::Hasher::hash_of(rest);
hash == self.ext_hash

You could also use .compact_len from the CompactLen trait for more of a guarantee, though since you want the remaining bytes I think the above is probably more straightrforward :)

let hash = T::Hasher::hash_of(&ext_without_prefix);
let extrinsic_start_index = compact.size_hint();
let hash = T::Hasher::hash_of(&&ext.0[extrinsic_start_index..]);
hash == self.ext_hash
})
// If we successfully obtain the block hash we think contains our
Expand Down