Skip to content

Commit 5a0b475

Browse files
committed
Introduce ChannelSignerType.
1 parent 9e18e13 commit 5a0b475

File tree

7 files changed

+212
-178
lines changed

7 files changed

+212
-178
lines changed

lightning/src/events/bump_transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::ln::chan_utils::{
2626
use crate::ln::features::ChannelTypeFeatures;
2727
use crate::ln::PaymentPreimage;
2828
use crate::prelude::*;
29-
use crate::sign::{ChannelSigner, EcdsaChannelSigner, SignerProvider, WriteableEcdsaChannelSigner};
29+
use crate::sign::{EcdsaChannelSigner, SignerProvider, WriteableEcdsaChannelSigner};
3030
use crate::sync::Mutex;
3131
use crate::util::logger::Logger;
3232

lightning/src/ln/channel.rs

Lines changed: 102 additions & 104 deletions
Large diffs are not rendered by default.

lightning/src/ln/channelmanager.rs

Lines changed: 50 additions & 48 deletions
Large diffs are not rendered by default.

lightning/src/ln/functional_tests.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::chain::chaininterface::LowerBoundedFeeEstimator;
1717
use crate::chain::channelmonitor;
1818
use crate::chain::channelmonitor::{CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY};
1919
use crate::chain::transaction::OutPoint;
20-
use crate::sign::{ChannelSigner, EcdsaChannelSigner, EntropySource};
20+
use crate::sign::{EcdsaChannelSigner, EntropySource};
2121
use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, ClosureReason, HTLCDestination, PaymentFailureReason};
2222
use crate::ln::{PaymentPreimage, PaymentSecret, PaymentHash};
2323
use crate::ln::channel::{commitment_tx_base_weight, COMMITMENT_TX_WEIGHT_PER_HTLC, CONCURRENT_INBOUND_HTLC_FEE_BUFFER, FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE, MIN_AFFORDABLE_HTLC_COUNT, get_holder_selected_channel_reserve_satoshis, OutboundV1Channel, InboundV1Channel};
@@ -703,7 +703,7 @@ fn test_update_fee_that_funder_cannot_afford() {
703703
let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
704704
let local_chan = chan_lock.channel_by_id.get(&chan.2).unwrap();
705705
let chan_signer = local_chan.get_signer();
706-
let pubkeys = chan_signer.pubkeys();
706+
let pubkeys = chan_signer.as_ref().pubkeys();
707707
(pubkeys.revocation_basepoint, pubkeys.htlc_basepoint,
708708
pubkeys.funding_pubkey)
709709
};
@@ -712,9 +712,9 @@ fn test_update_fee_that_funder_cannot_afford() {
712712
let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
713713
let remote_chan = chan_lock.channel_by_id.get(&chan.2).unwrap();
714714
let chan_signer = remote_chan.get_signer();
715-
let pubkeys = chan_signer.pubkeys();
715+
let pubkeys = chan_signer.as_ref().pubkeys();
716716
(pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint,
717-
chan_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx),
717+
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx),
718718
pubkeys.funding_pubkey)
719719
};
720720

@@ -738,7 +738,7 @@ fn test_update_fee_that_funder_cannot_afford() {
738738
&mut htlcs,
739739
&local_chan.context.channel_transaction_parameters.as_counterparty_broadcastable()
740740
);
741-
local_chan_signer.sign_counterparty_commitment(&commitment_tx, Vec::new(), &secp_ctx).unwrap()
741+
local_chan_signer.as_ecdsa().unwrap().sign_counterparty_commitment(&commitment_tx, Vec::new(), &secp_ctx).unwrap()
742742
};
743743

744744
let commit_signed_msg = msgs::CommitmentSigned {
@@ -1416,23 +1416,23 @@ fn test_fee_spike_violation_fails_htlc() {
14161416
let local_chan = chan_lock.channel_by_id.get(&chan.2).unwrap();
14171417
let chan_signer = local_chan.get_signer();
14181418
// Make the signer believe we validated another commitment, so we can release the secret
1419-
chan_signer.get_enforcement_state().last_holder_commitment -= 1;
1419+
chan_signer.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
14201420

1421-
let pubkeys = chan_signer.pubkeys();
1421+
let pubkeys = chan_signer.as_ref().pubkeys();
14221422
(pubkeys.revocation_basepoint, pubkeys.htlc_basepoint,
1423-
chan_signer.release_commitment_secret(INITIAL_COMMITMENT_NUMBER),
1424-
chan_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 2, &secp_ctx),
1425-
chan_signer.pubkeys().funding_pubkey)
1423+
chan_signer.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER),
1424+
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 2, &secp_ctx),
1425+
chan_signer.as_ref().pubkeys().funding_pubkey)
14261426
};
14271427
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_point, remote_funding) = {
14281428
let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
14291429
let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
14301430
let remote_chan = chan_lock.channel_by_id.get(&chan.2).unwrap();
14311431
let chan_signer = remote_chan.get_signer();
1432-
let pubkeys = chan_signer.pubkeys();
1432+
let pubkeys = chan_signer.as_ref().pubkeys();
14331433
(pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint,
1434-
chan_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx),
1435-
chan_signer.pubkeys().funding_pubkey)
1434+
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx),
1435+
chan_signer.as_ref().pubkeys().funding_pubkey)
14361436
};
14371437

14381438
// Assemble the set of keys we can use for signatures for our commitment_signed message.
@@ -1468,7 +1468,7 @@ fn test_fee_spike_violation_fails_htlc() {
14681468
&mut vec![(accepted_htlc_info, ())],
14691469
&local_chan.context.channel_transaction_parameters.as_counterparty_broadcastable()
14701470
);
1471-
local_chan_signer.sign_counterparty_commitment(&commitment_tx, Vec::new(), &secp_ctx).unwrap()
1471+
local_chan_signer.as_ecdsa().unwrap().sign_counterparty_commitment(&commitment_tx, Vec::new(), &secp_ctx).unwrap()
14721472
};
14731473

14741474
let commit_signed_msg = msgs::CommitmentSigned {
@@ -6965,7 +6965,7 @@ fn test_user_configurable_csv_delay() {
69656965

69666966
// We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in OutboundV1Channel::new()
69676967
if let Err(error) = OutboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
6968-
&nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[1].node.init_features(), 1000000, 1000000, 0,
6968+
&nodes[0].keys_manager, nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[1].node.init_features(), 1000000, 1000000, 0,
69696969
&low_our_to_self_config, 0, 42)
69706970
{
69716971
match error {
@@ -6979,7 +6979,7 @@ fn test_user_configurable_csv_delay() {
69796979
let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
69806980
open_channel.to_self_delay = 200;
69816981
if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
6982-
&nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[0].node.channel_type_features(), &nodes[1].node.init_features(), &open_channel, 0,
6982+
&nodes[0].keys_manager, nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[0].node.channel_type_features(), &nodes[1].node.init_features(), &open_channel, 0,
69836983
&low_our_to_self_config, 0, &nodes[0].logger, 42)
69846984
{
69856985
match error {
@@ -7011,7 +7011,7 @@ fn test_user_configurable_csv_delay() {
70117011
let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
70127012
open_channel.to_self_delay = 200;
70137013
if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
7014-
&nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[0].node.channel_type_features(), &nodes[1].node.init_features(), &open_channel, 0,
7014+
&nodes[0].keys_manager, nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[0].node.channel_type_features(), &nodes[1].node.init_features(), &open_channel, 0,
70157015
&high_their_to_self_config, 0, &nodes[0].logger, 42)
70167016
{
70177017
match error {
@@ -7559,16 +7559,16 @@ fn test_counterparty_raa_skip_no_crash() {
75597559
const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
75607560

75617561
// Make signer believe we got a counterparty signature, so that it allows the revocation
7562-
keys.get_enforcement_state().last_holder_commitment -= 1;
7563-
per_commitment_secret = keys.release_commitment_secret(INITIAL_COMMITMENT_NUMBER);
7562+
keys.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
7563+
per_commitment_secret = keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER);
75647564

75657565
// Must revoke without gaps
7566-
keys.get_enforcement_state().last_holder_commitment -= 1;
7567-
keys.release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1);
7566+
keys.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
7567+
keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1);
75687568

7569-
keys.get_enforcement_state().last_holder_commitment -= 1;
7569+
keys.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
75707570
next_per_commitment_point = PublicKey::from_secret_key(&Secp256k1::new(),
7571-
&SecretKey::from_slice(&keys.release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2)).unwrap());
7571+
&SecretKey::from_slice(&keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2)).unwrap());
75727572
}
75737573

75747574
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(),

lightning/src/sign/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ use crate::util::atomic_counter::AtomicCounter;
5454
use crate::util::chacha20::ChaCha20;
5555
use crate::util::invoice::construct_invoice_preimage;
5656

57+
pub(crate) mod type_resolver;
58+
5759
/// Used as initial key material, to be expanded into multiple secret keys (but not to be used
5860
/// directly). This is used within LDK to encrypt/decrypt inbound payment data.
5961
///

lightning/src/sign/type_resolver.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use crate::sign::ChannelSigner;
2+
3+
pub(crate) enum ChannelSignerType<ECS: ChannelSigner> {
4+
// in practice, this will only ever be an EcdsaChannelSigner (specifically, Writeable)
5+
Ecdsa(ECS)
6+
}
7+
8+
impl<ECS: ChannelSigner> ChannelSignerType<ECS>{
9+
pub(crate) fn as_ref(&self) -> &dyn ChannelSigner {
10+
match self {
11+
ChannelSignerType::Ecdsa(ecs) => ecs
12+
}
13+
}
14+
15+
pub(crate) fn as_mut(&mut self) -> &mut dyn ChannelSigner {
16+
match self {
17+
ChannelSignerType::Ecdsa(ecs) => ecs
18+
}
19+
}
20+
21+
pub(crate) fn as_ecdsa(&self) -> Option<&ECS> {
22+
match self {
23+
ChannelSignerType::Ecdsa(ecs) => Some(ecs)
24+
}
25+
}
26+
27+
pub(crate) fn as_mut_ecdsa(&mut self) -> Option<&mut ECS> {
28+
match self {
29+
ChannelSignerType::Ecdsa(ecs) => Some(ecs)
30+
}
31+
}
32+
}

lightning/src/util/persist.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub trait KVStorePersister {
3434
}
3535

3636
/// Trait that handles persisting a [`ChannelManager`], [`NetworkGraph`], and [`WriteableScore`] to disk.
37-
pub trait Persister<'a, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref, S: WriteableScore<'a>>
37+
pub trait Persister<'a, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref + Clone, F: Deref, R: Deref, L: Deref, S: WriteableScore<'a>>
3838
where M::Target: 'static + chain::Watch<<SP::Target as SignerProvider>::Signer>,
3939
T::Target: 'static + BroadcasterInterface,
4040
ES::Target: 'static + EntropySource,
@@ -54,7 +54,7 @@ pub trait Persister<'a, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F:
5454
fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error>;
5555
}
5656

57-
impl<'a, A: KVStorePersister, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref, S: WriteableScore<'a>> Persister<'a, M, T, ES, NS, SP, F, R, L, S> for A
57+
impl<'a, A: KVStorePersister, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref + Clone, F: Deref, R: Deref, L: Deref, S: WriteableScore<'a>> Persister<'a, M, T, ES, NS, SP, F, R, L, S> for A
5858
where M::Target: 'static + chain::Watch<<SP::Target as SignerProvider>::Signer>,
5959
T::Target: 'static + BroadcasterInterface,
6060
ES::Target: 'static + EntropySource,

0 commit comments

Comments
 (0)