Skip to content

Commit

Permalink
[WIP] index: Allow index to start at a specific height
Browse files Browse the repository at this point in the history
  • Loading branch information
fjahr committed Sep 28, 2023
1 parent 83e1755 commit cd7bf12
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
11 changes: 7 additions & 4 deletions src/index/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ void BaseIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator
batch.Write(DB_BEST_BLOCK, locator);
}

BaseIndex::BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name)
: m_chain{std::move(chain)}, m_name{std::move(name)} {}
BaseIndex::BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name, int start_height)
: m_chain{std::move(chain)}, m_name{std::move(name)}, m_start_height{std::move(start_height)} {}

BaseIndex::~BaseIndex()
{
Expand Down Expand Up @@ -119,11 +119,14 @@ bool BaseIndex::Init()
return true;
}

static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev, CChain& chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev, CChain& chain, int start_height) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);

if (!pindex_prev) {
if (start_height > 0) {
return chain[start_height];
}
return chain.Genesis();
}

Expand Down Expand Up @@ -153,7 +156,7 @@ void BaseIndex::ThreadSync()

{
LOCK(cs_main);
const CBlockIndex* pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain);
const CBlockIndex* pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain, m_start_height);
if (!pindex_next) {
SetBestBlockIndex(pindex);
m_synced = true;
Expand Down
3 changes: 2 additions & 1 deletion src/index/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class BaseIndex : public CValidationInterface
std::unique_ptr<interfaces::Chain> m_chain;
Chainstate* m_chainstate{nullptr};
const std::string m_name;
int m_start_height{0};

void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override;

Expand Down Expand Up @@ -129,7 +130,7 @@ class BaseIndex : public CValidationInterface
void SetBestBlockIndex(const CBlockIndex* block);

public:
BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name);
BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name, int start_height = 0);
/// Destructor interrupts sync thread if running and blocks until it exits.
virtual ~BaseIndex();

Expand Down
12 changes: 3 additions & 9 deletions src/index/silentpaymentindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ bool SilentPaymentIndex::DB::WriteSilentPayments(const std::pair<uint256, std::v
return WriteBatch(batch);
}

SilentPaymentIndex::SilentPaymentIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
: BaseIndex(std::move(chain), "silentpaymentindex"), m_db(std::make_unique<SilentPaymentIndex::DB>(n_cache_size, f_memory, f_wipe))
SilentPaymentIndex::SilentPaymentIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe, int start_height)
: BaseIndex(std::move(chain), "silentpaymentindex", start_height), m_db(std::make_unique<SilentPaymentIndex::DB>(n_cache_size, f_memory, f_wipe))
{}

SilentPaymentIndex::~SilentPaymentIndex() {}
Expand Down Expand Up @@ -98,12 +98,6 @@ bool SilentPaymentIndex::CustomAppend(const interfaces::BlockInfo& block)

assert(block.data);

Consensus::Params consensus = Params().GetConsensus();

if (block.height < consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height) {
return true;
}

std::vector<std::pair<uint256, CPubKey>> items;

const CBlockIndex* block_index = WITH_LOCK(cs_main, return m_chainstate->m_blockman.LookupBlockIndex(block.hash));
Expand All @@ -129,4 +123,4 @@ bool SilentPaymentIndex::FindSilentPayment(const uint256& block_hash, std::vecto
return m_db->Read(std::make_pair(DB_SILENT_PAYMENT_INDEX, block_hash), tweaked_pub_key_sums);
}

BaseIndex::DB& SilentPaymentIndex::GetDB() const { return *m_db; }
BaseIndex::DB& SilentPaymentIndex::GetDB() const { return *m_db; }
4 changes: 2 additions & 2 deletions src/index/silentpaymentindex.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SilentPaymentIndex final : public BaseIndex
BaseIndex::DB& GetDB() const override;
public:

explicit SilentPaymentIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
explicit SilentPaymentIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false, int start_height = 0);

// Destructor is declared because this class contains a unique_ptr to an incomplete type.
virtual ~SilentPaymentIndex() override;
Expand All @@ -56,4 +56,4 @@ class SilentPaymentIndex final : public BaseIndex
/// The global txo silent payment index. May be null.
extern std::unique_ptr<SilentPaymentIndex> g_silent_payment_index;

#endif // BITCOIN_INDEX_SILENTPAYMENTINDEX_H
#endif // BITCOIN_INDEX_SILENTPAYMENTINDEX_H
4 changes: 3 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1591,8 +1591,10 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
node.indexes.emplace_back(g_coin_stats_index.get());
}

Consensus::Params consensus = Params().GetConsensus();
const int sp_start_height = consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height;
if (args.GetBoolArg("-silentpaymentindex", DEFAULT_SILENT_PAYMENT_INDEX)) {
g_silent_payment_index = std::make_unique<SilentPaymentIndex>(interfaces::MakeChain(node), cache_sizes.tx_index, false, fReindex);
g_silent_payment_index = std::make_unique<SilentPaymentIndex>(interfaces::MakeChain(node), cache_sizes.tx_index, false, fReindex, sp_start_height);
node.indexes.emplace_back(g_silent_payment_index.get());
}

Expand Down

0 comments on commit cd7bf12

Please sign in to comment.