Skip to content

Commit 625228e

Browse files
committed
Use BTreeMap in NegotiationContext to order inputs and outputs
When an interactive tx sessions aborts, the contributed inputs and outputs will be given in the error in an upcoming commit. Use BTreeMap instead in NegotiationContext to give a consisted ordering by SerialId.
1 parent 8ed27b0 commit 625228e

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

lightning/src/ln/interactivetxs.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use crate::ln::msgs::{MessageSendEvent, SerialId, TxSignatures};
3535
use crate::ln::types::ChannelId;
3636
use crate::sign::{EntropySource, P2TR_KEY_PATH_WITNESS_WEIGHT, P2WPKH_WITNESS_WEIGHT};
3737

38+
use alloc::collections::{btree_map, BTreeMap};
3839
use core::fmt::Display;
3940
use core::ops::Deref;
4041

@@ -818,7 +819,7 @@ struct NegotiationContext {
818819
holder_is_initiator: bool,
819820
received_tx_add_input_count: u16,
820821
received_tx_add_output_count: u16,
821-
inputs: HashMap<SerialId, InteractiveTxInput>,
822+
inputs: BTreeMap<SerialId, InteractiveTxInput>,
822823
/// Optional intended/expected funding input, used during splicing.
823824
/// The funding input is shared, it is usually co-owned by both peers.
824825
/// - For the initiator:
@@ -836,7 +837,7 @@ struct NegotiationContext {
836837
shared_funding_output: SharedOwnedOutput,
837838
prevtx_outpoints: HashSet<OutPoint>,
838839
/// The outputs added so far.
839-
outputs: HashMap<SerialId, InteractiveTxOutput>,
840+
outputs: BTreeMap<SerialId, InteractiveTxOutput>,
840841
/// The locktime of the funding transaction.
841842
tx_locktime: AbsoluteLockTime,
842843
/// The fee rate used for the transaction
@@ -881,11 +882,11 @@ impl NegotiationContext {
881882
holder_is_initiator,
882883
received_tx_add_input_count: 0,
883884
received_tx_add_output_count: 0,
884-
inputs: new_hash_map(),
885+
inputs: BTreeMap::new(),
885886
shared_funding_input,
886887
shared_funding_output,
887888
prevtx_outpoints: new_hash_set(),
888-
outputs: new_hash_map(),
889+
outputs: BTreeMap::new(),
889890
tx_locktime,
890891
feerate_sat_per_kw,
891892
}
@@ -1030,13 +1031,13 @@ impl NegotiationContext {
10301031
};
10311032

10321033
match self.inputs.entry(msg.serial_id) {
1033-
hash_map::Entry::Occupied(_) => {
1034+
btree_map::Entry::Occupied(_) => {
10341035
// The receiving node:
10351036
// - MUST fail the negotiation if:
10361037
// - the `serial_id` is already included in the transaction
10371038
Err(AbortReason::DuplicateSerialId)
10381039
},
1039-
hash_map::Entry::Vacant(entry) => {
1040+
btree_map::Entry::Vacant(entry) => {
10401041
if !self.prevtx_outpoints.insert(prev_outpoint) {
10411042
// The receiving node:
10421043
// - MUST fail the negotiation if:
@@ -1140,13 +1141,13 @@ impl NegotiationContext {
11401141
let output =
11411142
InteractiveTxOutput { serial_id: msg.serial_id, added_by: AddingRole::Remote, output };
11421143
match self.outputs.entry(msg.serial_id) {
1143-
hash_map::Entry::Occupied(_) => {
1144+
btree_map::Entry::Occupied(_) => {
11441145
// The receiving node:
11451146
// - MUST fail the negotiation if:
11461147
// - the `serial_id` is already included in the transaction
11471148
Err(AbortReason::DuplicateSerialId)
11481149
},
1149-
hash_map::Entry::Vacant(entry) => {
1150+
btree_map::Entry::Vacant(entry) => {
11501151
entry.insert(output);
11511152
Ok(())
11521153
},

0 commit comments

Comments
 (0)