Skip to content

Commit 21ae60b

Browse files
authored
Merge pull request #1986 from TheBlueMatt/2023-01-monitor-eq
Implement PartialEq for ChannelMonitor
2 parents 8bc3428 + 53bc6db commit 21ae60b

File tree

5 files changed

+19
-70
lines changed

5 files changed

+19
-70
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 9 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ pub struct ChannelMonitor<Signer: WriteableEcdsaChannelSigner> {
713713
inner: Mutex<ChannelMonitorImpl<Signer>>,
714714
}
715715

716+
#[derive(PartialEq)]
716717
pub(crate) struct ChannelMonitorImpl<Signer: WriteableEcdsaChannelSigner> {
717718
latest_update_id: u64,
718719
commitment_transaction_number_obscure_factor: u64,
@@ -847,71 +848,19 @@ pub(crate) struct ChannelMonitorImpl<Signer: WriteableEcdsaChannelSigner> {
847848

848849
/// The node_id of our counterparty
849850
counterparty_node_id: Option<PublicKey>,
850-
851-
secp_ctx: Secp256k1<secp256k1::All>, //TODO: dedup this a bit...
852851
}
853852

854853
/// Transaction outputs to watch for on-chain spends.
855854
pub type TransactionOutputs = (Txid, Vec<(u32, TxOut)>);
856855

857-
#[cfg(any(test, fuzzing, feature = "_test_utils"))]
858-
/// Used only in testing and fuzzing to check serialization roundtrips don't change the underlying
859-
/// object
860-
impl<Signer: WriteableEcdsaChannelSigner> PartialEq for ChannelMonitor<Signer> {
856+
impl<Signer: WriteableEcdsaChannelSigner> PartialEq for ChannelMonitor<Signer> where Signer: PartialEq {
861857
fn eq(&self, other: &Self) -> bool {
862858
let inner = self.inner.lock().unwrap();
863859
let other = other.inner.lock().unwrap();
864860
inner.eq(&other)
865861
}
866862
}
867863

868-
#[cfg(any(test, fuzzing, feature = "_test_utils"))]
869-
/// Used only in testing and fuzzing to check serialization roundtrips don't change the underlying
870-
/// object
871-
impl<Signer: WriteableEcdsaChannelSigner> PartialEq for ChannelMonitorImpl<Signer> {
872-
fn eq(&self, other: &Self) -> bool {
873-
if self.latest_update_id != other.latest_update_id ||
874-
self.commitment_transaction_number_obscure_factor != other.commitment_transaction_number_obscure_factor ||
875-
self.destination_script != other.destination_script ||
876-
self.broadcasted_holder_revokable_script != other.broadcasted_holder_revokable_script ||
877-
self.counterparty_payment_script != other.counterparty_payment_script ||
878-
self.channel_keys_id != other.channel_keys_id ||
879-
self.holder_revocation_basepoint != other.holder_revocation_basepoint ||
880-
self.funding_info != other.funding_info ||
881-
self.current_counterparty_commitment_txid != other.current_counterparty_commitment_txid ||
882-
self.prev_counterparty_commitment_txid != other.prev_counterparty_commitment_txid ||
883-
self.counterparty_commitment_params != other.counterparty_commitment_params ||
884-
self.funding_redeemscript != other.funding_redeemscript ||
885-
self.channel_value_satoshis != other.channel_value_satoshis ||
886-
self.their_cur_per_commitment_points != other.their_cur_per_commitment_points ||
887-
self.on_holder_tx_csv != other.on_holder_tx_csv ||
888-
self.commitment_secrets != other.commitment_secrets ||
889-
self.counterparty_claimable_outpoints != other.counterparty_claimable_outpoints ||
890-
self.counterparty_commitment_txn_on_chain != other.counterparty_commitment_txn_on_chain ||
891-
self.counterparty_hash_commitment_number != other.counterparty_hash_commitment_number ||
892-
self.prev_holder_signed_commitment_tx != other.prev_holder_signed_commitment_tx ||
893-
self.current_counterparty_commitment_number != other.current_counterparty_commitment_number ||
894-
self.current_holder_commitment_number != other.current_holder_commitment_number ||
895-
self.current_holder_commitment_tx != other.current_holder_commitment_tx ||
896-
self.payment_preimages != other.payment_preimages ||
897-
self.pending_monitor_events != other.pending_monitor_events ||
898-
self.pending_events.len() != other.pending_events.len() || // We trust events to round-trip properly
899-
self.onchain_events_awaiting_threshold_conf != other.onchain_events_awaiting_threshold_conf ||
900-
self.outputs_to_watch != other.outputs_to_watch ||
901-
self.lockdown_from_offchain != other.lockdown_from_offchain ||
902-
self.holder_tx_signed != other.holder_tx_signed ||
903-
self.funding_spend_seen != other.funding_spend_seen ||
904-
self.funding_spend_confirmed != other.funding_spend_confirmed ||
905-
self.confirmed_commitment_tx_counterparty_output != other.confirmed_commitment_tx_counterparty_output ||
906-
self.htlcs_resolved_on_chain != other.htlcs_resolved_on_chain
907-
{
908-
false
909-
} else {
910-
true
911-
}
912-
}
913-
}
914-
915864
impl<Signer: WriteableEcdsaChannelSigner> Writeable for ChannelMonitor<Signer> {
916865
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
917866
self.inner.lock().unwrap().write(writer)
@@ -1140,7 +1089,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
11401089

11411090
let onchain_tx_handler =
11421091
OnchainTxHandler::new(destination_script.clone(), keys,
1143-
channel_parameters.clone(), initial_holder_commitment_tx, secp_ctx.clone());
1092+
channel_parameters.clone(), initial_holder_commitment_tx, secp_ctx);
11441093

11451094
let mut outputs_to_watch = HashMap::new();
11461095
outputs_to_watch.insert(funding_info.0.txid, vec![(funding_info.0.index as u32, funding_info.1.clone())]);
@@ -1196,8 +1145,6 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
11961145

11971146
best_block,
11981147
counterparty_node_id: Some(counterparty_node_id),
1199-
1200-
secp_ctx,
12011148
})
12021149
}
12031150

@@ -2512,9 +2459,9 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
25122459
if commitment_number >= self.get_min_seen_secret() {
25132460
let secret = self.get_secret(commitment_number).unwrap();
25142461
let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
2515-
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
2516-
let revocation_pubkey = chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &self.holder_revocation_basepoint);
2517-
let delayed_key = chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &self.counterparty_commitment_params.counterparty_delayed_payment_base_key);
2462+
let per_commitment_point = PublicKey::from_secret_key(&self.onchain_tx_handler.secp_ctx, &per_commitment_key);
2463+
let revocation_pubkey = chan_utils::derive_public_revocation_key(&self.onchain_tx_handler.secp_ctx, &per_commitment_point, &self.holder_revocation_basepoint);
2464+
let delayed_key = chan_utils::derive_public_key(&self.onchain_tx_handler.secp_ctx, &PublicKey::from_secret_key(&self.onchain_tx_handler.secp_ctx, &per_commitment_key), &self.counterparty_commitment_params.counterparty_delayed_payment_base_key);
25182465

25192466
let revokeable_redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.counterparty_commitment_params.on_counterparty_tx_csv, &delayed_key);
25202467
let revokeable_p2wsh = revokeable_redeemscript.to_v0_p2wsh();
@@ -2627,8 +2574,8 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
26272574

26282575
if let Some(transaction) = tx {
26292576
let revocation_pubkey = chan_utils::derive_public_revocation_key(
2630-
&self.secp_ctx, &per_commitment_point, &self.holder_revocation_basepoint);
2631-
let delayed_key = chan_utils::derive_public_key(&self.secp_ctx,
2577+
&self.onchain_tx_handler.secp_ctx, &per_commitment_point, &self.holder_revocation_basepoint);
2578+
let delayed_key = chan_utils::derive_public_key(&self.onchain_tx_handler.secp_ctx,
26322579
&per_commitment_point,
26332580
&self.counterparty_commitment_params.counterparty_delayed_payment_base_key);
26342581
let revokeable_p2wsh = chan_utils::get_revokeable_redeemscript(&revocation_pubkey,
@@ -2685,7 +2632,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
26852632
Ok(key) => key,
26862633
Err(_) => return (Vec::new(), None)
26872634
};
2688-
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
2635+
let per_commitment_point = PublicKey::from_secret_key(&self.onchain_tx_handler.secp_ctx, &per_commitment_key);
26892636

26902637
let htlc_txid = tx.txid();
26912638
let mut claimable_outpoints = vec![];
@@ -3931,9 +3878,6 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
39313878
(13, spendable_txids_confirmed, vec_type),
39323879
});
39333880

3934-
let mut secp_ctx = Secp256k1::new();
3935-
secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
3936-
39373881
Ok((best_block.block_hash(), ChannelMonitor::from_impl(ChannelMonitorImpl {
39383882
latest_update_id,
39393883
commitment_transaction_number_obscure_factor,
@@ -3985,8 +3929,6 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
39853929

39863930
best_block,
39873931
counterparty_node_id,
3988-
3989-
secp_ctx,
39903932
})))
39913933
}
39923934
}

lightning/src/chain/onchaintx.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ type PackageID = [u8; 32];
219219

220220
/// OnchainTxHandler receives claiming requests, aggregates them if it's sound, broadcast and
221221
/// do RBF bumping if possible.
222+
#[derive(PartialEq)]
222223
pub struct OnchainTxHandler<ChannelSigner: WriteableEcdsaChannelSigner> {
223224
destination_script: Script,
224225
holder_commitment: HolderCommitmentTransaction,

lightning/src/routing/router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl Readable for Route {
363363
/// [`Event::PaymentPathFailed`] for retrying a failed payment path.
364364
///
365365
/// [`Event::PaymentPathFailed`]: crate::util::events::Event::PaymentPathFailed
366-
#[derive(Clone, Debug)]
366+
#[derive(Clone, Debug, PartialEq, Eq)]
367367
pub struct RouteParameters {
368368
/// The parameters of the failed payment path.
369369
pub payment_params: PaymentParameters,

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ pub struct EnforcingSigner {
5858
pub disable_revocation_policy_check: bool,
5959
}
6060

61+
impl PartialEq for EnforcingSigner {
62+
fn eq(&self, o: &Self) -> bool {
63+
Arc::ptr_eq(&self.state, &o.state)
64+
}
65+
}
66+
6167
impl EnforcingSigner {
6268
/// Construct an EnforcingSigner
6369
pub fn new(inner: InMemorySigner) -> Self {

lightning/src/util/events.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use crate::sync::Arc;
4646

4747
/// Some information provided on receipt of payment depends on whether the payment received is a
4848
/// spontaneous payment or a "conventional" lightning payment that's paying an invoice.
49-
#[derive(Clone, Debug)]
49+
#[derive(Clone, Debug, PartialEq, Eq)]
5050
pub enum PaymentPurpose {
5151
/// Information for receiving a payment that we generated an invoice for.
5252
InvoicePayment {
@@ -455,7 +455,7 @@ impl_writeable_tlv_based_enum!(InterceptNextHop,
455455
/// Note that while Writeable and Readable are implemented for Event, you probably shouldn't use
456456
/// them directly as they don't round-trip exactly (for example FundingGenerationReady is never
457457
/// written as it makes no sense to respond to it after reconnecting to peers).
458-
#[derive(Clone, Debug)]
458+
#[derive(Clone, Debug, PartialEq, Eq)]
459459
pub enum Event {
460460
/// Used to indicate that the client should generate a funding transaction with the given
461461
/// parameters and then call [`ChannelManager::funding_transaction_generated`].

0 commit comments

Comments
 (0)