Browse Source

Merge #304

304: Re-introduce library r=luckysori a=luckysori

If we want to write proper integration tests, we need to have a library crate.

---

Work towards #231.

Co-authored-by: Lucas Soriano del Pino <l.soriano.del.pino@gmail.com>
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
refactor/no-log-handler
bors[bot] 3 years ago
committed by GitHub
parent
commit
09a938c980
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      daemon/src/actors.rs
  2. 24
      daemon/src/lib.rs
  3. 44
      daemon/src/maker.rs
  4. 3
      daemon/src/maker_cfd.rs
  5. 5
      daemon/src/maker_inc_connections.rs
  6. 3
      daemon/src/monitor.rs
  7. 3
      daemon/src/oracle.rs
  8. 10
      daemon/src/payout_curve.rs
  9. 14
      daemon/src/routes_maker.rs
  10. 10
      daemon/src/routes_taker.rs
  11. 12
      daemon/src/send_to_socket.rs
  12. 43
      daemon/src/taker.rs
  13. 3
      daemon/src/taker_cfd.rs
  14. 2
      daemon/src/wallet_sync.rs
  15. 7
      daemon/src/wire.rs

2
daemon/src/actors.rs

@ -1,4 +1,5 @@
/// Wrapper for handlers to log errors
#[macro_export]
macro_rules! log_error {
($future:expr) => {
if let Err(e) = $future.await {
@ -6,4 +7,3 @@ macro_rules! log_error {
}
};
}
pub(crate) use log_error;

24
daemon/src/lib.rs

@ -0,0 +1,24 @@
pub mod actors;
pub mod auth;
pub mod bitmex_price_feed;
pub mod db;
pub mod housekeeping;
pub mod keypair;
pub mod logger;
pub mod maker_cfd;
pub mod maker_inc_connections;
pub mod model;
pub mod monitor;
pub mod olivia;
pub mod oracle;
pub mod payout_curve;
pub mod routes;
pub mod seed;
pub mod send_to_socket;
pub mod setup_contract;
pub mod taker_cfd;
pub mod to_sse_event;
pub mod tokio_ext;
pub mod wallet;
pub mod wallet_sync;
pub mod wire;

44
daemon/src/maker.rs

@ -1,14 +1,17 @@
use crate::auth::MAKER_USERNAME;
use crate::db::load_all_cfds;
use crate::model::cfd::UpdateCfdProposals;
use crate::seed::Seed;
use crate::wallet::Wallet;
use anyhow::{Context, Result};
use bdk::bitcoin;
use bdk::bitcoin::secp256k1::schnorrsig;
use clap::Clap;
use model::cfd::Order;
use model::WalletInfo;
use daemon::auth::{self, MAKER_USERNAME};
use daemon::db::{self, load_all_cfds};
use daemon::model::cfd::{Order, UpdateCfdProposals};
use daemon::model::WalletInfo;
use daemon::seed::Seed;
use daemon::wallet::Wallet;
use daemon::{
bitmex_price_feed, housekeeping, logger, maker_cfd, maker_inc_connections, monitor, oracle,
wallet_sync,
};
use rocket::fairing::AdHoc;
use rocket_db_pools::Database;
use std::collections::HashMap;
@ -22,30 +25,7 @@ use tracing_subscriber::filter::LevelFilter;
use xtra::prelude::*;
use xtra::spawn::TokioGlobalSpawnExt;
mod actors;
mod auth;
mod bitmex_price_feed;
mod db;
mod housekeeping;
mod keypair;
mod logger;
mod maker_cfd;
mod maker_inc_connections;
mod model;
mod monitor;
mod olivia;
mod oracle;
mod payout_curve;
mod routes;
mod routes_maker;
mod seed;
mod send_to_socket;
mod setup_contract;
mod to_sse_event;
mod tokio_ext;
mod wallet;
mod wallet_sync;
mod wire;
#[derive(Database)]
#[database("maker")]
@ -332,7 +312,3 @@ async fn main() -> Result<()> {
Ok(())
}
impl xtra::Message for wire::MakerToTaker {
type Result = ();
}

3
daemon/src/maker_cfd.rs

@ -1,4 +1,3 @@
use crate::actors::log_error;
use crate::db::{
insert_cfd, insert_new_cfd_state_by_order_id, insert_order, load_all_cfds,
load_cfd_by_order_id, load_cfds_by_oracle_event_id, load_order_by_id,
@ -12,7 +11,7 @@ use crate::model::cfd::{
use crate::model::{TakerId, Usd};
use crate::monitor::MonitorParams;
use crate::wallet::Wallet;
use crate::{maker_inc_connections, monitor, oracle, setup_contract, wire};
use crate::{log_error, maker_inc_connections, monitor, oracle, setup_contract, wire};
use anyhow::{bail, Context as _, Result};
use async_trait::async_trait;
use bdk::bitcoin::secp256k1::schnorrsig;

5
daemon/src/maker_inc_connections.rs

@ -1,7 +1,6 @@
use crate::actors::log_error;
use crate::model::cfd::{Order, OrderId};
use crate::model::{BitMexPriceEventId, TakerId};
use crate::{maker_cfd, send_to_socket, wire};
use crate::{log_error, maker_cfd, send_to_socket, wire};
use anyhow::{Context as AnyhowContext, Result};
use async_trait::async_trait;
use futures::StreamExt;
@ -162,7 +161,7 @@ impl Actor {
tracing::info!("New taker {} connected on {}", taker_id, address);
let (read, write) = stream.into_split();
let read = FramedRead::new(read, wire::JsonCodec::new())
let read = FramedRead::new(read, wire::JsonCodec::default())
.map(move |item| maker_cfd::TakerStreamMessage { taker_id, item });
tokio::spawn(self.cfd_actor.clone().attach_stream(read));

3
daemon/src/monitor.rs

@ -1,8 +1,7 @@
use crate::actors::log_error;
use crate::model::cfd::{CetStatus, Cfd, CfdState, Dlc, OrderId};
use crate::model::BitMexPriceEventId;
use crate::oracle::Attestation;
use crate::{model, oracle};
use crate::{log_error, model, oracle};
use anyhow::{Context, Result};
use async_trait::async_trait;
use bdk::bitcoin::{PublicKey, Script, Txid};

3
daemon/src/oracle.rs

@ -1,7 +1,6 @@
use crate::actors::log_error;
use crate::model::cfd::{Cfd, CfdState};
use crate::model::BitMexPriceEventId;
use crate::tokio_ext;
use crate::{log_error, tokio_ext};
use anyhow::{Context, Result};
use async_trait::async_trait;
use cfd_protocol::secp256k1_zkp::{schnorrsig, SecretKey};

10
daemon/src/payout_curve.rs

@ -19,13 +19,7 @@ mod curve_factory;
mod splineobject;
mod utils;
/// function to generate an iterator of values, heuristically viewed as:
///
/// `[left_price_boundary, right_price_boundary], maker_payout_value`
///
/// with units
///
/// `[Usd, Usd], bitcoin::Amount`
/// Generate a list of [`Payout`]s.
///
/// A key item to note is that although the POC logic has been to imposed
/// that maker goes short every time, there is no reason to make the math
@ -39,7 +33,7 @@ mod utils;
/// in the call to PayoutCurve::new(), so this behaviour can be changed in
/// the future trivially.
///
/// ### Paramters
/// ### Parameters
///
/// * price: BTC-USD exchange rate used to create CFD contract
/// * quantity: Interger number of one-dollar USD contracts contained in the

14
daemon/src/routes_maker.rs

@ -1,11 +1,11 @@
use crate::auth::Authenticated;
use crate::model::cfd::{Cfd, Order, OrderId, Role, UpdateCfdProposals};
use crate::model::{Usd, WalletInfo};
use crate::routes::EmbeddedFileExt;
use crate::to_sse_event::{CfdAction, CfdsWithAuxData, ToSseEvent};
use crate::{bitmex_price_feed, maker_cfd};
use anyhow::Result;
use bdk::bitcoin::Network;
use daemon::auth::Authenticated;
use daemon::model::cfd::{Cfd, Order, OrderId, Role, UpdateCfdProposals};
use daemon::model::{Usd, WalletInfo};
use daemon::routes::EmbeddedFileExt;
use daemon::to_sse_event::{CfdAction, CfdsWithAuxData, ToSseEvent};
use daemon::{bitmex_price_feed, maker_cfd};
use rocket::http::{ContentType, Header, Status};
use rocket::response::stream::EventStream;
use rocket::response::{status, Responder};
@ -231,7 +231,7 @@ pub fn index<'r>(_paths: PathBuf, _auth: Authenticated) -> impl Responder<'r, 's
#[cfg(test)]
mod tests {
use super::*;
use crate::auth::Password;
use daemon::auth::Password;
use rocket::http::{Header, Status};
use rocket::local::blocking::Client;
use rocket::{Build, Rocket};

10
daemon/src/routes_taker.rs

@ -1,9 +1,9 @@
use crate::model::cfd::{calculate_buy_margin, Cfd, Order, OrderId, Role, UpdateCfdProposals};
use crate::model::{Leverage, Usd, WalletInfo};
use crate::routes::EmbeddedFileExt;
use crate::to_sse_event::{CfdAction, CfdsWithAuxData, ToSseEvent};
use crate::{bitmex_price_feed, taker_cfd};
use bdk::bitcoin::{Amount, Network};
use daemon::model::cfd::{calculate_buy_margin, Cfd, Order, OrderId, Role, UpdateCfdProposals};
use daemon::model::{Leverage, Usd, WalletInfo};
use daemon::routes::EmbeddedFileExt;
use daemon::to_sse_event::{CfdAction, CfdsWithAuxData, ToSseEvent};
use daemon::{bitmex_price_feed, taker_cfd};
use rocket::http::{ContentType, Status};
use rocket::response::stream::EventStream;
use rocket::response::{status, Responder};

12
daemon/src/send_to_socket.rs

@ -1,4 +1,4 @@
use crate::wire::JsonCodec;
use crate::wire::{self, JsonCodec};
use futures::SinkExt;
use serde::Serialize;
use std::fmt;
@ -13,7 +13,7 @@ pub struct Actor<T> {
impl<T> Actor<T> {
pub fn new(write: OwnedWriteHalf) -> Self {
Self {
write: FramedWrite::new(write, JsonCodec::new()),
write: FramedWrite::new(write, JsonCodec::default()),
}
}
}
@ -34,3 +34,11 @@ where
}
impl<T: 'static + Send> xtra::Actor for Actor<T> {}
impl xtra::Message for wire::MakerToTaker {
type Result = ();
}
impl xtra::Message for wire::TakerToMaker {
type Result = ();
}

43
daemon/src/taker.rs

@ -1,16 +1,19 @@
use crate::db::load_all_cfds;
use crate::model::cfd::UpdateCfdProposals;
use crate::model::WalletInfo;
use crate::wallet::Wallet;
use anyhow::{Context, Result};
use bdk::bitcoin;
use bdk::bitcoin::secp256k1::schnorrsig;
use clap::Clap;
use daemon::db::{self, load_all_cfds};
use daemon::model::cfd::{Order, UpdateCfdProposals};
use daemon::model::WalletInfo;
use daemon::seed::Seed;
use daemon::wallet::Wallet;
use daemon::{
bitmex_price_feed, housekeeping, logger, monitor, oracle, send_to_socket, taker_cfd,
wallet_sync, wire,
};
use futures::StreamExt;
use model::cfd::Order;
use rocket::fairing::AdHoc;
use rocket_db_pools::Database;
use seed::Seed;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::path::PathBuf;
@ -20,32 +23,10 @@ use std::time::Duration;
use tokio::sync::watch;
use tokio_util::codec::FramedRead;
use tracing_subscriber::filter::LevelFilter;
use wire::TakerToMaker;
use xtra::spawn::TokioGlobalSpawnExt;
use xtra::Actor;
mod actors;
mod bitmex_price_feed;
mod db;
mod housekeeping;
mod keypair;
mod logger;
mod model;
mod monitor;
mod olivia;
mod oracle;
mod payout_curve;
mod routes;
mod routes_taker;
mod seed;
mod send_to_socket;
mod setup_contract;
mod taker_cfd;
mod to_sse_event;
mod tokio_ext;
mod wallet;
mod wallet_sync;
mod wire;
const CONNECTION_RETRY_INTERVAL: Duration = Duration::from_secs(5);
@ -246,7 +227,7 @@ async fn main() -> Result<()> {
.create(None)
.spawn_global();
let read = FramedRead::new(read, wire::JsonCodec::new())
let read = FramedRead::new(read, wire::JsonCodec::default())
.map(move |item| taker_cfd::MakerStreamMessage { item });
tokio::spawn(cfd_actor_inbox.clone().attach_stream(read));
@ -305,7 +286,3 @@ async fn main() -> Result<()> {
Ok(())
}
impl xtra::Message for TakerToMaker {
type Result = ();
}

3
daemon/src/taker_cfd.rs

@ -1,4 +1,3 @@
use crate::actors::log_error;
use crate::db::{
insert_cfd, insert_new_cfd_state_by_order_id, insert_order, load_all_cfds,
load_cfd_by_order_id, load_cfds_by_oracle_event_id, load_order_by_id,
@ -12,7 +11,7 @@ use crate::model::{BitMexPriceEventId, Usd};
use crate::monitor::{self, MonitorParams};
use crate::wallet::Wallet;
use crate::wire::{MakerToTaker, RollOverMsg, SetupMsg};
use crate::{oracle, send_to_socket, setup_contract, wire};
use crate::{log_error, oracle, send_to_socket, setup_contract, wire};
use anyhow::{bail, Context as _, Result};
use async_trait::async_trait;
use bdk::bitcoin::secp256k1::schnorrsig;

2
daemon/src/wallet_sync.rs

@ -1,5 +1,5 @@
use crate::model::WalletInfo;
use crate::wallet::Wallet;
use crate::WalletInfo;
use std::time::Duration;
use tokio::sync::watch;
use tokio::time::sleep;

7
daemon/src/wire.rs

@ -1,6 +1,5 @@
use crate::model::cfd::OrderId;
use crate::model::cfd::{Order, OrderId};
use crate::model::{BitMexPriceEventId, Usd};
use crate::Order;
use anyhow::{bail, Result};
use bdk::bitcoin::secp256k1::Signature;
use bdk::bitcoin::util::psbt::PartiallySignedTransaction;
@ -99,8 +98,8 @@ pub struct JsonCodec<T> {
inner: LengthDelimitedCodec,
}
impl<T> JsonCodec<T> {
pub fn new() -> Self {
impl<T> Default for JsonCodec<T> {
fn default() -> Self {
Self {
_type: PhantomData,
inner: LengthDelimitedCodec::new(),

Loading…
Cancel
Save