@@ -204,18 +204,18 @@ fn new_extended_job(
204
204
. try_into ( )
205
205
. map_err ( |_| Error :: TxVersionTooBig ) ?;
206
206
207
- let bip34_bytes = get_bip_34_bytes ( new_template, tx_version ) ? ;
208
- let script_prefix_len = bip34_bytes . len ( ) + additional_coinbase_script_data. len ( ) ;
207
+ let script_sig_prefix = new_template. coinbase_prefix . to_vec ( ) ;
208
+ let script_sig_prefix_len = script_sig_prefix . len ( ) + additional_coinbase_script_data. len ( ) ;
209
209
210
210
let coinbase = coinbase (
211
- bip34_bytes ,
211
+ script_sig_prefix ,
212
212
tx_version,
213
213
new_template. coinbase_tx_locktime ,
214
214
new_template. coinbase_tx_input_sequence ,
215
215
coinbase_outputs,
216
216
additional_coinbase_script_data,
217
217
extranonce_len,
218
- ) ;
218
+ ) ? ;
219
219
220
220
let min_ntime = binary_sv2:: Sv2Option :: new ( if new_template. future_template {
221
221
None
@@ -230,8 +230,8 @@ fn new_extended_job(
230
230
version : new_template. version ,
231
231
version_rolling_allowed,
232
232
merkle_path : new_template. merkle_path . clone ( ) . into_static ( ) ,
233
- coinbase_tx_prefix : coinbase_tx_prefix ( & coinbase, script_prefix_len ) ?,
234
- coinbase_tx_suffix : coinbase_tx_suffix ( & coinbase, extranonce_len, script_prefix_len ) ?,
233
+ coinbase_tx_prefix : coinbase_tx_prefix ( & coinbase, script_sig_prefix_len ) ?,
234
+ coinbase_tx_suffix : coinbase_tx_suffix ( & coinbase, extranonce_len, script_sig_prefix_len ) ?,
235
235
} ;
236
236
237
237
debug ! (
@@ -245,12 +245,12 @@ fn new_extended_job(
245
245
// so the extranonce search space can be introduced
246
246
fn coinbase_tx_prefix (
247
247
coinbase : & Transaction ,
248
- script_prefix_len : usize ,
248
+ script_sig_prefix_len : usize ,
249
249
) -> Result < B064K < ' static > , Error > {
250
250
let encoded = consensus:: serialize ( coinbase) ;
251
251
// If script_prefix_len is not 0 we are not in a test environment and the coinbase will have the
252
252
// 0 witness
253
- let segwit_bytes = match script_prefix_len {
253
+ let segwit_bytes = match script_sig_prefix_len {
254
254
0 => 0 ,
255
255
_ => 2 ,
256
256
} ;
@@ -260,7 +260,7 @@ fn coinbase_tx_prefix(
260
260
+ 32 // prev OutPoint
261
261
+ 4 // index
262
262
+ 1 // bytes in script TODO can be also 3
263
- + script_prefix_len ; // bip34_bytes
263
+ + script_sig_prefix_len ; // script_sig_prefix
264
264
let r = encoded[ 0 ..index] . to_vec ( ) ;
265
265
r. try_into ( ) . map_err ( Error :: BinarySv2Error )
266
266
}
@@ -270,12 +270,12 @@ fn coinbase_tx_prefix(
270
270
fn coinbase_tx_suffix (
271
271
coinbase : & Transaction ,
272
272
extranonce_len : u8 ,
273
- script_prefix_len : usize ,
273
+ script_sig_prefix_len : usize ,
274
274
) -> Result < B064K < ' static > , Error > {
275
275
let encoded = consensus:: serialize ( coinbase) ;
276
- // If script_prefix_len is not 0 we are not in a test environment and the coinbase have the 0
277
- // witness
278
- let segwit_bytes = match script_prefix_len {
276
+ // If script_sig_prefix_len is not 0 we are not in a test environment and the coinbase have the
277
+ // 0 witness
278
+ let segwit_bytes = match script_sig_prefix_len {
279
279
0 => 0 ,
280
280
_ => 2 ,
281
281
} ;
@@ -285,78 +285,43 @@ fn coinbase_tx_suffix(
285
285
+ 32 // prev OutPoint
286
286
+ 4 // index
287
287
+ 1 // bytes in script TODO can be also 3
288
- + script_prefix_len // bip34_bytes
288
+ + script_sig_prefix_len // script_sig_prefix
289
289
+ ( extranonce_len as usize ) ..]
290
290
. to_vec ( ) ;
291
291
r. try_into ( ) . map_err ( Error :: BinarySv2Error )
292
292
}
293
293
294
- // Just double check if received coinbase_prefix is the right one can be removed or used only for
295
- // tests
296
- fn get_bip_34_bytes ( new_template : & NewTemplate , tx_version : i32 ) -> Result < Vec < u8 > , Error > {
297
- #[ cfg( test) ]
298
- if tx_version == 1 {
299
- return Ok ( vec ! [ ] ) ;
300
- } ;
301
-
302
- let script_prefix = & new_template. coinbase_prefix . to_vec ( ) [ ..] ;
303
-
304
- // Is ok to panic here cause condition will be always true when not in a test chain
305
- // (regtest ecc ecc)
306
- #[ cfg( not( test) ) ]
307
- assert ! (
308
- script_prefix. len( ) > 2 ,
309
- "Bitcoin blockchain should be at least 16 block long"
310
- ) ;
311
-
312
- // Txs version lower or equal to 1 are not allowed in new blocks we need it only to test the
313
- // JobCreator against old bitcoin blocks
314
- #[ cfg( not( test) ) ]
315
- if tx_version <= 1 {
316
- return Err ( Error :: TxVersionTooLow ) ;
317
- } ;
318
-
319
- // add 1 cause 0 is push 1 2 is 1 is push 2 ecc ecc
320
- // add 1 cause in the len there is also the op code itself
321
- let bip34_len = script_prefix[ 0 ] as usize + 2 ;
322
- if bip34_len == script_prefix. len ( ) {
323
- Ok ( script_prefix[ 0 ..bip34_len] . to_vec ( ) )
324
- } else {
325
- Err ( Error :: InvalidBip34Bytes ( script_prefix. to_vec ( ) ) )
326
- }
327
- }
328
-
329
- // coinbase_tx_input_script_prefix: extranonce prefix (script length + bip34 block height) provided
330
- // by the node It assume that NewTemplate.coinbase_tx_outputs == 0
294
+ // try to build a Transaction coinbase
331
295
fn coinbase (
332
- mut bip34_bytes : Vec < u8 > ,
296
+ script_sig_prefix : Vec < u8 > ,
333
297
version : i32 ,
334
298
lock_time : u32 ,
335
299
sequence : u32 ,
336
300
coinbase_outputs : & [ TxOut ] ,
337
301
additional_coinbase_script_data : Vec < u8 > ,
338
302
extranonce_len : u8 ,
339
- ) -> Transaction {
340
- // If script_prefix_len is not 0 we are not in a test environment and the coinbase have the 0
341
- // witness
342
- let witness = match bip34_bytes . len ( ) {
303
+ ) -> Result < Transaction , Error > {
304
+ // If script_sig_prefix_len is not 0 we are not in a test environment and the coinbase have the
305
+ // 0 witness
306
+ let witness = match script_sig_prefix . len ( ) {
343
307
0 => Witness :: from ( vec ! [ ] as Vec < Vec < u8 > > ) ,
344
308
_ => Witness :: from ( vec ! [ vec![ 0 ; 32 ] ] ) ,
345
309
} ;
346
- bip34_bytes. extend_from_slice ( & additional_coinbase_script_data) ;
347
- bip34_bytes. extend_from_slice ( & vec ! [ 0 ; extranonce_len as usize ] ) ;
310
+ let mut script_sig = script_sig_prefix;
311
+ script_sig. extend_from_slice ( & additional_coinbase_script_data) ;
312
+ script_sig. extend_from_slice ( & vec ! [ 0 ; extranonce_len as usize ] ) ;
348
313
let tx_in = TxIn {
349
314
previous_output : OutPoint :: null ( ) ,
350
- script_sig : bip34_bytes . into ( ) ,
315
+ script_sig : script_sig . into ( ) ,
351
316
sequence : bitcoin:: Sequence ( sequence) ,
352
317
witness,
353
318
} ;
354
- Transaction {
319
+ Ok ( Transaction {
355
320
version : Version :: non_standard ( version) ,
356
321
lock_time : LockTime :: from_consensus ( lock_time) ,
357
322
input : vec ! [ tx_in] ,
358
323
output : coinbase_outputs. to_vec ( ) ,
359
- }
324
+ } )
360
325
}
361
326
362
327
/// Helper type to strip a segwit data from the coinbase_tx_prefix and coinbase_tx_suffix
@@ -428,7 +393,7 @@ impl StrippedCoinbaseTx {
428
393
}
429
394
430
395
// The coinbase tx prefix is the LE bytes concatenation of the tx version and all
431
- // of the tx inputs minus the 32 bytes after the bip34 bytes in the script
396
+ // of the tx inputs minus the 32 bytes after the script_sig_prefix bytes
432
397
// and the last input's sequence (used as the first entry in the coinbase tx suffix).
433
398
// The last 32 bytes after the bip34 bytes in the script will be used to allow extranonce
434
399
// space for the miner. We remove the bip141 marker and flag since it is only used for
@@ -493,13 +458,13 @@ pub mod tests {
493
458
let mut coinbase_prefix_gen = Gen :: new ( 255 ) ;
494
459
let mut coinbase_prefix: vec:: Vec < u8 > = vec:: Vec :: new ( ) ;
495
460
496
- let max_num_for_script_prefix = 253 ;
461
+ let max_num_for_script_sig_prefix = 253 ;
497
462
let prefix_len = cmp:: min ( u8:: arbitrary ( & mut coinbase_prefix_gen) , 6 ) ;
498
463
coinbase_prefix. push ( prefix_len) ;
499
464
coinbase_prefix. resize_with ( prefix_len as usize + 2 , || {
500
465
cmp:: min (
501
466
u8:: arbitrary ( & mut coinbase_prefix_gen) ,
502
- max_num_for_script_prefix ,
467
+ max_num_for_script_sig_prefix ,
503
468
)
504
469
} ) ;
505
470
let coinbase_prefix: binary_sv2:: B0255 = coinbase_prefix. try_into ( ) . unwrap ( ) ;
@@ -539,8 +504,8 @@ pub mod tests {
539
504
pub fn new_pub_key ( ) -> PublicKey {
540
505
let priv_k = PrivateKey :: from_slice ( & PRIVATE_KEY_BTC , NETWORK ) . unwrap ( ) ;
541
506
let secp = Secp256k1 :: default ( ) ;
542
- let pub_k = PublicKey :: from_private_key ( & secp , & priv_k ) ;
543
- pub_k
507
+
508
+ PublicKey :: from_private_key ( & secp , & priv_k )
544
509
}
545
510
546
511
#[ cfg( feature = "prop_test" ) ]
@@ -698,7 +663,7 @@ pub mod tests {
698
663
encoded. append ( & mut encoded1. clone ( ) ) ;
699
664
encoded. append ( & mut encoded2. clone ( ) ) ;
700
665
let outs = tx_outputs_to_costum_scripts ( & encoded[ ..] ) ;
701
- assert ! ( & outs[ 0 ] == & tx1) ;
666
+ assert ! ( outs[ 0 ] == tx1) ;
702
667
assert ! ( outs[ 1 ] == tx2) ;
703
668
}
704
669
@@ -753,10 +718,10 @@ pub mod tests {
753
718
65 , 241 , 216 , 46 , 82 , 223 , 150 , 0 , 97 , 103 , 2 , 82 , 186 , 233 , 145 , 90 , 210 , 231 , 35 ,
754
719
100 , 107 , 52 , 171 , 233 , 50 , 200 , 0 , 0 , 0 , 0 ,
755
720
] ;
756
- let extranonce = vec ! [ 0_u8 ; 15 ] ; // braiins pool requires 15 bytes for extranonce
721
+ let extranonce = [ 0_u8 ; 15 ] ; // braiins pool requires 15 bytes for extranonce
757
722
encoded. extend_from_slice ( coinbase_prefix) ;
758
723
let mut encoded_clone = encoded. clone ( ) ;
759
- encoded_clone. extend_from_slice ( & extranonce[ .. ] ) ;
724
+ encoded_clone. extend_from_slice ( & extranonce) ;
760
725
encoded_clone. extend_from_slice ( coinbase_suffix) ;
761
726
// let mut i = 1;
762
727
// while let Err(_) = Transaction::deserialize(&encoded_clone) {
0 commit comments