Skip to content

Implement PartialEq for ChannelMonitor #1986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 2 additions & 51 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ pub struct ChannelMonitor<Signer: WriteableEcdsaChannelSigner> {
inner: Mutex<ChannelMonitorImpl<Signer>>,
}

#[derive(PartialEq)]
pub(crate) struct ChannelMonitorImpl<Signer: WriteableEcdsaChannelSigner> {
latest_update_id: u64,
commitment_transaction_number_obscure_factor: u64,
Expand Down Expand Up @@ -854,64 +855,14 @@ pub(crate) struct ChannelMonitorImpl<Signer: WriteableEcdsaChannelSigner> {
/// Transaction outputs to watch for on-chain spends.
pub type TransactionOutputs = (Txid, Vec<(u32, TxOut)>);

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

#[cfg(any(test, fuzzing, feature = "_test_utils"))]
/// Used only in testing and fuzzing to check serialization roundtrips don't change the underlying
/// object
impl<Signer: WriteableEcdsaChannelSigner> PartialEq for ChannelMonitorImpl<Signer> {
fn eq(&self, other: &Self) -> bool {
if self.latest_update_id != other.latest_update_id ||
self.commitment_transaction_number_obscure_factor != other.commitment_transaction_number_obscure_factor ||
self.destination_script != other.destination_script ||
self.broadcasted_holder_revokable_script != other.broadcasted_holder_revokable_script ||
self.counterparty_payment_script != other.counterparty_payment_script ||
self.channel_keys_id != other.channel_keys_id ||
self.holder_revocation_basepoint != other.holder_revocation_basepoint ||
self.funding_info != other.funding_info ||
self.current_counterparty_commitment_txid != other.current_counterparty_commitment_txid ||
self.prev_counterparty_commitment_txid != other.prev_counterparty_commitment_txid ||
self.counterparty_commitment_params != other.counterparty_commitment_params ||
self.funding_redeemscript != other.funding_redeemscript ||
self.channel_value_satoshis != other.channel_value_satoshis ||
self.their_cur_per_commitment_points != other.their_cur_per_commitment_points ||
self.on_holder_tx_csv != other.on_holder_tx_csv ||
self.commitment_secrets != other.commitment_secrets ||
self.counterparty_claimable_outpoints != other.counterparty_claimable_outpoints ||
self.counterparty_commitment_txn_on_chain != other.counterparty_commitment_txn_on_chain ||
self.counterparty_hash_commitment_number != other.counterparty_hash_commitment_number ||
self.prev_holder_signed_commitment_tx != other.prev_holder_signed_commitment_tx ||
self.current_counterparty_commitment_number != other.current_counterparty_commitment_number ||
self.current_holder_commitment_number != other.current_holder_commitment_number ||
self.current_holder_commitment_tx != other.current_holder_commitment_tx ||
self.payment_preimages != other.payment_preimages ||
self.pending_monitor_events != other.pending_monitor_events ||
self.pending_events.len() != other.pending_events.len() || // We trust events to round-trip properly
self.onchain_events_awaiting_threshold_conf != other.onchain_events_awaiting_threshold_conf ||
self.outputs_to_watch != other.outputs_to_watch ||
self.lockdown_from_offchain != other.lockdown_from_offchain ||
self.holder_tx_signed != other.holder_tx_signed ||
self.funding_spend_seen != other.funding_spend_seen ||
self.funding_spend_confirmed != other.funding_spend_confirmed ||
self.confirmed_commitment_tx_counterparty_output != other.confirmed_commitment_tx_counterparty_output ||
self.htlcs_resolved_on_chain != other.htlcs_resolved_on_chain
{
false
} else {
true
}
}
}

impl<Signer: WriteableEcdsaChannelSigner> Writeable for ChannelMonitor<Signer> {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
self.inner.lock().unwrap().write(writer)
Expand Down
1 change: 1 addition & 0 deletions lightning/src/chain/onchaintx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ type PackageID = [u8; 32];

/// OnchainTxHandler receives claiming requests, aggregates them if it's sound, broadcast and
/// do RBF bumping if possible.
#[derive(PartialEq)]
pub struct OnchainTxHandler<ChannelSigner: WriteableEcdsaChannelSigner> {
destination_script: Script,
holder_commitment: HolderCommitmentTransaction,
Expand Down
6 changes: 6 additions & 0 deletions lightning/src/util/enforcing_trait_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ pub struct EnforcingSigner {
pub disable_revocation_policy_check: bool,
}

impl PartialEq for EnforcingSigner {
fn eq(&self, o: &Self) -> bool {
Arc::ptr_eq(&self.state, &o.state)
Copy link
Contributor

@dunxen dunxen Jan 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interesting when I checked out this function and went down a bit of a rabbit hole and found out that currently for dyn Trait raw pointers, Rust also checks for equality of vtables which has caused what most would naturally consider false negatives as it was unexpected from the docs: rust-lang/rust#106447

We're at least good for EnforcementState structs though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh! Yea, that's surprising. I mean indeed doesn't impact us here (it snot even a dyn trait), but still.

}
}

impl EnforcingSigner {
/// Construct an EnforcingSigner
pub fn new(inner: InMemorySigner) -> Self {
Expand Down