Skip to content

test(P2P): add test for peer time sync validation#2304

Merged
onur-ozkan merged 11 commits intodevfrom
peer-time-diff-test
Jan 7, 2025
Merged

test(P2P): add test for peer time sync validation#2304
onur-ozkan merged 11 commits intodevfrom
peer-time-diff-test

Conversation

@dimxy
Copy link
Copy Markdown
Collaborator

@dimxy dimxy commented Dec 25, 2024

While doing review for peer time sync validation #2255 code I wrote a test for that.
(Attn: @onur-ozkan)

@dimxy dimxy changed the title feat(tests) add test for peer time sync validation feat(tests): add test for peer time sync validation Dec 25, 2024
@dimxy dimxy changed the title feat(tests): add test for peer time sync validation test(P2P): add test for peer time sync validation Dec 25, 2024
Comment on lines +1083 to +1094
#[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()
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You can use built-in cfg(test)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

there is run-docker-tests feature too

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

"for-tests" feature is also used already and looks more general for me (not specific for docker tests)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

onur-ozkan
onur-ozkan previously approved these changes Dec 26, 2024
Copy link
Copy Markdown

@borngraced borngraced left a comment

Choose a reason for hiding this comment

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

one note

borngraced
borngraced previously approved these changes Dec 29, 2024
Copy link
Copy Markdown

@borngraced borngraced left a comment

Choose a reason for hiding this comment

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

lgtm 🚀

onur-ozkan
onur-ozkan previously approved these changes Dec 30, 2024
Copy link
Copy Markdown

@onur-ozkan onur-ozkan left a comment

Choose a reason for hiding this comment

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

LGTM other than last nit

Comment on lines +5475 to +5478
#[test]
fn test_peer_time_sync_validation() {
const TIMEOFFSET_TOLERABLE: i64 = 19;
const TIMEOFFSET_TOO_BIG: i64 = 21;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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());

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

fixed in 3c6ac2a

@dimxy dimxy dismissed stale reviews from onur-ozkan and borngraced via 3c6ac2a January 5, 2025 10:16
Copy link
Copy Markdown

@onur-ozkan onur-ozkan left a comment

Choose a reason for hiding this comment

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

LGTM, feel free to merge with/without the nit.

Comment on lines +5479 to +5480
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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: casting to i64 with TryIntos and unwraps seems unnecessary since we are only using this value to convert it into a String.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I intended to have it as i64 to indicate it's possible to emulate time deviation in both directions, if anyone needs

@onur-ozkan onur-ozkan merged commit 1eb4bf7 into dev Jan 7, 2025
@onur-ozkan onur-ozkan deleted the peer-time-diff-test branch January 7, 2025 08:07
dimxy pushed a commit that referenced this pull request Jan 8, 2025
* dev:
  feat(tendermint): validators RPC (#2310)
  chore(CI): validate Cargo lock file (#2309)
  test(P2P): add test for peer time sync validation (#2304)
dimxy pushed a commit to dimxy/komodo-defi-framework that referenced this pull request Jan 17, 2025
* 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)
  ...
shamardy pushed a commit that referenced this pull request Jan 28, 2025
* 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
shamardy pushed a commit that referenced this pull request Jan 30, 2025
* 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
shamardy pushed a commit that referenced this pull request Jan 30, 2025
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants