Changeset View
Changeset View
Standalone View
Standalone View
src/interfaces/chain.h
Show First 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | public: | ||||
FoundBlock &maxTime(int64_t &max_time) { | FoundBlock &maxTime(int64_t &max_time) { | ||||
m_max_time = &max_time; | m_max_time = &max_time; | ||||
return *this; | return *this; | ||||
} | } | ||||
FoundBlock &mtpTime(int64_t &mtp_time) { | FoundBlock &mtpTime(int64_t &mtp_time) { | ||||
m_mtp_time = &mtp_time; | m_mtp_time = &mtp_time; | ||||
return *this; | return *this; | ||||
} | } | ||||
//! Return whether block is in the active (most-work) chain. | |||||
FoundBlock &inActiveChain(bool &in_active_chain) { | |||||
m_in_active_chain = &in_active_chain; | |||||
return *this; | |||||
} | |||||
//! Return next block in the active chain if current block is in the active | |||||
//! chain. | |||||
FoundBlock &nextBlock(const FoundBlock &next_block) { | |||||
m_next_block = &next_block; | |||||
return *this; | |||||
} | |||||
//! Read block data from disk. If the block exists but doesn't have data | //! Read block data from disk. If the block exists but doesn't have data | ||||
//! (for example due to pruning), the CBlock variable will be set to null. | //! (for example due to pruning), the CBlock variable will be set to null. | ||||
FoundBlock &data(CBlock &data) { | FoundBlock &data(CBlock &data) { | ||||
m_data = &data; | m_data = &data; | ||||
return *this; | return *this; | ||||
} | } | ||||
BlockHash *m_hash = nullptr; | BlockHash *m_hash = nullptr; | ||||
int *m_height = nullptr; | int *m_height = nullptr; | ||||
int64_t *m_time = nullptr; | int64_t *m_time = nullptr; | ||||
int64_t *m_max_time = nullptr; | int64_t *m_max_time = nullptr; | ||||
int64_t *m_mtp_time = nullptr; | int64_t *m_mtp_time = nullptr; | ||||
bool *m_in_active_chain = nullptr; | |||||
const FoundBlock *m_next_block = nullptr; | |||||
CBlock *m_data = nullptr; | CBlock *m_data = nullptr; | ||||
}; | }; | ||||
//! Interface giving clients (wallet processes, maybe other analysis tools in | //! Interface giving clients (wallet processes, maybe other analysis tools in | ||||
//! the future) ability to access to the chain state, receive notifications, | //! the future) ability to access to the chain state, receive notifications, | ||||
//! estimate fees, and submit transactions. | //! estimate fees, and submit transactions. | ||||
//! | //! | ||||
//! TODO: Current chain methods are too low level, exposing too much of the | //! TODO: Current chain methods are too low level, exposing too much of the | ||||
Show All 9 Lines | |||||
//! * The handleRpc, registerRpcs, rpcEnableDeprecated methods and other RPC | //! * The handleRpc, registerRpcs, rpcEnableDeprecated methods and other RPC | ||||
//! methods can go away if wallets listen for HTTP requests on their own | //! methods can go away if wallets listen for HTTP requests on their own | ||||
//! ports instead of registering to handle requests on the node HTTP port. | //! ports instead of registering to handle requests on the node HTTP port. | ||||
//! | //! | ||||
//! * Move fee estimation queries to an asynchronous interface and let the | //! * Move fee estimation queries to an asynchronous interface and let the | ||||
//! wallet cache it, fee estimation being driven by node mempool, wallet | //! wallet cache it, fee estimation being driven by node mempool, wallet | ||||
//! should be the consumer. | //! should be the consumer. | ||||
//! | //! | ||||
//! * The `guessVerificationProgress`, `getBlockHeight`, `getBlockHash`, etc | //! * `guessVerificationProgress` and similar methods can go away if rescan | ||||
//! methods can go away if rescan logic is moved on the node side, and wallet | //! logic moves out of the wallet, and the wallet just requests scans from the | ||||
//! only register rescan request. | //! node (https://github.com/bitcoin/bitcoin/issues/11756) | ||||
class Chain { | class Chain { | ||||
public: | public: | ||||
virtual ~Chain() {} | virtual ~Chain() {} | ||||
//! Get current chain height, not including genesis block (returns 0 if | //! Get current chain height, not including genesis block (returns 0 if | ||||
//! chain only contains genesis block, std::nullopt if chain does not | //! chain only contains genesis block, std::nullopt if chain does not | ||||
//! contain any blocks) | //! contain any blocks) | ||||
virtual std::optional<int> getHeight() = 0; | virtual std::optional<int> getHeight() = 0; | ||||
//! Get block height above genesis block. Returns 0 for genesis block, | |||||
//! 1 for following block, and so on. Returns std::nullopt for a block not | |||||
//! included in the current chain. | |||||
virtual std::optional<int> getBlockHeight(const BlockHash &hash) = 0; | |||||
//! Get block hash. Height must be valid or this function will abort. | //! Get block hash. Height must be valid or this function will abort. | ||||
virtual BlockHash getBlockHash(int height) = 0; | virtual BlockHash getBlockHash(int height) = 0; | ||||
//! Check that the block is available on disk (i.e. has not been | //! Check that the block is available on disk (i.e. has not been | ||||
//! pruned), and contains transactions. | //! pruned), and contains transactions. | ||||
virtual bool haveBlockOnDisk(int height) = 0; | virtual bool haveBlockOnDisk(int height) = 0; | ||||
//! Return height of the first block in the chain with timestamp equal | |||||
//! or greater than the given time and height equal or greater than the | |||||
//! given height, or std::nullopt if there is no block with a high enough | |||||
//! timestamp and height. Also return the block hash as an optional output | |||||
//! parameter (to avoid the cost of a second lookup in case this information | |||||
//! is needed.) | |||||
virtual std::optional<int> | |||||
findFirstBlockWithTimeAndHeight(int64_t time, int height, | |||||
BlockHash *hash) = 0; | |||||
//! Get locator for the current chain tip. | //! Get locator for the current chain tip. | ||||
virtual CBlockLocator getTipLocator() = 0; | virtual CBlockLocator getTipLocator() = 0; | ||||
//! Return height of the highest block on chain in common with the locator, | //! Return height of the highest block on chain in common with the locator, | ||||
//! which will either be the original block used to create the locator, | //! which will either be the original block used to create the locator, | ||||
//! or one of its ancestors. | //! or one of its ancestors. | ||||
virtual std::optional<int> | virtual std::optional<int> | ||||
findLocatorFork(const CBlockLocator &locator) = 0; | findLocatorFork(const CBlockLocator &locator) = 0; | ||||
Show All 11 Lines | public: | ||||
//! Find first block in the chain with timestamp >= the given time | //! Find first block in the chain with timestamp >= the given time | ||||
//! and height >= than the given height, return false if there is no block | //! and height >= than the given height, return false if there is no block | ||||
//! with a high enough timestamp and height. Optionally return block | //! with a high enough timestamp and height. Optionally return block | ||||
//! information. | //! information. | ||||
virtual bool | virtual bool | ||||
findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, | findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, | ||||
const FoundBlock &block = {}) = 0; | const FoundBlock &block = {}) = 0; | ||||
//! Find next block if block is part of current chain. Also flag if | |||||
//! there was a reorg and the specified block hash is no longer in the | |||||
//! current chain, and optionally return block information. | |||||
virtual bool findNextBlock(const BlockHash &block_hash, int block_height, | |||||
const FoundBlock &next = {}, | |||||
bool *reorg = nullptr) = 0; | |||||
//! Find ancestor of block at specified height and optionally return | //! Find ancestor of block at specified height and optionally return | ||||
//! ancestor information. | //! ancestor information. | ||||
virtual bool findAncestorByHeight(const BlockHash &block_hash, | virtual bool findAncestorByHeight(const BlockHash &block_hash, | ||||
int ancestor_height, | int ancestor_height, | ||||
const FoundBlock &ancestor_out = {}) = 0; | const FoundBlock &ancestor_out = {}) = 0; | ||||
//! Return whether block descends from a specified ancestor, and | //! Return whether block descends from a specified ancestor, and | ||||
//! optionally return ancestor information. | //! optionally return ancestor information. | ||||
▲ Show 20 Lines • Show All 184 Lines • Show Last 20 Lines |