diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp index cf4cbe930..105bd9da0 100644 --- a/src/wallet/bdb.cpp +++ b/src/wallet/bdb.cpp @@ -1,933 +1,932 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2020 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #ifndef WIN32 #include #endif namespace { //! Make sure database has a unique fileid within the environment. If it //! doesn't, throw an error. BDB caches do not work properly when more than one //! open database has the same fileid (values written to one database may show //! up in reads to other databases). //! //! BerkeleyDB generates unique fileids by default //! (https://docs.oracle.com/cd/E17275_01/html/programmer_reference/program_copy.html), //! so bitcoin should never create different databases with the same fileid, but //! this error can be triggered if users manually copy database files. void CheckUniqueFileid(const BerkeleyEnvironment &env, const std::string &filename, Db &db, WalletDatabaseFileId &fileid) { if (env.IsMock()) { return; } int ret = db.get_mpf()->get_fileid(fileid.value); if (ret != 0) { throw std::runtime_error( strprintf("BerkeleyDatabase: Can't open database %s (get_fileid " "failed with %d)", filename, ret)); } for (const auto &item : env.m_fileids) { if (fileid == item.second && &fileid != &item.second) { throw std::runtime_error( strprintf("BerkeleyDatabase: Can't open database %s " "(duplicates fileid %s " "from %s)", filename, HexStr(item.second.value), item.first)); } } } RecursiveMutex cs_db; //! Map from directory name to db environment. std::map> g_dbenvs GUARDED_BY(cs_db); } // namespace bool WalletDatabaseFileId::operator==(const WalletDatabaseFileId &rhs) const { return memcmp(value, &rhs.value, sizeof(value)) == 0; } /** * @param[in] wallet_path Path to wallet directory. Or (for backwards * compatibility only) a path to a berkeley btree data file inside a wallet * directory. * @param[out] database_filename Filename of berkeley btree data file inside the * wallet directory. * @return A shared pointer to the BerkeleyEnvironment object for the wallet * directory, never empty because ~BerkeleyEnvironment erases the weak pointer * from the g_dbenvs map. * @post A new BerkeleyEnvironment weak pointer is inserted into g_dbenvs if the * directory path key was not already in the map. */ std::shared_ptr GetWalletEnv(const fs::path &wallet_path, std::string &database_filename) { fs::path env_directory; SplitWalletPath(wallet_path, env_directory, database_filename); LOCK(cs_db); auto inserted = g_dbenvs.emplace(env_directory.string(), std::weak_ptr()); if (inserted.second) { auto env = std::make_shared(env_directory.string()); inserted.first->second = env; return env; } return inserted.first->second.lock(); } // // BerkeleyBatch // void BerkeleyEnvironment::Close() { if (!fDbEnvInit) { return; } fDbEnvInit = false; for (auto &db : m_databases) { BerkeleyDatabase &database = db.second.get(); assert(database.m_refcount <= 0); if (database.m_db) { database.m_db->close(0); database.m_db.reset(); } } FILE *error_file = nullptr; dbenv->get_errfile(&error_file); int ret = dbenv->close(0); if (ret != 0) { LogPrintf("BerkeleyEnvironment::Close: Error %d closing database " "environment: %s\n", ret, DbEnv::strerror(ret)); } if (!fMockDb) { DbEnv(u_int32_t(0)).remove(strPath.c_str(), 0); } if (error_file) { fclose(error_file); } UnlockDirectory(strPath, ".walletlock"); } void BerkeleyEnvironment::Reset() { dbenv.reset(new DbEnv(DB_CXX_NO_EXCEPTIONS)); fDbEnvInit = false; fMockDb = false; } BerkeleyEnvironment::BerkeleyEnvironment(const fs::path &dir_path) : strPath(dir_path.string()) { Reset(); } BerkeleyEnvironment::~BerkeleyEnvironment() { LOCK(cs_db); g_dbenvs.erase(strPath); Close(); } bool BerkeleyEnvironment::Open(bilingual_str &err) { if (fDbEnvInit) { return true; } fs::path pathIn = strPath; TryCreateDirectories(pathIn); if (!LockDirectory(pathIn, ".walletlock")) { LogPrintf("Cannot obtain a lock on wallet directory %s. Another " "instance of bitcoin may be using it.\n", strPath); err = strprintf(_("Error initializing wallet database environment %s!"), Directory()); return false; } fs::path pathLogDir = pathIn / "database"; TryCreateDirectories(pathLogDir); fs::path pathErrorFile = pathIn / "db.log"; LogPrintf("BerkeleyEnvironment::Open: LogDir=%s ErrorFile=%s\n", pathLogDir.string(), pathErrorFile.string()); unsigned int nEnvFlags = 0; if (gArgs.GetBoolArg("-privdb", DEFAULT_WALLET_PRIVDB)) { nEnvFlags |= DB_PRIVATE; } dbenv->set_lg_dir(pathLogDir.string().c_str()); // 1 MiB should be enough for just the wallet dbenv->set_cachesize(0, 0x100000, 1); dbenv->set_lg_bsize(0x10000); dbenv->set_lg_max(1048576); dbenv->set_lk_max_locks(40000); dbenv->set_lk_max_objects(40000); /// debug dbenv->set_errfile(fsbridge::fopen(pathErrorFile, "a")); dbenv->set_flags(DB_AUTO_COMMIT, 1); dbenv->set_flags(DB_TXN_WRITE_NOSYNC, 1); dbenv->log_set_config(DB_LOG_AUTO_REMOVE, 1); int ret = dbenv->open(strPath.c_str(), DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_THREAD | DB_RECOVER | nEnvFlags, S_IRUSR | S_IWUSR); if (ret != 0) { LogPrintf("BerkeleyEnvironment::Open: Error %d opening database " "environment: %s\n", ret, DbEnv::strerror(ret)); int ret2 = dbenv->close(0); if (ret2 != 0) { LogPrintf("BerkeleyEnvironment::Open: Error %d closing failed " "database environment: %s\n", ret2, DbEnv::strerror(ret2)); } Reset(); err = strprintf(_("Error initializing wallet database environment %s!"), Directory()); if (ret == DB_RUNRECOVERY) { err += Untranslated(" ") + _("This error could occur if this wallet was not shutdown " "cleanly and was last loaded using a build with a newer " "version of Berkeley DB. If so, please use the software " "that last loaded this wallet"); } return false; } fDbEnvInit = true; fMockDb = false; return true; } //! Construct an in-memory mock Berkeley environment for testing BerkeleyEnvironment::BerkeleyEnvironment() { Reset(); LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::MakeMock\n"); dbenv->set_cachesize(1, 0, 1); dbenv->set_lg_bsize(10485760 * 4); dbenv->set_lg_max(10485760); dbenv->set_lk_max_locks(10000); dbenv->set_lk_max_objects(10000); dbenv->set_flags(DB_AUTO_COMMIT, 1); dbenv->log_set_config(DB_LOG_IN_MEMORY, 1); int ret = dbenv->open(nullptr, DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_THREAD | DB_PRIVATE, S_IRUSR | S_IWUSR); if (ret > 0) { throw std::runtime_error( strprintf("BerkeleyEnvironment::MakeMock: Error %d opening " "database environment.", ret)); } fDbEnvInit = true; fMockDb = true; } BerkeleyBatch::SafeDbt::SafeDbt() { m_dbt.set_flags(DB_DBT_MALLOC); } BerkeleyBatch::SafeDbt::SafeDbt(void *data, size_t size) : m_dbt(data, size) {} BerkeleyBatch::SafeDbt::~SafeDbt() { if (m_dbt.get_data() != nullptr) { // Clear memory, e.g. in case it was a private key memory_cleanse(m_dbt.get_data(), m_dbt.get_size()); // under DB_DBT_MALLOC, data is malloced by the Dbt, but must be // freed by the caller. // https://docs.oracle.com/cd/E17275_01/html/api_reference/C/dbt.html if (m_dbt.get_flags() & DB_DBT_MALLOC) { free(m_dbt.get_data()); } } } const void *BerkeleyBatch::SafeDbt::get_data() const { return m_dbt.get_data(); } u_int32_t BerkeleyBatch::SafeDbt::get_size() const { return m_dbt.get_size(); } BerkeleyBatch::SafeDbt::operator Dbt *() { return &m_dbt; } bool BerkeleyDatabase::Verify(bilingual_str &errorStr) { fs::path walletDir = env->Directory(); fs::path file_path = walletDir / strFile; LogPrintf("Using BerkeleyDB version %s\n", BerkeleyDatabaseVersion()); LogPrintf("Using wallet %s\n", file_path.string()); if (!env->Open(errorStr)) { return false; } if (fs::exists(file_path)) { assert(m_refcount == 0); Db db(env->dbenv.get(), 0); int result = db.verify(strFile.c_str(), nullptr, nullptr, 0); if (result != 0) { errorStr = strprintf(_("%s corrupt. Try using the wallet tool " "bitcoin-wallet to salvage or restoring a backup."), file_path); return false; } } // also return true if files does not exists return true; } void BerkeleyEnvironment::CheckpointLSN(const std::string &strFile) { dbenv->txn_checkpoint(0, 0, 0); if (fMockDb) { return; } dbenv->lsn_reset(strFile.c_str(), 0); } BerkeleyDatabase::~BerkeleyDatabase() { if (env) { LOCK(cs_db); env->CloseDb(strFile); assert(!m_db); size_t erased = env->m_databases.erase(strFile); assert(erased == 1); env->m_fileids.erase(strFile); } } BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase &database, const char *pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr), m_database(database) { database.AddRef(); database.Open(pszMode); fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w')); fFlushOnClose = fFlushOnCloseIn; env = database.env.get(); pdb = database.m_db.get(); strFile = database.strFile; bool fCreate = strchr(pszMode, 'c') != nullptr; if (fCreate && !Exists(std::string("version"))) { bool fTmp = fReadOnly; fReadOnly = false; Write(std::string("version"), CLIENT_VERSION); fReadOnly = fTmp; } } void BerkeleyDatabase::Open(const char *pszMode) { bool fCreate = strchr(pszMode, 'c') != nullptr; unsigned int nFlags = DB_THREAD; if (fCreate) { nFlags |= DB_CREATE; } { LOCK(cs_db); bilingual_str open_err; if (!env->Open(open_err)) { throw std::runtime_error( "BerkeleyDatabase: Failed to open database environment."); } if (m_db == nullptr) { int ret; std::unique_ptr pdb_temp = std::make_unique(env->dbenv.get(), 0); bool fMockDb = env->IsMock(); if (fMockDb) { DbMpoolFile *mpf = pdb_temp->get_mpf(); ret = mpf->set_flags(DB_MPOOL_NOFILE, 1); if (ret != 0) { throw std::runtime_error(strprintf( "BerkeleyDatabase: Failed to configure for no " "temp file backing for database %s", strFile)); } } ret = pdb_temp->open( nullptr, // Txn pointer fMockDb ? nullptr : strFile.c_str(), // Filename fMockDb ? strFile.c_str() : "main", // Logical db name DB_BTREE, // Database type nFlags, // Flags 0); if (ret != 0) { throw std::runtime_error(strprintf( "BerkeleyDatabase: Error %d, can't open database %s", ret, strFile)); } - m_file_path = (env->Directory() / strFile).string(); // Call CheckUniqueFileid on the containing BDB environment to // avoid BDB data consistency bugs that happen when different data // files in the same environment have the same fileid. CheckUniqueFileid(*env, strFile, *pdb_temp, this->env->m_fileids[strFile]); m_db.reset(pdb_temp.release()); } } } void BerkeleyBatch::Flush() { if (activeTxn) { return; } // Flush database activity from memory pool to disk log unsigned int nMinutes = 0; if (fReadOnly) { nMinutes = 1; } // env is nullptr for dummy databases (i.e. in tests). Don't actually flush // if env is nullptr so we don't segfault if (env) { env->dbenv->txn_checkpoint( nMinutes ? gArgs.GetArg("-dblogsize", DEFAULT_WALLET_DBLOGSIZE) * 1024 : 0, nMinutes, 0); } } void BerkeleyDatabase::IncrementUpdateCounter() { ++nUpdateCounter; } BerkeleyBatch::~BerkeleyBatch() { Close(); m_database.RemoveRef(); } void BerkeleyBatch::Close() { if (!pdb) { return; } if (activeTxn) { activeTxn->abort(); } activeTxn = nullptr; pdb = nullptr; CloseCursor(); if (fFlushOnClose) { Flush(); } } void BerkeleyEnvironment::CloseDb(const std::string &strFile) { LOCK(cs_db); auto it = m_databases.find(strFile); assert(it != m_databases.end()); BerkeleyDatabase &database = it->second.get(); if (database.m_db) { // Close the database handle database.m_db->close(0); database.m_db.reset(); } } void BerkeleyEnvironment::ReloadDbEnv() { // Make sure that no Db's are in use AssertLockNotHeld(cs_db); std::unique_lock lock(cs_db); m_db_in_use.wait(lock, [this]() { for (auto &db : m_databases) { if (db.second.get().m_refcount > 0) { return false; } } return true; }); std::vector filenames; for (auto it : m_databases) { filenames.push_back(it.first); } // Close the individual Db's for (const std::string &filename : filenames) { CloseDb(filename); } // Reset the environment // This will flush and close the environment Flush(true); Reset(); bilingual_str open_err; Open(open_err); } bool BerkeleyDatabase::Rewrite(const char *pszSkip) { while (true) { { LOCK(cs_db); if (m_refcount <= 0) { // Flush log data to the dat file env->CloseDb(strFile); env->CheckpointLSN(strFile); m_refcount = -1; bool fSuccess = true; LogPrintf("BerkeleyBatch::Rewrite: Rewriting %s...\n", strFile); std::string strFileRes = strFile + ".rewrite"; { // surround usage of db with extra {} BerkeleyBatch db(*this, "r"); std::unique_ptr pdbCopy = std::make_unique(env->dbenv.get(), 0); int ret = pdbCopy->open(nullptr, // Txn pointer strFileRes.c_str(), // Filename "main", // Logical db name DB_BTREE, // Database type DB_CREATE, // Flags 0); if (ret > 0) { LogPrintf("BerkeleyBatch::Rewrite: Can't create " "database file %s\n", strFileRes); fSuccess = false; } if (db.StartCursor()) { while (fSuccess) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssValue(SER_DISK, CLIENT_VERSION); bool complete; bool ret1 = db.ReadAtCursor(ssKey, ssValue, complete); if (complete) { break; } if (!ret1) { fSuccess = false; break; } if (pszSkip && strncmp(ssKey.data(), pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0) { continue; } if (strncmp(ssKey.data(), "\x07version", 8) == 0) { // Update version: ssValue.clear(); ssValue << CLIENT_VERSION; } Dbt datKey(ssKey.data(), ssKey.size()); Dbt datValue(ssValue.data(), ssValue.size()); int ret2 = pdbCopy->put(nullptr, &datKey, &datValue, DB_NOOVERWRITE); if (ret2 > 0) { fSuccess = false; } } db.CloseCursor(); } if (fSuccess) { db.Close(); env->CloseDb(strFile); if (pdbCopy->close(0)) { fSuccess = false; } } else { pdbCopy->close(0); } } if (fSuccess) { Db dbA(env->dbenv.get(), 0); if (dbA.remove(strFile.c_str(), nullptr, 0)) { fSuccess = false; } Db dbB(env->dbenv.get(), 0); if (dbB.rename(strFileRes.c_str(), nullptr, strFile.c_str(), 0)) { fSuccess = false; } } if (!fSuccess) { LogPrintf("BerkeleyBatch::Rewrite: Failed to rewrite " "database file %s\n", strFileRes); } return fSuccess; } } UninterruptibleSleep(std::chrono::milliseconds{100}); } } void BerkeleyEnvironment::Flush(bool fShutdown) { int64_t nStart = GetTimeMillis(); // Flush log data to the actual data file on all files that are not in use LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: [%s] Flush(%s)%s\n", strPath, fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started"); if (!fDbEnvInit) { return; } { LOCK(cs_db); bool no_dbs_accessed = true; for (auto &db_it : m_databases) { std::string strFile = db_it.first; int nRefCount = db_it.second.get().m_refcount; if (nRefCount < 0) { continue; } LogPrint( BCLog::WALLETDB, "BerkeleyEnvironment::Flush: Flushing %s (refcount = %d)...\n", strFile, nRefCount); if (nRefCount == 0) { // Move log data to the dat file CloseDb(strFile); LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: %s checkpoint\n", strFile); dbenv->txn_checkpoint(0, 0, 0); LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: %s detach\n", strFile); if (!fMockDb) { dbenv->lsn_reset(strFile.c_str(), 0); } LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: %s closed\n", strFile); nRefCount = -1; } else { no_dbs_accessed = false; } } LogPrint(BCLog::WALLETDB, "BerkeleyEnvironment::Flush: Flush(%s)%s took %15dms\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started", GetTimeMillis() - nStart); if (fShutdown) { char **listp; if (no_dbs_accessed) { dbenv->log_archive(&listp, DB_ARCH_REMOVE); Close(); if (!fMockDb) { fs::remove_all(fs::path(strPath) / "database"); } } } } } bool BerkeleyDatabase::PeriodicFlush() { // Don't flush if we can't acquire the lock. TRY_LOCK(cs_db, lockDb); if (!lockDb) { return false; } // Don't flush if any databases are in use for (auto &it : env->m_databases) { if (it.second.get().m_refcount > 0) { return false; } } // Don't flush if there haven't been any batch writes for this database. if (m_refcount < 0) { return false; } LogPrint(BCLog::WALLETDB, "Flushing %s\n", strFile); int64_t nStart = GetTimeMillis(); // Flush wallet file so it's self contained env->CloseDb(strFile); env->CheckpointLSN(strFile); m_refcount = -1; LogPrint(BCLog::WALLETDB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart); return true; } bool BerkeleyDatabase::Backup(const std::string &strDest) const { while (true) { { LOCK(cs_db); if (m_refcount <= 0) { // Flush log data to the dat file env->CloseDb(strFile); env->CheckpointLSN(strFile); // Copy wallet file. fs::path pathSrc = env->Directory() / strFile; fs::path pathDest(strDest); if (fs::is_directory(pathDest)) { pathDest /= strFile; } try { if (fs::equivalent(pathSrc, pathDest)) { LogPrintf("cannot backup to wallet source file %s\n", pathDest.string()); return false; } fs::copy_file(pathSrc, pathDest, fs::copy_option::overwrite_if_exists); LogPrintf("copied %s to %s\n", strFile, pathDest.string()); return true; } catch (const fs::filesystem_error &e) { LogPrintf("error copying %s to %s - %s\n", strFile, pathDest.string(), fsbridge::get_filesystem_error_message(e)); return false; } } } UninterruptibleSleep(std::chrono::milliseconds{100}); } } void BerkeleyDatabase::Flush() { env->Flush(false); } void BerkeleyDatabase::Close() { env->Flush(true); } void BerkeleyDatabase::ReloadDbEnv() { env->ReloadDbEnv(); } bool BerkeleyBatch::StartCursor() { assert(!m_cursor); if (!pdb) { return false; } int ret = pdb->cursor(nullptr, &m_cursor, 0); return ret == 0; } bool BerkeleyBatch::ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, bool &complete) { complete = false; if (m_cursor == nullptr) { return false; } // Read at cursor SafeDbt datKey; SafeDbt datValue; int ret = m_cursor->get(datKey, datValue, DB_NEXT); if (ret == DB_NOTFOUND) { complete = true; } if (ret != 0) { return false; } else if (datKey.get_data() == nullptr || datValue.get_data() == nullptr) { return false; } // Convert to streams ssKey.SetType(SER_DISK); ssKey.clear(); ssKey.write((char *)datKey.get_data(), datKey.get_size()); ssValue.SetType(SER_DISK); ssValue.clear(); ssValue.write((char *)datValue.get_data(), datValue.get_size()); return true; } void BerkeleyBatch::CloseCursor() { if (!m_cursor) { return; } m_cursor->close(); m_cursor = nullptr; } bool BerkeleyBatch::TxnBegin() { if (!pdb || activeTxn) { return false; } DbTxn *ptxn = env->TxnBegin(); if (!ptxn) { return false; } activeTxn = ptxn; return true; } bool BerkeleyBatch::TxnCommit() { if (!pdb || !activeTxn) { return false; } int ret = activeTxn->commit(0); activeTxn = nullptr; return (ret == 0); } bool BerkeleyBatch::TxnAbort() { if (!pdb || !activeTxn) { return false; } int ret = activeTxn->abort(); activeTxn = nullptr; return (ret == 0); } std::string BerkeleyDatabaseVersion() { return DbEnv::version(nullptr, nullptr, nullptr); } bool BerkeleyBatch::ReadKey(CDataStream &&key, CDataStream &value) { if (!pdb) { return false; } SafeDbt datKey(key.data(), key.size()); SafeDbt datValue; int ret = pdb->get(activeTxn, datKey, datValue, 0); if (ret == 0 && datValue.get_data() != nullptr) { value.write((char *)datValue.get_data(), datValue.get_size()); return true; } return false; } bool BerkeleyBatch::WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite) { if (!pdb) { return false; } if (fReadOnly) { assert(!"Write called on database in read-only mode"); } SafeDbt datKey(key.data(), key.size()); SafeDbt datValue(value.data(), value.size()); int ret = pdb->put(activeTxn, datKey, datValue, (overwrite ? 0 : DB_NOOVERWRITE)); return (ret == 0); } bool BerkeleyBatch::EraseKey(CDataStream &&key) { if (!pdb) { return false; } if (fReadOnly) { assert(!"Erase called on database in read-only mode"); } SafeDbt datKey(key.data(), key.size()); int ret = pdb->del(activeTxn, datKey, 0); return (ret == 0 || ret == DB_NOTFOUND); } bool BerkeleyBatch::HasKey(CDataStream &&key) { if (!pdb) { return false; } SafeDbt datKey(key.data(), key.size()); int ret = pdb->exists(activeTxn, datKey, 0); return ret == 0; } void BerkeleyDatabase::AddRef() { LOCK(cs_db); if (m_refcount < 0) { m_refcount = 1; } else { m_refcount++; } } void BerkeleyDatabase::RemoveRef() { LOCK(cs_db); m_refcount--; if (env) { env->m_db_in_use.notify_all(); } } std::unique_ptr BerkeleyDatabase::MakeBatch(const char *mode, bool flush_on_close) { return std::make_unique(*this, mode, flush_on_close); } bool ExistsBerkeleyDatabase(const fs::path &path) { fs::path env_directory; std::string data_filename; SplitWalletPath(path, env_directory, data_filename); return IsBerkeleyBtree(env_directory / data_filename); } std::unique_ptr MakeBerkeleyDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error) { std::unique_ptr db; { // Lock env.m_databases until insert in BerkeleyDatabase constructor LOCK(cs_db); std::string data_filename; std::shared_ptr env = GetWalletEnv(path, data_filename); if (env->m_databases.count(data_filename)) { error = Untranslated(strprintf( "Refusing to load database. Data file '%s' is already loaded.", (env->Directory() / data_filename).string())); status = DatabaseStatus::FAILED_ALREADY_LOADED; return nullptr; } db = std::make_unique(std::move(env), std::move(data_filename)); } if (options.verify && !db->Verify(error)) { status = DatabaseStatus::FAILED_VERIFY; return nullptr; } status = DatabaseStatus::SUCCESS; return db; } diff --git a/src/wallet/bdb.h b/src/wallet/bdb.h index df98ba783..dfb724047 100644 --- a/src/wallet/bdb.h +++ b/src/wallet/bdb.h @@ -1,253 +1,258 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2020 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_WALLET_BDB_H #define BITCOIN_WALLET_BDB_H #include #include #include #include #include #include #include #include #include #include #include #include #include struct bilingual_str; static const unsigned int DEFAULT_WALLET_DBLOGSIZE = 100; static const bool DEFAULT_WALLET_PRIVDB = true; struct WalletDatabaseFileId { u_int8_t value[DB_FILE_ID_LEN]; bool operator==(const WalletDatabaseFileId &rhs) const; }; class BerkeleyDatabase; class BerkeleyEnvironment { private: bool fDbEnvInit; bool fMockDb; // Don't change into fs::path, as that can result in // shutdown problems/crashes caused by a static initialized internal // pointer. std::string strPath; public: std::unique_ptr dbenv; std::map> m_databases; std::unordered_map m_fileids; std::condition_variable_any m_db_in_use; BerkeleyEnvironment(const fs::path &env_directory); BerkeleyEnvironment(); ~BerkeleyEnvironment(); void Reset(); void MakeMock(); bool IsMock() const { return fMockDb; } bool IsInitialized() const { return fDbEnvInit; } fs::path Directory() const { return strPath; } bool Open(bilingual_str &error); void Close(); void Flush(bool fShutdown); void CheckpointLSN(const std::string &strFile); void CloseDb(const std::string &strFile); void ReloadDbEnv(); DbTxn *TxnBegin(int flags = DB_TXN_WRITE_NOSYNC) { DbTxn *ptxn = nullptr; int ret = dbenv->txn_begin(nullptr, &ptxn, flags); if (!ptxn || ret != 0) { return nullptr; } return ptxn; } }; /** Get BerkeleyEnvironment and database filename given a wallet path. */ std::shared_ptr GetWalletEnv(const fs::path &wallet_path, std::string &database_filename); /** Check format of database file */ bool IsBerkeleyBtree(const fs::path &path); class BerkeleyBatch; /** * An instance of this class represents one database. * For BerkeleyDB this is just a (env, strFile) tuple. */ class BerkeleyDatabase : public WalletDatabase { public: BerkeleyDatabase() = delete; /** Create DB handle to real database */ BerkeleyDatabase(std::shared_ptr envIn, std::string filename) : WalletDatabase(), env(std::move(envIn)), strFile(std::move(filename)) { auto inserted = this->env->m_databases.emplace(strFile, std::ref(*this)); assert(inserted.second); } ~BerkeleyDatabase() override; /** * Open the database if it is not already opened. * Dummy function, doesn't do anything right now, but is needed for class * abstraction */ void Open(const char *mode) override; /** * Rewrite the entire database on disk, with the exception of key pszSkip if * non-zero */ bool Rewrite(const char *pszSkip = nullptr) override; /** Indicate the a new database user has began using the database. */ void AddRef() override; /** * Indicate that database user has stopped using the database and that it * could be flushed or closed. */ void RemoveRef() override; /** * Back up the entire database to a file. */ bool Backup(const std::string &strDest) const override; /** * Make sure all changes are flushed to database file. */ void Flush() override; /** * Flush to the database file and close the database. * Also close the environment if no other databases are open in it. */ void Close() override; /** * flush the wallet passively (TRY_LOCK) * ideal to be called periodically */ bool PeriodicFlush() override; void IncrementUpdateCounter() override; void ReloadDbEnv() override; /** Verifies the environment and database file */ bool Verify(bilingual_str &error); + /** Return path to main database filename */ + std::string Filename() override { + return (env->Directory() / strFile).string(); + } + /** * Pointer to shared database environment. * * Normally there is only one BerkeleyDatabase object per * BerkeleyEnvivonment, but in the special, backwards compatible case where * multiple wallet BDB data files are loaded from the same directory, this * will point to a shared instance that gets freed when the last data file * is closed. */ std::shared_ptr env; /** * Database pointer. This is initialized lazily and reset during flushes, * so it can be null. */ std::unique_ptr m_db; std::string strFile; /** Make a BerkeleyBatch connected to this database */ std::unique_ptr MakeBatch(const char *mode = "r+", bool flush_on_close = true) override; }; /** RAII class that provides access to a Berkeley database */ class BerkeleyBatch : public DatabaseBatch { /** RAII class that automatically cleanses its data on destruction */ class SafeDbt final { Dbt m_dbt; public: // construct Dbt with internally-managed data SafeDbt(); // construct Dbt with provided data SafeDbt(void *data, size_t size); ~SafeDbt(); // delegate to Dbt const void *get_data() const; u_int32_t get_size() const; // conversion operator to access the underlying Dbt operator Dbt *(); }; private: bool ReadKey(CDataStream &&key, CDataStream &value) override; bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite = true) override; bool EraseKey(CDataStream &&key) override; bool HasKey(CDataStream &&key) override; protected: Db *pdb; std::string strFile; DbTxn *activeTxn; Dbc *m_cursor; bool fReadOnly; bool fFlushOnClose; BerkeleyEnvironment *env; BerkeleyDatabase &m_database; public: explicit BerkeleyBatch(BerkeleyDatabase &database, const char *pszMode = "r+", bool fFlushOnCloseIn = true); ~BerkeleyBatch() override; BerkeleyBatch(const BerkeleyBatch &) = delete; BerkeleyBatch &operator=(const BerkeleyBatch &) = delete; void Flush() override; void Close() override; bool StartCursor() override; bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, bool &complete) override; void CloseCursor() override; bool TxnBegin() override; bool TxnCommit() override; bool TxnAbort() override; }; std::string BerkeleyDatabaseVersion(); //! Check if Berkeley database exists at specified path. bool ExistsBerkeleyDatabase(const fs::path &path); //! Return object giving access to Berkeley database at specified path. std::unique_ptr MakeBerkeleyDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error); #endif // BITCOIN_WALLET_BDB_H diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index 37fdf43c5..aa14d4d3d 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -1,32 +1,25 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include void SplitWalletPath(const fs::path &wallet_path, fs::path &env_directory, std::string &database_filename) { if (fs::is_regular_file(wallet_path)) { // Special case for backwards compatibility: if wallet path points to an // existing file, treat it as the path to a BDB data file in a parent // directory that also contains BDB log files. env_directory = wallet_path.parent_path(); database_filename = wallet_path.filename().string(); } else { // Normal case: Interpret wallet path as a directory path containing // data and log files. env_directory = wallet_path; database_filename = "wallet.dat"; } } - -fs::path WalletDataFilePath(const fs::path &wallet_path) { - fs::path env_directory; - std::string database_filename; - SplitWalletPath(wallet_path, env_directory, database_filename); - return env_directory / database_filename; -} diff --git a/src/wallet/db.h b/src/wallet/db.h index 64797c8d9..62b3e59ef 100644 --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -1,249 +1,246 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_WALLET_DB_H #define BITCOIN_WALLET_DB_H #include #include #include #include #include #include #include struct bilingual_str; -/** - * Given a wallet directory path or legacy file path, return path to main data - * file in the wallet database. - */ -fs::path WalletDataFilePath(const fs::path &wallet_path); void SplitWalletPath(const fs::path &wallet_path, fs::path &env_directory, std::string &database_filename); /** RAII class that provides access to a WalletDatabase */ class DatabaseBatch { private: virtual bool ReadKey(CDataStream &&key, CDataStream &value) = 0; virtual bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite = true) = 0; virtual bool EraseKey(CDataStream &&key) = 0; virtual bool HasKey(CDataStream &&key) = 0; public: explicit DatabaseBatch() {} virtual ~DatabaseBatch() {} DatabaseBatch(const DatabaseBatch &) = delete; DatabaseBatch &operator=(const DatabaseBatch &) = delete; virtual void Flush() = 0; virtual void Close() = 0; template bool Read(const K &key, T &value) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); ssKey.reserve(1000); ssKey << key; CDataStream ssValue(SER_DISK, CLIENT_VERSION); if (!ReadKey(std::move(ssKey), ssValue)) { return false; } try { ssValue >> value; return true; } catch (const std::exception &) { return false; } } template bool Write(const K &key, const T &value, bool fOverwrite = true) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); ssKey.reserve(1000); ssKey << key; CDataStream ssValue(SER_DISK, CLIENT_VERSION); ssValue.reserve(10000); ssValue << value; return WriteKey(std::move(ssKey), std::move(ssValue), fOverwrite); } template bool Erase(const K &key) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); ssKey.reserve(1000); ssKey << key; return EraseKey(std::move(ssKey)); } template bool Exists(const K &key) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); ssKey.reserve(1000); ssKey << key; return HasKey(std::move(ssKey)); } virtual bool StartCursor() = 0; virtual bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, bool &complete) = 0; virtual void CloseCursor() = 0; virtual bool TxnBegin() = 0; virtual bool TxnCommit() = 0; virtual bool TxnAbort() = 0; }; /** * An instance of this class represents one database. */ class WalletDatabase { public: /** Create dummy DB handle */ WalletDatabase() : nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0) {} virtual ~WalletDatabase(){}; /** Open the database if it is not already opened. */ virtual void Open(const char *mode) = 0; //! Counts the number of active database users to be sure that the database //! is not closed while someone is using it std::atomic m_refcount{0}; /** * Indicate the a new database user has began using the database. * Increments m_refcount */ virtual void AddRef() = 0; /** * Indicate that database user has stopped using the database and that it * could be flushed or closed. Decrement m_refcount */ virtual void RemoveRef() = 0; /** * Rewrite the entire database on disk, with the exception of key pszSkip * if non-zero */ virtual bool Rewrite(const char *pszSkip = nullptr) = 0; /** * Back up the entire database to a file. */ virtual bool Backup(const std::string &strDest) const = 0; /** * Make sure all changes are flushed to database file. */ virtual void Flush() = 0; /** * Flush to the database file and close the database. * Also close the environment if no other databases are open in it. */ virtual void Close() = 0; /* flush the wallet passively (TRY_LOCK) ideal to be called periodically */ virtual bool PeriodicFlush() = 0; virtual void IncrementUpdateCounter() = 0; virtual void ReloadDbEnv() = 0; + /** Return path to main database file for logs and error messages. */ + virtual std::string Filename() = 0; + std::atomic nUpdateCounter; unsigned int nLastSeen; unsigned int nLastFlushed; int64_t nLastWalletUpdate; - std::string m_file_path; - /** Make a DatabaseBatch connected to this database */ virtual std::unique_ptr MakeBatch(const char *mode = "r+", bool flush_on_close = true) = 0; }; /** RAII class that provides access to a DummyDatabase. Never fails. */ class DummyBatch : public DatabaseBatch { private: bool ReadKey(CDataStream &&key, CDataStream &value) override { return true; } bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite = true) override { return true; } bool EraseKey(CDataStream &&key) override { return true; } bool HasKey(CDataStream &&key) override { return true; } public: void Flush() override {} void Close() override {} bool StartCursor() override { return true; } bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, bool &complete) override { return true; } void CloseCursor() override {} bool TxnBegin() override { return true; } bool TxnCommit() override { return true; } bool TxnAbort() override { return true; } }; /** * A dummy WalletDatabase that does nothing and never fails. Only used by unit * tests. */ class DummyDatabase : public WalletDatabase { public: void Open(const char *mode) override{}; void AddRef() override {} void RemoveRef() override {} bool Rewrite(const char *pszSkip = nullptr) override { return true; } bool Backup(const std::string &strDest) const override { return true; } void Close() override {} void Flush() override {} bool PeriodicFlush() override { return true; } void IncrementUpdateCounter() override { ++nUpdateCounter; } void ReloadDbEnv() override {} + std::string Filename() override { return "dummy"; } std::unique_ptr MakeBatch(const char *mode = "r+", bool flush_on_close = true) override { return std::make_unique(); } }; enum class DatabaseFormat { BERKELEY, }; struct DatabaseOptions { bool require_existing = false; bool require_create = false; uint64_t create_flags = 0; SecureString create_passphrase; bool verify = true; }; enum class DatabaseStatus { SUCCESS, FAILED_BAD_PATH, FAILED_BAD_FORMAT, FAILED_ALREADY_LOADED, FAILED_ALREADY_EXISTS, FAILED_NOT_FOUND, FAILED_CREATE, FAILED_VERIFY, FAILED_ENCRYPT, }; std::unique_ptr MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error); #endif // BITCOIN_WALLET_DB_H diff --git a/src/wallet/load.cpp b/src/wallet/load.cpp index 500ece2d9..8ec90aeca 100644 --- a/src/wallet/load.cpp +++ b/src/wallet/load.cpp @@ -1,145 +1,155 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2018 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include #include #include #include bool VerifyWallets(interfaces::Chain &chain, const std::vector &wallet_files) { if (gArgs.IsArgSet("-walletdir")) { fs::path wallet_dir = gArgs.GetArg("-walletdir", ""); boost::system::error_code error; // The canonical path cleans the path, preventing >1 Berkeley // environment instances for the same directory fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error); if (error || !fs::exists(wallet_dir)) { chain.initError( strprintf(_("Specified -walletdir \"%s\" does not exist"), wallet_dir.string())); return false; } else if (!fs::is_directory(wallet_dir)) { chain.initError( strprintf(_("Specified -walletdir \"%s\" is not a directory"), wallet_dir.string())); return false; // The canonical path transforms relative paths into absolute ones, // so we check the non-canonical version } else if (!wallet_dir.is_absolute()) { chain.initError( strprintf(_("Specified -walletdir \"%s\" is a relative path"), wallet_dir.string())); return false; } gArgs.ForceSetArg("-walletdir", canonical_wallet_dir.string()); } LogPrintf("Using wallet directory %s\n", GetWalletDir().string()); chain.initMessage(_("Verifying wallet(s)...").translated); // Keep track of each wallet absolute path to detect duplicates. std::set wallet_paths; for (const auto &wallet_file : wallet_files) { const fs::path path = fs::absolute(wallet_file, GetWalletDir()); if (!wallet_paths.insert(path).second) { chain.initError(strprintf(_("Error loading wallet %s. Duplicate " "-wallet filename specified."), wallet_file)); return false; } DatabaseOptions options; DatabaseStatus status; options.verify = true; bilingual_str error_string; if (!MakeWalletDatabase(wallet_file, options, status, error_string)) { chain.initError(error_string); return false; } } return true; } bool LoadWallets(interfaces::Chain &chain, const std::vector &wallet_files) { try { - for (const std::string &walletFile : wallet_files) { + for (const std::string &name : wallet_files) { + DatabaseOptions options; + DatabaseStatus status; + // No need to verify, assuming verified earlier in VerifyWallets() + options.verify = false; bilingual_str error; std::vector warnings; - std::shared_ptr pwallet = CWallet::CreateWalletFromFile( - chain, walletFile, error, warnings); + std::unique_ptr database = + MakeWalletDatabase(name, options, status, error); + std::shared_ptr pwallet = + database + ? CWallet::Create(chain, name, std::move(database), + options.create_flags, error, warnings) + : nullptr; + if (!warnings.empty()) { chain.initWarning(Join(warnings, Untranslated("\n"))); } if (!pwallet) { chain.initError(error); return false; } AddWallet(pwallet); } return true; } catch (const std::runtime_error &e) { chain.initError(Untranslated(e.what())); return false; } } void StartWallets(CScheduler &scheduler, const ArgsManager &args) { for (const std::shared_ptr &pwallet : GetWallets()) { pwallet->postInitProcess(); } // Schedule periodic wallet flushes and tx rebroadcasts if (args.GetBoolArg("-flushwallet", DEFAULT_FLUSHWALLET)) { scheduler.scheduleEvery( [] { MaybeCompactWalletDB(); return true; }, std::chrono::milliseconds{500}); } scheduler.scheduleEvery( [] { MaybeResendWalletTxs(); return true; }, std::chrono::milliseconds{1000}); } void FlushWallets() { for (const std::shared_ptr &pwallet : GetWallets()) { pwallet->Flush(); } } void StopWallets() { for (const std::shared_ptr &pwallet : GetWallets()) { pwallet->Close(); } } void UnloadWallets() { auto wallets = GetWallets(); while (!wallets.empty()) { auto wallet = wallets.back(); wallets.pop_back(); std::vector warnings; RemoveWallet(wallet, std::nullopt, warnings); UnloadWallet(std::move(wallet)); } } diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp index 8c6d2115c..744d4f323 100644 --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -1,902 +1,906 @@ // Copyright (c) 2012-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup) static std::shared_ptr TestLoadWallet(interfaces::Chain &chain) { + DatabaseOptions options; + DatabaseStatus status; bilingual_str error; std::vector warnings; - auto wallet = CWallet::CreateWalletFromFile(chain, "", error, warnings); + auto database = MakeWalletDatabase("", options, status, error); + auto wallet = CWallet::Create(chain, "", std::move(database), + options.create_flags, error, warnings); wallet->postInitProcess(); return wallet; } static void TestUnloadWallet(std::shared_ptr &&wallet) { SyncWithValidationInterfaceQueue(); wallet->m_chain_notifications_handler.reset(); UnloadWallet(std::move(wallet)); } static CMutableTransaction TestSimpleSpend(const CTransaction &from, uint32_t index, const CKey &key, const CScript &pubkey) { CMutableTransaction mtx; mtx.vout.push_back( {from.vout[index].nValue - DEFAULT_TRANSACTION_MAXFEE, pubkey}); mtx.vin.push_back({CTxIn{from.GetId(), index}}); FillableSigningProvider keystore; keystore.AddKey(key); std::map coins; coins[mtx.vin[0].prevout].GetTxOut() = from.vout[index]; std::map input_errors; BOOST_CHECK(SignTransaction(mtx, &keystore, coins, SigHashType().withForkId(), input_errors)); return mtx; } static void AddKey(CWallet &wallet, const CKey &key) { auto spk_man = wallet.GetOrCreateLegacyScriptPubKeyMan(); LOCK2(wallet.cs_wallet, spk_man->cs_KeyStore); spk_man->AddKeyPubKey(key, key.GetPubKey()); } BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup) { // Cap last block file size, and mine new block in a new block file. CBlockIndex *oldTip = ::ChainActive().Tip(); GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE; CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); CBlockIndex *newTip = ::ChainActive().Tip(); NodeContext node; auto chain = interfaces::MakeChain(node, Params()); // Verify ScanForWalletTransactions fails to read an unknown start block. { CWallet wallet(chain.get(), "", CreateDummyWalletDatabase()); { LOCK(wallet.cs_wallet); wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash()); } AddKey(wallet, coinbaseKey); WalletRescanReserver reserver(wallet); reserver.reserve(); CWallet::ScanResult result = wallet.ScanForWalletTransactions( BlockHash() /* start_block */, 0 /* start_height */, {} /* max_height */, reserver, false /* update */); BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE); BOOST_CHECK(result.last_failed_block.IsNull()); BOOST_CHECK(result.last_scanned_block.IsNull()); BOOST_CHECK(!result.last_scanned_height); BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, Amount::zero()); } // Verify ScanForWalletTransactions picks up transactions in both the old // and new block files. { CWallet wallet(chain.get(), "", CreateDummyWalletDatabase()); { LOCK(wallet.cs_wallet); wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash()); } AddKey(wallet, coinbaseKey); WalletRescanReserver reserver(wallet); reserver.reserve(); CWallet::ScanResult result = wallet.ScanForWalletTransactions( oldTip->GetBlockHash(), oldTip->nHeight, {} /* max_height */, reserver, false /* update */); BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS); BOOST_CHECK(result.last_failed_block.IsNull()); BOOST_CHECK_EQUAL(result.last_scanned_block, newTip->GetBlockHash()); BOOST_CHECK_EQUAL(*result.last_scanned_height, newTip->nHeight); BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, 100 * COIN); } // Prune the older block file. { LOCK(cs_main); Assert(m_node.chainman) ->m_blockman.PruneOneBlockFile(oldTip->GetBlockPos().nFile); } UnlinkPrunedFiles({oldTip->GetBlockPos().nFile}); // Verify ScanForWalletTransactions only picks transactions in the new block // file. { CWallet wallet(chain.get(), "", CreateDummyWalletDatabase()); { LOCK(wallet.cs_wallet); wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash()); } AddKey(wallet, coinbaseKey); WalletRescanReserver reserver(wallet); reserver.reserve(); CWallet::ScanResult result = wallet.ScanForWalletTransactions( oldTip->GetBlockHash(), oldTip->nHeight, {} /* max_height */, reserver, false /* update */); BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE); BOOST_CHECK_EQUAL(result.last_failed_block, oldTip->GetBlockHash()); BOOST_CHECK_EQUAL(result.last_scanned_block, newTip->GetBlockHash()); BOOST_CHECK_EQUAL(*result.last_scanned_height, newTip->nHeight); BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, 50 * COIN); } // Prune the remaining block file. { LOCK(cs_main); Assert(m_node.chainman) ->m_blockman.PruneOneBlockFile(newTip->GetBlockPos().nFile); } UnlinkPrunedFiles({newTip->GetBlockPos().nFile}); // Verify ScanForWalletTransactions scans no blocks. { CWallet wallet(chain.get(), "", CreateDummyWalletDatabase()); { LOCK(wallet.cs_wallet); wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash()); } AddKey(wallet, coinbaseKey); WalletRescanReserver reserver(wallet); reserver.reserve(); CWallet::ScanResult result = wallet.ScanForWalletTransactions( oldTip->GetBlockHash(), oldTip->nHeight, {} /* max_height */, reserver, false /* update */); BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE); BOOST_CHECK_EQUAL(result.last_failed_block, newTip->GetBlockHash()); BOOST_CHECK(result.last_scanned_block.IsNull()); BOOST_CHECK(!result.last_scanned_height); BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, Amount::zero()); } } BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup) { // Cap last block file size, and mine new block in a new block file. CBlockIndex *oldTip = ::ChainActive().Tip(); GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE; CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); CBlockIndex *newTip = ::ChainActive().Tip(); NodeContext node; auto chain = interfaces::MakeChain(node, Params()); // Prune the older block file. { LOCK(cs_main); Assert(m_node.chainman) ->m_blockman.PruneOneBlockFile(oldTip->GetBlockPos().nFile); } UnlinkPrunedFiles({oldTip->GetBlockPos().nFile}); // Verify importmulti RPC returns failure for a key whose creation time is // before the missing block, and success for a key whose creation time is // after. { std::shared_ptr wallet = std::make_shared( chain.get(), "", CreateDummyWalletDatabase()); wallet->SetupLegacyScriptPubKeyMan(); WITH_LOCK(wallet->cs_wallet, wallet->SetLastBlockProcessed(newTip->nHeight, newTip->GetBlockHash())); AddWallet(wallet); UniValue keys; keys.setArray(); UniValue key; key.setObject(); key.pushKV("scriptPubKey", HexStr(GetScriptForRawPubKey(coinbaseKey.GetPubKey()))); key.pushKV("timestamp", 0); key.pushKV("internal", UniValue(true)); keys.push_back(key); key.clear(); key.setObject(); CKey futureKey; futureKey.MakeNewKey(true); key.pushKV("scriptPubKey", HexStr(GetScriptForRawPubKey(futureKey.GetPubKey()))); key.pushKV("timestamp", newTip->GetBlockTimeMax() + TIMESTAMP_WINDOW + 1); key.pushKV("internal", UniValue(true)); keys.push_back(key); util::Ref context; JSONRPCRequest request(context); request.params.setArray(); request.params.push_back(keys); UniValue response = importmulti().HandleRequest(GetConfig(), request); BOOST_CHECK_EQUAL( response.write(), strprintf("[{\"success\":false,\"error\":{\"code\":-1,\"message\":" "\"Rescan failed for key with creation timestamp %d. " "There was an error reading a block from time %d, which " "is after or within %d seconds of key creation, and " "could contain transactions pertaining to the key. As a " "result, transactions and coins using this key may not " "appear in the wallet. This error could be caused by " "pruning or data corruption (see bitcoind log for " "details) and could be dealt with by downloading and " "rescanning the relevant blocks (see -reindex and " "-rescan options).\"}},{\"success\":true}]", 0, oldTip->GetBlockTimeMax(), TIMESTAMP_WINDOW)); RemoveWallet(wallet, std::nullopt); } } // Verify importwallet RPC starts rescan at earliest block with timestamp // greater or equal than key birthday. Previously there was a bug where // importwallet RPC would start the scan at the latest block with timestamp less // than or equal to key birthday. BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup) { // Create two blocks with same timestamp to verify that importwallet rescan // will pick up both blocks, not just the first. const int64_t BLOCK_TIME = ::ChainActive().Tip()->GetBlockTimeMax() + 5; SetMockTime(BLOCK_TIME); m_coinbase_txns.emplace_back( CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())) .vtx[0]); m_coinbase_txns.emplace_back( CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())) .vtx[0]); // Set key birthday to block time increased by the timestamp window, so // rescan will start at the block time. const int64_t KEY_TIME = BLOCK_TIME + TIMESTAMP_WINDOW; SetMockTime(KEY_TIME); m_coinbase_txns.emplace_back( CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())) .vtx[0]); NodeContext node; auto chain = interfaces::MakeChain(node, Params()); std::string backup_file = (GetDataDir() / "wallet.backup").string(); // Import key into wallet and call dumpwallet to create backup file. { std::shared_ptr wallet = std::make_shared( 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()); AddWallet(wallet); wallet->SetLastBlockProcessed( ::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash()); } util::Ref context; JSONRPCRequest request(context); request.params.setArray(); request.params.push_back(backup_file); ::dumpwallet().HandleRequest(GetConfig(), request); RemoveWallet(wallet, std::nullopt); } // Call importwallet RPC and verify all blocks with timestamps >= BLOCK_TIME // were scanned, and no prior blocks were scanned. { std::shared_ptr wallet = std::make_shared( chain.get(), "", CreateDummyWalletDatabase()); LOCK(wallet->cs_wallet); wallet->SetupLegacyScriptPubKeyMan(); util::Ref context; JSONRPCRequest request(context); request.params.setArray(); request.params.push_back(backup_file); AddWallet(wallet); wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash()); ::importwallet().HandleRequest(GetConfig(), request); RemoveWallet(wallet, std::nullopt); BOOST_CHECK_EQUAL(wallet->mapWallet.size(), 3U); BOOST_CHECK_EQUAL(m_coinbase_txns.size(), 103U); for (size_t i = 0; i < m_coinbase_txns.size(); ++i) { bool found = wallet->GetWalletTx(m_coinbase_txns[i]->GetId()); bool expected = i >= 100; BOOST_CHECK_EQUAL(found, expected); } } } // Check that GetImmatureCredit() returns a newly calculated value instead of // the cached value after a MarkDirty() call. // // This is a regression test written to verify a bugfix for the immature credit // function. Similar tests probably should be written for the other credit and // debit functions. BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup) { NodeContext node; auto chain = interfaces::MakeChain(node, Params()); CWallet wallet(chain.get(), "", CreateDummyWalletDatabase()); auto spk_man = wallet.GetOrCreateLegacyScriptPubKeyMan(); CWalletTx wtx(&wallet, m_coinbase_txns.back()); LOCK2(wallet.cs_wallet, spk_man->cs_KeyStore); wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash()); CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, ::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash(), 0); wtx.m_confirm = confirm; // Call GetImmatureCredit() once before adding the key to the wallet to // cache the current immature credit amount, which is 0. BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(), Amount::zero()); // Invalidate the cached value, add the key, and make sure a new immature // credit amount is calculated. wtx.MarkDirty(); BOOST_CHECK(spk_man->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey())); BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(), 50 * COIN); } static int64_t AddTx(ChainstateManager &chainman, CWallet &wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime) { CMutableTransaction tx; CWalletTx::Confirmation confirm; tx.nLockTime = lockTime; SetMockTime(mockTime); CBlockIndex *block = nullptr; if (blockTime > 0) { LOCK(cs_main); auto inserted = chainman.BlockIndex().emplace(BlockHash(GetRandHash()), new CBlockIndex); assert(inserted.second); const BlockHash &hash = inserted.first->first; block = inserted.first->second; block->nTime = blockTime; block->phashBlock = &hash; confirm = {CWalletTx::Status::CONFIRMED, block->nHeight, hash, 0}; } // If transaction is already in map, to avoid inconsistencies, // unconfirmation is needed before confirm again with different block. return wallet .AddToWallet(MakeTransactionRef(tx), confirm, [&](CWalletTx &wtx, bool /* new_tx */) { wtx.setUnconfirmed(); return true; }) ->nTimeSmart; } // Simple test to verify assignment of CWalletTx::nSmartTime value. Could be // expanded to cover more corner cases of smart time logic. BOOST_AUTO_TEST_CASE(ComputeTimeSmart) { // New transaction should use clock time if lower than block time. BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 1, 100, 120), 100); // Test that updating existing transaction does not change smart time. BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 1, 200, 220), 100); // New transaction should use clock time if there's no block time. BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 2, 300, 0), 300); // New transaction should use block time if lower than clock time. BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 3, 420, 400), 400); // New transaction should use latest entry time if higher than // min(block time, clock time). BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 4, 500, 390), 400); // If there are future entries, new transaction should use time of the // newest entry that is no more than 300 seconds ahead of the clock time. BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 5, 50, 600), 300); // Reset mock time for other tests. SetMockTime(0); } BOOST_AUTO_TEST_CASE(LoadReceiveRequests) { CTxDestination dest = PKHash(); LOCK(m_wallet.cs_wallet); WalletBatch batch{m_wallet.GetDatabase()}; m_wallet.AddDestData(batch, dest, "misc", "val_misc"); m_wallet.AddDestData(batch, dest, "rr0", "val_rr0"); m_wallet.AddDestData(batch, dest, "rr1", "val_rr1"); auto values = m_wallet.GetDestValues("rr"); BOOST_CHECK_EQUAL(values.size(), 2U); BOOST_CHECK_EQUAL(values[0], "val_rr0"); BOOST_CHECK_EQUAL(values[1], "val_rr1"); } // Test some watch-only LegacyScriptPubKeyMan methods by the procedure of // loading (LoadWatchOnly), checking (HaveWatchOnly), getting (GetWatchPubKey) // and removing (RemoveWatchOnly) a given PubKey, resp. its corresponding P2PK // Script. Results of the the impact on the address -> PubKey map is dependent // on whether the PubKey is a point on the curve static void TestWatchOnlyPubKey(LegacyScriptPubKeyMan *spk_man, const CPubKey &add_pubkey) { CScript p2pk = GetScriptForRawPubKey(add_pubkey); CKeyID add_address = add_pubkey.GetID(); CPubKey found_pubkey; LOCK(spk_man->cs_KeyStore); // all Scripts (i.e. also all PubKeys) are added to the general watch-only // set BOOST_CHECK(!spk_man->HaveWatchOnly(p2pk)); spk_man->LoadWatchOnly(p2pk); BOOST_CHECK(spk_man->HaveWatchOnly(p2pk)); // only PubKeys on the curve shall be added to the watch-only address -> // PubKey map bool is_pubkey_fully_valid = add_pubkey.IsFullyValid(); if (is_pubkey_fully_valid) { BOOST_CHECK(spk_man->GetWatchPubKey(add_address, found_pubkey)); BOOST_CHECK(found_pubkey == add_pubkey); } else { BOOST_CHECK(!spk_man->GetWatchPubKey(add_address, found_pubkey)); // passed key is unchanged BOOST_CHECK(found_pubkey == CPubKey()); } spk_man->RemoveWatchOnly(p2pk); BOOST_CHECK(!spk_man->HaveWatchOnly(p2pk)); if (is_pubkey_fully_valid) { BOOST_CHECK(!spk_man->GetWatchPubKey(add_address, found_pubkey)); // passed key is unchanged BOOST_CHECK(found_pubkey == add_pubkey); } } // Cryptographically invalidate a PubKey whilst keeping length and first byte static void PollutePubKey(CPubKey &pubkey) { std::vector pubkey_raw(pubkey.begin(), pubkey.end()); std::fill(pubkey_raw.begin() + 1, pubkey_raw.end(), 0); pubkey = CPubKey(pubkey_raw); assert(!pubkey.IsFullyValid()); assert(pubkey.IsValid()); } // Test watch-only logic for PubKeys BOOST_AUTO_TEST_CASE(WatchOnlyPubKeys) { CKey key; CPubKey pubkey; LegacyScriptPubKeyMan *spk_man = m_wallet.GetOrCreateLegacyScriptPubKeyMan(); BOOST_CHECK(!spk_man->HaveWatchOnly()); // uncompressed valid PubKey key.MakeNewKey(false); pubkey = key.GetPubKey(); assert(!pubkey.IsCompressed()); TestWatchOnlyPubKey(spk_man, pubkey); // uncompressed cryptographically invalid PubKey PollutePubKey(pubkey); TestWatchOnlyPubKey(spk_man, pubkey); // compressed valid PubKey key.MakeNewKey(true); pubkey = key.GetPubKey(); assert(pubkey.IsCompressed()); TestWatchOnlyPubKey(spk_man, pubkey); // compressed cryptographically invalid PubKey PollutePubKey(pubkey); TestWatchOnlyPubKey(spk_man, pubkey); // invalid empty PubKey pubkey = CPubKey(); TestWatchOnlyPubKey(spk_man, pubkey); } class ListCoinsTestingSetup : public TestChain100Setup { public: ListCoinsTestingSetup() { CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); wallet = std::make_unique(m_chain.get(), "", CreateMockWalletDatabase()); { LOCK2(wallet->cs_wallet, ::cs_main); wallet->SetLastBlockProcessed( ::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash()); } bool firstRun; wallet->LoadWallet(firstRun); AddKey(*wallet, coinbaseKey); WalletRescanReserver reserver(*wallet); reserver.reserve(); CWallet::ScanResult result = wallet->ScanForWalletTransactions( ::ChainActive().Genesis()->GetBlockHash(), 0 /* start_height */, {} /* max_height */, reserver, false /* update */); BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS); BOOST_CHECK_EQUAL(result.last_scanned_block, ::ChainActive().Tip()->GetBlockHash()); BOOST_CHECK_EQUAL(*result.last_scanned_height, ::ChainActive().Height()); BOOST_CHECK(result.last_failed_block.IsNull()); } ~ListCoinsTestingSetup() { wallet.reset(); } CWalletTx &AddTx(CRecipient recipient) { CTransactionRef tx; Amount fee; int changePos = -1; bilingual_str error; CCoinControl dummy; { BOOST_CHECK(wallet->CreateTransaction({recipient}, tx, fee, changePos, error, dummy)); } wallet->CommitTransaction(tx, {}, {}); CMutableTransaction blocktx; { LOCK(wallet->cs_wallet); blocktx = CMutableTransaction(*wallet->mapWallet.at(tx->GetId()).tx); } CreateAndProcessBlock({CMutableTransaction(blocktx)}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); LOCK(wallet->cs_wallet); wallet->SetLastBlockProcessed(wallet->GetLastBlockHeight() + 1, ::ChainActive().Tip()->GetBlockHash()); auto it = wallet->mapWallet.find(tx->GetId()); BOOST_CHECK(it != wallet->mapWallet.end()); CWalletTx::Confirmation confirm( CWalletTx::Status::CONFIRMED, ::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash(), 1); it->second.m_confirm = confirm; return it->second; } std::unique_ptr m_chain = interfaces::MakeChain(m_node, Params()); std::unique_ptr wallet; }; BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup) { std::string coinbaseAddress = coinbaseKey.GetPubKey().GetID().ToString(); // Confirm ListCoins initially returns 1 coin grouped under coinbaseKey // address. std::map> list; { LOCK(wallet->cs_wallet); list = wallet->ListCoins(); } BOOST_CHECK_EQUAL(list.size(), 1U); BOOST_CHECK_EQUAL(boost::get(list.begin()->first).ToString(), coinbaseAddress); BOOST_CHECK_EQUAL(list.begin()->second.size(), 1U); // Check initial balance from one mature coinbase transaction. BOOST_CHECK_EQUAL(50 * COIN, wallet->GetAvailableBalance()); // Add a transaction creating a change address, and confirm ListCoins still // returns the coin associated with the change address underneath the // coinbaseKey pubkey, even though the change address has a different // pubkey. AddTx(CRecipient{GetScriptForRawPubKey({}), 1 * COIN, false /* subtract fee */}); { LOCK(wallet->cs_wallet); list = wallet->ListCoins(); } BOOST_CHECK_EQUAL(list.size(), 1U); BOOST_CHECK_EQUAL(boost::get(list.begin()->first).ToString(), coinbaseAddress); BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U); // Lock both coins. Confirm number of available coins drops to 0. { LOCK(wallet->cs_wallet); std::vector available; wallet->AvailableCoins(available); BOOST_CHECK_EQUAL(available.size(), 2U); } for (const auto &group : list) { for (const auto &coin : group.second) { LOCK(wallet->cs_wallet); wallet->LockCoin(COutPoint(coin.tx->GetId(), coin.i)); } } { LOCK(wallet->cs_wallet); std::vector available; wallet->AvailableCoins(available); BOOST_CHECK_EQUAL(available.size(), 0U); } // Confirm ListCoins still returns same result as before, despite coins // being locked. { LOCK(wallet->cs_wallet); list = wallet->ListCoins(); } BOOST_CHECK_EQUAL(list.size(), 1U); BOOST_CHECK_EQUAL(boost::get(list.begin()->first).ToString(), coinbaseAddress); BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U); } BOOST_FIXTURE_TEST_CASE(wallet_disableprivkeys, TestChain100Setup) { NodeContext node; auto chain = interfaces::MakeChain(node, Params()); std::shared_ptr wallet = std::make_shared(chain.get(), "", CreateDummyWalletDatabase()); wallet->SetupLegacyScriptPubKeyMan(); wallet->SetMinVersion(FEATURE_LATEST); wallet->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS); BOOST_CHECK(!wallet->TopUpKeyPool(1000)); CTxDestination dest; std::string error; BOOST_CHECK( !wallet->GetNewDestination(OutputType::LEGACY, "", dest, error)); } // Explicit calculation which is used to test the wallet constant static size_t CalculateP2PKHInputSize(bool use_max_sig) { // Generate ephemeral valid pubkey CKey key; key.MakeNewKey(true); CPubKey pubkey = key.GetPubKey(); // Generate pubkey hash PKHash key_hash(pubkey); // Create script to enter into keystore. Key hash can't be 0... CScript script = GetScriptForDestination(key_hash); // Add script to key store and key to watchonly FillableSigningProvider keystore; keystore.AddKeyPubKey(key, pubkey); // Fill in dummy signatures for fee calculation. SignatureData sig_data; if (!ProduceSignature(keystore, use_max_sig ? DUMMY_MAXIMUM_SIGNATURE_CREATOR : DUMMY_SIGNATURE_CREATOR, script, sig_data)) { // We're hand-feeding it correct arguments; shouldn't happen assert(false); } CTxIn tx_in; UpdateInput(tx_in, sig_data); return (size_t)GetVirtualTransactionInputSize(tx_in); } BOOST_FIXTURE_TEST_CASE(dummy_input_size_test, TestChain100Setup) { BOOST_CHECK(CalculateP2PKHInputSize(false) <= DUMMY_P2PKH_INPUT_SIZE); BOOST_CHECK_EQUAL(CalculateP2PKHInputSize(true), DUMMY_P2PKH_INPUT_SIZE); } bool malformed_descriptor(std::ios_base::failure e) { std::string s(e.what()); return s.find("Missing checksum") != std::string::npos; } BOOST_FIXTURE_TEST_CASE(wallet_descriptor_test, BasicTestingSetup) { std::vector malformed_record; CVectorWriter vw(0, 0, malformed_record, 0); vw << std::string("notadescriptor"); vw << (uint64_t)0; vw << (int32_t)0; vw << (int32_t)0; vw << (int32_t)1; VectorReader vr(0, 0, malformed_record, 0); WalletDescriptor w_desc; BOOST_CHECK_EXCEPTION(vr >> w_desc, std::ios_base::failure, malformed_descriptor); } //! Test CreateWalletFromFile function and its behavior handling potential race //! conditions if it's called the same time an incoming transaction shows up in //! the mempool or a new block. //! //! It isn't possible to verify there aren't race condition in every case, so //! this test just checks two specific cases and ensures that timing of //! notifications in these cases doesn't prevent the wallet from detecting //! transactions. //! //! In the first case, block and mempool transactions are created before the //! wallet is loaded, but notifications about these transactions are delayed //! until after it is loaded. The notifications are superfluous in this case, so //! the test verifies the transactions are detected before they arrive. //! //! In the second case, block and mempool transactions are created after the //! wallet rescan and notifications are immediately synced, to verify the wallet //! must already have a handler in place for them, and there's no gap after //! rescanning where new transactions in new blocks could be lost. BOOST_FIXTURE_TEST_CASE(CreateWalletFromFile, TestChain100Setup) { // Create new wallet with known key and unload it. auto chain = interfaces::MakeChain(m_node, Params()); auto wallet = TestLoadWallet(*chain); CKey key; key.MakeNewKey(true); AddKey(*wallet, key); TestUnloadWallet(std::move(wallet)); // Add log hook to detect AddToWallet events from rescans, blockConnected, // and transactionAddedToMempool notifications int addtx_count = 0; DebugLogHelper addtx_counter("[default wallet] AddToWallet", [&](const std::string *s) { if (s) { ++addtx_count; } return false; }); bool rescan_completed = false; DebugLogHelper rescan_check("[default wallet] Rescan completed", [&](const std::string *s) { if (s) { rescan_completed = true; } return false; }); // Block the queue to prevent the wallet receiving blockConnected and // transactionAddedToMempool notifications, and create block and mempool // transactions paying to the wallet std::promise promise; CallFunctionInValidationInterfaceQueue( [&promise] { promise.get_future().wait(); }); std::string error; m_coinbase_txns.push_back( CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())) .vtx[0]); auto block_tx = TestSimpleSpend(*m_coinbase_txns[0], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey())); m_coinbase_txns.push_back( CreateAndProcessBlock({block_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())) .vtx[0]); auto mempool_tx = TestSimpleSpend(*m_coinbase_txns[1], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey())); BOOST_CHECK( chain->broadcastTransaction(GetConfig(), MakeTransactionRef(mempool_tx), DEFAULT_TRANSACTION_MAXFEE, false, error)); // Reload wallet and make sure new transactions are detected despite events // being blocked wallet = TestLoadWallet(*chain); BOOST_CHECK(rescan_completed); BOOST_CHECK_EQUAL(addtx_count, 2); { LOCK(wallet->cs_wallet); BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_tx.GetId()), 1U); BOOST_CHECK_EQUAL(wallet->mapWallet.count(mempool_tx.GetId()), 1U); } // Unblock notification queue and make sure stale blockConnected and // transactionAddedToMempool events are processed promise.set_value(); SyncWithValidationInterfaceQueue(); BOOST_CHECK_EQUAL(addtx_count, 4); TestUnloadWallet(std::move(wallet)); // Load wallet again, this time creating new block and mempool transactions // paying to the wallet as the wallet finishes loading and syncing the // queue so the events have to be handled immediately. Releasing the wallet // lock during the sync is a little artificial but is needed to avoid a // deadlock during the sync and simulates a new block notification happening // as soon as possible. addtx_count = 0; auto handler = HandleLoadWallet( [&](std::unique_ptr wallet_param) EXCLUSIVE_LOCKS_REQUIRED(wallet_param->wallet()->cs_wallet) { BOOST_CHECK(rescan_completed); m_coinbase_txns.push_back( CreateAndProcessBlock( {}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())) .vtx[0]); block_tx = TestSimpleSpend(*m_coinbase_txns[2], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey())); m_coinbase_txns.push_back( CreateAndProcessBlock( {block_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())) .vtx[0]); mempool_tx = TestSimpleSpend(*m_coinbase_txns[3], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey())); BOOST_CHECK(chain->broadcastTransaction( GetConfig(), MakeTransactionRef(mempool_tx), DEFAULT_TRANSACTION_MAXFEE, false, error)); LEAVE_CRITICAL_SECTION(wallet_param->wallet()->cs_wallet); SyncWithValidationInterfaceQueue(); ENTER_CRITICAL_SECTION(wallet_param->wallet()->cs_wallet); }); wallet = TestLoadWallet(*chain); BOOST_CHECK_EQUAL(addtx_count, 4); { LOCK(wallet->cs_wallet); BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_tx.GetId()), 1U); BOOST_CHECK_EQUAL(wallet->mapWallet.count(mempool_tx.GetId()), 1U); } TestUnloadWallet(std::move(wallet)); } BOOST_FIXTURE_TEST_CASE(ZapSelectTx, TestChain100Setup) { auto chain = interfaces::MakeChain(m_node, Params()); auto wallet = TestLoadWallet(*chain); CKey key; key.MakeNewKey(true); AddKey(*wallet, key); std::string error; m_coinbase_txns.push_back( CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())) .vtx[0]); auto block_tx = TestSimpleSpend(*m_coinbase_txns[0], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey())); CreateAndProcessBlock({block_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); SyncWithValidationInterfaceQueue(); { auto block_id = block_tx.GetId(); auto prev_id = m_coinbase_txns[0]->GetId(); LOCK(wallet->cs_wallet); BOOST_CHECK(wallet->HasWalletSpend(prev_id)); BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_id), 1u); std::vector vIdIn{block_id}, vIdOut; BOOST_CHECK_EQUAL(wallet->ZapSelectTx(vIdIn, vIdOut), DBErrors::LOAD_OK); BOOST_CHECK(!wallet->HasWalletSpend(prev_id)); BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_id), 0u); } TestUnloadWallet(std::move(wallet)); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 4f1f8ad07..dc6c2d993 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1,5090 +1,5097 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include