21typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> >
KeyValPair;
42 std::unique_ptr<DatabaseCursor>
GetNewCursor()
override {
return std::make_unique<DummyCursor>(); }
58 bool Rewrite(
const char* pszSkip=
nullptr)
override {
return true; }
59 bool Backup(
const std::string& strDest)
const override {
return true; }
65 std::string
Filename()
override {
return "dummy"; }
66 std::string
Format()
override {
return "dummy"; }
67 std::unique_ptr<DatabaseBatch>
MakeBatch(
bool flush_on_close =
true)
override {
return std::make_unique<DummyBatch>(); }
78 std::unique_ptr<WalletDatabase> database =
MakeDatabase(file_path, options, status, error);
79 if (!database)
return false;
82 std::string filename = berkeley_database.
Filename();
83 std::shared_ptr<BerkeleyEnvironment> env = berkeley_database.
env;
85 if (!env->Open(error)) {
97 std::string newFilename =
strprintf(
"%s.%d.bak", filename, now);
99 int result = env->dbenv->dbrename(
nullptr, filename.c_str(),
nullptr,
100 newFilename.c_str(), DB_AUTO_COMMIT);
113 std::vector<KeyValPair> salvagedData;
115 std::stringstream strDump;
117 Db db(env->dbenv.get(), 0);
118 result = db.verify(newFilename.c_str(),
nullptr, &strDump, DB_SALVAGE | DB_AGGRESSIVE);
119 if (result == DB_VERIFY_BAD) {
120 warnings.emplace_back(
Untranslated(
"Salvage: Database salvage found errors, all data may not be recoverable."));
122 if (result != 0 && result != DB_VERIFY_BAD) {
136 while (!strDump.eof() && strLine !=
HEADER_END)
137 getline(strDump, strLine);
139 std::string keyHex, valueHex;
140 while (!strDump.eof() && keyHex !=
DATA_END) {
141 getline(strDump, keyHex);
145 getline(strDump, valueHex);
147 warnings.emplace_back(
Untranslated(
"Salvage: WARNING: Number of keys in data does not match number of values."));
156 warnings.emplace_back(
Untranslated(
"Salvage: WARNING: Unexpected end of file while reading salvage output."));
159 fSuccess = (result == 0);
162 if (salvagedData.empty())
168 std::unique_ptr<Db> pdbCopy = std::make_unique<Db>(env->dbenv.get(), 0);
169 int ret = pdbCopy->open(
nullptr,
181 DbTxn* ptxn = env->TxnBegin(DB_TXN_WRITE_NOSYNC);
182 CWallet dummyWallet(
nullptr,
"", std::make_unique<DummyDatabase>());
188 std::string strType, strErr;
192 bool fReadOK =
false;
194 fReadOK =
LoadKey(&dummyWallet, ssKey, ssValue, strErr);
200 fReadOK =
LoadHDChain(&dummyWallet, ssValue, strErr);
207 warnings.push_back(
Untranslated(
strprintf(
"WARNING: WalletBatch::Recover skipping %s: %s", strType, strErr)));
210 Dbt datKey(row.first.data(), row.first.size());
211 Dbt datValue(row.second.data(), row.second.size());
212 int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE);
Double ended buffer combining vector and stream-like interfaces.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
An instance of this class represents one database.
std::shared_ptr< BerkeleyEnvironment > env
Pointer to shared database environment.
std::string Filename() override
Return path to main database filename.
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
RAII class that provides access to a WalletDatabase.
RAII class that provides access to a DummyDatabase.
bool EraseKey(DataStream &&key) override
bool HasActiveTxn() override
bool HasKey(DataStream &&key) override
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(Span< const std::byte > prefix) override
bool ReadKey(DataStream &&key, DataStream &value) override
bool ErasePrefix(Span< const std::byte > prefix) override
std::unique_ptr< DatabaseCursor > GetNewCursor() override
bool TxnCommit() override
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Status Next(DataStream &key, DataStream &value) override
A dummy WalletDatabase that does nothing and never fails.
bool Rewrite(const char *pszSkip=nullptr) override
Rewrite the entire database on disk, with the exception of key pszSkip if non-zero.
void AddRef() override
Indicate the a new database user has began using the database.
void ReloadDbEnv() override
bool PeriodicFlush() override
void IncrementUpdateCounter() override
void Flush() override
Make sure all changes are flushed to database file.
std::string Filename() override
Return path to main database file for logs and error messages.
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a DatabaseBatch connected to this database.
void Open() override
Open the database if it is not already opened.
std::string Format() override
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
void Close() override
Flush to the database file and close the database.
An instance of this class represents one database.
std::atomic< unsigned int > nUpdateCounter
const std::string CRYPTED_KEY
const std::string HDCHAIN
const std::string MASTER_KEY
void ReadDatabaseArgs(const ArgsManager &args, DatabaseOptions &options)
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
bool LoadKey(CWallet *pwallet, DataStream &ssKey, DataStream &ssValue, std::string &strErr)
bool LoadCryptedKey(CWallet *pwallet, DataStream &ssKey, DataStream &ssValue, std::string &strErr)
std::pair< std::vector< unsigned char >, std::vector< unsigned char > > KeyValPair
bool LoadEncryptionKey(CWallet *pwallet, DataStream &ssKey, DataStream &ssValue, std::string &strErr)
bool LoadHDChain(CWallet *pwallet, DataStream &ssValue, std::string &strErr)
static const char * HEADER_END
static const char * DATA_END
bool RecoverDatabaseFile(const ArgsManager &args, const fs::path &file_path, bilingual_str &error, std::vector< bilingual_str > &warnings)
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
bool verify
Check data integrity on load.
std::optional< DatabaseFormat > require_format
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.