diff --git a/daemon/tests/happy_path.rs b/daemon/tests/happy_path.rs index 7829c8b..b2dbc7b 100644 --- a/daemon/tests/happy_path.rs +++ b/daemon/tests/happy_path.rs @@ -2,7 +2,8 @@ use std::time::Duration; use crate::harness::flow::{is_next_none, next, next_cfd, next_order, next_some}; use crate::harness::{ - dummy_new_order, init_tracing, start_both, Maker, MakerConfig, Taker, TakerConfig, + deliver_lock_finality_event, dummy_new_order, init_tracing, start_both, Maker, MakerConfig, + Taker, TakerConfig, }; use daemon::connection::ConnectionStatus; use daemon::model::Usd; @@ -116,6 +117,12 @@ async fn taker_takes_order_and_maker_accepts_and_contract_setup() { assert_eq!(maker_cfd.order_id, received.id); assert!(matches!(taker_cfd.state, CfdState::PendingOpen { .. })); assert!(matches!(maker_cfd.state, CfdState::PendingOpen { .. })); + + deliver_lock_finality_event(&maker, &taker, received.id).await; + + let (taker_cfd, maker_cfd) = next_cfd(taker.cfd_feed(), maker.cfd_feed()).await.unwrap(); + assert!(matches!(taker_cfd.state, CfdState::Open { .. })); + assert!(matches!(maker_cfd.state, CfdState::Open { .. })); } #[tokio::test] diff --git a/daemon/tests/harness/bdk.rs b/daemon/tests/harness/bdk.rs deleted file mode 100644 index 51eb4cb..0000000 --- a/daemon/tests/harness/bdk.rs +++ /dev/null @@ -1,29 +0,0 @@ -use bdk::bitcoin::util::psbt::{Global, PartiallySignedTransaction}; -use bdk::bitcoin::{Transaction, Txid}; -use std::collections::BTreeMap; - -pub fn dummy_partially_signed_transaction() -> PartiallySignedTransaction { - // very simple dummy psbt that does not contain anything - // pulled in from github.com-1ecc6299db9ec823/bitcoin-0.27.1/src/util/psbt/mod.rs:238 - - PartiallySignedTransaction { - global: Global { - unsigned_tx: Transaction { - version: 2, - lock_time: 0, - input: vec![], - output: vec![], - }, - xpub: Default::default(), - version: 0, - proprietary: BTreeMap::new(), - unknown: BTreeMap::new(), - }, - inputs: vec![], - outputs: vec![], - } -} - -pub fn dummy_tx_id() -> Txid { - dummy_partially_signed_transaction().extract_tx().txid() -} diff --git a/daemon/tests/harness/mocks/mod.rs b/daemon/tests/harness/mocks/mod.rs index ec42752..5a67b65 100644 --- a/daemon/tests/harness/mocks/mod.rs +++ b/daemon/tests/harness/mocks/mod.rs @@ -6,8 +6,6 @@ use self::monitor::MonitorActor; use self::oracle::OracleActor; use self::wallet::WalletActor; -use super::bdk::{dummy_partially_signed_transaction, dummy_tx_id}; - pub mod monitor; pub mod oracle; pub mod wallet; @@ -39,19 +37,14 @@ impl Mocks { // Helper function setting up a "happy path" wallet mock pub async fn mock_wallet_sign_and_broadcast(&mut self) { - let mut seq = mockall::Sequence::new(); self.wallet() .await .expect_sign() - .times(1) - .returning(|_| Ok(dummy_partially_signed_transaction())) - .in_sequence(&mut seq); + .returning(|sign_msg| Ok(sign_msg.psbt)); self.wallet() .await .expect_broadcast() - .times(1) - .returning(|_| Ok(dummy_tx_id())) - .in_sequence(&mut seq); + .returning(|broadcast_msg| Ok(broadcast_msg.tx.txid())); } pub async fn mock_oracle_announcement(&mut self) { diff --git a/daemon/tests/harness/mod.rs b/daemon/tests/harness/mod.rs index 3435ea1..69ce9ed 100644 --- a/daemon/tests/harness/mod.rs +++ b/daemon/tests/harness/mod.rs @@ -5,7 +5,7 @@ use crate::schnorrsig; use ::bdk::bitcoin::Network; use daemon::bitmex_price_feed::Quote; use daemon::connection::{connect, ConnectionStatus}; -use daemon::model::cfd::Role; +use daemon::model::cfd::{OrderId, Role}; use daemon::model::{self, Price, Timestamp, Usd}; use daemon::projection::{Cfd, CfdOrder, Feeds, Identity}; use daemon::seed::Seed; @@ -27,7 +27,6 @@ use tracing_subscriber::util::SubscriberInitExt; use tracing_subscriber::EnvFilter; use xtra::Actor; -pub mod bdk; pub mod flow; pub mod maia; pub mod mocks; @@ -174,14 +173,8 @@ impl Maker { .await .unwrap(); - let dummy_quote = Quote { - timestamp: Timestamp::now(), - bid: Price::new(dec!(10000)).unwrap(), - ask: Price::new(dec!(10000)).unwrap(), - }; - let (proj_actor, feeds) = - projection::Actor::new(Role::Maker, Network::Testnet, vec![], dummy_quote); + projection::Actor::new(Role::Maker, Network::Testnet, vec![], dummy_quote()); tasks.add(projection_context.run(proj_actor)); let address = listener.local_addr().unwrap(); @@ -297,14 +290,8 @@ impl Taker { .await .unwrap(); - let dummy_quote = Quote { - timestamp: Timestamp::now(), - bid: Price::new(dec!(10000)).unwrap(), - ask: Price::new(dec!(10000)).unwrap(), - }; - let (proj_actor, feeds) = - projection::Actor::new(Role::Taker, Network::Testnet, vec![], dummy_quote); + projection::Actor::new(Role::Taker, Network::Testnet, vec![], dummy_quote()); tasks.add(projection_context.run(proj_actor)); tasks.add(connect( @@ -336,6 +323,22 @@ impl Taker { } } +/// Deliver the event that provokes the transition to cfd's "Open" state +pub async fn deliver_lock_finality_event(maker: &Maker, taker: &Taker, id: OrderId) { + maker + .system + .cfd_actor_addr + .send(daemon::monitor::Event::LockFinality(id)) + .await + .unwrap(); + taker + .system + .cfd_actor_addr + .send(daemon::monitor::Event::LockFinality(id)) + .await + .unwrap(); +} + async fn in_memory_db() -> SqlitePool { // Note: Every :memory: database is distinct from every other. So, opening two database // connections each with the filename ":memory:" will create two independent in-memory @@ -347,9 +350,21 @@ async fn in_memory_db() -> SqlitePool { pool } +pub fn dummy_price() -> Price { + Price::new(dec!(50_000)).expect("to not fail") +} + +pub fn dummy_quote() -> Quote { + Quote { + timestamp: Timestamp::now(), + bid: dummy_price(), + ask: dummy_price(), + } +} + pub fn dummy_new_order() -> maker_cfd::NewOrder { maker_cfd::NewOrder { - price: Price::new(dec!(50_000)).expect("unexpected failure"), + price: dummy_price(), min_quantity: Usd::new(dec!(5)), max_quantity: Usd::new(dec!(100)), }