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

Allow multiple instances of the pool to share the same redis #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions lib/shareProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,55 @@ var ShareProcessor = module.exports = function ShareProcessor(config, logger){
return fromGroup + ':' + toGroup + ':shares:' + blockHash;
}

this.shareCacheKey = function(fromGroup, toGroup){
return fromGroup + ':' + toGroup + ':sharecache';
}

var pendingBlocksKey = 'pendingBlocks';
var foundBlocksKey = 'foundBlocks';
var hashrateKey = 'hashrate';
var balancesKey = 'balances';

this._handleShare = function(share){
var redisTx = _this.redisClient.multi();
var currentMs = Date.now();
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 cacheKey = _this.shareCacheKey(fromGroup, toGroup);

_this.redisClient.sadd(cacheKey, blockHash, function(error, result){
if (error){
logger.error('Check share duplicated failed, error: ' + error);
callback(error);
return;
}

if (result === 0){
logger.error('Ignore duplicated share');
callback('Duplicated share');
return;
}

var currentTs = Math.floor(currentMs / 1000);
redisTx.zadd(hashrateKey, currentTs, [fromGroup, toGroup, share.worker, share.difficulty, currentMs].join(':'));
var redisTx = _this.redisClient.multi();
var currentMs = Date.now();
redisTx.hincrbyfloat(currentRound, share.workerAddress, share.difficulty);

if (share.foundBlock){
var blockHash = share.blockHash;
var newKey = _this.roundKey(fromGroup, toGroup, blockHash);
var blockWithTs = blockHash + ':' + currentMs.toString();
var currentTs = Math.floor(currentMs / 1000);
redisTx.zadd(hashrateKey, currentTs, [fromGroup, toGroup, share.worker, share.difficulty, currentMs].join(':'));

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);
if (share.foundBlock){
var blockHash = share.blockHash;
var newKey = _this.roundKey(fromGroup, toGroup, blockHash);
var blockWithTs = blockHash + ':' + currentMs.toString();

redisTx.renamenx(currentRound, newKey);
redisTx.sadd(pendingBlocksKey, blockWithTs);
redisTx.hsetnx(foundBlocksKey, blockHash, share.workerAddress)
redisTx.del(cacheKey);
}
redisTx.exec(function(error, _){
if (error) logger.error('Handle share failed, error: ' + error);
});
});
}

Expand Down