Skip to content

Commit

Permalink
add docker test for peer time sync validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dimxy committed Dec 25, 2024
1 parent 5bd0d96 commit bdba931
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions mm2src/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ path = "common.rs"
doctest = false

[features]
for-tests = []
track-ctx-pointer = ["shared_ref_counter/enable", "shared_ref_counter/log"]

[dependencies]
Expand Down
10 changes: 10 additions & 0 deletions mm2src/common/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,8 +1080,18 @@ impl<Id> Default for PagingOptionsEnum<Id> {
}

#[inline(always)]
#[cfg(not(feature = "for-tests"))]
pub fn get_utc_timestamp() -> i64 { Utc::now().timestamp() }

/// get_utc_timestamp for tests allowing to add some bias to 'now'
#[cfg(feature = "for-tests")]
pub fn get_utc_timestamp() -> i64 {
Utc::now().timestamp()
+ std::env::var("TEST_TIMESTAMP_OFFSET")
.map(|s| s.as_str().parse::<i64>().unwrap_or_default())
.unwrap_or_default()
}

#[inline(always)]
pub fn get_utc_timestamp_nanos() -> i64 { Utc::now().timestamp_nanos() }

Expand Down
1 change: 1 addition & 0 deletions mm2src/mm2_main/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ winapi = "0.3"
[dev-dependencies]
coins = { path = "../coins", features = ["for-tests"] }
coins_activation = { path = "../coins_activation", features = ["for-tests"] }
common = { path = "../common", features = ["for-tests"] }
mm2_test_helpers = { path = "../mm2_test_helpers" }
trading_api = { path = "../trading_api", features = ["mocktopus"] }
mocktopus = "0.8.0"
Expand Down
101 changes: 101 additions & 0 deletions mm2src/mm2_main/tests/docker_tests/docker_tests_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5471,3 +5471,104 @@ fn test_approve_erc20() {

block_on(mm.stop()).unwrap();
}

#[test]
fn test_peer_time_sync_validation() {
const TIMEOFFSET_TOLERABLE: i64 = 19;
const TIMEOFFSET_TOO_BIG: i64 = 21;

let start_peers_with_time_offset = |offset: i64| -> (Json, Json) {
let (_ctx, _, bob_priv_key) = generate_utxo_coin_with_random_privkey("MYCOIN", 10.into());
let (_ctx, _, alice_priv_key) = generate_utxo_coin_with_random_privkey("MYCOIN1", 10.into());
let coins = json!([mycoin_conf(1000), mycoin1_conf(1000)]);
let mut mm_bob = block_on(MarketMakerIt::start_with_envs(
json!({
"gui": "nogui",
"netid": 9000,
"dht": "on", // Enable DHT without delay.
"passphrase": format!("0x{}", hex::encode(bob_priv_key)),
"coins": coins,
"rpc_password": "pass",
"i_am_seed": true,
}),
"pass".to_string(),
None,
&[],
))
.unwrap();
let (_bob_dump_log, _bob_dump_dashboard) = mm_dump(&mm_bob.log_path);
block_on(mm_bob.wait_for_log(22., |log| log.contains(">>>>>>>>> DEX stats "))).unwrap();

let mut mm_alice = block_on(MarketMakerIt::start_with_envs(
json!({
"gui": "nogui",
"netid": 9000,
"dht": "on", // Enable DHT without delay.
"passphrase": format!("0x{}", hex::encode(alice_priv_key)),
"coins": coins,
"rpc_password": "pass",
"seednodes": vec![format!("{}", mm_bob.ip)],
}),
"pass".to_string(),
None,
&[("TEST_TIMESTAMP_OFFSET", offset.to_string().as_str())],
))
.unwrap();
let (_alice_dump_log, _alice_dump_dashboard) = mm_dump(&mm_alice.log_path);
block_on(mm_alice.wait_for_log(22., |log| log.contains(">>>>>>>>> DEX stats "))).unwrap();

let res_bob = block_on(mm_bob.rpc(&json!({
"userpass": mm_bob.userpass,
"method": "get_directly_connected_peers",
})))
.unwrap();
println!(
"test_peer_time_sync_validation bob get_directly_connected_peers={:?} offset={}",
res_bob, offset
);
assert!(res_bob.0.is_success(), "!get_directly_connected_peers: {}", res_bob.1);
let bob_peers = serde_json::from_str::<Json>(&res_bob.1).unwrap();

let res_alice = block_on(mm_alice.rpc(&json!({
"userpass": mm_alice.userpass,
"method": "get_directly_connected_peers",
})))
.unwrap();
println!(
"test_peer_time_sync_validation alice get_directly_connected_peers={:?} offset={}",
res_alice, offset
);
assert!(
res_alice.0.is_success(),
"!get_directly_connected_peers: {}",
res_alice.1
);
let alice_peers = serde_json::from_str::<Json>(&res_alice.1).unwrap();

block_on(mm_bob.stop()).unwrap();
block_on(mm_alice.stop()).unwrap();
(bob_peers, alice_peers)
};

// check with small time offset:
let (bob_peers, alice_peers) = start_peers_with_time_offset(TIMEOFFSET_TOLERABLE);
assert!(
bob_peers["result"].as_object().unwrap().len() == 1,
"bob must have one peer"
);
assert!(
alice_peers["result"].as_object().unwrap().len() == 1,
"alice must have one peer"
);

// check with too big time offset:
let (bob_peers, alice_peers) = start_peers_with_time_offset(TIMEOFFSET_TOO_BIG);
assert!(
bob_peers["result"].as_object().unwrap().is_empty(),
"bob must have no peers"
);
assert!(
alice_peers["result"].as_object().unwrap().is_empty(),
"alice must have no peers"
);
}
1 change: 1 addition & 0 deletions mm2src/mm2_p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ timed-map = { version = "1.1.1", features = ["rustc-hash"] }
[dev-dependencies]
async-std = "1.6.2"
env_logger = "0.9.3"
common = { path = "../common", features = ["for-tests"] }

0 comments on commit bdba931

Please sign in to comment.