1
1
var bignum = require ( 'bignum' ) ;
2
-
3
2
var merkleTree = require ( './merkleTree.js' ) ;
4
3
var transactions = require ( './transactions.js' ) ;
5
4
var util = require ( './util.js' ) ;
6
5
7
-
8
6
/**
9
7
* The BlockTemplate class holds a single job.
10
8
* and provides several methods to validate and submit it to the daemon coin
11
9
**/
12
10
var BlockTemplate = module . exports = function BlockTemplate ( jobId , rpcData , poolAddressScript , extraNoncePlaceholder , reward , txMessages , recipients , network ) {
13
11
14
12
//private members
15
-
16
13
var submits = [ ] ;
17
14
18
15
function getMerkleHashes ( steps ) {
19
- return steps . map ( function ( step ) {
20
- return step . toString ( 'hex' ) ;
21
- } ) ;
16
+ return steps . map ( step => step . toString ( 'hex' ) ) ;
22
17
}
23
18
24
19
function getTransactionBuffers ( txs ) {
25
- var txHashes = txs . map ( function ( tx ) {
26
- if ( tx . txid !== undefined ) {
27
- return util . uint256BufferFromHash ( tx . txid ) ;
28
- }
29
- return util . uint256BufferFromHash ( tx . hash ) ;
30
- } ) ;
31
- return [ null ] . concat ( txHashes ) ;
20
+ return [ null ] . concat ( txs . map ( tx => Buffer . from ( util . uint256BufferFromHash ( tx . txid || tx . hash ) ) ) ) ;
32
21
}
33
22
34
23
function getVoteData ( ) {
35
- if ( ! rpcData . masternode_payments ) return new Buffer ( [ ] ) ;
36
-
37
- return Buffer . concat (
38
- [ util . varIntBuffer ( rpcData . votes . length ) ] . concat (
39
- rpcData . votes . map ( function ( vt ) {
40
- return new Buffer ( vt , 'hex' ) ;
41
- } )
42
- )
43
- ) ;
24
+ if ( ! rpcData . masternode_payments ) return Buffer . alloc ( 0 ) ;
25
+ return Buffer . concat ( [
26
+ util . varIntBuffer ( rpcData . votes . length ) ,
27
+ ...rpcData . votes . map ( vt => Buffer . from ( vt , 'hex' ) )
28
+ ] ) ;
44
29
}
45
30
46
31
//public members
47
-
48
32
this . rpcData = rpcData ;
49
33
this . jobId = jobId ;
50
-
51
-
52
- this . target = rpcData . target ?
53
- bignum ( rpcData . target , 16 ) :
54
- util . bignumFromBitsHex ( rpcData . bits ) ;
55
-
34
+ this . target = rpcData . target ? bignum ( rpcData . target , 16 ) : util . bignumFromBitsHex ( rpcData . bits ) ;
56
35
this . difficulty = parseFloat ( ( diff1 / this . target . toNumber ( ) ) . toFixed ( 9 ) ) ;
36
+ this . prevHashReversed = util . reverseByteOrder ( Buffer . from ( rpcData . previousblockhash , 'hex' ) ) . toString ( 'hex' ) ;
57
37
58
-
59
-
60
- this . prevHashReversed = util . reverseByteOrder ( new Buffer ( rpcData . previousblockhash , 'hex' ) ) . toString ( 'hex' ) ;
61
38
if ( rpcData . finalsaplingroothash ) {
62
- this . finalsaplingroothashReversed = util . reverseByteOrder ( new Buffer ( rpcData . finalsaplingroothash , 'hex' ) ) . toString ( 'hex' ) ;
63
- }
64
- if ( rpcData . hashstateroot ) {
65
- this . hashstaterootReversed = util . reverseBuffer ( new Buffer ( rpcData . hashstateroot , 'hex' ) ) . toString ( 'hex' ) ;
39
+ this . finalsaplingroothashReversed = util . reverseByteOrder ( Buffer . from ( rpcData . finalsaplingroothash , 'hex' ) ) . toString ( 'hex' ) ;
66
40
}
67
- if ( rpcData . hashutxoroot ) {
68
- this . hashutxorootReversed = util . reverseBuffer ( new Buffer ( rpcData . hashutxoroot , 'hex' ) ) . toString ( 'hex' ) ;
69
- }
70
- this . transactionData = Buffer . concat ( rpcData . transactions . map ( function ( tx ) {
71
- return new Buffer ( tx . data , 'hex' ) ;
72
- } ) ) ;
41
+
42
+ this . transactionData = Buffer . concat ( rpcData . transactions . map ( tx => Buffer . from ( tx . data , 'hex' ) ) ) ;
73
43
this . merkleTree = new merkleTree ( getTransactionBuffers ( rpcData . transactions ) ) ;
74
44
this . merkleBranch = getMerkleHashes ( this . merkleTree . steps ) ;
75
45
this . generationTransaction = transactions . CreateGeneration (
@@ -83,31 +53,23 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, pool
83
53
) ;
84
54
85
55
this . serializeCoinbase = function ( extraNonce1 , extraNonce2 ) {
56
+ const nonce1Buffer = Buffer . isBuffer ( extraNonce1 ) ? extraNonce1 : Buffer . from ( extraNonce1 ) ;
57
+ const nonce2Buffer = Buffer . isBuffer ( extraNonce2 ) ? extraNonce2 : Buffer . from ( extraNonce2 ) ;
58
+
86
59
return Buffer . concat ( [
87
60
this . generationTransaction [ 0 ] ,
88
- extraNonce1 ,
89
- extraNonce2 ,
61
+ nonce1Buffer ,
62
+ nonce2Buffer ,
90
63
this . generationTransaction [ 1 ]
91
64
] ) ;
92
65
} ;
93
66
94
67
95
68
//https://en.bitcoin.it/wiki/Protocol_specification#Block_Headers
96
- this . serializeHeader = function ( merkleRoot , nTime , nonce ) {
97
-
98
- var headerSize ;
99
- if ( rpcData . version == 5 && rpcData . finalsaplingroothash ) {
100
- headerSize = 112 ;
101
- /*} else if (rpcData.hashstateroot && rpcData.hashutxoroot) {
102
- headerSize = 181;
103
- */
104
- } else {
105
- headerSize = 80 ;
106
- }
107
-
108
- var header = new Buffer ( headerSize ) ;
69
+ this . serializeHeader = function ( merkleRoot , nTime , nonce ) {
70
+ var headerSize = rpcData . version == 5 && rpcData . finalsaplingroothash ? 112 : 80 ;
71
+ var header = Buffer . alloc ( headerSize ) ;
109
72
var position = 0 ;
110
-
111
73
/*
112
74
console.log('headerSize:' + headerSize);
113
75
console.log('rpcData.finalsaplingroothash:' + rpcData.finalsaplingroothash);
@@ -120,7 +82,6 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, pool
120
82
console.log('rpcData.previousblockhash: ' + rpcData.previousblockhash);
121
83
console.log('rpcData.version: ' + rpcData.version);
122
84
*/
123
-
124
85
if ( rpcData . version == 5 && rpcData . finalsaplingroothash ) {
125
86
header . write ( rpcData . finalsaplingroothash , position , 32 , 'hex' ) ;
126
87
position += 32 ;
@@ -132,44 +93,41 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, pool
132
93
}
133
94
*/
134
95
header . write ( nonce , position , 4 , 'hex' ) ;
135
- header . write ( rpcData . bits , position += 4 , 4 , 'hex' ) ;
136
- header . write ( nTime , position += 4 , 4 , 'hex' ) ;
137
- header . write ( merkleRoot , position += 4 , 32 , 'hex' ) ;
138
- header . write ( rpcData . previousblockhash , position += 32 , 32 , 'hex' ) ;
96
+ position += 4 ;
97
+ header . write ( rpcData . bits , position , 4 , 'hex' ) ;
98
+ position += 4 ;
99
+ header . write ( nTime , position , 4 , 'hex' ) ;
100
+ position += 4 ;
101
+ header . write ( merkleRoot , position , 32 , 'hex' ) ;
102
+ position += 32 ;
103
+ header . write ( rpcData . previousblockhash , position , 32 , 'hex' ) ;
139
104
header . writeUInt32BE ( rpcData . version , position + 32 ) ;
140
- var header = util . reverseBuffer ( header ) ;
141
- return header ;
105
+ return util . reverseBuffer ( header ) ;
142
106
} ;
143
107
144
- this . serializeBlock = function ( header , coinbase ) {
108
+ this . serializeBlock = function ( header , coinbase ) {
145
109
return Buffer . concat ( [
146
110
header ,
147
-
148
111
util . varIntBuffer ( this . rpcData . transactions . length + 1 ) ,
149
112
coinbase ,
150
113
this . transactionData ,
151
-
152
114
getVoteData ( ) ,
153
-
154
115
//POS coins require a zero byte appended to block which the daemon replaces with the signature
155
- new Buffer ( reward === 'POS' ? [ 0 ] : [ ] )
116
+ Buffer . from ( reward === 'POS' ? [ 0 ] : [ ] )
156
117
] ) ;
157
118
} ;
158
119
159
120
this . registerSubmit = function ( extraNonce1 , extraNonce2 , nTime , nonce ) {
160
- var submission = extraNonce1 + extraNonce2 + nTime + nonce ;
161
- if ( submits . indexOf ( submission ) === - 1 ) {
121
+ const submission = ` ${ extraNonce1 } ${ extraNonce2 } ${ nTime } ${ nonce } ` ;
122
+ if ( ! submits . includes ( submission ) ) {
162
123
submits . push ( submission ) ;
163
124
return true ;
164
125
}
165
126
return false ;
166
127
} ;
167
128
168
129
this . getOdoKey = function ( ) {
169
- if ( this . rpcData && this . rpcData . odokey !== undefined ) {
170
- return this . rpcData . odokey ;
171
- }
172
- return null ;
130
+ return this . rpcData ?. odokey || null ;
173
131
} ;
174
132
175
133
this . getJobParams = function ( ) {
@@ -188,13 +146,7 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, pool
188
146
if ( this . finalsaplingroothashReversed ) {
189
147
this . jobParams . push ( this . finalsaplingroothashReversed ) ;
190
148
}
191
- /*if (this.hashstaterootReversed && this.hashutxorootReversed) {
192
- this.jobParams.push(this.hashstaterootReversed);
193
- this.jobParams.push(this.hashutxorootReversed);
194
- this.jobParams.push('0000000000000000000000000000000000000000000000000000000000000000ffffffff00');
195
- }
196
- */
197
149
}
198
150
return this . jobParams ;
199
151
} ;
200
- } ;
152
+ } ;
0 commit comments