|
| 1 | +// Copyright (c) 2024 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_MINING_H |
| 6 | +#define BITCOIN_INTERFACES_MINING_H |
| 7 | + |
| 8 | +#include <optional> |
| 9 | +#include <uint256.h> |
| 10 | + |
| 11 | +namespace node { |
| 12 | +struct CBlockTemplate; |
| 13 | +struct NodeContext; |
| 14 | +} // namespace node |
| 15 | + |
| 16 | +class BlockValidationState; |
| 17 | +class CBlock; |
| 18 | +class CScript; |
| 19 | + |
| 20 | +namespace interfaces { |
| 21 | + |
| 22 | +//! Interface giving clients (RPC, Stratum v2 Template Provider in the future) |
| 23 | +//! ability to create block templates. |
| 24 | + |
| 25 | +class Mining |
| 26 | +{ |
| 27 | +public: |
| 28 | + virtual ~Mining() {} |
| 29 | + |
| 30 | + //! If this chain is exclusively used for testing |
| 31 | + virtual bool isTestChain() = 0; |
| 32 | + |
| 33 | + //! Returns whether IBD is still in progress. |
| 34 | + virtual bool isInitialBlockDownload() = 0; |
| 35 | + |
| 36 | + //! Returns the hash for the tip of this chain |
| 37 | + virtual std::optional<uint256> getTipHash() = 0; |
| 38 | + |
| 39 | + /** |
| 40 | + * Construct a new block template |
| 41 | + * |
| 42 | + * @param[in] script_pub_key the coinbase output |
| 43 | + * @param[in] use_mempool set false to omit mempool transactions |
| 44 | + * @returns a block template |
| 45 | + */ |
| 46 | + virtual std::unique_ptr<node::CBlockTemplate> createNewBlock(const CScript& script_pub_key, bool use_mempool = true) = 0; |
| 47 | + /** |
| 48 | + * Processes new block. A valid new block is automatically relayed to peers. |
| 49 | + * |
| 50 | + * @param[in] block The block we want to process. |
| 51 | + * @param[out] new_block A boolean which is set to indicate if the block was first received via this call |
| 52 | + * @returns If the block was processed, independently of block validity |
| 53 | + */ |
| 54 | + virtual bool processNewBlock(const std::shared_ptr<const CBlock>& block, bool* new_block) = 0; |
| 55 | + |
| 56 | + //! Return the number of transaction updates in the mempool, |
| 57 | + //! used to decide whether to make a new block template. |
| 58 | + virtual unsigned int getTransactionsUpdated() = 0; |
| 59 | + |
| 60 | + /** |
| 61 | + * Check a block is completely valid from start to finish. |
| 62 | + * Only works on top of our current best block. |
| 63 | + * Does not check proof-of-work. |
| 64 | + * |
| 65 | + * @param[out] state details of why a block failed to validate |
| 66 | + * @param[in] block the block to validate |
| 67 | + * @param[in] check_merkle_root call CheckMerkleRoot() |
| 68 | + * @returns false if any of the checks fail |
| 69 | + */ |
| 70 | + virtual bool testBlockValidity(BlockValidationState& state, const CBlock& block, bool check_merkle_root = true) = 0; |
| 71 | + |
| 72 | + //! Get internal node context. Useful for RPC and testing, |
| 73 | + //! but not accessible across processes. |
| 74 | + virtual node::NodeContext* context() { return nullptr; } |
| 75 | +}; |
| 76 | + |
| 77 | +//! Return implementation of Mining interface. |
| 78 | +std::unique_ptr<Mining> MakeMining(node::NodeContext& node); |
| 79 | + |
| 80 | +} // namespace interfaces |
| 81 | + |
| 82 | +#endif // BITCOIN_INTERFACES_MINING_H |
0 commit comments