Skip to content

Commit 1d3861e

Browse files
committed
Add APIError::IncompatibleShutdownScript
1 parent 4a44bfa commit 1d3861e

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ fn check_api_err(api_err: APIError) {
253253
APIError::MonitorUpdateFailed => {
254254
// We can (obviously) temp-fail a monitor update
255255
},
256+
APIError::IncompatibleShutdownScript { .. } => panic!("Cannot send an incompatible shutdown script"),
256257
}
257258
}
258259
#[inline]

lightning/src/ln/channel.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,8 @@ impl<Signer: Sign> Channel<Signer> {
607607
} else { None };
608608

609609
if let Some(shutdown_scriptpubkey) = &shutdown_scriptpubkey {
610-
if !shutdown_scriptpubkey.is_compatible(their_features) {
611-
return Err(APIError::APIMisuseError { err: format!("Provided a scriptpubkey format not accepted by peer: {}", shutdown_scriptpubkey) });
610+
if !shutdown_scriptpubkey.is_compatible(&their_features) {
611+
return Err(APIError::IncompatibleShutdownScript { script: shutdown_scriptpubkey.clone() });
612612
}
613613
}
614614

@@ -4487,7 +4487,7 @@ impl<Signer: Sign> Channel<Signer> {
44874487
None => {
44884488
let shutdown_scriptpubkey = keys_provider.get_shutdown_scriptpubkey();
44894489
if !shutdown_scriptpubkey.is_compatible(their_features) {
4490-
return Err(APIError::APIMisuseError { err: format!("Provided a scriptpubkey format not accepted by peer: {}", shutdown_scriptpubkey) });
4490+
return Err(APIError::IncompatibleShutdownScript { script: shutdown_scriptpubkey.clone() });
44914491
}
44924492
self.shutdown_scriptpubkey = Some(shutdown_scriptpubkey);
44934493
true
@@ -5253,15 +5253,17 @@ mod tests {
52535253
let seed = [42; 32];
52545254
let network = Network::Testnet;
52555255
let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
5256-
keys_provider.expect(OnGetShutdownScriptpubkey { returns: non_v0_segwit_shutdown_script });
5256+
keys_provider.expect(OnGetShutdownScriptpubkey {
5257+
returns: non_v0_segwit_shutdown_script.clone(),
5258+
});
52575259

52585260
let fee_estimator = TestFeeEstimator { fee_est: 253 };
52595261
let secp_ctx = Secp256k1::new();
52605262
let node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
52615263
let config = UserConfig::default();
52625264
match Channel::<EnforcingSigner>::new_outbound(&&fee_estimator, &&keys_provider, node_id, &features, 10000000, 100000, 42, &config) {
5263-
Err(APIError::APIMisuseError { err }) => {
5264-
assert_eq!(err, "Provided a scriptpubkey format not accepted by peer. script: (60020028)");
5265+
Err(APIError::IncompatibleShutdownScript { script }) => {
5266+
assert_eq!(script.into_inner(), non_v0_segwit_shutdown_script.into_inner());
52655267
},
52665268
Err(e) => panic!("Unexpected error: {:?}", e),
52675269
Ok(_) => panic!("Expected error"),

lightning/src/ln/functional_tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7693,7 +7693,9 @@ fn test_unsupported_anysegwit_shutdown_script() {
76937693

76947694
let chan = create_announced_chan_between_nodes(&nodes, 0, 1, node_cfgs[0].features.clone(), node_cfgs[1].features.clone());
76957695
match nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()) {
7696-
Err(APIError::APIMisuseError { err }) => assert_eq!(err, "Provided a scriptpubkey format not accepted by peer. script: (60020028)"),
7696+
Err(APIError::IncompatibleShutdownScript { script }) => {
7697+
assert_eq!(script.into_inner(), unsupported_shutdown_script.clone().into_inner());
7698+
},
76977699
Err(e) => panic!("Unexpected error: {:?}", e),
76987700
Ok(_) => panic!("Expected error"),
76997701
}

lightning/src/util/errors.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
//! Error types live here.
1111
12+
use ln::script::ShutdownScript;
13+
1214
use alloc::string::String;
1315
use core::fmt;
1416

@@ -47,6 +49,18 @@ pub enum APIError {
4749
/// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
4850
/// attempted action to fail.
4951
MonitorUpdateFailed,
52+
/// [`KeysInterface::get_shutdown_scriptpubkey`] returned a shutdown scriptpubkey incompatible
53+
/// with the channel counterparty as negotiated in [`InitFeatures`].
54+
///
55+
/// Using a SegWit v0 script should resolve this issue. If you cannot, you won't be able to open
56+
/// a channel or cooperatively close one with this peer (and will have to force-close instead).
57+
///
58+
/// [`KeysInterface::get_shutdown_scriptpubkey`]: crate::chain::keysinterface::KeysInterface::get_shutdown_scriptpubkey
59+
/// [`InitFeatures`]: crate::ln::features::InitFeatures
60+
IncompatibleShutdownScript {
61+
/// The incompatible shutdown script.
62+
script: ShutdownScript,
63+
},
5064
}
5165

5266
impl fmt::Debug for APIError {
@@ -57,6 +71,9 @@ impl fmt::Debug for APIError {
5771
APIError::RouteError {ref err} => f.write_str(err),
5872
APIError::ChannelUnavailable {ref err} => f.write_str(err),
5973
APIError::MonitorUpdateFailed => f.write_str("Client indicated a channel monitor update failed"),
74+
APIError::IncompatibleShutdownScript { ref script } => {
75+
write!(f, "Provided a scriptpubkey format not accepted by peer: {}", script)
76+
},
6077
}
6178
}
6279
}

0 commit comments

Comments
 (0)