From ac2cfd7f656cfa1f7ce301ac888df294232d33e1 Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Tue, 3 Nov 2020 16:44:04 +1100 Subject: [PATCH] Print swap history on --history flag --- swap/Cargo.toml | 1 + swap/src/main.rs | 12 +++++------- swap/src/storage.rs | 38 +++++++++++++++++++++++++++++++++++++- swap/tests/e2e.rs | 5 +++++ 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/swap/Cargo.toml b/swap/Cargo.toml index 3d66058a..71e88626 100644 --- a/swap/Cargo.toml +++ b/swap/Cargo.toml @@ -21,6 +21,7 @@ libp2p-tokio-socks5 = "0.4" log = { version = "0.4", features = ["serde"] } monero = { version = "0.9", features = ["serde_support"] } monero-harness = { path = "../monero-harness" } +prettytable-rs = "0.8" rand = "0.7" reqwest = { version = "0.10", default-features = false, features = ["socks"] } serde = { version = "1", features = ["derive"] } diff --git a/swap/src/main.rs b/swap/src/main.rs index 5f57553d..d7c1fe23 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -16,6 +16,7 @@ use anyhow::Result; use futures::{channel::mpsc, StreamExt}; use libp2p::Multiaddr; use log::LevelFilter; +use prettytable::{row, Table}; use std::{io, io::Write, process, sync::Arc}; use structopt::StructOpt; use swap::{ @@ -30,6 +31,9 @@ use swap::{ use tempfile::tempdir; use tracing::info; +#[macro_use] +extern crate prettytable; + mod cli; mod trace; @@ -81,13 +85,7 @@ async fn main() -> Result<()> { let monero_wallet = Arc::new(monero::Wallet::new(monerod_url)); - let db = Database::open(db_dir.path()).unwrap(); - - swap_as_alice( - bitcoin_wallet, - monero_wallet, - db - listen_addr, + swap_as_alice(bitcoin_wallet, monero_wallet, dblisten_addr, transport, behaviour, ) diff --git a/swap/src/storage.rs b/swap/src/storage.rs index 3a797df9..8a1fc5ed 100644 --- a/swap/src/storage.rs +++ b/swap/src/storage.rs @@ -1,6 +1,6 @@ use anyhow::{anyhow, bail, Context, Result}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; -use std::path::Path; +use std::{fmt::Display, path::Path}; use uuid::Uuid; use xmr_btc::{alice, bob, monero, serde::monero_private_key}; @@ -53,6 +53,42 @@ impl From for Swap { } } +impl Display for Swap { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Swap::Alice(alice) => Display::fmt(alice, f), + Swap::Bob(bob) => Display::fmt(bob, f), + } + } +} + +impl Display for Alice { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Alice::Handshaken(_) => f.write_str("Handshake complete"), + Alice::BtcLocked(_) => f.write_str("Bitcoin locked"), + Alice::XmrLocked(_) => f.write_str("Monero locked"), + Alice::BtcRedeemable { .. } => f.write_str("Bitcoin redeemable"), + Alice::BtcPunishable(_) => f.write_str("Bitcoin punishable"), + Alice::BtcRefunded { .. } => f.write_str("Monero refundable"), + Alice::SwapComplete => f.write_str("Swap complete"), + } + } +} + +impl Display for Bob { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Bob::Handshaken(_) => f.write_str("Handshake complete"), + Bob::BtcLocked(_) | Bob::XmrLocked(_) | Bob::BtcRefundable(_) => { + f.write_str("Bitcoin refundable") + } + Bob::BtcRedeemed(_) => f.write_str("Monero redeemable"), + Bob::SwapComplete => f.write_str("Swap complete"), + } + } +} + pub struct Database(sled::Db); impl Database { diff --git a/swap/tests/e2e.rs b/swap/tests/e2e.rs index 90590e69..1f293d3c 100644 --- a/swap/tests/e2e.rs +++ b/swap/tests/e2e.rs @@ -8,6 +8,11 @@ mod e2e_test { use swap::{alice, bob, network::transport::build}; use testcontainers::clients::Cli; + // NOTE: For some reason running these tests overflows the stack. In order to + // mitigate this run them with: + // + // RUST_MIN_STACK=100000000 cargo test + #[tokio::test] async fn swap() { let alice_multiaddr: Multiaddr = "/ip4/127.0.0.1/tcp/9876"