-
Notifications
You must be signed in to change notification settings - Fork 33
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
Support multiple instance #20
Open
Lbqds
wants to merge
5
commits into
master
Choose a base branch
from
support-multiple-instance
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ const { Pool } = require('pg'); | |
var ShareProcessor = module.exports = function ShareProcessor(config, logger){ | ||
var confirmationTime = config.confirmationTime * 1000; | ||
var rewardPercent = 1 - config.withholdPercent; | ||
var shareExpiryPeriod = 15; | ||
|
||
var _this = this; | ||
this.redisClient = new Redis(config.redis.port, config.redis.host); | ||
|
@@ -71,11 +72,11 @@ var ShareProcessor = module.exports = function ShareProcessor(config, logger){ | |
createTables(_this.db); | ||
_this.handleShare = function(share){ | ||
persistShare(_this.db, share); | ||
_this._handleShare(share); | ||
_this._handleShare(share, _ => {}); | ||
} | ||
} | ||
else { | ||
_this.handleShare = share => _this._handleShare(share); | ||
_this.handleShare = share => _this._handleShare(share, _ => {}); | ||
} | ||
|
||
this.currentRoundKey = function(fromGroup, toGroup){ | ||
|
@@ -91,28 +92,50 @@ var ShareProcessor = module.exports = function ShareProcessor(config, logger){ | |
var hashrateKey = 'hashrate'; | ||
var balancesKey = 'balances'; | ||
|
||
this._handleShare = function(share){ | ||
var redisTx = _this.redisClient.multi(); | ||
var currentMs = Date.now(); | ||
this.shareCacheKey = function(fromGroup, toGroup, hash){ | ||
return fromGroup + ':' + toGroup + ':hashes:' + hash; | ||
} | ||
|
||
this._handleShare = function(share, callback){ | ||
var fromGroup = share.job.fromGroup; | ||
var toGroup = share.job.toGroup; | ||
var blockHash = share.blockHash; | ||
var currentRound = _this.currentRoundKey(fromGroup, toGroup); | ||
redisTx.hincrbyfloat(currentRound, share.workerAddress, share.difficulty); | ||
var hashKey = _this.shareCacheKey(fromGroup, toGroup, blockHash); | ||
|
||
var currentTs = Math.floor(currentMs / 1000); | ||
redisTx.zadd(hashrateKey, currentTs, [fromGroup, toGroup, share.worker, share.difficulty, currentMs].join(':')); | ||
_this.redisClient.set(hashKey, true, 'EX', shareExpiryPeriod, 'NX', function(error, result){ | ||
if (error){ | ||
logger.error('Check share duplicated failed, error: ' + error); | ||
callback(error); | ||
return; | ||
} | ||
|
||
if (share.foundBlock){ | ||
var blockHash = share.blockHash; | ||
var newKey = _this.roundKey(fromGroup, toGroup, blockHash); | ||
var blockWithTs = blockHash + ':' + currentMs.toString(); | ||
if (result == null){ | ||
logger.error('Ignore duplicated share, key: ' + hashKey); | ||
callback('duplicated share'); | ||
return; | ||
} | ||
|
||
redisTx.rename(currentRound, newKey); | ||
redisTx.sadd(pendingBlocksKey, blockWithTs); | ||
redisTx.hset(foundBlocksKey, blockHash, share.workerAddress) | ||
} | ||
redisTx.exec(function(error, _){ | ||
if (error) logger.error('Handle share failed, error: ' + error); | ||
var redisTx = _this.redisClient.multi(); | ||
redisTx.hincrbyfloat(currentRound, share.workerAddress, share.difficulty); | ||
|
||
var currentMs = Date.now(); | ||
var currentTs = Math.floor(currentMs / 1000); | ||
redisTx.zadd(hashrateKey, currentTs, [fromGroup, toGroup, share.worker, share.difficulty, currentMs].join(':')); | ||
|
||
if (share.foundBlock){ | ||
var blockHash = share.blockHash; | ||
var newKey = _this.roundKey(fromGroup, toGroup, blockHash); | ||
var blockWithTs = blockHash + ':' + currentMs.toString(); | ||
|
||
redisTx.rename(currentRound, newKey); | ||
redisTx.sadd(pendingBlocksKey, blockWithTs); | ||
redisTx.hset(foundBlocksKey, blockHash, share.workerAddress) | ||
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.
|
||
} | ||
redisTx.exec(function(error, _){ | ||
if (error) logger.error('Handle share failed, error: ' + error); | ||
callback(error); | ||
}); | ||
}); | ||
} | ||
|
||
|
@@ -265,6 +288,8 @@ var ShareProcessor = module.exports = function ShareProcessor(config, logger){ | |
} | ||
|
||
this.start = function(){ | ||
setInterval(scanBlocks, config.rewardInterval * 1000); | ||
if (config.rewardEnabled){ | ||
setInterval(scanBlocks, config.rewardInterval * 1000); | ||
} | ||
} | ||
} |
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 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
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.
renamenx
should probably be used here, otherwise in case of found block sent with delay (but still not expired) by one worker, thesharecache
key would have been deleted and the shares for the block overwritten by new currentRound.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.
Great catch, thanks, the remaining commands will continue to execute even if
renamenx
failed in redis transaction, so my thought is that we can useset hash true ex expiryPeriod nx
to check if the share is duplicated, whichexpiryPeriod > jobExpiryPeriod
, how do you think?