-
Notifications
You must be signed in to change notification settings - Fork 28
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
pool: Use regular expressions to identify clients. #406
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) 2020-2023 The Decred developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package pool | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
errs "github.com/decred/dcrpool/errors" | ||
) | ||
|
||
// newUserAgentRE returns a compiled regular expression that matches a user | ||
// agent with the provided client name, major version, and minor version as well | ||
// as any patch, pre-release, and build metadata suffix that are valid per the | ||
// semantic versioning 2.0.0 spec. | ||
// | ||
// For reference, user agents are expected to be of the form "name/version" | ||
// where the name is a string and the version follows the semantic versioning | ||
// 2.0.0 spec. | ||
func newUserAgentRE(clientName string, clientMajor, clientMinor uint32) *regexp.Regexp { | ||
// semverBuildAndMetadataSuffixRE is a regular expression to match the | ||
// optional pre-release and build metadata portions of a semantic version | ||
// 2.0 string. | ||
const semverBuildAndMetadataSuffixRE = `(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-]` + | ||
`[0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?` + | ||
`(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?` | ||
|
||
return regexp.MustCompile(fmt.Sprintf(`^%s\/%d\.%d\.(0|[1-9]\d*)%s$`, | ||
clientName, clientMajor, clientMinor, semverBuildAndMetadataSuffixRE)) | ||
} | ||
|
||
var ( | ||
// These regular expressions are used to identify the expected mining | ||
// clients by the user agents in their mining.subscribe requests. | ||
cpuRE = newUserAgentRE("cpuminer", 1, 0) | ||
nhRE = newUserAgentRE("NiceHash", 1, 0) | ||
|
||
// miningClients maps regular expressions to the supported mining client IDs | ||
// for all user agents that match the regular expression. | ||
miningClients = map[*regexp.Regexp][]string{ | ||
cpuRE: {CPU}, | ||
nhRE: {NiceHashValidator}, | ||
} | ||
) | ||
|
||
// identifyMiningClients returns the possible mining client IDs for a given user agent | ||
// or an error when the user agent is not supported. | ||
func identifyMiningClients(userAgent string) ([]string, error) { | ||
for re, clients := range miningClients { | ||
if re.MatchString(userAgent) { | ||
return clients, nil | ||
} | ||
} | ||
|
||
msg := fmt.Sprintf("connected miner with id %s is unsupported", userAgent) | ||
return nil, errs.PoolError(errs.MinerUnknown, msg) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
I think this change would serve to simplify things quite a bit. Maybe there is a reason for it, but I don't see why a single ID would need to be mapping to multiple miner types. We can add support for that later if it ever becomes necessary.
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 left it that way because there is infrastructure for upgrading based on the hash rate. There is nothing forcing two different devices to use a different user agent, and as a result, you can have multiple devices with wildly different hashrates identifying themselves the same.
There are tests which cover the upgrading as well (see
testClientUpgrades
).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 an aside, I think that ideally the pool really generally shouldn't even be assigning hash rates and instead should figure it out dynamically. It would be similar to how the client upgrade code is doing for devices with the same UA, but instead it would allow it to become more or less difficult depending on share submission rate.
Then, it could assign some initial hash rates per UA and even better would be for the protocol to allow clients to specify it themselves (which is unfortunately not part of stratum). Naturally, it would need to be sanity checked and only serve as an initial starting point for the aforementioned dynamic behavior.
However, that is a fair amount of work that doesn't seem worth it given the goal at the moment is primarily getting into shape for BLAKE3 and gominer as opposed to adding new features.