Skip to content

Commit 8757eae

Browse files
committed
Introduce ChannelSignerType.
1 parent 4289b1f commit 8757eae

File tree

6 files changed

+188
-154
lines changed

6 files changed

+188
-154
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ impl BackgroundProcessor {
724724
T: 'static + Deref + Send + Sync,
725725
ES: 'static + Deref + Send + Sync,
726726
NS: 'static + Deref + Send + Sync,
727-
SP: 'static + Deref + Send + Sync,
727+
SP: 'static + Deref + Clone + Send + Sync,
728728
F: 'static + Deref + Send + Sync,
729729
R: 'static + Deref + Send + Sync,
730730
G: 'static + Deref<Target = NetworkGraph<L>> + Send + Sync,

lightning/src/ln/channel.rs

Lines changed: 101 additions & 103 deletions
Large diffs are not rendered by default.

lightning/src/ln/channelmanager.rs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError};
5555
use crate::ln::outbound_payment;
5656
use crate::ln::outbound_payment::{OutboundPayments, PaymentAttempts, PendingOutboundPayment, SendAlongPathArgs};
5757
use crate::ln::wire::Encode;
58-
use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient, SignerProvider, ChannelSigner, WriteableEcdsaChannelSigner};
58+
use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient, SignerProvider, WriteableEcdsaChannelSigner};
5959
use crate::util::config::{UserConfig, ChannelConfig, ChannelConfigUpdate};
6060
use crate::util::wakers::{Future, Notifier};
6161
use crate::util::scid_utils::fake_scid;
@@ -643,23 +643,23 @@ impl_writeable_tlv_based_enum!(RAAMonitorUpdateBlockingAction,
643643

644644

645645
/// State we hold per-peer.
646-
pub(super) struct PeerState<Signer: ChannelSigner> {
646+
pub(super) struct PeerState<SP: Deref> where SP::Target: SignerProvider {
647647
/// `channel_id` -> `Channel`.
648648
///
649649
/// Holds all funded channels where the peer is the counterparty.
650-
pub(super) channel_by_id: HashMap<[u8; 32], Channel<Signer>>,
650+
pub(super) channel_by_id: HashMap<[u8; 32], Channel<SP>>,
651651
/// `temporary_channel_id` -> `OutboundV1Channel`.
652652
///
653653
/// Holds all outbound V1 channels where the peer is the counterparty. Once an outbound channel has
654654
/// been assigned a `channel_id`, the entry in this map is removed and one is created in
655655
/// `channel_by_id`.
656-
pub(super) outbound_v1_channel_by_id: HashMap<[u8; 32], OutboundV1Channel<Signer>>,
656+
pub(super) outbound_v1_channel_by_id: HashMap<[u8; 32], OutboundV1Channel<SP>>,
657657
/// `temporary_channel_id` -> `InboundV1Channel`.
658658
///
659659
/// Holds all inbound V1 channels where the peer is the counterparty. Once an inbound channel has
660660
/// been assigned a `channel_id`, the entry in this map is removed and one is created in
661661
/// `channel_by_id`.
662-
pub(super) inbound_v1_channel_by_id: HashMap<[u8; 32], InboundV1Channel<Signer>>,
662+
pub(super) inbound_v1_channel_by_id: HashMap<[u8; 32], InboundV1Channel<SP>>,
663663
/// `temporary_channel_id` -> `InboundChannelRequest`.
664664
///
665665
/// When manual channel acceptance is enabled, this holds all unaccepted inbound channels where
@@ -705,7 +705,7 @@ pub(super) struct PeerState<Signer: ChannelSigner> {
705705
is_connected: bool,
706706
}
707707

708-
impl <Signer: ChannelSigner> PeerState<Signer> {
708+
impl <SP: Deref> PeerState<SP> where SP::Target: SignerProvider {
709709
/// Indicates that a peer meets the criteria where we're ok to remove it from our storage.
710710
/// If true is passed for `require_disconnected`, the function will return false if we haven't
711711
/// disconnected from the node already, ie. `PeerState::is_connected` is set to `true`.
@@ -1130,9 +1130,9 @@ where
11301130
///
11311131
/// See `ChannelManager` struct-level documentation for lock order requirements.
11321132
#[cfg(not(any(test, feature = "_test_utils")))]
1133-
per_peer_state: FairRwLock<HashMap<PublicKey, Mutex<PeerState<<SP::Target as SignerProvider>::Signer>>>>,
1133+
per_peer_state: FairRwLock<HashMap<PublicKey, Mutex<PeerState<SP>>>>,
11341134
#[cfg(any(test, feature = "_test_utils"))]
1135-
pub(super) per_peer_state: FairRwLock<HashMap<PublicKey, Mutex<PeerState<<SP::Target as SignerProvider>::Signer>>>>,
1135+
pub(super) per_peer_state: FairRwLock<HashMap<PublicKey, Mutex<PeerState<SP>>>>,
11361136

11371137
/// The set of events which we need to give to the user to handle. In some cases an event may
11381138
/// require some further action after the user handles it (currently only blocking a monitor
@@ -1574,11 +1574,13 @@ impl ChannelDetails {
15741574
self.short_channel_id.or(self.outbound_scid_alias)
15751575
}
15761576

1577-
fn from_channel_context<Signer: WriteableEcdsaChannelSigner, F: Deref>(
1578-
context: &ChannelContext<Signer>, best_block_height: u32, latest_features: InitFeatures,
1577+
fn from_channel_context<SP: Deref, F: Deref>(
1578+
context: &ChannelContext<SP>, best_block_height: u32, latest_features: InitFeatures,
15791579
fee_estimator: &LowerBoundedFeeEstimator<F>
15801580
) -> Self
1581-
where F::Target: FeeEstimator
1581+
where
1582+
SP::Target: SignerProvider,
1583+
F::Target: FeeEstimator
15821584
{
15831585
let balance = context.get_available_balances(fee_estimator);
15841586
let (to_remote_reserve_satoshis, to_self_reserve_satoshis) =
@@ -2279,7 +2281,7 @@ where
22792281
Ok(temporary_channel_id)
22802282
}
22812283

2282-
fn list_funded_channels_with_filter<Fn: FnMut(&(&[u8; 32], &Channel<<SP::Target as SignerProvider>::Signer>)) -> bool + Copy>(&self, f: Fn) -> Vec<ChannelDetails> {
2284+
fn list_funded_channels_with_filter<Fn: FnMut(&(&[u8; 32], &Channel<SP>)) -> bool + Copy>(&self, f: Fn) -> Vec<ChannelDetails> {
22832285
// Allocate our best estimate of the number of channels we have in the `res`
22842286
// Vec. Sadly the `short_to_chan_info` map doesn't cover channels without
22852287
// a scid or a scid alias, and the `id_to_peer` shouldn't be used outside
@@ -2405,7 +2407,7 @@ where
24052407
}
24062408

24072409
/// Helper function that issues the channel close events
2408-
fn issue_channel_close_events(&self, context: &ChannelContext<<SP::Target as SignerProvider>::Signer>, closure_reason: ClosureReason) {
2410+
fn issue_channel_close_events(&self, context: &ChannelContext<SP>, closure_reason: ClosureReason) {
24092411
let mut pending_events_lock = self.pending_events.lock().unwrap();
24102412
match context.unbroadcasted_funding() {
24112413
Some(transaction) => {
@@ -3095,7 +3097,7 @@ where
30953097
///
30963098
/// [`channel_update`]: msgs::ChannelUpdate
30973099
/// [`internal_closing_signed`]: Self::internal_closing_signed
3098-
fn get_channel_update_for_broadcast(&self, chan: &Channel<<SP::Target as SignerProvider>::Signer>) -> Result<msgs::ChannelUpdate, LightningError> {
3100+
fn get_channel_update_for_broadcast(&self, chan: &Channel<SP>) -> Result<msgs::ChannelUpdate, LightningError> {
30993101
if !chan.context.should_announce() {
31003102
return Err(LightningError {
31013103
err: "Cannot broadcast a channel_update for a private channel".to_owned(),
@@ -3120,7 +3122,7 @@ where
31203122
///
31213123
/// [`channel_update`]: msgs::ChannelUpdate
31223124
/// [`internal_closing_signed`]: Self::internal_closing_signed
3123-
fn get_channel_update_for_unicast(&self, chan: &Channel<<SP::Target as SignerProvider>::Signer>) -> Result<msgs::ChannelUpdate, LightningError> {
3125+
fn get_channel_update_for_unicast(&self, chan: &Channel<SP>) -> Result<msgs::ChannelUpdate, LightningError> {
31243126
log_trace!(self.logger, "Attempting to generate channel update for channel {}", log_bytes!(chan.context.channel_id()));
31253127
let short_channel_id = match chan.context.get_short_channel_id().or(chan.context.latest_inbound_scid_alias()) {
31263128
None => return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError}),
@@ -3130,7 +3132,7 @@ where
31303132
self.get_channel_update_for_onion(short_channel_id, chan)
31313133
}
31323134

3133-
fn get_channel_update_for_onion(&self, short_channel_id: u64, chan: &Channel<<SP::Target as SignerProvider>::Signer>) -> Result<msgs::ChannelUpdate, LightningError> {
3135+
fn get_channel_update_for_onion(&self, short_channel_id: u64, chan: &Channel<SP>) -> Result<msgs::ChannelUpdate, LightningError> {
31343136
log_trace!(self.logger, "Generating channel update for channel {}", log_bytes!(chan.context.channel_id()));
31353137
let were_node_one = self.our_network_pubkey.serialize()[..] < chan.context.get_counterparty_node_id().serialize()[..];
31363138

@@ -3424,7 +3426,7 @@ where
34243426

34253427
/// Handles the generation of a funding transaction, optionally (for tests) with a function
34263428
/// which checks the correctness of the funding transaction given the associated channel.
3427-
fn funding_transaction_generated_intern<FundingOutput: Fn(&OutboundV1Channel<<SP::Target as SignerProvider>::Signer>, &Transaction) -> Result<OutPoint, APIError>>(
3429+
fn funding_transaction_generated_intern<FundingOutput: Fn(&OutboundV1Channel<SP>, &Transaction) -> Result<OutPoint, APIError>>(
34283430
&self, temporary_channel_id: &[u8; 32], counterparty_node_id: &PublicKey, funding_transaction: Transaction, find_funding_output: FundingOutput
34293431
) -> Result<(), APIError> {
34303432
let per_peer_state = self.per_peer_state.read().unwrap();
@@ -4337,7 +4339,7 @@ where
43374339
let _ = self.process_background_events();
43384340
}
43394341

4340-
fn update_channel_fee(&self, chan_id: &[u8; 32], chan: &mut Channel<<SP::Target as SignerProvider>::Signer>, new_feerate: u32) -> NotifyOption {
4342+
fn update_channel_fee(&self, chan_id: &[u8; 32], chan: &mut Channel<SP>, new_feerate: u32) -> NotifyOption {
43414343
if !chan.context.is_outbound() { return NotifyOption::SkipPersist; }
43424344
// If the feerate has decreased by less than half, don't bother
43434345
if new_feerate <= chan.context.get_feerate_sat_per_1000_weight() && new_feerate * 2 > chan.context.get_feerate_sat_per_1000_weight() {
@@ -4496,7 +4498,7 @@ where
44964498

44974499
let process_unfunded_channel_tick = |
44984500
chan_id: &[u8; 32],
4499-
chan_context: &mut ChannelContext<<SP::Target as SignerProvider>::Signer>,
4501+
chan_context: &mut ChannelContext<SP>,
45004502
unfunded_chan_context: &mut UnfundedChannelContext,
45014503
pending_msg_events: &mut Vec<MessageSendEvent>,
45024504
| {
@@ -4686,7 +4688,7 @@ where
46864688
///
46874689
/// This is for failures on the channel on which the HTLC was *received*, not failures
46884690
/// forwarding
4689-
fn get_htlc_inbound_temp_fail_err_and_data(&self, desired_err_code: u16, chan: &Channel<<SP::Target as SignerProvider>::Signer>) -> (u16, Vec<u8>) {
4691+
fn get_htlc_inbound_temp_fail_err_and_data(&self, desired_err_code: u16, chan: &Channel<SP>) -> (u16, Vec<u8>) {
46904692
// We can't be sure what SCID was used when relaying inbound towards us, so we have to
46914693
// guess somewhat. If its a public channel, we figure best to just use the real SCID (as
46924694
// we're not leaking that we have a channel with the counterparty), otherwise we try to use
@@ -4706,7 +4708,7 @@ where
47064708

47074709
/// Gets an HTLC onion failure code and error data for an `UPDATE` error, given the error code
47084710
/// that we want to return and a channel.
4709-
fn get_htlc_temp_fail_err_and_data(&self, desired_err_code: u16, scid: u64, chan: &Channel<<SP::Target as SignerProvider>::Signer>) -> (u16, Vec<u8>) {
4711+
fn get_htlc_temp_fail_err_and_data(&self, desired_err_code: u16, scid: u64, chan: &Channel<SP>) -> (u16, Vec<u8>) {
47104712
debug_assert_eq!(desired_err_code & 0x1000, 0x1000);
47114713
if let Ok(upd) = self.get_channel_update_for_onion(scid, chan) {
47124714
let mut enc = VecWriter(Vec::with_capacity(upd.serialized_length() + 6));
@@ -5150,7 +5152,7 @@ where
51505152
/// Handles a channel reentering a functional state, either due to reconnect or a monitor
51515153
/// update completion.
51525154
fn handle_channel_resumption(&self, pending_msg_events: &mut Vec<MessageSendEvent>,
5153-
channel: &mut Channel<<SP::Target as SignerProvider>::Signer>, raa: Option<msgs::RevokeAndACK>,
5155+
channel: &mut Channel<SP>, raa: Option<msgs::RevokeAndACK>,
51545156
commitment_update: Option<msgs::CommitmentUpdate>, order: RAACommitmentOrder,
51555157
pending_forwards: Vec<(PendingHTLCInfo, u64)>, funding_broadcastable: Option<Transaction>,
51565158
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>)
@@ -5385,7 +5387,7 @@ where
53855387
/// The filter is called for each peer and provided with the number of unfunded, inbound, and
53865388
/// non-0-conf channels we have with the peer.
53875389
fn peers_without_funded_channels<Filter>(&self, maybe_count_peer: Filter) -> usize
5388-
where Filter: Fn(&PeerState<<SP::Target as SignerProvider>::Signer>) -> bool {
5390+
where Filter: Fn(&PeerState<SP>) -> bool {
53895391
let mut peers_without_funded_channels = 0;
53905392
let best_block_height = self.best_block.read().unwrap().height();
53915393
{
@@ -5403,7 +5405,7 @@ where
54035405
}
54045406

54055407
fn unfunded_channel_count(
5406-
peer: &PeerState<<SP::Target as SignerProvider>::Signer>, best_block_height: u32
5408+
peer: &PeerState<SP>, best_block_height: u32
54075409
) -> usize {
54085410
let mut num_unfunded_channels = 0;
54095411
for (_, chan) in peer.channel_by_id.iter() {
@@ -5849,7 +5851,7 @@ where
58495851
chan.get().context.config().accept_underpaying_htlcs, next_packet_pk_opt),
58505852
Err(e) => PendingHTLCStatus::Fail(e)
58515853
};
5852-
let create_pending_htlc_status = |chan: &Channel<<SP::Target as SignerProvider>::Signer>, pending_forward_info: PendingHTLCStatus, error_code: u16| {
5854+
let create_pending_htlc_status = |chan: &Channel<SP>, pending_forward_info: PendingHTLCStatus, error_code: u16| {
58535855
// If the update_add is completely bogus, the call will Err and we will close,
58545856
// but if we've sent a shutdown and they haven't acknowledged it yet, we just
58555857
// want to reject the new HTLC and fail it backwards instead of forwarding.
@@ -6999,7 +7001,7 @@ where
69997001
/// Calls a function which handles an on-chain event (blocks dis/connected, transactions
70007002
/// un/confirmed, etc) on each channel, handling any resulting errors or messages generated by
70017003
/// the function.
7002-
fn do_chain_event<FN: Fn(&mut Channel<<SP::Target as SignerProvider>::Signer>) -> Result<(Option<msgs::ChannelReady>, Vec<(HTLCSource, PaymentHash)>, Option<msgs::AnnouncementSignatures>), ClosureReason>>
7004+
fn do_chain_event<FN: Fn(&mut Channel<SP>) -> Result<(Option<msgs::ChannelReady>, Vec<(HTLCSource, PaymentHash)>, Option<msgs::AnnouncementSignatures>), ClosureReason>>
70037005
(&self, height_opt: Option<u32>, f: FN) {
70047006
// Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
70057007
// during initialization prior to the chain_monitor being fully configured in some cases.
@@ -8503,13 +8505,13 @@ where
85038505

85048506
let channel_count: u64 = Readable::read(reader)?;
85058507
let mut funding_txo_set = HashSet::with_capacity(cmp::min(channel_count as usize, 128));
8506-
let mut peer_channels: HashMap<PublicKey, HashMap<[u8; 32], Channel<<SP::Target as SignerProvider>::Signer>>> = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
8508+
let mut peer_channels: HashMap<PublicKey, HashMap<[u8; 32], Channel<SP>>> = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
85078509
let mut id_to_peer = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
85088510
let mut short_to_chan_info = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
85098511
let mut channel_closures = VecDeque::new();
85108512
let mut close_background_events = Vec::new();
85118513
for _ in 0..channel_count {
8512-
let mut channel: Channel<<SP::Target as SignerProvider>::Signer> = Channel::read(reader, (
8514+
let mut channel: Channel<SP> = Channel::read(reader, (
85138515
&args.entropy_source, &args.signer_provider, best_block_height, &provided_channel_type_features(&args.default_config)
85148516
))?;
85158517
let funding_txo = channel.context.get_funding_txo().ok_or(DecodeError::InvalidValue)?;
@@ -8654,7 +8656,7 @@ where
86548656
};
86558657

86568658
let peer_count: u64 = Readable::read(reader)?;
8657-
let mut per_peer_state = HashMap::with_capacity(cmp::min(peer_count as usize, MAX_ALLOC_SIZE/mem::size_of::<(PublicKey, Mutex<PeerState<<SP::Target as SignerProvider>::Signer>>)>()));
8659+
let mut per_peer_state = HashMap::with_capacity(cmp::min(peer_count as usize, MAX_ALLOC_SIZE/mem::size_of::<(PublicKey, Mutex<PeerState<SP>>)>()));
86588660
for _ in 0..peer_count {
86598661
let peer_pubkey = Readable::read(reader)?;
86608662
let peer_chans = peer_channels.remove(&peer_pubkey).unwrap_or(HashMap::new());

0 commit comments

Comments
 (0)