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

feat(trading-proto-upgrade): swap UTXO PoC using Storable State Machine. #1958

Merged
merged 33 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6524c38
StorableStateMachine WIP.
artemii235 Aug 3, 2023
0b8ff51
WIP. Created mm2_state_machine crate.
artemii235 Aug 4, 2023
3322f6f
Storable state machine WIP.
Aug 4, 2023
b4100e0
Storable state machine WIP.
artemii235 Aug 7, 2023
454c8a3
Storable state machine WIP.
artemii235 Aug 8, 2023
bb34df8
Storable state machine WIP.
Aug 9, 2023
b0cf800
Storable state machine WIP.
Aug 9, 2023
e8fc4d4
Storable state machine WIP.
Aug 10, 2023
6988b33
Fix wasm.
artemii235 Aug 15, 2023
7108f51
MakerSwapStateMachine WIP.
artemii235 Aug 16, 2023
fbb8682
MakerSwapStateMachine WIP.
artemii235 Aug 17, 2023
d549a1c
Refactor new proto dex fee to combined payment.
artemii235 Aug 18, 2023
9587b48
TakerSwapStateMachine WIP.
artemii235 Aug 21, 2023
f9c2867
test_v2_swap_utxo_utxo WIP.
artemii235 Aug 22, 2023
8cbc813
WIP. Implementing swap v2 messages using protobuf.
Aug 24, 2023
eb22f7e
WIP. P2P messages.
artemii235 Aug 25, 2023
21d8db8
WIP. Swap negotiation.
artemii235 Aug 28, 2023
47a8a20
WIP. Swap negotiation.
artemii235 Aug 29, 2023
cd14f50
WIP. P2P messages exchange.
Aug 29, 2023
6c7636d
WIP. Sending actual transactions.
artemii235 Aug 30, 2023
5f1c8e6
WIP. Sending actual transactions.
artemii235 Aug 31, 2023
eb282a3
Preparing to open PR.
artemii235 Sep 1, 2023
5e89e62
Fix fmt.
artemii235 Sep 1, 2023
e41c7b3
Preparing to open PR. Fix after cherry-pick.
artemii235 Sep 4, 2023
8835b1a
Merge remote-tracking branch 'origin/dev' into swap-proto-upgrade-ite…
artemii235 Sep 5, 2023
ea6ed8a
Add more logging.
artemii235 Sep 5, 2023
7a1f609
Adding doc comments.
artemii235 Sep 6, 2023
7ad248a
Use SIGHASH_SINGLE for taker signature to spend payment.
artemii235 Sep 5, 2023
b60ba4d
Adding doc comments.
artemii235 Sep 6, 2023
b99fd30
Adding doc comments.
artemii235 Sep 7, 2023
b54fea5
Adding doc comments.
artemii235 Sep 7, 2023
05c2467
Merge remote-tracking branch 'origin/dev' into swap-proto-upgrade-ite…
artemii235 Sep 12, 2023
0e2b7b2
Review fixes.
artemii235 Sep 13, 2023
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
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ members = [
"mm2src/mm2_net",
"mm2src/mm2_number",
"mm2src/mm2_rpc",
"mm2src/mm2_state_machine",
"mm2src/rpc_task",
"mm2src/mm2_test_helpers",
"mm2src/trezor",
Expand Down
1 change: 1 addition & 0 deletions mm2src/coins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ mm2_metrics = { path = "../mm2_metrics" }
mm2_net = { path = "../mm2_net" }
mm2_number = { path = "../mm2_number"}
mm2_rpc = { path = "../mm2_rpc" }
mm2_state_machine = { path = "../mm2_state_machine" }
mocktopus = "0.8.0"
num-traits = "0.2"
parking_lot = { version = "0.12.0", features = ["nightly"] }
Expand Down
17 changes: 15 additions & 2 deletions mm2src/coins/coin_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,36 @@ use crate::{eth::Web3RpcError, my_tx_history_v2::MyTxHistoryErrorV2, utxo::rpc_c
use futures01::Future;
use mm2_err_handle::prelude::MmError;
use spv_validation::helpers_validation::SPVError;
use std::num::TryFromIntError;

/// Helper type used as result for swap payment validation function(s)
pub type ValidatePaymentFut<T> = Box<dyn Future<Item = T, Error = MmError<ValidatePaymentError>> + Send>;
laruh marked this conversation as resolved.
Show resolved Hide resolved

/// Enum covering possible error cases of swap payment validation
#[derive(Debug, Display)]
pub enum ValidatePaymentError {
/// Should be used to indicate internal MM2 state problems (e.g., DB errors, etc.).
InternalError(String),
// Problem with deserializing the transaction, or one of the transaction parts is invalid.
/// Problem with deserializing the transaction, or one of the transaction parts is invalid.
TxDeserializationError(String),
/// One of the input parameters is invalid.
InvalidParameter(String),
/// Coin's RPC returned unexpected/invalid response during payment validation.
InvalidRpcResponse(String),
/// Payment transaction doesn't exist on-chain.
TxDoesNotExist(String),
/// SPV client error.
SPVError(SPVError),
/// Payment transaction is in unexpected state. E.g., `Uninitialized` instead of `Sent` for ETH payment.
UnexpectedPaymentState(String),
/// Transport (RPC) error.
Transport(String),
// Transaction has wrong properties, for example, it has been sent to a wrong address
/// Transaction has wrong properties, for example, it has been sent to a wrong address.
WrongPaymentTx(String),
/// Indicates error during watcher reward calculation.
WatcherRewardError(String),
/// Input payment timelock overflows the type used by specific coin.
TimelockOverflow(TryFromIntError),
laruh marked this conversation as resolved.
Show resolved Hide resolved
}

impl From<rlp::DecoderError> for ValidatePaymentError {
Expand Down
25 changes: 18 additions & 7 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ use serde_json::{self as json, Value as Json};
use serialization::{CompactInteger, Serializable, Stream};
use sha3::{Digest, Keccak256};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::convert::{TryFrom, TryInto};
use std::ops::Deref;
#[cfg(not(target_arch = "wasm32"))] use std::path::PathBuf;
use std::str::FromStr;
Expand Down Expand Up @@ -1137,7 +1137,10 @@ impl SwapOps for EthCoin {
&self,
if_my_payment_sent_args: CheckIfMyPaymentSentArgs,
) -> Box<dyn Future<Item = Option<TransactionEnum>, Error = String> + Send> {
let id = self.etomic_swap_id(if_my_payment_sent_args.time_lock, if_my_payment_sent_args.secret_hash);
let id = self.etomic_swap_id(
try_fus!(if_my_payment_sent_args.time_lock.try_into()),
if_my_payment_sent_args.secret_hash,
);
let swap_contract_address = try_fus!(if_my_payment_sent_args.swap_contract_address.try_to_address());
let selfi = self.clone();
let from_block = if_my_payment_sent_args.search_from_block;
Expand Down Expand Up @@ -1420,7 +1423,7 @@ impl WatcherOps for EthCoin {
fn create_maker_payment_spend_preimage(
&self,
maker_payment_tx: &[u8],
_time_lock: u32,
_time_lock: u64,
_maker_pub: &[u8],
_secret_hash: &[u8],
_swap_unique_data: &[u8],
Expand All @@ -1435,7 +1438,7 @@ impl WatcherOps for EthCoin {
fn create_taker_payment_refund_preimage(
&self,
taker_payment_tx: &[u8],
_time_lock: u32,
_time_lock: u64,
_maker_pub: &[u8],
_secret_hash: &[u8],
_swap_contract_address: &Option<BytesJson>,
Expand Down Expand Up @@ -1476,9 +1479,13 @@ impl WatcherOps for EthCoin {
.map_to_mm(|err| ValidatePaymentError::TxDeserializationError(err.to_string())));
let sender = try_f!(addr_from_raw_pubkey(&input.taker_pub).map_to_mm(ValidatePaymentError::InvalidParameter));
let receiver = try_f!(addr_from_raw_pubkey(&input.maker_pub).map_to_mm(ValidatePaymentError::InvalidParameter));
let time_lock = try_f!(input
.time_lock
.try_into()
.map_to_mm(ValidatePaymentError::TimelockOverflow));

let selfi = self.clone();
let swap_id = selfi.etomic_swap_id(input.time_lock, &input.secret_hash);
let swap_id = selfi.etomic_swap_id(time_lock, &input.secret_hash);
let secret_hash = if input.secret_hash.len() == 32 {
ripemd160(&input.secret_hash).to_vec()
} else {
Expand Down Expand Up @@ -3023,7 +3030,7 @@ impl EthCoin {
fn send_hash_time_locked_payment(&self, args: SendPaymentArgs<'_>) -> EthTxFut {
let receiver_addr = try_tx_fus!(addr_from_raw_pubkey(args.other_pubkey));
let swap_contract_address = try_tx_fus!(args.swap_contract_address.try_to_address());
let id = self.etomic_swap_id(args.time_lock, args.secret_hash);
let id = self.etomic_swap_id(try_tx_fus!(args.time_lock.try_into()), args.secret_hash);
let trade_amount = try_tx_fus!(wei_from_big_decimal(&args.amount, self.decimals));

let time_lock = U256::from(args.time_lock);
Expand Down Expand Up @@ -3882,9 +3889,13 @@ impl EthCoin {
try_f!(SignedEthTx::new(unsigned)
.map_to_mm(|err| ValidatePaymentError::TxDeserializationError(err.to_string())));
let sender = try_f!(addr_from_raw_pubkey(&input.other_pub).map_to_mm(ValidatePaymentError::InvalidParameter));
let time_lock = try_f!(input
.time_lock
.try_into()
.map_to_mm(ValidatePaymentError::TimelockOverflow));

let selfi = self.clone();
let swap_id = selfi.etomic_swap_id(input.time_lock, &input.secret_hash);
let swap_id = selfi.etomic_swap_id(time_lock, &input.secret_hash);
let decimals = self.decimals;
let secret_hash = if input.secret_hash.len() == 32 {
ripemd160(&input.secret_hash).to_vec()
Expand Down
10 changes: 5 additions & 5 deletions mm2src/coins/eth/eth_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use crate::IguanaPrivKey;
use common::{block_on, now_sec_u32, wait_until_sec};
use common::{block_on, now_sec, wait_until_sec};
use crypto::privkey::key_pair_from_seed;
use ethkey::{Generator, Random};
use mm2_core::mm_ctx::{MmArc, MmCtxBuilder};
Expand Down Expand Up @@ -343,7 +343,7 @@ fn send_and_refund_erc20_payment() {
abortable_system: AbortableQueue::default(),
}));

let time_lock = now_sec_u32() - 200;
let time_lock = now_sec() - 200;
let secret_hash = &[1; 20];
let maker_payment_args = SendPaymentArgs {
time_lock_duration: 0,
Expand All @@ -360,7 +360,7 @@ fn send_and_refund_erc20_payment() {
let payment = coin.send_maker_payment(maker_payment_args).wait().unwrap();
log!("{:?}", payment);

let swap_id = coin.etomic_swap_id(time_lock, secret_hash);
let swap_id = coin.etomic_swap_id(time_lock.try_into().unwrap(), secret_hash);
let status = block_on(
coin.payment_status(
Address::from_str(ETH_DEV_SWAP_CONTRACT).unwrap(),
Expand Down Expand Up @@ -429,7 +429,7 @@ fn send_and_refund_eth_payment() {
abortable_system: AbortableQueue::default(),
}));

let time_lock = now_sec_u32() - 200;
let time_lock = now_sec() - 200;
let secret_hash = &[1; 20];
let send_maker_payment_args = SendPaymentArgs {
time_lock_duration: 0,
Expand All @@ -447,7 +447,7 @@ fn send_and_refund_eth_payment() {

log!("{:?}", payment);

let swap_id = coin.etomic_swap_id(time_lock, secret_hash);
let swap_id = coin.etomic_swap_id(time_lock.try_into().unwrap(), secret_hash);
let status = block_on(
coin.payment_status(
Address::from_str(ETH_DEV_SWAP_CONTRACT).unwrap(),
Expand Down
4 changes: 2 additions & 2 deletions mm2src/coins/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ impl WatcherOps for LightningCoin {
fn create_maker_payment_spend_preimage(
&self,
_maker_payment_tx: &[u8],
_time_lock: u32,
_time_lock: u64,
_maker_pub: &[u8],
_secret_hash: &[u8],
_swap_unique_data: &[u8],
Expand All @@ -974,7 +974,7 @@ impl WatcherOps for LightningCoin {
fn create_taker_payment_refund_preimage(
&self,
_taker_payment_tx: &[u8],
_time_lock: u32,
_time_lock: u64,
_maker_pub: &[u8],
_secret_hash: &[u8],
_swap_contract_address: &Option<BytesJson>,
Expand Down
Loading