fix(eth-tpu): remove state from funding validation#2334
Conversation
mm2src/coins/eth.rs
Outdated
| pub(crate) async fn call_contract_function( | ||
| &self, | ||
| contract_abi: &Contract, | ||
| function_name: &str, |
There was a problem hiding this comment.
Can we use EthPaymentType to keep it pre-validated instead of using plain string?
There was a problem hiding this comment.
p.s. as_str can be called inside to cast it into &str.
There was a problem hiding this comment.
Can we use
EthPaymentTypeto keep it pre-validated instead of using plain string?
It was supposed to be a common function for eth. EthPaymentType is a swap v2 specific type for payments.
I suggest to keep function_name as &str
if you dont like it we can ask for Function type in function params

and call contract_abi.function before using call_contract_function
There was a problem hiding this comment.
Check this f91dfb8 please.
I think having &Function in call_contract_function even better than requiring contract and function name
|
Could you state what's the purpose of the PR? i.e.:
Could you elaborate why?
And also why this stays. |
mariocynicys
left a comment
There was a problem hiding this comment.
Thanks! Looks good with the small context I have rn.
Gonna need another review after I understand what was this for.
| decode_index: TAKER_PAYMENT_STATE_INDEX, | ||
| }; | ||
|
|
||
| let validator = |token: &Token| -> Result<bool, String> { | ||
| match token { | ||
| Token::Uint(state) => Ok(*state == U256::from(TakerPaymentStateV2::TakerApproved as u8)), | ||
| _ => Err(format!("Payment status must be Uint, got {:?}", token)), | ||
| } | ||
| }; |
There was a problem hiding this comment.
the validation function seems to process only a single token at a specific index and not an array of tokens.
don't u think we can make things simpler by passing an expected value (or expected token) instead of a validation function?
There was a problem hiding this comment.
hmmm, agree, I overcomplicated such trivial thing, will update it.
There was a problem hiding this comment.
Wait, no, closure actually makes process of Token validation simple, as we can validate any Token enum varian with this. I tried to make call and validate functionality general for any Token variant we would like to check. Also we can create any err message in closures or use log if we need for some case.
So I added closure to have some space for validation flexibility.
There was a problem hiding this comment.
About vector. I decided to not overcomplicate things by adding vector of tokens validator. As Im not sure we will ever need it. If this time come, then we will think.
There was a problem hiding this comment.
as we can validate any Token enum varian with this.
we can make things simpler by passing an expected value (or expected token) instead
Also we can create any err message in closures
Well, if the errorring is strictly needed, im okay with the closure 👍
I updated PR description info providing answers in sub points, please let me know if you have questions. |
…r_payment_spend_impl, as we check TakerApproved in search_for_taker_funding_spend_impl; Provide comment related to Latest block.
…arch_for_taker_funding_spend_impl
8a67d55 to
1e87e34
Compare
|
Since the base branch was changed, I force-pushed an updated branch (cherry picked feature commits) with a clean git history to resolve conflicts |
About this, I have to disagree. Delayed transaction propagation or confirmation is an issue for all types of chains even for utxo but we have this implementation for utxo https://github.com/KomodoPlatform/komodo-defi-framework/blob/39515a9f3ea1089bb462e99c8cafb1049a920dbd/mm2src/coins/utxo/utxo_standard.rs#L724 |
I see, seems like, after removing payment call from validation steps, there is no need in retry call functions (loop in state machine will be trying to get proper payment state). I will revert them in separate commit, to be able to re create them later if we need it. |
shamardy
left a comment
There was a problem hiding this comment.
Thanks for the PR! Only one comment unrelated to the PR.
There was a problem hiding this comment.
Unrelated to this PR, but I noticed that the functions here are related to taker payments not taker swap, e.g. sign_and_broadcast_taker_payment_spend_impl is called by maker. Maybe we should call this file eth_taker_payments_v2 and rename eth_maker_swap_v2 to eth_maker_payment_v2 as well. You should do it in a new PR.
…yment-spend-payment-state-recall
* dev: (24 commits) fix(eth-tpu): remove state from funding validation (GLEECBTC#2334) improvement(rpc-server): rpc server dynamic port allocation (GLEECBTC#2342) fix(tests): fix or ignore unstable tests (GLEECBTC#2365) fix(fs): make `filter_files_by_extension` return only files (GLEECBTC#2364) fix(derive_key_from_path): check length of current_key_material (GLEECBTC#2356) chore(release): bump mm2 version to 2.4.0-beta (GLEECBTC#2346) fix(tests): add additional testnet sepolia nodes to test code (GLEECBTC#2358) fix(swaps): maintain legacy compatibility for negotiation messages (GLEECBTC#2353) refactor(SwapOps): impl defaults for protocol specific swapops fns (GLEECBTC#2354) feat(tpu-v2): provide swap protocol versioning (GLEECBTC#2324) feat(wallet): add change mnemonic password rpc (GLEECBTC#2317) fix(tpu-v2): fix tpu-v2 wait for payment spend and extract secret (GLEECBTC#2261) feat(tendermint): unstaking/undelegation (GLEECBTC#2330) fix(utxo-withdraw): get hw ctx only when `PrivKeyPolicy` is trezor (GLEECBTC#2333) feat(event-streaming): API-driven subscription management (GLEECBTC#2172) fix(hash-types): remove panic, enforce fixed-size arrays (GLEECBTC#2279) fix(ARRR): store unconfirmed change output (GLEECBTC#2276) feat(tendermint): staking/delegation (GLEECBTC#2322) chore(deps): `timed-map` migration (GLEECBTC#2247) fix(mem-leak): `running_swap` never shrinks (GLEECBTC#2301) ...
* dev: feat(tendermint): claim delegation rewards (#2351) fix(eth-tpu): remove state from funding validation (#2334) improvement(rpc-server): rpc server dynamic port allocation (#2342) fix(tests): fix or ignore unstable tests (#2365) fix(fs): make `filter_files_by_extension` return only files (#2364)
* dev: feat(rpc): add is_success field to legacy MySwapStatusResponse (#2371) fix(key-derivation): use stored Argon2 parameters instead of default values (#2360) fix(tests): stabilize `tendermint_coin::test_claim_staking_rewards` (#2373) improvement(RPCs): group staking rpcs under a namespace (#2372) feat(tendermint): claim delegation rewards (#2351) fix(eth-tpu): remove state from funding validation (#2334) improvement(rpc-server): rpc server dynamic port allocation (#2342) fix(tests): fix or ignore unstable tests (#2365) fix(fs): make `filter_files_by_extension` return only files (#2364) fix(derive_key_from_path): check length of current_key_material (#2356) chore(release): bump mm2 version to 2.4.0-beta (#2346) fix(tests): add additional testnet sepolia nodes to test code (#2358) fix(swaps): maintain legacy compatibility for negotiation messages (#2353) refactor(SwapOps): impl defaults for protocol specific swapops fns (#2354) feat(tpu-v2): provide swap protocol versioning (#2324) feat(wallet): add change mnemonic password rpc (#2317)
Note: This PRs updates eth TPU part.
This PR covers this issue #2328 in eth tpu functionality.
The payment status (
PaymentSent) check has been removed from TPU validation payment functionality for all ETH implementations, including taker, maker, and maker-nft 7be74aa.It is done to avoid unnecessary loops of checking
PaymentSentin KDF code, as we already explicitly wait for taker/maker payment transaction confirmations in TPU (seewait_for_confirmationsfunction). If tx confirmation failed, it would mean that payment doesnt havePaymentSentstate in smart contract.Additionally this check is handled in smart contract code (see
MakerPaymentState.PaymentSentin EtomicSwapMakerV2 and TakerPaymentState.PaymentSent in EtomicSwapTakerV2)Payment state check is now only in
search_for_taker_funding_spend_impl, specifically checkingTakerPaymentStateV2::TakerApproved.In TPU state machine we wait for taker payment tx confirmation (in smart contract storage it changes payment state from
UnintializedtoPaymentSent) before callingsign_and_broadcast_taker_payment_spend.However, Taker payment has more states than maker payment, with the additional
TakerApprovedstate. So we need to check somehow on KDF that taker approve tx was successful.enum TakerPaymentState { Uninitialized, PaymentSent, TakerApproved, MakerSpent, TakerRefunded }This
TakerApprovedstate check is handled insearch_for_taker_funding_spendfunction.As maker payment has less statuses, it is sufficient to wait for maker payment tx confirmation (in smart contract storage it changes payment state from
UnintializedtoPaymentSent). There is no need to do additional eth calls in retry loop for this.enum MakerPaymentState { Uninitialized, PaymentSent, TakerSpent, MakerRefunded }NOTE: The below is removed, see #2334 (comment)
call_validate_token_with_retry, which validates ineth_taker_swap_v2.rsthe payment state decoded fromTokenwith a retry mechanism (incudes retry delay and attempts) for ETH calls.Even though in generic state machine
search_for_taker_funding_spendis already called in a loop with a timeout (see picture below), having retry logic within the ETH implementation is necessary because it directly addresses an ETH-specific issue (ref. issue #2328).If the loop is ever removed from the state machine and ETH

search_for_taker_funding_spendlacks this retry mechanism, the taker-approved check could fail, leading to a swap failure.This can happen for
"eth_call"API if the Latest block flag is used and the smart contract storage didnt get updates yet, or if the Pending flag is used and the ETH call is sent to a node that doesn't have the pending transaction.