@@ -7,6 +7,8 @@ var meterify = require("meterify").meterify;
77var Web3 = require ( "web3" ) ;
88const { default : BigNumber } = require ( "bignumber.js" ) ;
99
10+ const ADDR_PATTERN = RegExp ( "^0x[0-9a-fA-F]{40}$" ) ;
11+
1012module . exports = function ( logger ) {
1113 var poolConfigs = JSON . parse ( process . env . pools ) ;
1214
@@ -251,14 +253,24 @@ function SetupForPool(logger, poolOptions, setupFinished) {
251253 } ,
252254 0
253255 ) ;
256+
254257 totalShares += totalSharesInRound ;
258+ logger . debug (
259+ logSystem ,
260+ logComponent ,
261+ "Total shares in round " +
262+ round . height +
263+ ": " +
264+ totalSharesInRound
265+ ) ;
255266
256267 for ( var workerAddress in workerShares ) {
257268 if ( ! ( workerAddress in pendingShares ) ) {
258269 pendingShares [ workerAddress ] = 0 ;
259270 }
260- pendingShares [ workerAddress ] +=
261- workerShares [ workerAddress ] ;
271+ pendingShares [ workerAddress ] += Number (
272+ workerShares [ workerAddress ]
273+ ) ;
262274 }
263275 break ;
264276 }
@@ -286,9 +298,18 @@ function SetupForPool(logger, poolOptions, setupFinished) {
286298 logComponent ,
287299 "Loading MTR balance and calculate the actual reward amount for each worker"
288300 ) ;
301+ console . log ( "get energy for beneficiary: " , beneficiary ) ;
289302 web3 . eth . getEnergy ( beneficiary , function ( err , bal ) {
290303 let pendingReward = new BigNumber ( bal ) ;
304+ if ( err ) {
305+ console . log ( "ERROR: " , err ) ;
306+ console . log ( "err Bal: " , bal ) ;
307+ return callback ( err ) ;
308+ }
291309 console . log ( "balance: " , bal ) ;
310+ if ( Number ( bal ) == NaN ) {
311+ callback ( "could not load balance" ) ;
312+ }
292313 if ( beneficiary in workers ) {
293314 const selfBalance = new BigNumber (
294315 workers [ beneficiary ] . balance || 0
@@ -320,30 +341,55 @@ function SetupForPool(logger, poolOptions, setupFinished) {
320341 "Pool has pending reward of " + pendingReward . toFixed ( 0 )
321342 ) ;
322343
323- for ( let w in workers ) {
324- if ( w in pendingShares ) {
325- if ( ! workers [ w ] . reward ) {
326- workers [ w ] . reward = new BigNumber ( 0 ) ;
327- } else {
344+ console . log ( "pendingShares: " , pendingShares ) ;
345+ for ( let w in pendingShares ) {
346+ const share = new BigNumber ( pendingShares [ w ] ) . toNumber ( ) ;
347+ w = w . toLowerCase ( ) ;
348+ if ( w == "time" ) {
349+ continue ;
350+ }
351+ if ( ! ( w in workers ) ) {
352+ workers [ w ] = {
353+ reward : new BigNumber ( 0 ) ,
354+ balance : new BigNumber ( 0 ) ,
355+ } ;
356+ } else {
357+ console . log ( "reward: " , workers [ w ] . reward ) ;
358+ console . log ( "balance: " , workers [ w ] . balance ) ;
359+ if ( workers [ w ] . reward ) {
328360 workers [ w ] . reward = new BigNumber ( workers [ w ] . reward ) ;
329- }
330- if ( ! workers [ w ] . balance ) {
331- workers [ w ] . balance = new BigNumber ( 0 ) ;
332361 } else {
333- workers [ w ] . balance = new BigNumber ( workers [ w ] . balance ) ;
362+ workers [ w ] . reward = new BigNumber ( 0 ) ;
334363 }
335-
336- const workerReward = pendingReward
337- . times ( share )
338- . div ( totalShares ) ;
339- if ( w . match ( ADDR_PATTERN ) ) {
340- workers [ w ] . reward = workerReward . add ( workers [ w ] . reward ) ;
341- workers [ w ] . issue = true ;
364+ if ( workers [ w ] . balance ) {
365+ workers [ w ] . balance = new BigNumber ( workers [ w ] . balance ) ;
342366 } else {
343- workers [ w ] . balance = workerReward . add ( workers [ w ] . balance ) ;
344- workers [ w ] . issue = false ;
367+ workers [ w ] . balance = new BigNumber ( 0 ) ;
345368 }
346369 }
370+
371+ console . log ( "PENDING REWARD: " , pendingReward . toFixed ( 0 ) ) ;
372+ console . log ( "SHARE:" , share ) ;
373+ console . log ( "TOTAL SHARES: " , totalShares ) ;
374+ const workerReward = pendingReward . times ( share ) . div ( totalShares ) ;
375+ console . log (
376+ "worker: " ,
377+ w ,
378+ "reward amount: " ,
379+ workerReward . toNumber ( )
380+ ) ;
381+ console . log ( `workers[w].reward: ${ workers [ w ] . reward } ` ) ;
382+ console . log ( `workers[w].balance: ${ workers [ w ] . balance } ` ) ;
383+ if ( w . match ( ADDR_PATTERN ) ) {
384+ workers [ w ] . reward = workerReward . plus ( workers [ w ] . reward ) ;
385+ workers [ w ] . issue = true ;
386+ } else {
387+ workers [ w ] . balance = workerReward . plus ( workers [ w ] . balance ) ;
388+ workers [ w ] . issue = false ;
389+ }
390+ console . log (
391+ `worker ${ w } , balance: ${ workers [ w ] . balance } , reward: ${ workers [ w ] . reward } `
392+ ) ;
347393 }
348394
349395 callback ( null , workers , rounds ) ;
@@ -359,8 +405,8 @@ function SetupForPool(logger, poolOptions, setupFinished) {
359405 worker . reward = worker . reward || 0 ;
360406 if ( worker . issue ) {
361407 addressAmounts [ w ] = new BigNumber ( 0 )
362- . add ( worker . balance )
363- . add ( worker . reward ) ;
408+ . plus ( worker . balance )
409+ . plus ( worker . reward ) ;
364410 }
365411 }
366412
@@ -370,9 +416,17 @@ function SetupForPool(logger, poolOptions, setupFinished) {
370416 }
371417
372418 logger . debug ( logSystem , logComponent , "Prepare to send reward tx" ) ;
373- web3 . eth . getBlockNum ( function ( err , blockNum ) {
419+ web3 . eth . getBlockNumber ( function ( err , blockNum ) {
420+ if ( err ) {
421+ console . log ( "getBlockNumber erro: " , err ) ;
422+ return callback ( err ) ;
423+ }
374424 console . log ( "best num:" , blockNum ) ;
375425 web3 . eth . getBlock ( blockNum , function ( err , best ) {
426+ if ( err ) {
427+ console . log ( "getBlock erro: " , err ) ;
428+ return callback ( err ) ;
429+ }
376430 console . log ( best ) ;
377431 const blockRef = best . id . substr ( 0 , 18 ) ;
378432 let chainTag = processingConfig . chainTag ; // chainTag for testnet
@@ -381,24 +435,29 @@ function SetupForPool(logger, poolOptions, setupFinished) {
381435 let clauses = [ ] ;
382436 for ( const addr in addressAmounts ) {
383437 const amount = addressAmounts [ addr ] ;
384- console . log ( `pay to ${ addr } with ${ amount } ` ) ;
385- clauses . push ( { to : addr , value : amount . toFixed ( 0 ) } ) ;
438+ console . log ( `pay to ${ addr } with ${ amount . toFixed ( 0 ) } ` ) ;
439+ clauses . push ( {
440+ to : addr ,
441+ value : amount . toFixed ( 0 ) ,
442+ token : 0 ,
443+ data : "0x" ,
444+ } ) ;
386445 }
387446 const baseGas = 5000 + clauses . length * 16000 ; // fixed value
388447 console . log ( "base gas: " , baseGas ) ;
389448 console . log ( "data gas: " , dataGas ) ;
390449 let txObj = {
391450 chainTag,
392451 blockRef, // the first 8 bytes of latest block
393- expiration : 48 , // blockRefHeight + expiration is the height for tx expire
452+ expiration : 64 , // blockRefHeight + expiration is the height for tx expire
394453 clauses,
395454 gasPriceCoef : 0 ,
396455 gas : baseGas + dataGas ,
397456 dependsOn : null ,
398- nonce : getRandomInt ( Number . MAX_SAFE_INTEGER ) , // random number
457+ nonce : 0 , // random number
399458 } ;
400459 let tx = new Transaction ( txObj ) ;
401- const pkBuffer = Buffer . from ( pk . replace ( "0x" , "" ) , "hex" ) ;
460+ const pkBuffer = Buffer . from ( poolPK . replace ( "0x" , "" ) , "hex" ) ;
402461 const signingHash = cry . blake2b256 ( tx . encode ( ) ) ;
403462 logger . debug ( logSystem , logComponent , "Signed reward tx" ) ;
404463 tx . signature = cry . secp256k1 . sign ( signingHash , pkBuffer ) ;
@@ -407,6 +466,10 @@ function SetupForPool(logger, poolOptions, setupFinished) {
407466 const rawTx = "0x" + raw . toString ( "hex" ) ;
408467 logger . debug ( logSystem , logComponent , "Sending out reward tx" ) ;
409468 web3 . eth . sendSignedTransaction ( rawTx , function ( err ) {
469+ if ( err ) {
470+ console . log ( "sendSignedTransaction error:" , err ) ;
471+ return callback ( err ) ;
472+ }
410473 callback ( null , workers , rounds ) ;
411474 } ) ;
412475 } ) ;
@@ -464,7 +527,7 @@ function SetupForPool(logger, poolOptions, setupFinished) {
464527 trySend ( 0 ) ;
465528 } ,
466529 function ( workers , rounds , callback ) {
467- var totalPaid = 0 ;
530+ var totalPaid = new BigNumber ( 0 ) ;
468531
469532 var balanceUpdateCommands = [ ] ;
470533 var workerPayoutsCommand = [ ] ;
@@ -488,7 +551,7 @@ function SetupForPool(logger, poolOptions, setupFinished) {
488551 "hset" ,
489552 coin + ":balances" ,
490553 w ,
491- worker . balance . toNumber ( ) ,
554+ worker . balance ,
492555 ] ) ;
493556 }
494557 }
@@ -549,7 +612,7 @@ function SetupForPool(logger, poolOptions, setupFinished) {
549612 "hincrbyfloat" ,
550613 coin + ":stats" ,
551614 "totalPaid" ,
552- totalPaid ,
615+ totalPaid . toFixed ( 0 ) ,
553616 ] ) ;
554617 }
555618
0 commit comments