Skip to content

Commit

Permalink
Clean up some spurious test_utils/transaction_utils variables
Browse files Browse the repository at this point in the history
PR lightningdevkit#3323 introduced some extra variables to avoid `rustfmt` making
a total mess of our code, but introduced a few that don't make
`rustfmt` do dumb things, which we remove here.
  • Loading branch information
TheBlueMatt committed Jan 20, 2025
1 parent 8e9ad55 commit bc7631f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 43 deletions.
45 changes: 15 additions & 30 deletions lightning/src/util/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,10 @@ impl<'a> TestChainMonitor<'a> {
fee_estimator: &'a TestFeeEstimator, persister: &'a dyn Persist<TestChannelSigner>,
keys_manager: &'a TestKeysInterface,
) -> Self {
let added_monitors = Mutex::new(Vec::new());
let monitor_updates = Mutex::new(new_hash_map());
let latest_monitor_update_id = Mutex::new(new_hash_map());
let expect_channel_force_closed = Mutex::new(None);
let expect_monitor_round_trip_fail = Mutex::new(None);
Self {
added_monitors,
monitor_updates,
latest_monitor_update_id,
added_monitors: Mutex::new(Vec::new()),
monitor_updates: Mutex::new(new_hash_map()),
latest_monitor_update_id: Mutex::new(new_hash_map()),
chain_monitor: ChainMonitor::new(
chain_source,
broadcaster,
Expand All @@ -431,8 +426,8 @@ impl<'a> TestChainMonitor<'a> {
persister,
),
keys_manager,
expect_channel_force_closed,
expect_monitor_round_trip_fail,
expect_channel_force_closed: Mutex::new(None),
expect_monitor_round_trip_fail: Mutex::new(None),
}
}

Expand Down Expand Up @@ -909,13 +904,10 @@ impl TestChannelMessageHandler {

impl TestChannelMessageHandler {
pub fn new(chain_hash: ChainHash) -> Self {
let pending_events = Mutex::new(Vec::new());
let expected_recv_msgs = Mutex::new(None);
let connected_peers = Mutex::new(new_hash_set());
TestChannelMessageHandler {
pending_events,
expected_recv_msgs,
connected_peers,
pending_events: Mutex::new(Vec::new()),
expected_recv_msgs: Mutex::new(None),
connected_peers: Mutex::new(new_hash_set()),
chain_hash,
}
}
Expand Down Expand Up @@ -1567,19 +1559,14 @@ impl SignerProvider for TestKeysInterface {
impl TestKeysInterface {
pub fn new(seed: &[u8; 32], network: Network) -> Self {
let now = Duration::from_secs(genesis_block(network).header.time as u64);
let override_random_bytes = Mutex::new(None);
let enforcement_states = Mutex::new(new_hash_map());
let expectations = Mutex::new(None);
let unavailable_signers_ops = Mutex::new(new_hash_map());
let next_signer_disabled_ops = Mutex::new(new_hash_set());
Self {
backing: sign::PhantomKeysManager::new(seed, now.as_secs(), now.subsec_nanos(), seed),
override_random_bytes,
override_random_bytes: Mutex::new(None),
disable_revocation_policy_check: false,
enforcement_states,
expectations,
unavailable_signers_ops,
next_signer_disabled_ops,
enforcement_states: Mutex::new(new_hash_map()),
expectations: Mutex::new(None),
unavailable_signers_ops: Mutex::new(new_hash_map()),
next_signer_disabled_ops: Mutex::new(new_hash_set()),
}
}

Expand Down Expand Up @@ -1653,14 +1640,12 @@ impl TestChainSource {
let script_pubkey = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
let utxo_ret =
Mutex::new(UtxoResult::Sync(Ok(TxOut { value: Amount::MAX, script_pubkey })));
let watched_txn = Mutex::new(new_hash_set());
let watched_outputs = Mutex::new(new_hash_set());
Self {
chain_hash: ChainHash::using_genesis_block(network),
utxo_ret,
get_utxo_call_count: AtomicUsize::new(0),
watched_txn,
watched_outputs,
watched_txn: Mutex::new(new_hash_set()),
watched_outputs: Mutex::new(new_hash_set()),
}
}
pub fn remove_watched_txn_and_outputs(&self, outpoint: OutPoint, script_pubkey: ScriptBuf) {
Expand Down
20 changes: 7 additions & 13 deletions lightning/src/util/transaction_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,8 @@ mod tests {
// If we have a bogus input amount or outputs valued more than inputs, we should fail
let version = Version::TWO;
let lock_time = LockTime::ZERO;
let input = Vec::new();
let tx_out = TxOut { script_pubkey: ScriptBuf::new(), value: Amount::from_sat(1000) };
let output = vec![tx_out];
let mut tx = Transaction { version, lock_time, input, output };
let mut tx = Transaction { version, lock_time, input: Vec::new(), output: vec![tx_out] };
let amount = Amount::from_sat(21_000_000_0000_0001);
assert!(maybe_add_change_output(&mut tx, amount, 0, 253, ScriptBuf::new()).is_err());
let amount = Amount::from_sat(400);
Expand Down Expand Up @@ -294,36 +292,32 @@ mod tests {
let tx_in = TxIn { previous_output, script_sig, witness, sequence };
let version = Version::TWO;
let lock_time = LockTime::ZERO;
let input = vec![tx_in];
let output = vec![tx_out];
let mut tx = Transaction { version, lock_time, input, output };
let mut tx = Transaction { version, lock_time, input: vec![tx_in], output: vec![tx_out] };
let orig_wtxid = tx.compute_wtxid();
let orig_weight = tx.weight().to_wu();
assert_eq!(orig_weight / 4, 61);

assert_eq!(Builder::new().push_int(2).into_script().minimal_non_dust().to_sat(), 474);

let script = Builder::new().push_int(2).into_script();

// Input value of the output value + fee - 1 should fail:
let amount = Amount::from_sat(1000 + 61 + 100 - 1);
let script = Builder::new().push_int(2).into_script();
assert!(maybe_add_change_output(&mut tx, amount, 400, 250, script).is_err());
assert!(maybe_add_change_output(&mut tx, amount, 400, 250, script.clone()).is_err());
// Failure doesn't change the transaction
assert_eq!(tx.compute_wtxid(), orig_wtxid);
// but one more input sat should succeed, without changing the transaction
let amount = Amount::from_sat(1000 + 61 + 100);
let script = Builder::new().push_int(2).into_script();
assert!(maybe_add_change_output(&mut tx, amount, 400, 250, script).is_ok());
assert!(maybe_add_change_output(&mut tx, amount, 400, 250, script.clone()).is_ok());
// If we don't add an output, we don't change the transaction
assert_eq!(tx.compute_wtxid(), orig_wtxid);
// In order to get a change output, we need to add 474 plus the output's weight / 4 (10)...
let amount = Amount::from_sat(1000 + 61 + 100 + 474 + 9);
let script = Builder::new().push_int(2).into_script();
assert!(maybe_add_change_output(&mut tx, amount, 400, 250, script).is_ok());
assert!(maybe_add_change_output(&mut tx, amount, 400, 250, script.clone()).is_ok());
// If we don't add an output, we don't change the transaction
assert_eq!(tx.compute_wtxid(), orig_wtxid);

let amount = Amount::from_sat(1000 + 61 + 100 + 474 + 10);
let script = Builder::new().push_int(2).into_script();
assert!(maybe_add_change_output(&mut tx, amount, 400, 250, script).is_ok());
assert_eq!(tx.output.len(), 2);
assert_eq!(tx.output[1].value.to_sat(), 474);
Expand Down

0 comments on commit bc7631f

Please sign in to comment.