Loop on blockchain call instead of delaying

Previously there was a delay making a get raw transaction call to
give some time for a transaction to be confirmed on the blockchain.
This has been replaced with a loop that waits until the call is
succesful.
pull/2/head
rishflab 4 years ago
parent f6f4ec2bdb
commit 4e031ff9a2

@ -4,7 +4,7 @@ use rand::{CryptoRng, RngCore};
use crate::{ use crate::{
bitcoin, bitcoin,
bitcoin::{BroadcastSignedTransaction, GetRawTransaction}, bitcoin::{BroadcastSignedTransaction, WatchForRawTransaction},
bob, monero, bob, monero,
monero::{ImportOutput, Transfer}, monero::{ImportOutput, Transfer},
transport::SendReceive, transport::SendReceive,
@ -18,7 +18,7 @@ pub use message::{Message, Message0, Message1, Message2, UnexpectedMessage};
pub async fn next_state< pub async fn next_state<
R: RngCore + CryptoRng, R: RngCore + CryptoRng,
B: GetRawTransaction + BroadcastSignedTransaction, B: WatchForRawTransaction + BroadcastSignedTransaction,
M: ImportOutput + Transfer, M: ImportOutput + Transfer,
T: SendReceive<Message, bob::Message>, T: SendReceive<Message, bob::Message>,
>( >(
@ -48,7 +48,6 @@ pub async fn next_state<
State::State2(state2) => { State::State2(state2) => {
let bob_message2: bob::Message2 = transport.receive_message().await?.try_into()?; let bob_message2: bob::Message2 = transport.receive_message().await?.try_into()?;
let state3 = state2.receive(bob_message2)?; let state3 = state2.receive(bob_message2)?;
tokio::time::delay_for(std::time::Duration::new(5, 0)).await;
Ok(state3.into()) Ok(state3.into())
} }
State::State3(state3) => { State::State3(state3) => {
@ -394,11 +393,11 @@ pub struct State3 {
impl State3 { impl State3 {
pub async fn watch_for_lock_btc<W>(self, bitcoin_wallet: &W) -> Result<State4> pub async fn watch_for_lock_btc<W>(self, bitcoin_wallet: &W) -> Result<State4>
where where
W: bitcoin::GetRawTransaction, W: bitcoin::WatchForRawTransaction,
{ {
tracing::info!("{}", self.tx_lock.txid()); tracing::info!("{}", self.tx_lock.txid());
let tx = bitcoin_wallet let tx = bitcoin_wallet
.get_raw_transaction(self.tx_lock.txid()) .watch_for_raw_transaction(self.tx_lock.txid())
.await?; .await?;
tracing::info!("{}", tx.txid()); tracing::info!("{}", tx.txid());
@ -581,7 +580,7 @@ impl State5 {
// watch for refund on btc, recover s_b and refund xmr // watch for refund on btc, recover s_b and refund xmr
pub async fn refund_xmr<B, M>(self, bitcoin_wallet: &B, monero_wallet: &M) -> Result<()> pub async fn refund_xmr<B, M>(self, bitcoin_wallet: &B, monero_wallet: &M) -> Result<()>
where where
B: GetRawTransaction, B: WatchForRawTransaction,
M: ImportOutput, M: ImportOutput,
{ {
let tx_cancel = bitcoin::TxCancel::new( let tx_cancel = bitcoin::TxCancel::new(
@ -595,7 +594,9 @@ impl State5 {
let tx_refund_encsig = self.a.encsign(self.S_b_bitcoin.clone(), tx_refund.digest()); let tx_refund_encsig = self.a.encsign(self.S_b_bitcoin.clone(), tx_refund.digest());
let tx_refund_candidate = bitcoin_wallet.get_raw_transaction(tx_refund.txid()).await?; let tx_refund_candidate = bitcoin_wallet
.watch_for_raw_transaction(tx_refund.txid())
.await?;
let tx_refund_sig = let tx_refund_sig =
tx_refund.extract_signature_by_key(tx_refund_candidate, self.a.public())?; tx_refund.extract_signature_by_key(tx_refund_candidate, self.a.public())?;

@ -188,8 +188,8 @@ pub trait BroadcastSignedTransaction {
} }
#[async_trait] #[async_trait]
pub trait GetRawTransaction { pub trait WatchForRawTransaction {
async fn get_raw_transaction(&self, txid: Txid) -> Result<Transaction>; async fn watch_for_raw_transaction(&self, txid: Txid) -> Result<Transaction>;
} }
pub fn recover(S: PublicKey, sig: Signature, encsig: EncryptedSignature) -> Result<SecretKey> { pub fn recover(S: PublicKey, sig: Signature, encsig: EncryptedSignature) -> Result<SecretKey> {

@ -1,7 +1,8 @@
use crate::{ use crate::{
alice, alice,
bitcoin::{ bitcoin::{
self, BroadcastSignedTransaction, BuildTxLockPsbt, GetRawTransaction, SignTxLock, TxCancel, self, BroadcastSignedTransaction, BuildTxLockPsbt, SignTxLock, TxCancel,
WatchForRawTransaction,
}, },
monero, monero,
monero::{CheckTransfer, ImportOutput}, monero::{CheckTransfer, ImportOutput},
@ -22,7 +23,7 @@ pub use message::{Message, Message0, Message1, Message2, Message3, UnexpectedMes
pub async fn next_state< pub async fn next_state<
R: RngCore + CryptoRng, R: RngCore + CryptoRng,
B: GetRawTransaction + SignTxLock + BuildTxLockPsbt + BroadcastSignedTransaction, B: WatchForRawTransaction + SignTxLock + BuildTxLockPsbt + BroadcastSignedTransaction,
M: ImportOutput + CheckTransfer, M: ImportOutput + CheckTransfer,
T: SendReceive<Message, alice::Message>, T: SendReceive<Message, alice::Message>,
>( >(
@ -500,12 +501,14 @@ impl State4 {
pub async fn watch_for_redeem_btc<W>(self, bitcoin_wallet: &W) -> Result<State5> pub async fn watch_for_redeem_btc<W>(self, bitcoin_wallet: &W) -> Result<State5>
where where
W: GetRawTransaction, W: WatchForRawTransaction,
{ {
let tx_redeem = bitcoin::TxRedeem::new(&self.tx_lock, &self.redeem_address); let tx_redeem = bitcoin::TxRedeem::new(&self.tx_lock, &self.redeem_address);
let tx_redeem_encsig = self.b.encsign(self.S_a_bitcoin.clone(), tx_redeem.digest()); let tx_redeem_encsig = self.b.encsign(self.S_a_bitcoin.clone(), tx_redeem.digest());
let tx_redeem_candidate = bitcoin_wallet.get_raw_transaction(tx_redeem.txid()).await?; let tx_redeem_candidate = bitcoin_wallet
.watch_for_raw_transaction(tx_redeem.txid())
.await?;
let tx_redeem_sig = let tx_redeem_sig =
tx_redeem.extract_signature_by_key(tx_redeem_candidate, self.b.public())?; tx_redeem.extract_signature_by_key(tx_redeem_candidate, self.b.public())?;

@ -6,7 +6,7 @@ use reqwest::Url;
use std::time::Duration; use std::time::Duration;
use tokio::time; use tokio::time;
use xmr_btc::bitcoin::{ use xmr_btc::bitcoin::{
BroadcastSignedTransaction, BuildTxLockPsbt, GetRawTransaction, SignTxLock, TxLock, BroadcastSignedTransaction, BuildTxLockPsbt, SignTxLock, TxLock, WatchForRawTransaction,
}; };
#[derive(Debug)] #[derive(Debug)]
@ -107,13 +107,13 @@ impl BroadcastSignedTransaction for Wallet {
} }
#[async_trait] #[async_trait]
impl GetRawTransaction for Wallet { impl WatchForRawTransaction for Wallet {
async fn get_raw_transaction(&self, txid: Txid) -> Result<Transaction> { async fn watch_for_raw_transaction(&self, txid: Txid) -> Result<Transaction> {
// TODO: put into loop instead of delaying loop {
time::delay_for(Duration::from_millis(5000)).await; if let Ok(tx) = self.0.get_raw_transaction(txid).await {
let tx = self.0.get_raw_transaction(txid).await?; return Ok(tx);
tracing::info!("{}", tx.txid()); }
time::delay_for(Duration::from_millis(200)).await;
Ok(tx) }
} }
} }

Loading…
Cancel
Save