Skip to content

Commit

Permalink
Merge bitcoin#18671: wallet: Add BlockUntilSyncedToCurrentChain to du…
Browse files Browse the repository at this point in the history
…mpwallet

fa60afc wallet: Add BlockUntilSyncedToCurrentChain to dumpwallet (MarcoFalke)

Pull request description:

  dumpwallet includes the block hash in the output, so this method depends on the chainstate. According to the developer notes https://github.com/bitcoin/bitcoin/blame/e84a5f000493fe39adb2a5f22b43c3848dcd0a4f/doc/developer-notes.md#L1095 it must include a `BlockUntilSyncedToCurrentChain`.

  This is a minor fix and does not need backport, I think.

  It fixes test failures such as https://travis-ci.org/github/bitcoin/bitcoin/jobs/675487097#L2657 , which can only happen in master because the test was not backported.

ACKs for top commit:
  promag:
    Code review ACK fa60afc.
  ryanofsky:
    Code review ACK fa60afc
  meshcollider:
    utACK fa60afc

Tree-SHA512: 8df70b06b226b2cdf880dec9264adb72d66fd81b09b404fd1665a79e5f5236d26122eebf15df00fe71ee292b5c91b2dc23a0a42b2aa50a8d690604b23832723f
  • Loading branch information
meshcollider authored and knst committed Sep 19, 2023
1 parent 9f1d5e5 commit 391c8b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
28 changes: 16 additions & 12 deletions src/wallet/rpcdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,15 +908,19 @@ UniValue dumpwallet(const JSONRPCRequest& request)
},
}.Check(request);

std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
if (!wallet) return NullUniValue;
const CWallet* const pwallet = wallet.get();
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
if (!pwallet) return NullUniValue;

LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*wallet);
CWallet& wallet = *pwallet;
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(wallet);

LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
wallet.BlockUntilSyncedToCurrentChain();

EnsureWalletIsUnlocked(pwallet);
LOCK2(wallet.cs_wallet, spk_man.cs_KeyStore);

EnsureWalletIsUnlocked(&wallet);

fs::path filepath = request.params[0].get_str();
filepath = fs::absolute(filepath);
Expand All @@ -937,7 +941,7 @@ UniValue dumpwallet(const JSONRPCRequest& request)

std::map<CKeyID, int64_t> mapKeyBirth;
const std::map<CKeyID, int64_t>& mapKeyPool = spk_man.GetAllReserveKeys();
pwallet->GetKeyBirthTimes(mapKeyBirth);
wallet.GetKeyBirthTimes(mapKeyBirth);

std::set<CScriptID> scripts = spk_man.GetCScripts();

Expand All @@ -952,16 +956,16 @@ UniValue dumpwallet(const JSONRPCRequest& request)
// produce output
file << strprintf("# Wallet dump created by Dash Core %s\n", CLIENT_BUILD);
file << strprintf("# * Created on %s\n", FormatISO8601DateTime(GetTime()));
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", wallet.GetLastBlockHeight(), wallet.GetLastBlockHash().ToString());
int64_t block_time = 0;
CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(block_time)));
CHECK_NONFATAL(wallet.chain().findBlock(wallet.GetLastBlockHash(), FoundBlock().time(block_time)));
file << strprintf("# mined on %s\n", FormatISO8601DateTime(block_time));
file << "\n";

UniValue obj(UniValue::VOBJ);
obj.pushKV("dashcoreversion", CLIENT_BUILD);
obj.pushKV("lastblockheight", pwallet->GetLastBlockHeight());
obj.pushKV("lastblockhash", pwallet->GetLastBlockHash().ToString());
obj.pushKV("lastblockheight", wallet.GetLastBlockHeight());
obj.pushKV("lastblockhash", wallet.GetLastBlockHash().ToString());
obj.pushKV("lastblocktime", block_time);

// add the base58check encoded extended master if the wallet uses HD
Expand Down Expand Up @@ -1012,7 +1016,7 @@ UniValue dumpwallet(const JSONRPCRequest& request)
CKey key;
if (spk_man.GetKey(keyid, key)) {
file << strprintf("%s %s ", EncodeSecret(key), strTime);
const auto* address_book_entry = pwallet->FindAddressBookEntry(pkhash);
const auto* address_book_entry = wallet.FindAddressBookEntry(pkhash);
if (address_book_entry) {
file << strprintf("label=%s", EncodeDumpString(address_book_entry->GetLabel()));
} else if (mapKeyPool.count(keyid)) {
Expand Down
16 changes: 10 additions & 6 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,21 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
// Import key into wallet and call dumpwallet to create backup file.
{
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", CreateDummyWalletDatabase());
auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan();
LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore);
spk_man->mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = KEY_TIME;
spk_man->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
{
auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan();
LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore);
spk_man->mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = KEY_TIME;
spk_man->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());

AddWallet(wallet);
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
}
CoreContext context{m_node};
JSONRPCRequest request(context);

request.params.setArray();
request.params.push_back(backup_file);
AddWallet(wallet);
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());

::dumpwallet(request);
RemoveWallet(wallet, std::nullopt);
}
Expand Down

0 comments on commit 391c8b3

Please sign in to comment.