2020-01-14 16:17:38 -03:00
|
|
|
// Copyright (c) 2018-2020 The Bitcoin Core developers
|
2017-05-30 15:55:17 -04:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_INTERFACES_CHAIN_H
|
|
|
|
#define BITCOIN_INTERFACES_CHAIN_H
|
|
|
|
|
2017-07-28 19:25:26 -04:00
|
|
|
#include <optional.h> // For Optional and nullopt
|
2017-07-31 16:31:29 -04:00
|
|
|
#include <primitives/transaction.h> // For CTransactionRef
|
2019-05-01 15:12:44 -04:00
|
|
|
#include <util/settings.h> // For util::SettingsValue
|
2017-07-27 10:08:31 -04:00
|
|
|
|
2020-06-05 16:11:36 -04:00
|
|
|
#include <functional>
|
2017-05-30 15:55:17 -04:00
|
|
|
#include <memory>
|
2017-07-28 19:42:27 -04:00
|
|
|
#include <stddef.h>
|
2019-01-08 03:35:47 -03:00
|
|
|
#include <stdint.h>
|
2017-05-30 15:55:17 -04:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-05-11 11:40:21 -04:00
|
|
|
class ArgsManager;
|
2019-01-08 03:56:46 -03:00
|
|
|
class CBlock;
|
2019-03-04 17:57:58 -03:00
|
|
|
class CFeeRate;
|
2017-07-31 11:46:13 -04:00
|
|
|
class CRPCCommand;
|
2017-09-28 15:13:29 -03:00
|
|
|
class CScheduler;
|
2018-07-17 13:04:35 -04:00
|
|
|
class Coin;
|
2017-07-27 10:08:31 -04:00
|
|
|
class uint256;
|
2020-05-15 09:23:55 -04:00
|
|
|
enum class MemPoolRemovalReason;
|
2019-03-04 17:57:58 -03:00
|
|
|
enum class RBFTransactionState;
|
2020-04-11 11:48:04 -04:00
|
|
|
struct bilingual_str;
|
2019-01-08 05:06:24 -03:00
|
|
|
struct CBlockLocator;
|
2017-07-28 21:40:29 -04:00
|
|
|
struct FeeCalculation;
|
2019-09-17 19:28:03 -03:00
|
|
|
struct NodeContext;
|
2017-09-28 15:13:29 -03:00
|
|
|
|
2017-05-30 15:55:17 -04:00
|
|
|
namespace interfaces {
|
|
|
|
|
2017-07-30 16:00:56 -04:00
|
|
|
class Handler;
|
2018-04-08 15:37:50 -03:00
|
|
|
class Wallet;
|
|
|
|
|
2020-02-24 16:34:17 -03:00
|
|
|
//! Helper for findBlock to selectively return pieces of block data.
|
|
|
|
class FoundBlock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FoundBlock& hash(uint256& hash) { m_hash = &hash; return *this; }
|
|
|
|
FoundBlock& height(int& height) { m_height = &height; return *this; }
|
|
|
|
FoundBlock& time(int64_t& time) { m_time = &time; return *this; }
|
|
|
|
FoundBlock& maxTime(int64_t& max_time) { m_max_time = &max_time; return *this; }
|
|
|
|
FoundBlock& mtpTime(int64_t& mtp_time) { m_mtp_time = &mtp_time; return *this; }
|
|
|
|
//! 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.
|
|
|
|
FoundBlock& data(CBlock& data) { m_data = &data; return *this; }
|
|
|
|
|
|
|
|
uint256* m_hash = nullptr;
|
|
|
|
int* m_height = nullptr;
|
|
|
|
int64_t* m_time = nullptr;
|
|
|
|
int64_t* m_max_time = nullptr;
|
|
|
|
int64_t* m_mtp_time = nullptr;
|
|
|
|
CBlock* m_data = nullptr;
|
|
|
|
};
|
|
|
|
|
2019-03-04 17:57:58 -03:00
|
|
|
//! Interface giving clients (wallet processes, maybe other analysis tools in
|
|
|
|
//! the future) ability to access to the chain state, receive notifications,
|
|
|
|
//! estimate fees, and submit transactions.
|
|
|
|
//!
|
|
|
|
//! TODO: Current chain methods are too low level, exposing too much of the
|
|
|
|
//! internal workings of the bitcoin node, and not being very convenient to use.
|
|
|
|
//! Chain methods should be cleaned up and simplified over time. Examples:
|
|
|
|
//!
|
2019-12-18 19:46:53 -03:00
|
|
|
//! * The initMessages() and showProgress() methods which the wallet uses to send
|
2019-03-04 17:57:58 -03:00
|
|
|
//! notifications to the GUI should go away when GUI and wallet can directly
|
|
|
|
//! communicate with each other without going through the node
|
|
|
|
//! (https://github.com/bitcoin/bitcoin/pull/15288#discussion_r253321096).
|
2019-03-22 01:42:17 -03:00
|
|
|
//!
|
|
|
|
//! * The handleRpc, registerRpcs, rpcEnableDeprecated methods and other RPC
|
|
|
|
//! 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.
|
2019-12-18 19:46:53 -03:00
|
|
|
//!
|
|
|
|
//! * Move fee estimation queries to an asynchronous interface and let the
|
|
|
|
//! wallet cache it, fee estimation being driven by node mempool, wallet
|
|
|
|
//! should be the consumer.
|
|
|
|
//!
|
|
|
|
//! * The `guessVerificationProgress`, `getBlockHeight`, `getBlockHash`, etc
|
|
|
|
//! methods can go away if rescan logic is moved on the node side, and wallet
|
|
|
|
//! only register rescan request.
|
2017-05-30 15:55:17 -04:00
|
|
|
class Chain
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~Chain() {}
|
2017-07-26 10:23:01 -04:00
|
|
|
|
2019-07-09 20:07:39 -04:00
|
|
|
//! Get current chain height, not including genesis block (returns 0 if
|
|
|
|
//! chain only contains genesis block, nullopt if chain does not contain
|
|
|
|
//! any blocks)
|
|
|
|
virtual Optional<int> getHeight() = 0;
|
|
|
|
|
2019-07-15 18:20:12 -04:00
|
|
|
//! Get block height above genesis block. Returns 0 for genesis block,
|
|
|
|
//! 1 for following block, and so on. Returns nullopt for a block not
|
|
|
|
//! included in the current chain.
|
|
|
|
virtual Optional<int> getBlockHeight(const uint256& hash) = 0;
|
|
|
|
|
2019-07-16 15:20:01 -04:00
|
|
|
//! Get block hash. Height must be valid or this function will abort.
|
|
|
|
virtual uint256 getBlockHash(int height) = 0;
|
|
|
|
|
2019-07-16 17:01:46 -04:00
|
|
|
//! Check that the block is available on disk (i.e. has not been
|
|
|
|
//! pruned), and contains transactions.
|
|
|
|
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 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 Optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height, uint256* hash) = 0;
|
|
|
|
|
|
|
|
//! Get locator for the current chain tip.
|
|
|
|
virtual CBlockLocator getTipLocator() = 0;
|
|
|
|
|
|
|
|
//! 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,
|
|
|
|
//! or one of its ancestors.
|
|
|
|
virtual Optional<int> findLocatorFork(const CBlockLocator& locator) = 0;
|
|
|
|
|
|
|
|
//! Check if transaction will be final given chain height current time.
|
|
|
|
virtual bool checkFinalTx(const CTransaction& tx) = 0;
|
|
|
|
|
2019-01-08 03:56:46 -03:00
|
|
|
//! Return whether node has the block and optionally return block metadata
|
|
|
|
//! or contents.
|
2020-02-24 16:34:17 -03:00
|
|
|
virtual bool findBlock(const uint256& hash, const FoundBlock& block={}) = 0;
|
2019-01-08 04:38:53 -03:00
|
|
|
|
2020-01-21 17:55:19 -03:00
|
|
|
//! Find first block in the chain with timestamp >= the given time
|
|
|
|
//! and height >= than the given height, return false if there is no block
|
|
|
|
//! with a high enough timestamp and height. Optionally return block
|
|
|
|
//! information.
|
|
|
|
virtual bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block={}) = 0;
|
|
|
|
|
2020-01-22 18:53:42 -03:00
|
|
|
//! 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 uint256& block_hash, int block_height, const FoundBlock& next={}, bool* reorg=nullptr) = 0;
|
|
|
|
|
2020-01-21 17:00:33 -03:00
|
|
|
//! Find ancestor of block at specified height and optionally return
|
|
|
|
//! ancestor information.
|
|
|
|
virtual bool findAncestorByHeight(const uint256& block_hash, int ancestor_height, const FoundBlock& ancestor_out={}) = 0;
|
|
|
|
|
2020-01-16 18:38:30 -03:00
|
|
|
//! Return whether block descends from a specified ancestor, and
|
|
|
|
//! optionally return ancestor information.
|
|
|
|
virtual bool findAncestorByHash(const uint256& block_hash,
|
|
|
|
const uint256& ancestor_hash,
|
|
|
|
const FoundBlock& ancestor_out={}) = 0;
|
|
|
|
|
2020-01-21 17:00:33 -03:00
|
|
|
//! Find most recent common ancestor between two blocks and optionally
|
|
|
|
//! return block information.
|
|
|
|
virtual bool findCommonAncestor(const uint256& block_hash1,
|
|
|
|
const uint256& block_hash2,
|
|
|
|
const FoundBlock& ancestor_out={},
|
|
|
|
const FoundBlock& block1_out={},
|
|
|
|
const FoundBlock& block2_out={}) = 0;
|
2019-01-08 04:38:53 -03:00
|
|
|
|
2018-07-17 13:04:35 -04:00
|
|
|
//! Look up unspent output information. Returns coins in the mempool and in
|
|
|
|
//! the current chain UTXO set. Iterates through all the keys in the map and
|
|
|
|
//! populates the values.
|
|
|
|
virtual void findCoins(std::map<COutPoint, Coin>& coins) = 0;
|
|
|
|
|
2019-01-08 04:38:53 -03:00
|
|
|
//! Estimate fraction of total transactions verified if blocks up to
|
|
|
|
//! the specified block hash are verified.
|
|
|
|
virtual double guessVerificationProgress(const uint256& block_hash) = 0;
|
2017-07-28 19:25:26 -04:00
|
|
|
|
2020-01-21 19:08:12 -03:00
|
|
|
//! Return true if data is available for all blocks in the specified range
|
|
|
|
//! of blocks. This checks all blocks that are ancestors of block_hash in
|
|
|
|
//! the height range from min_height to max_height, inclusive.
|
|
|
|
virtual bool hasBlocks(const uint256& block_hash, int min_height = 0, Optional<int> max_height = {}) = 0;
|
|
|
|
|
2017-07-28 19:25:26 -04:00
|
|
|
//! Check if transaction is RBF opt in.
|
|
|
|
virtual RBFTransactionState isRBFOptIn(const CTransaction& tx) = 0;
|
2017-07-28 19:29:50 -04:00
|
|
|
|
|
|
|
//! Check if transaction has descendants in mempool.
|
|
|
|
virtual bool hasDescendantsInMempool(const uint256& txid) = 0;
|
2017-07-28 19:34:54 -04:00
|
|
|
|
2019-04-11 10:37:30 -04:00
|
|
|
//! Transaction is added to memory pool, if the transaction fee is below the
|
|
|
|
//! amount specified by max_tx_fee, and broadcast to all peers if relay is set to true.
|
|
|
|
//! Return false if the transaction could not be added due to the fee or for another reason.
|
2017-12-05 17:57:12 -03:00
|
|
|
virtual bool broadcastTransaction(const CTransactionRef& tx,
|
|
|
|
const CAmount& max_tx_fee,
|
|
|
|
bool relay,
|
|
|
|
std::string& err_string) = 0;
|
2019-04-11 10:37:30 -04:00
|
|
|
|
2017-07-28 19:42:27 -04:00
|
|
|
//! Calculate mempool ancestor and descendant counts for the given transaction.
|
|
|
|
virtual void getTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) = 0;
|
2017-07-28 19:45:45 -04:00
|
|
|
|
2019-10-14 14:32:41 -03:00
|
|
|
//! Get the node's package limits.
|
|
|
|
//! Currently only returns the ancestor and descendant count limits, but could be enhanced to
|
|
|
|
//! return more policy settings.
|
|
|
|
virtual void getPackageLimits(unsigned int& limit_ancestor_count, unsigned int& limit_descendant_count) = 0;
|
|
|
|
|
2019-03-04 17:57:58 -03:00
|
|
|
//! Check if transaction will pass the mempool's chain limits.
|
|
|
|
virtual bool checkChainLimits(const CTransactionRef& tx) = 0;
|
2017-07-28 21:40:29 -04:00
|
|
|
|
|
|
|
//! Estimate smart fee.
|
|
|
|
virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc = nullptr) = 0;
|
|
|
|
|
|
|
|
//! Fee estimator max target.
|
|
|
|
virtual unsigned int estimateMaxBlocks() = 0;
|
|
|
|
|
2019-03-04 17:57:58 -03:00
|
|
|
//! Mempool minimum fee.
|
2017-07-28 21:40:29 -04:00
|
|
|
virtual CFeeRate mempoolMinFee() = 0;
|
2017-07-28 22:45:01 -04:00
|
|
|
|
2019-03-06 18:47:57 -03:00
|
|
|
//! Relay current minimum fee (from -minrelaytxfee and -incrementalrelayfee settings).
|
|
|
|
virtual CFeeRate relayMinFee() = 0;
|
|
|
|
|
|
|
|
//! Relay incremental fee setting (-incrementalrelayfee), reflecting cost of relay.
|
|
|
|
virtual CFeeRate relayIncrementalFee() = 0;
|
|
|
|
|
|
|
|
//! Relay dust fee setting (-dustrelayfee), reflecting lowest rate it's economical to spend.
|
|
|
|
virtual CFeeRate relayDustFee() = 0;
|
|
|
|
|
2019-04-22 14:30:02 -04:00
|
|
|
//! Check if any block has been pruned.
|
|
|
|
virtual bool havePruned() = 0;
|
2017-07-28 22:50:07 -04:00
|
|
|
|
2019-03-20 18:46:38 -03:00
|
|
|
//! Check if the node is ready to broadcast transactions.
|
|
|
|
virtual bool isReadyToBroadcast() = 0;
|
|
|
|
|
2019-03-06 18:47:57 -03:00
|
|
|
//! Check if in IBD.
|
2018-07-10 15:48:03 -04:00
|
|
|
virtual bool isInitialBlockDownload() = 0;
|
|
|
|
|
2019-03-06 18:47:57 -03:00
|
|
|
//! Check if shutdown requested.
|
|
|
|
virtual bool shutdownRequested() = 0;
|
|
|
|
|
2017-07-28 22:54:31 -04:00
|
|
|
//! Get adjusted time.
|
|
|
|
virtual int64_t getAdjustedTime() = 0;
|
2017-07-28 22:59:47 -04:00
|
|
|
|
|
|
|
//! Send init message.
|
|
|
|
virtual void initMessage(const std::string& message) = 0;
|
|
|
|
|
|
|
|
//! Send init warning.
|
2020-05-09 07:46:01 -04:00
|
|
|
virtual void initWarning(const bilingual_str& message) = 0;
|
2017-07-28 22:59:47 -04:00
|
|
|
|
|
|
|
//! Send init error.
|
2020-04-11 11:48:04 -04:00
|
|
|
virtual void initError(const bilingual_str& message) = 0;
|
2018-04-08 15:37:50 -03:00
|
|
|
|
2019-03-06 18:47:57 -03:00
|
|
|
//! Send progress indicator.
|
|
|
|
virtual void showProgress(const std::string& title, int progress, bool resume_possible) = 0;
|
|
|
|
|
2017-07-30 16:00:56 -04:00
|
|
|
//! Chain notifications.
|
|
|
|
class Notifications
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~Notifications() {}
|
2020-02-26 18:05:49 -03:00
|
|
|
virtual void transactionAddedToMempool(const CTransactionRef& tx) {}
|
2020-05-15 09:23:55 -04:00
|
|
|
virtual void transactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason) {}
|
2020-02-26 18:05:49 -03:00
|
|
|
virtual void blockConnected(const CBlock& block, int height) {}
|
|
|
|
virtual void blockDisconnected(const CBlock& block, int height) {}
|
|
|
|
virtual void updatedBlockTip() {}
|
|
|
|
virtual void chainStateFlushed(const CBlockLocator& locator) {}
|
2017-07-30 16:00:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
//! Register handler for notifications.
|
2020-03-10 16:46:20 -03:00
|
|
|
virtual std::unique_ptr<Handler> handleNotifications(std::shared_ptr<Notifications> notifications) = 0;
|
2017-07-30 16:00:56 -04:00
|
|
|
|
2019-04-18 08:21:35 -04:00
|
|
|
//! Wait for pending notifications to be processed unless block hash points to the current
|
2019-06-24 19:07:09 -04:00
|
|
|
//! chain tip.
|
|
|
|
virtual void waitForNotificationsIfTipChanged(const uint256& old_tip) = 0;
|
2017-07-31 11:46:13 -04:00
|
|
|
|
|
|
|
//! Register handler for RPC. Command is not copied, so reference
|
|
|
|
//! needs to remain valid until Handler is disconnected.
|
|
|
|
virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) = 0;
|
2019-03-27 14:00:06 -03:00
|
|
|
|
2019-03-22 01:42:17 -03:00
|
|
|
//! Check if deprecated RPC is enabled.
|
|
|
|
virtual bool rpcEnableDeprecated(const std::string& method) = 0;
|
|
|
|
|
|
|
|
//! Run function after given number of seconds. Cancel any previous calls with same name.
|
|
|
|
virtual void rpcRunLater(const std::string& name, std::function<void()> fn, int64_t seconds) = 0;
|
|
|
|
|
|
|
|
//! Current RPC serialization flags.
|
|
|
|
virtual int rpcSerializationFlags() = 0;
|
|
|
|
|
2019-05-01 15:12:44 -04:00
|
|
|
//! Return <datadir>/settings.json setting value.
|
|
|
|
virtual util::SettingsValue getRwSetting(const std::string& name) = 0;
|
|
|
|
|
|
|
|
//! Write a setting to <datadir>/settings.json.
|
|
|
|
virtual bool updateRwSetting(const std::string& name, const util::SettingsValue& value) = 0;
|
|
|
|
|
2020-02-26 18:05:49 -03:00
|
|
|
//! Synchronously send transactionAddedToMempool notifications about all
|
2019-03-27 14:00:06 -03:00
|
|
|
//! current mempool transactions to the specified handler and return after
|
|
|
|
//! the last one is sent. These notifications aren't coordinated with async
|
|
|
|
//! notifications sent by handleNotifications, so out of date async
|
|
|
|
//! notifications from handleNotifications can arrive during and after
|
|
|
|
//! synchronous notifications from requestMempoolTransactions. Clients need
|
|
|
|
//! to be prepared to handle this by ignoring notifications about unknown
|
|
|
|
//! removed transactions and already added new transactions.
|
|
|
|
virtual void requestMempoolTransactions(Notifications& notifications) = 0;
|
2017-05-30 15:55:17 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
//! Interface to let node manage chain clients (wallets, or maybe tools for
|
|
|
|
//! monitoring and analysis in the future).
|
|
|
|
class ChainClient
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~ChainClient() {}
|
2017-09-28 15:13:29 -03:00
|
|
|
|
|
|
|
//! Register rpcs.
|
|
|
|
virtual void registerRpcs() = 0;
|
|
|
|
|
|
|
|
//! Check for errors before loading.
|
|
|
|
virtual bool verify() = 0;
|
|
|
|
|
|
|
|
//! Load saved state.
|
|
|
|
virtual bool load() = 0;
|
|
|
|
|
|
|
|
//! Start client execution and provide a scheduler.
|
|
|
|
virtual void start(CScheduler& scheduler) = 0;
|
|
|
|
|
|
|
|
//! Save state to disk.
|
|
|
|
virtual void flush() = 0;
|
|
|
|
|
|
|
|
//! Shut down client.
|
|
|
|
virtual void stop() = 0;
|
2017-12-05 17:57:12 -03:00
|
|
|
|
|
|
|
//! Set mock time.
|
|
|
|
virtual void setMockTime(int64_t time) = 0;
|
2017-05-30 15:55:17 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
//! Return implementation of Chain interface.
|
2019-09-17 19:28:03 -03:00
|
|
|
std::unique_ptr<Chain> MakeChain(NodeContext& node);
|
2017-05-30 15:55:17 -04:00
|
|
|
|
|
|
|
} // namespace interfaces
|
|
|
|
|
|
|
|
#endif // BITCOIN_INTERFACES_CHAIN_H
|