Skip to content

Commit 5d02b94

Browse files
committed
test: add assertions for TxOut
1 parent 0e1160e commit 5d02b94

File tree

1 file changed

+69
-6
lines changed

1 file changed

+69
-6
lines changed

testnet/stacks-node/src/burnchains/bitcoin_regtest_controller.rs

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2852,6 +2852,8 @@ mod tests {
28522852
mod utils {
28532853
use std::net::TcpListener;
28542854

2855+
use stacks::burnchains::MagicBytes;
2856+
28552857
use super::*;
28562858
use crate::tests::bitcoin_regtest::BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
28572859
use crate::util::get_epoch_time_nanos;
@@ -2935,6 +2937,35 @@ mod tests {
29352937
burn_header_hash: BurnchainHeaderHash([0x01; 32]),
29362938
}
29372939
}
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+
}
29382969
}
29392970

29402971
#[test]
@@ -3360,7 +3391,7 @@ mod tests {
33603391
.start_bitcoind()
33613392
.expect("bitcoind should be started!");
33623393

3363-
let mut btc_controller = BitcoinRegtestController::new(config, None);
3394+
let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
33643395
btc_controller
33653396
.connect_dbs()
33663397
.expect("Dbs initialization required!");
@@ -3369,13 +3400,27 @@ mod tests {
33693400
let commit_op = utils::create_commit_op();
33703401

33713402
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)
33733404
.expect("Build leader block commit should work");
33743405

33753406
assert_eq!(1, tx.version);
33763407
assert_eq!(0, tx.lock_time);
33773408
assert_eq!(1, tx.input.len());
33783409
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]);
33793424
}
33803425

33813426
#[test]
@@ -3492,7 +3537,7 @@ mod tests {
34923537
.start_bitcoind()
34933538
.expect("bitcoind should be started!");
34943539

3495-
let mut btc_controller = BitcoinRegtestController::new(config, None);
3540+
let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
34963541
btc_controller
34973542
.connect_dbs()
34983543
.expect("Dbs initialization required!");
@@ -3516,13 +3561,22 @@ mod tests {
35163561
commit_op.burn_fee += 10;
35173562

35183563
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)
35203565
.expect("Commit tx should be rbf-ed");
35213566

35223567
assert_eq!(1, rbf_tx.version);
35233568
assert_eq!(0, rbf_tx.lock_time);
35243569
assert_eq!(1, rbf_tx.input.len());
35253570
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]);
35263580
}
35273581

35283582
#[test]
@@ -3543,7 +3597,7 @@ mod tests {
35433597
.start_bitcoind()
35443598
.expect("bitcoind should be started!");
35453599

3546-
let mut btc_controller = BitcoinRegtestController::new(config, None);
3600+
let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
35473601
btc_controller
35483602
.connect_dbs()
35493603
.expect("Dbs initialization required!");
@@ -3567,12 +3621,21 @@ mod tests {
35673621
commit_op.burn_fee += 10;
35683622

35693623
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)
35713625
.expect("Commit tx should be rbf-ed");
35723626

35733627
assert_eq!(1, rbf_tx.version);
35743628
assert_eq!(0, rbf_tx.lock_time);
35753629
assert_eq!(1, rbf_tx.input.len());
35763630
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]);
35773640
}
35783641
}

0 commit comments

Comments
 (0)