depolymerization

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

commit af731945c8480a28102c66cf84cf2fb2b8a4f444
parent 80005a6b383eeaf4593a25173727ba5c03fa2e74
Author: Antoine A <>
Date:   Tue, 28 Dec 2021 19:56:31 +0100

btc-wire: improve code and test

Diffstat:
Mbtc-wire/src/main.rs | 147++++++++++++++++++++++++++++++++++++-------------------------------------------
Mscript/test_btc_stress.sh | 2+-
2 files changed, 68 insertions(+), 81 deletions(-)

diff --git a/btc-wire/src/main.rs b/btc-wire/src/main.rs @@ -242,97 +242,84 @@ fn sync_chain( Ok((full, bytes)) => { let mut tx = db.transaction()?; match decode_info(&bytes) { - Ok(info) => match info { - Info::Transaction { wtid, url } => { - let row = tx.query_opt( - "SELECT id, status FROM tx_out WHERE wtid=$1 FOR UPDATE", - &[&wtid.as_ref()], - )?; - if let Some(row) = row { - let _id: i32 = row.get(0); - let status: i16 = row.get(1); - let status: TxStatus = - TxStatus::try_from(status as u8).unwrap(); - // TODO match - if status != TxStatus::Sent { - tx.execute( - "UPDATE tx_out SET status=$1 where id=$2", - &[&(TxStatus::Sent as i16), &_id], + Ok(info) => { + match info { + Info::Transaction { wtid, url } => { + let addr = full.details[0].address.as_ref().unwrap(); + let amount = btc_amount_to_taler_amount(&full.amount); + + let row = tx.query_opt( + "SELECT id, status FROM tx_out WHERE wtid=$1 FOR UPDATE", + &[&wtid.as_ref()], + )?; + if let Some(row) = row { + let _id: i32 = row.get(0); + let status: i16 = row.get(1); + match TxStatus::try_from(status as u8).unwrap() { + TxStatus::Requested | TxStatus::Delayed => { + tx.execute( + "UPDATE tx_out SET status=$1 where id=$2", + &[&(TxStatus::Sent as i16), &_id], + )?; + warn!( + "send (recoverd) {} {} in {}", + addr, amount, &id + ); + } + TxStatus::Sent => {} + } + } else { + let debit_addr = sender_address(rpc, &full)?; + let date = SystemTime::UNIX_EPOCH + + Duration::from_secs(full.time); + // Generate a random request_uid + let mut request_uid = [0; 64]; + OsRng.fill_bytes(&mut request_uid); + let nb = tx.execute( + "INSERT INTO tx_out (_date, amount, wtid, debit_acc, credit_acc, exchange_url, status, txid, request_uid) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) ON CONFLICT (wtid) DO NOTHING", + &[&date, &amount.to_string(), &wtid.as_ref(), &btc_payto_url(&debit_addr).as_ref(), &btc_payto_url(addr).as_ref(), &config.base_url.as_ref(), &(TxStatus::Sent as i16), &id.as_ref(), &request_uid.as_ref()], )?; - if status == TxStatus::Delayed - || status == TxStatus::Requested - { + if nb > 0 { warn!( - "watcher: tx {} have been recovered automatically", - _id - ); - let addr = - full.details[0].address.as_ref().unwrap(); - let amount = - btc_amount_to_taler_amount(&full.amount); - info!("send {} {} in {}", addr, amount, &id); + "onchain {} {} in tx {}", + crockford_base32_encode(&wtid), + &url, + id + ); } } - } else { - let debit_addr = sender_address(rpc, &full)?; - let credit_addr = full.details[0].address.as_ref().unwrap(); - let date = - SystemTime::UNIX_EPOCH + Duration::from_secs(full.time); - let amount = btc_amount_to_taler_amount(&full.amount); - // Generate a random request_uid - let mut request_uid = [0; 64]; - OsRng.fill_bytes(&mut request_uid); - let nb = tx.execute( - "INSERT INTO tx_out (_date, amount, wtid, debit_acc, credit_acc, exchange_url, status, txid, request_uid) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) ON CONFLICT (wtid) DO NOTHING", - &[&date, &amount.to_string(), &wtid.as_ref(), &btc_payto_url(&debit_addr).as_ref(), &btc_payto_url(credit_addr).as_ref(), &config.base_url.as_ref(), &(TxStatus::Sent as i16), &id.as_ref(), &request_uid.as_ref()], - )?; - if nb > 0 { - warn!( - "recovered {} {} in tx {}", - crockford_base32_encode(&wtid), - &url, - id - ); - } } - } - Info::Bounce { bounced } => { - let row = tx.query_opt( + Info::Bounce { bounced } => { + let row = tx.query_opt( "SELECT id, status FROM bounce WHERE bounced=$1 FOR UPDATE", &[&bounced.as_ref()], )?; - if let Some(row) = row { - let _id: i32 = row.get(0); - let status: i16 = row.get(1); - let status: BounceStatus = - BounceStatus::try_from(status as u8).unwrap(); - assert!(status != BounceStatus::Ignored); // TODO - if status != BounceStatus::Sent { - tx.execute( - "UPDATE bounce SET status=$1 where id=$2", - &[&(BounceStatus::Sent as i16), &_id], + if let Some(row) = row { + let _id: i32 = row.get(0); + let status: i16 = row.get(1); + match BounceStatus::try_from(status as u8).unwrap() { + BounceStatus::Requested | BounceStatus::Delayed => { + tx.execute( + "UPDATE bounce SET status=$1 where id=$2", + &[&(BounceStatus::Sent as i16), &_id], + )?; + warn!("recovered bounce {} in {}", &bounced, &id); + } + BounceStatus::Ignored => error!("watcher: ignored bounce {} found in chain at {}", bounced, id), + BounceStatus::Sent => {} + } + } else { + let nb = tx.execute( + "INSERT INTO bounce (bounced, txid, status) VALUES ($1, $2, $3) ON CONFLICT (txid) DO NOTHING", + &[&bounced.as_ref(), &id.as_ref(), &(BounceStatus::Sent as i16)], )?; - if status == BounceStatus::Delayed - || status == BounceStatus::Requested - { - warn!( - "watcher: bounce {} have been recovered automatically", - _id - ); - info!("bounce {} in {}", &bounced, &id); + if nb > 0 { + warn!("onchain bounce {} in {}", &bounced, &id); } } - } else { - let nb = tx.execute( - "INSERT INTO bounce (bounced, txid, status) VALUES ($1, $2, $3) ON CONFLICT (txid) DO NOTHING", - &[&bounced.as_ref(), &id.as_ref(), &(BounceStatus::Sent as i16)], - )?; - if nb > 0 { - info!("recovered bounce {} in {}", &bounced, &id); - } } } - }, + } Err(err) => warn!("send: decode-info {} - {}", id, err), } tx.commit()?; @@ -351,8 +338,8 @@ fn sync_chain( let date = SystemTime::UNIX_EPOCH + Duration::from_secs(full.time); let amount = btc_amount_to_taler_amount(&full.amount); let nb = db.execute("INSERT INTO tx_in (_date, amount, reserve_pub, debit_acc, credit_acc) VALUES ($1, $2, $3, $4, $5) ON CONFLICT (reserve_pub) DO NOTHING ", &[ - &date, &amount.to_string(), &reserve_pub.as_ref(), &btc_payto_url(&debit_addr).as_ref(), &btc_payto_url(credit_addr).as_ref() - ])?; + &date, &amount.to_string(), &reserve_pub.as_ref(), &btc_payto_url(&debit_addr).as_ref(), &btc_payto_url(credit_addr).as_ref() + ])?; if nb > 0 { info!( "receive {} << {} {} in {}", diff --git a/script/test_btc_stress.sh b/script/test_btc_stress.sh @@ -28,7 +28,7 @@ init_btc echo "Init bitcoin regtest" setup_btc echo "Start btc-wire stressed" -btc_wire +stressed_btc_wire echo "Start gateway" gateway echo ""