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] UTXO swap watcher further improvements #1552

Merged
merged 166 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from 160 commits
Commits
Show all changes
166 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
5ff97e9
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Oct 26, 2022
5d8af0b
maker watchers periodically check for maker payment spend
caglaryucekaya Oct 26, 2022
8382041
remove the watcher_search_for_swap_tx_spend method
caglaryucekaya Oct 26, 2022
26712bf
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Oct 27, 2022
48408dd
fix payment search interval
caglaryucekaya Oct 30, 2022
5d4cb65
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Oct 31, 2022
3375118
small fixes
caglaryucekaya Oct 31, 2022
42417ba
add more watcher taker fee validations
caglaryucekaya Nov 2, 2022
dc2a98a
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Nov 2, 2022
7cda25d
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Nov 3, 2022
c035859
validate taker fee lock_time with taker payment
caglaryucekaya Nov 4, 2022
1cb01ee
remove debugging leftover
caglaryucekaya Nov 8, 2022
f2731eb
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Nov 9, 2022
3e1b07d
improve swap watcher integration tests
caglaryucekaya Nov 9, 2022
006b28b
fix small timing issues in taker swap
caglaryucekaya Nov 10, 2022
9534f62
adjust swap watcher timings and add more integration tests
caglaryucekaya Nov 15, 2022
df02c78
remove redundant taker_payment_lock field from the watcher message
caglaryucekaya Nov 15, 2022
f060b7d
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Nov 15, 2022
0d846a1
use maker payment hash instead of tx bytes
caglaryucekaya Nov 16, 2022
70aa77c
add missing implementations for watcher_search_for_swap_tx_spend
caglaryucekaya Nov 17, 2022
571e05f
add test_watcher_validate_taker_fee
caglaryucekaya Nov 20, 2022
30c4009
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Nov 20, 2022
39d36fb
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Nov 21, 2022
107111a
add WatcherValidateTakerFeeInput struct
caglaryucekaya Nov 22, 2022
d4e59bc
use taker payment hash instead of hex in watcher message
caglaryucekaya Nov 23, 2022
0286376
remove redundant taker_pub field from the watcher message
caglaryucekaya Nov 23, 2022
81e3055
use derive_htlc_key_pair to sign watcher messages
caglaryucekaya Nov 23, 2022
118d8a4
apply minor fixes
caglaryucekaya Nov 23, 2022
0bd767a
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Nov 23, 2022
79a3865
fix a bug in test_watcher_validate_taker_fee
caglaryucekaya Nov 23, 2022
c599e42
remove the taker_amount field and its usages
caglaryucekaya Nov 26, 2022
c9d20b0
remove the wait_for_confirmations_by_hash method
caglaryucekaya Nov 26, 2022
1a74760
implement helper methods for WatcherContext
caglaryucekaya Nov 26, 2022
d607058
remove wait_for_confirmations from RefundTakerPayment state
caglaryucekaya Nov 26, 2022
48e5a4a
increase wait_for_taker_refund_deadline
caglaryucekaya Nov 26, 2022
6a5760f
pass the secret_hash to the wait_for_htlc_tx_spend method
caglaryucekaya Nov 26, 2022
beaf2fb
make watchers sleep before calling can_refund_htlc
caglaryucekaya Nov 26, 2022
d244722
fix a clippy warning
caglaryucekaya Nov 26, 2022
bc8e204
add more helper functions for WatcherContext
caglaryucekaya Nov 27, 2022
931af36
use debug macro instead of log
caglaryucekaya Nov 27, 2022
db35962
fix formatting
caglaryucekaya Nov 27, 2022
57d3394
retry watcher_validate_taker_fee in case of error
caglaryucekaya Nov 27, 2022
dce6a12
define TAKER_PAYMENT_SPEND_SEARCH_INTERVAL separately in different mo…
caglaryucekaya Nov 27, 2022
ae0ec77
implement get_tx_hex_by_hash for the missing coins
caglaryucekaya Nov 27, 2022
30d9239
validate taker payment locking script
caglaryucekaya Nov 28, 2022
9f4b1f7
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Nov 28, 2022
d3d8653
fix a bug in docker_tests_inner
caglaryucekaya Nov 28, 2022
46e7bc3
fix formatting
caglaryucekaya Nov 28, 2022
2c72d50
remove an unused test case
caglaryucekaya Nov 28, 2022
8d3b7e0
add DEFAULT_SWAP_VIN constant
caglaryucekaya Nov 29, 2022
caaa0aa
remove loop to search for taker payment
caglaryucekaya Nov 29, 2022
0fa7222
fix taker payment locking script validation
caglaryucekaya Nov 29, 2022
2d390b4
move the loop out from watcher_search_for_swap_tx_spend
caglaryucekaya Nov 30, 2022
f2fef9b
use helper functions for mycoin, mycoin1 and kmd configuration
caglaryucekaya Nov 30, 2022
9f49c2c
remove logs before errors
caglaryucekaya Nov 30, 2022
c3738bf
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Nov 30, 2022
436183f
fix a mistake in test_get_max_taker_vol_dust_threshold
caglaryucekaya Nov 30, 2022
c4266a6
use or_mm_err instead of ok_or_else
caglaryucekaya Nov 30, 2022
0c33079
add as_slice method for Script and Bytes structs
caglaryucekaya Dec 1, 2022
dbc6352
use configurations for watcher variables
caglaryucekaya Dec 2, 2022
f640b19
improve error handling
caglaryucekaya Dec 2, 2022
81c2683
refactor the WaitForTakerPaymentSpend state
caglaryucekaya Dec 2, 2022
f1a6818
reduce TAKER_PAYMENT_SPEND_SEARCH_INTERVAL to 1 for tests
caglaryucekaya Dec 2, 2022
255fce5
refactor get_tx_hex_by_hash_impl and get_raw_transaction_impl methods
caglaryucekaya Dec 2, 2022
df8cc20
change watcher log messages
caglaryucekaya Dec 2, 2022
cc8dc51
use try_s macro instead of map_err
caglaryucekaya Dec 3, 2022
d95e784
use constants for logs used in watcher tests
caglaryucekaya Dec 3, 2022
0fd875e
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Dec 5, 2022
5a7b27e
add new struct WatcherConf
caglaryucekaya Dec 5, 2022
5f70aaa
fix a success case name
caglaryucekaya Dec 5, 2022
dec9c70
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Dec 5, 2022
cfbcae3
implement Default for WatcherConf
caglaryucekaya Dec 8, 2022
42de00b
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into wa…
caglaryucekaya Dec 8, 2022
10163d1
fix the name of wait_for_taker_payment_conf methods
caglaryucekaya Dec 8, 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
49 changes: 37 additions & 12 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ use super::{coin_conf, AsyncMutex, BalanceError, BalanceFut, CheckIfMyPaymentSen
TransactionDetails, TransactionEnum, TransactionErr, TransactionFut, TxMarshalingErr,
UnexpectedDerivationMethod, ValidateAddressResult, ValidateFeeArgs, ValidateInstructionsErr,
ValidateOtherPubKeyErr, ValidatePaymentError, ValidatePaymentFut, ValidatePaymentInput, VerificationError,
VerificationResult, WatcherOps, WatcherValidatePaymentInput, WithdrawError, WithdrawFee, WithdrawFut,
WithdrawRequest, WithdrawResult};
VerificationResult, WatcherOps, WatcherSearchForSwapTxSpendInput, WatcherValidatePaymentInput,
WatcherValidateTakerFeeInput, WithdrawError, WithdrawFee, WithdrawFut, WithdrawRequest, WithdrawResult};
pub use rlp;

#[cfg(test)] mod eth_tests;
Expand Down Expand Up @@ -581,8 +581,17 @@ async fn get_raw_transaction_impl(coin: EthCoin, req: RawTransactionRequest) ->
None => &req.tx_hash,
};
let hash = H256::from_str(tx).map_to_mm(|e| RawTransactionError::InvalidHashError(e.to_string()))?;
let web3_tx = coin.web3.eth().transaction(TransactionId::Hash(hash)).compat().await?;
let web3_tx = web3_tx.or_mm_err(|| RawTransactionError::HashNotExist(req.tx_hash))?;
get_tx_hex_by_hash_impl(coin, hash).await
}

async fn get_tx_hex_by_hash_impl(coin: EthCoin, tx_hash: H256) -> RawTransactionResult {
let web3_tx = coin
.web3
.eth()
.transaction(TransactionId::Hash(tx_hash))
.compat()
.await?
.or_mm_err(|| RawTransactionError::HashNotExist(tx_hash.to_string()))?;
let raw = signed_tx_from_web3_tx(web3_tx).map_to_mm(RawTransactionError::InternalError)?;
Ok(RawTransactionRes {
tx_hex: BytesJson(rlp::encode(&raw)),
Expand Down Expand Up @@ -1052,7 +1061,7 @@ impl SwapOps for EthCoin {
.await
}

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

Expand Down Expand Up @@ -1132,11 +1141,11 @@ impl SwapOps for EthCoin {

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

fn create_taker_spends_maker_payment_preimage(
fn create_maker_payment_spend_preimage(
&self,
_maker_payment_tx: &[u8],
_time_lock: u32,
Expand All @@ -1147,7 +1156,7 @@ impl WatcherOps for EthCoin {
unimplemented!();
}

fn create_taker_refunds_payment_preimage(
fn create_taker_payment_refund_preimage(
&self,
_taker_payment_tx: &[u8],
_time_lock: u32,
Expand All @@ -1159,17 +1168,24 @@ impl WatcherOps for EthCoin {
unimplemented!();
}

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

fn watcher_validate_taker_fee(&self, _taker_fee_hash: Vec<u8>, _verified_pub: Vec<u8>) -> ValidatePaymentFut<()> {
fn watcher_validate_taker_fee(&self, _input: WatcherValidateTakerFeeInput) -> 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)]
Expand Down Expand Up @@ -1354,6 +1370,7 @@ impl MarketCoinOps for EthCoin {
wait_until: u64,
from_block: u64,
swap_contract_address: &Option<BytesJson>,
check_every: f64,
) -> TransactionFut {
let unverified: UnverifiedTransaction = try_tx_fus!(rlp::decode(tx_bytes));
let tx = try_tx_fus!(SignedEthTx::new(unverified));
Expand Down Expand Up @@ -1415,12 +1432,12 @@ impl MarketCoinOps for EthCoin {
Ok(Some(t)) => t,
Ok(None) => {
info!("Tx {} not found yet", tx_hash);
Timer::sleep(5.).await;
Timer::sleep(check_every).await;
continue;
},
Err(e) => {
error!("Get tx {} error: {}", tx_hash, e);
Timer::sleep(5.).await;
Timer::sleep(check_every).await;
continue;
},
};
Expand Down Expand Up @@ -3135,6 +3152,14 @@ impl MmCoin for EthCoin {
Box::new(get_raw_transaction_impl(self.clone(), req).boxed().compat())
}

fn get_tx_hex_by_hash(&self, tx_hash: Vec<u8>) -> RawTransactionFut {
Box::new(
get_tx_hex_by_hash_impl(self.clone(), H256::from(tx_hash.as_slice()))
.boxed()
.compat(),
)
}

fn withdraw(&self, req: WithdrawRequest) -> WithdrawFut {
Box::new(Box::pin(withdraw_impl(self.clone(), req)).compat())
}
Expand Down
11 changes: 10 additions & 1 deletion mm2src/coins/eth/eth_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const GAS_PRICE_APPROXIMATION_ON_ORDER_ISSUE: u64 = 52_500_000_000;
// `GAS_PRICE` increased by 7%
const GAS_PRICE_APPROXIMATION_ON_TRADE_PREIMAGE: u64 = 53_500_000_000;

const TAKER_PAYMENT_SPEND_SEARCH_INTERVAL: f64 = 1.;

fn check_sum(addr: &str, expected: &str) {
let actual = checksum_address(addr);
assert_eq!(expected, actual);
Expand Down Expand Up @@ -470,7 +472,14 @@ fn test_wait_for_payment_spend_timeout() {
];

assert!(coin
.wait_for_htlc_tx_spend(&tx_bytes, &[], wait_until, from_block, &coin.swap_contract_address())
.wait_for_htlc_tx_spend(
&tx_bytes,
&[],
wait_until,
from_block,
&coin.swap_contract_address(),
TAKER_PAYMENT_SPEND_SEARCH_INTERVAL
)
.wait()
.is_err());
}
Expand Down
33 changes: 25 additions & 8 deletions mm2src/coins/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use crate::{BalanceFut, CheckIfMyPaymentSentArgs, CoinBalance, CoinFutSpawner, F
TradePreimageResult, TradePreimageValue, Transaction, TransactionEnum, TransactionErr, TransactionFut,
TxMarshalingErr, UnexpectedDerivationMethod, UtxoStandardCoin, ValidateAddressResult, ValidateFeeArgs,
ValidateInstructionsErr, ValidateOtherPubKeyErr, ValidatePaymentError, ValidatePaymentFut,
ValidatePaymentInput, VerificationError, VerificationResult, WatcherOps, WatcherValidatePaymentInput,
WithdrawError, WithdrawFut, WithdrawRequest};
ValidatePaymentInput, VerificationError, VerificationResult, WatcherOps, WatcherSearchForSwapTxSpendInput,
WatcherValidatePaymentInput, WatcherValidateTakerFeeInput, WithdrawError, WithdrawFut, WithdrawRequest};
use async_trait::async_trait;
use bitcoin::bech32::ToBase32;
use bitcoin::hashes::Hash;
Expand Down Expand Up @@ -629,7 +629,7 @@ impl SwapOps for LightningCoin {
unimplemented!()
}

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

Expand Down Expand Up @@ -734,7 +734,7 @@ fn payment_hash_from_slice(data: &[u8]) -> Result<PaymentHash, PaymentHashFromSl

#[async_trait]
impl WatcherOps for LightningCoin {
fn create_taker_spends_maker_payment_preimage(
fn create_maker_payment_spend_preimage(
&self,
_maker_payment_tx: &[u8],
_time_lock: u32,
Expand All @@ -745,11 +745,11 @@ impl WatcherOps for LightningCoin {
unimplemented!();
}

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

fn create_taker_refunds_payment_preimage(
fn create_taker_payment_refund_preimage(
&self,
_taker_payment_tx: &[u8],
_time_lock: u32,
Expand All @@ -761,17 +761,24 @@ impl WatcherOps for LightningCoin {
unimplemented!();
}

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

fn watcher_validate_taker_fee(&self, _taker_fee_hash: Vec<u8>, _verified_pub: Vec<u8>) -> ValidatePaymentFut<()> {
fn watcher_validate_taker_fee(&self, _input: WatcherValidateTakerFeeInput) -> 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 {
Expand Down Expand Up @@ -908,6 +915,7 @@ impl MarketCoinOps for LightningCoin {
wait_until: u64,
_from_block: u64,
_swap_contract_address: &Option<BytesJson>,
_check_every: f64,
) -> TransactionFut {
let payment_hash = try_tx_fus!(payment_hash_from_slice(transaction));
let payment_hex = hex::encode(payment_hash.0);
Expand Down Expand Up @@ -1005,6 +1013,15 @@ impl MmCoin for LightningCoin {
Box::new(fut.boxed().compat())
}

fn get_tx_hex_by_hash(&self, _tx_hash: Vec<u8>) -> RawTransactionFut {
let fut = async move {
MmError::err(RawTransactionError::InternalError(
"get_tx_hex_by_hash method is not supported for lightning.".into(),
))
};
Box::new(fut.boxed().compat())
}

fn withdraw(&self, _req: WithdrawRequest) -> WithdrawFut {
let fut = async move {
MmError::err(WithdrawError::InternalError(
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/lightning/ln_platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::sync::atomic::{AtomicU64, Ordering as AtomicOrdering, Ordering};

const CHECK_FOR_NEW_BEST_BLOCK_INTERVAL: f64 = 60.;
const TRY_LOOP_INTERVAL: f64 = 60.;
const TAKER_PAYMENT_SPEND_SEARCH_INTERVAL: f64 = 10.;

#[inline]
pub fn h256_json_from_txid(txid: Txid) -> H256Json { H256Json::from(txid.as_hash().into_inner()).reversed() }
Expand Down Expand Up @@ -520,6 +521,7 @@ impl Platform {
(now_ms() / 1000) + 3600,
from_block.try_into()?,
&None,
TAKER_PAYMENT_SPEND_SEARCH_INTERVAL,
)
.compat()
.await
Expand Down
45 changes: 36 additions & 9 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,23 @@ pub enum ValidateOtherPubKeyErr {
InvalidPubKey(String),
}

#[derive(Clone, Debug)]
pub struct WatcherValidateTakerFeeInput {
pub taker_fee_hash: Vec<u8>,
pub sender_pubkey: Vec<u8>,
pub min_block_number: u64,
pub fee_addr: Vec<u8>,
pub lock_duration: u64,
}

#[derive(Clone, Debug)]
pub struct WatcherValidatePaymentInput {
pub payment_tx: Vec<u8>,
pub taker_payment_refund_preimage: Vec<u8>,
pub time_lock: u32,
pub taker_pub: Vec<u8>,
pub maker_pub: Vec<u8>,
pub secret_hash: Vec<u8>,
pub amount: BigDecimal,
pub try_spv_proof_until: u64,
pub confirmations: u64,
}
Expand All @@ -509,6 +518,16 @@ pub struct ValidatePaymentInput {
pub unique_swap_data: Vec<u8>,
}

#[derive(Clone, Debug)]
pub struct WatcherSearchForSwapTxSpendInput<'a> {
pub time_lock: u32,
pub taker_pub: &'a [u8],
pub maker_pub: &'a [u8],
pub secret_hash: &'a [u8],
pub tx: &'a [u8],
pub search_from_block: u64,
}

pub struct SearchForSwapTxSpendInput<'a> {
pub time_lock: u32,
pub other_pub: &'a [u8],
Expand Down Expand Up @@ -661,7 +680,7 @@ pub trait SwapOps {

async fn extract_secret(&self, secret_hash: &[u8], spend_tx: &[u8]) -> Result<Vec<u8>, String>;

fn check_tx_signed_by_pub(&self, tx: &[u8], expected_pub: &[u8]) -> Result<bool, String>;
fn check_tx_signed_by_pub(&self, tx: &[u8], expected_pub: &[u8]) -> Result<bool, MmError<ValidatePaymentError>>;

/// Whether the refund transaction can be sent now
/// For example: there are no additional conditions for ETH, but for some UTXO coins we should wait for
Expand Down Expand Up @@ -703,11 +722,11 @@ pub trait SwapOps {

#[async_trait]
pub trait WatcherOps {
fn send_taker_spends_maker_payment_preimage(&self, preimage: &[u8], secret: &[u8]) -> TransactionFut;
fn send_maker_payment_spend_preimage(&self, preimage: &[u8], secret: &[u8]) -> TransactionFut;

fn send_watcher_refunds_taker_payment_preimage(&self, _taker_refunds_payment: &[u8]) -> TransactionFut;
fn send_taker_payment_refund_preimage(&self, preimage: &[u8]) -> TransactionFut;

fn create_taker_refunds_payment_preimage(
fn create_taker_payment_refund_preimage(
&self,
_taker_payment_tx: &[u8],
_time_lock: u32,
Expand All @@ -717,7 +736,7 @@ pub trait WatcherOps {
_swap_unique_data: &[u8],
) -> TransactionFut;

fn create_taker_spends_maker_payment_preimage(
fn create_maker_payment_spend_preimage(
&self,
_maker_payment_tx: &[u8],
_time_lock: u32,
Expand All @@ -726,9 +745,14 @@ pub trait WatcherOps {
_swap_unique_data: &[u8],
) -> TransactionFut;

fn watcher_validate_taker_fee(&self, _taker_fee_hash: Vec<u8>, _verified_pub: Vec<u8>) -> ValidatePaymentFut<()>;
fn watcher_validate_taker_fee(&self, input: WatcherValidateTakerFeeInput) -> ValidatePaymentFut<()>;

fn watcher_validate_taker_payment(&self, _input: WatcherValidatePaymentInput) -> ValidatePaymentFut<()>;

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

/// Operations that coins have independently from the MarketMaker.
Expand Down Expand Up @@ -789,6 +813,7 @@ pub trait MarketCoinOps {
wait_until: u64,
from_block: u64,
swap_contract_address: &Option<BytesJson>,
check_every: f64,
) -> TransactionFut;

fn tx_enum_from_bytes(&self, bytes: &[u8]) -> Result<TransactionEnum, MmError<TxMarshalingErr>>;
Expand Down Expand Up @@ -1171,12 +1196,12 @@ pub enum FeeApproxStage {
WithoutApprox,
/// Increase the trade fee slightly.
StartSwap,
/// Increase the trade fee slightly
WatcherPreimage,
/// Increase the trade fee significantly.
OrderIssue,
/// Increase the trade fee largely.
TradePreimage,
/// Increase the trade fee very largely
WatcherPreimage,
}

#[derive(Debug)]
Expand Down Expand Up @@ -1845,6 +1870,8 @@ pub trait MmCoin: SwapOps + WatcherOps + MarketCoinOps + Send + Sync + 'static {

fn get_raw_transaction(&self, req: RawTransactionRequest) -> RawTransactionFut;

fn get_tx_hex_by_hash(&self, tx_hash: Vec<u8>) -> RawTransactionFut;

/// Maximum number of digits after decimal point used to denominate integer coin units (satoshis, wei, etc.)
fn decimals(&self) -> u8;

Expand Down
Loading