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] Mm2 error refactoring #1444

Merged
merged 28 commits into from
Sep 28, 2022
Merged

[r2r] Mm2 error refactoring #1444

merged 28 commits into from
Sep 28, 2022

Conversation

borngraced
Copy link
Member

@borngraced borngraced commented Aug 16, 2022

  • ValidatePaymentError
  • AddressParseError
  • MyAddressError
  • CheckPaymentSentError
  • SendRawTxError
  • WaitForConfirmationsErr
  • CoinInitResponseError
  • GossipPeerError

@borngraced borngraced changed the title Mm2 error refactoring [WIP] Mm2 error refactoring Aug 16, 2022
Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the changes!
First review iteration.
Please consider applying all comments to other coins, not ETH only.

mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The next review iteration

mm2src/coins/coin_errors.rs Outdated Show resolved Hide resolved
mm2src/coins/coin_errors.rs Outdated Show resolved Hide resolved
mm2src/coins/coin_errors.rs Outdated Show resolved Hide resolved
mm2src/coins/coin_errors.rs Outdated Show resolved Hide resolved
Comment on lines 89 to 91
impl From<ScriptHashTypeNotSupported> for ValidatePaymentError {
fn from(err: ScriptHashTypeNotSupported) -> Self { Self::ScriptHashTypeNotSupported(err.to_string()) }
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes we shouldn't implement From trait for specific errors. If only you are sure that such error can be converted the same way in different contexts.
Currently, ScriptHashTypeNotSupported appears in only one case when we want to convert our QTUM address into a QRC20 contract address:
https://github.com/KomodoPlatform/atomicDEX-API/blob/18754a2017c868138387080b0dbf5ba1a1710df4/mm2src/coins/qrc20/swap.rs#L132
But what if we will need later to convert other's address into a QRC20 contract address?
ValidatePaymentError:: ScriptHashTypeNotSupported is not obvious, because the coin.validate_maker_payment() method caller wants to know about what was the reason of this error and which address has a script hash type that is not supported.

In a nutshell, I'd suggest not to implement From< ScriptHashTypeNotSupported> for ValidatePaymentError and convert it manually in the code:

let expected_receiver = qtum::contract_addr_from_utxo_addr(my_address).mm_err(|e| ValidatePaymentError::Internal(e.to_string()))?;

It's an internal error because we must be able to convert our QTUM address, otherwise it's an unexpected behaviour.

Warning

Please use the From trait carefully because later it can turn into a bug even if it's currently a correct behaviour

Copy link
Member Author

@borngraced borngraced Aug 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe not. I can actually use ValidatePaymentError:: ScriptHashTypeNotSupported(String) and implement From<ScriptHashTypeNotSupported> in QTUM mod alone and It won't have any of the side effects you listed above. but since it's being used here alone

let expected_receiver = qtum::contract_addr_from_utxo_addr(my_address).mm_err(|e| ValidatePaymentError::Internal(e.to_string()))?;

then I can do it manually like you said.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementing traits in private submodules occurs at the crate level.
For example,

struct MyFoo;

struct MyBar;

mod foo {
    use crate::{MyBar, MyFoo};

    impl From<MyFoo> for MyBar {
        fn from(_: MyFoo) -> Self {
            println!("from MyFoo for MyBar");
            MyBar
        }
    }
}

fn main() {
    //
    let x = MyFoo;
    let y = MyBar::from(x);
}

This will work.

mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/lp_coins.rs Outdated Show resolved Hide resolved
mm2src/coins/qrc20.rs Outdated Show resolved Hide resolved
mm2src/coins/qrc20/swap.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start 🙂 ! First review iteration.

mm2src/coins/coin_errors.rs Outdated Show resolved Hide resolved
mm2src/coins/coin_errors.rs Outdated Show resolved Hide resolved
mm2src/coins/coin_errors.rs Outdated Show resolved Hide resolved
mm2src/coins/coin_errors.rs Outdated Show resolved Hide resolved
mm2src/coins/coin_errors.rs Outdated Show resolved Hide resolved
mm2src/coins/coin_errors.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/qrc20/swap.rs Show resolved Hide resolved
mm2src/coins/qrc20/swap.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_common.rs Outdated Show resolved Hide resolved
@borngraced
Copy link
Member Author

@shamardy @sergeyboyko0791 I have worked on the reviews and it's ready for another round of review..so I can continue with other errors. thanks)

@borngraced borngraced changed the title [WIP] Mm2 error refactoring [r2r] Mm2 error refactoring Sep 1, 2022
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
Comment on lines 89 to 91
impl From<ScriptHashTypeNotSupported> for ValidatePaymentError {
fn from(err: ScriptHashTypeNotSupported) -> Self { Self::ScriptHashTypeNotSupported(err.to_string()) }
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementing traits in private submodules occurs at the crate level.
For example,

struct MyFoo;

struct MyBar;

mod foo {
    use crate::{MyBar, MyFoo};

    impl From<MyFoo> for MyBar {
        fn from(_: MyFoo) -> Self {
            println!("from MyFoo for MyBar");
            MyBar
        }
    }
}

fn main() {
    //
    let x = MyFoo;
    let y = MyBar::from(x);
}

This will work.

Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the previous fixes!
Next review iteration.

let function = try_s!(SWAP_CONTRACT.function("ethPayment"));
let decoded = try_s!(function.decode_input(&tx_from_rpc.input.0));
let function = SWAP_CONTRACT.function("ethPayment")?;
let decoded = function.decode_input(&tx_from_rpc.input.0)?;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not fixed yet. Now ethabi::Error is converted into Web3RpcError. Please remove the From trait implementation and convert the error manually:

let decoded = function
                        .decode_input(&tx_from_rpc.input.0)
                        .map_to_mm(|e| ValidatePaymentError::InvalidPaymentTxData(e.to_string()))?;

mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/lp_coins.rs Outdated Show resolved Hide resolved
mm2src/coins/lp_coins.rs Outdated Show resolved Hide resolved
mm2src/coins/qrc20/swap.rs Show resolved Hide resolved
mm2src/coins/qrc20/swap.rs Outdated Show resolved Hide resolved
mm2src/coins/qrc20/swap.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/qtum.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/slp.rs Outdated Show resolved Hide resolved
@borngraced borngraced changed the title [r2r] Mm2 error refactoring [wip] Mm2 error refactoring Sep 1, 2022
@borngraced
Copy link
Member Author

@sergeyboyko0791 I have fixed the previous reviews and it's ready for another round:) thanks

@borngraced borngraced changed the title [wip] Mm2 error refactoring [r2r] Mm2 error refactoring Sep 2, 2022
Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next review iteration.

mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/qrc20/swap.rs Show resolved Hide resolved
@@ -79,6 +79,10 @@ pub struct ValidateSlpUtxosErr {
kind: ValidateSlpUtxosErrKind,
}

impl From<ValidateSlpUtxosErr> for ValidatePaymentError {
fn from(err: ValidateSlpUtxosErr) -> Self { Self::InvalidPaymentTxData(err.to_string()) }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check what's the actual cause of the error.
For example, ValidateSlpUtxosErrKind::UnexpectedUtxoInResponse should be converted into ValidateSlpUtxosErrKind::InvalidRpcResponse, ValidateSlpUtxosErrKind::UnexpectedValidityResultType and ValidateSlpUtxosErrKind::UnexpectedTokenId are more likely InvalidRpcResponse as well (but it's better to check the code). But ValidateSlpUtxosErrKind::MultiReqErr is probably Transport error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok done

mm2src/coins/utxo/slp.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/slp.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/slp.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/slp.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/slp.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_common.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_common.rs Outdated Show resolved Hide resolved
@borngraced borngraced changed the title [r2r] Mm2 error refactoring [wip] Mm2 error refactoring Sep 2, 2022
@borngraced borngraced changed the title [wip] Mm2 error refactoring [r2r] Mm2 error refactoring Sep 5, 2022
Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are at the finish line!
Next review iteration

mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/slp.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/slp.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/slp.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/slp.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/slp.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_common.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_common.rs Outdated Show resolved Hide resolved
@borngraced borngraced changed the title [r2r] Mm2 error refactoring [wip] Mm2 error refactoring Sep 6, 2022
@borngraced borngraced changed the title [wip] Mm2 error refactoring [r2r] Mm2 error refactoring Sep 7, 2022
Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one change request please

Comment on lines 2115 to 2104
ValidateHtlcError::InvalidSlpUtxo(e) => println!("{:?}", e),
ValidatePaymentError::InvalidInput(e) => println!("{:?}", e),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid that here we need to expect WrongPaymentTx, not InvalidInput. Please check if everything is correct.

Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Approve!
@shamardy please continue reviewing it

Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final review notes/questions from my side :)

mm2src/coins/qrc20/swap.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/bchd_grpc.rs Show resolved Hide resolved
mm2src/coins/utxo/slp.rs Outdated Show resolved Hide resolved
@artemii235
Copy link
Member

@sergeyboyko0791

Multiple tests expected ValidateHtlcError::InvalidSlpUtxo(ValidateSlpUtxosErr) error, but now we don't have ValidateHtlcError at all, and we need to convert ValidateSlpUtxosErr into ValidatePaymentError correctly, especially we need to determine if the UTXOs are invalid or not.

ValidateHtlcError was SLP specific, so I doubt that this should have been removed at all. The solution can be to bring it back and map ValidateHtlcError -> ValidatePaymentError.

@sergeyboyko0791
Copy link

sergeyboyko0791 commented Sep 14, 2022

@artemii235 There was a problem with the cyclic conversion: #1444 (comment)

ValidateHtlcError was SLP specific, so I doubt that this should have been removed at all. The solution can be to bring it back and map ValidateHtlcError -> ValidatePaymentError.

@artemii235
Copy link
Member

@sergeyboyko0791

There was a problem with the cyclic conversion: #1444 (comment)

I think I have understood the problem finally and responded here: #1444 (comment) 🙂 No need to return ValidateHtlcError back, but we can refactor lower level errors a bit for clearer handling.

@borngraced
Copy link
Member Author

borngraced commented Sep 14, 2022

@artemii235 I have updated this PR

Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last note.

fn from(err: ValidateSlpUtxosErr) -> Self {
match err.kind {
ValidateSlpUtxosErrKind::MultiReqErr(_) => Self::Transport(err.to_string()),
ValidateSlpUtxosErrKind::InvalidSlpTxData(_) => Self::TxDeserializationError(err.to_string()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Transaction is deserialized successfully in this case, so WrongPaymentTx seems more suitable here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

shamardy
shamardy previously approved these changes Sep 15, 2022
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fixes :)

Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this refactoring! LGTM
@artemii235 please confirm if all change requests are solved.

# Conflicts:
#	mm2src/coins/eth.rs
#	mm2src/coins/lightning.rs
#	mm2src/coins/qrc20.rs
#	mm2src/coins/solana.rs
#	mm2src/coins/solana/spl.rs
#	mm2src/coins/tendermint/tendermint_coin.rs
#	mm2src/coins/test_coin.rs
#	mm2src/coins/utxo/bch.rs
#	mm2src/coins/utxo/qtum.rs
#	mm2src/coins/utxo/slp.rs
#	mm2src/coins/utxo/utxo_common.rs
#	mm2src/coins/utxo/utxo_standard.rs
#	mm2src/coins/z_coin.rs
Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make last changes?

Comment on lines 1752 to 1754
.map_err(|err| ValidatePaymentError::TxDeserializationError(err.to_string())));
let second_pub = &try_f!(Public::from_slice(&input.maker_pub)
.map_err(|err| ValidatePaymentError::TxDeserializationError(err.to_string())));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two errors are InvalidInput

Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick fixes! LGTM!

Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@caglaryucekaya
Copy link

The discussion is closed, but while using the ValidatePaymentError I got the impression that InvalidInput means one of the transaction inputs is invalid and only after reading #1444 (comment) I understood what it actually means, so the naming was confusing for me. How about changing it to something like InvalidParameter?

@sergeyboyko0791
Copy link

It makes sense for me. Could you please rename the variant in your PR?

The discussion is closed, but while using the ValidatePaymentError I got the impression that InvalidInput means one of the transaction inputs is invalid and only after reading #1444 (comment) I understood what it actually means, so the naming was confusing for me. How about changing it to something like InvalidParameter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants