Skip to content

Commit

Permalink
refactor: rename BlockKey to BlockRef
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Sep 17, 2024
1 parent 9f1aa88 commit 89a8f74
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/index/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ bool BaseIndex::Init()

// Child init
const CBlockIndex* start_block = m_best_block_index.load();
if (!CustomInit(start_block ? std::make_optional(interfaces::BlockKey{start_block->GetBlockHash(), start_block->nHeight}) : std::nullopt)) {
if (!CustomInit(start_block ? std::make_optional(interfaces::BlockRef{start_block->GetBlockHash(), start_block->nHeight}) : std::nullopt)) {
return false;
}

Expand Down
5 changes: 3 additions & 2 deletions src/index/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <dbwrapper.h>
#include <interfaces/chain.h>
#include <interfaces/types.h>
#include <util/string.h>
#include <util/threadinterrupt.h>
#include <validationinterface.h>
Expand Down Expand Up @@ -107,7 +108,7 @@ class BaseIndex : public CValidationInterface
void ChainStateFlushed(ChainstateRole role, const CBlockLocator& locator) override;

/// Initialize internal state from the database and block index.
[[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockKey>& block) { return true; }
[[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockRef>& block) { return true; }

/// Write update index entries for a newly connected block.
[[nodiscard]] virtual bool CustomAppend(const interfaces::BlockInfo& block) { return true; }
Expand All @@ -118,7 +119,7 @@ class BaseIndex : public CValidationInterface

/// Rewind index to an earlier chain tip during a chain reorg. The tip must
/// be an ancestor of the current best block.
[[nodiscard]] virtual bool CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip) { return true; }
[[nodiscard]] virtual bool CustomRewind(const interfaces::BlockRef& current_tip, const interfaces::BlockRef& new_tip) { return true; }

virtual DB& GetDB() const = 0;

Expand Down
4 changes: 2 additions & 2 deletions src/index/blockfilterindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ BlockFilterIndex::BlockFilterIndex(std::unique_ptr<interfaces::Chain> chain, Blo
m_filter_fileseq = std::make_unique<FlatFileSeq>(std::move(path), "fltr", FLTR_FILE_CHUNK_SIZE);
}

bool BlockFilterIndex::CustomInit(const std::optional<interfaces::BlockKey>& block)
bool BlockFilterIndex::CustomInit(const std::optional<interfaces::BlockRef>& block)
{
if (!m_db->Read(DB_FILTER_POS, m_next_filter_pos)) {
// Check that the cause of the read failure is that the key does not exist. Any other errors
Expand Down Expand Up @@ -316,7 +316,7 @@ bool BlockFilterIndex::Write(const BlockFilter& filter, uint32_t block_height, c
return true;
}

bool BlockFilterIndex::CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip)
bool BlockFilterIndex::CustomRewind(const interfaces::BlockRef& current_tip, const interfaces::BlockRef& new_tip)
{
CDBBatch batch(*m_db);
std::unique_ptr<CDBIterator> db_it(m_db->NewIterator());
Expand Down
4 changes: 2 additions & 2 deletions src/index/blockfilterindex.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ class BlockFilterIndex final : public BaseIndex
std::optional<uint256> ReadFilterHeader(int height, const uint256& expected_block_hash);

protected:
bool CustomInit(const std::optional<interfaces::BlockKey>& block) override;
bool CustomInit(const std::optional<interfaces::BlockRef>& block) override;

bool CustomCommit(CDBBatch& batch) override;

bool CustomAppend(const interfaces::BlockInfo& block) override;

bool CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip) override;
bool CustomRewind(const interfaces::BlockRef& current_tip, const interfaces::BlockRef& new_tip) override;

BaseIndex::DB& GetDB() const LIFETIMEBOUND override { return *m_db; }

Expand Down
6 changes: 3 additions & 3 deletions src/index/coinstatsindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
return true;
}

bool CoinStatsIndex::CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip)
bool CoinStatsIndex::CustomRewind(const interfaces::BlockRef& current_tip, const interfaces::BlockRef& new_tip)
{
CDBBatch batch(*m_db);
std::unique_ptr<CDBIterator> db_it(m_db->NewIterator());
Expand Down Expand Up @@ -304,7 +304,7 @@ bool CoinStatsIndex::CustomRewind(const interfaces::BlockKey& current_tip, const
return true;
}

static bool LookUpOne(const CDBWrapper& db, const interfaces::BlockKey& block, DBVal& result)
static bool LookUpOne(const CDBWrapper& db, const interfaces::BlockRef& block, DBVal& result)
{
// First check if the result is stored under the height index and the value
// there matches the block hash. This should be the case if the block is on
Expand Down Expand Up @@ -350,7 +350,7 @@ std::optional<CCoinsStats> CoinStatsIndex::LookUpStats(const CBlockIndex& block_
return stats;
}

bool CoinStatsIndex::CustomInit(const std::optional<interfaces::BlockKey>& block)
bool CoinStatsIndex::CustomInit(const std::optional<interfaces::BlockRef>& block)
{
if (!m_db->Read(DB_MUHASH, m_muhash)) {
// Check that the cause of the read failure is that the key does not
Expand Down
4 changes: 2 additions & 2 deletions src/index/coinstatsindex.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ class CoinStatsIndex final : public BaseIndex
bool AllowPrune() const override { return true; }

protected:
bool CustomInit(const std::optional<interfaces::BlockKey>& block) override;
bool CustomInit(const std::optional<interfaces::BlockRef>& block) override;

bool CustomCommit(CDBBatch& batch) override;

bool CustomAppend(const interfaces::BlockInfo& block) override;

bool CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip) override;
bool CustomRewind(const interfaces::BlockRef& current_tip, const interfaces::BlockRef& new_tip) override;

BaseIndex::DB& GetDB() const override { return *m_db; }

Expand Down
6 changes: 0 additions & 6 deletions src/interfaces/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ namespace interfaces {
class Handler;
class Wallet;

//! Hash/height pair to help track and identify blocks.
struct BlockKey {
uint256 hash;
int height = -1;
};

//! Helper for findBlock to selectively return pieces of block data. If block is
//! found, data will be returned by setting specified output variables. If block
//! is not found, output variables will keep their previous values.
Expand Down
20 changes: 20 additions & 0 deletions src/interfaces/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2024 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_INTERFACES_TYPES_H
#define BITCOIN_INTERFACES_TYPES_H

#include <uint256.h>

namespace interfaces {

//! Hash/height pair to help track and identify blocks.
struct BlockRef {
uint256 hash;
int height = -1;
};

} // namespace interfaces

#endif // BITCOIN_INTERFACES_TYPES_H

0 comments on commit 89a8f74

Please sign in to comment.