Skip to content

Commit

Permalink
#2351 to polkadot-staging (#2359)
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatonik authored and bkchr committed Apr 10, 2024
1 parent 31a6cbe commit cb10062
Showing 1 changed file with 49 additions and 9 deletions.
58 changes: 49 additions & 9 deletions bridges/relays/client-substrate/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ struct ClientData {
client: Arc<RpcClient>,
}

/// Already encoded value.
struct PreEncoded(Vec<u8>);

impl Encode for PreEncoded {
fn encode(&self) -> Vec<u8> {
self.0.clone()
}
}

#[async_trait]
impl<C: Chain> relay_utils::relay_loop::Client for Client<C> {
type Error = Error;
Expand Down Expand Up @@ -438,6 +447,16 @@ impl<C: Chain> Client<C> {
///
/// Note: The given transaction needs to be SCALE encoded beforehand.
pub async fn submit_unsigned_extrinsic(&self, transaction: Bytes) -> Result<C::Hash> {
// one last check that the transaction is valid. Most of checks happen in the relay loop and
// it is the "final" check before submission.
let best_header_hash = self.best_header().await?.hash();
self.validate_transaction(best_header_hash, PreEncoded(transaction.0.clone()))
.await
.map_err(|e| {
log::error!(target: "bridge", "Pre-submit {} transaction validation failed: {:?}", C::NAME, e);
e
})??;

self.jsonrpsee_execute(move |client| async move {
let tx_hash = SubstrateAuthorClient::<C>::submit_extrinsic(&*client, transaction)
.await
Expand Down Expand Up @@ -494,9 +513,19 @@ impl<C: Chain> Client<C> {
// will be dropped from the pool.
let best_header_id = best_header.parent_id().unwrap_or_else(|| best_header.id());

let extrinsic = prepare_extrinsic(best_header_id, transaction_nonce)?;
let signed_extrinsic = C::sign_transaction(signing_data, extrinsic)?.encode();

// one last check that the transaction is valid. Most of checks happen in the relay loop and
// it is the "final" check before submission.
self.validate_transaction(best_header_id.1, PreEncoded(signed_extrinsic.clone()))
.await
.map_err(|e| {
log::error!(target: "bridge", "Pre-submit {} transaction validation failed: {:?}", C::NAME, e);
e
})??;

self.jsonrpsee_execute(move |client| async move {
let extrinsic = prepare_extrinsic(best_header_id, transaction_nonce)?;
let signed_extrinsic = C::sign_transaction(signing_data, extrinsic)?.encode();
let tx_hash =
SubstrateAuthorClient::<C>::submit_extrinsic(&*client, Bytes(signed_extrinsic))
.await
Expand Down Expand Up @@ -529,16 +558,27 @@ impl<C: Chain> Client<C> {
let transaction_nonce = self.next_account_index(signer.public().into()).await?;
let best_header = self.best_header().await?;
let best_header_id = best_header.id();

let extrinsic = prepare_extrinsic(best_header_id, transaction_nonce)?;
let stall_timeout = transaction_stall_timeout(
extrinsic.era.mortality_period(),
C::AVERAGE_BLOCK_INTERVAL,
STALL_TIMEOUT,
);
let signed_extrinsic = C::sign_transaction(signing_data, extrinsic)?.encode();

// one last check that the transaction is valid. Most of checks happen in the relay loop and
// it is the "final" check before submission.
self.validate_transaction(best_header_id.1, PreEncoded(signed_extrinsic.clone()))
.await
.map_err(|e| {
log::error!(target: "bridge", "Pre-submit {} transaction validation failed: {:?}", C::NAME, e);
e
})??;

let (sender, receiver) = futures::channel::mpsc::channel(MAX_SUBSCRIPTION_CAPACITY);
let (tracker, subscription) = self
.jsonrpsee_execute(move |client| async move {
let extrinsic = prepare_extrinsic(best_header_id, transaction_nonce)?;
let stall_timeout = transaction_stall_timeout(
extrinsic.era.mortality_period(),
C::AVERAGE_BLOCK_INTERVAL,
STALL_TIMEOUT,
);
let signed_extrinsic = C::sign_transaction(signing_data, extrinsic)?.encode();
let tx_hash = C::Hasher::hash(&signed_extrinsic);
let subscription = SubstrateAuthorClient::<C>::submit_and_watch_extrinsic(
&*client,
Expand Down

0 comments on commit cb10062

Please sign in to comment.