Skip to content

Commit 17a827c

Browse files
committed
Add more tests
1 parent 5e0a9ea commit 17a827c

File tree

1 file changed

+75
-10
lines changed

1 file changed

+75
-10
lines changed

test/shareProcessorTest.js

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ describe('test share processor', function(){
8484
shareProcessor.redisClient = redisClient;
8585

8686
var shares = {'miner0': '4', 'miner1': '2', 'miner2': '2'};
87-
var blockData = {hash: '0011', fromGroup: 0, toGroup: 1, height: 1, rewardAmount: '40000000000000000000'};
88-
var block = {pendingBlockValue: blockData.hash + ':' + '0', data: blockData};
87+
var block = {pendingBlockValue: '0011' + ':' + '0', hash: '0011', fromGroup: 0, toGroup: 1, height: 1, rewardAmount: '40000000000000000000'};
8988

9089
var checkState = function(){
9190
redisClient
@@ -106,9 +105,9 @@ describe('test share processor', function(){
106105
}
107106

108107
var roundKey = shareProcessor.roundKey(
109-
blockData.fromGroup,
110-
blockData.toGroup,
111-
blockData.hash
108+
block.fromGroup,
109+
block.toGroup,
110+
block.hash
112111
);
113112

114113
redisClient
@@ -123,6 +122,69 @@ describe('test share processor', function(){
123122
});
124123
})
125124

125+
it('should reward uncle miners with correct reward amount', function(done){
126+
var config = { ...test.config, confirmationTime: 0 }
127+
var shareProcessor = new ShareProcessor(config, test.logger);
128+
shareProcessor.redisClient = redisClient;
129+
130+
var currentMs = Date.now();
131+
var rewardAmount = '4000000000000000000';
132+
var ghostUncleRewardAmount = '2000000000000000000';
133+
var ghostUncleCoinbaseTx = [{unsigned:{fixedOutputs:[{attoAlphAmount: rewardAmount}]}}];
134+
var ghostUncleBlock = {hash: 'block1', height: 1, chainFrom: 0, chainTo: 0, transactions: ghostUncleCoinbaseTx, inMainChain: false, submittedMs: currentMs, ghostUncles: []}
135+
136+
var mainChainCoinbaseTx = [{unsigned:{fixedOutputs:[{attoAlphAmount: rewardAmount},{attoAlphAmount: ghostUncleRewardAmount}]}}];
137+
var mainChainBlock = {hash: 'block2', height: 2, chainFrom: 0, chainTo: 0, transactions: mainChainCoinbaseTx, inMainChain: true, submittedMs: currentMs, ghostUncles: [{hash:ghostUncleBlock.hash}]}
138+
var blocks = [ghostUncleBlock, mainChainBlock]
139+
140+
function prepare(blocks, callback){
141+
var restServer = nock('http://127.0.0.1:12973');
142+
var redisTx = redisClient.multi();
143+
restServer.persist().get('/blockflow/main-chain-block-by-ghost-uncle/' + ghostUncleBlock.hash).reply(200, mainChainBlock)
144+
for (var block of blocks){
145+
restServer.persist().get('/blockflow/blocks/' + block.hash).reply(200, block);
146+
var isInMainChainPath = '/blockflow/is-block-in-main-chain?blockHash=' + block.hash;
147+
restServer.persist().get(isInMainChainPath).reply(200, block.inMainChain ? true : false);
148+
149+
var blockWithTs = block.hash + ':' + block.submittedMs;
150+
redisTx.sadd('pendingBlocks', blockWithTs);
151+
}
152+
153+
redisTx.exec(function(error, _){
154+
if (error) assert.fail('Test failed: ' + error);
155+
callback(restServer);
156+
});
157+
}
158+
159+
prepare(blocks, _ => {
160+
shareProcessor.getPendingBlocks(
161+
blocks.map(block => block.hash + ':' + block.submittedMs),
162+
function(pendingBlocks){
163+
expect(pendingBlocks).to.deep.equal([
164+
{
165+
fromGroup: 0,
166+
hash: "block1",
167+
height: 1,
168+
pendingBlockValue: blocks[0].hash + ':' + blocks[0].submittedMs,
169+
rewardAmount: "2000000000000000000",
170+
toGroup: 0,
171+
},
172+
{
173+
fromGroup: 0,
174+
hash: "block2",
175+
height: 2,
176+
pendingBlockValue: blocks[1].hash + ':' + blocks[1].submittedMs,
177+
rewardAmount: "4000000000000000000",
178+
toGroup: 0
179+
}
180+
]);
181+
nock.cleanAll();
182+
done();
183+
}
184+
);
185+
});
186+
})
187+
126188
it('should remove orphan block and shares', function(done){
127189
var shareProcessor = new ShareProcessor(test.config, test.logger);
128190
shareProcessor.redisClient = redisClient;
@@ -137,6 +199,7 @@ describe('test share processor', function(){
137199
{hash: 'block3', height: 3, chainFrom: 0, chainTo: 0, transactions: transactions, inMainChain: true, submittedMs: currentMs - confirmationTime},
138200
{hash: 'block4', height: 4, chainFrom: 0, chainTo: 0, transactions: transactions, inMainChain: true, submittedMs: currentMs - confirmationTime},
139201
];
202+
var orphanBlock = blocks[1];
140203

141204
var shares = {};
142205
for (var block of blocks){
@@ -146,10 +209,13 @@ describe('test share processor', function(){
146209
function prepare(blocks, shares, callback){
147210
var restServer = nock('http://127.0.0.1:12973');
148211
var redisTx = redisClient.multi();
212+
restServer.persist()
213+
.get('/blockflow/main-chain-block-by-ghost-uncle/' + orphanBlock.hash)
214+
.reply(404, { detail: `Mainchain block by ghost uncle hash ${orphanBlock.hash} not found` });
149215
for (var block of blocks){
150-
restServer.get('/blockflow/blocks/' + block.hash).reply(200, block);
216+
restServer.persist().get('/blockflow/blocks/' + block.hash).reply(200, block);
151217
var path = '/blockflow/is-block-in-main-chain?blockHash=' + block.hash;
152-
restServer.get(path).reply(200, block.inMainChain ? true : false);
218+
restServer.persist().get(path).reply(200, block.inMainChain ? true : false);
153219

154220
var blockWithTs = block.hash + ':' + block.submittedMs;
155221
redisTx.sadd('pendingBlocks', blockWithTs);
@@ -171,7 +237,6 @@ describe('test share processor', function(){
171237
}
172238

173239
var checkState = function(){
174-
var orphanBlock = blocks[1];
175240
var orphanBlockWithTs = orphanBlock.hash + ':' + orphanBlock.submittedMs;
176241
var roundKey = shareProcessor.roundKey(orphanBlock.chainFrom, orphanBlock.chainTo, orphanBlock.hash);
177242

@@ -193,11 +258,11 @@ describe('test share processor', function(){
193258
var expected = [
194259
{
195260
pendingBlockValue: blocks[2].hash + ':' + blocks[2].submittedMs,
196-
data: blockData(blocks[2])
261+
...blockData(blocks[2])
197262
},
198263
{
199264
pendingBlockValue: blocks[3].hash + ':' + blocks[3].submittedMs,
200-
data: blockData(blocks[3])
265+
...blockData(blocks[3])
201266
}
202267
];
203268

0 commit comments

Comments
 (0)