Skip to content

Commit 12057c5

Browse files
committed
test: add assertions for TxOut
1 parent c6e6bcd commit 12057c5

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
@@ -2841,6 +2841,8 @@ mod tests {
28412841
mod utils {
28422842
use std::net::TcpListener;
28432843

2844+
use stacks::burnchains::MagicBytes;
2845+
28442846
use super::*;
28452847
use crate::tests::bitcoin_regtest::BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
28462848
use crate::util::get_epoch_time_nanos;
@@ -2924,6 +2926,35 @@ mod tests {
29242926
burn_header_hash: BurnchainHeaderHash([0x01; 32]),
29252927
}
29262928
}
2929+
2930+
pub fn txout_opreturn(op: &LeaderBlockCommitOp, magic: &MagicBytes) -> TxOut {
2931+
let op_bytes = {
2932+
let mut buffer = vec![];
2933+
let mut magic_bytes = magic.as_bytes().to_vec();
2934+
buffer.append(&mut magic_bytes);
2935+
op.consensus_serialize(&mut buffer)
2936+
.expect("FATAL: invalid operation");
2937+
buffer
2938+
};
2939+
2940+
TxOut {
2941+
value: op.sunset_burn,
2942+
script_pubkey: Builder::new()
2943+
.push_opcode(opcodes::All::OP_RETURN)
2944+
.push_slice(&op_bytes)
2945+
.into_script(),
2946+
}
2947+
}
2948+
2949+
pub fn txout_opdup_commit_to(addr: &PoxAddress, amount: u64) -> TxOut {
2950+
addr.to_bitcoin_tx_out(amount)
2951+
}
2952+
2953+
pub fn txout_opdup_change_legacy(signer: &mut BurnchainOpSigner, amount: u64) -> TxOut {
2954+
let public_key = signer.get_public_key();
2955+
let change_address_hash = Hash160::from_data(&public_key.to_bytes());
2956+
LegacyBitcoinAddress::to_p2pkh_tx_out(&change_address_hash, amount)
2957+
}
29272958
}
29282959

29292960
#[test]
@@ -3349,7 +3380,7 @@ mod tests {
33493380
.start_bitcoind()
33503381
.expect("bitcoind should be started!");
33513382

3352-
let mut btc_controller = BitcoinRegtestController::new(config, None);
3383+
let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
33533384
btc_controller
33543385
.connect_dbs()
33553386
.expect("Dbs initialization required!");
@@ -3358,13 +3389,27 @@ mod tests {
33583389
let commit_op = utils::create_commit_op();
33593390

33603391
let tx = btc_controller
3361-
.build_leader_block_commit_tx(StacksEpochId::Epoch31, commit_op, &mut signer, 0)
3392+
.build_leader_block_commit_tx(StacksEpochId::Epoch31, commit_op.clone(), &mut signer, 0)
33623393
.expect("Build leader block commit should work");
33633394

33643395
assert_eq!(1, tx.version);
33653396
assert_eq!(0, tx.lock_time);
33663397
assert_eq!(1, tx.input.len());
33673398
assert_eq!(4, tx.output.len());
3399+
3400+
//TODO: Try to implement assertion for TxIn
3401+
//let utxos = btc_controller.get_all_utxos(&miner_pubkey);
3402+
//let input_0 = utils::txin_utxo_legacy(&utxos[0])
3403+
//assert_ne!(tx.input[0], tx.input[0]);
3404+
3405+
let op_return = utils::txout_opreturn(&commit_op, &config.burnchain.magic_bytes);
3406+
let op_commit_1 = utils::txout_opdup_commit_to(&commit_op.commit_outs[0], 55_000);
3407+
let op_commit_2 = utils::txout_opdup_commit_to(&commit_op.commit_outs[1], 55_000);
3408+
let op_change = utils::txout_opdup_change_legacy(&mut signer, 4_999_865_300);
3409+
assert_eq!(op_return, tx.output[0]);
3410+
assert_eq!(op_commit_1, tx.output[1]);
3411+
assert_eq!(op_commit_2, tx.output[2]);
3412+
assert_eq!(op_change, tx.output[3]);
33683413
}
33693414

33703415
#[test]
@@ -3481,7 +3526,7 @@ mod tests {
34813526
.start_bitcoind()
34823527
.expect("bitcoind should be started!");
34833528

3484-
let mut btc_controller = BitcoinRegtestController::new(config, None);
3529+
let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
34853530
btc_controller
34863531
.connect_dbs()
34873532
.expect("Dbs initialization required!");
@@ -3505,13 +3550,22 @@ mod tests {
35053550
commit_op.burn_fee += 10;
35063551

35073552
let rbf_tx = btc_controller
3508-
.build_leader_block_commit_tx(StacksEpochId::Epoch31, commit_op, &mut signer, 0)
3553+
.build_leader_block_commit_tx(StacksEpochId::Epoch31, commit_op.clone(), &mut signer, 0)
35093554
.expect("Commit tx should be rbf-ed");
35103555

35113556
assert_eq!(1, rbf_tx.version);
35123557
assert_eq!(0, rbf_tx.lock_time);
35133558
assert_eq!(1, rbf_tx.input.len());
35143559
assert_eq!(4, rbf_tx.output.len());
3560+
3561+
let op_return = utils::txout_opreturn(&commit_op, &config.burnchain.magic_bytes);
3562+
let op_commit_1 = utils::txout_opdup_commit_to(&commit_op.commit_outs[0], 55_005);
3563+
let op_commit_2 = utils::txout_opdup_commit_to(&commit_op.commit_outs[1], 55_005);
3564+
let op_change = utils::txout_opdup_change_legacy(&mut signer, 4_999_730_590);
3565+
assert_eq!(op_return, rbf_tx.output[0]);
3566+
assert_eq!(op_commit_1, rbf_tx.output[1]);
3567+
assert_eq!(op_commit_2, rbf_tx.output[2]);
3568+
assert_eq!(op_change, rbf_tx.output[3]);
35153569
}
35163570

35173571
#[test]
@@ -3532,7 +3586,7 @@ mod tests {
35323586
.start_bitcoind()
35333587
.expect("bitcoind should be started!");
35343588

3535-
let mut btc_controller = BitcoinRegtestController::new(config, None);
3589+
let mut btc_controller = BitcoinRegtestController::new(config.clone(), None);
35363590
btc_controller
35373591
.connect_dbs()
35383592
.expect("Dbs initialization required!");
@@ -3556,12 +3610,21 @@ mod tests {
35563610
commit_op.burn_fee += 10;
35573611

35583612
let rbf_tx = btc_controller
3559-
.build_leader_block_commit_tx(StacksEpochId::Epoch31, commit_op, &mut signer, 0)
3613+
.build_leader_block_commit_tx(StacksEpochId::Epoch31, commit_op.clone(), &mut signer, 0)
35603614
.expect("Commit tx should be rbf-ed");
35613615

35623616
assert_eq!(1, rbf_tx.version);
35633617
assert_eq!(0, rbf_tx.lock_time);
35643618
assert_eq!(1, rbf_tx.input.len());
35653619
assert_eq!(4, rbf_tx.output.len());
3620+
3621+
let op_return = utils::txout_opreturn(&commit_op, &config.burnchain.magic_bytes);
3622+
let op_commit_1 = utils::txout_opdup_commit_to(&commit_op.commit_outs[0], 55_005);
3623+
let op_commit_2 = utils::txout_opdup_commit_to(&commit_op.commit_outs[1], 55_005);
3624+
let op_change = utils::txout_opdup_change_legacy(&mut signer, 4_999_730_590);
3625+
assert_eq!(op_return, rbf_tx.output[0]);
3626+
assert_eq!(op_commit_1, rbf_tx.output[1]);
3627+
assert_eq!(op_commit_2, rbf_tx.output[2]);
3628+
assert_eq!(op_change, rbf_tx.output[3]);
35663629
}
35673630
}

0 commit comments

Comments
 (0)