text: // Copyright (c) 2017-2019 The Bitcoin Core developers
  // Distributed under the MIT software license, see the accompanying
  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  
  #ifndef BITCOIN_RPC_BLOCKCHAIN_H
  #define BITCOIN_RPC_BLOCKCHAIN_H
  
  #include <amount.h>
  #include <sync.h>
  
  #include <stdint.h>
  #include <vector>
  
  extern RecursiveMutex cs_main;
  
  class CBlock;
  class CBlockIndex;
  class CTxMemPool;
  class UniValue;
  struct NodeContext;
  
  class JSONRPCRequest;
  
  static constexpr int NUM_GETBLOCKSTATS_PERCENTILES = 5;
  
  /**
   * Returns the numeric difficulty for the given nBits.
   */
  double GetDifficultyForBits(uint32_t nBits);
  
  /** Callback for when block tip changed. */
  void RPCNotifyBlockChange(bool ibd, const CBlockIndex *);
  
  /** Block description to JSON */
  UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails = false) LOCKS_EXCLUDED(cs_main);
  
  /** Mempool information to JSON */
  UniValue MempoolInfoToJSON(const CTxMemPool& pool);
  
  /** Mempool to JSON */
  UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose = false);
  
  /** Block header to JSON */
  UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex) LOCKS_EXCLUDED(cs_main);
  
  /** Used by getblockstats to get feerates at different percentiles by weight  */
  void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES], std::vector<std::pair<CAmount, int64_t>>& scores, int64_t total_weight);
  
  //! Pointer to node state that needs to be declared as a global to be accessible
  //! RPC methods. Due to limitations of the RPC framework, there's currently no
  //! direct way to pass in state to RPC methods without globals.
  extern NodeContext* g_rpc_node;
  
  CTxMemPool& EnsureMemPool();
  
  UniValue getdifficulty(const JSONRPCRequest& request);
  
  #endif
  