@@ -842,7 +842,7 @@ struct NegotiationContext {
842
842
feerate_sat_per_kw : u32 ,
843
843
}
844
844
845
- pub ( crate ) fn estimate_input_weight ( prev_output : & TxOut ) -> Weight {
845
+ fn estimate_input_satisfaction_weight ( prev_output : & TxOut ) -> Weight {
846
846
Weight :: from_wu ( if prev_output. script_pubkey . is_p2wpkh ( ) {
847
847
P2WPKH_INPUT_WEIGHT_LOWER_BOUND
848
848
} else if prev_output. script_pubkey . is_p2wsh ( ) {
@@ -851,7 +851,7 @@ pub(crate) fn estimate_input_weight(prev_output: &TxOut) -> Weight {
851
851
P2TR_INPUT_WEIGHT_LOWER_BOUND
852
852
} else {
853
853
UNKNOWN_SEGWIT_VERSION_INPUT_WEIGHT_LOWER_BOUND
854
- } )
854
+ } - BASE_INPUT_WEIGHT )
855
855
}
856
856
857
857
pub ( crate ) fn get_output_weight ( script_pubkey : & ScriptBuf ) -> Weight {
@@ -906,7 +906,9 @@ impl NegotiationContext {
906
906
. iter ( )
907
907
. filter ( |( serial_id, _) | self . is_serial_id_valid_for_counterparty ( serial_id) )
908
908
. fold ( 0u64 , |weight, ( _, input) | {
909
- weight. saturating_add ( input. estimate_input_weight ( ) . to_wu ( ) )
909
+ weight
910
+ . saturating_add ( BASE_INPUT_WEIGHT )
911
+ . saturating_add ( input. satisfaction_weight ( ) . to_wu ( ) )
910
912
} ) ,
911
913
)
912
914
}
@@ -998,6 +1000,7 @@ impl NegotiationContext {
998
1000
input : txin,
999
1001
prev_tx : prevtx. clone ( ) ,
1000
1002
prev_output : tx_out. clone ( ) ,
1003
+ satisfaction_weight : estimate_input_satisfaction_weight ( & tx_out) ,
1001
1004
} ) ,
1002
1005
prev_outpoint,
1003
1006
)
@@ -1150,7 +1153,7 @@ impl NegotiationContext {
1150
1153
}
1151
1154
}
1152
1155
1153
- fn sent_tx_add_input ( & mut self , msg : & msgs:: TxAddInput ) -> Result < ( ) , AbortReason > {
1156
+ fn sent_tx_add_input ( & mut self , ( msg, satisfaction_weight ) : ( & msgs:: TxAddInput , Weight ) ) -> Result < ( ) , AbortReason > {
1154
1157
let vout = msg. prevtx_out as usize ;
1155
1158
let ( prev_outpoint, input) = if let Some ( shared_input_txid) = msg. shared_input_txid {
1156
1159
let prev_outpoint = OutPoint { txid : shared_input_txid, vout : msg. prevtx_out } ;
@@ -1169,7 +1172,7 @@ impl NegotiationContext {
1169
1172
..Default :: default ( )
1170
1173
} ;
1171
1174
let single_input =
1172
- SingleOwnedInput { input : txin, prev_tx : prevtx. clone ( ) , prev_output } ;
1175
+ SingleOwnedInput { input : txin, prev_tx : prevtx. clone ( ) , prev_output, satisfaction_weight } ;
1173
1176
( prev_outpoint, InputOwned :: Single ( single_input) )
1174
1177
} else {
1175
1178
return Err ( AbortReason :: MissingPrevTx ) ;
@@ -1432,7 +1435,7 @@ define_state_transitions!(SENT_MSG_STATE, [
1432
1435
// State transitions when we have received some messages from our counterparty and we should
1433
1436
// respond.
1434
1437
define_state_transitions ! ( RECEIVED_MSG_STATE , [
1435
- DATA & msgs:: TxAddInput , TRANSITION sent_tx_add_input,
1438
+ DATA ( & msgs:: TxAddInput , Weight ) , TRANSITION sent_tx_add_input,
1436
1439
DATA & msgs:: TxRemoveInput , TRANSITION sent_tx_remove_input,
1437
1440
DATA & msgs:: TxAddOutput , TRANSITION sent_tx_add_output,
1438
1441
DATA & msgs:: TxRemoveOutput , TRANSITION sent_tx_remove_output
@@ -1499,7 +1502,7 @@ impl StateMachine {
1499
1502
}
1500
1503
1501
1504
// TxAddInput
1502
- define_state_machine_transitions ! ( sent_tx_add_input, & msgs:: TxAddInput , [
1505
+ define_state_machine_transitions ! ( sent_tx_add_input, ( & msgs:: TxAddInput , Weight ) , [
1503
1506
FROM ReceivedChangeMsg , TO SentChangeMsg ,
1504
1507
FROM ReceivedTxComplete , TO SentChangeMsg
1505
1508
] ) ;
@@ -1566,6 +1569,7 @@ struct SingleOwnedInput {
1566
1569
input : TxIn ,
1567
1570
prev_tx : Transaction ,
1568
1571
prev_output : TxOut ,
1572
+ satisfaction_weight : Weight ,
1569
1573
}
1570
1574
1571
1575
#[ derive( Clone , Debug , Eq , PartialEq ) ]
@@ -1658,12 +1662,12 @@ impl InputOwned {
1658
1662
}
1659
1663
}
1660
1664
1661
- fn estimate_input_weight ( & self ) -> Weight {
1665
+ fn satisfaction_weight ( & self ) -> Weight {
1662
1666
match self {
1663
- InputOwned :: Single ( single) => estimate_input_weight ( & single. prev_output ) ,
1667
+ InputOwned :: Single ( single) => single. satisfaction_weight ,
1664
1668
// TODO(taproot): Needs to consider different weights based on channel type
1665
1669
InputOwned :: Shared ( _) => Weight :: from_wu (
1666
- BASE_INPUT_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT + FUNDING_TRANSACTION_WITNESS_WEIGHT ,
1670
+ EMPTY_SCRIPT_SIG_WEIGHT + FUNDING_TRANSACTION_WITNESS_WEIGHT ,
1667
1671
) ,
1668
1672
}
1669
1673
}
@@ -1835,12 +1839,12 @@ impl InteractiveTxInput {
1835
1839
self . input . remote_value ( self . added_by )
1836
1840
}
1837
1841
1838
- pub fn estimate_input_weight ( & self ) -> Weight {
1839
- self . input . estimate_input_weight ( )
1842
+ pub fn satisfaction_weight ( & self ) -> Weight {
1843
+ self . input . satisfaction_weight ( )
1840
1844
}
1841
1845
1842
1846
fn into_negotiated_input ( self ) -> NegotiatedTxInput {
1843
- let weight = self . input . estimate_input_weight ( ) ;
1847
+ let weight = Weight :: from_wu ( BASE_INPUT_WEIGHT ) + self . input . satisfaction_weight ( ) ;
1844
1848
let ( txin, prev_output) = self . input . into_tx_in_with_prev_output ( ) ;
1845
1849
NegotiatedTxInput { serial_id : self . serial_id , txin, weight, prev_output }
1846
1850
}
@@ -1965,8 +1969,12 @@ impl InteractiveTxConstructor {
1965
1969
let serial_id = generate_holder_serial_id ( entropy_source, is_initiator) ;
1966
1970
let txin = TxIn { previous_output : utxo. outpoint , sequence, ..Default :: default ( ) } ;
1967
1971
let prev_output = utxo. output ;
1968
- let input =
1969
- InputOwned :: Single ( SingleOwnedInput { input : txin, prev_tx, prev_output } ) ;
1972
+ let input = InputOwned :: Single ( SingleOwnedInput {
1973
+ input : txin,
1974
+ prev_tx,
1975
+ prev_output,
1976
+ satisfaction_weight : Weight :: from_wu ( utxo. satisfaction_weight ) ,
1977
+ } ) ;
1970
1978
( serial_id, input)
1971
1979
} )
1972
1980
. collect ( ) ;
@@ -2022,6 +2030,7 @@ impl InteractiveTxConstructor {
2022
2030
// We first attempt to send inputs we want to add, then outputs. Once we are done sending
2023
2031
// them both, then we always send tx_complete.
2024
2032
if let Some ( ( serial_id, input) ) = self . inputs_to_contribute . pop ( ) {
2033
+ let satisfaction_weight = input. satisfaction_weight ( ) ;
2025
2034
let msg = match input {
2026
2035
InputOwned :: Single ( single) => msgs:: TxAddInput {
2027
2036
channel_id : self . channel_id ,
@@ -2040,7 +2049,7 @@ impl InteractiveTxConstructor {
2040
2049
shared_input_txid : Some ( shared. input . previous_output . txid ) ,
2041
2050
} ,
2042
2051
} ;
2043
- do_state_transition ! ( self , sent_tx_add_input, & msg) ?;
2052
+ do_state_transition ! ( self , sent_tx_add_input, ( & msg, satisfaction_weight ) ) ?;
2044
2053
Ok ( InteractiveTxMessageSend :: TxAddInput ( msg) )
2045
2054
} else if let Some ( ( serial_id, output) ) = self . outputs_to_contribute . pop ( ) {
2046
2055
let msg = msgs:: TxAddOutput {
0 commit comments