Skip to content

Commit

Permalink
fix parse_transaction on Rialto+Millau (paritytech#1360)
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatonik authored and serban300 committed Apr 10, 2024
1 parent 2b9d39e commit 9b7fa01
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bridges/primitives/runtime/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use sp_runtime::{
use sp_std::{convert::TryFrom, fmt::Debug, hash::Hash, str::FromStr, vec, vec::Vec};

/// Chain call, that is either SCALE-encoded, or decoded.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum EncodedOrDecodedCall<ChainCall> {
/// The call that is SCALE-encoded.
///
Expand Down
35 changes: 32 additions & 3 deletions bridges/relays/client-millau/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::time::Duration;
pub type HeaderId = relay_utils::HeaderId<millau_runtime::Hash, millau_runtime::BlockNumber>;

/// Millau chain definition.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Millau;

impl ChainBase for Millau {
Expand Down Expand Up @@ -154,8 +154,8 @@ impl TransactionSignScheme for Millau {
let extra = &tx.signature.as_ref()?.2;
Some(UnsignedTransaction {
call: tx.function.into(),
nonce: Compact::<IndexOf<Self::Chain>>::decode(&mut &extra.4.encode()[..]).ok()?.into(),
tip: Compact::<BalanceOf<Self::Chain>>::decode(&mut &extra.6.encode()[..])
nonce: Compact::<IndexOf<Self::Chain>>::decode(&mut &extra.5.encode()[..]).ok()?.into(),
tip: Compact::<BalanceOf<Self::Chain>>::decode(&mut &extra.7.encode()[..])
.ok()?
.into(),
})
Expand All @@ -167,3 +167,32 @@ pub type SigningParams = sp_core::sr25519::Pair;

/// Millau header type used in headers sync.
pub type SyncHeader = relay_substrate_client::SyncHeader<millau_runtime::Header>;

#[cfg(test)]
mod tests {
use super::*;
use relay_substrate_client::TransactionEra;

#[test]
fn parse_transaction_works() {
let unsigned = UnsignedTransaction {
call: millau_runtime::Call::System(millau_runtime::SystemCall::remark {
remark: b"Hello world!".to_vec(),
})
.into(),
nonce: 777,
tip: 888,
};
let signed_transaction = Millau::sign_transaction(SignParam {
spec_version: 42,
transaction_version: 50000,
genesis_hash: [42u8; 64].into(),
signer: sp_core::sr25519::Pair::from_seed_slice(&[1u8; 32]).unwrap(),
era: TransactionEra::immortal(),
unsigned: unsigned.clone(),
})
.unwrap();
let parsed_transaction = Millau::parse_transaction(signed_transaction).unwrap();
assert_eq!(parsed_transaction, unsigned);
}
}
35 changes: 32 additions & 3 deletions bridges/relays/client-rialto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::time::Duration;
pub type HeaderId = relay_utils::HeaderId<rialto_runtime::Hash, rialto_runtime::BlockNumber>;

/// Rialto chain definition
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rialto;

impl ChainBase for Rialto {
Expand Down Expand Up @@ -152,8 +152,8 @@ impl TransactionSignScheme for Rialto {
let extra = &tx.signature.as_ref()?.2;
Some(UnsignedTransaction {
call: tx.function.into(),
nonce: Compact::<IndexOf<Self::Chain>>::decode(&mut &extra.4.encode()[..]).ok()?.into(),
tip: Compact::<BalanceOf<Self::Chain>>::decode(&mut &extra.6.encode()[..])
nonce: Compact::<IndexOf<Self::Chain>>::decode(&mut &extra.5.encode()[..]).ok()?.into(),
tip: Compact::<BalanceOf<Self::Chain>>::decode(&mut &extra.7.encode()[..])
.ok()?
.into(),
})
Expand All @@ -165,3 +165,32 @@ pub type SigningParams = sp_core::sr25519::Pair;

/// Rialto header type used in headers sync.
pub type SyncHeader = relay_substrate_client::SyncHeader<rialto_runtime::Header>;

#[cfg(test)]
mod tests {
use super::*;
use relay_substrate_client::TransactionEra;

#[test]
fn parse_transaction_works() {
let unsigned = UnsignedTransaction {
call: rialto_runtime::Call::System(rialto_runtime::SystemCall::remark {
remark: b"Hello world!".to_vec(),
})
.into(),
nonce: 777,
tip: 888,
};
let signed_transaction = Rialto::sign_transaction(SignParam {
spec_version: 42,
transaction_version: 50000,
genesis_hash: [42u8; 32].into(),
signer: sp_core::sr25519::Pair::from_seed_slice(&[1u8; 32]).unwrap(),
era: TransactionEra::immortal(),
unsigned: unsigned.clone(),
})
.unwrap();
let parsed_transaction = Rialto::parse_transaction(signed_transaction).unwrap();
assert_eq!(parsed_transaction, unsigned);
}
}
2 changes: 1 addition & 1 deletion bridges/relays/client-substrate/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub trait BlockWithJustification<Header> {
}

/// Transaction before it is signed.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct UnsignedTransaction<C: Chain> {
/// Runtime call of this transaction.
pub call: EncodedOrDecodedCall<C::Call>,
Expand Down

0 comments on commit 9b7fa01

Please sign in to comment.