@@ -140,6 +140,7 @@ 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 ( & cl) ;
143
144
test_get_best_block_hash ( & cl) ;
144
145
test_get_block_count ( & cl) ;
145
146
test_get_block_hash ( & cl) ;
@@ -277,6 +278,56 @@ fn test_get_balances_generate_to_address(cl: &Client) {
277
278
}
278
279
}
279
280
281
+ fn test_generate_block ( cl : & Client ) {
282
+ let sk = PrivateKey {
283
+ network : Network :: Regtest ,
284
+ inner : secp256k1:: SecretKey :: new ( & mut secp256k1:: rand:: thread_rng ( ) ) ,
285
+ compressed : true ,
286
+ } ;
287
+ let addr = Address :: p2wpkh ( & sk. public_key ( & SECP ) , Network :: Regtest ) . unwrap ( ) ;
288
+
289
+ let options = json:: ListUnspentQueryOptions {
290
+ minimum_amount : Some ( btc ( 2 ) ) ,
291
+ ..Default :: default ( )
292
+ } ;
293
+ let unspent = cl. list_unspent ( Some ( 6 ) , None , None , None , Some ( options) ) . unwrap ( ) ;
294
+ let unspent = unspent. into_iter ( ) . nth ( 0 ) . unwrap ( ) ;
295
+
296
+ let tx = Transaction {
297
+ version : 1 ,
298
+ lock_time : PackedLockTime :: ZERO ,
299
+ input : vec ! [ TxIn {
300
+ previous_output: OutPoint {
301
+ txid: unspent. txid,
302
+ vout: unspent. vout,
303
+ } ,
304
+ sequence: Sequence :: MAX ,
305
+ script_sig: Script :: new( ) ,
306
+ witness: Witness :: new( ) ,
307
+ } ] ,
308
+ output : vec ! [ TxOut {
309
+ value: ( unspent. amount - * FEE ) . to_sat( ) ,
310
+ script_pubkey: addr. script_pubkey( ) ,
311
+ } ] ,
312
+ } ;
313
+
314
+ let input = json:: SignRawTransactionInput {
315
+ txid : unspent. txid ,
316
+ vout : unspent. vout ,
317
+ script_pub_key : unspent. script_pub_key ,
318
+ redeem_script : None ,
319
+ amount : Some ( unspent. amount ) ,
320
+ } ;
321
+ let res = cl. sign_raw_transaction_with_wallet ( & tx, Some ( & [ input] ) , None ) . unwrap ( ) ;
322
+ assert ! ( res. complete) ;
323
+
324
+ let result = cl
325
+ . generate_block ( & cl. get_new_address ( None , None ) . unwrap ( ) , & [ & res. transaction ( ) . unwrap ( ) ] )
326
+ . unwrap ( ) ;
327
+ let tip = cl. get_best_block_hash ( ) . unwrap ( ) ;
328
+ assert_eq ! ( result. hash, tip) ;
329
+ }
330
+
280
331
fn test_get_best_block_hash ( cl : & Client ) {
281
332
let _ = cl. get_best_block_hash ( ) . unwrap ( ) ;
282
333
}
0 commit comments