Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
daeafc8
send p2p transaction message to `txhlp/$coin.ticker` topic
onur-ozkan Apr 5, 2022
860163c
propogate the message if coin inside of the topic is activated
onur-ozkan Apr 5, 2022
43862b6
impl `send_raw_tx_bytes` for `MarketCoinOps`
onur-ozkan Apr 5, 2022
b02ce08
update transaction broadcasting
onur-ozkan Apr 5, 2022
f3747f7
create and use dedicated func for sending transaction messages
onur-ozkan Apr 5, 2022
7acdd35
remove unused field from `SwapMsgStore`
onur-ozkan Apr 5, 2022
06ecd56
fix linting
onur-ozkan Apr 6, 2022
5a73d5d
log the error on transaction broadcasting
onur-ozkan Apr 6, 2022
9a4c496
fix `send_raw_tx_bytes` of solana
onur-ozkan Apr 6, 2022
4a5d9c5
fix `send_raw_tx_bytes`
onur-ozkan Apr 6, 2022
a9f2e6b
move use statement
onur-ozkan Apr 6, 2022
840d022
save development state
onur-ozkan Apr 6, 2022
658ff56
send transactions as p2p message
onur-ozkan Apr 6, 2022
f8ed4cc
update exptected error result in test fn `construct_and_send_invalid_…
onur-ozkan Apr 7, 2022
fedbc32
[opt] inline wrapper functions
onur-ozkan Apr 7, 2022
78d64e3
fix `send_raw_tx` and test `send_raw_tx_bytes`
onur-ozkan Apr 7, 2022
e9d16a9
un-inline `send_raw_tx`
onur-ozkan Apr 7, 2022
8545045
Updates after code-review
onur-ozkan Apr 10, 2022
5c24769
change log level of tx broadcasting
onur-ozkan Apr 11, 2022
1c3d7bd
save development state
onur-ozkan Apr 13, 2022
b11fe2c
implement fail-safe returns for `send_taker_refunds_payment`
onur-ozkan Apr 13, 2022
98df7cf
save development state
onur-ozkan Apr 13, 2022
b1f9745
optimize code-base
onur-ozkan Apr 13, 2022
f136301
fix fmt
onur-ozkan Apr 13, 2022
9a203e7
create wrapper macro `FSTX_ERR` that wraps `ERR`
onur-ozkan Apr 13, 2022
b6c8ecb
fix indendation of `FSTX_ERR` macro
onur-ozkan Apr 13, 2022
d10d4a9
update namings
onur-ozkan Apr 13, 2022
3a2b0e9
replace `format!` with `ERRL!` in `FailSafeTxErr::RpcCallFailed`
onur-ozkan Apr 13, 2022
3299524
optimize PR diffs
onur-ozkan Apr 13, 2022
d2cbe7b
doc comment `send_raw_tx_bytes`
onur-ozkan Apr 13, 2022
6534913
optimize `mm2src/coins/utxo/utxo_common.rs` diffs
onur-ozkan Apr 13, 2022
1778350
use full path for tx macros
onur-ozkan Apr 14, 2022
903526f
Merge remote-tracking branch 'origin/dev' into p2p-transaction-helper…
onur-ozkan Apr 14, 2022
6b9e579
save development state
onur-ozkan Apr 18, 2022
19a493d
add test fn for `send_maker_spends_taker_payment`
onur-ozkan Apr 18, 2022
aa43384
update `test_send_contract_calls_recoverable_tx`
onur-ozkan Apr 18, 2022
21f2b6a
fix typo
onur-ozkan Apr 18, 2022
7811106
impl helper functions for `enum TransactionErr`
onur-ozkan Apr 19, 2022
9c9db1c
refactor `send_raw_tx_bytes`
onur-ozkan Apr 20, 2022
f461dcc
sub to `txhlp/$coin` when utxo coin enabled in native mode
onur-ozkan Apr 20, 2022
bad90b6
unbox `TransactionEnum` in `TxRecoverableError`
onur-ozkan Apr 20, 2022
8ca638f
add more spesific conditions on `txhlp` subscription
onur-ozkan Apr 21, 2022
ed574a5
rollback unnecessary broadcastings
onur-ozkan Apr 21, 2022
46ad808
fix PR notes
onur-ozkan Apr 29, 2022
d456326
fix auto-format's bug
onur-ozkan Apr 29, 2022
7e6c215
sync dev
onur-ozkan Apr 29, 2022
9444eda
remove unnecessary type casting
onur-ozkan Apr 29, 2022
5ba0dd1
resolve pr notes
onur-ozkan Apr 30, 2022
f87cef3
resolve pr notes
onur-ozkan Apr 30, 2022
21f9684
resolve pr notes
onur-ozkan May 4, 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
14 changes: 14 additions & 0 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,20 @@ impl MarketCoinOps for EthCoin {
)
}

fn send_raw_tx_bytes(&self, mut tx: &[u8]) -> Box<dyn Future<Item = String, Error = String> + Send> {
if tx.starts_with(b"0x") {
Comment thread
onur-ozkan marked this conversation as resolved.
Outdated
tx = &tx[2..];
}

Box::new(
self.web3
.eth()
.send_raw_transaction(tx.into())
.map(|res| format!("{:02x}", res))
.map_err(|e| ERRL!("{}", e)),
)
}

fn wait_for_confirmations(
&self,
tx: &[u8],
Expand Down
9 changes: 9 additions & 0 deletions mm2src/coins/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,15 @@ impl MarketCoinOps for LightningCoin {
))
}

fn send_raw_tx_bytes(&self, _tx: &[u8]) -> Box<dyn Future<Item = String, Error = String> + Send> {
Box::new(futures01::future::err(
MmError::new(
"send_raw_tx is not supported for lightning, please use send_payment method instead.".to_string(),
)
.to_string(),
))
}

// Todo: Implement this when implementing swaps for lightning as it's is used mainly for swaps
fn wait_for_confirmations(
&self,
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ pub trait MarketCoinOps {
/// Receives raw transaction bytes in hexadecimal format as input and returns tx hash in hexadecimal format
fn send_raw_tx(&self, tx: &str) -> Box<dyn Future<Item = String, Error = String> + Send>;

fn send_raw_tx_bytes(&self, tx: &[u8]) -> Box<dyn Future<Item = String, Error = String> + Send>;

fn wait_for_confirmations(
&self,
tx: &[u8],
Expand Down
4 changes: 4 additions & 0 deletions mm2src/coins/qrc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,10 @@ impl MarketCoinOps for Qrc20Coin {
utxo_common::send_raw_tx(&self.utxo, tx)
}

fn send_raw_tx_bytes(&self, tx: &[u8]) -> Box<dyn Future<Item = String, Error = String> + Send> {
utxo_common::send_raw_tx_bytes(&self.utxo, tx)
}

fn wait_for_confirmations(
&self,
tx: &[u8],
Expand Down
13 changes: 13 additions & 0 deletions mm2src/coins/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,19 @@ impl MarketCoinOps for SolanaCoin {
Box::new(fut.boxed().compat())
}

fn send_raw_tx_bytes(&self, tx: &[u8]) -> Box<dyn Future<Item = String, Error = String> + Send> {
let coin = self.clone();
let tx = tx.to_owned();
let fut = async move {
let tx: Transaction = deserialize(tx.as_slice())
.map_to_mm(|e| e)
.map_err(|e| format!("{:?}", e))?;
Comment thread
sergeyboyko0791 marked this conversation as resolved.
Outdated
let signature = coin.rpc().send_transaction(&tx).await.map_err(|e| format!("{:?}", e))?;
Comment thread
onur-ozkan marked this conversation as resolved.
Ok(signature.to_string())
};
Box::new(fut.boxed().compat())
}

fn wait_for_confirmations(
&self,
_tx: &[u8],
Expand Down
4 changes: 4 additions & 0 deletions mm2src/coins/solana/spl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ impl MarketCoinOps for SplToken {
self.platform_coin.send_raw_tx(tx)
}

fn send_raw_tx_bytes(&self, tx: &[u8]) -> Box<dyn Future<Item = String, Error = String> + Send> {
self.platform_coin.send_raw_tx_bytes(tx)
}

fn wait_for_confirmations(
&self,
_tx: &[u8],
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/test_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ impl MarketCoinOps for TestCoin {
/// Receives raw transaction bytes in hexadecimal format as input and returns tx hash in hexadecimal format
fn send_raw_tx(&self, tx: &str) -> Box<dyn Future<Item = String, Error = String> + Send> { unimplemented!() }

fn send_raw_tx_bytes(&self, tx: &[u8]) -> Box<dyn Future<Item = String, Error = String> + Send> { unimplemented!() }

fn wait_for_confirmations(
&self,
tx: &[u8],
Expand Down
4 changes: 4 additions & 0 deletions mm2src/coins/utxo/bch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,10 @@ impl MarketCoinOps for BchCoin {
utxo_common::send_raw_tx(&self.utxo_arc, tx)
}

fn send_raw_tx_bytes(&self, tx: &[u8]) -> Box<dyn Future<Item = String, Error = String> + Send> {
utxo_common::send_raw_tx_bytes(&self.utxo_arc, tx)
}

fn wait_for_confirmations(
&self,
tx: &[u8],
Expand Down
4 changes: 4 additions & 0 deletions mm2src/coins/utxo/qtum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,10 @@ impl MarketCoinOps for QtumCoin {
utxo_common::send_raw_tx(&self.utxo_arc, tx)
}

fn send_raw_tx_bytes(&self, tx: &[u8]) -> Box<dyn Future<Item = String, Error = String> + Send> {
utxo_common::send_raw_tx_bytes(&self.utxo_arc, tx)
}

fn wait_for_confirmations(
&self,
tx: &[u8],
Expand Down
13 changes: 13 additions & 0 deletions mm2src/coins/utxo/slp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,19 @@ impl MarketCoinOps for SlpToken {
Box::new(fut.boxed().compat())
}

fn send_raw_tx_bytes(&self, tx: &[u8]) -> Box<dyn Future<Item = String, Error = String> + Send> {
let coin = self.clone();
Comment thread
onur-ozkan marked this conversation as resolved.
Outdated
let tx = tx.to_owned();
let fut = async move {
let tx = deserialize(tx.as_slice())
.map_to_mm(|e| e)
.map_err(|e| format!("{:?}", e))?;
Comment thread
onur-ozkan marked this conversation as resolved.
Outdated
let hash = coin.broadcast_tx(&tx).await.map_err(|e| format!("{:?}", e))?;
Ok(format!("{:?}", hash))
};
Box::new(fut.boxed().compat())
}

fn wait_for_confirmations(
&self,
tx: &[u8],
Expand Down
9 changes: 9 additions & 0 deletions mm2src/coins/utxo/utxo_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1706,6 +1706,15 @@ pub fn send_raw_tx(coin: &UtxoCoinFields, tx: &str) -> Box<dyn Future<Item = Str
)
}

pub fn send_raw_tx_bytes(coin: &UtxoCoinFields, tx: &[u8]) -> Box<dyn Future<Item = String, Error = String> + Send> {
Box::new(
coin.rpc_client
.send_raw_transaction(tx.into())
.map_err(|e| ERRL!("{}", e))
.map(|hash| format!("{:?}", hash)),
)
}

pub fn wait_for_confirmations(
coin: &UtxoCoinFields,
tx: &[u8],
Expand Down
4 changes: 4 additions & 0 deletions mm2src/coins/utxo/utxo_standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ impl MarketCoinOps for UtxoStandardCoin {
utxo_common::send_raw_tx(&self.utxo_arc, tx)
}

fn send_raw_tx_bytes(&self, tx: &[u8]) -> Box<dyn Future<Item = String, Error = String> + Send> {
utxo_common::send_raw_tx_bytes(&self.utxo_arc, tx)
}

fn wait_for_confirmations(
&self,
tx: &[u8],
Expand Down
4 changes: 4 additions & 0 deletions mm2src/coins/z_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,10 @@ impl MarketCoinOps for ZCoin {
utxo_common::send_raw_tx(self.as_ref(), tx)
}

fn send_raw_tx_bytes(&self, tx: &[u8]) -> Box<dyn Future<Item = String, Error = String> + Send> {
utxo_common::send_raw_tx_bytes(self.as_ref(), tx)
}

fn wait_for_confirmations(
&self,
tx: &[u8],
Expand Down
14 changes: 13 additions & 1 deletion mm2src/lp_network.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use coins::lp_coinfind;
Comment thread
onur-ozkan marked this conversation as resolved.
Outdated
/******************************************************************************
* Copyright © 2014-2019 The SuperNET Developers. *
* *
Expand All @@ -17,10 +18,10 @@
// marketmaker
//
use common::executor::spawn;
use common::log;
use common::mm_ctx::{MmArc, MmWeak};
use common::mm_error::prelude::*;
use common::mm_metrics::{ClockOps, MetricsOps};
use common::{log, Future01CompatExt};
use derive_more::Display;
use futures::{channel::oneshot, StreamExt};
use mm2_libp2p::atomicdex_behaviour::{AdexBehaviourCmd, AdexBehaviourEvent, AdexCmdTx, AdexEventRx, AdexResponse,
Expand Down Expand Up @@ -137,6 +138,16 @@ async fn process_p2p_message(
lp_swap::process_msg(ctx.clone(), split.next().unwrap_or_default(), &message.data).await;
to_propagate = true;
},
Some(lp_swap::TX_HELPER_PREFIX) => {
if let Some(pair) = split.next() {
if let Ok(Some(coin)) = lp_coinfind(&ctx, pair).await {
match coin.send_raw_tx_bytes(&message.data).compat().await {
Ok(id) => log::info!("Transaction broadcasted successfully: {:?} ", id),
Comment thread
sergeyboyko0791 marked this conversation as resolved.
Outdated
Err(e) => log::error!("Broadcast transaction failed. {}", e),
}
}
}
},
None | Some(_) => (),
}
}
Expand All @@ -149,6 +160,7 @@ async fn process_p2p_message(
&message.data,
i_am_relay,
);

if process_fut.await {
to_propagate = true;
}
Expand Down
22 changes: 22 additions & 0 deletions mm2src/lp_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ pub use trade_preimage::trade_preimage_rpc;

pub const SWAP_PREFIX: TopicPrefix = "swap";

pub const TX_HELPER_PREFIX: TopicPrefix = "txhlp";

cfg_wasm32! {
use common::indexed_db::{ConstructibleDb, DbLocked};
use swap_wasm_db::{InitDbResult, SwapDb};
Expand Down Expand Up @@ -188,6 +190,16 @@ pub fn broadcast_swap_message(ctx: &MmArc, topic: String, msg: SwapMsg, p2p_priv
broadcast_p2p_msg(ctx, vec![topic], encoded_msg);
}

pub fn broadcast_transaction_message(ctx: &MmArc, topic: String, msg: Vec<u8>, p2p_privkey: &Option<H256Json>) {
let p2p_private = match p2p_privkey {
Some(privkey) => privkey.0,
None => ctx.secp256k1_key_pair.or(&&|| panic!()).private().secret.take(),
};

let encoded_msg = encode_and_sign(&msg, &p2p_private).unwrap();
broadcast_p2p_msg(ctx, vec![topic], encoded_msg);
}

pub async fn process_msg(ctx: MmArc, topic: &str, msg: &[u8]) {
let uuid = match Uuid::from_str(topic) {
Ok(u) => u,
Expand Down Expand Up @@ -235,6 +247,16 @@ pub async fn process_msg(ctx: MmArc, topic: &str, msg: &[u8]) {

pub fn swap_topic(uuid: &Uuid) -> String { pub_sub_topic(SWAP_PREFIX, &uuid.to_string()) }

/// Formats and returns a topic format for `txhlp`.
///
/// # Usage
/// ```ignore
/// let topic = tx_helper_topic("BTC");
/// // Returns topic format `txhlp/BTC` as String type.
/// ```
#[inline(always)]
pub fn tx_helper_topic(coin: &str) -> String { pub_sub_topic(TX_HELPER_PREFIX, coin) }

async fn recv_swap_msg<T>(
ctx: MmArc,
mut getter: impl FnMut(&mut SwapMsgStore) -> Option<T>,
Expand Down
16 changes: 16 additions & 0 deletions mm2src/lp_swap/maker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::{broadcast_my_swap_status, broadcast_swap_message_every, check_other_
use crate::mm2::lp_dispatcher::{DispatcherContext, LpEvents};
use crate::mm2::lp_network::subscribe_to_topic;
use crate::mm2::lp_ordermatch::{MakerOrderBuilder, OrderConfirmationsSettings};
use crate::mm2::lp_swap::{broadcast_transaction_message, tx_helper_topic};
use crate::mm2::MM_VERSION;
use bigdecimal::BigDecimal;
use bitcrypto::dhash160;
Expand Down Expand Up @@ -853,6 +854,13 @@ impl MakerSwap {
},
};

broadcast_transaction_message(
&self.ctx,
tx_helper_topic(self.taker_coin.ticker()),
transaction.tx_hex(),
&self.p2p_privkey,
);

let tx_hash = transaction.tx_hash();
log!({ "Taker payment spend tx {:02x}", tx_hash });
let tx_ident = TransactionIdentifier {
Expand Down Expand Up @@ -925,6 +933,14 @@ impl MakerSwap {
]))
},
};

broadcast_transaction_message(
&self.ctx,
tx_helper_topic(self.maker_coin.ticker()),
transaction.tx_hex(),
&self.p2p_privkey,
);

let tx_hash = transaction.tx_hash();
log!({ "Maker payment refund tx {:02x}", tx_hash });
let tx_ident = TransactionIdentifier {
Expand Down
15 changes: 15 additions & 0 deletions mm2src/lp_swap/taker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::{broadcast_my_swap_status, broadcast_swap_message_every, check_other_
SwapConfirmationsSettings, SwapError, SwapMsg, SwapsContext, TransactionIdentifier, WAIT_CONFIRM_INTERVAL};
use crate::mm2::lp_network::subscribe_to_topic;
use crate::mm2::lp_ordermatch::{MatchBy, OrderConfirmationsSettings, TakerAction, TakerOrderBuilder};
use crate::mm2::lp_swap::{broadcast_transaction_message, tx_helper_topic};
use crate::mm2::MM_VERSION;
use bigdecimal::BigDecimal;
use coins::{lp_coinfind, CanRefundHtlc, FeeApproxStage, FoundSwapTxSpend, MmCoinEnum, TradeFee, TradePreimageValue,
Expand Down Expand Up @@ -1287,6 +1288,13 @@ impl TakerSwap {
},
};

broadcast_transaction_message(
&self.ctx,
tx_helper_topic(self.maker_coin.ticker()),
transaction.tx_hex(),
&self.p2p_privkey,
);

let tx_hash = transaction.tx_hash();
log!({"Maker payment spend tx {:02x}", tx_hash });
let tx_ident = TransactionIdentifier {
Expand Down Expand Up @@ -1330,6 +1338,13 @@ impl TakerSwap {
},
};

broadcast_transaction_message(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I have noticed that it's better to broadcast_transaction_message also if send_taker_refunds_payment and other fns fail. Please make send_taker_refunds_payment and others to also return the generated transaction in case of error and call broadcast_transaction_message for it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

trait SwapOps functions are (only TransactionFut returners) now calling broadcast_transaction_message right after generating transaction.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'm a little confused 😅
Could you please tell where trait SwapOps functions call broadcast_transaction_message? I couldn't find it in the changes

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'm a little confused sweat_smile Could you please tell where trait SwapOps functions call broadcast_transaction_message? I couldn't find it in the changes

Sorry, I couldn't explain correctly. I didn't call broadcast_transaction_messageinside of the trait SwapOps functions, I call it in maker_swap and taker_swap after each trait SwapOps functions. But I guess Artem is pointing different thing than what I did here. I will update the changes today.

&self.ctx,
tx_helper_topic(self.taker_coin.ticker()),
transaction.tx_hex(),
&self.p2p_privkey,
);

let tx_hash = transaction.tx_hash();
log!({"Taker refund tx hash {:02x}", tx_hash });
let tx_ident = TransactionIdentifier {
Expand Down