Skip to content

Commit

Permalink
erc20 init services
Browse files Browse the repository at this point in the history
  • Loading branch information
F3kilo committed Oct 21, 2024
1 parent 38d01f9 commit 491dbc6
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 77 deletions.
9 changes: 6 additions & 3 deletions src/brc20-bridge/src/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,12 @@ fn init_runtime() -> SharedRuntime {
let mint_tx_handler = Brc20MintTxHandler::new(state.clone());
let mint_tx_service = Rc::new(SendMintTxService::new(mint_tx_handler));

let bft_events_handler = Brc20BftEventsHandler::new(runtime.clone());
let fetch_bft_events_service =
Rc::new(FetchBftBridgeEventsService::new(bft_events_handler, config));
let bft_events_handler = Brc20BftEventsHandler::new(get_brc20_state());
let fetch_bft_events_service = Rc::new(FetchBftBridgeEventsService::new(
bft_events_handler,
runtime.clone(),
config,
));

let services = state.borrow().services.clone();
services.borrow_mut().add_service(
Expand Down
19 changes: 9 additions & 10 deletions src/brc20-bridge/src/ops/events_handler.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
use std::cell::RefCell;
use std::rc::Rc;

use bridge_canister::bridge::OperationAction;
use bridge_canister::runtime::service::fetch_logs::BftBridgeEventHandler;
use bridge_canister::runtime::RuntimeState;
use bridge_did::event_data::{BurntEventData, MintedEventData, NotifyMinterEventData};
use bridge_did::operations::{
Brc20BridgeDepositOp, Brc20BridgeOp, Brc20BridgeWithdrawOp, DepositRequest,
};

use crate::canister::{get_brc20_state, SharedRuntime};
use crate::core::withdrawal;
use crate::ops::{Brc20BridgeOpImpl, Brc20MinterNotification};
use crate::state::Brc20State;

/// Decsribes event processing logic.
pub struct Brc20BftEventsHandler {
runtime: SharedRuntime,
brc20_state: Rc<RefCell<Brc20State>>,
}

impl Brc20BftEventsHandler {
pub fn new(runtime: SharedRuntime) -> Self {
Self { runtime }
}

fn state(&self) -> RuntimeState<Brc20BridgeOpImpl> {
self.runtime.borrow().state().clone()
pub fn new(brc20_state: Rc<RefCell<Brc20State>>) -> Self {
Self { brc20_state }
}
}

Expand Down Expand Up @@ -48,7 +47,7 @@ impl BftBridgeEventHandler<Brc20BridgeOpImpl> for Brc20BftEventsHandler {
) -> Option<OperationAction<Brc20BridgeOpImpl>> {
log::debug!("on_wrapped_token_burnt {event:?}");
let memo = event.memo();
let op = match withdrawal::new_withdraw_payload(event, &get_brc20_state().borrow()) {
let op = match withdrawal::new_withdraw_payload(event, &self.brc20_state.borrow()) {
Ok(payload) => Brc20BridgeOpImpl(Brc20BridgeOp::Withdraw(
Brc20BridgeWithdrawOp::CreateInscriptionTxs(payload),
)),
Expand Down
2 changes: 0 additions & 2 deletions src/btc-bridge/src/ops/events_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use bridge_canister::bridge::OperationAction;
use bridge_canister::runtime::service::fetch_logs::BftBridgeEventHandler;
use bridge_canister::runtime::RuntimeState;
use bridge_did::error::{BftResult, Error};
use bridge_did::event_data::{BurntEventData, MintedEventData, NotifyMinterEventData};
use bridge_did::operations::BtcBridgeOp;
use bridge_did::reason::BtcDeposit;
Expand Down
49 changes: 42 additions & 7 deletions src/erc20-bridge/src/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use std::cell::RefCell;
use std::rc::Rc;

use bridge_canister::memory::{memory_by_id, StableMemory};
use bridge_canister::runtime::service::fetch_logs::FetchBftBridgeEventsService;
use bridge_canister::runtime::service::mint_tx::SendMintTxService;
use bridge_canister::runtime::service::sign_orders::SignMintOrdersService;
use bridge_canister::runtime::service::ServiceOrder;
use bridge_canister::runtime::state::config::ConfigStorage;
use bridge_canister::runtime::state::SharedConfig;
use bridge_canister::runtime::{BridgeRuntime, RuntimeState};
use bridge_canister::BridgeCanister;
use bridge_did::bridge_side::BridgeSide;
use bridge_did::init::BridgeInitData;
use bridge_did::op_id::OperationId;
use bridge_did::operation_log::{Memo, OperationLog};
Expand All @@ -23,9 +25,10 @@ use ic_stable_structures::StableCell;
use ic_storage::IcStorage;

use crate::memory::NONCE_COUNTER_MEMORY_ID;
use crate::ops::events_handler::Erc20EventsHandler;
use crate::ops::{
Erc20BridgeOpImpl, Erc20OrderHandler, Erc20ServiceSelector, SEND_MINT_TX_SERVICE_ID,
SIGN_MINT_ORDER_SERVICE_ID,
Erc20BridgeOpImpl, Erc20OrderHandler, Erc20ServiceSelector, FETCH_BASE_LOGS_SERVICE_ID,
FETCH_WRAPPED_LOGS_SERVICE_ID, SEND_MINT_TX_SERVICE_ID, SIGN_MINT_ORDER_SERVICE_ID,
};
use crate::state::{BaseEvmSettings, SharedBaseEvmState};

Expand Down Expand Up @@ -151,13 +154,35 @@ impl LogCanister for Erc20Bridge {

fn init_runtime() -> SharedRuntime {
let runtime = BridgeRuntime::default(ConfigStorage::get());
let state = runtime.state();
let base_evm_config = get_base_evm_config();
let state = runtime.state().clone();
let base_config = get_base_evm_config();
let wrapped_config = state.borrow().config.clone();
let scheduler = runtime.scheduler().clone();
let runtime = Rc::new(RefCell::new(runtime));

// Init event listener services
let base_event_handler = Erc20EventsHandler::new(
get_mint_order_nonce_counter(),
BridgeSide::Base,
base_config.clone(),
wrapped_config.clone(),
);
let base_events_service =
FetchBftBridgeEventsService::new(base_event_handler, runtime.clone(), base_config.clone());
let wrapped_event_handler = Erc20EventsHandler::new(
get_mint_order_nonce_counter(),
BridgeSide::Wrapped,
wrapped_config.clone(),
base_config.clone(),
);
let wrapped_events_service = FetchBftBridgeEventsService::new(
wrapped_event_handler,
runtime.clone(),
wrapped_config.clone(),
);

// Init operation handlers
let base_handler = Erc20OrderHandler::new(state.clone(), base_evm_config, scheduler.clone());
let wrapped_config = state.borrow().config.clone();
let base_handler = Erc20OrderHandler::new(state.clone(), base_config, scheduler.clone());
let wrapped_handler = Erc20OrderHandler::new(state.clone(), wrapped_config, scheduler.clone());

// Init mint order signing service
Expand All @@ -172,6 +197,16 @@ fn init_runtime() -> SharedRuntime {
Erc20ServiceSelector::new(base_mint_tx_service, wrapped_mint_tx_service);

let services = state.borrow().services.clone();
services.borrow_mut().add_service(
ServiceOrder::BeforeOperations,
FETCH_BASE_LOGS_SERVICE_ID,
Rc::new(base_events_service),
);
services.borrow_mut().add_service(
ServiceOrder::BeforeOperations,
FETCH_WRAPPED_LOGS_SERVICE_ID,
Rc::new(wrapped_events_service),
);
services.borrow_mut().add_service(
ServiceOrder::ConcurrentWithOperations,
SIGN_MINT_ORDER_SERVICE_ID,
Expand All @@ -183,7 +218,7 @@ fn init_runtime() -> SharedRuntime {
Rc::new(send_mint_tx_service),
);

Rc::new(RefCell::new(runtime))
runtime
}

pub type SharedNonceCounter = Rc<RefCell<StableCell<u32, StableMemory>>>;
Expand Down
8 changes: 5 additions & 3 deletions src/erc20-bridge/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ use serde::{Deserialize, Serialize};

use crate::canister::get_runtime_state;

mod events_handler;
pub mod events_handler;

pub const SIGN_MINT_ORDER_SERVICE_ID: ServiceId = 0;
pub const SEND_MINT_TX_SERVICE_ID: ServiceId = 1;
pub const FETCH_BASE_LOGS_SERVICE_ID: ServiceId = 0;
pub const FETCH_WRAPPED_LOGS_SERVICE_ID: ServiceId = 1;
pub const SIGN_MINT_ORDER_SERVICE_ID: ServiceId = 2;
pub const SEND_MINT_TX_SERVICE_ID: ServiceId = 3;

#[derive(Debug, Serialize, Deserialize, CandidType, Clone)]
pub struct Erc20BridgeOpImpl(pub Erc20BridgeOp);
Expand Down
2 changes: 2 additions & 0 deletions src/erc20-bridge/src/ops/events_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub struct Erc20EventsHandler {
}

impl Erc20EventsHandler {
/// Creates new events handler instance, which listens `src_evm` and
/// create operations for `dst_evm`.
pub fn new(
nonce_counter: SharedNonceCounter,
side: BridgeSide,
Expand Down
57 changes: 31 additions & 26 deletions src/rune-bridge/src/ops/tests/deposit_request.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::collections::HashMap;

use bridge_canister::bridge::{Operation, OperationAction};
use bridge_canister::runtime::service::fetch_logs::BftBridgeEventHandler;
use bridge_canister::{
bridge::OperationAction, runtime::service::fetch_logs::BftBridgeEventHandler,
};
use bridge_did::event_data::*;
use bridge_did::runes::RuneName;
use candid::Encode;
Expand All @@ -26,16 +27,16 @@ async fn invalid_notification_type_is_noop() {
memo: vec![],
};

let handler = RuneEventsHandler::new(tests::test_runtime(), tests::test_rune_state());
let handler = RuneEventsHandler::new(tests::test_rune_state());
let result = handler.on_minter_notification(event.clone());
assert!(result.is_err());
assert!(result.is_none());

let event = NotifyMinterEventData {
notification_type: MinterNotificationType::Other,
..event
};
let result = handler.on_minter_notification(event);
assert!(result.is_err());
assert!(result.is_none());
}

#[tokio::test]
Expand All @@ -55,16 +56,16 @@ async fn invalid_notification_payload_is_noop() {
memo: vec![],
};

let handler = RuneEventsHandler::new(tests::test_runtime(), tests::test_rune_state());
let handler = RuneEventsHandler::new(tests::test_rune_state());
let result = handler.on_minter_notification(event.clone());
assert!(result.is_err());
assert!(result.is_none());

let event = NotifyMinterEventData {
user_data: vec![],
..event
};
let result = handler.on_minter_notification(event.clone());
assert!(result.is_err());
assert!(result.is_none());
}

#[tokio::test]
Expand All @@ -83,9 +84,19 @@ async fn deposit_request_creates_correct_operation() {
memo: vec![],
};

let handler = RuneEventsHandler::new(tests::test_runtime(), tests::test_rune_state());
let handler = RuneEventsHandler::new(tests::test_rune_state());
let result = handler.on_minter_notification(event.clone());
assert!(result.is_ok());
assert_eq!(
result,
Some(OperationAction::Create(
RuneBridgeOpImpl(RuneBridgeOp::Deposit(RuneBridgeDepositOp::AwaitInputs {
dst_address: tests::sender(),
dst_tokens: tests::dst_tokens(),
requested_amounts: None,
})),
None
))
)
}

#[tokio::test]
Expand All @@ -105,23 +116,17 @@ async fn deposit_request_adds_amounts_to_operation() {
memo: vec![],
};

let handler = RuneEventsHandler::new(tests::test_runtime(), tests::test_rune_state());
let handler = RuneEventsHandler::new(tests::test_rune_state());
let result = handler.on_minter_notification(event.clone());
assert!(result.is_ok());

let (_, op) = tests::test_state()
.borrow_mut()
.operations
.get_for_address(&tests::sender(), None)
.first()
.cloned()
.unwrap();
assert_eq!(
op,
RuneBridgeOpImpl(RuneBridgeOp::Deposit(RuneBridgeDepositOp::AwaitInputs {
dst_address: tests::sender(),
dst_tokens: tests::dst_tokens(),
requested_amounts: Some(amounts),
}))
result,
Some(OperationAction::Create(
RuneBridgeOpImpl(RuneBridgeOp::Deposit(RuneBridgeDepositOp::AwaitInputs {
dst_address: tests::sender(),
dst_tokens: tests::dst_tokens(),
requested_amounts: Some(amounts),
})),
None
))
)
}
Loading

0 comments on commit 491dbc6

Please sign in to comment.