@@ -842,16 +842,18 @@ struct NegotiationContext {
842
842
feerate_sat_per_kw : u32 ,
843
843
}
844
844
845
- pub ( crate ) fn estimate_input_weight ( prev_output : & TxOut ) -> Weight {
846
- Weight :: from_wu ( if prev_output. script_pubkey . is_p2wpkh ( ) {
847
- P2WPKH_INPUT_WEIGHT_LOWER_BOUND
848
- } else if prev_output. script_pubkey . is_p2wsh ( ) {
849
- P2WSH_INPUT_WEIGHT_LOWER_BOUND
850
- } else if prev_output. script_pubkey . is_p2tr ( ) {
851
- P2TR_INPUT_WEIGHT_LOWER_BOUND
852
- } else {
853
- UNKNOWN_SEGWIT_VERSION_INPUT_WEIGHT_LOWER_BOUND
854
- } )
845
+ fn estimate_input_satisfaction_weight ( prev_output : & TxOut ) -> Weight {
846
+ Weight :: from_wu (
847
+ if prev_output. script_pubkey . is_p2wpkh ( ) {
848
+ P2WPKH_INPUT_WEIGHT_LOWER_BOUND
849
+ } else if prev_output. script_pubkey . is_p2wsh ( ) {
850
+ P2WSH_INPUT_WEIGHT_LOWER_BOUND
851
+ } else if prev_output. script_pubkey . is_p2tr ( ) {
852
+ P2TR_INPUT_WEIGHT_LOWER_BOUND
853
+ } else {
854
+ UNKNOWN_SEGWIT_VERSION_INPUT_WEIGHT_LOWER_BOUND
855
+ } - BASE_INPUT_WEIGHT ,
856
+ )
855
857
}
856
858
857
859
pub ( crate ) fn get_output_weight ( script_pubkey : & ScriptBuf ) -> Weight {
@@ -906,7 +908,9 @@ impl NegotiationContext {
906
908
. iter ( )
907
909
. filter ( |( serial_id, _) | self . is_serial_id_valid_for_counterparty ( serial_id) )
908
910
. fold ( 0u64 , |weight, ( _, input) | {
909
- weight. saturating_add ( input. estimate_input_weight ( ) . to_wu ( ) )
911
+ weight
912
+ . saturating_add ( BASE_INPUT_WEIGHT )
913
+ . saturating_add ( input. satisfaction_weight ( ) . to_wu ( ) )
910
914
} ) ,
911
915
)
912
916
}
@@ -998,6 +1002,7 @@ impl NegotiationContext {
998
1002
input : txin,
999
1003
prev_tx : prevtx. clone ( ) ,
1000
1004
prev_output : tx_out. clone ( ) ,
1005
+ satisfaction_weight : estimate_input_satisfaction_weight ( & tx_out) ,
1001
1006
} ) ,
1002
1007
prev_outpoint,
1003
1008
)
@@ -1150,7 +1155,9 @@ impl NegotiationContext {
1150
1155
}
1151
1156
}
1152
1157
1153
- fn sent_tx_add_input ( & mut self , msg : & msgs:: TxAddInput ) -> Result < ( ) , AbortReason > {
1158
+ fn sent_tx_add_input (
1159
+ & mut self , ( msg, satisfaction_weight) : ( & msgs:: TxAddInput , Weight ) ,
1160
+ ) -> Result < ( ) , AbortReason > {
1154
1161
let vout = msg. prevtx_out as usize ;
1155
1162
let ( prev_outpoint, input) = if let Some ( shared_input_txid) = msg. shared_input_txid {
1156
1163
let prev_outpoint = OutPoint { txid : shared_input_txid, vout : msg. prevtx_out } ;
@@ -1168,8 +1175,12 @@ impl NegotiationContext {
1168
1175
sequence : Sequence ( msg. sequence ) ,
1169
1176
..Default :: default ( )
1170
1177
} ;
1171
- let single_input =
1172
- SingleOwnedInput { input : txin, prev_tx : prevtx. clone ( ) , prev_output } ;
1178
+ let single_input = SingleOwnedInput {
1179
+ input : txin,
1180
+ prev_tx : prevtx. clone ( ) ,
1181
+ prev_output,
1182
+ satisfaction_weight,
1183
+ } ;
1173
1184
( prev_outpoint, InputOwned :: Single ( single_input) )
1174
1185
} else {
1175
1186
return Err ( AbortReason :: MissingPrevTx ) ;
@@ -1432,7 +1443,7 @@ define_state_transitions!(SENT_MSG_STATE, [
1432
1443
// State transitions when we have received some messages from our counterparty and we should
1433
1444
// respond.
1434
1445
define_state_transitions ! ( RECEIVED_MSG_STATE , [
1435
- DATA & msgs:: TxAddInput , TRANSITION sent_tx_add_input,
1446
+ DATA ( & msgs:: TxAddInput , Weight ) , TRANSITION sent_tx_add_input,
1436
1447
DATA & msgs:: TxRemoveInput , TRANSITION sent_tx_remove_input,
1437
1448
DATA & msgs:: TxAddOutput , TRANSITION sent_tx_add_output,
1438
1449
DATA & msgs:: TxRemoveOutput , TRANSITION sent_tx_remove_output
@@ -1499,7 +1510,7 @@ impl StateMachine {
1499
1510
}
1500
1511
1501
1512
// TxAddInput
1502
- define_state_machine_transitions ! ( sent_tx_add_input, & msgs:: TxAddInput , [
1513
+ define_state_machine_transitions ! ( sent_tx_add_input, ( & msgs:: TxAddInput , Weight ) , [
1503
1514
FROM ReceivedChangeMsg , TO SentChangeMsg ,
1504
1515
FROM ReceivedTxComplete , TO SentChangeMsg
1505
1516
] ) ;
@@ -1566,6 +1577,7 @@ struct SingleOwnedInput {
1566
1577
input : TxIn ,
1567
1578
prev_tx : Transaction ,
1568
1579
prev_output : TxOut ,
1580
+ satisfaction_weight : Weight ,
1569
1581
}
1570
1582
1571
1583
#[ derive( Clone , Debug , Eq , PartialEq ) ]
@@ -1658,13 +1670,13 @@ impl InputOwned {
1658
1670
}
1659
1671
}
1660
1672
1661
- fn estimate_input_weight ( & self ) -> Weight {
1673
+ fn satisfaction_weight ( & self ) -> Weight {
1662
1674
match self {
1663
- InputOwned :: Single ( single) => estimate_input_weight ( & single. prev_output ) ,
1675
+ InputOwned :: Single ( single) => single. satisfaction_weight ,
1664
1676
// TODO(taproot): Needs to consider different weights based on channel type
1665
- InputOwned :: Shared ( _) => Weight :: from_wu (
1666
- BASE_INPUT_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT + FUNDING_TRANSACTION_WITNESS_WEIGHT ,
1667
- ) ,
1677
+ InputOwned :: Shared ( _) => {
1678
+ Weight :: from_wu ( EMPTY_SCRIPT_SIG_WEIGHT + FUNDING_TRANSACTION_WITNESS_WEIGHT )
1679
+ } ,
1668
1680
}
1669
1681
}
1670
1682
@@ -1835,12 +1847,12 @@ impl InteractiveTxInput {
1835
1847
self . input . remote_value ( self . added_by )
1836
1848
}
1837
1849
1838
- pub fn estimate_input_weight ( & self ) -> Weight {
1839
- self . input . estimate_input_weight ( )
1850
+ pub fn satisfaction_weight ( & self ) -> Weight {
1851
+ self . input . satisfaction_weight ( )
1840
1852
}
1841
1853
1842
1854
fn into_negotiated_input ( self ) -> NegotiatedTxInput {
1843
- let weight = self . input . estimate_input_weight ( ) ;
1855
+ let weight = Weight :: from_wu ( BASE_INPUT_WEIGHT ) + self . input . satisfaction_weight ( ) ;
1844
1856
let ( txin, prev_output) = self . input . into_tx_in_with_prev_output ( ) ;
1845
1857
NegotiatedTxInput { serial_id : self . serial_id , txin, weight, prev_output }
1846
1858
}
@@ -1965,8 +1977,12 @@ impl InteractiveTxConstructor {
1965
1977
let serial_id = generate_holder_serial_id ( entropy_source, is_initiator) ;
1966
1978
let txin = TxIn { previous_output : utxo. outpoint , sequence, ..Default :: default ( ) } ;
1967
1979
let prev_output = utxo. output ;
1968
- let input =
1969
- InputOwned :: Single ( SingleOwnedInput { input : txin, prev_tx, prev_output } ) ;
1980
+ let input = InputOwned :: Single ( SingleOwnedInput {
1981
+ input : txin,
1982
+ prev_tx,
1983
+ prev_output,
1984
+ satisfaction_weight : Weight :: from_wu ( utxo. satisfaction_weight ) ,
1985
+ } ) ;
1970
1986
( serial_id, input)
1971
1987
} )
1972
1988
. collect ( ) ;
@@ -2022,6 +2038,7 @@ impl InteractiveTxConstructor {
2022
2038
// We first attempt to send inputs we want to add, then outputs. Once we are done sending
2023
2039
// them both, then we always send tx_complete.
2024
2040
if let Some ( ( serial_id, input) ) = self . inputs_to_contribute . pop ( ) {
2041
+ let satisfaction_weight = input. satisfaction_weight ( ) ;
2025
2042
let msg = match input {
2026
2043
InputOwned :: Single ( single) => msgs:: TxAddInput {
2027
2044
channel_id : self . channel_id ,
@@ -2040,7 +2057,7 @@ impl InteractiveTxConstructor {
2040
2057
shared_input_txid : Some ( shared. input . previous_output . txid ) ,
2041
2058
} ,
2042
2059
} ;
2043
- do_state_transition ! ( self , sent_tx_add_input, & msg) ?;
2060
+ do_state_transition ! ( self , sent_tx_add_input, ( & msg, satisfaction_weight ) ) ?;
2044
2061
Ok ( InteractiveTxMessageSend :: TxAddInput ( msg) )
2045
2062
} else if let Some ( ( serial_id, output) ) = self . outputs_to_contribute . pop ( ) {
2046
2063
let msg = msgs:: TxAddOutput {
0 commit comments