Skip to content

Commit 9695296

Browse files
committed
Merge #1537: Use explicit names for wallet builder methods that validate rather than set
2391b76 refactor(wallet)!: rename LoadParams methods (thunderbiscuit) Pull request description: This PR is a follow up to the dev call discussion where we decided it was better to PR these changes separate from #1533. This is a breaking change, but I think it's worth it because those will potentially cause runtime errors for users that expect one thing to happen and realize it's something else. Left out of this PR but as surfaced in the call probably worth discussing is whether these methods make sense at all or whether they should be removed entirely. What does it mean to return an error when someone loads a wallet from persistence for which the genesis hash doesn't match the one persisted? Maybe worth a new issue; this PR simply attempts to name them correctly. ### Description See [Q1 in comment here](#1533 (comment)) for more context into the initial question. Two of the methods on the builder that loads a wallet were checkers/validators rather than setters: - `network()` - `genesis_hash()` This is confusing for users because when loading a wallet from persistence those things are persisted and cannot be changed, and yet seemingly the option to do that is there with those methods (so now you're re-thinking what you think you know about the changeset and persistence). Moreover, the fields on the [`LoadParams` type](https://docs.rs/bdk_wallet/1.0.0-beta.1/src/bdk_wallet/wallet/params.rs.html#116-124) are correctly named `check_network` and `check_genesis_hash`. This PR simply brings those two methods in line with what they actually do and set on the struct they modify. This modification was not done on the `descriptors()` method, because that one is both a validator and a setter if the descriptors passed contain private keys. Note that I chose the names `validate_network` and `validate_genesis_hash` but I'm not married to them. If others prefer `check_network` and `check_genesis_hash`, I'm happy to fix them that way instead! ### Changelog notice ```md Breaking: - The `LoadParams` type used in the wallet load builder renamed its `network()` and `genesis_hash` methods to `check_network()` and `check_genesis_hash`. [#1537] [#1537]: #1537 ``` ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### New Features: * [ ] I've added tests for the new feature * [ ] I've added docs for the new feature #### Bugfixes: * [x] This pull request breaks the existing API * [ ] I've added tests to reproduce the issue which are now passing * [ ] I'm linking the issue being fixed by this PR ACKs for top commit: notmandatory: ACK 2391b76 Tree-SHA512: 6852ad165bab230a003b92ae0408176055f8c9101a0936d2d5a41c2a3577e258b045a7f4b796d9bab29ed261caf80b738c4338d4ff9322fbddc8c273ab0ff914
2 parents cc84872 + 2391b76 commit 9695296

File tree

8 files changed

+14
-26
lines changed

8 files changed

+14
-26
lines changed

crates/wallet/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ let network = Network::Testnet;
7979
let descriptor = "wpkh(tprv8ZgxMBicQKsPdcAqYBpzAFwU5yxBUo88ggoBqu1qPcHUfSbKK1sKMLmC7EAk438btHQrSdu3jGGQa6PA71nvH5nkDexhLteJqkM4dQmWF9g/84'/1'/0'/0/*)";
8080
let change_descriptor = "wpkh(tprv8ZgxMBicQKsPdcAqYBpzAFwU5yxBUo88ggoBqu1qPcHUfSbKK1sKMLmC7EAk438btHQrSdu3jGGQa6PA71nvH5nkDexhLteJqkM4dQmWF9g/84'/1'/0'/1/*)";
8181
let wallet_opt = Wallet::load()
82-
.network(network)
8382
.descriptor(KeychainKind::External, Some(descriptor))
8483
.descriptor(KeychainKind::Internal, Some(change_descriptor))
8584
.extract_keys()
85+
.check_network(network)
8686
.load_wallet(&mut db)
8787
.expect("wallet");
8888
let mut wallet = match wallet_opt {

crates/wallet/src/wallet/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ impl Wallet {
482482
/// .keymap(KeychainKind::External, external_keymap)
483483
/// .keymap(KeychainKind::Internal, internal_keymap)
484484
/// // ensure loaded wallet's genesis hash matches this value
485-
/// .genesis_hash(genesis_hash)
485+
/// .check_genesis_hash(genesis_hash)
486486
/// // set a lookahead for our indexer
487487
/// .lookahead(101)
488488
/// .load_wallet(&mut conn)?

crates/wallet/src/wallet/params.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,14 @@ impl LoadParams {
194194
self
195195
}
196196

197-
/// Check for `network`.
198-
pub fn network(mut self, network: Network) -> Self {
197+
/// Checks that the given network matches the one loaded from persistence.
198+
pub fn check_network(mut self, network: Network) -> Self {
199199
self.check_network = Some(network);
200200
self
201201
}
202202

203-
/// Check for a `genesis_hash`.
204-
pub fn genesis_hash(mut self, genesis_hash: BlockHash) -> Self {
203+
/// Checks that the given `genesis_hash` matches the one loaded from persistence.
204+
pub fn check_genesis_hash(mut self, genesis_hash: BlockHash) -> Self {
205205
self.check_genesis_hash = Some(genesis_hash);
206206
self
207207
}

crates/wallet/tests/wallet.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ fn wallet_is_persisted() -> anyhow::Result<()> {
137137
{
138138
let mut db = open_db(&file_path).context("failed to recover db")?;
139139
let _ = Wallet::load()
140-
.network(Network::Testnet)
140+
.check_network(Network::Testnet)
141141
.load_wallet(&mut db)?
142142
.expect("wallet must exist");
143143
}
@@ -146,7 +146,7 @@ fn wallet_is_persisted() -> anyhow::Result<()> {
146146
let wallet = Wallet::load()
147147
.descriptor(KeychainKind::External, Some(external_desc))
148148
.descriptor(KeychainKind::Internal, Some(internal_desc))
149-
.network(Network::Testnet)
149+
.check_network(Network::Testnet)
150150
.load_wallet(&mut db)?
151151
.expect("wallet must exist");
152152

@@ -218,7 +218,7 @@ fn wallet_load_checks() -> anyhow::Result<()> {
218218

219219
assert_matches!(
220220
Wallet::load()
221-
.network(Network::Regtest)
221+
.check_network(Network::Regtest)
222222
.load_wallet(&mut open_db(&file_path)?),
223223
Err(LoadWithPersistError::InvalidChangeSet(LoadError::Mismatch(
224224
LoadMismatch::Network {
@@ -228,21 +228,9 @@ fn wallet_load_checks() -> anyhow::Result<()> {
228228
))),
229229
"unexpected network check result: Regtest (check) is not Testnet (loaded)",
230230
);
231-
assert_matches!(
232-
Wallet::load()
233-
.network(Network::Bitcoin)
234-
.load_wallet(&mut open_db(&file_path)?),
235-
Err(LoadWithPersistError::InvalidChangeSet(LoadError::Mismatch(
236-
LoadMismatch::Network {
237-
loaded: Network::Testnet,
238-
expected: Network::Bitcoin,
239-
}
240-
))),
241-
"unexpected network check result: Bitcoin (check) is not Testnet (loaded)",
242-
);
243231
let mainnet_hash = BlockHash::from_byte_array(ChainHash::BITCOIN.to_bytes());
244232
assert_matches!(
245-
Wallet::load().genesis_hash(mainnet_hash).load_wallet(&mut open_db(&file_path)?),
233+
Wallet::load().check_genesis_hash(mainnet_hash).load_wallet(&mut open_db(&file_path)?),
246234
Err(LoadWithPersistError::InvalidChangeSet(LoadError::Mismatch(LoadMismatch::Genesis { .. }))),
247235
"unexpected genesis hash check result: mainnet hash (check) is not testnet hash (loaded)",
248236
);

example-crates/wallet_electrum/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ fn main() -> Result<(), anyhow::Error> {
2626
let mut db = Store::<bdk_wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?;
2727

2828
let wallet_opt = Wallet::load()
29-
.network(NETWORK)
3029
.descriptor(KeychainKind::External, Some(EXTERNAL_DESC))
3130
.descriptor(KeychainKind::Internal, Some(INTERNAL_DESC))
3231
.extract_keys()
32+
.check_network(NETWORK)
3333
.load_wallet(&mut db)?;
3434
let mut wallet = match wallet_opt {
3535
Some(wallet) => wallet,

example-crates/wallet_esplora_async/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ async fn main() -> Result<(), anyhow::Error> {
2323
let mut conn = Connection::open(DB_PATH)?;
2424

2525
let wallet_opt = Wallet::load()
26-
.network(NETWORK)
2726
.descriptor(KeychainKind::External, Some(EXTERNAL_DESC))
2827
.descriptor(KeychainKind::Internal, Some(INTERNAL_DESC))
2928
.extract_keys()
29+
.check_network(NETWORK)
3030
.load_wallet(&mut conn)?;
3131
let mut wallet = match wallet_opt {
3232
Some(wallet) => wallet,

example-crates/wallet_esplora_blocking/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ fn main() -> Result<(), anyhow::Error> {
2222
let mut db = Store::<bdk_wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), DB_PATH)?;
2323

2424
let wallet_opt = Wallet::load()
25-
.network(NETWORK)
2625
.descriptor(KeychainKind::External, Some(EXTERNAL_DESC))
2726
.descriptor(KeychainKind::Internal, Some(INTERNAL_DESC))
2827
.extract_keys()
28+
.check_network(NETWORK)
2929
.load_wallet(&mut db)?;
3030
let mut wallet = match wallet_opt {
3131
Some(wallet) => wallet,

example-crates/wallet_rpc/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ fn main() -> anyhow::Result<()> {
8989
let mut db =
9090
Store::<bdk_wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), args.db_path)?;
9191
let wallet_opt = Wallet::load()
92-
.network(args.network)
9392
.descriptor(KeychainKind::External, Some(args.descriptor.clone()))
9493
.descriptor(KeychainKind::Internal, Some(args.change_descriptor.clone()))
9594
.extract_keys()
95+
.check_network(args.network)
9696
.load_wallet(&mut db)?;
9797
let mut wallet = match wallet_opt {
9898
Some(wallet) => wallet,

0 commit comments

Comments
 (0)