Branch data Line data Source code
# 1 : : // Copyright (c) 2018-2020 The Bitcoin Core 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 : : #ifndef BITCOIN_INTERFACES_CHAIN_H
# 6 : : #define BITCOIN_INTERFACES_CHAIN_H
# 7 : :
# 8 : : #include <primitives/transaction.h> // For CTransactionRef
# 9 : : #include <util/settings.h> // For util::SettingsValue
# 10 : :
# 11 : : #include <functional>
# 12 : : #include <memory>
# 13 : : #include <optional>
# 14 : : #include <stddef.h>
# 15 : : #include <stdint.h>
# 16 : : #include <string>
# 17 : : #include <vector>
# 18 : :
# 19 : : class ArgsManager;
# 20 : : class CBlock;
# 21 : : class CFeeRate;
# 22 : : class CRPCCommand;
# 23 : : class CScheduler;
# 24 : : class Coin;
# 25 : : class uint256;
# 26 : : enum class MemPoolRemovalReason;
# 27 : : enum class RBFTransactionState;
# 28 : : struct bilingual_str;
# 29 : : struct CBlockLocator;
# 30 : : struct FeeCalculation;
# 31 : : struct NodeContext;
# 32 : :
# 33 : : namespace interfaces {
# 34 : :
# 35 : : class Handler;
# 36 : : class Wallet;
# 37 : :
# 38 : : //! Helper for findBlock to selectively return pieces of block data.
# 39 : : class FoundBlock
# 40 : : {
# 41 : : public:
# 42 : 79354 : FoundBlock& hash(uint256& hash) { m_hash = &hash; return *this; }
# 43 : 10596 : FoundBlock& height(int& height) { m_height = &height; return *this; }
# 44 : 31449 : FoundBlock& time(int64_t& time) { m_time = &time; return *this; }
# 45 : 3 : FoundBlock& maxTime(int64_t& max_time) { m_max_time = &max_time; return *this; }
# 46 : 371 : FoundBlock& mtpTime(int64_t& mtp_time) { m_mtp_time = &mtp_time; return *this; }
# 47 : : //! Return whether block is in the active (most-work) chain.
# 48 : 167402 : FoundBlock& inActiveChain(bool& in_active_chain) { m_in_active_chain = &in_active_chain; return *this; }
# 49 : : //! Return next block in the active chain if current block is in the active chain.
# 50 : 78756 : FoundBlock& nextBlock(const FoundBlock& next_block) { m_next_block = &next_block; return *this; }
# 51 : : //! Read block data from disk. If the block exists but doesn't have data
# 52 : : //! (for example due to pruning), the CBlock variable will be set to null.
# 53 : 78778 : FoundBlock& data(CBlock& data) { m_data = &data; return *this; }
# 54 : :
# 55 : : uint256* m_hash = nullptr;
# 56 : : int* m_height = nullptr;
# 57 : : int64_t* m_time = nullptr;
# 58 : : int64_t* m_max_time = nullptr;
# 59 : : int64_t* m_mtp_time = nullptr;
# 60 : : bool* m_in_active_chain = nullptr;
# 61 : : const FoundBlock* m_next_block = nullptr;
# 62 : : CBlock* m_data = nullptr;
# 63 : : };
# 64 : :
# 65 : : //! Interface giving clients (wallet processes, maybe other analysis tools in
# 66 : : //! the future) ability to access to the chain state, receive notifications,
# 67 : : //! estimate fees, and submit transactions.
# 68 : : //!
# 69 : : //! TODO: Current chain methods are too low level, exposing too much of the
# 70 : : //! internal workings of the bitcoin node, and not being very convenient to use.
# 71 : : //! Chain methods should be cleaned up and simplified over time. Examples:
# 72 : : //!
# 73 : : //! * The initMessages() and showProgress() methods which the wallet uses to send
# 74 : : //! notifications to the GUI should go away when GUI and wallet can directly
# 75 : : //! communicate with each other without going through the node
# 76 : : //! (https://github.com/bitcoin/bitcoin/pull/15288#discussion_r253321096).
# 77 : : //!
# 78 : : //! * The handleRpc, registerRpcs, rpcEnableDeprecated methods and other RPC
# 79 : : //! methods can go away if wallets listen for HTTP requests on their own
# 80 : : //! ports instead of registering to handle requests on the node HTTP port.
# 81 : : //!
# 82 : : //! * Move fee estimation queries to an asynchronous interface and let the
# 83 : : //! wallet cache it, fee estimation being driven by node mempool, wallet
# 84 : : //! should be the consumer.
# 85 : : //!
# 86 : : //! * `guessVerificationProgress` and similar methods can go away if rescan
# 87 : : //! logic moves out of the wallet, and the wallet just requests scans from the
# 88 : : //! node (https://github.com/bitcoin/bitcoin/issues/11756)
# 89 : : class Chain
# 90 : : {
# 91 : : public:
# 92 : 1510 : virtual ~Chain() {}
# 93 : :
# 94 : : //! Get current chain height, not including genesis block (returns 0 if
# 95 : : //! chain only contains genesis block, nullopt if chain does not contain
# 96 : : //! any blocks)
# 97 : : virtual std::optional<int> getHeight() = 0;
# 98 : :
# 99 : : //! Get block hash. Height must be valid or this function will abort.
# 100 : : virtual uint256 getBlockHash(int height) = 0;
# 101 : :
# 102 : : //! Check that the block is available on disk (i.e. has not been
# 103 : : //! pruned), and contains transactions.
# 104 : : virtual bool haveBlockOnDisk(int height) = 0;
# 105 : :
# 106 : : //! Get locator for the current chain tip.
# 107 : : virtual CBlockLocator getTipLocator() = 0;
# 108 : :
# 109 : : //! Return height of the highest block on chain in common with the locator,
# 110 : : //! which will either be the original block used to create the locator,
# 111 : : //! or one of its ancestors.
# 112 : : virtual std::optional<int> findLocatorFork(const CBlockLocator& locator) = 0;
# 113 : :
# 114 : : //! Check if transaction will be final given chain height current time.
# 115 : : virtual bool checkFinalTx(const CTransaction& tx) = 0;
# 116 : :
# 117 : : //! Return whether node has the block and optionally return block metadata
# 118 : : //! or contents.
# 119 : : virtual bool findBlock(const uint256& hash, const FoundBlock& block={}) = 0;
# 120 : :
# 121 : : //! Find first block in the chain with timestamp >= the given time
# 122 : : //! and height >= than the given height, return false if there is no block
# 123 : : //! with a high enough timestamp and height. Optionally return block
# 124 : : //! information.
# 125 : : virtual bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block={}) = 0;
# 126 : :
# 127 : : //! Find ancestor of block at specified height and optionally return
# 128 : : //! ancestor information.
# 129 : : virtual bool findAncestorByHeight(const uint256& block_hash, int ancestor_height, const FoundBlock& ancestor_out={}) = 0;
# 130 : :
# 131 : : //! Return whether block descends from a specified ancestor, and
# 132 : : //! optionally return ancestor information.
# 133 : : virtual bool findAncestorByHash(const uint256& block_hash,
# 134 : : const uint256& ancestor_hash,
# 135 : : const FoundBlock& ancestor_out={}) = 0;
# 136 : :
# 137 : : //! Find most recent common ancestor between two blocks and optionally
# 138 : : //! return block information.
# 139 : : virtual bool findCommonAncestor(const uint256& block_hash1,
# 140 : : const uint256& block_hash2,
# 141 : : const FoundBlock& ancestor_out={},
# 142 : : const FoundBlock& block1_out={},
# 143 : : const FoundBlock& block2_out={}) = 0;
# 144 : :
# 145 : : //! Look up unspent output information. Returns coins in the mempool and in
# 146 : : //! the current chain UTXO set. Iterates through all the keys in the map and
# 147 : : //! populates the values.
# 148 : : virtual void findCoins(std::map<COutPoint, Coin>& coins) = 0;
# 149 : :
# 150 : : //! Estimate fraction of total transactions verified if blocks up to
# 151 : : //! the specified block hash are verified.
# 152 : : virtual double guessVerificationProgress(const uint256& block_hash) = 0;
# 153 : :
# 154 : : //! Return true if data is available for all blocks in the specified range
# 155 : : //! of blocks. This checks all blocks that are ancestors of block_hash in
# 156 : : //! the height range from min_height to max_height, inclusive.
# 157 : : virtual bool hasBlocks(const uint256& block_hash, int min_height = 0, std::optional<int> max_height = {}) = 0;
# 158 : :
# 159 : : //! Check if transaction is RBF opt in.
# 160 : : virtual RBFTransactionState isRBFOptIn(const CTransaction& tx) = 0;
# 161 : :
# 162 : : //! Check if transaction is in mempool.
# 163 : : virtual bool isInMempool(const uint256& txid) = 0;
# 164 : :
# 165 : : //! Check if transaction has descendants in mempool.
# 166 : : virtual bool hasDescendantsInMempool(const uint256& txid) = 0;
# 167 : :
# 168 : : //! Transaction is added to memory pool, if the transaction fee is below the
# 169 : : //! amount specified by max_tx_fee, and broadcast to all peers if relay is set to true.
# 170 : : //! Return false if the transaction could not be added due to the fee or for another reason.
# 171 : : virtual bool broadcastTransaction(const CTransactionRef& tx,
# 172 : : const CAmount& max_tx_fee,
# 173 : : bool relay,
# 174 : : std::string& err_string) = 0;
# 175 : :
# 176 : : //! Calculate mempool ancestor and descendant counts for the given transaction.
# 177 : : virtual void getTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) = 0;
# 178 : :
# 179 : : //! Get the node's package limits.
# 180 : : //! Currently only returns the ancestor and descendant count limits, but could be enhanced to
# 181 : : //! return more policy settings.
# 182 : : virtual void getPackageLimits(unsigned int& limit_ancestor_count, unsigned int& limit_descendant_count) = 0;
# 183 : :
# 184 : : //! Check if transaction will pass the mempool's chain limits.
# 185 : : virtual bool checkChainLimits(const CTransactionRef& tx) = 0;
# 186 : :
# 187 : : //! Estimate smart fee.
# 188 : : virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc = nullptr) = 0;
# 189 : :
# 190 : : //! Fee estimator max target.
# 191 : : virtual unsigned int estimateMaxBlocks() = 0;
# 192 : :
# 193 : : //! Mempool minimum fee.
# 194 : : virtual CFeeRate mempoolMinFee() = 0;
# 195 : :
# 196 : : //! Relay current minimum fee (from -minrelaytxfee and -incrementalrelayfee settings).
# 197 : : virtual CFeeRate relayMinFee() = 0;
# 198 : :
# 199 : : //! Relay incremental fee setting (-incrementalrelayfee), reflecting cost of relay.
# 200 : : virtual CFeeRate relayIncrementalFee() = 0;
# 201 : :
# 202 : : //! Relay dust fee setting (-dustrelayfee), reflecting lowest rate it's economical to spend.
# 203 : : virtual CFeeRate relayDustFee() = 0;
# 204 : :
# 205 : : //! Check if any block has been pruned.
# 206 : : virtual bool havePruned() = 0;
# 207 : :
# 208 : : //! Check if the node is ready to broadcast transactions.
# 209 : : virtual bool isReadyToBroadcast() = 0;
# 210 : :
# 211 : : //! Check if in IBD.
# 212 : : virtual bool isInitialBlockDownload() = 0;
# 213 : :
# 214 : : //! Check if shutdown requested.
# 215 : : virtual bool shutdownRequested() = 0;
# 216 : :
# 217 : : //! Get adjusted time.
# 218 : : virtual int64_t getAdjustedTime() = 0;
# 219 : :
# 220 : : //! Send init message.
# 221 : : virtual void initMessage(const std::string& message) = 0;
# 222 : :
# 223 : : //! Send init warning.
# 224 : : virtual void initWarning(const bilingual_str& message) = 0;
# 225 : :
# 226 : : //! Send init error.
# 227 : : virtual void initError(const bilingual_str& message) = 0;
# 228 : :
# 229 : : //! Send progress indicator.
# 230 : : virtual void showProgress(const std::string& title, int progress, bool resume_possible) = 0;
# 231 : :
# 232 : : //! Chain notifications.
# 233 : : class Notifications
# 234 : : {
# 235 : : public:
# 236 : 817 : virtual ~Notifications() {}
# 237 : 0 : virtual void transactionAddedToMempool(const CTransactionRef& tx, uint64_t mempool_sequence) {}
# 238 : 0 : virtual void transactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) {}
# 239 : 0 : virtual void blockConnected(const CBlock& block, int height) {}
# 240 : 0 : virtual void blockDisconnected(const CBlock& block, int height) {}
# 241 : 0 : virtual void updatedBlockTip() {}
# 242 : 0 : virtual void chainStateFlushed(const CBlockLocator& locator) {}
# 243 : : };
# 244 : :
# 245 : : //! Register handler for notifications.
# 246 : : virtual std::unique_ptr<Handler> handleNotifications(std::shared_ptr<Notifications> notifications) = 0;
# 247 : :
# 248 : : //! Wait for pending notifications to be processed unless block hash points to the current
# 249 : : //! chain tip.
# 250 : : virtual void waitForNotificationsIfTipChanged(const uint256& old_tip) = 0;
# 251 : :
# 252 : : //! Register handler for RPC. Command is not copied, so reference
# 253 : : //! needs to remain valid until Handler is disconnected.
# 254 : : virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) = 0;
# 255 : :
# 256 : : //! Check if deprecated RPC is enabled.
# 257 : : virtual bool rpcEnableDeprecated(const std::string& method) = 0;
# 258 : :
# 259 : : //! Run function after given number of seconds. Cancel any previous calls with same name.
# 260 : : virtual void rpcRunLater(const std::string& name, std::function<void()> fn, int64_t seconds) = 0;
# 261 : :
# 262 : : //! Current RPC serialization flags.
# 263 : : virtual int rpcSerializationFlags() = 0;
# 264 : :
# 265 : : //! Return <datadir>/settings.json setting value.
# 266 : : virtual util::SettingsValue getRwSetting(const std::string& name) = 0;
# 267 : :
# 268 : : //! Write a setting to <datadir>/settings.json.
# 269 : : virtual bool updateRwSetting(const std::string& name, const util::SettingsValue& value) = 0;
# 270 : :
# 271 : : //! Synchronously send transactionAddedToMempool notifications about all
# 272 : : //! current mempool transactions to the specified handler and return after
# 273 : : //! the last one is sent. These notifications aren't coordinated with async
# 274 : : //! notifications sent by handleNotifications, so out of date async
# 275 : : //! notifications from handleNotifications can arrive during and after
# 276 : : //! synchronous notifications from requestMempoolTransactions. Clients need
# 277 : : //! to be prepared to handle this by ignoring notifications about unknown
# 278 : : //! removed transactions and already added new transactions.
# 279 : : virtual void requestMempoolTransactions(Notifications& notifications) = 0;
# 280 : : };
# 281 : :
# 282 : : //! Interface to let node manage chain clients (wallets, or maybe tools for
# 283 : : //! monitoring and analysis in the future).
# 284 : : class ChainClient
# 285 : : {
# 286 : : public:
# 287 : 1520 : virtual ~ChainClient() {}
# 288 : :
# 289 : : //! Register rpcs.
# 290 : : virtual void registerRpcs() = 0;
# 291 : :
# 292 : : //! Check for errors before loading.
# 293 : : virtual bool verify() = 0;
# 294 : :
# 295 : : //! Load saved state.
# 296 : : virtual bool load() = 0;
# 297 : :
# 298 : : //! Start client execution and provide a scheduler.
# 299 : : virtual void start(CScheduler& scheduler) = 0;
# 300 : :
# 301 : : //! Save state to disk.
# 302 : : virtual void flush() = 0;
# 303 : :
# 304 : : //! Shut down client.
# 305 : : virtual void stop() = 0;
# 306 : :
# 307 : : //! Set mock time.
# 308 : : virtual void setMockTime(int64_t time) = 0;
# 309 : : };
# 310 : :
# 311 : : //! Return implementation of Chain interface.
# 312 : : std::unique_ptr<Chain> MakeChain(NodeContext& node);
# 313 : :
# 314 : : } // namespace interfaces
# 315 : :
# 316 : : #endif // BITCOIN_INTERFACES_CHAIN_H
|