blockfilterindex.cpp raw

   1  // Copyright (c) 2018-2022 The Limenka developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #include <map>
   6  
   7  #include <clientversion.h>
   8  #include <common/args.h>
   9  #include <dbwrapper.h>
  10  #include <hash.h>
  11  #include <index/blockfilterindex.h>
  12  #include <logging.h>
  13  #include <node/blockstorage.h>
  14  #include <undo.h>
  15  #include <util/fs_helpers.h>
  16  #include <util/syserror.h>
  17  #include <validation.h>
  18  
  19  /* The index database stores three items for each block: the disk location of the encoded filter,
  20   * its dSHA256 hash, and the header. Those belonging to blocks on the active chain are indexed by
  21   * height, and those belonging to blocks that have been reorganized out of the active chain are
  22   * indexed by block hash. This ensures that filter data for any block that becomes part of the
  23   * active chain can always be retrieved, alleviating timing concerns.
  24   *
  25   * The filters themselves are stored in flat files and referenced by the LevelDB entries. This
  26   * minimizes the amount of data written to LevelDB and keeps the database values constant size. The
  27   * disk location of the next block filter to be written (represented as a FlatFilePos) is stored
  28   * under the DB_FILTER_POS key.
  29   *
  30   * Keys for the height index have the type [DB_BLOCK_HEIGHT, uint32 (BE)]. The height is represented
  31   * as big-endian so that sequential reads of filters by height are fast.
  32   * Keys for the hash index have the type [DB_BLOCK_HASH, uint256].
  33   */
  34  constexpr uint8_t DB_BLOCK_HASH{'s'};
  35  constexpr uint8_t DB_BLOCK_HEIGHT{'t'};
  36  constexpr uint8_t DB_FILTER_POS{'P'};
  37  
  38  constexpr unsigned int MAX_FLTR_FILE_SIZE = 0x1000000; // 16 MiB
  39  /** The pre-allocation chunk size for fltr?????.dat files */
  40  constexpr unsigned int FLTR_FILE_CHUNK_SIZE = 0x100000; // 1 MiB
  41  /** Maximum size of the cfheaders cache
  42   *  We have a limit to prevent a bug in filling this cache
  43   *  potentially turning into an OOM. At 2000 entries, this cache
  44   *  is big enough for a 2,000,000 length block chain, which
  45   *  we should be enough until ~2047. */
  46  constexpr size_t CF_HEADERS_CACHE_MAX_SZ{2000};
  47  
  48  namespace {
  49  
  50  struct DBVal {
  51      uint256 hash;
  52      uint256 header;
  53      FlatFilePos pos;
  54  
  55      SERIALIZE_METHODS(DBVal, obj) { READWRITE(obj.hash, obj.header, obj.pos); }
  56  };
  57  
  58  struct DBHeightKey {
  59      int height;
  60  
  61      explicit DBHeightKey(int height_in) : height(height_in) {}
  62  
  63      template<typename Stream>
  64      void Serialize(Stream& s) const
  65      {
  66          ser_writedata8(s, DB_BLOCK_HEIGHT);
  67          ser_writedata32be(s, height);
  68      }
  69  
  70      template<typename Stream>
  71      void Unserialize(Stream& s)
  72      {
  73          const uint8_t prefix{ser_readdata8(s)};
  74          if (prefix != DB_BLOCK_HEIGHT) {
  75              throw std::ios_base::failure("Invalid format for block filter index DB height key");
  76          }
  77          height = ser_readdata32be(s);
  78      }
  79  };
  80  
  81  struct DBHashKey {
  82      uint256 hash;
  83  
  84      explicit DBHashKey(const uint256& hash_in) : hash(hash_in) {}
  85  
  86      SERIALIZE_METHODS(DBHashKey, obj) {
  87          uint8_t prefix{DB_BLOCK_HASH};
  88          READWRITE(prefix);
  89          if (prefix != DB_BLOCK_HASH) {
  90              throw std::ios_base::failure("Invalid format for block filter index DB hash key");
  91          }
  92  
  93          READWRITE(obj.hash);
  94      }
  95  };
  96  
  97  }; // namespace
  98  
  99  static std::map<BlockFilterType, BlockFilterIndex> g_filter_indexes;
 100  
 101  BlockFilterIndex::BlockFilterIndex(std::unique_ptr<interfaces::Chain> chain, BlockFilterType filter_type,
 102                                     size_t n_cache_size, bool f_memory, bool f_wipe)
 103      : BaseIndex(std::move(chain), BlockFilterTypeName(filter_type) + " block filter index")
 104      , m_filter_type(filter_type)
 105  {
 106      const std::string& filter_name = BlockFilterTypeName(filter_type);
 107      if (filter_name.empty()) throw std::invalid_argument("unknown filter_type");
 108  
 109      fs::path path = gArgs.GetDataDirNet() / "indexes" / "blockfilter" / fs::u8path(filter_name);
 110      fs::create_directories(path);
 111  
 112      m_db = std::make_unique<BaseIndex::DB>(path / "db", n_cache_size, f_memory, f_wipe);
 113      m_filter_fileseq = std::make_unique<FlatFileSeq>(std::move(path), "fltr", FLTR_FILE_CHUNK_SIZE);
 114  }
 115  
 116  bilingual_str BlockFilterIndex::GetDisableAction() const
 117  {
 118      return strprintf(_("remove \"%s\" from -blockfilterindex"), BlockFilterTypeName(m_filter_type));
 119  }
 120  
 121  bool BlockFilterIndex::CustomInit(const std::optional<interfaces::BlockRef>& block)
 122  {
 123      if (!m_db->Read(DB_FILTER_POS, m_next_filter_pos)) {
 124          // Check that the cause of the read failure is that the key does not exist. Any other errors
 125          // indicate database corruption or a disk failure, and starting the index would cause
 126          // further corruption.
 127          if (m_db->Exists(DB_FILTER_POS)) {
 128              LogError("%s: Cannot read current %s state; index may be corrupted\n",
 129                           __func__, GetName());
 130              return false;
 131          }
 132  
 133          // If the DB_FILTER_POS is not set, then initialize to the first location.
 134          m_next_filter_pos.nFile = 0;
 135          m_next_filter_pos.nPos = 0;
 136      }
 137  
 138      if (block) {
 139          auto op_last_header = ReadFilterHeader(block->height, block->hash);
 140          if (!op_last_header) {
 141              LogError("Cannot read last block filter header; index may be corrupted\n");
 142              return false;
 143          }
 144          m_last_header = *op_last_header;
 145      }
 146  
 147      return true;
 148  }
 149  
 150  bool BlockFilterIndex::CustomCommit(CDBBatch& batch)
 151  {
 152      const FlatFilePos& pos = m_next_filter_pos;
 153  
 154      // Flush current filter file to disk.
 155      AutoFile file{m_filter_fileseq->Open(pos)};
 156      if (file.IsNull()) {
 157          LogError("%s: Failed to open filter file %d\n", __func__, pos.nFile);
 158          return false;
 159      }
 160      if (!file.Commit()) {
 161          LogError("%s: Failed to commit filter file %d\n", __func__, pos.nFile);
 162          (void)file.fclose();
 163          return false;
 164      }
 165      if (file.fclose() != 0) {
 166          LogError("Failed to close filter file %d after commit: %s", pos.nFile, SysErrorString(errno));
 167          return false;
 168      }
 169  
 170      batch.Write(DB_FILTER_POS, pos);
 171      return true;
 172  }
 173  
 174  bool BlockFilterIndex::ReadFilterFromDisk(const FlatFilePos& pos, const uint256& hash, BlockFilter& filter) const
 175  {
 176      AutoFile filein{m_filter_fileseq->Open(pos, true)};
 177      if (filein.IsNull()) {
 178          return false;
 179      }
 180  
 181      // Check that the hash of the encoded_filter matches the one stored in the db.
 182      uint256 block_hash;
 183      std::vector<uint8_t> encoded_filter;
 184      try {
 185          filein >> block_hash >> encoded_filter;
 186          if (Hash(encoded_filter) != hash) {
 187              LogError("Checksum mismatch in filter decode.\n");
 188              return false;
 189          }
 190          filter = BlockFilter(GetFilterType(), block_hash, std::move(encoded_filter), /*skip_decode_check=*/true);
 191      }
 192      catch (const std::exception& e) {
 193          LogError("%s: Failed to deserialize block filter from disk: %s\n", __func__, e.what());
 194          return false;
 195      }
 196  
 197      return true;
 198  }
 199  
 200  size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter& filter)
 201  {
 202      assert(filter.GetFilterType() == GetFilterType());
 203  
 204      size_t data_size =
 205          GetSerializeSize(filter.GetBlockHash()) +
 206          GetSerializeSize(filter.GetEncodedFilter());
 207  
 208      // If writing the filter would overflow the file, flush and move to the next one.
 209      if (pos.nPos + data_size > MAX_FLTR_FILE_SIZE) {
 210          AutoFile last_file{m_filter_fileseq->Open(pos)};
 211          if (last_file.IsNull()) {
 212              LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
 213              return 0;
 214          }
 215          if (!last_file.Truncate(pos.nPos)) {
 216              LogPrintf("%s: Failed to truncate filter file %d\n", __func__, pos.nFile);
 217              return 0;
 218          }
 219          if (!last_file.Commit()) {
 220              LogPrintf("%s: Failed to commit filter file %d\n", __func__, pos.nFile);
 221              (void)last_file.fclose();
 222              return 0;
 223          }
 224          if (last_file.fclose() != 0) {
 225              LogError("Failed to close filter file %d after commit: %s", pos.nFile, SysErrorString(errno));
 226              return 0;
 227          }
 228  
 229          pos.nFile++;
 230          pos.nPos = 0;
 231      }
 232  
 233      // Pre-allocate sufficient space for filter data.
 234      bool out_of_space;
 235      m_filter_fileseq->Allocate(pos, data_size, out_of_space);
 236      if (out_of_space) {
 237          LogPrintf("%s: out of disk space\n", __func__);
 238          return 0;
 239      }
 240  
 241      AutoFile fileout{m_filter_fileseq->Open(pos)};
 242      if (fileout.IsNull()) {
 243          LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
 244          return 0;
 245      }
 246  
 247      fileout << filter.GetBlockHash() << filter.GetEncodedFilter();
 248  
 249      if (fileout.fclose() != 0) {
 250          LogError("Failed to close filter file %d: %s", pos.nFile, SysErrorString(errno));
 251          return 0;
 252      }
 253  
 254      return data_size;
 255  }
 256  
 257  std::optional<uint256> BlockFilterIndex::ReadFilterHeader(int height, const uint256& expected_block_hash)
 258  {
 259      std::pair<uint256, DBVal> read_out;
 260      if (!m_db->Read(DBHeightKey(height), read_out)) {
 261          return std::nullopt;
 262      }
 263  
 264      if (read_out.first != expected_block_hash) {
 265          LogError("%s: previous block header belongs to unexpected block %s; expected %s\n",
 266                           __func__, read_out.first.ToString(), expected_block_hash.ToString());
 267          return std::nullopt;
 268      }
 269  
 270      return read_out.second.header;
 271  }
 272  
 273  bool BlockFilterIndex::CustomAppend(const interfaces::BlockInfo& block)
 274  {
 275      CBlockUndo block_undo;
 276  
 277      if (block.height > 0) {
 278          // pindex variable gives indexing code access to node internals. It
 279          // will be removed in upcoming commit
 280          const CBlockIndex* pindex = WITH_LOCK(cs_main, return m_chainstate->m_blockman.LookupBlockIndex(block.hash));
 281          if (!m_chainstate->m_blockman.ReadBlockUndo(block_undo, *pindex)) {
 282              return false;
 283          }
 284      }
 285  
 286      BlockFilter filter(m_filter_type, *Assert(block.data), block_undo);
 287  
 288      const uint256& header = filter.ComputeHeader(m_last_header);
 289      bool res = Write(filter, block.height, header);
 290      if (res) m_last_header = header; // update last header
 291      return res;
 292  }
 293  
 294  bool BlockFilterIndex::Write(const BlockFilter& filter, uint32_t block_height, const uint256& filter_header)
 295  {
 296      size_t bytes_written = WriteFilterToDisk(m_next_filter_pos, filter);
 297      if (bytes_written == 0) return false;
 298  
 299      std::pair<uint256, DBVal> value;
 300      value.first = filter.GetBlockHash();
 301      value.second.hash = filter.GetHash();
 302      value.second.header = filter_header;
 303      value.second.pos = m_next_filter_pos;
 304  
 305      if (!m_db->Write(DBHeightKey(block_height), value)) {
 306          return false;
 307      }
 308  
 309      m_next_filter_pos.nPos += bytes_written;
 310      return true;
 311  }
 312  
 313  [[nodiscard]] static bool CopyHeightIndexToHashIndex(CDBIterator& db_it, CDBBatch& batch,
 314                                         const std::string& index_name,
 315                                         int start_height, int stop_height)
 316  {
 317      DBHeightKey key(start_height);
 318      db_it.Seek(key);
 319  
 320      for (int height = start_height; height <= stop_height; ++height) {
 321          if (!db_it.GetKey(key) || key.height != height) {
 322              LogError("%s: unexpected key in %s: expected (%c, %d)\n",
 323                           __func__, index_name, DB_BLOCK_HEIGHT, height);
 324              return false;
 325          }
 326  
 327          std::pair<uint256, DBVal> value;
 328          if (!db_it.GetValue(value)) {
 329              LogError("%s: unable to read value in %s at key (%c, %d)\n",
 330                           __func__, index_name, DB_BLOCK_HEIGHT, height);
 331              return false;
 332          }
 333  
 334          batch.Write(DBHashKey(value.first), std::move(value.second));
 335  
 336          db_it.Next();
 337      }
 338      return true;
 339  }
 340  
 341  bool BlockFilterIndex::CustomRewind(const interfaces::BlockRef& current_tip, const interfaces::BlockRef& new_tip)
 342  {
 343      CDBBatch batch(*m_db);
 344      std::unique_ptr<CDBIterator> db_it(m_db->NewIterator());
 345  
 346      // During a reorg, we need to copy all filters for blocks that are getting disconnected from the
 347      // height index to the hash index so we can still find them when the height index entries are
 348      // overwritten.
 349      if (!CopyHeightIndexToHashIndex(*db_it, batch, m_name, new_tip.height, current_tip.height)) {
 350          return false;
 351      }
 352  
 353      // The latest filter position gets written in Commit by the call to the BaseIndex::Rewind.
 354      // But since this creates new references to the filter, the position should get updated here
 355      // atomically as well in case Commit fails.
 356      batch.Write(DB_FILTER_POS, m_next_filter_pos);
 357      if (!m_db->WriteBatch(batch)) return false;
 358  
 359      // Update cached header
 360      m_last_header = *Assert(ReadFilterHeader(new_tip.height, new_tip.hash));
 361      return true;
 362  }
 363  
 364  static bool LookupOne(const CDBWrapper& db, const CBlockIndex* block_index, DBVal& result)
 365  {
 366      // First check if the result is stored under the height index and the value there matches the
 367      // block hash. This should be the case if the block is on the active chain.
 368      std::pair<uint256, DBVal> read_out;
 369      if (!db.Read(DBHeightKey(block_index->nHeight), read_out)) {
 370          return false;
 371      }
 372      if (read_out.first == block_index->GetBlockHash()) {
 373          result = std::move(read_out.second);
 374          return true;
 375      }
 376  
 377      // If value at the height index corresponds to an different block, the result will be stored in
 378      // the hash index.
 379      return db.Read(DBHashKey(block_index->GetBlockHash()), result);
 380  }
 381  
 382  static bool LookupRange(CDBWrapper& db, const std::string& index_name, int start_height,
 383                          const CBlockIndex* stop_index, std::vector<DBVal>& results)
 384  {
 385      if (start_height < 0) {
 386          LogError("%s: start height (%d) is negative\n", __func__, start_height);
 387          return false;
 388      }
 389      if (start_height > stop_index->nHeight) {
 390          LogError("%s: start height (%d) is greater than stop height (%d)\n",
 391                       __func__, start_height, stop_index->nHeight);
 392          return false;
 393      }
 394  
 395      size_t results_size = static_cast<size_t>(stop_index->nHeight - start_height + 1);
 396      std::vector<std::pair<uint256, DBVal>> values(results_size);
 397  
 398      DBHeightKey key(start_height);
 399      std::unique_ptr<CDBIterator> db_it(db.NewIterator());
 400      db_it->Seek(DBHeightKey(start_height));
 401      for (int height = start_height; height <= stop_index->nHeight; ++height) {
 402          if (!db_it->Valid() || !db_it->GetKey(key) || key.height != height) {
 403              return false;
 404          }
 405  
 406          size_t i = static_cast<size_t>(height - start_height);
 407          if (!db_it->GetValue(values[i])) {
 408              LogError("%s: unable to read value in %s at key (%c, %d)\n",
 409                           __func__, index_name, DB_BLOCK_HEIGHT, height);
 410              return false;
 411          }
 412  
 413          db_it->Next();
 414      }
 415  
 416      results.resize(results_size);
 417  
 418      // Iterate backwards through block indexes collecting results in order to access the block hash
 419      // of each entry in case we need to look it up in the hash index.
 420      for (const CBlockIndex* block_index = stop_index;
 421           block_index && block_index->nHeight >= start_height;
 422           block_index = block_index->pprev) {
 423          uint256 block_hash = block_index->GetBlockHash();
 424  
 425          size_t i = static_cast<size_t>(block_index->nHeight - start_height);
 426          if (block_hash == values[i].first) {
 427              results[i] = std::move(values[i].second);
 428              continue;
 429          }
 430  
 431          if (!db.Read(DBHashKey(block_hash), results[i])) {
 432              LogError("%s: unable to read value in %s at key (%c, %s)\n",
 433                           __func__, index_name, DB_BLOCK_HASH, block_hash.ToString());
 434              return false;
 435          }
 436      }
 437  
 438      return true;
 439  }
 440  
 441  bool BlockFilterIndex::LookupFilter(const CBlockIndex* block_index, BlockFilter& filter_out) const
 442  {
 443      DBVal entry;
 444      if (!LookupOne(*m_db, block_index, entry)) {
 445          return false;
 446      }
 447  
 448      return ReadFilterFromDisk(entry.pos, entry.hash, filter_out);
 449  }
 450  
 451  bool BlockFilterIndex::LookupFilterHeader(const CBlockIndex* block_index, uint256& header_out)
 452  {
 453      LOCK(m_cs_headers_cache);
 454  
 455      bool is_checkpoint{block_index->nHeight % CFCHECKPT_INTERVAL == 0};
 456  
 457      if (is_checkpoint) {
 458          // Try to find the block in the headers cache if this is a checkpoint height.
 459          auto header = m_headers_cache.find(block_index->GetBlockHash());
 460          if (header != m_headers_cache.end()) {
 461              header_out = header->second;
 462              return true;
 463          }
 464      }
 465  
 466      DBVal entry;
 467      if (!LookupOne(*m_db, block_index, entry)) {
 468          return false;
 469      }
 470  
 471      if (is_checkpoint &&
 472          m_headers_cache.size() < CF_HEADERS_CACHE_MAX_SZ) {
 473          // Add to the headers cache if this is a checkpoint height.
 474          m_headers_cache.emplace(block_index->GetBlockHash(), entry.header);
 475      }
 476  
 477      header_out = entry.header;
 478      return true;
 479  }
 480  
 481  bool BlockFilterIndex::LookupFilterRange(int start_height, const CBlockIndex* stop_index,
 482                                           std::vector<BlockFilter>& filters_out) const
 483  {
 484      std::vector<DBVal> entries;
 485      if (!LookupRange(*m_db, m_name, start_height, stop_index, entries)) {
 486          return false;
 487      }
 488  
 489      filters_out.resize(entries.size());
 490      auto filter_pos_it = filters_out.begin();
 491      for (const auto& entry : entries) {
 492          if (!ReadFilterFromDisk(entry.pos, entry.hash, *filter_pos_it)) {
 493              return false;
 494          }
 495          ++filter_pos_it;
 496      }
 497  
 498      return true;
 499  }
 500  
 501  bool BlockFilterIndex::LookupFilterHashRange(int start_height, const CBlockIndex* stop_index,
 502                                               std::vector<uint256>& hashes_out) const
 503  
 504  {
 505      std::vector<DBVal> entries;
 506      if (!LookupRange(*m_db, m_name, start_height, stop_index, entries)) {
 507          return false;
 508      }
 509  
 510      hashes_out.clear();
 511      hashes_out.reserve(entries.size());
 512      for (const auto& entry : entries) {
 513          hashes_out.push_back(entry.hash);
 514      }
 515      return true;
 516  }
 517  
 518  BlockFilterIndex* GetBlockFilterIndex(BlockFilterType filter_type)
 519  {
 520      auto it = g_filter_indexes.find(filter_type);
 521      return it != g_filter_indexes.end() ? &it->second : nullptr;
 522  }
 523  
 524  void ForEachBlockFilterIndex(std::function<void (BlockFilterIndex&)> fn)
 525  {
 526      for (auto& entry : g_filter_indexes) fn(entry.second);
 527  }
 528  
 529  bool InitBlockFilterIndex(std::function<std::unique_ptr<interfaces::Chain>()> make_chain, BlockFilterType filter_type,
 530                            size_t n_cache_size, bool f_memory, bool f_wipe)
 531  {
 532      auto result = g_filter_indexes.emplace(std::piecewise_construct,
 533                                             std::forward_as_tuple(filter_type),
 534                                             std::forward_as_tuple(make_chain(), filter_type,
 535                                                                   n_cache_size, f_memory, f_wipe));
 536      return result.second;
 537  }
 538  
 539  bool DestroyBlockFilterIndex(BlockFilterType filter_type)
 540  {
 541      return g_filter_indexes.erase(filter_type);
 542  }
 543  
 544  void DestroyAllBlockFilterIndexes()
 545  {
 546      g_filter_indexes.clear();
 547  }
 548