@@ -84,8 +84,7 @@ describe('test share processor', function(){
84
84
shareProcessor . redisClient = redisClient ;
85
85
86
86
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' } ;
89
88
90
89
var checkState = function ( ) {
91
90
redisClient
@@ -106,9 +105,9 @@ describe('test share processor', function(){
106
105
}
107
106
108
107
var roundKey = shareProcessor . roundKey (
109
- blockData . fromGroup ,
110
- blockData . toGroup ,
111
- blockData . hash
108
+ block . fromGroup ,
109
+ block . toGroup ,
110
+ block . hash
112
111
) ;
113
112
114
113
redisClient
@@ -123,6 +122,69 @@ describe('test share processor', function(){
123
122
} ) ;
124
123
} )
125
124
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
+
126
188
it ( 'should remove orphan block and shares' , function ( done ) {
127
189
var shareProcessor = new ShareProcessor ( test . config , test . logger ) ;
128
190
shareProcessor . redisClient = redisClient ;
@@ -137,6 +199,7 @@ describe('test share processor', function(){
137
199
{ hash : 'block3' , height : 3 , chainFrom : 0 , chainTo : 0 , transactions : transactions , inMainChain : true , submittedMs : currentMs - confirmationTime } ,
138
200
{ hash : 'block4' , height : 4 , chainFrom : 0 , chainTo : 0 , transactions : transactions , inMainChain : true , submittedMs : currentMs - confirmationTime } ,
139
201
] ;
202
+ var orphanBlock = blocks [ 1 ] ;
140
203
141
204
var shares = { } ;
142
205
for ( var block of blocks ) {
@@ -146,10 +209,13 @@ describe('test share processor', function(){
146
209
function prepare ( blocks , shares , callback ) {
147
210
var restServer = nock ( 'http://127.0.0.1:12973' ) ;
148
211
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` } ) ;
149
215
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 ) ;
151
217
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 ) ;
153
219
154
220
var blockWithTs = block . hash + ':' + block . submittedMs ;
155
221
redisTx . sadd ( 'pendingBlocks' , blockWithTs ) ;
@@ -171,7 +237,6 @@ describe('test share processor', function(){
171
237
}
172
238
173
239
var checkState = function ( ) {
174
- var orphanBlock = blocks [ 1 ] ;
175
240
var orphanBlockWithTs = orphanBlock . hash + ':' + orphanBlock . submittedMs ;
176
241
var roundKey = shareProcessor . roundKey ( orphanBlock . chainFrom , orphanBlock . chainTo , orphanBlock . hash ) ;
177
242
@@ -193,11 +258,11 @@ describe('test share processor', function(){
193
258
var expected = [
194
259
{
195
260
pendingBlockValue : blocks [ 2 ] . hash + ':' + blocks [ 2 ] . submittedMs ,
196
- data : blockData ( blocks [ 2 ] )
261
+ ... blockData ( blocks [ 2 ] )
197
262
} ,
198
263
{
199
264
pendingBlockValue : blocks [ 3 ] . hash + ':' + blocks [ 3 ] . submittedMs ,
200
- data : blockData ( blocks [ 3 ] )
265
+ ... blockData ( blocks [ 3 ] )
201
266
}
202
267
] ;
203
268
0 commit comments