Skip to content

Commit 25a9fcf

Browse files
committed
wallet: Avoid use of Chain::Lock in importwallet and dumpwallet
This is a step toward removing the Chain::Lock class and reducing cs_main locking. This change only affects behavior in the case where wallet last block processed falls behind the chain tip, in which case it will use more accurate backup and rescan timestamps.
1 parent c1694ce commit 25a9fcf

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/wallet/rpcdump.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,7 @@ UniValue importwallet(const JSONRPCRequest& request)
566566
if (!file.is_open()) {
567567
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
568568
}
569-
Optional<int> tip_height = locked_chain->getHeight();
570-
nTimeBegin = tip_height ? locked_chain->getBlockTime(*tip_height) : 0;
569+
CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(nTimeBegin)));
571570

572571
int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg());
573572
file.seekg(0, file.beg);
@@ -791,9 +790,10 @@ UniValue dumpwallet(const JSONRPCRequest& request)
791790
// produce output
792791
file << strprintf("# Wallet dump created by Bitcoin %s\n", CLIENT_BUILD);
793792
file << strprintf("# * Created on %s\n", FormatISO8601DateTime(GetTime()));
794-
const Optional<int> tip_height = locked_chain->getHeight();
795-
file << strprintf("# * Best block at time of backup was %i (%s),\n", tip_height.get_value_or(-1), tip_height ? locked_chain->getBlockHash(*tip_height).ToString() : "(missing block hash)");
796-
file << strprintf("# mined on %s\n", tip_height ? FormatISO8601DateTime(locked_chain->getBlockTime(*tip_height)) : "(missing block time)");
793+
file << strprintf("# * Best block at time of backup was %i (%s),\n", pwallet->GetLastBlockHeight(), pwallet->GetLastBlockHash().ToString());
794+
int64_t block_time = 0;
795+
CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(block_time)));
796+
file << strprintf("# mined on %s\n", FormatISO8601DateTime(block_time));
797797
file << "\n";
798798

799799
// add the base58check encoded extended master if the wallet uses HD

src/wallet/test/wallet_tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
225225
request.params.setArray();
226226
request.params.push_back(backup_file);
227227
AddWallet(wallet);
228+
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
228229
::dumpwallet(request);
229230
RemoveWallet(wallet);
230231
}
@@ -233,16 +234,17 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
233234
// were scanned, and no prior blocks were scanned.
234235
{
235236
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
237+
LOCK(wallet->cs_wallet);
236238
wallet->SetupLegacyScriptPubKeyMan();
237239

238240
JSONRPCRequest request;
239241
request.params.setArray();
240242
request.params.push_back(backup_file);
241243
AddWallet(wallet);
244+
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
242245
::importwallet(request);
243246
RemoveWallet(wallet);
244247

245-
LOCK(wallet->cs_wallet);
246248
BOOST_CHECK_EQUAL(wallet->mapWallet.size(), 3U);
247249
BOOST_CHECK_EQUAL(m_coinbase_txns.size(), 103U);
248250
for (size_t i = 0; i < m_coinbase_txns.size(); ++i) {

0 commit comments

Comments
 (0)