-
Notifications
You must be signed in to change notification settings - Fork 220
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
refactor: better network global #6014
Conversation
Test Results (Integration tests) 2 files + 2 12 suites +12 1h 23m 51s ⏱️ + 1h 23m 51s 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. |
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.
this looks like it could solve the test problem
Co-authored-by: SW van Heerden <[email protected]>
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.
LGTM
let mut current_network = CURRENT_NETWORK.lock()?; | ||
*current_network = network; | ||
Ok(()) | ||
// let mut current_network = Network::current(); |
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.
// let mut current_network = Network::current(); |
// assert_eq!(Network::current(), Network::Esmeralda); | ||
|
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.
// assert_eq!(Network::current(), Network::Esmeralda); |
// assert_eq!(Network::current(), Network::Esmeralda); | ||
|
||
// Generate a mainnet hash | ||
// Network::set_current(Network::MainNet); |
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.
// Network::set_current(Network::MainNet); |
|
||
// They should be distinct | ||
assert_ne!(hash_mainnet, hash_stagenet); | ||
|
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.
|
||
// 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()); |
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.
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.
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.
Closing this for: #6004 |
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