mirror of
https://github.com/bitcoin/bitcoin.git
synced 2024-05-17 23:56:39 +00:00
Refactor importwallet to extract data from the file and then import
Instead of importing keys and scripts as each line in the file is read, first extract the data then import them.
This commit is contained in:
parent
1f77f6754c
commit
b5c5021b64
@ -578,8 +578,10 @@ UniValue importwallet(const JSONRPCRequest& request)
|
|||||||
// Use uiInterface.ShowProgress instead of pwallet.ShowProgress because pwallet.ShowProgress has a cancel button tied to AbortRescan which
|
// Use uiInterface.ShowProgress instead of pwallet.ShowProgress because pwallet.ShowProgress has a cancel button tied to AbortRescan which
|
||||||
// we don't want for this progress bar showing the import progress. uiInterface.ShowProgress does not have a cancel button.
|
// we don't want for this progress bar showing the import progress. uiInterface.ShowProgress does not have a cancel button.
|
||||||
uiInterface.ShowProgress(strprintf("%s " + _("Importing..."), pwallet->GetDisplayName()), 0, false); // show progress dialog in GUI
|
uiInterface.ShowProgress(strprintf("%s " + _("Importing..."), pwallet->GetDisplayName()), 0, false); // show progress dialog in GUI
|
||||||
|
std::vector<std::tuple<CKey, int64_t, bool, std::string>> keys;
|
||||||
|
std::vector<std::pair<CScript, int64_t>> scripts;
|
||||||
while (file.good()) {
|
while (file.good()) {
|
||||||
uiInterface.ShowProgress("", std::max(1, std::min(99, (int)(((double)file.tellg() / (double)nFilesize) * 100))), false);
|
uiInterface.ShowProgress("", std::max(1, std::min(50, (int)(((double)file.tellg() / (double)nFilesize) * 100))), false);
|
||||||
std::string line;
|
std::string line;
|
||||||
std::getline(file, line);
|
std::getline(file, line);
|
||||||
if (line.empty() || line[0] == '#')
|
if (line.empty() || line[0] == '#')
|
||||||
@ -591,13 +593,6 @@ UniValue importwallet(const JSONRPCRequest& request)
|
|||||||
continue;
|
continue;
|
||||||
CKey key = DecodeSecret(vstr[0]);
|
CKey key = DecodeSecret(vstr[0]);
|
||||||
if (key.IsValid()) {
|
if (key.IsValid()) {
|
||||||
CPubKey pubkey = key.GetPubKey();
|
|
||||||
assert(key.VerifyPubKey(pubkey));
|
|
||||||
CKeyID keyid = pubkey.GetID();
|
|
||||||
if (pwallet->HaveKey(keyid)) {
|
|
||||||
pwallet->WalletLogPrintf("Skipping import of %s (key already present)\n", EncodeDestination(keyid));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
int64_t nTime = DecodeDumpTime(vstr[1]);
|
int64_t nTime = DecodeDumpTime(vstr[1]);
|
||||||
std::string strLabel;
|
std::string strLabel;
|
||||||
bool fLabel = true;
|
bool fLabel = true;
|
||||||
@ -613,36 +608,62 @@ UniValue importwallet(const JSONRPCRequest& request)
|
|||||||
fLabel = true;
|
fLabel = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
keys.push_back(std::make_tuple(key, nTime, fLabel, strLabel));
|
||||||
|
} else if(IsHex(vstr[0])) {
|
||||||
|
std::vector<unsigned char> vData(ParseHex(vstr[0]));
|
||||||
|
CScript script = CScript(vData.begin(), vData.end());
|
||||||
|
int64_t birth_time = DecodeDumpTime(vstr[1]);
|
||||||
|
scripts.push_back(std::pair<CScript, int64_t>(script, birth_time));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
double total = (double)(keys.size() + scripts.size());
|
||||||
|
double progress = 0;
|
||||||
|
for (const auto& key_tuple : keys) {
|
||||||
|
uiInterface.ShowProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
|
||||||
|
const CKey& key = std::get<0>(key_tuple);
|
||||||
|
int64_t time = std::get<1>(key_tuple);
|
||||||
|
bool has_label = std::get<2>(key_tuple);
|
||||||
|
std::string label = std::get<3>(key_tuple);
|
||||||
|
|
||||||
|
CPubKey pubkey = key.GetPubKey();
|
||||||
|
assert(key.VerifyPubKey(pubkey));
|
||||||
|
CKeyID keyid = pubkey.GetID();
|
||||||
|
if (pwallet->HaveKey(keyid)) {
|
||||||
|
pwallet->WalletLogPrintf("Skipping import of %s (key already present)\n", EncodeDestination(keyid));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
pwallet->WalletLogPrintf("Importing %s...\n", EncodeDestination(keyid));
|
pwallet->WalletLogPrintf("Importing %s...\n", EncodeDestination(keyid));
|
||||||
if (!pwallet->AddKeyPubKey(key, pubkey)) {
|
if (!pwallet->AddKeyPubKey(key, pubkey)) {
|
||||||
fGood = false;
|
fGood = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
pwallet->mapKeyMetadata[keyid].nCreateTime = nTime;
|
pwallet->mapKeyMetadata[keyid].nCreateTime = time;
|
||||||
if (fLabel)
|
if (has_label)
|
||||||
pwallet->SetAddressBook(keyid, strLabel, "receive");
|
pwallet->SetAddressBook(keyid, label, "receive");
|
||||||
nTimeBegin = std::min(nTimeBegin, nTime);
|
nTimeBegin = std::min(nTimeBegin, time);
|
||||||
} else if(IsHex(vstr[0])) {
|
progress++;
|
||||||
std::vector<unsigned char> vData(ParseHex(vstr[0]));
|
}
|
||||||
CScript script = CScript(vData.begin(), vData.end());
|
for (const auto& script_pair : scripts) {
|
||||||
|
uiInterface.ShowProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
|
||||||
|
const CScript& script = script_pair.first;
|
||||||
|
int64_t time = script_pair.second;
|
||||||
CScriptID id(script);
|
CScriptID id(script);
|
||||||
if (pwallet->HaveCScript(id)) {
|
if (pwallet->HaveCScript(id)) {
|
||||||
pwallet->WalletLogPrintf("Skipping import of %s (script already present)\n", vstr[0]);
|
pwallet->WalletLogPrintf("Skipping import of %s (script already present)\n", HexStr(script));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(!pwallet->AddCScript(script)) {
|
if(!pwallet->AddCScript(script)) {
|
||||||
pwallet->WalletLogPrintf("Error importing script %s\n", vstr[0]);
|
pwallet->WalletLogPrintf("Error importing script %s\n", HexStr(script));
|
||||||
fGood = false;
|
fGood = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
int64_t birth_time = DecodeDumpTime(vstr[1]);
|
if (time > 0) {
|
||||||
if (birth_time > 0) {
|
pwallet->m_script_metadata[id].nCreateTime = time;
|
||||||
pwallet->m_script_metadata[id].nCreateTime = birth_time;
|
nTimeBegin = std::min(nTimeBegin, time);
|
||||||
nTimeBegin = std::min(nTimeBegin, birth_time);
|
|
||||||
}
|
}
|
||||||
|
progress++;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
file.close();
|
|
||||||
uiInterface.ShowProgress("", 100, false); // hide progress dialog in GUI
|
uiInterface.ShowProgress("", 100, false); // hide progress dialog in GUI
|
||||||
pwallet->UpdateTimeFirstKey(nTimeBegin);
|
pwallet->UpdateTimeFirstKey(nTimeBegin);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user