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] Include maker/taker pubkeys in MM2.db stats_swaps table #1665

Merged
merged 22 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
5 changes: 5 additions & 0 deletions mm2src/mm2_main/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ fn migration_7() -> Vec<(&'static str, Vec<String>)> {
db_common::sqlite::execute_batch(stats_swaps::ADD_COINS_PRICE_INFOMATION)
}

fn migration_8() -> Vec<(&'static str, Vec<String>)> {
db_common::sqlite::execute_batch(stats_swaps::ADD_MAKER_TAKER_PUBKEYS)
}

async fn statements_for_migration(ctx: &MmArc, current_migration: i64) -> Option<Vec<(&'static str, Vec<String>)>> {
match current_migration {
1 => Some(migration_1(ctx).await),
Expand All @@ -106,6 +110,7 @@ async fn statements_for_migration(ctx: &MmArc, current_migration: i64) -> Option
5 => Some(migration_5()),
6 => Some(migration_6()),
7 => Some(migration_7()),
8 => Some(migration_8()),
_ => None,
}
}
Expand Down
37 changes: 35 additions & 2 deletions mm2src/mm2_main/src/database/stats_swaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,23 @@ const INSERT_STATS_SWAP: &str = "INSERT INTO stats_swaps (
taker_amount,
is_success,
maker_coin_usd_price,
taker_coin_usd_price
) VALUES (:maker_coin, :maker_coin_ticker, :maker_coin_platform, :taker_coin, :taker_coin_ticker, :taker_coin_platform, :uuid, :started_at, :finished_at, :maker_amount, :taker_amount, :is_success, :maker_coin_usd_price, :taker_coin_usd_price)";
taker_coin_usd_price,
maker_pubkey,
taker_pubkey
) VALUES (:maker_coin, :maker_coin_ticker, :maker_coin_platform, :taker_coin, :taker_coin_ticker,
:taker_coin_platform, :uuid, :started_at, :finished_at, :maker_amount, :taker_amount, :is_success,
:maker_coin_usd_price, :taker_coin_usd_price, :maker_pubkey, :taker_pubkey)";

pub const ADD_COINS_PRICE_INFOMATION: &[&str] = &[
"ALTER TABLE stats_swaps ADD COLUMN maker_coin_usd_price DECIMAL;",
"ALTER TABLE stats_swaps ADD COLUMN taker_coin_usd_price DECIMAL;",
];

pub const ADD_MAKER_TAKER_PUBKEYS: &[&str] = &[
"ALTER TABLE stats_swaps ADD COLUMN maker_pubkey VARCHAR(255);",
"ALTER TABLE stats_swaps ADD COLUMN taker_pubkey VARCHAR(255);",
Comment on lines +59 to +60
Copy link
Member

Choose a reason for hiding this comment

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

Can't just "ALTER TABLE stats_swaps ADD COLUMN maker_pubkey VARCHAR(255), ADD COLUMN taker_pubkey VARCHAR(255); work?

Copy link
Member Author

Choose a reason for hiding this comment

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

];

pub const ADD_SPLIT_TICKERS: &[&str] = &[
"ALTER TABLE stats_swaps ADD COLUMN maker_coin_ticker VARCHAR(255) NOT NULL DEFAULT '';",
"ALTER TABLE stats_swaps ADD COLUMN maker_coin_platform VARCHAR(255) NOT NULL DEFAULT '';",
Expand Down Expand Up @@ -118,13 +127,23 @@ fn insert_stats_maker_swap_sql(swap: &MakerSavedSwap) -> Option<(&'static str, O
return None;
},
};

let finished_at = match swap.finished_at() {
Ok(t) => t.to_string(),
Err(e) => {
error!("Error {} on getting swap {} finished_at", e, swap.uuid);
return None;
},
};

let pubkeys = match swap.swap_pubkeys() {
Ok(p) => p,
Err(e) => {
error!("Error {} on getting swap {} pubkeys", e, swap.uuid);
return None;
},
};

let is_success = swap
.is_success()
.expect("is_success can return error only when swap is not finished");
Expand All @@ -147,7 +166,10 @@ fn insert_stats_maker_swap_sql(swap: &MakerSavedSwap) -> Option<(&'static str, O
":is_success": (is_success as u32).to_string(),
":maker_coin_usd_price": swap.maker_coin_usd_price.as_ref().map(|p| p.to_string()),
":taker_coin_usd_price": swap.taker_coin_usd_price.as_ref().map(|p| p.to_string()),
":maker_pubkey": pubkeys.maker,
":taker_pubkey": pubkeys.taker,
};

Some((INSERT_STATS_SWAP, params))
}

Expand Down Expand Up @@ -198,6 +220,15 @@ fn insert_stats_taker_swap_sql(swap: &TakerSavedSwap) -> Option<(&'static str, O
return None;
},
};

let pubkeys = match swap.swap_pubkeys() {
Ok(p) => p,
Err(e) => {
error!("Error {} on getting swap {} pubkeys", e, swap.uuid);
return None;
},
};

let is_success = swap
.is_success()
.expect("is_success can return error only when swap is not finished");
Expand All @@ -220,6 +251,8 @@ fn insert_stats_taker_swap_sql(swap: &TakerSavedSwap) -> Option<(&'static str, O
":is_success": (is_success as u32).to_string(),
":maker_coin_usd_price": swap.maker_coin_usd_price.as_ref().map(|p| p.to_string()),
":taker_coin_usd_price": swap.taker_coin_usd_price.as_ref().map(|p| p.to_string()),
":maker_pubkey": pubkeys.maker,
":taker_pubkey": pubkeys.taker,
};
Some((INSERT_STATS_SWAP, params))
}
Expand Down
10 changes: 10 additions & 0 deletions mm2src/mm2_main/src/lp_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,16 @@ fn detect_secret_hash_algo(maker_coin: &MmCoinEnum, taker_coin: &MmCoinEnum) ->
}
}

pub struct SwapPubkeys {
pub maker: String,
pub taker: String,
}

impl SwapPubkeys {
#[inline]
fn new(maker: String, taker: String) -> Self { SwapPubkeys { maker, taker } }
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod lp_swap_tests {
use super::*;
Expand Down
19 changes: 17 additions & 2 deletions mm2src/mm2_main/src/lp_swap/maker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use super::{broadcast_my_swap_status, broadcast_p2p_tx_msg, broadcast_swap_messa
get_locked_amount, recv_swap_msg, swap_topic, taker_payment_spend_deadline, tx_helper_topic,
wait_for_maker_payment_conf_until, AtomicSwap, LockedAmount, MySwapInfo, NegotiationDataMsg,
NegotiationDataV2, NegotiationDataV3, RecoveredSwap, RecoveredSwapAction, SavedSwap, SavedSwapIo,
SavedTradeFee, SecretHashAlgo, SwapConfirmationsSettings, SwapError, SwapMsg, SwapTxDataMsg, SwapsContext,
TransactionIdentifier, WAIT_CONFIRM_INTERVAL};
SavedTradeFee, SecretHashAlgo, SwapConfirmationsSettings, SwapError, SwapMsg, SwapPubkeys, SwapTxDataMsg,
SwapsContext, TransactionIdentifier, WAIT_CONFIRM_INTERVAL};
use crate::mm2::lp_dispatcher::{DispatcherContext, LpEvents};
use crate::mm2::lp_network::subscribe_to_topic;
use crate::mm2::lp_ordermatch::{MakerOrderBuilder, OrderConfirmationsSettings};
Expand Down Expand Up @@ -1922,6 +1922,21 @@ impl MakerSavedSwap {
self.taker_coin_usd_price = Some(rates.rel);
}
}

pub fn swap_pubkeys(&self) -> Result<SwapPubkeys, String> {
match self.events.first() {
Some(event) => match &event.event {
// TODO: Adjust for private coins when/if they are braodcasted
// TODO: Adjust for HD wallet when completed
MakerSwapEvent::Started(data) => Ok(SwapPubkeys::new(
data.my_persistent_pub.to_string(),
data.taker.to_string(),
)),
_ => ERR!("First swap event must be Started"),
},
None => ERR!("Can't get maker/taker pubkey, events are empty"),
}
}
}

#[allow(clippy::large_enum_variant)]
Expand Down
19 changes: 17 additions & 2 deletions mm2src/mm2_main/src/lp_swap/taker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use super::{broadcast_my_swap_status, broadcast_swap_message, broadcast_swap_mes
check_other_coin_balance_for_swap, dex_fee_amount_from_taker_coin, dex_fee_rate, dex_fee_threshold,
get_locked_amount, recv_swap_msg, swap_topic, wait_for_maker_payment_conf_until, AtomicSwap, LockedAmount,
MySwapInfo, NegotiationDataMsg, NegotiationDataV2, NegotiationDataV3, RecoveredSwap, RecoveredSwapAction,
SavedSwap, SavedSwapIo, SavedTradeFee, SwapConfirmationsSettings, SwapError, SwapMsg, SwapTxDataMsg,
SwapsContext, TransactionIdentifier, WAIT_CONFIRM_INTERVAL};
SavedSwap, SavedSwapIo, SavedTradeFee, SwapConfirmationsSettings, SwapError, SwapMsg, SwapPubkeys,
SwapTxDataMsg, 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_price::fetch_swap_coins_price;
Expand Down Expand Up @@ -302,6 +302,21 @@ impl TakerSavedSwap {
self.taker_coin_usd_price = Some(rates.rel);
}
}

pub fn swap_pubkeys(&self) -> Result<SwapPubkeys, String> {
match self.events.first() {
Some(event) => match &event.event {
// TODO: Adjust for private coins when/if they are braodcasted
// TODO: Adjust for HD wallet when completed
TakerSwapEvent::Started(data) => Ok(SwapPubkeys::new(
data.maker.to_string(),
data.my_persistent_pub.to_string(),
)),
_ => ERR!("First swap event must be Started"),
},
None => ERR!("Can't get maker/taker pubkey, events are empty"),
}
}
}

#[allow(clippy::large_enum_variant)]
Expand Down
18 changes: 9 additions & 9 deletions mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::integration_tests_common::*;
use common::executor::Timer;
use common::{cfg_native, cfg_wasm32, get_utc_timestamp, log};
use common::{cfg_native, cfg_wasm32, get_utc_timestamp, log, new_uuid};
use crypto::privkey::key_pair_from_seed;
use http::{HeaderMap, StatusCode};
use mm2_main::mm2::lp_ordermatch::MIN_ORDER_KEEP_ALIVE_INTERVAL;
Expand Down Expand Up @@ -1417,7 +1417,7 @@ fn test_swap_status() {
"userpass": mm.userpass,
"method": "my_swap_status",
"params": {
"uuid":Uuid::new_v4(),
"uuid": new_uuid(),
}
})))
.unwrap();
Expand All @@ -1428,7 +1428,7 @@ fn test_swap_status() {
"userpass": mm.userpass,
"method": "stats_swap_status",
"params": {
"uuid":Uuid::new_v4(),
"uuid": new_uuid(),
}
})))
.unwrap();
Expand Down Expand Up @@ -3349,7 +3349,7 @@ fn test_add_delegation_qtum() {
"pass".into(),
None,
)
.unwrap();
.unwrap();

let json = block_on(enable_electrum(&mm, "tQTUM", false, &[
"electrum1.cipig.net:10071",
Expand Down Expand Up @@ -3434,7 +3434,7 @@ fn test_remove_delegation_qtum() {
"pass".into(),
None,
)
.unwrap();
.unwrap();

let json = block_on(enable_electrum(&mm, "tQTUM", false, &[
"electrum1.cipig.net:10071",
Expand Down Expand Up @@ -4518,7 +4518,7 @@ fn test_tx_history_tbtc_non_segwit() {
"mm2": 1,
"tx_history": true,
})))
.unwrap();
.unwrap();
assert_eq!(
electrum.0,
StatusCode::OK,
Expand Down Expand Up @@ -6827,9 +6827,9 @@ fn test_sign_verify_message_eth() {
"0xcdf11a9c4591fb7334daa4b21494a2590d3f7de41c7d2b333a5b61ca59da9b311b492374cc0ba4fbae53933260fa4b1c18f15d95b694629a7b0620eec77a938600"
);

let response = block_on(verify_message(&mm_bob, "ETH",
"0xcdf11a9c4591fb7334daa4b21494a2590d3f7de41c7d2b333a5b61ca59da9b311b492374cc0ba4fbae53933260fa4b1c18f15d95b694629a7b0620eec77a938600",
"0xbAB36286672fbdc7B250804bf6D14Be0dF69fa29"));
let response = block_on(verify_message(&mm_bob, "ETH",
"0xcdf11a9c4591fb7334daa4b21494a2590d3f7de41c7d2b333a5b61ca59da9b311b492374cc0ba4fbae53933260fa4b1c18f15d95b694629a7b0620eec77a938600",
"0xbAB36286672fbdc7B250804bf6D14Be0dF69fa29"));
let response: RpcV2Response<VerificationResponse> = json::from_value(response).unwrap();
let response = response.result;

Expand Down