Skip to content

Fix clippy error size #248

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 46 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
set quiet := true
msrv := "1.63"

# list of recipes
default:
just --list

# format the project code
fmt:
cargo +nightly fmt

# lint the project, skip examples if using msrv
clippy: fmt
if rustc --version | fgrep -q "{{msrv}}"; then \
cargo clippy --workspace --exclude 'example_*' --all-features --tests; \
else \
cargo clippy --all-features --tests; \
fi

# build the project, skip examples if using msrv
build: fmt
if rustc --version | fgrep -q "{{msrv}}"; then \
cargo build --workspace --exclude 'example_*' --all-features --tests; \
else \
cargo build --all-features --tests; \
fi

# test the project, skip examples if using msrv
test:
if rustc --version | fgrep -q "{{msrv}}"; then \
cargo test --workspace --exclude 'example_*' --all-features --tests; \
else \
cargo test --all-features --tests; \
fi

# clean the project target directory
clean:
cargo clean

# set the rust version to stable
stable: clean
rustup override set stable; cargo update

# set the rust version to the msrv and pin dependencies
msrv: clean
rustup override set {{msrv}}; cargo update; ./ci/pin-msrv.sh
2 changes: 1 addition & 1 deletion rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.85.0
1.87.0
32 changes: 15 additions & 17 deletions wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ pub enum LoadMismatch {
/// Keychain identifying the descriptor.
keychain: KeychainKind,
/// The loaded descriptor.
loaded: Option<ExtendedDescriptor>,
loaded: Box<Option<ExtendedDescriptor>>,
/// The expected descriptor.
expected: Option<ExtendedDescriptor>,
expected: Box<Option<ExtendedDescriptor>>,
},
}

Expand Down Expand Up @@ -258,11 +258,9 @@ impl fmt::Display for LoadMismatch {
f,
"Descriptor mismatch for {} keychain: loaded {}, expected {}",
keychain,
loaded
.as_ref()
.map_or("None".to_string(), |d| d.to_string()),
loaded.clone().map_or("None".to_string(), |d| d.to_string()),
expected
.as_ref()
.clone()
.map_or("None".to_string(), |d| d.to_string())
)
}
Expand All @@ -278,7 +276,7 @@ impl From<LoadMismatch> for LoadError {

impl<E> From<LoadMismatch> for LoadWithPersistError<E> {
fn from(mismatch: LoadMismatch) -> Self {
Self::InvalidChangeSet(LoadError::Mismatch(mismatch))
Self::InvalidChangeSet(Box::new(LoadError::Mismatch(mismatch)))
}
}

Expand Down Expand Up @@ -559,8 +557,8 @@ impl Wallet {
if descriptor.descriptor_id() != exp_desc.descriptor_id() {
return Err(LoadError::Mismatch(LoadMismatch::Descriptor {
keychain: KeychainKind::External,
loaded: Some(descriptor),
expected: Some(exp_desc),
loaded: Box::new(Some(descriptor)),
expected: Box::new(Some(exp_desc)),
}));
}
if params.extract_keys {
Expand All @@ -569,8 +567,8 @@ impl Wallet {
} else {
return Err(LoadError::Mismatch(LoadMismatch::Descriptor {
keychain: KeychainKind::External,
loaded: Some(descriptor),
expected: None,
loaded: Box::new(Some(descriptor)),
expected: Box::new(None),
}));
}
}
Expand All @@ -588,8 +586,8 @@ impl Wallet {
let (exp_desc, _) = make_desc(&secp, network).map_err(LoadError::Descriptor)?;
return Err(LoadError::Mismatch(LoadMismatch::Descriptor {
keychain: KeychainKind::Internal,
loaded: None,
expected: Some(exp_desc),
loaded: Box::new(None),
expected: Box::new(Some(exp_desc)),
}));
}
}
Expand All @@ -603,8 +601,8 @@ impl Wallet {
None => {
return Err(LoadError::Mismatch(LoadMismatch::Descriptor {
keychain: KeychainKind::Internal,
loaded: Some(desc),
expected: None,
loaded: Box::new(Some(desc)),
expected: Box::new(None),
}))
}
// parameters must match
Expand All @@ -615,8 +613,8 @@ impl Wallet {
if desc.descriptor_id() != exp_desc.descriptor_id() {
return Err(LoadError::Mismatch(LoadMismatch::Descriptor {
keychain: KeychainKind::Internal,
loaded: Some(desc),
expected: Some(exp_desc),
loaded: Box::new(Some(desc)),
expected: Box::new(Some(exp_desc)),
}));
}
if params.extract_keys {
Expand Down
20 changes: 12 additions & 8 deletions wallet/src/wallet/persisted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ impl<P: WalletPersister> PersistedWallet<P> {
) -> Result<Self, CreateWithPersistError<P::Error>> {
let existing = P::initialize(persister).map_err(CreateWithPersistError::Persist)?;
if !existing.is_empty() {
return Err(CreateWithPersistError::DataAlreadyExists(existing));
return Err(CreateWithPersistError::DataAlreadyExists(Box::new(
existing,
)));
}
let mut inner =
Wallet::create_with_params(params).map_err(CreateWithPersistError::Descriptor)?;
Expand All @@ -176,7 +178,7 @@ impl<P: WalletPersister> PersistedWallet<P> {
_marker: PhantomData,
})
})
.map_err(LoadWithPersistError::InvalidChangeSet)
.map_err(|cs| LoadWithPersistError::InvalidChangeSet(Box::new(cs)))
}

/// Persist staged changes of wallet into `persister`.
Expand Down Expand Up @@ -207,7 +209,9 @@ impl<P: AsyncWalletPersister> PersistedWallet<P> {
.await
.map_err(CreateWithPersistError::Persist)?;
if !existing.is_empty() {
return Err(CreateWithPersistError::DataAlreadyExists(existing));
return Err(CreateWithPersistError::DataAlreadyExists(Box::new(
existing,
)));
}
let mut inner =
Wallet::create_with_params(params).map_err(CreateWithPersistError::Descriptor)?;
Expand Down Expand Up @@ -237,7 +241,7 @@ impl<P: AsyncWalletPersister> PersistedWallet<P> {
_marker: PhantomData,
})
})
.map_err(LoadWithPersistError::InvalidChangeSet)
.map_err(|cs| LoadWithPersistError::InvalidChangeSet(Box::new(cs)))
}

/// Persist staged changes of wallet into an async `persister`.
Expand Down Expand Up @@ -295,7 +299,7 @@ impl WalletPersister for bdk_chain::rusqlite::Connection {
#[derive(Debug)]
pub enum FileStoreError {
/// Error when loading from the store.
Load(bdk_file_store::StoreErrorWithDump<ChangeSet>),
Load(Box<bdk_file_store::StoreErrorWithDump<ChangeSet>>),
/// Error when writing to the store.
Write(std::io::Error),
}
Expand All @@ -322,7 +326,7 @@ impl WalletPersister for bdk_file_store::Store<ChangeSet> {
persister
.dump()
.map(Option::unwrap_or_default)
.map_err(FileStoreError::Load)
.map_err(|e| FileStoreError::Load(Box::new(e)))
}

fn persist(persister: &mut Self, changeset: &ChangeSet) -> Result<(), Self::Error> {
Expand All @@ -336,7 +340,7 @@ pub enum LoadWithPersistError<E> {
/// Error from persistence.
Persist(E),
/// Occurs when the loaded changeset cannot construct [`Wallet`].
InvalidChangeSet(crate::LoadError),
InvalidChangeSet(Box<crate::LoadError>),
}

impl<E: fmt::Display> fmt::Display for LoadWithPersistError<E> {
Expand All @@ -357,7 +361,7 @@ pub enum CreateWithPersistError<E> {
/// Error from persistence.
Persist(E),
/// Persister already has wallet data.
DataAlreadyExists(ChangeSet),
DataAlreadyExists(Box<ChangeSet>),
/// Occurs when the loaded changeset cannot construct [`Wallet`].
Descriptor(DescriptorError),
}
Expand Down
61 changes: 46 additions & 15 deletions wallet/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,40 +151,64 @@ fn wallet_load_checks() -> anyhow::Result<()> {
.network(network)
.create_wallet(&mut create_db(&file_path)?)?;

let _expected_err = LoadWithPersistError::<Db::Error>::InvalidChangeSet(Box::new(
LoadError::Mismatch(LoadMismatch::Network {
loaded: Network::Testnet,
expected: Network::Regtest,
}),
));
assert_matches!(
Wallet::load()
.check_network(Network::Regtest)
.load_wallet(&mut open_db(&file_path)?),
Err(LoadWithPersistError::InvalidChangeSet(LoadError::Mismatch(
LoadMismatch::Network {
loaded: Network::Testnet,
expected: Network::Regtest,
}
))),
Err(_expected_err),
"unexpected network check result: Regtest (check) is not Testnet (loaded)",
);
let mainnet_hash = BlockHash::from_byte_array(ChainHash::BITCOIN.to_bytes());
let _expected_err = LoadWithPersistError::<Db::Error>::InvalidChangeSet(Box::new(
LoadError::Mismatch(LoadMismatch::Genesis {
loaded: BlockHash::from_byte_array(ChainHash::REGTEST.to_bytes()),
expected: mainnet_hash,
}),
));
assert_matches!(
Wallet::load().check_genesis_hash(mainnet_hash).load_wallet(&mut open_db(&file_path)?),
Err(LoadWithPersistError::InvalidChangeSet(LoadError::Mismatch(LoadMismatch::Genesis { .. }))),
Err(_expected_err),
"unexpected genesis hash check result: mainnet hash (check) is not testnet hash (loaded)",
);
let secp = Secp256k1::new();
let external_desc_pub = Descriptor::parse_descriptor(&secp, external_desc)
.expect("invalid external descriptor")
.0;
let internal_desc_pub = Descriptor::parse_descriptor(&secp, internal_desc)
.expect("invalid internal descriptor")
.0;
let _expected_err = LoadWithPersistError::<Db::Error>::InvalidChangeSet(Box::new(
LoadError::Mismatch(LoadMismatch::Descriptor {
keychain: KeychainKind::External,
loaded: Box::new(Some(external_desc_pub.clone())),
expected: Box::new(Some(internal_desc_pub.clone())),
}),
));
assert_matches!(
Wallet::load()
.descriptor(KeychainKind::External, Some(internal_desc))
.load_wallet(&mut open_db(&file_path)?),
Err(LoadWithPersistError::InvalidChangeSet(LoadError::Mismatch(
LoadMismatch::Descriptor { .. }
))),
Err(_expected_err),
"unexpected descriptors check result",
);
let _expected_err = LoadWithPersistError::<Db::Error>::InvalidChangeSet(Box::new(
LoadError::Mismatch(LoadMismatch::Descriptor {
keychain: KeychainKind::External,
loaded: Box::new(Some(internal_desc_pub.clone())),
expected: Box::new(None),
}),
));
assert_matches!(
Wallet::load()
.descriptor(KeychainKind::External, Option::<&str>::None)
.load_wallet(&mut open_db(&file_path)?),
Err(LoadWithPersistError::InvalidChangeSet(LoadError::Mismatch(
LoadMismatch::Descriptor { .. }
))),
Err(_expected_err),
"unexpected descriptors check result",
);
// check setting keymaps
Expand Down Expand Up @@ -318,10 +342,17 @@ fn single_descriptor_wallet_persist_and_recover() {
.descriptor(KeychainKind::Internal, Some(desc))
.extract_keys()
.load_wallet(&mut db);
let _expected_err = LoadWithPersistError::<rusqlite::Error>::InvalidChangeSet(Box::new(
LoadError::Mismatch(LoadMismatch::Descriptor {
keychain: KeychainKind::Internal,
loaded: Box::new(None),
expected: Box::new(Some(exp_desc)),
}),
));
//if keychain == && loaded.is_none() && expected == Some(exp_desc);
assert_matches!(
err,
Err(LoadWithPersistError::InvalidChangeSet(LoadError::Mismatch(LoadMismatch::Descriptor { keychain, loaded, expected })))
if keychain == KeychainKind::Internal && loaded.is_none() && expected == Some(exp_desc),
Err(_expected_err),
"single descriptor wallet should refuse change descriptor param"
);
}
Expand Down
Loading