forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
z_coin wasm tx_history streaming test
ignoring the non-wasm test for now since it needs zcash params downloaded which we don't have an auto way to do. would be better if we can download them in the same way we do for wasm (right from kdf).
- Loading branch information
1 parent
c29cf81
commit 6a700f7
Showing
8 changed files
with
214 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#[cfg(not(target_arch = "wasm32"))] mod native; | ||
#[cfg(target_arch = "wasm32")] mod wasm; | ||
|
||
use common::now_sec; | ||
use mm2_test_helpers::for_tests::{PIRATE_ELECTRUMS, PIRATE_LIGHTWALLETD_URLS}; | ||
|
||
use crate::utxo::rpc_clients::ElectrumConnectionSettings; | ||
use crate::z_coin::{ZcoinActivationParams, ZcoinRpcMode}; | ||
|
||
fn light_zcoin_activation_params() -> ZcoinActivationParams { | ||
ZcoinActivationParams { | ||
mode: ZcoinRpcMode::Light { | ||
electrum_servers: PIRATE_ELECTRUMS | ||
.iter() | ||
.map(|s| ElectrumConnectionSettings { | ||
url: s.to_string(), | ||
protocol: Default::default(), | ||
disable_cert_verification: Default::default(), | ||
timeout_sec: None, | ||
}) | ||
.collect(), | ||
min_connected: None, | ||
max_connected: None, | ||
light_wallet_d_servers: PIRATE_LIGHTWALLETD_URLS.iter().map(|s| s.to_string()).collect(), | ||
sync_params: Some(crate::z_coin::SyncStartPoint::Date(now_sec() - 24 * 60 * 60)), | ||
skip_sync_params: None, | ||
}, | ||
..Default::default() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use common::custom_futures::timeout::FutureTimerExt; | ||
use common::{block_on, Future01CompatExt}; | ||
use mm2_core::mm_ctx::MmCtxBuilder; | ||
use mm2_test_helpers::for_tests::{pirate_conf, ARRR}; | ||
use std::time::Duration; | ||
|
||
use super::light_zcoin_activation_params; | ||
use crate::z_coin::tx_history_events::ZCoinTxHistoryEventStreamer; | ||
use crate::z_coin::z_coin_from_conf_and_params; | ||
use crate::z_coin::z_htlc::z_send_dex_fee; | ||
use crate::{CoinProtocol, MarketCoinOps, MmCoin, PrivKeyBuildPolicy}; | ||
|
||
#[test] | ||
#[ignore] // Ignored because we don't have zcash params in CI. TODO: Why not download them on demand like how we do in wasm (see download_and_save_params). | ||
fn test_zcoin_tx_streaming() { | ||
let ctx = MmCtxBuilder::default().into_mm_arc(); | ||
let conf = pirate_conf(); | ||
let params = light_zcoin_activation_params(); | ||
// Address: RQX5MnqnxEk6P33LSEAxC2vqA7DfSdWVyH | ||
// Or: zs1n2azlwcj9pvl2eh36qvzgeukt2cpzmw44hya8wyu52j663d0dfs4d5hjx6tr04trz34jxyy433j | ||
let priv_key_policy = | ||
PrivKeyBuildPolicy::IguanaPrivKey("6d862798ef956fb60fb17bcc417dd6d44bfff066a4a49301cd2528e41a4a3e45".into()); | ||
let protocol_info = match serde_json::from_value::<CoinProtocol>(conf["protocol"].clone()).unwrap() { | ||
CoinProtocol::ZHTLC(protocol_info) => protocol_info, | ||
other_protocol => panic!("Failed to get protocol from config: {:?}", other_protocol), | ||
}; | ||
|
||
let coin = block_on(z_coin_from_conf_and_params( | ||
&ctx, | ||
ARRR, | ||
&conf, | ||
¶ms, | ||
protocol_info, | ||
priv_key_policy, | ||
)) | ||
.unwrap(); | ||
|
||
// Wait till we are synced with the sapling state. | ||
while !block_on(coin.is_sapling_state_synced()) { | ||
std::thread::sleep(Duration::from_secs(1)); | ||
} | ||
|
||
// Query the block height to make sure our electrums are actually connected. | ||
log!("current block = {:?}", block_on(coin.current_block().compat()).unwrap()); | ||
|
||
// Add a new client to use it for listening to tx history events. | ||
let client_id = 1; | ||
let mut event_receiver = ctx.event_stream_manager.new_client(client_id).unwrap(); | ||
// Add the streamer that will stream the tx history events. | ||
let streamer = ZCoinTxHistoryEventStreamer::new(coin.clone()); | ||
// Subscribe the client to the streamer. | ||
block_on(ctx.event_stream_manager.add(client_id, streamer, coin.spawner())).unwrap(); | ||
|
||
// Send a tx to have it in the tx history. | ||
let tx = block_on(z_send_dex_fee(&coin, "0.0001".parse().unwrap(), &[1; 16])).unwrap(); | ||
|
||
// Wait for the tx history event (should be streamed next block). | ||
let event = block_on(Box::pin(event_receiver.recv()).timeout_secs(120.)) | ||
.expect("timed out waiting for tx to showup") | ||
.expect("tx history sender shutdown"); | ||
|
||
log!("{:?}", event.get()); | ||
let (event_type, event_data) = event.get(); | ||
// Make sure this is not an error event, | ||
assert!(!event_type.starts_with("ERROR_")); | ||
// from the expected streamer, | ||
assert_eq!( | ||
event_type, | ||
ZCoinTxHistoryEventStreamer::derive_streamer_id(coin.ticker()) | ||
); | ||
// and has the expected data. | ||
assert_eq!(event_data["tx_hash"].as_str().unwrap(), tx.txid().to_string()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use common::custom_futures::timeout::FutureTimerExt; | ||
use common::{executor::Timer, Future01CompatExt}; | ||
use mm2_core::mm_ctx::MmCtxBuilder; | ||
use mm2_test_helpers::for_tests::{pirate_conf, ARRR}; | ||
use wasm_bindgen_test::*; | ||
|
||
use super::light_zcoin_activation_params; | ||
use crate::z_coin::tx_history_events::ZCoinTxHistoryEventStreamer; | ||
use crate::z_coin::z_coin_from_conf_and_params; | ||
use crate::z_coin::z_htlc::z_send_dex_fee; | ||
use crate::PrivKeyBuildPolicy; | ||
use crate::{CoinProtocol, MarketCoinOps, MmCoin}; | ||
|
||
#[wasm_bindgen_test] | ||
async fn test_zcoin_tx_streaming() { | ||
let ctx = MmCtxBuilder::default().into_mm_arc(); | ||
let conf = pirate_conf(); | ||
let params = light_zcoin_activation_params(); | ||
// Address: RQX5MnqnxEk6P33LSEAxC2vqA7DfSdWVyH | ||
// Or: zs1n2azlwcj9pvl2eh36qvzgeukt2cpzmw44hya8wyu52j663d0dfs4d5hjx6tr04trz34jxyy433j | ||
let priv_key_policy = | ||
PrivKeyBuildPolicy::IguanaPrivKey("6d862798ef956fb60fb17bcc417dd6d44bfff066a4a49301cd2528e41a4a3e45".into()); | ||
let protocol_info = match serde_json::from_value::<CoinProtocol>(conf["protocol"].clone()).unwrap() { | ||
CoinProtocol::ZHTLC(protocol_info) => protocol_info, | ||
other_protocol => panic!("Failed to get protocol from config: {:?}", other_protocol), | ||
}; | ||
|
||
let coin = z_coin_from_conf_and_params(&ctx, ARRR, &conf, ¶ms, protocol_info, priv_key_policy) | ||
.await | ||
.unwrap(); | ||
|
||
// Wait till we are synced with the sapling state. | ||
while !coin.is_sapling_state_synced().await { | ||
Timer::sleep(1.).await; | ||
} | ||
|
||
// Query the block height to make sure our electrums are actually connected. | ||
log!("current block = {:?}", coin.current_block().compat().await.unwrap()); | ||
|
||
// Add a new client to use it for listening to tx history events. | ||
let client_id = 1; | ||
let mut event_receiver = ctx.event_stream_manager.new_client(client_id).unwrap(); | ||
// Add the streamer that will stream the tx history events. | ||
let streamer = ZCoinTxHistoryEventStreamer::new(coin.clone()); | ||
// Subscribe the client to the streamer. | ||
ctx.event_stream_manager | ||
.add(client_id, streamer, coin.spawner()) | ||
.await | ||
.unwrap(); | ||
|
||
// Send a tx to have it in the tx history. | ||
let tx = z_send_dex_fee(&coin, "0.0001".parse().unwrap(), &[1; 16]) | ||
.await | ||
.unwrap(); | ||
|
||
// Wait for the tx history event (should be streamed next block). | ||
let event = Box::pin(event_receiver.recv()) | ||
.timeout_secs(120.) | ||
.await | ||
.expect("timed out waiting for tx to showup") | ||
.expect("tx history sender shutdown"); | ||
|
||
log!("{:?}", event.get()); | ||
let (event_type, event_data) = event.get(); | ||
// Make sure this is not an error event, | ||
assert!(!event_type.starts_with("ERROR_")); | ||
// from the expected streamer, | ||
assert_eq!( | ||
event_type, | ||
ZCoinTxHistoryEventStreamer::derive_streamer_id(coin.ticker()) | ||
); | ||
// and has the expected data. | ||
assert_eq!(event_data["tx_hash"].as_str().unwrap(), tx.txid().to_string()); | ||
} |
Oops, something went wrong.