Skip to content

Commit

Permalink
Use separate sql statement to avoid stats_swap migration_2 failures.
Browse files Browse the repository at this point in the history
  • Loading branch information
artemii235 committed May 5, 2021
1 parent ff5c305 commit 419f36e
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 4 deletions.
79 changes: 77 additions & 2 deletions mm2src/database/stats_swaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ const CREATE_STATS_SWAPS_TABLE: &str = "CREATE TABLE IF NOT EXISTS stats_swaps (
is_success INTEGER NOT NULL
);";

const INSERT_STATS_SWAP_ON_INIT: &str = "INSERT INTO stats_swaps (
maker_coin,
taker_coin,
uuid,
started_at,
finished_at,
maker_amount,
taker_amount,
is_success
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)";

const INSERT_STATS_SWAP: &str = "INSERT INTO stats_swaps (
maker_coin,
maker_coin_ticker,
Expand Down Expand Up @@ -72,7 +83,7 @@ pub fn create_and_fill_stats_swaps_from_json_statements(ctx: &MmArc) -> Vec<(&'s
let content = slurp(&file).expect("slurp should not fail at this point");
match json::from_slice(&content) {
Ok(swap) => {
if let Some(sql_with_params) = insert_stats_maker_swap_sql(&swap) {
if let Some(sql_with_params) = insert_stats_maker_swap_sql_init(&swap) {
inserted_maker_uuids.insert(swap.uuid);
result.push(sql_with_params);
}
Expand Down Expand Up @@ -118,7 +129,7 @@ pub fn create_and_fill_stats_swaps_from_json_statements(ctx: &MmArc) -> Vec<(&'s
let content = slurp(&file).expect("slurp should not fail at this point");
match json::from_slice(&content) {
Ok(swap) => {
if let Some(sql_with_params) = insert_stats_taker_swap_sql(&swap) {
if let Some(sql_with_params) = insert_stats_taker_swap_sql_init(&swap) {
result.push(sql_with_params);
}
},
Expand Down Expand Up @@ -179,6 +190,38 @@ fn insert_stats_maker_swap_sql(swap: &MakerSavedSwap) -> Option<(&'static str, V
Some((INSERT_STATS_SWAP, params))
}

fn insert_stats_maker_swap_sql_init(swap: &MakerSavedSwap) -> Option<(&'static str, Vec<String>)> {
let swap_data = match swap.swap_data() {
Ok(d) => d,
Err(e) => {
error!("Error {} on getting swap {} data", e, swap.uuid);
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 is_success = swap
.is_success()
.expect("is_success can return error only when swap is not finished");

let params = vec![
swap_data.maker_coin.clone(),
swap_data.taker_coin.clone(),
swap.uuid.to_string(),
swap_data.started_at.to_string(),
finished_at,
swap_data.maker_amount.to_string(),
swap_data.taker_amount.to_string(),
(is_success as u32).to_string(),
];
Some((INSERT_STATS_SWAP_ON_INIT, params))
}

fn insert_stats_taker_swap_sql(swap: &TakerSavedSwap) -> Option<(&'static str, Vec<String>)> {
let swap_data = match swap.swap_data() {
Ok(d) => d,
Expand Down Expand Up @@ -218,6 +261,38 @@ fn insert_stats_taker_swap_sql(swap: &TakerSavedSwap) -> Option<(&'static str, V
Some((INSERT_STATS_SWAP, params))
}

fn insert_stats_taker_swap_sql_init(swap: &TakerSavedSwap) -> Option<(&'static str, Vec<String>)> {
let swap_data = match swap.swap_data() {
Ok(d) => d,
Err(e) => {
error!("Error {} on getting swap {} data", e, swap.uuid);
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 is_success = swap
.is_success()
.expect("is_success can return error only when swap is not finished");

let params = vec![
swap_data.maker_coin.clone(),
swap_data.taker_coin.clone(),
swap.uuid.to_string(),
swap_data.started_at.to_string(),
finished_at,
swap_data.maker_amount.to_string(),
swap_data.taker_amount.to_string(),
(is_success as u32).to_string(),
];
Some((INSERT_STATS_SWAP_ON_INIT, params))
}

pub fn add_swap_to_index(conn: &Connection, swap: &SavedSwap) {
let params = vec![swap.uuid().to_string()];
let query_row = conn.query_row(SELECT_ID_BY_UUID, &params, |row| row.get::<_, i64>(0));
Expand Down
48 changes: 46 additions & 2 deletions mm2src/mm2_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use bigdecimal::BigDecimal;
use common::executor::Timer;
use common::for_tests::{check_my_swap_status, check_recent_swaps, check_stats_swap_status,
enable_electrum as enable_electrum_impl, enable_native as enable_native_impl, enable_qrc20,
find_metrics_in_json, from_env_file, get_passphrase, mm_spat, LocalStart, MarketMakerIt,
RaiiDump, MAKER_ERROR_EVENTS, MAKER_SUCCESS_EVENTS, TAKER_ERROR_EVENTS, TAKER_SUCCESS_EVENTS};
find_metrics_in_json, from_env_file, get_passphrase, mm_spat, new_mm2_temp_folder_path,
LocalStart, MarketMakerIt, RaiiDump, MAKER_ERROR_EVENTS, MAKER_SUCCESS_EVENTS,
TAKER_ERROR_EVENTS, TAKER_SUCCESS_EVENTS};
use common::mm_metrics::{MetricType, MetricsJson};
use common::mm_number::{Fraction, MmNumber};
use common::privkey::key_pair_from_seed;
Expand Down Expand Up @@ -6098,6 +6099,49 @@ fn test_orderbook_depth() {
block_on(mm_alice.stop()).unwrap();
}

// https://github.com/KomodoPlatform/atomicDEX-API/issues/932
#[test]
fn test_mm2_db_migration() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();

let coins = json! ([
{"coin":"RICK","asset":"RICK","required_confirmations":0,"txversion":4,"overwintered":1,"protocol":{"type":"UTXO"}},
{"coin":"MORTY","asset":"MORTY","required_confirmations":0,"txversion":4,"overwintered":1,"protocol":{"type":"UTXO"}},
{"coin":"ETH","name":"ethereum","protocol":{"type":"ETH"}},
{"coin":"JST","name":"jst","protocol":{"type":"ERC20","protocol_data":{"platform":"ETH","contract_address":"0x2b294F029Fde858b2c62184e8390591755521d8E"}}}
]);

let mm2_folder = new_mm2_temp_folder_path(None);
let swaps_dir = mm2_folder.join(format!(
"{}/SWAPS/STATS/MAKER",
hex::encode(rmd160_from_passphrase(&bob_passphrase))
));
std::fs::create_dir_all(&swaps_dir).unwrap();
let swap_path = swaps_dir.join("5d02843e-d1b4-488d-aad0-114d82020453.json");
let swap_json = r#"{"uuid":"5d02843e-d1b4-488d-aad0-114d82020453","events":[{"timestamp":1612780908136,"event":{"type":"Started","data":{"taker_coin":"MORTY-BEP20","maker_coin":"RICK-BEP20","taker":"ad6f89abc2e5beaa8a3ac28e22170659b3209fe2ddf439681b4b8f31508c36fa","secret":"0000000000000000000000000000000000000000000000000000000000000000","secret_hash":"026bebc2e19c243d0940dd583c9573bf10377afd","my_persistent_pub":"037310a8fb9fd8f198a1a21db830252ad681fccda580ed4101f3f6bfb98b34fab5","lock_duration":7800,"maker_amount":"1","taker_amount":"1","maker_payment_confirmations":1,"maker_payment_requires_nota":false,"taker_payment_confirmations":1,"taker_payment_requires_nota":false,"maker_payment_lock":1612796508,"uuid":"5d02843e-d1b4-488d-aad0-114d82020453","started_at":1612780908,"maker_coin_start_block":793472,"taker_coin_start_block":797356,"maker_payment_trade_fee":null,"taker_payment_spend_trade_fee":null}}},{"timestamp":1612780924142,"event":{"type":"Negotiated","data":{"taker_payment_locktime":1612788708,"taker_pubkey":"03ad6f89abc2e5beaa8a3ac28e22170659b3209fe2ddf439681b4b8f31508c36fa"}}},{"timestamp":1612780935156,"event":{"type":"TakerFeeValidated","data":{"tx_hex":"0400008085202f8901f425fbefe21f33ccb7b487df251191b27dfa7b639b04f60e5493c7ea41dbf149000000006b483045022100d5ec3e542175479bd4bd011e19b76a75e99f19cc49867e5bca9541950322c33a02207a4d1ffd674fb9760de79bb4929af44d66344b5e182de3c377186deebf6bf376012103ad6f89abc2e5beaa8a3ac28e22170659b3209fe2ddf439681b4b8f31508c36faffffffff02bcf60100000000001976a914ca1e04745e8ca0c60d8c5881531d51bec470743f88ac5ce6f305000000001976a914d55f0df6cb82630ad21a4e6049522a6f2b6c9d4588ac7c152160000000000000000000000000000000","tx_hash":"75323ab7acd64bd35242611fabaec560d9acf2e1f9ca28d3a4aba47a79fb49c4"}}},{"timestamp":1612780935174,"event":{"type":"MakerPaymentSent","data":{"tx_hex":"0400008085202f89028bef955e42107c562e4e02421f25c455723a701573f86c17b4d82e35a7d8f9f7020000006b483045022100b12fc9d95acca76bf5fd8d5c6acc288b454032ba4561b1c2b1f5f33b2cf2926d022017e561bc2cd93308848674b47b2e8ebd8f074ea78e32454d5fea6f08c0b1f1e40121037310a8fb9fd8f198a1a21db830252ad681fccda580ed4101f3f6bfb98b34fab5ffffffff5dfd0b24c0f7c3cf235868cf9a26ec49574764d135796fc4e7d20e95d55a8653000000006a47304402207c752d14601d1c99892f9d6c88c8ff2f93211640a65b2ee69172a16b908b21e402206f0b66684158445888271a849ab46258ad722496ee64fde055a6f44e36ed2ccc0121037310a8fb9fd8f198a1a21db830252ad681fccda580ed4101f3f6bfb98b34fab5ffffffff0300e1f5050000000017a9141b85c1a277f44f7d77d52b78e2ba70a0becc2ff9870000000000000000166a14026bebc2e19c243d0940dd583c9573bf10377afda7d26301000000001976a91486f747b28c60ad1130bdd3f84f48eeaf1801ca9888ac87152160000000000000000000000000000000","tx_hash":"27dafe553246553d54f909fbbded80e6d490fdb95ca7b6807d73eca45f0d7a22"}}},{"timestamp":1612780982221,"event":{"type":"TakerPaymentReceived","data":{"tx_hex":"0400008085202f8902c449fb797aa4aba4d328caf9e1f2acd960c5aeab1f614252d34bd6acb73a3275010000006a47304402200438c96bf457bacf906e94c98f91783129cb1c3a8f3d9355e1c39a9857fb2c6b02201d3c71b3f243f7a3c91bb9a15e80bb26e47bed04e798106a8af8dac61082ec41012103ad6f89abc2e5beaa8a3ac28e22170659b3209fe2ddf439681b4b8f31508c36fafffffffff425fbefe21f33ccb7b487df251191b27dfa7b639b04f60e5493c7ea41dbf149010000006b483045022100efa00c742159b0b05433678aa95f0c8900adaddf5011bfaf56d6a7679aed428b022043f68efc3cb386dd10a65a2a3e8a904541c8f1ddbd7dddbcda2ccdd7938c5934012103ad6f89abc2e5beaa8a3ac28e22170659b3209fe2ddf439681b4b8f31508c36faffffffff0300e1f5050000000017a914bc8e8f2648f7bb4dbd612f2e71dd7b23c54880b7870000000000000000166a14026bebc2e19c243d0940dd583c9573bf10377afd74c3e90b000000001976a914d55f0df6cb82630ad21a4e6049522a6f2b6c9d4588acb5152160000000000000000000000000000000","tx_hash":"94c8a1244421465b618a36e7647a270c7b2ef20eff3cd1317761cc242c49cc99"}}},{"timestamp":1612780982222,"event":{"type":"TakerPaymentWaitConfirmStarted"}},{"timestamp":1612781042265,"event":{"type":"TakerPaymentValidatedAndConfirmed"}},{"timestamp":1612781042272,"event":{"type":"TakerPaymentSpent","data":{"tx_hex":"0400008085202f890199cc492c24cc617731d13cff0ef22e7b0c277a64e7368a615b46214424a1c89400000000d84830450221008f38d29e7990bd694f2c4fd4c235fe00997da4e5133208d7c38e75e806d9be1702201ff1d598ceafc099dc4af7d4b91db535196f642cf31b5b5e386b28574a378b9b0120e8512e2afb02d3a90590d30095286e2293f51f9d4411ad87ef398ee8f566de43004c6b6304e4332160b1752103ad6f89abc2e5beaa8a3ac28e22170659b3209fe2ddf439681b4b8f31508c36faac6782012088a914026bebc2e19c243d0940dd583c9573bf10377afd8821037310a8fb9fd8f198a1a21db830252ad681fccda580ed4101f3f6bfb98b34fab5ac68ffffffff0118ddf505000000001976a91486f747b28c60ad1130bdd3f84f48eeaf1801ca9888ace2072160000000000000000000000000000000","tx_hash":"d21173cca32b83ffe5d4cc327f7eff09496f52876614dbbfe7963284818ba9a1"}}},{"timestamp":1612781042273,"event":{"type":"TakerPaymentSpendConfirmStarted"}},{"timestamp":1612781207356,"event":{"type":"TakerPaymentSpendConfirmed"}},{"timestamp":1612781207357,"event":{"type":"Finished"}}],"maker_amount":"1","maker_coin":"RICK-BEP20","taker_amount":"1","taker_coin":"MORTY-BEP20","gui":"dexstats","mm_version":"19701cc87","success_events":["Started","Negotiated","TakerFeeValidated","MakerPaymentSent","TakerPaymentReceived","TakerPaymentWaitConfirmStarted","TakerPaymentValidatedAndConfirmed","TakerPaymentSpent","TakerPaymentSpendConfirmStarted","TakerPaymentSpendConfirmed","Finished"],"error_events":["StartFailed","NegotiateFailed","TakerFeeValidateFailed","MakerPaymentTransactionFailed","MakerPaymentDataSendFailed","MakerPaymentWaitConfirmFailed","TakerPaymentValidateFailed","TakerPaymentWaitConfirmFailed","TakerPaymentSpendFailed","TakerPaymentSpendConfirmFailed","MakerPaymentWaitRefundStarted","MakerPaymentRefunded","MakerPaymentRefundFailed"]}"#;
std::fs::write(swap_path, swap_json.as_bytes()).unwrap();

// if there is an issue with migration the start will fail
MarketMakerIt::start(
json! ({
"gui": "nogui",
"netid": 8999,
"dht": "on", // Enable DHT without delay.
"myipaddr": env::var ("BOB_TRADE_IP") .ok(),
"rpcip": env::var ("BOB_TRADE_IP") .ok(),
"canbind": env::var ("BOB_TRADE_PORT") .ok().map (|s| s.parse::<i64>().unwrap()),
"passphrase": bob_passphrase,
"coins": coins,
"rpc_password": "password",
"i_am_seed": true,
"dbdir": mm2_folder.display().to_string(),
}),
"password".into(),
None,
)
.unwrap();
}

// HOWTO
// 1. Install Firefox.
// 2. Install wasm-bindgen-cli: cargo install wasm-bindgen-cli
Expand Down

0 comments on commit 419f36e

Please sign in to comment.