@ -6,18 +6,13 @@ use crate::env::Config;
use crate ::monero_ext ::ScalarExt ;
use crate ::protocol ::alice ;
use crate ::protocol ::alice ::event_loop ::EventLoopHandle ;
use crate ::protocol ::alice ::steps ::{
extract_monero_private_key , lock_xmr , publish_cancel_transaction ,
wait_for_bitcoin_encrypted_signature , wait_for_bitcoin_refund ,
} ;
use crate ::protocol ::alice ::AliceState ;
use crate ::{ bitcoin , database , monero } ;
use anyhow ::{ bail , Context , Result } ;
use async_recursion ::async_recursion ;
use futures ::future ::{ select , Either } ;
use futures ::pin_mut ;
use rand ::{ CryptoRng , RngCore } ;
use std ::sync ::Arc ;
use tokio ::select ;
use tokio ::time ::timeout ;
use tracing ::{ error , info } ;
use uuid ::Uuid ;
@ -73,382 +68,275 @@ async fn run_until_internal(
) -> Result < AliceState > {
info ! ( "Current state: {}" , state ) ;
if is_target_state ( & state ) {
Ok ( state )
} else {
match state {
AliceState ::Started { state3 } = > {
timeout (
env_config . bob_time_to_act ,
bitcoin_wallet
. watch_until_status ( & state3 . tx_lock , | status | status . has_been_seen ( ) ) ,
)
. await
. context ( "Failed to find lock Bitcoin tx" ) ? ? ;
bitcoin_wallet
. watch_until_status ( & state3 . tx_lock , | status | {
status . is_confirmed_with ( env_config . bitcoin_finality_confirmations )
} )
. await ? ;
let state = AliceState ::BtcLocked { state3 } ;
let db_state = ( & state ) . into ( ) ;
db . insert_latest_state ( swap_id , database ::Swap ::Alice ( db_state ) )
. await ? ;
run_until_internal (
state ,
is_target_state ,
event_loop_handle ,
bitcoin_wallet ,
monero_wallet ,
env_config ,
swap_id ,
db ,
)
. await
}
AliceState ::BtcLocked { state3 } = > {
// Record the current monero wallet block height so we don't have to scan from
// block 0 for scenarios where we create a refund wallet.
let monero_wallet_restore_blockheight = monero_wallet . block_height ( ) . await ? ;
lock_xmr ( * state3 . clone ( ) , & mut event_loop_handle , & monero_wallet ) . await ? ;
let state = AliceState ::XmrLocked {
state3 ,
monero_wallet_restore_blockheight ,
} ;
let db_state = ( & state ) . into ( ) ;
db . insert_latest_state ( swap_id , database ::Swap ::Alice ( db_state ) )
. await ? ;
run_until_internal (
state ,
is_target_state ,
event_loop_handle ,
bitcoin_wallet ,
monero_wallet ,
env_config ,
swap_id ,
db ,
)
. await
}
return Ok ( state ) ;
}
let new_state = match state {
AliceState ::Started { state3 } = > {
timeout (
env_config . bob_time_to_act ,
bitcoin_wallet . watch_until_status ( & state3 . tx_lock , | status | status . has_been_seen ( ) ) ,
)
. await
. context ( "Failed to find lock Bitcoin tx" ) ? ? ;
bitcoin_wallet
. watch_until_status ( & state3 . tx_lock , | status | {
status . is_confirmed_with ( env_config . bitcoin_finality_confirmations )
} )
. await ? ;
AliceState ::BtcLocked { state3 }
}
AliceState ::BtcLocked { state3 } = > {
// Record the current monero wallet block height so we don't have to scan from
// block 0 for scenarios where we create a refund wallet.
let monero_wallet_restore_blockheight = monero_wallet . block_height ( ) . await ? ;
let transfer_proof = monero_wallet
. transfer ( state3 . lock_xmr_transfer_request ( ) )
. await ? ;
// TODO(Franck): Wait for Monero to be confirmed once
// Waiting for XMR confirmations should not be done in here, but in a separate
// state! We have to record that Alice has already sent the transaction.
// Otherwise Alice might publish the lock tx twice!
event_loop_handle
. send_transfer_proof ( transfer_proof )
. await ? ;
AliceState ::XmrLocked {
state3 ,
monero_wallet_restore_blockheight ,
} = > {
let state = match state3 . expired_timelocks ( bitcoin_wallet . as_ref ( ) ) . await ? {
ExpiredTimelocks ::None = > {
let wait_for_enc_sig =
wait_for_bitcoin_encrypted_signature ( & mut event_loop_handle ) ;
let state3_clone = state3 . clone ( ) ;
let cancel_timelock_expires = state3_clone
. wait_for_cancel_timelock_to_expire ( bitcoin_wallet . as_ref ( ) ) ;
pin_mut ! ( wait_for_enc_sig ) ;
pin_mut ! ( cancel_timelock_expires ) ;
match select ( cancel_timelock_expires , wait_for_enc_sig ) . await {
Either ::Left ( _ ) = > AliceState ::CancelTimelockExpired {
state3 ,
monero_wallet_restore_blockheight ,
} ,
Either ::Right ( ( enc_sig , _ ) ) = > AliceState ::EncSigLearned {
state3 ,
encrypted_signature : Box ::new ( enc_sig ? ) ,
monero_wallet_restore_blockheight ,
} ,
}
}
AliceState ::XmrLocked {
state3 ,
monero_wallet_restore_blockheight ,
} = > match state3 . expired_timelocks ( bitcoin_wallet . as_ref ( ) ) . await ? {
ExpiredTimelocks ::None = > {
select ! {
_ = state3 . wait_for_cancel_timelock_to_expire ( bitcoin_wallet . as_ref ( ) ) = > {
AliceState ::CancelTimelockExpired {
state3 ,
monero_wallet_restore_blockheight ,
}
}
_ = > AliceState ::CancelTimelockExpired {
state3 ,
monero_wallet_restore_blockheight ,
} ,
} ;
let db_state = ( & state ) . into ( ) ;
db . insert_latest_state ( swap_id , database ::Swap ::Alice ( db_state ) )
. await ? ;
run_until_internal (
state ,
is_target_state ,
event_loop_handle ,
bitcoin_wallet . clone ( ) ,
monero_wallet ,
env_config ,
swap_id ,
db ,
)
. await
}
AliceState ::EncSigLearned {
state3 ,
encrypted_signature ,
monero_wallet_restore_blockheight ,
} = > {
let state = match state3 . expired_timelocks ( bitcoin_wallet . as_ref ( ) ) . await ? {
ExpiredTimelocks ::None = > {
match TxRedeem ::new ( & state3 . tx_lock , & state3 . redeem_address ) . complete (
* encrypted_signature ,
state3 . a . clone ( ) ,
state3 . s_a . to_secpfun_scalar ( ) ,
state3 . B ,
) {
Ok ( tx ) = > match bitcoin_wallet . broadcast ( tx , "redeem" ) . await {
Ok ( ( _ , finality ) ) = > match finality . await {
Ok ( _ ) = > AliceState ::BtcRedeemed ,
Err ( e ) = > {
bail ! ( "Waiting for Bitcoin transaction finality failed with {}! The redeem transaction was published, but it is not ensured that the transaction was included! You're screwed." , e )
}
} ,
Err ( e ) = > {
error ! ( "Publishing the redeem transaction failed with {}, attempting to wait for cancellation now. If you restart the application before the timelock is expired publishing the redeem transaction will be retried." , e ) ;
state3
. wait_for_cancel_timelock_to_expire ( bitcoin_wallet . as_ref ( ) )
. await ? ;
AliceState ::CancelTimelockExpired {
state3 ,
monero_wallet_restore_blockheight ,
}
}
} ,
Err ( e ) = > {
error ! ( "Constructing the redeem transaction failed with {}, attempting to wait for cancellation now." , e ) ;
state3
. wait_for_cancel_timelock_to_expire ( bitcoin_wallet . as_ref ( ) )
. await ? ;
AliceState ::CancelTimelockExpired {
state3 ,
monero_wallet_restore_blockheight ,
}
}
enc_sig = event_loop_handle . recv_encrypted_signature ( ) = > {
tracing ::info ! ( "Received encrypted signature" ) ;
AliceState ::EncSigLearned {
state3 ,
encrypted_signature : Box ::new ( enc_sig ? ) ,
monero_wallet_restore_blockheight ,
}
}
_ = > AliceState ::CancelTimelockExpired {
state3 ,
monero_wallet_restore_blockheight ,
} ,
} ;
let db_state = ( & state ) . into ( ) ;
db . insert_latest_state ( swap_id , database ::Swap ::Alice ( db_state ) )
. await ? ;
run_until_internal (
state ,
is_target_state ,
event_loop_handle ,
bitcoin_wallet ,
monero_wallet ,
env_config ,
swap_id ,
db ,
)
. await
}
}
AliceState::CancelTimelockExpired {
_ = > AliceState ::CancelTimelockExpired {
state3 ,
monero_wallet_restore_blockheight ,
} = > {
publish_cancel_transaction (
state3 . tx_lock . clone ( ) ,
} ,
} ,
AliceState ::EncSigLearned {
state3 ,
encrypted_signature ,
monero_wallet_restore_blockheight ,
} = > match state3 . expired_timelocks ( bitcoin_wallet . as_ref ( ) ) . await ? {
ExpiredTimelocks ::None = > {
match TxRedeem ::new ( & state3 . tx_lock , & state3 . redeem_address ) . complete (
* encrypted_signature ,
state3 . a . clone ( ) ,
state3 . s_a . to_secpfun_scalar ( ) ,
state3 . B ,
state3 . cancel_timelock ,
state3 . tx_cancel_sig_bob . clone ( ) ,
& bitcoin_wallet ,
)
. await ? ;
let state = AliceState ::BtcCancelled {
state3 ,
monero_wallet_restore_blockheight ,
} ;
let db_state = ( & state ) . into ( ) ;
db . insert_latest_state ( swap_id , database ::Swap ::Alice ( db_state ) )
. await ? ;
run_until_internal (
state ,
is_target_state ,
event_loop_handle ,
bitcoin_wallet ,
monero_wallet ,
env_config ,
swap_id ,
db ,
)
. await
}
AliceState ::BtcCancelled {
state3 ,
monero_wallet_restore_blockheight ,
} = > {
let published_refund_tx = wait_for_bitcoin_refund (
& state3 . tx_cancel ( ) ,
& state3 . tx_refund ( ) ,
state3 . punish_timelock ,
& bitcoin_wallet ,
)
. await ? ;
) {
Ok ( tx ) = > match bitcoin_wallet . broadcast ( tx , "redeem" ) . await {
Ok ( ( _ , finality ) ) = > match finality . await {
Ok ( _ ) = > AliceState ::BtcRedeemed ,
Err ( e ) = > {
bail ! ( "Waiting for Bitcoin transaction finality failed with {}! The redeem transaction was published, but it is not ensured that the transaction was included! You're screwed." , e )
}
} ,
Err ( e ) = > {
error ! ( "Publishing the redeem transaction failed with {}, attempting to wait for cancellation now. If you restart the application before the timelock is expired publishing the redeem transaction will be retried." , e ) ;
state3
. wait_for_cancel_timelock_to_expire ( bitcoin_wallet . as_ref ( ) )
. await ? ;
// TODO(Franck): Review error handling
match published_refund_tx {
None = > {
let state = AliceState ::BtcPunishable {
state3 ,
monero_wallet_restore_blockheight ,
} ;
let db_state = ( & state ) . into ( ) ;
db . insert_latest_state ( swap_id , database ::Swap ::Alice ( db_state ) )
AliceState ::CancelTimelockExpired {
state3 ,
monero_wallet_restore_blockheight ,
}
}
} ,
Err ( e ) = > {
error ! ( "Constructing the redeem transaction failed with {}, attempting to wait for cancellation now." , e ) ;
state3
. wait_for_cancel_timelock_to_expire ( bitcoin_wallet . as_ref ( ) )
. await ? ;
run_until_internal (
state ,
is_target_state ,
event_loop_handle ,
bitcoin_wallet . clone ( ) ,
monero_wallet ,
env_config ,
swap_id ,
db ,
)
. await
}
Some ( published_refund_tx ) = > {
let spend_key = extract_monero_private_key (
published_refund_tx ,
& state3 . tx_refund ( ) ,
state3 . s_a ,
state3 . a . clone ( ) ,
state3 . S_b_bitcoin ,
) ? ;
let state = AliceState ::BtcRefunded {
spend_key ,
AliceState ::CancelTimelockExpired {
state3 ,
monero_wallet_restore_blockheight ,
} ;
let db_state = ( & state ) . into ( ) ;
db . insert_latest_state ( swap_id , database ::Swap ::Alice ( db_state ) )
. await ? ;
run_until_internal (
state ,
is_target_state ,
event_loop_handle ,
bitcoin_wallet . clone ( ) ,
monero_wallet ,
env_config ,
swap_id ,
db ,
)
. await
}
}
}
}
AliceState ::BtcRefunded {
spend_key ,
_ = > AliceState ::CancelTimelockExpired {
state3 ,
monero_wallet_restore_blockheight ,
} = > {
let view_key = state3 . v ;
monero_wallet
. create_from ( spend_key , view_key , monero_wallet_restore_blockheight )
. await ? ;
let state = AliceState ::XmrRefunded ;
let db_state = ( & state ) . into ( ) ;
db . insert_latest_state ( swap_id , database ::Swap ::Alice ( db_state ) )
. await ? ;
Ok ( state )
} ,
} ,
AliceState ::CancelTimelockExpired {
state3 ,
monero_wallet_restore_blockheight ,
} = > {
let tx_cancel = state3 . tx_cancel ( ) ;
// If Bob hasn't yet broadcasted the tx cancel, we do it
if bitcoin_wallet
. get_raw_transaction ( tx_cancel . txid ( ) )
. await
. is_err ( )
{
let transaction = tx_cancel
. complete_as_alice ( state3 . a . clone ( ) , state3 . B , state3 . tx_cancel_sig_bob . clone ( ) )
. context ( "Failed to complete Bitcoin cancel transaction" ) ? ;
if let Err ( e ) = bitcoin_wallet . broadcast ( transaction , "cancel" ) . await {
tracing ::debug ! (
"Assuming transaction is already broadcasted because: {:#}" ,
e
)
}
// TODO(Franck): Wait until transaction is mined and
// returned mined block height
}
AliceState ::BtcPunishable {
AliceState ::BtcCancelled {
state3 ,
monero_wallet_restore_blockheight ,
} = > {
let signed_tx_punish = state3 . tx_punish ( ) . complete (
state3 . tx_punish_sig_bob . clone ( ) ,
state3 . a . clone ( ) ,
state3 . B ,
) ? ;
let punish_tx_finalised = async {
let ( txid , finality ) =
bitcoin_wallet . broadcast ( signed_tx_punish , "punish" ) . await ? ;
finality . await ? ;
Result ::< _ , anyhow ::Error > ::Ok ( txid )
} ;
let tx_refund = state3 . tx_refund ( ) ;
let refund_tx_seen =
bitcoin_wallet . watch_until_status ( & tx_refund , | status | status . has_been_seen ( ) ) ;
pin_mut ! ( punish_tx_finalised ) ;
pin_mut ! ( refund_tx_seen ) ;
match select ( refund_tx_seen , punish_tx_finalised ) . await {
Either ::Left ( ( Ok ( ( ) ) , _ ) ) = > {
let published_refund_tx =
bitcoin_wallet . get_raw_transaction ( tx_refund . txid ( ) ) . await ? ;
let spend_key = extract_monero_private_key (
published_refund_tx ,
& tx_refund ,
state3 . s_a ,
state3 . a . clone ( ) ,
state3 . S_b_bitcoin ,
) ? ;
let state = AliceState ::BtcRefunded {
spend_key ,
state3 ,
monero_wallet_restore_blockheight ,
} ;
let db_state = ( & state ) . into ( ) ;
db . insert_latest_state ( swap_id , database ::Swap ::Alice ( db_state ) )
. await ? ;
run_until_internal (
state ,
is_target_state ,
event_loop_handle ,
bitcoin_wallet . clone ( ) ,
monero_wallet ,
env_config ,
swap_id ,
db ,
)
. await
}
}
AliceState ::BtcCancelled {
state3 ,
monero_wallet_restore_blockheight ,
} = > {
let tx_refund = state3 . tx_refund ( ) ;
let tx_cancel = state3 . tx_cancel ( ) ;
let seen_refund_tx =
bitcoin_wallet . watch_until_status ( & tx_refund , | status | status . has_been_seen ( ) ) ;
let punish_timelock_expired = bitcoin_wallet . watch_until_status ( & tx_cancel , | status | {
status . is_confirmed_with ( state3 . punish_timelock )
} ) ;
select ! {
seen_refund = seen_refund_tx = > {
seen_refund . context ( "Failed to monitor refund transaction" ) ? ;
let published_refund_tx = bitcoin_wallet . get_raw_transaction ( tx_refund . txid ( ) ) . await ? ;
let spend_key = tx_refund . extract_monero_private_key (
published_refund_tx ,
state3 . s_a ,
state3 . a . clone ( ) ,
state3 . S_b_bitcoin ,
) ? ;
AliceState ::BtcRefunded {
spend_key ,
state3 ,
monero_wallet_restore_blockheight ,
}
Either ::Left ( ( Err ( e ) , _ ) ) = > {
bail ! ( e . context ( "Failed to monitor refund transaction" ) )
}
_ = punish_timelock_expired = > {
AliceState ::BtcPunishable {
state3 ,
monero_wallet_restore_blockheight ,
}
Either ::Right ( _ ) = > {
let state = AliceState ::BtcPunished ;
let db_state = ( & state ) . into ( ) ;
db . insert_latest_state ( swap_id , database ::Swap ::Alice ( db_state ) )
. await ? ;
run_until_internal (
state ,
is_target_state ,
event_loop_handle ,
bitcoin_wallet . clone ( ) ,
monero_wallet ,
env_config ,
swap_id ,
db ,
)
. await
}
}
}
AliceState ::BtcRefunded {
spend_key ,
state3 ,
monero_wallet_restore_blockheight ,
} = > {
let view_key = state3 . v ;
monero_wallet
. create_from ( spend_key , view_key , monero_wallet_restore_blockheight )
. await ? ;
AliceState ::XmrRefunded
}
AliceState ::BtcPunishable {
state3 ,
monero_wallet_restore_blockheight ,
} = > {
let signed_tx_punish = state3 . tx_punish ( ) . complete (
state3 . tx_punish_sig_bob . clone ( ) ,
state3 . a . clone ( ) ,
state3 . B ,
) ? ;
let punish_tx_finalised = async {
let ( txid , finality ) = bitcoin_wallet . broadcast ( signed_tx_punish , "punish" ) . await ? ;
finality . await ? ;
Result ::< _ , anyhow ::Error > ::Ok ( txid )
} ;
let tx_refund = state3 . tx_refund ( ) ;
let refund_tx_seen =
bitcoin_wallet . watch_until_status ( & tx_refund , | status | status . has_been_seen ( ) ) ;
select ! {
result = refund_tx_seen = > {
result . context ( "Failed to monitor refund transaction" ) ? ;
let published_refund_tx =
bitcoin_wallet . get_raw_transaction ( tx_refund . txid ( ) ) . await ? ;
let spend_key = tx_refund . extract_monero_private_key (
published_refund_tx ,
state3 . s_a ,
state3 . a . clone ( ) ,
state3 . S_b_bitcoin ,
) ? ;
AliceState ::BtcRefunded {
spend_key ,
state3 ,
monero_wallet_restore_blockheight ,
}
}
_ = punish_tx_finalised = > {
AliceState ::BtcPunished
}
}
AliceState ::XmrRefunded = > Ok ( AliceState ::XmrRefunded ) ,
AliceState ::BtcRedeemed = > Ok ( AliceState ::BtcRedeemed ) ,
AliceState ::BtcPunished = > Ok ( AliceState ::BtcPunished ) ,
AliceState ::SafelyAborted = > Ok ( AliceState ::SafelyAborted ) ,
}
}
AliceState ::XmrRefunded = > AliceState ::XmrRefunded ,
AliceState ::BtcRedeemed = > AliceState ::BtcRedeemed ,
AliceState ::BtcPunished = > AliceState ::BtcPunished ,
AliceState ::SafelyAborted = > AliceState ::SafelyAborted ,
} ;
let db_state = ( & new_state ) . into ( ) ;
db . insert_latest_state ( swap_id , database ::Swap ::Alice ( db_state ) )
. await ? ;
run_until_internal (
new_state ,
is_target_state ,
event_loop_handle ,
bitcoin_wallet ,
monero_wallet ,
env_config ,
swap_id ,
db ,
)
. await
}