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

refactor: better network global #6014

Conversation

stringhandler
Copy link
Collaborator

Description

Removes the mutex and allows tests to run in parallel

Motivation and Context

Built on #6004

The previous versions of these test relied on setting the Network globally. If other tests are running at the same time, these tests can fail because the network is switched. I investigated using ThreadLocalStorage and TaskLocalStorage from tokio, but these could result threads having different networks set and it would be hard to confirm that the entire process was using the correct network.

I opted instead to ignore the set_network test and add a parameter to the DomainSeparatedHasher to allow a different network to be tested.

How Has This Been Tested?

Ran cargo test

What process can a PR reviewer use to test or verify this change?

This fixes tests and provides a cleaner interface

Breaking Changes

  • None
  • Requires data directory on base node to be deleted
  • Requires hard fork
  • Other - Please specify

Copy link

github-actions bot commented Dec 5, 2023

Test Results (CI)

1 261 tests   1 261 ✔️  11m 10s ⏱️
     39 suites         0 💤
       1 files           0

Results for commit a8d3dda.

♻️ This comment has been updated with latest results.

@ghpbot-tari-project ghpbot-tari-project added P-acks_required Process - Requires more ACKs or utACKs P-reviews_required Process - Requires a review from a lead maintainer to be merged labels Dec 5, 2023
Copy link

github-actions bot commented Dec 5, 2023

Test Results (Integration tests)

  2 files  +  2  12 suites  +12   1h 23m 51s ⏱️ + 1h 23m 51s
32 tests +32  27 ✔️ +27  0 💤 ±0    5 +  5 
42 runs  +42  27 ✔️ +27  0 💤 ±0  15 +15 

For more details on these failures, see this check.

Results for commit a8d3dda. ± Comparison against base commit dee1892.

♻️ This comment has been updated with latest results.

SWvheerden
SWvheerden previously approved these changes Dec 5, 2023
Copy link
Collaborator

@SWvheerden SWvheerden left a comment

Choose a reason for hiding this comment

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

this looks like it could solve the test problem

@ghpbot-tari-project ghpbot-tari-project removed the P-reviews_required Process - Requires a review from a lead maintainer to be merged label Dec 5, 2023
@ghpbot-tari-project ghpbot-tari-project added the P-reviews_required Process - Requires a review from a lead maintainer to be merged label Dec 5, 2023
Copy link
Contributor

@brianp brianp left a comment

Choose a reason for hiding this comment

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

LGTM

@ghpbot-tari-project ghpbot-tari-project removed the P-reviews_required Process - Requires a review from a lead maintainer to be merged label Dec 5, 2023
let mut current_network = CURRENT_NETWORK.lock()?;
*current_network = network;
Ok(())
// let mut current_network = Network::current();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// let mut current_network = Network::current();

Comment on lines +160 to +161
// assert_eq!(Network::current(), Network::Esmeralda);

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// assert_eq!(Network::current(), Network::Esmeralda);

// assert_eq!(Network::current(), Network::Esmeralda);

// Generate a mainnet hash
// Network::set_current(Network::MainNet);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// Network::set_current(Network::MainNet);


// They should be distinct
assert_ne!(hash_mainnet, hash_stagenet);

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change


// Valid networks (we can't test against other binary configurations)
for network in [Network::LocalNet, Network::Igor, Network::Esmeralda] {
assert!(set_network_if_choice_valid(network).is_ok());
Copy link
Collaborator

@AaronFeickert AaronFeickert Dec 5, 2023

Choose a reason for hiding this comment

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

This fails for me locally on the first trip through the loop. The issue is that Network::set_current returns an error once the value has been initialized. I don't think this behavior is correct.

SWvheerden pushed a commit that referenced this pull request Dec 8, 2023
Description
---
Refactors the handling of network-based hashing operations introduced in
#5980 to better handle read operations. Adds a sanity test for hash
independence.

Supersedes #6014.

Closes #6003.

Motivation and Context
---
Recent work in #5980 binds the current network into consensus hashing.
It uses a `Mutex`, which has two subtle issues. First, it allows the
network to be set multiple times, which should not occur. Second, it
locks on reads, which is unnecessary and inefficient.

This PR updates how the current network is handled. It adds
`Network::get_current_or_default` that will return either the current
network (if it has been set) or the default network (if it has not). It
adds `Network:set_current` that attempts to set the network; if it has
been set before, this operation will fail. Note that calling
`Network::get_current_or_default` does _not_ set the network for this
purpose.

It modifies the API for consensus hashing to add a wrapper that uses the
current network. This wraps functionality allowing for specification of
a network, which is useful for testing.

On top of this new API, a new test is added that checks for distinct
hashes using the same input but different networks. This is not
comprehensive, but will detect obvious issues.

How Has This Been Tested?
---
Existing tests pass. A new test passes.

What process can a PR reviewer use to test or verify this change?
---
Check that the tests do what they claim. Check that the updates to
consensus hashing properly introduce the expected wrapping
functionality. Check that the updated network API does what it is
supposed to.
@SWvheerden
Copy link
Collaborator

Closing this for: #6004

@SWvheerden SWvheerden closed this Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P-acks_required Process - Requires more ACKs or utACKs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants