Skip to content

Commit 06dfee9

Browse files
authored
Merge pull request #6 from 0xZaddyy/add-missing-assertions
Add proper assertions to incomplete tests
2 parents 4dc6e82 + 58b53c1 commit 06dfee9

4 files changed

Lines changed: 113 additions & 20 deletions

File tree

src/lib.rs

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ impl SimulationResult {
732732
self.total_payment_obligations
733733
}
734734

735-
pub fn precentage_of_payment_obligations_missed(&self) -> f64 {
735+
pub fn percentage_of_payment_obligations_missed(&self) -> f64 {
736736
let total_payment_obligations = self.total_payment_obligations();
737737
self.missed_payment_obligations
738738
.iter()
@@ -786,22 +786,20 @@ mod tests {
786786
let result = sim.run();
787787
sim.assert_invariants();
788788

789-
println!("{}", sim);
790-
result.save_tx_graph("graph.svg");
791-
// Lets check the simulation state after the run
792-
// Specifically how many payment obligations we're missed
793-
// And how many were created in a cospend
794-
println!(
795-
"Total payment obligations: {}",
796-
result.total_payment_obligations()
789+
// Assert simulation completed successfully
790+
assert!(
791+
result.total_payment_obligations() > 0,
792+
"Simulation should create payment obligations"
797793
);
798-
println!(
799-
"Missed payment obligations: {:?}",
800-
result.missed_payment_obligations
794+
assert!(
795+
result.percentage_of_payment_obligations_missed() < 1.0,
796+
"Not all obligations should be missed"
801797
);
802-
println!(
803-
"Missed payment obligations percentage: {:?}",
804-
result.precentage_of_payment_obligations_missed()
798+
799+
assert_eq!(
800+
result.percentage_of_payment_obligations_missed(),
801+
0.5384615384615384,
802+
"With seed 42, missed percentage should be deterministic"
805803
);
806804
}
807805

@@ -983,8 +981,33 @@ mod tests {
983981

984982
assert!(bob.with(&sim).info().received_transactions.contains(&spend));
985983

986-
// TODO mine another block, check wallet utxos, et
984+
// Mine another block to confirm the transaction
985+
let miner_addr = alice.with_mut(&mut sim).new_address();
986+
let block_bx = BroadcastSetHandleMut {
987+
id: BroadcastSetId(sim.broadcast_set_data.len() - 1),
988+
sim: &mut sim,
989+
};
990+
991+
let _block = block_bx
992+
.construct_block_template(Weight::MAX_BLOCK)
993+
.mine(miner_addr, &mut sim);
994+
995+
sim.assert_invariants();
987996

988-
println!("{:?}", sim);
997+
// Verify transaction is now confirmed and UTXOs are updated
998+
assert!(alice
999+
.with(&sim)
1000+
.info()
1001+
.confirmed_utxos
1002+
.contains(&spend.with(&sim).outpoints().nth(1).unwrap()));
1003+
assert!(bob
1004+
.with(&sim)
1005+
.info()
1006+
.confirmed_utxos
1007+
.contains(&spend.with(&sim).outpoints().nth(0).unwrap()));
1008+
1009+
// Verify the spend transaction is no longer unconfirmed
1010+
assert!(alice.with(&sim).info().unconfirmed_txos.is_empty());
1011+
assert!(bob.with(&sim).info().unconfirmed_txos.is_empty());
9891012
}
9901013
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ fn main() {
2828
);
2929
println!(
3030
"Missed payment obligations percentage: {:?}",
31-
result.precentage_of_payment_obligations_missed()
31+
result.percentage_of_payment_obligations_missed()
3232
);
3333
}

src/transaction.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::wallet::{AddressHandle, AddressId, WalletHandle, WalletHandleMut, WalletId};
22
use crate::Simulation;
33
use bitcoin::consensus::Decodable;
4+
use bitcoin::hashes::Hash;
45
use bitcoin::transaction::{predict_weight, InputWeightPrediction};
56
use bitcoin::{Amount, Weight};
67
use bitcoin::{FeeRate, ScriptBuf, WitnessProgram};
@@ -29,6 +30,16 @@ impl From<TxId> for bitcoin::Txid {
2930
}
3031
}
3132

33+
impl From<bitcoin::Txid> for TxId {
34+
fn from(bitcoin_txid: bitcoin::Txid) -> Self {
35+
let bytes = bitcoin_txid.as_byte_array();
36+
// Extract first 8 bytes and convert to usize (little endian)
37+
let mut txid_bytes = [0u8; 8];
38+
txid_bytes.copy_from_slice(&bytes[..8]);
39+
TxId(u64::from_le_bytes(txid_bytes) as usize)
40+
}
41+
}
42+
3243
// TODO rename to OutputId?
3344
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
3445
pub(crate) struct Outpoint {
@@ -296,11 +307,19 @@ impl TxInfo {
296307

297308
#[cfg(test)]
298309
mod tests {
310+
use bitcoin::hashes::Hash;
311+
299312
use super::*;
300313

301314
#[test]
302315
fn test_txid_encoding() {
303316
let txid = TxId(1);
304-
let txid_from_bytes = bitcoin::Txid::from(txid);
317+
let bitcoin_txid = bitcoin::Txid::from(txid);
318+
319+
assert_eq!(bitcoin_txid.as_byte_array()[0], 1);
320+
assert_eq!(bitcoin_txid.to_byte_array()[1..], [0u8; 31]);
321+
322+
let converted_back = TxId::from(bitcoin_txid);
323+
assert_eq!(converted_back, txid);
305324
}
306325
}

src/tx_contruction.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,59 @@ mod tests {
382382
let txdata = sent_ready
383383
.have_enough_ready_to_sign()
384384
.expect("should have enough ready to sign");
385-
// TODO: assert the inputs and outputs are correct
385+
386+
// Verify input composition (2 from template + 2 from others)
386387
assert_eq!(txdata.inputs.len(), 4);
388+
389+
// Collect all TxIds from inputs
390+
let input_txids: Vec<usize> = txdata
391+
.inputs
392+
.iter()
393+
.map(|input| input.outpoint.txid.0)
394+
.collect();
395+
396+
// Should have template inputs (TxId(0) and TxId(1))
397+
assert!(
398+
input_txids.contains(&0),
399+
"Should contain template input TxId(0)"
400+
);
401+
assert!(
402+
input_txids.contains(&1),
403+
"Should contain template input TxId(1)"
404+
);
405+
406+
// Should have other participant inputs (TxId(100) and TxId(101))
407+
assert!(
408+
input_txids.contains(&100),
409+
"Should contain other participant input TxId(100)"
410+
);
411+
assert!(
412+
input_txids.contains(&101),
413+
"Should contain other participant input TxId(101)"
414+
);
415+
416+
// Verify output composition (1 from template + 2 from others)
387417
assert_eq!(txdata.outputs.len(), 3);
418+
419+
// Should have 1 output with 1000 sats (from template) and 2 outputs with 2000 sats (from others)
420+
let output_1000_count = txdata
421+
.outputs
422+
.iter()
423+
.filter(|output| output.amount == Amount::from_sat(1000))
424+
.count();
425+
let output_2000_count = txdata
426+
.outputs
427+
.iter()
428+
.filter(|output| output.amount == Amount::from_sat(2000))
429+
.count();
430+
431+
assert_eq!(
432+
output_1000_count, 1,
433+
"Should have exactly 1 output with 1000 sats from template"
434+
);
435+
assert_eq!(
436+
output_2000_count, 2,
437+
"Should have exactly 2 outputs with 2000 sats from other participants"
438+
);
388439
}
389440
}

0 commit comments

Comments
 (0)