Skip to content

Commit 07c805c

Browse files
committed
Implement logic for handling last tx_complete, signature for shared input
1 parent 1229034 commit 07c805c

File tree

2 files changed

+48
-41
lines changed

2 files changed

+48
-41
lines changed

lightning/src/ln/channel.rs

+11-20
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::ln::types::ChannelId;
3030
use crate::types::payment::{PaymentPreimage, PaymentHash};
3131
use crate::types::features::{ChannelTypeFeatures, InitFeatures};
3232
use crate::ln::interactivetxs::{
33-
calculate_change_output_value, get_output_weight, HandleTxCompleteValue, HandleTxCompleteResult, InteractiveTxConstructor,
33+
calculate_change_output_value, get_output_weight, AbortReason, HandleTxCompleteValue, HandleTxCompleteResult, InteractiveTxConstructor,
3434
InteractiveTxConstructorArgs, InteractiveTxMessageSend, InteractiveTxSigningSession, InteractiveTxMessageSendResult,
3535
OutputOwned, SharedOwnedOutput, TX_COMMON_FIELDS_WEIGHT,
3636
};
@@ -2223,13 +2223,15 @@ impl<SP: Deref> InitialRemoteCommitmentReceiver<SP> for FundedChannel<SP> where
22232223

22242224
impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22252225
/// Prepare and start interactive transaction negotiation.
2226-
/// `change_destination_opt` - Optional destination for optional change; if None, default destination address is used.
2226+
/// `change_destination_opt` - Optional destination for optional change; if None,
2227+
/// default destination address is used.
2228+
/// If error occurs, it is caused by our side, not the counterparty.
22272229
#[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled
22282230
fn begin_interactive_funding_tx_construction<ES: Deref>(
22292231
&mut self, signer_provider: &SP, entropy_source: &ES, holder_node_id: PublicKey,
22302232
change_destination_opt: Option<ScriptBuf>,
22312233
prev_funding_input: Option<(TxIn, TransactionU16LenLimited)>,
2232-
) -> Result<Option<InteractiveTxMessageSend>, ChannelError>
2234+
) -> Result<Option<InteractiveTxMessageSend>, AbortReason>
22332235
where ES::Target: EntropySource
22342236
{
22352237
debug_assert!(self.interactive_tx_constructor.is_none());
@@ -2273,19 +2275,14 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22732275
script
22742276
} else {
22752277
signer_provider.get_destination_script(self.context.channel_keys_id)
2276-
.map_err(|err| ChannelError::Warn(format!(
2277-
"Failed to get change script as new destination script, {:?}", err,
2278-
)))?
2278+
.map_err(|_err| AbortReason::InternalErrorGettingDestinationScript)?
22792279
};
22802280
let change_value_opt = calculate_change_output_value(
22812281
self.funding.is_outbound(), self.dual_funding_context.our_funding_satoshis,
22822282
&funding_inputs_prev_outputs, &funding_outputs,
22832283
self.dual_funding_context.funding_feerate_sat_per_1000_weight,
22842284
change_script.minimal_non_dust().to_sat(),
2285-
).map_err(|err| ChannelError::Warn(format!(
2286-
"Insufficient inputs, cannot cover intended contribution of {} and fees; {}",
2287-
self.dual_funding_context.our_funding_satoshis, err
2288-
)))?;
2285+
)?;
22892286
if let Some(change_value) = change_value_opt {
22902287
let mut change_output = TxOut {
22912288
value: Amount::from_sat(change_value),
@@ -2313,8 +2310,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23132310
outputs_to_contribute: funding_outputs,
23142311
expected_remote_shared_funding_output,
23152312
};
2316-
let mut tx_constructor = InteractiveTxConstructor::new(constructor_args)
2317-
.map_err(|_| ChannelError::Warn("Incorrect shared output provided".into()))?;
2313+
let mut tx_constructor = InteractiveTxConstructor::new(constructor_args)?;
23182314
let msg = tx_constructor.take_initiator_first_message();
23192315

23202316
self.interactive_tx_constructor = Some(tx_constructor);
@@ -4999,23 +4995,18 @@ impl DualFundingChannelContext {
49994995
/// Obtain prev outputs for each supplied input and matching transaction.
50004996
/// Will error when a prev tx does not have an output for the specified vout.
50014997
/// Also checks for matching of transaction IDs.
5002-
fn txouts_from_input_prev_txs(inputs: &Vec<(TxIn, TransactionU16LenLimited)>) -> Result<Vec<&TxOut>, ChannelError> {
4998+
fn txouts_from_input_prev_txs(inputs: &Vec<(TxIn, TransactionU16LenLimited)>) -> Result<Vec<&TxOut>, AbortReason> {
50034999
let mut prev_outputs: Vec<&TxOut> = Vec::with_capacity(inputs.len());
50045000
// Check that vouts exist for each TxIn in provided transactions.
50055001
for (idx, (txin, tx)) in inputs.iter().enumerate() {
50065002
let txid = tx.as_transaction().compute_txid();
50075003
if txin.previous_output.txid != txid {
5008-
return Err(ChannelError::Warn(
5009-
format!("Transaction input txid mismatch, {} vs. {}, at index {}", txin.previous_output.txid, txid, idx)
5010-
));
5004+
return Err(AbortReason::ProvidedInputsAndPrevtxsTxIdMismatch(idx as u32));
50115005
}
50125006
if let Some(output) = tx.as_transaction().output.get(txin.previous_output.vout as usize) {
50135007
prev_outputs.push(output);
50145008
} else {
5015-
return Err(ChannelError::Warn(
5016-
format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn, at index {}",
5017-
txid, txin.previous_output.vout, idx)
5018-
));
5009+
return Err(AbortReason::ProvidedInputsAndPrevtxsVoutNotFound(idx as u32));
50195010
}
50205011
}
50215012
Ok(prev_outputs)

lightning/src/ln/interactivetxs.rs

+37-21
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ pub(crate) enum AbortReason {
108108
/// if funding output is provided by the peer this is an interop error,
109109
/// if provided by the same node than internal input consistency error.
110110
InvalidLowFundingOutputValue,
111+
/// TxId mismatch in the provided inputs and previous transactions, input index in data
112+
ProvidedInputsAndPrevtxsTxIdMismatch(u32),
113+
/// A vout provided in an input is not found in the matching previous transaction, input index in data
114+
ProvidedInputsAndPrevtxsVoutNotFound(u32),
115+
/// Internal error, error while getting destination script
116+
InternalErrorGettingDestinationScript,
111117
}
112118

113119
impl AbortReason {
@@ -118,34 +124,44 @@ impl AbortReason {
118124

119125
impl Display for AbortReason {
120126
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
121-
f.write_str(match self {
122-
AbortReason::InvalidStateTransition => "State transition was invalid",
123-
AbortReason::UnexpectedCounterpartyMessage => "Unexpected message",
124-
AbortReason::ReceivedTooManyTxAddInputs => "Too many `tx_add_input`s received",
125-
AbortReason::ReceivedTooManyTxAddOutputs => "Too many `tx_add_output`s received",
127+
f.write_str(&match self {
128+
AbortReason::InvalidStateTransition => "State transition was invalid".into(),
129+
AbortReason::UnexpectedCounterpartyMessage => "Unexpected message".into(),
130+
AbortReason::ReceivedTooManyTxAddInputs => "Too many `tx_add_input`s received".into(),
131+
AbortReason::ReceivedTooManyTxAddOutputs => "Too many `tx_add_output`s received".into(),
126132
AbortReason::IncorrectInputSequenceValue => {
127-
"Input has a sequence value greater than 0xFFFFFFFD"
133+
"Input has a sequence value greater than 0xFFFFFFFD".into()
128134
},
129-
AbortReason::IncorrectSerialIdParity => "Parity for `serial_id` was incorrect",
130-
AbortReason::SerialIdUnknown => "The `serial_id` is unknown",
131-
AbortReason::DuplicateSerialId => "The `serial_id` already exists",
132-
AbortReason::PrevTxOutInvalid => "Invalid previous transaction output",
135+
AbortReason::IncorrectSerialIdParity => "Parity for `serial_id` was incorrect".into(),
136+
AbortReason::SerialIdUnknown => "The `serial_id` is unknown".into(),
137+
AbortReason::DuplicateSerialId => "The `serial_id` already exists".into(),
138+
AbortReason::PrevTxOutInvalid => "Invalid previous transaction output".into(),
133139
AbortReason::ExceededMaximumSatsAllowed => {
134-
"Output amount exceeded total bitcoin supply"
140+
"Output amount exceeded total bitcoin supply".into()
135141
},
136-
AbortReason::ExceededNumberOfInputsOrOutputs => "Too many inputs or outputs",
137-
AbortReason::TransactionTooLarge => "Transaction weight is too large",
138-
AbortReason::BelowDustLimit => "Output amount is below the dust limit",
139-
AbortReason::InvalidOutputScript => "The output script is non-standard",
140-
AbortReason::InsufficientFees => "Insufficient fees paid",
142+
AbortReason::ExceededNumberOfInputsOrOutputs => "Too many inputs or outputs".into(),
143+
AbortReason::TransactionTooLarge => "Transaction weight is too large".into(),
144+
AbortReason::BelowDustLimit => "Output amount is below the dust limit".into(),
145+
AbortReason::InvalidOutputScript => "The output script is non-standard".into(),
146+
AbortReason::InsufficientFees => "Insufficient fees paid".into(),
141147
AbortReason::OutputsValueExceedsInputsValue => {
142-
"Total value of outputs exceeds total value of inputs"
148+
"Total value of outputs exceeds total value of inputs".into()
143149
},
144-
AbortReason::InvalidTx => "The transaction is invalid",
145-
AbortReason::MissingFundingOutput => "No shared funding output found",
146-
AbortReason::DuplicateFundingOutput => "More than one funding output found",
150+
AbortReason::InvalidTx => "The transaction is invalid".into(),
151+
AbortReason::MissingFundingOutput => "No shared funding output found".into(),
152+
AbortReason::DuplicateFundingOutput => "More than one funding output found".into(),
147153
AbortReason::InvalidLowFundingOutputValue => {
148-
"Local part of funding output value is greater than the funding output value"
154+
"Local part of funding output value is greater than the funding output value".into()
155+
},
156+
AbortReason::ProvidedInputsAndPrevtxsTxIdMismatch(idx) => format!(
157+
"TxId mismatch in the provided inputs and previous transactions, input index {}",
158+
idx
159+
),
160+
AbortReason::ProvidedInputsAndPrevtxsVoutNotFound(idx) => format!(
161+
"Vout provided in an input is not found in the previous transaction, input index {}",
162+
idx),
163+
AbortReason::InternalErrorGettingDestinationScript => {
164+
"Internal error getting destination script".into()
149165
},
150166
})
151167
}

0 commit comments

Comments
 (0)