-
Notifications
You must be signed in to change notification settings - Fork 288
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
cpuminer: Rework to use bg template generator. #2277
Merged
Merged
Conversation
This file contains 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
792e3b1
to
fb4fd00
Compare
dnldd
reviewed
Jul 20, 2020
JoeGruffins
reviewed
Jul 21, 2020
fb4fd00
to
69f26c0
Compare
JoeGruffins
approved these changes
Jul 23, 2020
69f26c0
to
2bd4ae7
Compare
davecgh
commented
Jul 23, 2020
This significantly reworks the CPU miner to make use of the background block template generator that was introduced in the previous release as well as to convert its lifecycle to use the expected pattern for running subsystems based on contexts. The switch to using the background block template generator provides all the benefits it offers to miners such as: - Intelligent vote propagation handling - Improved handling of chain reorganizations necessary when the current tip is unable to obtain enough votes - Current state synchronization - Near elimination of stale templates when new blocks and votes are received This is particularly important in testing scenarios, such as when using the simulation network, since the difficulty is so low that the code this is replacing is often able to mine blocks so fast it ends up mining multiple alternative blocks while waiting for the votes to show up which makes testing with it more difficult. This is no longer an issue when requesting template updates since the background block template generator has logic to provide the votes a chance to arrive before building and notifying the template. Another modification this makes is to also make use of template notifications when mining blocks mined via the discrete mining mode and to only count blocks if they successfully extend the main chain. This also significantly helps in testing scenarios since it means the testing code and/or testers using the simulation test network can rely on the requested number of blocks actually being added to the main chain even if more need to be generated to make that happen. Along similar lines, one area this change does not address, but does pave the way to support and would be useful in the future for the simulation test network is to add some additional logic to provide ticket purchases a chance to arrive when prior to stake validation height, since that is another area that often complicates testing. In terms of the lifecycle changes, this replaces the Start, Stop, and Wait methods with a single method named Run and arranges for it to block until the provided context is cancelled. This is more flexible for the caller since it can easily turn blocking code into async code while the reverse is not true. The new Run method waits for all goroutines that it starts to shutdown before returning to help ensure an orderly shutdown. In addition, the semantics of Run are different from Start/Stop in that the Start method launched the default number of mining worker goroutines whereas Run only starts the CPU miner with the main infrastructure goroutines and zero workers, meaning it will be idle until explicitly requested to mine by the caller setting the number of workers to use. Since there are exported methods that send messages to the CPU miner infrastructure goroutines via a channel and those goroutines are stopped during the shutdown process, all sends select across both the channel in question as well as a quit channel which is closed when the context is canceled. This ensures callers can't end up blocking on send to a goroutine that is no longer running without needing additional mutexes. As part of the conversion, it improves a lot of the comments to better describe the expected behavior and further integrates the context to support cancellation where it previously was not supported. Finally, all callers are updated to use the new API and the server is modified to take advantage of the new lifecycle semantics by taking ownership of the CPU miner code directly in its Run method where it more logically makes sense and only creating the template generate and CPU miner infrastructure if needed based on the configuration options.
2bd4ae7
to
a6feca2
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This requires #2276.
This significantly reworks the CPU miner to make use of the background block template generator that was introduced in the previous release as well as to convert its lifecycle to use the expected pattern for running subsystems based on contexts.
The switch to using the background block template generator provides all the benefits it offers to miners such as:
This is particularly important in testing scenarios, such as when using the simulation network, since the difficulty is so low that the code this is replacing is often able to mine blocks so fast it ends up mining multiple alternative blocks while waiting for the votes to show up which makes testing with it more difficult.
This is no longer an issue when requesting template updates since the background block template generator has logic to provide the votes a chance to arrive before building and notifying the template.
Another modification this makes is to also make use of template notifications when mining blocks mined via the discrete mining mode and to only count blocks if they successfully extend the main chain. This also significantly helps in testing scenarios since it means the testing code and/or testers using the simulation test network can rely on the requested number of blocks actually being added to the main chain even if more need to be generated to make that happen.
Along similar lines, one area this change does not address, but does pave the way to support and would be useful in the future for the simulation test network is to add some additional logic to provide ticket purchases a chance to arrive when prior to stake validation height, since that is another area that often complicates testing.
In terms of the lifecycle changes, this replaces the
Start
,Stop
, andWait
methods with a single method namedRun
and arranges for it to block until the provided context is cancelled. This is more flexible for the caller since it can easily turn blocking code into async code while the reverse is not true.The new
Run
method waits for all goroutines that it starts to shutdown before returning to help ensure an orderly shutdown.In addition, the semantics of
Run
are different fromStart
/Stop
in that theStart
method launched the default number of mining worker goroutines whereasRun
only starts the CPU miner with the main infrastructure goroutines and zero workers, meaning it will be idle until explicitly requested to mine by the caller setting the number of workers to use.Since there are exported methods that send messages to the CPU miner infrastructure goroutines via a channel and those goroutines are stopped during the shutdown process, all sends select across both the channel in question as well as a
quit
channel which is closed when the context is canceled. This ensures callers can't end up blocking on send to a goroutine that is no longer running without needing additional mutexes.As part of the conversion, it improves a lot of the comments to better describe the expected behavior and further integrates the context to support cancellation where it previously was not supported.
Finally, all callers are updated to use the new API and the server is modified to take advantage of the new lifecycle semantics by taking ownership of the CPU miner code directly in its Run method where it more logically makes sense and only creating the template generate and CPU miner infrastructure if needed based on the configuration options.