-
Notifications
You must be signed in to change notification settings - Fork 94
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
Conversation
borngraced
commented
Aug 16, 2022
•
edited
Loading
edited
- ValidatePaymentError
- AddressParseError
- MyAddressError
- CheckPaymentSentError
- SendRawTxError
- WaitForConfirmationsErr
- CoinInitResponseError
- GossipPeerError
There was a problem hiding this 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.
There was a problem hiding this 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
impl From<ScriptHashTypeNotSupported> for ValidatePaymentError { | ||
fn from(err: ScriptHashTypeNotSupported) -> Self { Self::ScriptHashTypeNotSupported(err.to_string()) } | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
@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) |
mm2src/coins/coin_errors.rs
Outdated
impl From<ScriptHashTypeNotSupported> for ValidatePaymentError { | ||
fn from(err: ScriptHashTypeNotSupported) -> Self { Self::ScriptHashTypeNotSupported(err.to_string()) } | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
mm2src/coins/eth.rs
Outdated
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)?; |
There was a problem hiding this comment.
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()))?;
@sergeyboyko0791 I have fixed the previous reviews and it's ready for another round:) thanks |
There was a problem hiding this 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/utxo/bchd_grpc.rs
Outdated
@@ -79,6 +79,10 @@ pub struct ValidateSlpUtxosErr { | |||
kind: ValidateSlpUtxosErrKind, | |||
} | |||
|
|||
impl From<ValidateSlpUtxosErr> for ValidatePaymentError { | |||
fn from(err: ValidateSlpUtxosErr) -> Self { Self::InvalidPaymentTxData(err.to_string()) } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok done
There was a problem hiding this 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
There was a problem hiding this 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
mm2src/coins/utxo/slp.rs
Outdated
ValidateHtlcError::InvalidSlpUtxo(e) => println!("{:?}", e), | ||
ValidatePaymentError::InvalidInput(e) => println!("{:?}", e), |
There was a problem hiding this comment.
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.
There was a problem hiding this 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
There was a problem hiding this 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 :)
|
@artemii235 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 |
@artemii235 I have updated this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last note.
mm2src/coins/utxo/bchd_grpc.rs
Outdated
fn from(err: ValidateSlpUtxosErr) -> Self { | ||
match err.kind { | ||
ValidateSlpUtxosErrKind::MultiReqErr(_) => Self::Transport(err.to_string()), | ||
ValidateSlpUtxosErrKind::InvalidSlpTxData(_) => Self::TxDeserializationError(err.to_string()), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this 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 :)
There was a problem hiding this 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
9e5f49e
There was a problem hiding this 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?
mm2src/coins/utxo/utxo_common.rs
Outdated
.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()))); |
There was a problem hiding this comment.
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
There was a problem hiding this 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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
The discussion is closed, but while using the |
It makes sense for me. Could you please rename the variant in your PR?
|