diff --git a/src/lez_indexer_module_impl.cpp b/src/lez_indexer_module_impl.cpp index 919ed52..67539c5 100644 --- a/src/lez_indexer_module_impl.cpp +++ b/src/lez_indexer_module_impl.cpp @@ -5,6 +5,7 @@ #include #include +#include #include using namespace marshalling; @@ -83,6 +84,54 @@ int64_t LezIndexerModuleImpl::stop_indexer() { return 0; } +int64_t LezIndexerModuleImpl::reset_storage(const std::string& config_path) { + // Stop first so RocksDB is closed before its files are deleted + const int64_t stop_code = stop_indexer(); + if (stop_code != 0) { + error("reset_storage", "could not stop indexer before wiping storage"); + return stop_code; + } + + const std::string& storage = instancePersistencePath(); + if (storage.empty()) { + error("reset_storage", "no instance persistence path; refusing to wipe (nothing to target)"); + return -1; + } + + // FFI stores RocksDB at /rocksdb-{channel_id} + std::string channel_id; + try { + std::ifstream in(config_path); + if (!in) { + error("reset_storage", "could not open config " + config_path); + return -1; + } + channel_id = nlohmann::json::parse(in).at("channel_id").get(); + } catch (const std::exception& e) { + error("reset_storage", std::string("could not read channel_id from config: ") + e.what()); + return -1; + } + if (channel_id.empty()) { + error("reset_storage", "config has an empty channel_id"); + return -1; + } + + const std::filesystem::path store = std::filesystem::path(storage) / ("rocksdb-" + channel_id); + std::error_code ec; + if (!std::filesystem::exists(store, ec)) { + info("reset_storage", "no store at " + store.string() + "; nothing to wipe"); + return 0; + } + std::filesystem::remove_all(store, ec); + if (ec) { + error("reset_storage", "failed to remove " + store.string() + ": " + ec.message()); + return -1; + } + + info("reset_storage", "wiped rocksdb store " + store.string()); + return 0; +} + // === Indexer Queries === // // Each method calls the matching query_* FFI function on the handle we hold, diff --git a/src/lez_indexer_module_impl.h b/src/lez_indexer_module_impl.h index e2e2206..d0d2fa6 100644 --- a/src/lez_indexer_module_impl.h +++ b/src/lez_indexer_module_impl.h @@ -41,6 +41,14 @@ public: /// own). Returns 0 on success, else the FFI OperationStatus code. int64_t stop_indexer(); + /// Stop the indexer (if running) and delete the RocksDB store for the config's + /// channel — `/rocksdb-`, the exact path the + /// FFI uses — so the next start_indexer re-indexes from scratch. The recovery + /// path when the store is stale against a different/reset chain. Pass the same + /// `config_path` given to start_indexer; does NOT restart. Returns 0 on success, + /// else non-zero. int64_t for the same codegen reason as start_indexer. + int64_t reset_storage(const std::string& config_path); + /// Account by id, accepting Base58 (canonical) or 32-byte hex. The returned /// JSON omits the id; callers inject the queried id themselves. std::string getAccount(const std::string& account_id);