Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[r2r] Extend swap watcher node functionality for UTXO #1496

Merged
merged 92 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 91 commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
a8d110b
add initial swap watcher node functionality
caglaryucekaya Sep 1, 2022
735dc2b
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Sep 1, 2022
b922e4e
broadcast watcher message immediately after taker payment is sent
caglaryucekaya Sep 2, 2022
8127ee5
rename the WatcherData struct to TakerSwapWatcherData
caglaryucekaya Sep 3, 2022
638f1b7
use separate SwapWatcherMsg for watchers
caglaryucekaya Sep 3, 2022
a14d31d
make taker send the entire spending transaction to watcher nodes
caglaryucekaya Sep 4, 2022
8d2e6cb
broadcast watcher message periodically in wait_for_taker_payment_spend
caglaryucekaya Sep 5, 2022
39f5be2
check if transaction outputs is empty in p2sh_spending_tx
caglaryucekaya Sep 5, 2022
a534d8d
fix minor details
caglaryucekaya Sep 5, 2022
8b57858
move utxo coin generation methods to docker_tests_common
caglaryucekaya Sep 5, 2022
df84e4d
use Mm2TestConf for watcher node tests
caglaryucekaya Sep 5, 2022
1f0c6a7
prevent running multiple watcher threads for the same swap
caglaryucekaya Sep 7, 2022
efe4294
remove async from spawn_taker_swap_watcher
caglaryucekaya Sep 7, 2022
96e1aeb
check if htlc spend fee is greater than the transaction output
caglaryucekaya Sep 7, 2022
bf93a96
add empty line at the end of docker_tests_common
caglaryucekaya Sep 7, 2022
03044f1
discard prev_transaction mutability after modification
caglaryucekaya Sep 7, 2022
52ecd87
check if transaction outputs is empty in utxo_common functions
caglaryucekaya Sep 7, 2022
979d036
use the drop_mutability macro
caglaryucekaya Sep 7, 2022
d897dbf
move all watcher related code to the swap_watcher module
caglaryucekaya Sep 8, 2022
807386e
improve the taker_swap_watchers usage
caglaryucekaya Sep 8, 2022
63ffaa9
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Sep 8, 2022
693c005
remove unused types and structs
caglaryucekaya Sep 9, 2022
726afe3
check if the transaction script has instructions
caglaryucekaya Sep 9, 2022
e89f88d
check if the transaction has any inputs
caglaryucekaya Sep 9, 2022
c0f8c1e
fix minor stuff
caglaryucekaya Sep 9, 2022
011ba95
make watchers validate taker payment at most once
caglaryucekaya Sep 9, 2022
34dc5b7
release taker_swap_watchers lock after inserting the uuid to the set
caglaryucekaya Sep 9, 2022
4958834
fix the error type in instruction iterator
caglaryucekaya Sep 11, 2022
152d507
reorder taker_swap_watchers lock/release structure
caglaryucekaya Sep 11, 2022
2521f43
use separate files for large json test strings
caglaryucekaya Sep 11, 2022
376d97d
remove watcher arguments from test helper functions
caglaryucekaya Sep 12, 2022
20c35e8
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Sep 12, 2022
40e94b4
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Sep 12, 2022
9130ba8
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Sep 13, 2022
4e36127
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Sep 13, 2022
d2166de
propagate watcher messages whether the node is watcher or not
caglaryucekaya Sep 14, 2022
424b1fb
remove swap lock related code from the swap_watcher
caglaryucekaya Sep 14, 2022
7aaa88f
remove running_swaps related code from swap_watcher
caglaryucekaya Sep 14, 2022
e5dd063
check if a watcher with the same uuid exists before spawning a thread
caglaryucekaya Sep 14, 2022
65b437c
fix minor problems
caglaryucekaya Sep 14, 2022
f58bc9d
remove AtomicSwap implementation from swap_watcher
caglaryucekaya Sep 14, 2022
c403935
make taker trigger a WatcherMessageSent event only if it is actually …
caglaryucekaya Sep 14, 2022
b5077b5
remove an unnecessary line
caglaryucekaya Sep 15, 2022
f9ad101
log error if watcher message could not be sent
caglaryucekaya Sep 19, 2022
473a04c
improve mutex usage for the taker_swap_watchers
caglaryucekaya Sep 22, 2022
0c8b58f
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Sep 22, 2022
281fb4b
fix formatting
caglaryucekaya Sep 22, 2022
56aac96
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Sep 24, 2022
1708a2b
add state machines, watcher refund and ValidatePaymentError
caglaryucekaya Sep 27, 2022
a6d5707
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Sep 28, 2022
fbe57e9
use DuplicateCache for taker_swap_watchers
caglaryucekaya Sep 29, 2022
500f18a
remove println statements
caglaryucekaya Sep 29, 2022
f2045cf
make the watchers wait for half the locktime before spending the make…
caglaryucekaya Oct 3, 2022
1070295
make the watchers wait for the taker before refunding
caglaryucekaya Oct 3, 2022
22ce9e3
add refund transaction confirmation for watchers
caglaryucekaya Oct 4, 2022
7e2ea18
check at watchers if the taker payment is already spent or refunded
caglaryucekaya Oct 4, 2022
675f5ed
validate the public keys in the watcher message
caglaryucekaya Oct 6, 2022
8a278a4
validate taker fees at watchers
caglaryucekaya Oct 9, 2022
03619f7
fix state implementation return types
caglaryucekaya Oct 10, 2022
77bd130
fix a bug in test_watcher_node
caglaryucekaya Oct 10, 2022
5023c5a
change the name of test_watcher_node
caglaryucekaya Oct 10, 2022
95360e7
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Oct 10, 2022
20bd5c8
fix a bug in test_validate_maker_payment_malicious
caglaryucekaya Oct 10, 2022
0f7df0f
Fix ValidatePaymentError usage
caglaryucekaya Oct 11, 2022
de69922
add preimage suffix to watcher-related transaction methods
caglaryucekaya Oct 11, 2022
1ff3aaa
fix error handling
caglaryucekaya Oct 11, 2022
89e9114
separate WatcherOps from SwapOps
caglaryucekaya Oct 11, 2022
d94dfcd
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Oct 11, 2022
7c8cd6a
fix error match usage
caglaryucekaya Oct 12, 2022
cb0aee7
reduce bchd_grpc module visibility to pub(crate)
caglaryucekaya Oct 12, 2022
1330c76
fix a bug in qrc20_tests
caglaryucekaya Oct 12, 2022
9627988
inline WatcherOps functions
caglaryucekaya Oct 12, 2022
52c5600
increase dynamic fees for watcher preimages
caglaryucekaya Oct 13, 2022
c1bbaf7
fix ethereum watcher preimage fee approximation
caglaryucekaya Oct 13, 2022
76960a7
take back unnecessary changes
caglaryucekaya Oct 13, 2022
7be1c09
inline SwapOps functions
caglaryucekaya Oct 13, 2022
cf12b78
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Oct 17, 2022
d8aed58
use taker fee hash for logs instead of uuid
caglaryucekaya Oct 17, 2022
dc4abef
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Oct 19, 2022
0182f27
extend TAKER_SWAP_ENTRY_TIMEOUT to 6 hours
caglaryucekaya Oct 19, 2022
ba35547
remove swap_unique_data field from WatcherSearchForSwapTxSpendInput s…
caglaryucekaya Oct 19, 2022
586ec04
fix error handling
caglaryucekaya Oct 19, 2022
810eedc
remove unnecessary taker swap removal in Stopped state
caglaryucekaya Oct 19, 2022
f37cb3b
improve match usage
caglaryucekaya Oct 19, 2022
64d7b1e
use WAIT_FOR_TAKER_PAYMENT_INTERVAL const
caglaryucekaya Oct 19, 2022
527dd43
inline DuplicateCache remove method
caglaryucekaya Oct 19, 2022
2e67cc4
add functions for wait taker/maker payment confirmation durations
caglaryucekaya Oct 19, 2022
9a18733
improve error handling
caglaryucekaya Oct 20, 2022
563395b
rename check_all_inputs_signed_by_pub method
caglaryucekaya Oct 20, 2022
90f69d3
add small fixes
caglaryucekaya Oct 21, 2022
ca8e435
fix WatcherError naming
caglaryucekaya Oct 22, 2022
3519e4b
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Oct 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mm2src/coins/coin_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub enum ValidatePaymentError {
InternalError(String),
// Problem with deserializing the transaction, or one of the transaction parts is invalid.
TxDeserializationError(String),
InvalidInput(String),
InvalidParameter(String),
InvalidRpcResponse(String),
SPVError(SPVError),
UnexpectedPaymentState(String),
Expand Down
93 changes: 65 additions & 28 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ use super::{coin_conf, AsyncMutex, BalanceError, BalanceFut, CoinBalance, CoinFu
TradePreimageResult, TradePreimageValue, Transaction, TransactionDetails, TransactionEnum, TransactionErr,
TransactionFut, TxMarshalingErr, UnexpectedDerivationMethod, ValidateAddressResult,
ValidateOtherPubKeyErr, ValidatePaymentError, ValidatePaymentFut, ValidatePaymentInput, VerificationError,
VerificationResult, WatcherValidatePaymentInput, WithdrawError, WithdrawFee, WithdrawFut, WithdrawRequest,
WithdrawResult};
VerificationResult, WatcherOps, WatcherSearchForSwapTxSpendInput, WatcherValidatePaymentInput,
WithdrawError, WithdrawFee, WithdrawFut, WithdrawRequest, WithdrawResult};

pub use rlp;

Expand Down Expand Up @@ -105,6 +105,8 @@ const ETH_DECIMALS: u8 = 18;

/// Take into account that the dynamic fee may increase by 3% during the swap.
const GAS_PRICE_APPROXIMATION_PERCENT_ON_START_SWAP: u64 = 3;
/// Take into account that the dynamic fee may increase until the locktime is expired
const GAS_PRICE_APPROXIMATION_PERCENT_ON_WATCHER_PREIMAGE: u64 = 3;
/// Take into account that the dynamic fee may increase at each of the following stages:
/// - it may increase by 2% until a swap is started;
/// - it may increase by 3% during the swap.
Expand Down Expand Up @@ -796,21 +798,6 @@ impl SwapOps for EthCoin {
)
}

fn send_taker_spends_maker_payment_preimage(&self, _preimage: &[u8], _secret: &[u8]) -> TransactionFut {
unimplemented!();
}

fn create_taker_spends_maker_payment_preimage(
&self,
_maker_payment_tx: &[u8],
_time_lock: u32,
_maker_pub: &[u8],
_secret_hash: &[u8],
_swap_unique_data: &[u8],
) -> TransactionFut {
unimplemented!();
}

fn send_taker_spends_maker_payment(
&self,
maker_payment_tx: &[u8],
Expand Down Expand Up @@ -978,7 +965,7 @@ impl SwapOps for EthCoin {
let swap_contract_address = try_f!(input
.swap_contract_address
.try_to_address()
.map_to_mm(ValidatePaymentError::InvalidInput));
.map_to_mm(ValidatePaymentError::InvalidParameter));
self.validate_payment(
&input.payment_tx,
input.time_lock,
Expand All @@ -993,7 +980,7 @@ impl SwapOps for EthCoin {
let swap_contract_address = try_f!(input
.swap_contract_address
.try_to_address()
.map_to_mm(ValidatePaymentError::InvalidInput));
.map_to_mm(ValidatePaymentError::InvalidParameter));
self.validate_payment(
&input.payment_tx,
input.time_lock,
Expand All @@ -1004,13 +991,6 @@ impl SwapOps for EthCoin {
)
}

fn watcher_validate_taker_payment(
&self,
_input: WatcherValidatePaymentInput,
) -> Box<dyn Future<Item = (), Error = MmError<ValidatePaymentError>> + Send> {
unimplemented!();
}

fn check_if_my_payment_sent(
&self,
time_lock: u32,
Expand Down Expand Up @@ -1099,6 +1079,10 @@ impl SwapOps for EthCoin {
.await
}

fn check_tx_signed_by_pub(&self, _tx: &[u8], _expected_pub: &[u8]) -> Result<bool, String> {
unimplemented!();
}

fn extract_secret(&self, _secret_hash: &[u8], spend_tx: &[u8]) -> Result<Vec<u8>, String> {
let unverified: UnverifiedTransaction = try_s!(rlp::decode(spend_tx));
let function = try_s!(SWAP_CONTRACT.function("receiverSpend"));
Expand Down Expand Up @@ -1141,6 +1125,7 @@ impl SwapOps for EthCoin {
}
}

#[inline]
fn derive_htlc_key_pair(&self, _swap_unique_data: &[u8]) -> keys::KeyPair {
key_pair_from_secret(self.key_pair.secret()).expect("valid key")
}
Expand All @@ -1153,6 +1138,55 @@ impl SwapOps for EthCoin {
}
}

#[async_trait]
impl WatcherOps for EthCoin {
fn send_taker_spends_maker_payment_preimage(&self, _preimage: &[u8], _secret: &[u8]) -> TransactionFut {
unimplemented!();
}

fn create_taker_spends_maker_payment_preimage(
&self,
_maker_payment_tx: &[u8],
_time_lock: u32,
_maker_pub: &[u8],
_secret_hash: &[u8],
_swap_unique_data: &[u8],
) -> TransactionFut {
unimplemented!();
}

fn create_taker_refunds_payment_preimage(
&self,
_taker_payment_tx: &[u8],
_time_lock: u32,
_maker_pub: &[u8],
_secret_hash: &[u8],
_swap_contract_address: &Option<BytesJson>,
_swap_unique_data: &[u8],
) -> TransactionFut {
unimplemented!();
}

fn send_watcher_refunds_taker_payment_preimage(&self, _taker_refunds_payment: &[u8]) -> TransactionFut {
unimplemented!();
}

fn watcher_validate_taker_fee(&self, _taker_fee_hash: Vec<u8>, _verified_pub: Vec<u8>) -> ValidatePaymentFut<()> {
unimplemented!();
}

fn watcher_validate_taker_payment(&self, _input: WatcherValidatePaymentInput) -> ValidatePaymentFut<()> {
unimplemented!();
}

async fn watcher_search_for_swap_tx_spend(
&self,
_input: WatcherSearchForSwapTxSpendInput<'_>,
) -> Result<Option<FoundSwapTxSpend>, String> {
unimplemented!();
}
}

#[cfg_attr(test, mockable)]
impl MarketCoinOps for EthCoin {
fn ticker(&self) -> &str { &self.ticker[..] }
Expand Down Expand Up @@ -2717,7 +2751,7 @@ impl EthCoin {
let tx =
try_f!(SignedEthTx::new(unsigned)
.map_to_mm(|err| ValidatePaymentError::TxDeserializationError(err.to_string())));
let sender = try_f!(addr_from_raw_pubkey(sender_pub).map_to_mm(ValidatePaymentError::InvalidInput));
let sender = try_f!(addr_from_raw_pubkey(sender_pub).map_to_mm(ValidatePaymentError::InvalidParameter));
let expected_value = try_f!(wei_from_big_decimal(&amount, self.decimals));
let selfi = self.clone();
let secret_hash = secret_hash.to_vec();
Expand All @@ -2744,7 +2778,7 @@ impl EthCoin {
let tx_from_rpc = match tx_from_rpc {
Some(t) => t,
None => {
return MmError::err(ValidatePaymentError::UnexpectedPaymentState(format!(
return MmError::err(ValidatePaymentError::InvalidRpcResponse(format!(
"Didn't find provided tx {:?} on ETH node",
tx
)))
Expand Down Expand Up @@ -3785,5 +3819,8 @@ fn increase_gas_price_by_stage(gas_price: U256, level: &FeeApproxStage) -> U256
FeeApproxStage::TradePreimage => {
increase_by_percent_one_gwei(gas_price, GAS_PRICE_APPROXIMATION_PERCENT_ON_TRADE_PREIMAGE)
},
FeeApproxStage::WatcherPreimage => {
increase_by_percent_one_gwei(gas_price, GAS_PRICE_APPROXIMATION_PERCENT_ON_WATCHER_PREIMAGE)
},
}
}
81 changes: 56 additions & 25 deletions mm2src/coins/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod ln_storage;
mod ln_utils;

use super::{lp_coinfind_or_err, DerivationMethod, MmCoinEnum};
use crate::coin_errors::{MyAddressError, ValidatePaymentError};
use crate::coin_errors::MyAddressError;
use crate::lightning::ln_conf::OurChannelsConfigs;
use crate::lightning::ln_errors::{TrustedNodeError, TrustedNodeResult, UpdateChannelError, UpdateChannelResult};
use crate::lightning::ln_events::init_abortable_events;
Expand All @@ -26,8 +26,8 @@ use crate::{BalanceFut, CoinBalance, CoinFutSpawner, FeeApproxStage, FoundSwapTx
SearchForSwapTxSpendInput, SignatureError, SignatureResult, SwapOps, TradeFee, TradePreimageFut,
TradePreimageResult, TradePreimageValue, TransactionEnum, TransactionFut, TxMarshalingErr,
UnexpectedDerivationMethod, UtxoStandardCoin, ValidateAddressResult, ValidateOtherPubKeyErr,
ValidatePaymentFut, ValidatePaymentInput, VerificationError, VerificationResult,
WatcherValidatePaymentInput, WithdrawError, WithdrawFut, WithdrawRequest};
ValidatePaymentFut, ValidatePaymentInput, VerificationError, VerificationResult, WatcherOps,
WatcherSearchForSwapTxSpendInput, WatcherValidatePaymentInput, WithdrawError, WithdrawFut, WithdrawRequest};
use async_trait::async_trait;
use bitcoin::hashes::Hash;
use bitcoin_hashes::sha256::Hash as Sha256;
Expand Down Expand Up @@ -316,17 +316,6 @@ impl SwapOps for LightningCoin {
unimplemented!()
}

fn create_taker_spends_maker_payment_preimage(
&self,
_maker_payment_tx: &[u8],
_time_lock: u32,
_maker_pub: &[u8],
_secret_hash: &[u8],
_swap_unique_data: &[u8],
) -> TransactionFut {
unimplemented!();
}

fn send_taker_spends_maker_payment(
&self,
_maker_payment_tx: &[u8],
Expand All @@ -339,10 +328,6 @@ impl SwapOps for LightningCoin {
unimplemented!()
}

fn send_taker_spends_maker_payment_preimage(&self, _preimage: &[u8], _secret: &[u8]) -> TransactionFut {
unimplemented!();
}

fn send_taker_refunds_payment(
&self,
_taker_payment_tx: &[u8],
Expand Down Expand Up @@ -383,13 +368,6 @@ impl SwapOps for LightningCoin {

fn validate_taker_payment(&self, _input: ValidatePaymentInput) -> ValidatePaymentFut<()> { unimplemented!() }

fn watcher_validate_taker_payment(
&self,
_input: WatcherValidatePaymentInput,
) -> Box<dyn Future<Item = (), Error = MmError<ValidatePaymentError>> + Send> {
unimplemented!();
}

fn check_if_my_payment_sent(
&self,
_time_lock: u32,
Expand All @@ -416,6 +394,10 @@ impl SwapOps for LightningCoin {
unimplemented!()
}

fn check_tx_signed_by_pub(&self, _tx: &[u8], _expected_pub: &[u8]) -> Result<bool, String> {
unimplemented!();
}

fn extract_secret(&self, _secret_hash: &[u8], _spend_tx: &[u8]) -> Result<Vec<u8>, String> { unimplemented!() }

fn negotiate_swap_contract_addr(
Expand All @@ -430,6 +412,55 @@ impl SwapOps for LightningCoin {
fn validate_other_pubkey(&self, _raw_pubkey: &[u8]) -> MmResult<(), ValidateOtherPubKeyErr> { unimplemented!() }
}

#[async_trait]
impl WatcherOps for LightningCoin {
fn create_taker_spends_maker_payment_preimage(
&self,
_maker_payment_tx: &[u8],
_time_lock: u32,
_maker_pub: &[u8],
_secret_hash: &[u8],
_swap_unique_data: &[u8],
) -> TransactionFut {
unimplemented!();
}

fn send_taker_spends_maker_payment_preimage(&self, _preimage: &[u8], _secret: &[u8]) -> TransactionFut {
unimplemented!();
}

fn create_taker_refunds_payment_preimage(
&self,
_taker_payment_tx: &[u8],
_time_lock: u32,
_maker_pub: &[u8],
_secret_hash: &[u8],
_swap_contract_address: &Option<BytesJson>,
_swap_unique_data: &[u8],
) -> TransactionFut {
unimplemented!();
}

fn send_watcher_refunds_taker_payment_preimage(&self, _taker_refunds_payment: &[u8]) -> TransactionFut {
unimplemented!();
}

fn watcher_validate_taker_fee(&self, _taker_fee_hash: Vec<u8>, _verified_pub: Vec<u8>) -> ValidatePaymentFut<()> {
unimplemented!();
}

fn watcher_validate_taker_payment(&self, _input: WatcherValidatePaymentInput) -> ValidatePaymentFut<()> {
unimplemented!();
}

async fn watcher_search_for_swap_tx_spend(
&self,
_input: WatcherSearchForSwapTxSpendInput<'_>,
) -> Result<Option<FoundSwapTxSpend>, String> {
unimplemented!();
}
}

impl MarketCoinOps for LightningCoin {
fn ticker(&self) -> &str { &self.conf.ticker }

Expand Down
Loading