Skip to content

Commit

Permalink
Use bridge registry for substrate chains (#447)
Browse files Browse the repository at this point in the history
* use bridge registry for substrate

* cargo.lock file
  • Loading branch information
salman01zp authored Apr 10, 2023
1 parent bd5d911 commit d73369e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
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 event-watchers/substrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repository = { workspace = true }

[dependencies]
webb-proposal-signing-backends = { workspace = true }
webb-bridge-registry-backends = { workspace = true }
webb-event-watcher-traits = { workspace = true }
webb-relayer-types = { workspace = true }
webb-relayer-store = { workspace = true }
Expand Down
24 changes: 18 additions & 6 deletions event-watchers/substrate/src/vanchor_deposit_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use webb::substrate::protocol_substrate_runtime::api as RuntimeApi;
use webb::substrate::protocol_substrate_runtime::api::v_anchor_bn254;
use webb::substrate::scale::Encode;
use webb::substrate::subxt::{self, OnlineClient, SubstrateConfig};
use webb_bridge_registry_backends::BridgeRegistryBackend;
use webb_event_watcher_traits::substrate::EventHandler;

use webb_proposal_signing_backends::{
Expand All @@ -29,30 +30,36 @@ use webb_relayer_store::EventHashStore;
use webb_relayer_store::SledStore;
use webb_relayer_utils::metric;
/// SubstrateVAnchorDeposit handler handles `Transaction` event and creates `AnchorUpdate` proposals for linked anchors.
pub struct SubstrateVAnchorDepositHandler<B> {
pub struct SubstrateVAnchorDepositHandler<B, C> {
proposal_signing_backend: B,
linked_anchors: Vec<LinkedAnchorConfig>,
bridge_registry_backend: C,
linked_anchors: Option<Vec<LinkedAnchorConfig>>,
}

impl<B> SubstrateVAnchorDepositHandler<B>
impl<B, C> SubstrateVAnchorDepositHandler<B, C>
where
B: ProposalSigningBackend,
C: BridgeRegistryBackend,
{
pub fn new(
proposal_signing_backend: B,
linked_anchors: Vec<LinkedAnchorConfig>,
bridge_registry_backend: C,
linked_anchors: Option<Vec<LinkedAnchorConfig>>,
) -> Self {
Self {
proposal_signing_backend,
bridge_registry_backend,
linked_anchors,
}
}
}

#[async_trait::async_trait]
impl<B> EventHandler<SubstrateConfig> for SubstrateVAnchorDepositHandler<B>
impl<B, C> EventHandler<SubstrateConfig>
for SubstrateVAnchorDepositHandler<B, C>
where
B: ProposalSigningBackend + Send + Sync,
C: BridgeRegistryBackend + Send + Sync,
{
type Client = OnlineClient<SubstrateConfig>;

Expand Down Expand Up @@ -130,8 +137,13 @@ where
let mut merkle_root = [0; 32];
merkle_root.copy_from_slice(&root.encode());

let linked_anchors = self
.bridge_registry_backend
.config_or_dkg_bridges(&self.linked_anchors, &src_resource_id)
.await?;

// update linked anchors
for linked_anchor in &self.linked_anchors {
for linked_anchor in linked_anchors {
let target_resource_id = match linked_anchor {
LinkedAnchorConfig::Raw(target) => {
let bytes: [u8; 32] = target.resource_id.into();
Expand Down
19 changes: 12 additions & 7 deletions services/webb-relayer/src/service/substrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use axum::Router;
use sp_core::sr25519;
use webb::substrate::subxt::config::ExtrinsicParams;
use webb::substrate::subxt::{self, PolkadotConfig};
use webb_bridge_registry_backends::dkg::DkgBridgeRegistryBackend;
use webb_bridge_registry_backends::mocked::MockedBridgeRegistryBackend;
use webb_event_watcher_traits::{
SubstrateBridgeWatcher, SubstrateEventWatcher,
};
Expand Down Expand Up @@ -361,12 +363,13 @@ pub fn start_substrate_vanchor_event_watcher(
.await?;
match proposal_signing_backend {
ProposalSigningBackendSelector::Dkg(backend) => {
// its safe to use unwrap on linked_anchors here
// since this option is always going to return Some(value).
// linked_anchors are validated in make_proposal_signing_backend() method
let bridge_registry =
DkgBridgeRegistryBackend::new(backend.client.clone());

let deposit_handler = SubstrateVAnchorDepositHandler::new(
backend,
my_config.linked_anchors.unwrap_or_default(),
bridge_registry,
my_config.linked_anchors,
);
let leaves_handler = SubstrateVAnchorLeavesHandler::default();
let encrypted_output_handler =
Expand Down Expand Up @@ -403,11 +406,13 @@ pub fn start_substrate_vanchor_event_watcher(
}
}
ProposalSigningBackendSelector::Mocked(backend) => {
// its safe to use unwrap on linked_anchors here
// since this option is always going to return Some(value).
let bridge_registry =
MockedBridgeRegistryBackend::builder().build();

let deposit_handler = SubstrateVAnchorDepositHandler::new(
backend,
my_config.linked_anchors.unwrap_or_default(),
bridge_registry,
my_config.linked_anchors,
);
let leaves_handler = SubstrateVAnchorLeavesHandler::default();
let encrypted_output_handler =
Expand Down

0 comments on commit d73369e

Please sign in to comment.