forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 117
test(P2P): add test for peer time sync validation #2304
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b6d6fbc
fix peer response error log (only if error)
c8d1165
add test for peer time difference validation
2bfe34c
fix linux clippy
cfa6586
Revert "fix linux clippy"
ef71c7d
Revert "add test for peer time difference validation"
5bd0d96
Revert "fix peer response error log (only if error)"
bdba931
add docker test for peer time sync validation
e735d26
remove unneeded test println
5be3e5c
use Mm2TestConf::seednode
6eaa3cf
review(borngraced): refactor get_utc_timestamp with test code
3c6ac2a
use const in peer time sync test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -5471,3 +5471,96 @@ 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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());
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in 3c6ac2a |
||
|
|
||
| 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)], | ||
| }), | ||
|
onur-ozkan marked this conversation as resolved.
Outdated
|
||
| "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(); | ||
| 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(); | ||
| 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" | ||
| ); | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)There was a problem hiding this comment.
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))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is
run-docker-testsfeature tooThere was a problem hiding this comment.
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)
There was a problem hiding this comment.
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-testsfeature, idk how it's already there but it's fine, I will makecfg(test)working and replace all these test flags in another PR.