Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 11 commits into from
Jan 7, 2025
Merged

Conversation

dimxy
Copy link
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
Member

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

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

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
Member

@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

mm2src/common/common.rs Outdated Show resolved Hide resolved
borngraced
borngraced previously approved these changes Dec 29, 2024
Copy link
Member

@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
Member

@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
Member

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

@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
Member

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
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
15 of 23 checks passed
@onur-ozkan onur-ozkan deleted the peer-time-diff-test branch January 7, 2025 08:07
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