From 269498453eaafcbe4736b16bc1cbd9e3d0599166 Mon Sep 17 00:00:00 2001 From: Sami Ahmed Date: Sun, 26 Apr 2026 17:36:33 -0700 Subject: [PATCH] Track src/txdb-factory.cpp This file has been built into the binary since the M1.3 chain-DB backend split (referenced from src/CMakeLists.txt) but was never committed. A fresh clone wouldn't build without it. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/txdb-factory.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/txdb-factory.cpp diff --git a/src/txdb-factory.cpp b/src/txdb-factory.cpp new file mode 100644 index 0000000..60cc5a7 --- /dev/null +++ b/src/txdb-factory.cpp @@ -0,0 +1,64 @@ +// Copyright (c) 2026 The Triangles developers. +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "txdb.h" +#include "util.h" + +#include +#include + +namespace { + +// Pick the backend once per process. -chaindb is a startup flag; switching at +// runtime would require reopening every CTxDB instance, which the codebase +// doesn't currently support. We cache the resolved choice so subsequent +// MakeChainDB calls don't re-parse the argument. +enum class ChainDbKind { LevelDB, RocksDB }; + +ChainDbKind ResolveChainDbKind() +{ + static const ChainDbKind kKind = []() { + std::string s = GetArg("-chaindb", std::string("leveldb")); + for (auto& c : s) c = std::tolower(static_cast(c)); + + if (s == "leveldb") + return ChainDbKind::LevelDB; + + if (s == "rocksdb") { +#ifdef BUILD_ROCKSDB + return ChainDbKind::RocksDB; +#else + throw std::runtime_error( + "-chaindb=rocksdb requested but this binary was built without " + "BUILD_ROCKSDB. Rebuild with -DBUILD_ROCKSDB=ON, or use " + "-chaindb=leveldb."); +#endif + } + + throw std::runtime_error( + "-chaindb=" + s + " is not a recognized backend. " + "Valid values: leveldb" +#ifdef BUILD_ROCKSDB + ", rocksdb" +#endif + "."); + }(); + return kKind; +} + +} // anonymous namespace + +std::unique_ptr MakeChainDB(const char* pszMode) +{ + switch (ResolveChainDbKind()) { + case ChainDbKind::LevelDB: + return std::unique_ptr(new CTxDB(pszMode)); +#ifdef BUILD_ROCKSDB + case ChainDbKind::RocksDB: + return std::unique_ptr(new CRocksTxDB(pszMode)); +#endif + } + // Unreachable — ResolveChainDbKind throws on bad input. + return nullptr; +}