Skip to content

Commit fa60afc

Browse files
author
MarcoFalke
committed
wallet: Add BlockUntilSyncedToCurrentChain to dumpwallet
1 parent 79b0459 commit fa60afc

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/wallet/rpcdump.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -724,9 +724,8 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
724724

725725
UniValue dumpwallet(const JSONRPCRequest& request)
726726
{
727-
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
728-
const CWallet* const pwallet = wallet.get();
729-
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
727+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
728+
if (!EnsureWalletIsAvailable(pwallet.get(), request.fHelp)) {
730729
return NullUniValue;
731730
}
732731

@@ -750,12 +749,17 @@ UniValue dumpwallet(const JSONRPCRequest& request)
750749
},
751750
}.Check(request);
752751

753-
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*wallet);
752+
CWallet& wallet = *pwallet;
753+
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(wallet);
754+
755+
// Make sure the results are valid at least up to the most recent block
756+
// the user could have gotten from another RPC command prior to now
757+
wallet.BlockUntilSyncedToCurrentChain();
754758

755759
auto locked_chain = pwallet->chain().lock();
756760
LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
757761

758-
EnsureWalletIsUnlocked(pwallet);
762+
EnsureWalletIsUnlocked(&wallet);
759763

760764
fs::path filepath = request.params[0].get_str();
761765
filepath = fs::absolute(filepath);
@@ -791,9 +795,9 @@ UniValue dumpwallet(const JSONRPCRequest& request)
791795
// produce output
792796
file << strprintf("# Wallet dump created by Bitcoin %s\n", CLIENT_BUILD);
793797
file << strprintf("# * Created on %s\n", FormatISO8601DateTime(GetTime()));
794-
file << strprintf("# * Best block at time of backup was %i (%s),\n", pwallet->GetLastBlockHeight(), pwallet->GetLastBlockHash().ToString());
798+
file << strprintf("# * Best block at time of backup was %i (%s),\n", wallet.GetLastBlockHeight(), wallet.GetLastBlockHash().ToString());
795799
int64_t block_time = 0;
796-
CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(block_time)));
800+
CHECK_NONFATAL(wallet.chain().findBlock(wallet.GetLastBlockHash(), FoundBlock().time(block_time)));
797801
file << strprintf("# mined on %s\n", FormatISO8601DateTime(block_time));
798802
file << "\n";
799803

@@ -817,8 +821,8 @@ UniValue dumpwallet(const JSONRPCRequest& request)
817821
CKey key;
818822
if (spk_man.GetKey(keyid, key)) {
819823
file << strprintf("%s %s ", EncodeSecret(key), strTime);
820-
if (GetWalletAddressesForKey(&spk_man, pwallet, keyid, strAddr, strLabel)) {
821-
file << strprintf("label=%s", strLabel);
824+
if (GetWalletAddressesForKey(&spk_man, &wallet, keyid, strAddr, strLabel)) {
825+
file << strprintf("label=%s", strLabel);
822826
} else if (keyid == seed_id) {
823827
file << "hdseed=1";
824828
} else if (mapKeyPool.count(keyid)) {

src/wallet/test/wallet_tests.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,19 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
217217
// Import key into wallet and call dumpwallet to create backup file.
218218
{
219219
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
220-
auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan();
221-
LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore);
222-
spk_man->mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = KEY_TIME;
223-
spk_man->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
220+
{
221+
auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan();
222+
LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore);
223+
spk_man->mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = KEY_TIME;
224+
spk_man->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
224225

226+
AddWallet(wallet);
227+
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
228+
}
225229
JSONRPCRequest request;
226230
request.params.setArray();
227231
request.params.push_back(backup_file);
228-
AddWallet(wallet);
229-
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
232+
230233
::dumpwallet(request);
231234
RemoveWallet(wallet);
232235
}

0 commit comments

Comments
 (0)