|
|
@ -108,7 +108,7 @@ void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountin |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int |
|
|
|
DBErrors |
|
|
|
CWalletDB::ReorderTransactions(CWallet* pwallet) |
|
|
|
CWalletDB::ReorderTransactions(CWallet* pwallet) |
|
|
|
{ |
|
|
|
{ |
|
|
|
LOCK(pwallet->cs_wallet); |
|
|
|
LOCK(pwallet->cs_wallet); |
|
|
@ -181,51 +181,15 @@ CWalletDB::ReorderTransactions(CWallet* pwallet) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int CWalletDB::LoadWallet(CWallet* pwallet) |
|
|
|
bool |
|
|
|
|
|
|
|
ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, |
|
|
|
|
|
|
|
int& nFileVersion, vector<uint256>& vWalletUpgrade, |
|
|
|
|
|
|
|
bool& fIsEncrypted, bool& fAnyUnordered, string& strType, string& strErr) |
|
|
|
{ |
|
|
|
{ |
|
|
|
pwallet->vchDefaultKey = CPubKey(); |
|
|
|
try { |
|
|
|
int nFileVersion = 0; |
|
|
|
|
|
|
|
vector<uint256> vWalletUpgrade; |
|
|
|
|
|
|
|
bool fIsEncrypted = false; |
|
|
|
|
|
|
|
bool fAnyUnordered = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//// todo: shouldn't we catch exceptions and try to recover and continue?
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LOCK(pwallet->cs_wallet); |
|
|
|
|
|
|
|
int nMinVersion = 0; |
|
|
|
|
|
|
|
if (Read((string)"minversion", nMinVersion)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (nMinVersion > CLIENT_VERSION) |
|
|
|
|
|
|
|
return DB_TOO_NEW; |
|
|
|
|
|
|
|
pwallet->LoadMinVersion(nMinVersion); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get cursor
|
|
|
|
|
|
|
|
Dbc* pcursor = GetCursor(); |
|
|
|
|
|
|
|
if (!pcursor) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
printf("Error getting wallet database cursor\n"); |
|
|
|
|
|
|
|
return DB_CORRUPT; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
loop |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Read next record
|
|
|
|
|
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
|
|
|
|
|
|
|
CDataStream ssValue(SER_DISK, CLIENT_VERSION); |
|
|
|
|
|
|
|
int ret = ReadAtCursor(pcursor, ssKey, ssValue); |
|
|
|
|
|
|
|
if (ret == DB_NOTFOUND) |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
else if (ret != 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
printf("Error reading next record from wallet database\n"); |
|
|
|
|
|
|
|
return DB_CORRUPT; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Unserialize
|
|
|
|
// Unserialize
|
|
|
|
// Taking advantage of the fact that pair serialization
|
|
|
|
// Taking advantage of the fact that pair serialization
|
|
|
|
// is just the two items serialized one after the other
|
|
|
|
// is just the two items serialized one after the other
|
|
|
|
string strType; |
|
|
|
|
|
|
|
ssKey >> strType; |
|
|
|
ssKey >> strType; |
|
|
|
if (strType == "name") |
|
|
|
if (strType == "name") |
|
|
|
{ |
|
|
|
{ |
|
|
@ -239,10 +203,13 @@ int CWalletDB::LoadWallet(CWallet* pwallet) |
|
|
|
ssKey >> hash; |
|
|
|
ssKey >> hash; |
|
|
|
CWalletTx& wtx = pwallet->mapWallet[hash]; |
|
|
|
CWalletTx& wtx = pwallet->mapWallet[hash]; |
|
|
|
ssValue >> wtx; |
|
|
|
ssValue >> wtx; |
|
|
|
|
|
|
|
if (wtx.CheckTransaction() && (wtx.GetHash() == hash)) |
|
|
|
wtx.BindWallet(pwallet); |
|
|
|
wtx.BindWallet(pwallet); |
|
|
|
|
|
|
|
else |
|
|
|
if (wtx.GetHash() != hash) |
|
|
|
{ |
|
|
|
printf("Error in wallet.dat, hash mismatch\n"); |
|
|
|
pwallet->mapWallet.erase(hash); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Undo serialize changes in 31600
|
|
|
|
// Undo serialize changes in 31600
|
|
|
|
if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703) |
|
|
|
if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703) |
|
|
@ -252,12 +219,13 @@ int CWalletDB::LoadWallet(CWallet* pwallet) |
|
|
|
char fTmp; |
|
|
|
char fTmp; |
|
|
|
char fUnused; |
|
|
|
char fUnused; |
|
|
|
ssValue >> fTmp >> fUnused >> wtx.strFromAccount; |
|
|
|
ssValue >> fTmp >> fUnused >> wtx.strFromAccount; |
|
|
|
printf("LoadWallet() upgrading tx ver=%d %d '%s' %s\n", wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str()); |
|
|
|
strErr = strprintf("LoadWallet() upgrading tx ver=%d %d '%s' %s", |
|
|
|
|
|
|
|
wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str()); |
|
|
|
wtx.fTimeReceivedIsTxTime = fTmp; |
|
|
|
wtx.fTimeReceivedIsTxTime = fTmp; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
printf("LoadWallet() repairing tx ver=%d %s\n", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str()); |
|
|
|
strErr = strprintf("LoadWallet() repairing tx ver=%d %s", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str()); |
|
|
|
wtx.fTimeReceivedIsTxTime = 0; |
|
|
|
wtx.fTimeReceivedIsTxTime = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
vWalletUpgrade.push_back(hash); |
|
|
|
vWalletUpgrade.push_back(hash); |
|
|
@ -301,16 +269,20 @@ int CWalletDB::LoadWallet(CWallet* pwallet) |
|
|
|
CPrivKey pkey; |
|
|
|
CPrivKey pkey; |
|
|
|
ssValue >> pkey; |
|
|
|
ssValue >> pkey; |
|
|
|
key.SetPubKey(vchPubKey); |
|
|
|
key.SetPubKey(vchPubKey); |
|
|
|
key.SetPrivKey(pkey); |
|
|
|
if (!key.SetPrivKey(pkey)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
strErr = "Error reading wallet database: CPrivKey corrupt"; |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
if (key.GetPubKey() != vchPubKey) |
|
|
|
if (key.GetPubKey() != vchPubKey) |
|
|
|
{ |
|
|
|
{ |
|
|
|
printf("Error reading wallet database: CPrivKey pubkey inconsistency\n"); |
|
|
|
strErr = "Error reading wallet database: CPrivKey pubkey inconsistency"; |
|
|
|
return DB_CORRUPT; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
if (!key.IsValid()) |
|
|
|
if (!key.IsValid()) |
|
|
|
{ |
|
|
|
{ |
|
|
|
printf("Error reading wallet database: invalid CPrivKey\n"); |
|
|
|
strErr = "Error reading wallet database: invalid CPrivKey"; |
|
|
|
return DB_CORRUPT; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
@ -318,22 +290,26 @@ int CWalletDB::LoadWallet(CWallet* pwallet) |
|
|
|
CWalletKey wkey; |
|
|
|
CWalletKey wkey; |
|
|
|
ssValue >> wkey; |
|
|
|
ssValue >> wkey; |
|
|
|
key.SetPubKey(vchPubKey); |
|
|
|
key.SetPubKey(vchPubKey); |
|
|
|
key.SetPrivKey(wkey.vchPrivKey); |
|
|
|
if (!key.SetPrivKey(wkey.vchPrivKey)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
strErr = "Error reading wallet database: CPrivKey corrupt"; |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
if (key.GetPubKey() != vchPubKey) |
|
|
|
if (key.GetPubKey() != vchPubKey) |
|
|
|
{ |
|
|
|
{ |
|
|
|
printf("Error reading wallet database: CWalletKey pubkey inconsistency\n"); |
|
|
|
strErr = "Error reading wallet database: CWalletKey pubkey inconsistency"; |
|
|
|
return DB_CORRUPT; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
if (!key.IsValid()) |
|
|
|
if (!key.IsValid()) |
|
|
|
{ |
|
|
|
{ |
|
|
|
printf("Error reading wallet database: invalid CWalletKey\n"); |
|
|
|
strErr = "Error reading wallet database: invalid CWalletKey"; |
|
|
|
return DB_CORRUPT; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (!pwallet->LoadKey(key)) |
|
|
|
if (!pwallet->LoadKey(key)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
printf("Error reading wallet database: LoadKey failed\n"); |
|
|
|
strErr = "Error reading wallet database: LoadKey failed"; |
|
|
|
return DB_CORRUPT; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else if (strType == "mkey") |
|
|
|
else if (strType == "mkey") |
|
|
@ -344,8 +320,8 @@ int CWalletDB::LoadWallet(CWallet* pwallet) |
|
|
|
ssValue >> kMasterKey; |
|
|
|
ssValue >> kMasterKey; |
|
|
|
if(pwallet->mapMasterKeys.count(nID) != 0) |
|
|
|
if(pwallet->mapMasterKeys.count(nID) != 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
printf("Error reading wallet database: duplicate CMasterKey id %u\n", nID); |
|
|
|
strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID); |
|
|
|
return DB_CORRUPT; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
pwallet->mapMasterKeys[nID] = kMasterKey; |
|
|
|
pwallet->mapMasterKeys[nID] = kMasterKey; |
|
|
|
if (pwallet->nMasterKeyMaxID < nID) |
|
|
|
if (pwallet->nMasterKeyMaxID < nID) |
|
|
@ -359,8 +335,8 @@ int CWalletDB::LoadWallet(CWallet* pwallet) |
|
|
|
ssValue >> vchPrivKey; |
|
|
|
ssValue >> vchPrivKey; |
|
|
|
if (!pwallet->LoadCryptedKey(vchPubKey, vchPrivKey)) |
|
|
|
if (!pwallet->LoadCryptedKey(vchPubKey, vchPrivKey)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
printf("Error reading wallet database: LoadCryptedKey failed\n"); |
|
|
|
strErr = "Error reading wallet database: LoadCryptedKey failed"; |
|
|
|
return DB_CORRUPT; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
fIsEncrypted = true; |
|
|
|
fIsEncrypted = true; |
|
|
|
} |
|
|
|
} |
|
|
@ -388,23 +364,109 @@ int CWalletDB::LoadWallet(CWallet* pwallet) |
|
|
|
ssValue >> script; |
|
|
|
ssValue >> script; |
|
|
|
if (!pwallet->LoadCScript(script)) |
|
|
|
if (!pwallet->LoadCScript(script)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
printf("Error reading wallet database: LoadCScript failed\n"); |
|
|
|
strErr = "Error reading wallet database: LoadCScript failed"; |
|
|
|
return DB_CORRUPT; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else if (strType == "orderposnext") |
|
|
|
else if (strType == "orderposnext") |
|
|
|
{ |
|
|
|
{ |
|
|
|
ssValue >> pwallet->nOrderPosNext; |
|
|
|
ssValue >> pwallet->nOrderPosNext; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} catch (...) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool IsKeyType(string strType) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return (strType== "key" || strType == "wkey" || |
|
|
|
|
|
|
|
strType == "mkey" || strType == "ckey"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DBErrors CWalletDB::LoadWallet(CWallet* pwallet) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
pwallet->vchDefaultKey = CPubKey(); |
|
|
|
|
|
|
|
int nFileVersion = 0; |
|
|
|
|
|
|
|
vector<uint256> vWalletUpgrade; |
|
|
|
|
|
|
|
bool fIsEncrypted = false; |
|
|
|
|
|
|
|
bool fAnyUnordered = false; |
|
|
|
|
|
|
|
bool fNoncriticalErrors = false; |
|
|
|
|
|
|
|
DBErrors result = DB_LOAD_OK; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
LOCK(pwallet->cs_wallet); |
|
|
|
|
|
|
|
int nMinVersion = 0; |
|
|
|
|
|
|
|
if (Read((string)"minversion", nMinVersion)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (nMinVersion > CLIENT_VERSION) |
|
|
|
|
|
|
|
return DB_TOO_NEW; |
|
|
|
|
|
|
|
pwallet->LoadMinVersion(nMinVersion); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get cursor
|
|
|
|
|
|
|
|
Dbc* pcursor = GetCursor(); |
|
|
|
|
|
|
|
if (!pcursor) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
printf("Error getting wallet database cursor\n"); |
|
|
|
|
|
|
|
return DB_CORRUPT; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
loop |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Read next record
|
|
|
|
|
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
|
|
|
|
|
|
|
CDataStream ssValue(SER_DISK, CLIENT_VERSION); |
|
|
|
|
|
|
|
int ret = ReadAtCursor(pcursor, ssKey, ssValue); |
|
|
|
|
|
|
|
if (ret == DB_NOTFOUND) |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
else if (ret != 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
printf("Error reading next record from wallet database\n"); |
|
|
|
|
|
|
|
return DB_CORRUPT; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Try to be tolerant of single corrupt records:
|
|
|
|
|
|
|
|
string strType, strErr; |
|
|
|
|
|
|
|
if (!ReadKeyValue(pwallet, ssKey, ssValue, nFileVersion, |
|
|
|
|
|
|
|
vWalletUpgrade, fIsEncrypted, fAnyUnordered, strType, strErr)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// losing keys is considered a catastrophic error, anything else
|
|
|
|
|
|
|
|
// we assume the user can live with:
|
|
|
|
|
|
|
|
if (IsKeyType(strType)) |
|
|
|
|
|
|
|
result = DB_CORRUPT; |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Leave other errors alone, if we try to fix them we might make things worse.
|
|
|
|
|
|
|
|
fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
|
|
|
|
|
|
|
|
if (strType == "tx") |
|
|
|
|
|
|
|
// Rescan if there is a bad transaction record:
|
|
|
|
|
|
|
|
SoftSetBoolArg("-rescan", true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!strErr.empty()) |
|
|
|
|
|
|
|
printf("%s\n", strErr.c_str()); |
|
|
|
} |
|
|
|
} |
|
|
|
pcursor->close(); |
|
|
|
pcursor->close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
catch (...) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
result = DB_CORRUPT; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
BOOST_FOREACH(uint256 hash, vWalletUpgrade) |
|
|
|
if (fNoncriticalErrors && result == DB_LOAD_OK) |
|
|
|
WriteTx(hash, pwallet->mapWallet[hash]); |
|
|
|
result = DB_NONCRITICAL_ERROR; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Any wallet corruption at all: skip any rewriting or
|
|
|
|
|
|
|
|
// upgrading, we don't want to make it worse.
|
|
|
|
|
|
|
|
if (result != DB_LOAD_OK) |
|
|
|
|
|
|
|
return result; |
|
|
|
|
|
|
|
|
|
|
|
printf("nFileVersion = %d\n", nFileVersion); |
|
|
|
printf("nFileVersion = %d\n", nFileVersion); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_FOREACH(uint256 hash, vWalletUpgrade) |
|
|
|
|
|
|
|
WriteTx(hash, pwallet->mapWallet[hash]); |
|
|
|
|
|
|
|
|
|
|
|
// Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
|
|
|
|
// Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
|
|
|
|
if (fIsEncrypted && (nFileVersion == 40000 || nFileVersion == 50000)) |
|
|
|
if (fIsEncrypted && (nFileVersion == 40000 || nFileVersion == 50000)) |
|
|
@ -414,10 +476,9 @@ int CWalletDB::LoadWallet(CWallet* pwallet) |
|
|
|
WriteVersion(CLIENT_VERSION); |
|
|
|
WriteVersion(CLIENT_VERSION); |
|
|
|
|
|
|
|
|
|
|
|
if (fAnyUnordered) |
|
|
|
if (fAnyUnordered) |
|
|
|
return ReorderTransactions(pwallet); |
|
|
|
result = ReorderTransactions(pwallet); |
|
|
|
|
|
|
|
|
|
|
|
// If you add anything else here... be sure to do it if ReorderTransactions returns DB_LOAD_OK too!
|
|
|
|
return result; |
|
|
|
return DB_LOAD_OK; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ThreadFlushWalletDB(void* parg) |
|
|
|
void ThreadFlushWalletDB(void* parg) |
|
|
@ -521,3 +582,94 @@ bool BackupWallet(const CWallet& wallet, const string& strDest) |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// Try to (very carefully!) recover wallet.dat if there is a problem.
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Recovery procedure:
|
|
|
|
|
|
|
|
// move wallet.dat to wallet.timestamp.bak
|
|
|
|
|
|
|
|
// Call Salvage with fAggressive=true to
|
|
|
|
|
|
|
|
// get as much data as possible.
|
|
|
|
|
|
|
|
// Rewrite salvaged data to wallet.dat
|
|
|
|
|
|
|
|
// Set -rescan so any missing transactions will be
|
|
|
|
|
|
|
|
// found.
|
|
|
|
|
|
|
|
int64 now = GetTime(); |
|
|
|
|
|
|
|
std::string newFilename = strprintf("wallet.%"PRI64d".bak", now); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int result = dbenv.dbenv.dbrename(NULL, filename.c_str(), NULL, |
|
|
|
|
|
|
|
newFilename.c_str(), DB_AUTO_COMMIT); |
|
|
|
|
|
|
|
if (result == 0) |
|
|
|
|
|
|
|
printf("Renamed %s to %s\n", filename.c_str(), newFilename.c_str()); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
printf("Failed to rename %s to %s\n", filename.c_str(), newFilename.c_str()); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<CDBEnv::KeyValPair> salvagedData; |
|
|
|
|
|
|
|
bool allOK = dbenv.Salvage(newFilename, true, salvagedData); |
|
|
|
|
|
|
|
if (salvagedData.empty()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
printf("Salvage(aggressive) found no records in %s.\n", newFilename.c_str()); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
printf("Salvage(aggressive) found %"PRIszu" records\n", salvagedData.size()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool fSuccess = allOK; |
|
|
|
|
|
|
|
Db* pdbCopy = new Db(&dbenv.dbenv, 0); |
|
|
|
|
|
|
|
int ret = pdbCopy->open(NULL, // Txn pointer
|
|
|
|
|
|
|
|
filename.c_str(), // Filename
|
|
|
|
|
|
|
|
"main", // Logical db name
|
|
|
|
|
|
|
|
DB_BTREE, // Database type
|
|
|
|
|
|
|
|
DB_CREATE, // Flags
|
|
|
|
|
|
|
|
0); |
|
|
|
|
|
|
|
if (ret > 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
printf("Cannot create database file %s\n", filename.c_str()); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
CWallet dummyWallet; |
|
|
|
|
|
|
|
int nFileVersion = 0; |
|
|
|
|
|
|
|
vector<uint256> vWalletUpgrade; |
|
|
|
|
|
|
|
bool fIsEncrypted = false; |
|
|
|
|
|
|
|
bool fAnyUnordered = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DbTxn* ptxn = dbenv.TxnBegin(); |
|
|
|
|
|
|
|
BOOST_FOREACH(CDBEnv::KeyValPair& row, salvagedData) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (fOnlyKeys) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION); |
|
|
|
|
|
|
|
CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION); |
|
|
|
|
|
|
|
string strType, strErr; |
|
|
|
|
|
|
|
bool fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue, |
|
|
|
|
|
|
|
nFileVersion, vWalletUpgrade, |
|
|
|
|
|
|
|
fIsEncrypted, fAnyUnordered, |
|
|
|
|
|
|
|
strType, strErr); |
|
|
|
|
|
|
|
if (!IsKeyType(strType)) |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
if (!fReadOK) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
printf("WARNING: CWalletDB::Recover skipping %s: %s\n", strType.c_str(), strErr.c_str()); |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Dbt datKey(&row.first[0], row.first.size()); |
|
|
|
|
|
|
|
Dbt datValue(&row.second[0], row.second.size()); |
|
|
|
|
|
|
|
int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE); |
|
|
|
|
|
|
|
if (ret2 > 0) |
|
|
|
|
|
|
|
fSuccess = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ptxn->commit(0); |
|
|
|
|
|
|
|
pdbCopy->close(0); |
|
|
|
|
|
|
|
delete pdbCopy; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return fSuccess; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return CWalletDB::Recover(dbenv, filename, false); |
|
|
|
|
|
|
|
} |
|
|
|