@@ -1827,6 +1827,8 @@ pub(super) struct InteractiveTxConstructor {
1827
1827
channel_id : ChannelId ,
1828
1828
inputs_to_contribute : Vec < ( SerialId , InputOwned ) > ,
1829
1829
outputs_to_contribute : Vec < ( SerialId , OutputOwned ) > ,
1830
+ next_input_index : Option < usize > ,
1831
+ next_output_index : Option < usize > ,
1830
1832
}
1831
1833
1832
1834
#[ allow( clippy:: enum_variant_names) ] // Clippy doesn't like the repeated `Tx` prefix here
@@ -1979,12 +1981,17 @@ impl InteractiveTxConstructor {
1979
1981
// In the same manner and for the same rationale as the inputs above, we'll shuffle the outputs.
1980
1982
outputs_to_contribute. sort_unstable_by_key ( |( serial_id, _) | * serial_id) ;
1981
1983
1984
+ let next_input_index = ( !inputs_to_contribute. is_empty ( ) ) . then_some ( 0 ) ;
1985
+ let next_output_index = ( !outputs_to_contribute. is_empty ( ) ) . then_some ( 0 ) ;
1986
+
1982
1987
let mut constructor = Self {
1983
1988
state_machine,
1984
1989
initiator_first_message : None ,
1985
1990
channel_id,
1986
1991
inputs_to_contribute,
1987
1992
outputs_to_contribute,
1993
+ next_input_index,
1994
+ next_output_index,
1988
1995
} ;
1989
1996
// We'll store the first message for the initiator.
1990
1997
if is_initiator {
@@ -1998,22 +2005,24 @@ impl InteractiveTxConstructor {
1998
2005
}
1999
2006
2000
2007
fn maybe_send_message ( & mut self ) -> Result < InteractiveTxMessageSend , AbortReason > {
2008
+ let channel_id = self . channel_id ;
2009
+
2001
2010
// We first attempt to send inputs we want to add, then outputs. Once we are done sending
2002
2011
// them both, then we always send tx_complete.
2003
- if let Some ( ( serial_id, input) ) = self . inputs_to_contribute . pop ( ) {
2012
+ if let Some ( ( serial_id, input) ) = self . next_input_to_contribute ( ) {
2004
2013
let satisfaction_weight = input. satisfaction_weight ( ) ;
2005
2014
let msg = match input {
2006
2015
InputOwned :: Single ( single) => msgs:: TxAddInput {
2007
- channel_id : self . channel_id ,
2008
- serial_id,
2009
- prevtx : Some ( single. prev_tx ) ,
2016
+ channel_id,
2017
+ serial_id : * serial_id ,
2018
+ prevtx : Some ( single. prev_tx . clone ( ) ) ,
2010
2019
prevtx_out : single. input . previous_output . vout ,
2011
2020
sequence : single. input . sequence . to_consensus_u32 ( ) ,
2012
2021
shared_input_txid : None ,
2013
2022
} ,
2014
2023
InputOwned :: Shared ( shared) => msgs:: TxAddInput {
2015
- channel_id : self . channel_id ,
2016
- serial_id,
2024
+ channel_id,
2025
+ serial_id : * serial_id ,
2017
2026
prevtx : None ,
2018
2027
prevtx_out : shared. input . previous_output . vout ,
2019
2028
sequence : shared. input . sequence . to_consensus_u32 ( ) ,
@@ -2022,22 +2031,52 @@ impl InteractiveTxConstructor {
2022
2031
} ;
2023
2032
do_state_transition ! ( self , sent_tx_add_input, ( & msg, satisfaction_weight) ) ?;
2024
2033
Ok ( InteractiveTxMessageSend :: TxAddInput ( msg) )
2025
- } else if let Some ( ( serial_id, output) ) = self . outputs_to_contribute . pop ( ) {
2034
+ } else if let Some ( ( serial_id, output) ) = self . next_output_to_contribute ( ) {
2026
2035
let msg = msgs:: TxAddOutput {
2027
- channel_id : self . channel_id ,
2028
- serial_id,
2036
+ channel_id,
2037
+ serial_id : * serial_id ,
2029
2038
sats : output. tx_out ( ) . value . to_sat ( ) ,
2030
2039
script : output. tx_out ( ) . script_pubkey . clone ( ) ,
2031
2040
} ;
2032
2041
do_state_transition ! ( self , sent_tx_add_output, & msg) ?;
2033
2042
Ok ( InteractiveTxMessageSend :: TxAddOutput ( msg) )
2034
2043
} else {
2035
- let msg = msgs:: TxComplete { channel_id : self . channel_id } ;
2044
+ let msg = msgs:: TxComplete { channel_id } ;
2036
2045
do_state_transition ! ( self , sent_tx_complete, & msg) ?;
2037
2046
Ok ( InteractiveTxMessageSend :: TxComplete ( msg) )
2038
2047
}
2039
2048
}
2040
2049
2050
+ fn next_input_to_contribute ( & mut self ) -> Option < & ( SerialId , InputOwned ) > {
2051
+ match self . next_input_index {
2052
+ Some ( index) => {
2053
+ self . next_input_index = if index + 1 < self . inputs_to_contribute . len ( ) {
2054
+ Some ( index + 1 )
2055
+ } else {
2056
+ None
2057
+ } ;
2058
+
2059
+ self . inputs_to_contribute . get ( index)
2060
+ } ,
2061
+ None => None ,
2062
+ }
2063
+ }
2064
+
2065
+ fn next_output_to_contribute ( & mut self ) -> Option < & ( SerialId , OutputOwned ) > {
2066
+ match self . next_output_index {
2067
+ Some ( index) => {
2068
+ self . next_output_index = if index + 1 < self . outputs_to_contribute . len ( ) {
2069
+ Some ( index + 1 )
2070
+ } else {
2071
+ None
2072
+ } ;
2073
+
2074
+ self . outputs_to_contribute . get ( index)
2075
+ } ,
2076
+ None => None ,
2077
+ }
2078
+ }
2079
+
2041
2080
pub fn handle_tx_add_input (
2042
2081
& mut self , msg : & msgs:: TxAddInput ,
2043
2082
) -> Result < InteractiveTxMessageSend , AbortReason > {
0 commit comments