depolymerization

wire gateway for Bitcoin/Ethereum
Log | Files | Refs | Submodules | README | LICENSE

commit 9c49f61e1591b445fcb07efb68248a2238df1b70
parent 5d96e3d3dee1db26967b7b23cfa35a0e13790127
Author: Antoine A <>
Date:   Wed,  8 Dec 2021 14:55:38 +0100

Complete history support with tests

Diffstat:
Mbtc-wire/Cargo.toml | 2+-
Ascript/generate_in_tx.sh | 8++++++++
Ascript/reset_database.sh | 2++
Mscript/test_btc_wire.sh | 15++++++++++-----
Mscript/test_gateway.sh | 61++++++++++++++++++++++++++++++++++++++++++-------------------
Mwire-gateway/src/main.rs | 19+++++++++++++++++--
6 files changed, 80 insertions(+), 27 deletions(-)

diff --git a/btc-wire/Cargo.toml b/btc-wire/Cargo.toml @@ -27,7 +27,7 @@ url = { version = "2.2.2", features = ["serde"] } # Wire gateway api wire-gateway = { path = "../wire-gateway" } # Ansi color -owo-colors = "3.1.0" +owo-colors = "3.1.1" [dev-dependencies] # statistics-driven micro-benchmarks diff --git a/script/generate_in_tx.sh b/script/generate_in_tx.sh @@ -0,0 +1,7 @@ +for n in `seq 0 $1`; do + RAND=$RANDOM + btc-wire-cli transfer 0.0000$RANDOM + echo "BTC0.0000$RANDOM" +done +btc-wire-cli nblock +echo "$1 incoming transaction generated" +\ No newline at end of file diff --git a/script/reset_database.sh b/script/reset_database.sh @@ -0,0 +1 @@ +sudo -u postgres psql < wire-gateway/db/schema.sql > /dev/null +\ No newline at end of file diff --git a/script/test_btc_wire.sh b/script/test_btc_wire.sh @@ -15,6 +15,8 @@ trap cleanup EXIT BANK_ENDPOINT=http://localhost:8080/ +echo "---- Setup -----" + echo "Reset database" sudo -u postgres psql < wire-gateway/db/schema.sql > /dev/null echo "Reset bitcoin regtest" @@ -31,22 +33,24 @@ echo "Start gateway" cargo build --bin wire-gateway --features test &> /dev/null target/debug/wire-gateway &> /dev/null & for n in `seq 1 50`; do - echo -n "-" + echo -n "." sleep 0.2 curl -s $BANK_ENDPOINT -o /dev/null && break done echo "" -echo -n "Making wire transfer to exchange ..." +echo "---- Gateway API -----" + +echo -n "Making wire transfer to exchange:" btc-wire-cli transfer 0.00004 btc-wire-cli nblock echo " OK" -echo -n "Requesting exchange incoming transaction list ..." +echo -n "Requesting exchange incoming transaction list:" taler-exchange-wire-gateway-client -b $BANK_ENDPOINT -i | grep BTC:0.00004 > /dev/null echo " OK" -echo -n "Making wire transfer from exchange..." +echo -n "Making wire transfer from exchange:" ADDRESS=`bitcoin-cli -rpcwallet=client getnewaddress` taler-exchange-wire-gateway-client \ @@ -57,9 +61,10 @@ btc-wire-cli nblock echo " OK" -echo -n "Requesting exchange's outgoing transaction list..." +echo -n "Requesting exchange's outgoing transaction list:" taler-exchange-wire-gateway-client -b $BANK_ENDPOINT -o | grep BTC:0.00002 > /dev/null echo " OK" + echo "All tests passed" \ No newline at end of file diff --git a/script/test_gateway.sh b/script/test_gateway.sh @@ -15,13 +15,15 @@ trap cleanup EXIT BANK_ENDPOINT=http://127.0.0.1:8080/ +echo "---- Setup -----" + echo "Reset database" sudo -u postgres psql < wire-gateway/db/schema.sql > /dev/null echo "Start gateway" cargo build --bin wire-gateway --features test &> /dev/null target/debug/wire-gateway &> /dev/null & for n in `seq 1 50`; do - echo -n "-" + echo -n "." sleep 0.2 curl -s $BANK_ENDPOINT -o /dev/null && break done @@ -29,25 +31,27 @@ echo "" # Get client address ADDRESS=address +BANK_ENDPOINT=http://127.0.0.1:8080/ -echo -n "Making wire transfer to exchange ..." +echo "---- Gateway API -----" + +echo -n "Making wire transfer to exchange:" for n in `seq 1 9`; do taler-exchange-wire-gateway-client \ -b $BANK_ENDPOINT \ - -S `openssl rand -hex 26` \ -D payto://bitcoin/$ADDRESS \ -a BTC:0.0000$n > /dev/null done echo " OK" -echo -n "Requesting exchange incoming transaction list ..." +echo -n "Requesting exchange incoming transaction list:" ALL=`taler-exchange-wire-gateway-client -b $BANK_ENDPOINT -i` for n in `seq 1 9`; do echo $ALL | grep BTC:0.0000$n > /dev/null done echo " OK" -echo -n "Making wire transfer from exchange..." +echo -n "Making wire transfer from exchange:" for n in `seq 1 9`; do taler-exchange-wire-gateway-client \ -b $BANK_ENDPOINT \ @@ -57,28 +61,24 @@ done echo " OK" -echo -n "Requesting exchange's outgoing transaction list..." +echo -n "Requesting exchange's outgoing transaction list:" ALL=`taler-exchange-wire-gateway-client -b $BANK_ENDPOINT -o` for n in `seq 1 9`; do echo $ALL | grep BTC:0.0000$n > /dev/null done echo " OK" +echo "---- Endpoint & Method -----" -echo "All tests passed" - - -# ---- Endpoint & Method ----- # - -echo -n "Unknown endpoint..." +echo -n "Unknown endpoint:" test `curl -w %{http_code} -s -o /dev/null ${BANK_ENDPOINT}test` -eq 404 && echo " OK" || echo " Failed" -echo -n "Method not allowed..." +echo -n "Method not allowed:" test `curl -w %{http_code} -s -o /dev/null ${BANK_ENDPOINT}transfer` -eq 405 && echo " OK" || echo " Failed" -# ----- Request format ----- # +echo "----- Request format -----" -echo -n "Bad payto url..." +echo -n "Bad payto url:" for bad_payto in http://bitcoin/$ADDRESS payto://btc/$ADDRESS payto://bitcoin/$ADDRESS?id=admin payto://bitcoin/$ADDRESS#admin; do taler-exchange-wire-gateway-client -b $BANK_ENDPOINT -C $bad_payto -a BTC:0.00042 2>&1 | grep -q "(400/24)" && echo -n " OK" || echo -n " Failed" done @@ -87,12 +87,35 @@ echo "" #echo -n "Bad bitcoin address..." #taler-exchange-wire-gateway-client -b $BANK_ENDPOINT -C payto://bitcoin/ADDRESS -a BTC:0.00042 2>&1 | grep -q "(400/26)" && echo " OK" || echo " Failed" -echo -n "Bad transaction amount..." +echo -n "Bad transaction amount:" taler-exchange-wire-gateway-client -b $BANK_ENDPOINT -C payto://bitcoin/$ADDRESS -a ATC:0.00042 2>&1 | grep -q "(400/26)" && echo " OK" || echo " Failed" -echo -n "Bad history delta..." +echo -n "Bad history delta:" for bad_delta in incoming outgoing incoming?delta=0 outgoing?delta=0; do - test `curl -w %{http_code} -s -o /dev/null ${BANK_ENDPOINT}history/$bad_delta` -eq 400 && echo -n " OK" || echo -n " Failed" + test `curl -w %{http_code} -s -o /dev/null "${BANK_ENDPOINT}history/$bad_delta"` -eq 400 && echo -n " OK" || echo -n " Failed" done echo "" -exit 0 + +echo "----- History delta -----" + +function check_delta() { + ALL=`curl -s ${BANK_ENDPOINT}history/$1` + test `echo $ALL | grep -o BTC | wc -l` -eq $2 || return 1; + for n in `$3`; do + echo $ALL | grep BTC:0.0000$n > /dev/null || return 1; + done +} + +for endpoint in incoming outgoing; do + echo -n "History $endpoint" + check_delta ${endpoint}?delta=-9 9 "seq 1 9" && echo -n " OK" || echo -n " Failed" + check_delta ${endpoint}?delta=9 9 "seq 1 9" && echo -n " OK" || echo -n " Failed" + check_delta ${endpoint}?delta=-4 4 "seq 6 9" && echo -n " OK" || echo -n " Failed" + check_delta ${endpoint}?delta=4 4 "seq 1 4" && echo -n " OK" || echo -n " Failed" + check_delta "${endpoint}?delta=-3&start=5" 3 "seq 2 4" && echo -n " OK" || echo -n " Failed" + check_delta "${endpoint}?delta=3&start=4" 3 "seq 5 7" && echo -n " OK" || echo -n " Failed" + echo "" +done + + +echo "All tests passed" diff --git a/wire-gateway/src/main.rs b/wire-gateway/src/main.rs @@ -156,6 +156,18 @@ fn history_params(parts: &Parts) -> Result<HistoryParams, ServerErr> { Ok(params) } +fn sql_history_filter(params: &HistoryParams) -> String { + let asc = params.delta > 0; + let limit = params.delta.abs(); + let order_sql = if asc { "ASC" } else { "DESC" }; + let where_sql = if let Some(start) = params.start { + format!("WHERE id {} {}", if asc { '>' } else { '<' }, start) + } else { + String::new() + }; + format!("{} ORDER BY id {} LIMIT {}", where_sql, order_sql, limit) +} + async fn router( req: Request<Body>, state: &'static ServerState, @@ -196,10 +208,12 @@ async fn router( "/history/incoming" => { assert_method(&parts, Method::GET)?; let params = history_params(&parts)?; + let filter = sql_history_filter(&params); + let transactions = state .client .query( - "SELECT id, _date, amount, reserve_pub, debit_acc, credit_acc FROM tx_in", + &format!("SELECT id, _date, amount, reserve_pub, debit_acc, credit_acc FROM tx_in {}", filter), &[], ) .await @@ -232,11 +246,12 @@ async fn router( "/history/outgoing" => { assert_method(&parts, Method::GET)?; let params = history_params(&parts)?; + let filter = sql_history_filter(&params); let transactions = state .client .query( - "SELECT id, _date, amount, wtid, debit_acc, credit_acc, exchange_url FROM tx_out", + &format!("SELECT id, _date, amount, wtid, debit_acc, credit_acc, exchange_url FROM tx_out {}",filter), &[], ) .await