1 // Copyright (c) 2017-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_RPC_BLOCKCHAIN_H
6 #define BITCOIN_RPC_BLOCKCHAIN_H
7 
8 #include <amount.h>
9 #include <core_io.h>
10 #include <streams.h>
11 #include <sync.h>
12 
13 #include <any>
14 #include <stdint.h>
15 #include <vector>
16 
17 extern RecursiveMutex cs_main;
18 
19 class CBlock;
20 class CBlockIndex;
21 class CBlockPolicyEstimator;
22 class CChainState;
23 class CTxMemPool;
24 class ChainstateManager;
25 class UniValue;
26 struct NodeContext;
27 
28 static constexpr int NUM_GETBLOCKSTATS_PERCENTILES = 5;
29 
30 /**
31  * Get the difficulty of the net wrt to the given block index.
32  *
33  * @return A floating point number that is a multiple of the main net minimum
34  * difficulty (4295032833 hashes).
35  */
36 double GetDifficulty(const CBlockIndex* blockindex);
37 
38 /** Callback for when block tip changed. */
39 void RPCNotifyBlockChange(const CBlockIndex*);
40 
41 /** Block description to JSON */
42 UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails = false) LOCKS_EXCLUDED(cs_main);
43 
44 /** Mempool information to JSON */
45 UniValue MempoolInfoToJSON(const CTxMemPool& pool);
46 
47 /** Mempool to JSON */
48 UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose = false, bool include_mempool_sequence = false);
49 
50 /** Block header to JSON */
51 UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex) LOCKS_EXCLUDED(cs_main);
52 
53 /** Used by getblockstats to get feerates at different percentiles by weight  */
54 void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES], std::vector<std::pair<CAmount, int64_t>>& scores, int64_t total_weight);
55 
56 void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex);
57 void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex = true, int serialize_flags = 0, const CTxUndo* txundo = nullptr);
58 
59 NodeContext& EnsureAnyNodeContext(const std::any& context);
60 CTxMemPool& EnsureMemPool(const NodeContext& node);
61 CTxMemPool& EnsureAnyMemPool(const std::any& context);
62 ChainstateManager& EnsureChainman(const NodeContext& node);
63 ChainstateManager& EnsureAnyChainman(const std::any& context);
EnsureAnyNodeContext(const std::any & context)64 CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node);
65 CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context);
66 
67 /**
68  * Helper to create UTXO snapshots given a chainstate and a file handle.
69  * @return a UniValue map containing metadata about the snapshot.
70  */
71 UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFile& afile);
72 
EnsureMemPool(const NodeContext & node)73 #endif
74