test(P2P): add test for peer time sync validation#2304
Conversation
mm2src/common/common.rs
Outdated
| #[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() | ||
| } | ||
|
|
There was a problem hiding this comment.
I need to call it for other crates and I believe cfg(test) is in effect only for unit tests in same crate
(checked btw - test does not work with cfg(test))
There was a problem hiding this comment.
there is run-docker-tests feature too
There was a problem hiding this comment.
"for-tests" feature is also used already and looks more general for me (not specific for docker tests)
There was a problem hiding this comment.
As far as I see you just added for-tests feature, idk how it's already there but it's fine, I will make cfg(test) working and replace all these test flags in another PR.
| #[test] | ||
| fn test_peer_time_sync_validation() { | ||
| const TIMEOFFSET_TOLERABLE: i64 = 19; | ||
| const TIMEOFFSET_TOO_BIG: i64 = 21; |
There was a problem hiding this comment.
We can stabilize this logic a bit more here:
diff --git a/mm2src/mm2_main/tests/docker_tests/docker_tests_inner.rs b/mm2src/mm2_main/tests/docker_tests/docker_tests_inner.rs
index acf60ee55..e2dcd228f 100644
--- a/mm2src/mm2_main/tests/docker_tests/docker_tests_inner.rs
+++ b/mm2src/mm2_main/tests/docker_tests/docker_tests_inner.rs
@@ -16,6 +16,7 @@ use common::{block_on, block_on_f01, executor::Timer, get_utc_timestamp, now_sec
use crypto::privkey::key_pair_from_seed;
use crypto::{CryptoCtx, DerivationPath, KeyPairPolicy};
use http::StatusCode;
+use mm2_libp2p::behaviours::atomicdex::MAX_TIME_GAP_FOR_CONNECTED_PEER;
use mm2_number::{BigDecimal, BigRational, MmNumber};
use mm2_test_helpers::for_tests::{check_my_swap_status_amounts, disable_coin, disable_coin_err, enable_eth_coin,
enable_eth_with_tokens_v2, erc20_dev_conf, eth_dev_conf, get_locked_amount,
@@ -5474,8 +5475,8 @@ fn test_approve_erc20() {
#[test]
fn test_peer_time_sync_validation() {
- const TIMEOFFSET_TOLERABLE: i64 = 19;
- const TIMEOFFSET_TOO_BIG: i64 = 21;
+ const TIMEOFFSET_TOLERABLE: i64 = MAX_TIME_GAP_FOR_CONNECTED_PEER - 1;
+ const TIMEOFFSET_TOO_BIG: i64 = MAX_TIME_GAP_FOR_CONNECTED_PEER + 1;
let start_peers_with_time_offset = |offset: i64| -> (Json, Json) {
let (_ctx, _, bob_priv_key) = generate_utxo_coin_with_random_privkey("MYCOIN", 10.into());
onur-ozkan
left a comment
There was a problem hiding this comment.
LGTM, feel free to merge with/without the nit.
| let timeoffset_tolerable = TryInto::<i64>::try_into(MAX_TIME_GAP_FOR_CONNECTED_PEER).unwrap() - 1; | ||
| let timeoffset_too_big = TryInto::<i64>::try_into(MAX_TIME_GAP_FOR_CONNECTED_PEER).unwrap() + 1; |
There was a problem hiding this comment.
nit: casting to i64 with TryIntos and unwraps seems unnecessary since we are only using this value to convert it into a String.
There was a problem hiding this comment.
I intended to have it as i64 to indicate it's possible to emulate time deviation in both directions, if anyone needs
* dev: (35 commits) fix(crypto): allow non bip39 mnemonics storage (GLEECBTC#2312) fix(legacy_swap): check for existing maker/taker payment before timeout (GLEECBTC#2283) feat(tendermint): validators RPC (GLEECBTC#2310) chore(CI): validate Cargo lock file (GLEECBTC#2309) test(P2P): add test for peer time sync validation (GLEECBTC#2304) fix mm2_p2p dev build (GLEECBTC#2311) update Cargo.lock (GLEECBTC#2308) chore(CI): unlock wasm-pack version (GLEECBTC#2307) add `wasm` feature on WASM for timed-map (GLEECBTC#2306) replace broken rpc link (GLEECBTC#2305) chore(eth-websocket): remove some unnecessary wrappers (GLEECBTC#2291) improvement(CI): switch to proper rust caching (GLEECBTC#2303) fix(wasm): add test-ext-api feature to mm2_main and mm2_bin_lib tomls (GLEECBTC#2295) chore(ci): Update docker build for wasm (GLEECBTC#2294) chore(p2p): follow-up nits (GLEECBTC#2302) feat(p2p): ensure time synchronization in the network (GLEECBTC#2255) bump libp2p (GLEECBTC#2296) chore(adex-cli): use "Komodo DeFi Framework" name in adex_cli (GLEECBTC#2290) chore(ctx): replace gstuff constructible with oncelock (GLEECBTC#2267) don't rely on core (GLEECBTC#2289) ...
* fix peer response error log (only if error) * add test for peer time difference validation * fix linux clippy * Revert "fix linux clippy" This reverts commit 2bfe34c. * Revert "add test for peer time difference validation" This reverts commit c8d1165. * Revert "fix peer response error log (only if error)" This reverts commit b6d6fbc. * add docker test for peer time sync validation * remove unneeded test println * use Mm2TestConf::seednode * review(borngraced): refactor get_utc_timestamp with test code * use const in peer time sync test
* fix peer response error log (only if error) * add test for peer time difference validation * fix linux clippy * Revert "fix linux clippy" This reverts commit 2bfe34c. * Revert "add test for peer time difference validation" This reverts commit c8d1165. * Revert "fix peer response error log (only if error)" This reverts commit b6d6fbc. * add docker test for peer time sync validation * remove unneeded test println * use Mm2TestConf::seednode * review(borngraced): refactor get_utc_timestamp with test code * use const in peer time sync test
* fix peer response error log (only if error) * add test for peer time difference validation * fix linux clippy * Revert "fix linux clippy" This reverts commit 2bfe34c. * Revert "add test for peer time difference validation" This reverts commit c8d1165. * Revert "fix peer response error log (only if error)" This reverts commit b6d6fbc. * add docker test for peer time sync validation * remove unneeded test println * use Mm2TestConf::seednode * review(borngraced): refactor get_utc_timestamp with test code * use const in peer time sync test
While doing review for peer time sync validation #2255 code I wrote a test for that.
(Attn: @onur-ozkan)