From 8a520b6d28e81bab7f7d695bd4a03a2aaa3c78e8 Mon Sep 17 00:00:00 2001 From: Philipp Hoenisch Date: Thu, 29 Jul 2021 11:10:09 +1000 Subject: [PATCH] Remove fake_bobtimus and make the faucet a compile time feature. This simplifies our code-base as we now have only one binary to maintain. Also, the exchange rate in the UI are now real world values. Signed-off-by: Philipp Hoenisch --- bobtimus/Cargo.toml | 4 + bobtimus/src/bin/bobtimus.rs | 91 ++++++++++++++++++- bobtimus/src/bin/fake_bobtimus.rs | 145 ------------------------------ e2e_tests/e2e_test_setup.sh | 4 +- waves-scripts | 4 +- 5 files changed, 98 insertions(+), 150 deletions(-) delete mode 100644 bobtimus/src/bin/fake_bobtimus.rs diff --git a/bobtimus/Cargo.toml b/bobtimus/Cargo.toml index 8a7cf2fd..63771a83 100644 --- a/bobtimus/Cargo.toml +++ b/bobtimus/Cargo.toml @@ -37,3 +37,7 @@ warp = { version = "0.3", default-features = false, features = [ "tls" ] } [dev-dependencies] testcontainers = "0.12" + +[features] +default = [ ] +faucet = [ ] diff --git a/bobtimus/src/bin/bobtimus.rs b/bobtimus/src/bin/bobtimus.rs index 76d4be83..a2f9de45 100644 --- a/bobtimus/src/bin/bobtimus.rs +++ b/bobtimus/src/bin/bobtimus.rs @@ -50,7 +50,28 @@ async fn main() -> Result<()> { }); let http = http.map(|listen_http| { - warp::serve(http::routes(bobtimus, subscription)).run(listen_http) + let filter = http::routes(bobtimus.clone(), subscription); + + #[cfg(feature = "faucet")] + let filter = { + use elements::Address; + use warp::Filter; + + let cors = warp::cors().allow_any_origin(); + + let cloned_bobtimus = bobtimus.clone(); + let maybe_faucet = warp::post() + .and(warp::path!("api" / "faucet" / Address)) + .and_then(move |address| { + let bobtimus = cloned_bobtimus.clone(); + async move { + let mut bobtimus = bobtimus.lock().await; + faucet::faucet(&mut bobtimus, address).await + } + }); + filter.or(maybe_faucet).with(cors) + }; + warp::serve(filter).run(listen_http) }); match (http, https) { @@ -79,3 +100,71 @@ async fn main() -> Result<()> { Ok(()) } + +#[cfg(feature = "faucet")] +mod faucet { + use super::*; + use bobtimus::{elements_rpc::ElementsRpc, LiquidUsdt}; + use elements::{bitcoin::Amount, Address}; + use warp::{Rejection, Reply}; + + pub(crate) async fn faucet( + bobtimus: &mut Bobtimus, + address: Address, + ) -> Result { + let mut txids = Vec::new(); + for (asset_id, amount) in &[ + (bobtimus.btc_asset_id, Amount::from_sat(1_000_000_000)), + ( + bobtimus.usdt_asset_id, + LiquidUsdt::from_str_in_dollar("200000.0") + .expect("valid dollars") + .into(), + ), + ] { + let txid = bobtimus + .elementsd + .send_asset_to_address(&address, *amount, Some(*asset_id)) + .await + .map_err(|e| { + tracing::error!( + "could not fund address {} with asset {}: {}", + address, + asset_id, + e + ); + warp::reject::reject() + })?; + + txids.push(txid); + } + + let _ = bobtimus + .elementsd + .reissueasset(bobtimus.usdt_asset_id, 200000.0) + .await + .map_err(|e| { + tracing::error!("could not reissue asset: {}", e); + warp::reject::reject() + })?; + + let address = bobtimus + .elementsd + .get_new_segwit_confidential_address() + .await + .map_err(|e| { + tracing::error!("could not get new address: {}", e); + warp::reject::reject() + })?; + bobtimus + .elementsd + .generatetoaddress(1, &address) + .await + .map_err(|e| { + tracing::error!("could not generate block: {}", e); + warp::reject::reject() + })?; + + Ok(warp::reply::json(&txids)) + } +} diff --git a/bobtimus/src/bin/fake_bobtimus.rs b/bobtimus/src/bin/fake_bobtimus.rs deleted file mode 100644 index dd3120e2..00000000 --- a/bobtimus/src/bin/fake_bobtimus.rs +++ /dev/null @@ -1,145 +0,0 @@ -use anyhow::{bail, Result}; -use bobtimus::{ - cli::Config, - database::Sqlite, - elements_rpc::{Client, ElementsRpc}, - fixed_rate, http, liquidate_loans, Bobtimus, LiquidUsdt, -}; -use elements::{ - bitcoin::{secp256k1::Secp256k1, Amount}, - secp256k1_zkp::rand::{rngs::StdRng, thread_rng, SeedableRng}, - Address, -}; -use std::{collections::HashMap, sync::Arc}; -use tokio::sync::Mutex; -use warp::{Filter, Rejection, Reply}; - -#[tokio::main] -async fn main() -> Result<()> { - tracing_subscriber::fmt::init(); - - match Config::parse()? { - Config::Start { - elementsd_url, - http, - usdt_asset_id, - db_file, - https, - } => { - let db = Sqlite::new(db_file.as_path())?; - - let elementsd = Client::new(elementsd_url.into())?; - let btc_asset_id = elementsd.get_bitcoin_asset_id().await?; - - let rate_service = fixed_rate::Service::new(); - let subscription = rate_service.subscribe(); - - let bobtimus = Bobtimus { - rng: StdRng::from_rng(&mut thread_rng()).unwrap(), - rate_service, - secp: Secp256k1::new(), - elementsd, - btc_asset_id, - usdt_asset_id, - db, - lender_states: HashMap::new(), - }; - let bobtimus = Arc::new(Mutex::new(bobtimus)); - - let routes = http::routes(bobtimus.clone(), subscription); - - let cors = warp::cors().allow_any_origin(); - - let faucet = warp::post() - .and(warp::path!("api" / "faucet" / Address)) - .and_then(move |address| { - let bobtimus = bobtimus.clone(); - async move { - let mut bobtimus = bobtimus.lock().await; - faucet(&mut bobtimus, address).await - } - }); - - if https.is_some() { - bail!("Fake Bobtimus is not configured to run on https"); - } - - if let Some(listen_http) = http { - warp::serve(routes.or(faucet).with(cors)) - .run(listen_http) - .await; - } - } - Config::LiquidateLoans { - elementsd_url, - db_file, - } => { - let db = Sqlite::new(db_file.as_path())?; - let elementsd = Client::new(elementsd_url.into())?; - - liquidate_loans(&elementsd, db).await?; - } - }; - - Ok(()) -} - -async fn faucet( - bobtimus: &mut Bobtimus, - address: Address, -) -> Result { - let mut txids = Vec::new(); - for (asset_id, amount) in &[ - (bobtimus.btc_asset_id, Amount::from_sat(1_000_000_000)), - ( - bobtimus.usdt_asset_id, - LiquidUsdt::from_str_in_dollar("200000.0") - .expect("valid dollars") - .into(), - ), - ] { - let txid = bobtimus - .elementsd - .send_asset_to_address(&address, *amount, Some(*asset_id)) - .await - .map_err(|e| { - tracing::error!( - "could not fund address {} with asset {}: {}", - address, - asset_id, - e - ); - warp::reject::reject() - })?; - - txids.push(txid); - } - - let _ = bobtimus - .elementsd - .reissueasset(bobtimus.usdt_asset_id, 200000.0) - .await - .map_err(|e| { - tracing::error!("could not reissue asset: {}", e); - warp::reject::reject() - })?; - - let address = bobtimus - .elementsd - .get_new_segwit_confidential_address() - .await - .map_err(|e| { - tracing::error!("could not get new address: {}", e); - warp::reject::reject() - })?; - bobtimus - .elementsd - .generatetoaddress(1, &address) - .await - .map_err(|e| { - tracing::error!("could not generate block: {}", e); - warp::reject::reject() - })?; - - Ok(warp::reply::json(&txids)) -} diff --git a/e2e_tests/e2e_test_setup.sh b/e2e_tests/e2e_test_setup.sh index a01610d8..b34933a8 100755 --- a/e2e_tests/e2e_test_setup.sh +++ b/e2e_tests/e2e_test_setup.sh @@ -51,8 +51,8 @@ echo "Bitcoin Asset ID: "$btc_asset_id cd ../ - cargo build --bin fake_bobtimus - RUST_LOG=debug,hyper=info,reqwest=info cargo run --bin fake_bobtimus -- \ + cargo build --bin bobtimus --features faucet + RUST_LOG=debug,hyper=info,reqwest=info cargo run --bin bobtimus --features faucet -- \ start \ --http 127.0.0.1:3030 \ --elementsd http://admin1:123@localhost:18884 \ diff --git a/waves-scripts b/waves-scripts index d04eff98..9b352484 100755 --- a/waves-scripts +++ b/waves-scripts @@ -90,12 +90,12 @@ start_bobtimus () { __ensure_log_dir __read_env_file - cargo build --bin fake_bobtimus + cargo build --bin bobtimus --features faucet local -r elementsd_rpc_user="admin1" local -r elementsd_rpc_password="123" - RUST_LOG=debug,hyper=info,reqwest=info cargo run --bin fake_bobtimus -- \ + RUST_LOG=debug,hyper=info,reqwest=info cargo run --bin bobtimus --features faucet -- \ start \ --http 127.0.0.1:3030 \ --elementsd http://$elementsd_rpc_user:$elementsd_rpc_password@127.0.0.1:$LIQUID_NODE_PORT \