mining.h raw

   1  // Copyright (c) 2024 The Limenka 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 LIMENKA_INTERFACES_MINING_H
   6  #define LIMENKA_INTERFACES_MINING_H
   7  
   8  #include <consensus/amount.h>       // for CAmount
   9  #include <interfaces/types.h>       // for BlockRef
  10  #include <node/types.h>             // for BlockCreateOptions
  11  #include <primitives/block.h>       // for CBlock, CBlockHeader
  12  #include <primitives/transaction.h> // for CTransactionRef
  13  #include <stdint.h>                 // for int64_t
  14  #include <uint256.h>                // for uint256
  15  #include <util/time.h>              // for MillisecondsDouble
  16  
  17  #include <memory>   // for unique_ptr, shared_ptr
  18  #include <optional> // for optional
  19  #include <vector>   // for vector
  20  
  21  namespace node {
  22  struct NodeContext;
  23  } // namespace node
  24  
  25  class BlockValidationState;
  26  class CScript;
  27  
  28  namespace interfaces {
  29  
  30  //! Block template interface
  31  class BlockTemplate
  32  {
  33  public:
  34      virtual ~BlockTemplate() = default;
  35  
  36      virtual const CBlockHeader& getBlockHeader() const = 0;
  37      virtual const CBlock& getBlock() const = 0;
  38  
  39      virtual const std::vector<CAmount>& getTxFees() const = 0;
  40      virtual const std::vector<int64_t>& getTxSigops() const = 0;
  41      virtual const std::vector<double>& getTxCoinAgePriorities() const = 0;
  42  
  43      virtual CTransactionRef getCoinbaseTx() const = 0;
  44      virtual const std::vector<unsigned char>& getCoinbaseCommitment() const = 0;
  45      virtual int getWitnessCommitmentIndex() const = 0;
  46  
  47      /**
  48       * Compute merkle path to the coinbase transaction
  49       *
  50       * @return merkle path ordered from the deepest
  51       */
  52      virtual std::vector<uint256> getCoinbaseMerklePath() const = 0;
  53  
  54      /**
  55       * Construct and broadcast the block.
  56       *
  57       * @returns if the block was processed, independent of block validity
  58       */
  59      virtual bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CTransactionRef coinbase) = 0;
  60  };
  61  
  62  //! Interface giving clients (RPC, Stratum v2 Template Provider in the future)
  63  //! ability to create block templates.
  64  class Mining
  65  {
  66  public:
  67      virtual ~Mining() = default;
  68  
  69      //! If this chain is exclusively used for testing
  70      virtual bool isTestChain() = 0;
  71  
  72      //! Returns whether IBD is still in progress.
  73      virtual bool isInitialBlockDownload() = 0;
  74  
  75      //! Returns the hash and height for the tip of this chain
  76      virtual std::optional<BlockRef> getTip() = 0;
  77  
  78      /**
  79       * Waits for the connected tip to change. During node initialization, this will
  80       * wait until the tip is connected.
  81       *
  82       * @param[in] current_tip block hash of the current chain tip. Function waits
  83       *                        for the chain tip to differ from this.
  84       * @param[in] timeout     how long to wait for a new tip
  85       * @returns               Hash and height of the current chain tip after this call.
  86       */
  87      virtual std::optional<BlockRef> waitTipChanged(uint256 current_tip, MillisecondsDouble timeout = MillisecondsDouble::max()) = 0;
  88  
  89     /**
  90       * Construct a new block template. For the createNewBlock variant, subclass options (if any) are silently lost and overridden by any config args. For createNewBlock2, the options are assumed to be complete.
  91       *
  92       * @param[in] options options for creating the block
  93       * @returns a block template
  94       */
  95      virtual std::unique_ptr<BlockTemplate> createNewBlock(const node::BlockCreateOptions& options = {}) = 0;
  96      virtual std::unique_ptr<BlockTemplate> createNewBlock2(const node::BlockCreateOptions& assemble_options) = 0;
  97  
  98      //! Get internal node context. Useful for RPC and testing,
  99      //! but not accessible across processes.
 100      virtual node::NodeContext* context() { return nullptr; }
 101  };
 102  
 103  //! Return implementation of Mining interface.
 104  std::unique_ptr<Mining> MakeMining(node::NodeContext& node);
 105  
 106  } // namespace interfaces
 107  
 108  #endif // LIMENKA_INTERFACES_MINING_H
 109