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

Remove dynamic tx from relayer #477

Merged
merged 4 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/event-watcher-traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ native-tls = { workspace = true }
webb-proposals = { workspace = true }
tracing-test = "0.2"
sled = { version = "^0.34" }
sp-core = { workspace = true }

[features]
default = ["std", "evm", "substrate"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use super::{event_watcher::SubstrateEventWatcher, *};

use sp_core::sr25519::Pair as Sr25519Pair;
// A Substrate Bridge Watcher is a trait for Signature Bridge Pallet that is not specific for watching events from that pallet,
/// instead it watches for commands sent from other event watchers or services, it helps decouple the event watchers
/// from the actual action that should be taken depending on the event.
Expand All @@ -32,6 +32,7 @@ where
chain_id: u32,
store: Arc<Self::Store>,
client: Arc<Self::Client>,
pair: Sr25519Pair,
cmd: BridgeCommand,
) -> webb_relayer_utils::Result<()>;

Expand All @@ -48,6 +49,7 @@ where
&self,
chain_id: u32,
client: Arc<Self::Client>,
pair: Sr25519Pair,
store: Arc<Self::Store>,
) -> webb_relayer_utils::Result<()> {
let backoff = backoff::backoff::Constant::new(Duration::from_secs(1));
Expand All @@ -64,6 +66,7 @@ where
chain_id,
store.clone(),
client.clone(),
pair.clone(),
cmd,
)
.await
Expand Down
87 changes: 41 additions & 46 deletions crates/proposal-signing-backends/src/dkg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::borrow::Cow;
use std::sync::Arc;
use tokio::sync::Mutex;
use webb::substrate::dkg_runtime::api::runtime_types::webb_proposals::header::{TypedChainId, ResourceId};
Expand All @@ -10,38 +9,27 @@ use webb_proposals::ProposalTrait;
use webb::substrate::scale::{Encode, Decode};
use webb_relayer_utils::metric;
use webb::substrate::dkg_runtime::api as RuntimeApi;
use webb::substrate::subxt::dynamic::Value;
use webb_relayer_store::{QueueStore, SledStore};
use webb_relayer_store::sled::SledQueueKey;
use webb_relayer_types::dynamic_payload::WebbDynamicTxPayload;
use webb::substrate::subxt::tx::{PairSigner};
use webb::substrate::subxt::tx::PairSigner;
use webb::substrate::dkg_runtime::api::runtime_types::sp_core::bounded::bounded_vec::BoundedVec;

type DkgConfig = PolkadotConfig;
type DkgClient = OnlineClient<DkgConfig>;
/// A ProposalSigningBackend that uses the DKG System for Signing Proposals.
#[derive(typed_builder::TypedBuilder)]
pub struct DkgProposalSigningBackend {
#[builder(setter(into))]
pub client: DkgClient,
pub pair: PairSigner<PolkadotConfig, Sr25519Pair>,
pub typed_chain_id: webb_proposals::TypedChainId,
/// Something that implements the QueueStore trait.
#[builder(setter(into))]
store: Arc<SledStore>,
}

impl DkgProposalSigningBackend {
pub fn new(
client: OnlineClient<PolkadotConfig>,
pair: PairSigner<PolkadotConfig, Sr25519Pair>,
typed_chain_id: webb_proposals::TypedChainId,
store: Arc<SledStore>,
) -> Self {
Self {
client,
pair,
typed_chain_id,
store,
}
}
/// The chain id of the chain that this backend is running on.
///
/// This used as the source chain id for the proposals.
#[builder(setter(into))]
src_chain_id: webb_proposals::TypedChainId,
}

//AnchorUpdateProposal for evm
Expand All @@ -53,9 +41,8 @@ impl super::ProposalSigningBackend for DkgProposalSigningBackend {
) -> webb_relayer_utils::Result<bool> {
let header = proposal.header();
let resource_id = header.resource_id();

let src_chain_id =
webb_proposals_typed_chain_converter(self.typed_chain_id);
webb_proposals_typed_chain_converter(self.src_chain_id);
let chain_nonce_addrs = RuntimeApi::storage()
.dkg_proposals()
.chain_nonces(&src_chain_id);
Expand Down Expand Up @@ -97,49 +84,57 @@ impl super::ProposalSigningBackend for DkgProposalSigningBackend {
proposal: &(impl ProposalTrait + Sync + Send + 'static),
_metrics: Arc<Mutex<metric::Metrics>>,
) -> webb_relayer_utils::Result<()> {
let my_chain_id_addr =
RuntimeApi::constants().dkg_proposals().chain_identifier();
let my_chain_id = self.client.constants().at(&my_chain_id_addr)?;
let my_chain_id = match my_chain_id {
TypedChainId::Substrate(chain_id) => chain_id,
TypedChainId::PolkadotParachain(chain_id) => chain_id,
TypedChainId::KusamaParachain(chain_id) => chain_id,
_ => return Err(webb_relayer_utils::Error::Generic(
"dkg proposal signing backend only supports substrate chains",
)),
};
let tx_api = RuntimeApi::tx().dkg_proposals();
let resource_id = proposal.header().resource_id();
let nonce = proposal.header().nonce();
let src_chain_id =
webb_proposals_typed_chain_converter(self.typed_chain_id);
let nonce = Nonce::decode(&mut nonce.encode().as_slice())?;
webb_proposals_typed_chain_converter(self.src_chain_id);
tracing::debug!(
nonce = %hex::encode(nonce.encode()),
?nonce,
resource_id = %hex::encode(resource_id.into_bytes()),
src_chain_id = ?self.typed_chain_id,
src_chain_id = ?self.src_chain_id,
proposal = %hex::encode(proposal.to_vec()),
"sending proposal to DKG runtime"
);

let xt = tx_api.acknowledge_proposal(
let nonce = Nonce::decode(&mut nonce.encode().as_slice())?;

let acknowledge_proposal_tx = tx_api.acknowledge_proposal(
nonce.clone(),
src_chain_id,
ResourceId(resource_id.into_bytes()),
BoundedVec(proposal.to_vec()),
);
// webb dynamic payload
let execute_proposal_tx = WebbDynamicTxPayload {
pallet_name: Cow::Borrowed("DKGProposals"),
call_name: Cow::Borrowed("acknowledge_proposal"),
fields: vec![
Value::u128(u128::from(nonce.0)),
Value::u128(u128::from(self.typed_chain_id.chain_id())),
Value::from_bytes(
ResourceId(resource_id.into_bytes()).encode(),
),
Value::from_bytes(BoundedVec(proposal.to_vec()).encode()),
],
};
let data_hash = utils::keccak256(xt.call_data().encode());

let signer = &self.pair;
let signed_acknowledge_proposal_tx = self
.client
.tx()
.create_signed(&acknowledge_proposal_tx, signer, Default::default())
.await?;

let data_hash =
utils::keccak256(acknowledge_proposal_tx.call_data().encode());
let tx_key = SledQueueKey::from_substrate_with_custom_key(
self.typed_chain_id.underlying_chain_id(),
my_chain_id,
make_acknowledge_proposal_key(data_hash),
);
// Enqueue WebbDynamicTxPayload in protocol-substrate transaction queue
QueueStore::<WebbDynamicTxPayload>::enqueue_item(
// Enqueue transaction in protocol-substrate transaction queue
QueueStore::<Vec<u8>>::enqueue_item(
&self.store,
tx_key,
execute_proposal_tx,
signed_acknowledge_proposal_tx.into_encoded(),
)?;

Ok(())
Expand Down
28 changes: 0 additions & 28 deletions crates/relayer-types/src/dynamic_payload.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/relayer-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod dynamic_payload;
pub mod etherscan_api;
pub mod mnemonic;
pub mod private_key;
Expand Down
49 changes: 14 additions & 35 deletions crates/tx-queue/src/substrate/substrate_tx_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use futures::StreamExt;
use futures::TryFutureExt;
use rand::Rng;
use webb::substrate::subxt;
use webb::substrate::subxt::config::ExtrinsicParams;
use webb::substrate::subxt::tx::SubmittableExtrinsic;
use webb_relayer_context::RelayerContext;
use webb_relayer_store::sled::SledQueueKey;
use webb_relayer_store::QueueStore;
Expand All @@ -24,28 +26,24 @@ use std::sync::Arc;
use std::time::Duration;

use sp_core::sr25519;
use std::marker::PhantomData;
use webb::substrate::subxt;
use webb::substrate::subxt::tx::{PairSigner, TxStatus as TransactionStatus};
use webb_relayer_types::dynamic_payload::WebbDynamicTxPayload;
use webb::substrate::subxt::tx::TxStatus as TransactionStatus;

/// The SubstrateTxQueue stores transaction call params in bytes so the relayer can process them later.
/// This prevents issues such as creating transactions with the same nonce.
/// Randomized sleep intervals are used to prevent relayers from submitting
/// the same transaction.
#[derive(Clone)]
pub struct SubstrateTxQueue<'a, S>
pub struct SubstrateTxQueue<S>
where
S: QueueStore<WebbDynamicTxPayload<'a>, Key = SledQueueKey>,
S: QueueStore<Vec<u8>, Key = SledQueueKey>,
{
ctx: RelayerContext,
chain_id: u32,
store: Arc<S>,
_marker: PhantomData<&'a ()>,
}
impl<'a, S> SubstrateTxQueue<'a, S>
impl<S> SubstrateTxQueue<S>
where
S: QueueStore<WebbDynamicTxPayload<'a>, Key = SledQueueKey>,
S: QueueStore<Vec<u8>, Key = SledQueueKey>,
{
/// Creates a new SubstrateTxQueue instance.
///
Expand All @@ -61,7 +59,6 @@ where
ctx,
chain_id,
store,
_marker: PhantomData {},
}
}
/// Starts the SubstrateTxQueue service.
Expand Down Expand Up @@ -96,10 +93,6 @@ where
.substrate_provider::<X>(&chain_id.to_string())
.await?;

// get pair
let pair = self.ctx.substrate_wallet(&chain_id.to_string()).await?;
let signer = PairSigner::new(pair);

tracing::event!(
target: webb_relayer_utils::probe::TARGET,
tracing::Level::DEBUG,
Expand All @@ -113,26 +106,16 @@ where
let task = || async {
loop {
tracing::trace!("Checking for any txs in the queue ...");
// dequeue transaction call data. This are call params stored as bytes
// dequeue signed transaction
let maybe_call_data = store.dequeue_item(
SledQueueKey::from_substrate_chain_id(chain_id),
)?;
if let Some(payload) = maybe_call_data {
let dynamic_tx_payload = subxt::dynamic::tx(
payload.pallet_name,
payload.call_name,
payload.fields,
let signed_extrinsic = SubmittableExtrinsic::from_bytes(
client.clone(),
payload,
);
let signed_extrinsic = client
.tx()
.create_signed(
&dynamic_tx_payload,
&signer,
Default::default(),
)
.map_err(Into::into)
.map_err(backoff::Error::transient)
.await?;

// dry run test
let dry_run_outcome = signed_extrinsic.dry_run(None).await;
match dry_run_outcome {
Expand Down Expand Up @@ -161,12 +144,8 @@ where
}
}
// watch_extrinsic submits and returns transaction subscription
let mut progress = client
.tx()
.sign_and_submit_then_watch_default(
&dynamic_tx_payload,
&signer,
)
let mut progress = signed_extrinsic
.submit_and_watch()
.map_err(Into::into)
.map_err(backoff::Error::transient)
.await?;
Expand Down
Loading