@@ -2852,6 +2852,8 @@ mod tests {
2852
2852
mod utils {
2853
2853
use std:: net:: TcpListener ;
2854
2854
2855
+ use stacks:: burnchains:: MagicBytes ;
2856
+
2855
2857
use super :: * ;
2856
2858
use crate :: tests:: bitcoin_regtest:: BURNCHAIN_CONFIG_PEER_PORT_DISABLED ;
2857
2859
use crate :: util:: get_epoch_time_nanos;
@@ -2935,6 +2937,35 @@ mod tests {
2935
2937
burn_header_hash : BurnchainHeaderHash ( [ 0x01 ; 32 ] ) ,
2936
2938
}
2937
2939
}
2940
+
2941
+ pub fn txout_opreturn ( op : & LeaderBlockCommitOp , magic : & MagicBytes ) -> TxOut {
2942
+ let op_bytes = {
2943
+ let mut buffer = vec ! [ ] ;
2944
+ let mut magic_bytes = magic. as_bytes ( ) . to_vec ( ) ;
2945
+ buffer. append ( & mut magic_bytes) ;
2946
+ op. consensus_serialize ( & mut buffer)
2947
+ . expect ( "FATAL: invalid operation" ) ;
2948
+ buffer
2949
+ } ;
2950
+
2951
+ TxOut {
2952
+ value : op. sunset_burn ,
2953
+ script_pubkey : Builder :: new ( )
2954
+ . push_opcode ( opcodes:: All :: OP_RETURN )
2955
+ . push_slice ( & op_bytes)
2956
+ . into_script ( ) ,
2957
+ }
2958
+ }
2959
+
2960
+ pub fn txout_opdup_commit_to ( addr : & PoxAddress , amount : u64 ) -> TxOut {
2961
+ addr. to_bitcoin_tx_out ( amount)
2962
+ }
2963
+
2964
+ pub fn txout_opdup_change_legacy ( signer : & mut BurnchainOpSigner , amount : u64 ) -> TxOut {
2965
+ let public_key = signer. get_public_key ( ) ;
2966
+ let change_address_hash = Hash160 :: from_data ( & public_key. to_bytes ( ) ) ;
2967
+ LegacyBitcoinAddress :: to_p2pkh_tx_out ( & change_address_hash, amount)
2968
+ }
2938
2969
}
2939
2970
2940
2971
#[ test]
@@ -3360,7 +3391,7 @@ mod tests {
3360
3391
. start_bitcoind ( )
3361
3392
. expect ( "bitcoind should be started!" ) ;
3362
3393
3363
- let mut btc_controller = BitcoinRegtestController :: new ( config, None ) ;
3394
+ let mut btc_controller = BitcoinRegtestController :: new ( config. clone ( ) , None ) ;
3364
3395
btc_controller
3365
3396
. connect_dbs ( )
3366
3397
. expect ( "Dbs initialization required!" ) ;
@@ -3369,13 +3400,27 @@ mod tests {
3369
3400
let commit_op = utils:: create_commit_op ( ) ;
3370
3401
3371
3402
let tx = btc_controller
3372
- . build_leader_block_commit_tx ( StacksEpochId :: Epoch31 , commit_op, & mut signer, 0 )
3403
+ . build_leader_block_commit_tx ( StacksEpochId :: Epoch31 , commit_op. clone ( ) , & mut signer, 0 )
3373
3404
. expect ( "Build leader block commit should work" ) ;
3374
3405
3375
3406
assert_eq ! ( 1 , tx. version) ;
3376
3407
assert_eq ! ( 0 , tx. lock_time) ;
3377
3408
assert_eq ! ( 1 , tx. input. len( ) ) ;
3378
3409
assert_eq ! ( 4 , tx. output. len( ) ) ;
3410
+
3411
+ //TODO: Try to implement assertion for TxIn
3412
+ //let utxos = btc_controller.get_all_utxos(&miner_pubkey);
3413
+ //let input_0 = utils::txin_utxo_legacy(&utxos[0])
3414
+ //assert_ne!(tx.input[0], tx.input[0]);
3415
+
3416
+ let op_return = utils:: txout_opreturn ( & commit_op, & config. burnchain . magic_bytes ) ;
3417
+ let op_commit_1 = utils:: txout_opdup_commit_to ( & commit_op. commit_outs [ 0 ] , 55_000 ) ;
3418
+ let op_commit_2 = utils:: txout_opdup_commit_to ( & commit_op. commit_outs [ 1 ] , 55_000 ) ;
3419
+ let op_change = utils:: txout_opdup_change_legacy ( & mut signer, 4_999_865_300 ) ;
3420
+ assert_eq ! ( op_return, tx. output[ 0 ] ) ;
3421
+ assert_eq ! ( op_commit_1, tx. output[ 1 ] ) ;
3422
+ assert_eq ! ( op_commit_2, tx. output[ 2 ] ) ;
3423
+ assert_eq ! ( op_change, tx. output[ 3 ] ) ;
3379
3424
}
3380
3425
3381
3426
#[ test]
@@ -3492,7 +3537,7 @@ mod tests {
3492
3537
. start_bitcoind ( )
3493
3538
. expect ( "bitcoind should be started!" ) ;
3494
3539
3495
- let mut btc_controller = BitcoinRegtestController :: new ( config, None ) ;
3540
+ let mut btc_controller = BitcoinRegtestController :: new ( config. clone ( ) , None ) ;
3496
3541
btc_controller
3497
3542
. connect_dbs ( )
3498
3543
. expect ( "Dbs initialization required!" ) ;
@@ -3516,13 +3561,22 @@ mod tests {
3516
3561
commit_op. burn_fee += 10 ;
3517
3562
3518
3563
let rbf_tx = btc_controller
3519
- . build_leader_block_commit_tx ( StacksEpochId :: Epoch31 , commit_op, & mut signer, 0 )
3564
+ . build_leader_block_commit_tx ( StacksEpochId :: Epoch31 , commit_op. clone ( ) , & mut signer, 0 )
3520
3565
. expect ( "Commit tx should be rbf-ed" ) ;
3521
3566
3522
3567
assert_eq ! ( 1 , rbf_tx. version) ;
3523
3568
assert_eq ! ( 0 , rbf_tx. lock_time) ;
3524
3569
assert_eq ! ( 1 , rbf_tx. input. len( ) ) ;
3525
3570
assert_eq ! ( 4 , rbf_tx. output. len( ) ) ;
3571
+
3572
+ let op_return = utils:: txout_opreturn ( & commit_op, & config. burnchain . magic_bytes ) ;
3573
+ let op_commit_1 = utils:: txout_opdup_commit_to ( & commit_op. commit_outs [ 0 ] , 55_005 ) ;
3574
+ let op_commit_2 = utils:: txout_opdup_commit_to ( & commit_op. commit_outs [ 1 ] , 55_005 ) ;
3575
+ let op_change = utils:: txout_opdup_change_legacy ( & mut signer, 4_999_730_590 ) ;
3576
+ assert_eq ! ( op_return, rbf_tx. output[ 0 ] ) ;
3577
+ assert_eq ! ( op_commit_1, rbf_tx. output[ 1 ] ) ;
3578
+ assert_eq ! ( op_commit_2, rbf_tx. output[ 2 ] ) ;
3579
+ assert_eq ! ( op_change, rbf_tx. output[ 3 ] ) ;
3526
3580
}
3527
3581
3528
3582
#[ test]
@@ -3543,7 +3597,7 @@ mod tests {
3543
3597
. start_bitcoind ( )
3544
3598
. expect ( "bitcoind should be started!" ) ;
3545
3599
3546
- let mut btc_controller = BitcoinRegtestController :: new ( config, None ) ;
3600
+ let mut btc_controller = BitcoinRegtestController :: new ( config. clone ( ) , None ) ;
3547
3601
btc_controller
3548
3602
. connect_dbs ( )
3549
3603
. expect ( "Dbs initialization required!" ) ;
@@ -3567,12 +3621,21 @@ mod tests {
3567
3621
commit_op. burn_fee += 10 ;
3568
3622
3569
3623
let rbf_tx = btc_controller
3570
- . build_leader_block_commit_tx ( StacksEpochId :: Epoch31 , commit_op, & mut signer, 0 )
3624
+ . build_leader_block_commit_tx ( StacksEpochId :: Epoch31 , commit_op. clone ( ) , & mut signer, 0 )
3571
3625
. expect ( "Commit tx should be rbf-ed" ) ;
3572
3626
3573
3627
assert_eq ! ( 1 , rbf_tx. version) ;
3574
3628
assert_eq ! ( 0 , rbf_tx. lock_time) ;
3575
3629
assert_eq ! ( 1 , rbf_tx. input. len( ) ) ;
3576
3630
assert_eq ! ( 4 , rbf_tx. output. len( ) ) ;
3631
+
3632
+ let op_return = utils:: txout_opreturn ( & commit_op, & config. burnchain . magic_bytes ) ;
3633
+ let op_commit_1 = utils:: txout_opdup_commit_to ( & commit_op. commit_outs [ 0 ] , 55_005 ) ;
3634
+ let op_commit_2 = utils:: txout_opdup_commit_to ( & commit_op. commit_outs [ 1 ] , 55_005 ) ;
3635
+ let op_change = utils:: txout_opdup_change_legacy ( & mut signer, 4_999_730_590 ) ;
3636
+ assert_eq ! ( op_return, rbf_tx. output[ 0 ] ) ;
3637
+ assert_eq ! ( op_commit_1, rbf_tx. output[ 1 ] ) ;
3638
+ assert_eq ! ( op_commit_2, rbf_tx. output[ 2 ] ) ;
3639
+ assert_eq ! ( op_change, rbf_tx. output[ 3 ] ) ;
3577
3640
}
3578
3641
}
0 commit comments