From 9e08c1d5c7b54bd4d762d17c8521dc08cbc4630e Mon Sep 17 00:00:00 2001 From: Benoit Perroud Date: Fri, 30 Sep 2022 10:03:40 +0200 Subject: [PATCH] Allow multiple instances of the pool to share the same redis --- lib/shareProcessor.js | 52 ++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/lib/shareProcessor.js b/lib/shareProcessor.js index 18142b7..932c0f3 100644 --- a/lib/shareProcessor.js +++ b/lib/shareProcessor.js @@ -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); + }); }); }