55#ifndef BITCOIN_INTERFACES_CHAIN_H
66#define BITCOIN_INTERFACES_CHAIN_H
77
8+ #include < optional.h>
9+
810#include < memory>
11+ #include < stdint.h>
912#include < string>
1013#include < vector>
1114
15+ class CBlock ;
1216class CScheduler ;
17+ class uint256 ;
18+ struct CBlockLocator ;
1319
1420namespace interfaces {
1521
@@ -28,6 +34,67 @@ class Chain
2834 {
2935 public:
3036 virtual ~Lock () {}
37+
38+ // ! Get current chain height, not including genesis block (returns 0 if
39+ // ! chain only contains genesis block, nothing if chain does not contain
40+ // ! any blocks).
41+ virtual Optional<int > getHeight () = 0;
42+
43+ // ! Get block height above genesis block. Returns 0 for genesis block,
44+ // ! 1 for following block, and so on. Returns nothing for a block not
45+ // ! included in the current chain.
46+ virtual Optional<int > getBlockHeight (const uint256& hash) = 0;
47+
48+ // ! Get block depth. Returns 1 for chain tip, 2 for preceding block, and
49+ // ! so on. Returns 0 for a block not included in the current chain.
50+ virtual int getBlockDepth (const uint256& hash) = 0;
51+
52+ // ! Get block hash.
53+ virtual uint256 getBlockHash (int height) = 0;
54+
55+ // ! Get block time.
56+ virtual int64_t getBlockTime (int height) = 0;
57+
58+ // ! Get block median time past.
59+ virtual int64_t getBlockMedianTimePast (int height) = 0;
60+
61+ // ! Check that the full block is available on disk (ie has not been
62+ // ! pruned), and contains transactions.
63+ virtual bool haveBlockOnDisk (int height) = 0;
64+
65+ // ! Return height of the first block in the chain with timestamp equal
66+ // ! or greater than the given time, or nothing if there is no block with
67+ // ! a high enough timestamp.
68+ virtual Optional<int > findFirstBlockWithTime (int64_t time) = 0;
69+
70+ // ! Return height of the first block in the chain with timestamp equal
71+ // ! or greater than the given time and height equal or greater than the
72+ // ! given height, or nothing if there is no such block.
73+ // !
74+ // ! Calling this with height 0 is equivalent to calling
75+ // ! findFirstBlockWithTime, but less efficient because it requires a
76+ // ! linear instead of a binary search.
77+ virtual Optional<int > findFirstBlockWithTimeAndHeight (int64_t time, int height) = 0;
78+
79+ // ! Return height of last block in the specified range which is pruned, or
80+ // ! nothing if no block in the range is pruned. Range is inclusive.
81+ virtual Optional<int > findPruned (int start_height = 0 , Optional<int > stop_height = nullopt ) = 0;
82+
83+ // ! Return height of the highest block on the chain that is an ancestor
84+ // ! of the specified block. Also return the height of the specified
85+ // ! block as an optional output parameter.
86+ virtual Optional<int > findFork (const uint256& hash, Optional<int >* height) = 0;
87+
88+ // ! Return true if block hash points to the current chain tip, or to a
89+ // ! possible descendant of the current chain tip that isn't currently
90+ // ! connected.
91+ virtual bool isPotentialTip (const uint256& hash) = 0;
92+
93+ // ! Get locator for the current chain tip.
94+ virtual CBlockLocator getLocator () = 0;
95+
96+ // ! Return height of block on the chain using locator.
97+ virtual Optional<int > findLocatorFork (const CBlockLocator& locator) = 0;
3198 };
3299
33100 // ! Return Lock interface. Chain is locked when this is called, and
@@ -38,6 +105,21 @@ class Chain
38105 // ! method is temporary and is only used in a few places to avoid changing
39106 // ! behavior while code is transitioned to use the Chain::Lock interface.
40107 virtual std::unique_ptr<Lock> assumeLocked () = 0;
108+
109+ // ! Return whether node has the block and optionally return block metadata
110+ // ! or contents.
111+ // !
112+ // ! If a block pointer is provided to retrieve the block contents, and the
113+ // ! block exists but doesn't have data (for example due to pruning), the
114+ // ! block will be empty and all fields set to null.
115+ virtual bool findBlock (const uint256& hash,
116+ CBlock* block = nullptr ,
117+ int64_t * time = nullptr ,
118+ int64_t * max_time = nullptr ) = 0;
119+
120+ // ! Estimate fraction of total transactions verified if blocks up to
121+ // ! given height are verified.
122+ virtual double guessVerificationProgress (const uint256& block_hash) = 0;
41123};
42124
43125// ! Interface to let node manage chain clients (wallets, or maybe tools for
0 commit comments