mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-05 14:06:27 -05:00
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.
This commit is contained in:
parent
c1694ce6bb
commit
25a9fcf9e5
2 changed files with 8 additions and 6 deletions
|
@ -566,8 +566,7 @@ UniValue importwallet(const JSONRPCRequest& request)
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
|
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
|
||||||
}
|
}
|
||||||
Optional<int> tip_height = locked_chain->getHeight();
|
CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(nTimeBegin)));
|
||||||
nTimeBegin = tip_height ? locked_chain->getBlockTime(*tip_height) : 0;
|
|
||||||
|
|
||||||
int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg());
|
int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg());
|
||||||
file.seekg(0, file.beg);
|
file.seekg(0, file.beg);
|
||||||
|
@ -791,9 +790,10 @@ UniValue dumpwallet(const JSONRPCRequest& request)
|
||||||
// produce output
|
// produce output
|
||||||
file << strprintf("# Wallet dump created by Bitcoin %s\n", CLIENT_BUILD);
|
file << strprintf("# Wallet dump created by Bitcoin %s\n", CLIENT_BUILD);
|
||||||
file << strprintf("# * Created on %s\n", FormatISO8601DateTime(GetTime()));
|
file << strprintf("# * Created on %s\n", FormatISO8601DateTime(GetTime()));
|
||||||
const Optional<int> tip_height = locked_chain->getHeight();
|
file << strprintf("# * Best block at time of backup was %i (%s),\n", pwallet->GetLastBlockHeight(), pwallet->GetLastBlockHash().ToString());
|
||||||
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)");
|
int64_t block_time = 0;
|
||||||
file << strprintf("# mined on %s\n", tip_height ? FormatISO8601DateTime(locked_chain->getBlockTime(*tip_height)) : "(missing block time)");
|
CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(block_time)));
|
||||||
|
file << strprintf("# mined on %s\n", FormatISO8601DateTime(block_time));
|
||||||
file << "\n";
|
file << "\n";
|
||||||
|
|
||||||
// add the base58check encoded extended master if the wallet uses HD
|
// add the base58check encoded extended master if the wallet uses HD
|
||||||
|
|
|
@ -225,6 +225,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
|
||||||
request.params.setArray();
|
request.params.setArray();
|
||||||
request.params.push_back(backup_file);
|
request.params.push_back(backup_file);
|
||||||
AddWallet(wallet);
|
AddWallet(wallet);
|
||||||
|
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
|
||||||
::dumpwallet(request);
|
::dumpwallet(request);
|
||||||
RemoveWallet(wallet);
|
RemoveWallet(wallet);
|
||||||
}
|
}
|
||||||
|
@ -233,16 +234,17 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
|
||||||
// were scanned, and no prior blocks were scanned.
|
// were scanned, and no prior blocks were scanned.
|
||||||
{
|
{
|
||||||
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
|
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
|
||||||
|
LOCK(wallet->cs_wallet);
|
||||||
wallet->SetupLegacyScriptPubKeyMan();
|
wallet->SetupLegacyScriptPubKeyMan();
|
||||||
|
|
||||||
JSONRPCRequest request;
|
JSONRPCRequest request;
|
||||||
request.params.setArray();
|
request.params.setArray();
|
||||||
request.params.push_back(backup_file);
|
request.params.push_back(backup_file);
|
||||||
AddWallet(wallet);
|
AddWallet(wallet);
|
||||||
|
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
|
||||||
::importwallet(request);
|
::importwallet(request);
|
||||||
RemoveWallet(wallet);
|
RemoveWallet(wallet);
|
||||||
|
|
||||||
LOCK(wallet->cs_wallet);
|
|
||||||
BOOST_CHECK_EQUAL(wallet->mapWallet.size(), 3U);
|
BOOST_CHECK_EQUAL(wallet->mapWallet.size(), 3U);
|
||||||
BOOST_CHECK_EQUAL(m_coinbase_txns.size(), 103U);
|
BOOST_CHECK_EQUAL(m_coinbase_txns.size(), 103U);
|
||||||
for (size_t i = 0; i < m_coinbase_txns.size(); ++i) {
|
for (size_t i = 0; i < m_coinbase_txns.size(); ++i) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue