@@ -140,6 +140,8 @@ fn main() {
140
140
test_generate ( & cl) ;
141
141
test_get_balance_generate_to_address ( & cl) ;
142
142
test_get_balances_generate_to_address ( & cl) ;
143
+ test_generate_block_raw_tx ( & cl) ;
144
+ test_generate_block_txid ( & cl) ;
143
145
test_get_best_block_hash ( & cl) ;
144
146
test_get_block_count ( & cl) ;
145
147
test_get_block_hash ( & cl) ;
@@ -277,6 +279,106 @@ fn test_get_balances_generate_to_address(cl: &Client) {
277
279
}
278
280
}
279
281
282
+ fn test_generate_block_raw_tx ( cl : & Client ) {
283
+ let sk = PrivateKey {
284
+ network : Network :: Regtest ,
285
+ inner : secp256k1:: SecretKey :: new ( & mut secp256k1:: rand:: thread_rng ( ) ) ,
286
+ compressed : true ,
287
+ } ;
288
+ let addr = Address :: p2wpkh ( & sk. public_key ( & SECP ) , Network :: Regtest ) . unwrap ( ) ;
289
+
290
+ let options = json:: ListUnspentQueryOptions {
291
+ minimum_amount : Some ( btc ( 2 ) ) ,
292
+ ..Default :: default ( )
293
+ } ;
294
+ let unspent = cl. list_unspent ( Some ( 6 ) , None , None , None , Some ( options) ) . unwrap ( ) ;
295
+ let unspent = unspent. into_iter ( ) . nth ( 0 ) . unwrap ( ) ;
296
+
297
+ let tx = Transaction {
298
+ version : 1 ,
299
+ lock_time : PackedLockTime :: ZERO ,
300
+ input : vec ! [ TxIn {
301
+ previous_output: OutPoint {
302
+ txid: unspent. txid,
303
+ vout: unspent. vout,
304
+ } ,
305
+ sequence: Sequence :: MAX ,
306
+ script_sig: Script :: new( ) ,
307
+ witness: Witness :: new( ) ,
308
+ } ] ,
309
+ output : vec ! [ TxOut {
310
+ value: ( unspent. amount - * FEE ) . to_sat( ) ,
311
+ script_pubkey: addr. script_pubkey( ) ,
312
+ } ] ,
313
+ } ;
314
+
315
+ let input = json:: SignRawTransactionInput {
316
+ txid : unspent. txid ,
317
+ vout : unspent. vout ,
318
+ script_pub_key : unspent. script_pub_key ,
319
+ redeem_script : None ,
320
+ amount : Some ( unspent. amount ) ,
321
+ } ;
322
+ let res = cl. sign_raw_transaction_with_wallet ( & tx, Some ( & [ input] ) , None ) . unwrap ( ) ;
323
+ assert ! ( res. complete) ;
324
+
325
+ let raw_tx = bitcoincore_rpc:: MineableTx :: RawTx ( res. transaction ( ) . unwrap ( ) ) ;
326
+ let result = cl. generate_block ( & cl. get_new_address ( None , None ) . unwrap ( ) , & [ raw_tx] ) . unwrap ( ) ;
327
+ let tip = cl. get_best_block_hash ( ) . unwrap ( ) ;
328
+ assert_eq ! ( result. hash, tip) ;
329
+ }
330
+
331
+ fn test_generate_block_txid ( cl : & Client ) {
332
+ let sk = PrivateKey {
333
+ network : Network :: Regtest ,
334
+ inner : secp256k1:: SecretKey :: new ( & mut secp256k1:: rand:: thread_rng ( ) ) ,
335
+ compressed : true ,
336
+ } ;
337
+ let addr = Address :: p2wpkh ( & sk. public_key ( & SECP ) , Network :: Regtest ) . unwrap ( ) ;
338
+
339
+ let options = json:: ListUnspentQueryOptions {
340
+ minimum_amount : Some ( btc ( 2 ) ) ,
341
+ ..Default :: default ( )
342
+ } ;
343
+ let unspent = cl. list_unspent ( Some ( 6 ) , None , None , None , Some ( options) ) . unwrap ( ) ;
344
+ let unspent = unspent. into_iter ( ) . nth ( 0 ) . unwrap ( ) ;
345
+
346
+ let tx = Transaction {
347
+ version : 1 ,
348
+ lock_time : PackedLockTime :: ZERO ,
349
+ input : vec ! [ TxIn {
350
+ previous_output: OutPoint {
351
+ txid: unspent. txid,
352
+ vout: unspent. vout,
353
+ } ,
354
+ sequence: Sequence :: MAX ,
355
+ script_sig: Script :: new( ) ,
356
+ witness: Witness :: new( ) ,
357
+ } ] ,
358
+ output : vec ! [ TxOut {
359
+ value: ( unspent. amount - * FEE ) . to_sat( ) ,
360
+ script_pubkey: addr. script_pubkey( ) ,
361
+ } ] ,
362
+ } ;
363
+
364
+ let input = json:: SignRawTransactionInput {
365
+ txid : unspent. txid ,
366
+ vout : unspent. vout ,
367
+ script_pub_key : unspent. script_pub_key ,
368
+ redeem_script : None ,
369
+ amount : Some ( unspent. amount ) ,
370
+ } ;
371
+ let res = cl. sign_raw_transaction_with_wallet ( & tx, Some ( & [ input] ) , None ) . unwrap ( ) ;
372
+ assert ! ( res. complete) ;
373
+
374
+ let tx = res. transaction ( ) . unwrap ( ) ;
375
+ let txid = bitcoincore_rpc:: MineableTx :: Txid ( tx. txid ( ) ) ;
376
+ cl. send_raw_transaction ( & tx) . unwrap ( ) ;
377
+ let result = cl. generate_block ( & cl. get_new_address ( None , None ) . unwrap ( ) , & [ txid] ) . unwrap ( ) ;
378
+ let tip = cl. get_best_block_hash ( ) . unwrap ( ) ;
379
+ assert_eq ! ( result. hash, tip) ;
380
+ }
381
+
280
382
fn test_get_best_block_hash ( cl : & Client ) {
281
383
let _ = cl. get_best_block_hash ( ) . unwrap ( ) ;
282
384
}
0 commit comments