Skip to content

Add NetworkMismatch error type #485

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 1 commit into from
Mar 25, 2025

Conversation

elnosh
Copy link
Contributor

@elnosh elnosh commented Mar 17, 2025

for #117

Added a NetworkMismatch error type if the node is restarted with a different network. This is caught when trying to load bdk wallet.

Copy link
Collaborator

@tnull tnull left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this!

src/builder.rs Outdated
@@ -189,6 +191,7 @@ impl fmt::Display for BuildError {
Self::WalletSetupFailed => write!(f, "Failed to setup onchain wallet."),
Self::LoggerSetupFailed => write!(f, "Failed to setup the logger."),
Self::InvalidNodeAlias => write!(f, "Given node alias is invalid."),
Self::NetworkMismatch => write!(f, "Given network does match."),
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Could you specify what it doesn't match?

src/builder.rs Outdated
BuildError::WalletSetupFailed
.map_err(|e| match e {
bdk_wallet::LoadWithPersistError::InvalidChangeSet(
bdk_wallet::LoadError::Mismatch(bdk_wallet::LoadMismatch::Network { .. }),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems we might also want to match on the Genesis variant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmmm, since the genesis hash is not a configuration that can be directly set by the user I think the check on the network mismatch will already catch it? Because to do the check on the genesis hash we would first need to get it with something like: bitcoin::blockdata::constants::genesis_block(config.network).block_hash(); which depends on the network provided by the user. So it doesn't seem possible to pass the network check but fail the Genesis one. That's how I saw it but lmk if this is wrong and I'll change it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I mean, technically yes, but are we sure BDK won't ever emit a Genesis variant if we set the wrong Network?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if we set the wrong network, yes I guess any of these two errors (Network or Genesis mismatch) could be thrown.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right, that's why I'd prefer to match on both and return the mismatch error if either variant is returned.

@elnosh elnosh force-pushed the check-network-switch branch from 8991768 to 206a092 Compare March 19, 2025 12:37
Copy link
Contributor

@G8XSU G8XSU left a comment

Choose a reason for hiding this comment

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

Changes look good to me overall.

src/builder.rs Outdated
@@ -189,6 +191,7 @@ impl fmt::Display for BuildError {
Self::WalletSetupFailed => write!(f, "Failed to setup onchain wallet."),
Self::LoggerSetupFailed => write!(f, "Failed to setup the logger."),
Self::InvalidNodeAlias => write!(f, "Given node alias is invalid."),
Self::NetworkMismatch => write!(f, "Given network does not match the node's network."),
Copy link
Contributor

Choose a reason for hiding this comment

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

nit : Given network does not match the node's previously configured network.

@elnosh elnosh force-pushed the check-network-switch branch from 206a092 to b1b2caf Compare March 20, 2025 21:19
Copy link
Collaborator

@tnull tnull left a comment

Choose a reason for hiding this comment

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

Excuse the delay here.

src/builder.rs Outdated
@@ -903,10 +908,19 @@ fn build_with_store_internal(
.descriptor(KeychainKind::Internal, Some(change_descriptor.clone()))
.extract_keys()
.check_network(config.network)
.check_genesis_hash(constants::genesis_block(config.network).block_hash())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, I'd rather not include this line. My main point below was that BDK's docs aren't super clear on when which variant would be emitted, so we might as well match on the Genesis variant just to be sure. But we probably don't want do both check_network and check_genesis.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh ok. Yeah I'm not sure either, maybe I should ask them, but from looking at their docs I thought the Genesis error would only be thrown if you pre-call that check_genesis_hash method. I thought that because for each of the 3 LoadMismatch variants there is a respective check_[variant] method.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right. I don’t think it matters too much, so we shouldn’t block this PR on it. Should be fine whether we match on Genesis or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I checked with them. The errors will not be returned unless the check_* methods are called so I removed the Genesis one.

src/builder.rs Outdated
.map_err(|e| match e {
bdk_wallet::LoadWithPersistError::InvalidChangeSet(
bdk_wallet::LoadError::Mismatch(bdk_wallet::LoadMismatch::Network { .. }),
) => BuildError::NetworkMismatch,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we still log that we failed to setup the wallet due to mismatching network here?

@elnosh elnosh force-pushed the check-network-switch branch from b1b2caf to ebc5548 Compare March 25, 2025 13:10
Return specific error if the node is restarted with a different network
from the one it was first created with. This will be caught when trying
to load the `BDK` wallet.
@elnosh elnosh force-pushed the check-network-switch branch from ebc5548 to ca49cb7 Compare March 25, 2025 16:17
Copy link
Collaborator

@tnull tnull left a comment

Choose a reason for hiding this comment

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

LGTM

@tnull tnull merged commit 9c02f23 into lightningdevkit:main Mar 25, 2025
10 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants