2014-09-02 17:20:09 -07:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-12-16 17:47:57 -08:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
2023-01-23 10:31:54 -08:00
|
|
|
// Copyright (c) 2016-2023 The Zcash developers
|
2014-09-28 23:22:03 -07:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2019-07-18 07:16:09 -07:00
|
|
|
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
2014-09-02 17:20:09 -07:00
|
|
|
|
2014-11-03 07:16:40 -08:00
|
|
|
#ifndef BITCOIN_CHAIN_H
|
|
|
|
#define BITCOIN_CHAIN_H
|
2014-09-02 17:20:09 -07:00
|
|
|
|
2014-12-16 06:43:03 -08:00
|
|
|
#include "arith_uint256.h"
|
2014-11-18 13:03:02 -08:00
|
|
|
#include "primitives/block.h"
|
2014-09-02 17:20:09 -07:00
|
|
|
#include "pow.h"
|
2014-09-24 20:32:36 -07:00
|
|
|
#include "tinyformat.h"
|
2014-09-02 17:20:09 -07:00
|
|
|
#include "uint256.h"
|
2022-10-07 06:40:07 -07:00
|
|
|
#include "util/strencodings.h"
|
2014-09-02 17:20:09 -07:00
|
|
|
|
2020-10-20 17:39:13 -07:00
|
|
|
#include <optional>
|
2014-09-02 17:20:09 -07:00
|
|
|
#include <vector>
|
|
|
|
|
2022-11-13 11:50:56 -08:00
|
|
|
#include <rust/metrics.h>
|
|
|
|
|
2017-11-09 14:09:54 -08:00
|
|
|
static const int SPROUT_VALUE_VERSION = 1001400;
|
2018-05-09 04:15:46 -07:00
|
|
|
static const int SAPLING_VALUE_VERSION = 1010100;
|
2020-04-01 03:52:37 -07:00
|
|
|
static const int CHAIN_HISTORY_ROOT_VERSION = 2010200;
|
2021-06-15 05:21:09 -07:00
|
|
|
static const int NU5_DATA_VERSION = 4050000;
|
2023-01-23 11:31:09 -08:00
|
|
|
static const int TRANSPARENT_VALUE_VERSION = 5040026;
|
2017-11-09 14:09:54 -08:00
|
|
|
|
2020-02-04 11:26:08 -08:00
|
|
|
/**
|
|
|
|
* Maximum amount of time that a block timestamp is allowed to be ahead of the
|
|
|
|
* median-time-past of the previous block.
|
|
|
|
*/
|
|
|
|
static const int64_t MAX_FUTURE_BLOCK_TIME_MTP = 90 * 60;
|
|
|
|
|
2020-02-04 11:46:48 -08:00
|
|
|
/**
|
|
|
|
* Maximum amount of time that a block timestamp is allowed to be ahead of the
|
2020-02-05 08:49:48 -08:00
|
|
|
* current local time.
|
2020-02-04 11:46:48 -08:00
|
|
|
*/
|
2020-02-05 08:49:48 -08:00
|
|
|
static const int64_t MAX_FUTURE_BLOCK_TIME_LOCAL = 2 * 60 * 60;
|
2020-02-04 11:46:48 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Timestamp window used as a grace period by code that compares external
|
|
|
|
* timestamps (such as timestamps passed to RPCs, or wallet key creation times)
|
|
|
|
* to block timestamps.
|
|
|
|
*/
|
2020-02-05 08:49:48 -08:00
|
|
|
static const int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME_LOCAL + 60;
|
2020-02-04 11:46:48 -08:00
|
|
|
|
2020-02-05 08:49:48 -08:00
|
|
|
static_assert(MAX_FUTURE_BLOCK_TIME_LOCAL > MAX_FUTURE_BLOCK_TIME_MTP,
|
|
|
|
"MAX_FUTURE_BLOCK_TIME_LOCAL must be greater than MAX_FUTURE_BLOCK_TIME_MTP");
|
|
|
|
static_assert(TIMESTAMP_WINDOW > MAX_FUTURE_BLOCK_TIME_LOCAL,
|
|
|
|
"TIMESTAMP_WINDOW must be greater than MAX_FUTURE_BLOCK_TIME_LOCAL");
|
2020-02-04 11:46:48 -08:00
|
|
|
|
|
|
|
|
2016-04-05 06:14:48 -07:00
|
|
|
class CBlockFileInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
unsigned int nBlocks; //!< number of blocks stored in file
|
|
|
|
unsigned int nSize; //!< number of used bytes of block file
|
|
|
|
unsigned int nUndoSize; //!< number of used bytes in the undo file
|
|
|
|
unsigned int nHeightFirst; //!< lowest height of block in file
|
|
|
|
unsigned int nHeightLast; //!< highest height of block in file
|
|
|
|
uint64_t nTimeFirst; //!< earliest time of block in file
|
|
|
|
uint64_t nTimeLast; //!< latest time of block in file
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
|
|
|
READWRITE(VARINT(nBlocks));
|
|
|
|
READWRITE(VARINT(nSize));
|
|
|
|
READWRITE(VARINT(nUndoSize));
|
|
|
|
READWRITE(VARINT(nHeightFirst));
|
|
|
|
READWRITE(VARINT(nHeightLast));
|
|
|
|
READWRITE(VARINT(nTimeFirst));
|
|
|
|
READWRITE(VARINT(nTimeLast));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull() {
|
|
|
|
nBlocks = 0;
|
|
|
|
nSize = 0;
|
|
|
|
nUndoSize = 0;
|
|
|
|
nHeightFirst = 0;
|
|
|
|
nHeightLast = 0;
|
|
|
|
nTimeFirst = 0;
|
|
|
|
nTimeLast = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CBlockFileInfo() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToString() const;
|
|
|
|
|
|
|
|
/** update statistics (does not update nSize) */
|
|
|
|
void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn) {
|
|
|
|
if (nBlocks==0 || nHeightFirst > nHeightIn)
|
|
|
|
nHeightFirst = nHeightIn;
|
|
|
|
if (nBlocks==0 || nTimeFirst > nTimeIn)
|
|
|
|
nTimeFirst = nTimeIn;
|
|
|
|
nBlocks++;
|
|
|
|
if (nHeightIn > nHeightLast)
|
|
|
|
nHeightLast = nHeightIn;
|
|
|
|
if (nTimeIn > nTimeLast)
|
|
|
|
nTimeLast = nTimeIn;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-02 17:20:09 -07:00
|
|
|
struct CDiskBlockPos
|
|
|
|
{
|
|
|
|
int nFile;
|
|
|
|
unsigned int nPos;
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
2016-10-28 16:29:17 -07:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
2014-09-02 17:20:09 -07:00
|
|
|
READWRITE(VARINT(nFile));
|
|
|
|
READWRITE(VARINT(nPos));
|
|
|
|
}
|
|
|
|
|
|
|
|
CDiskBlockPos() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
CDiskBlockPos(int nFileIn, unsigned int nPosIn) {
|
|
|
|
nFile = nFileIn;
|
|
|
|
nPos = nPosIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b) {
|
|
|
|
return (a.nFile == b.nFile && a.nPos == b.nPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull() { nFile = -1; nPos = 0; }
|
|
|
|
bool IsNull() const { return (nFile == -1); }
|
2015-01-26 00:47:59 -08:00
|
|
|
|
|
|
|
std::string ToString() const
|
|
|
|
{
|
|
|
|
return strprintf("CBlockDiskPos(nFile=%i, nPos=%i)", nFile, nPos);
|
|
|
|
}
|
|
|
|
|
2014-09-02 17:20:09 -07:00
|
|
|
};
|
|
|
|
|
2016-04-28 04:35:16 -07:00
|
|
|
enum BlockStatus: uint32_t {
|
2014-10-25 01:46:54 -07:00
|
|
|
//! Unused.
|
2014-09-02 17:20:09 -07:00
|
|
|
BLOCK_VALID_UNKNOWN = 0,
|
2014-07-11 15:02:35 -07:00
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future
|
2014-07-11 15:02:35 -07:00
|
|
|
BLOCK_VALID_HEADER = 1,
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! All parent headers found, difficulty matches, timestamp >= median previous, checkpoint. Implies all parents
|
|
|
|
//! are also at least TREE.
|
2014-07-11 15:02:35 -07:00
|
|
|
BLOCK_VALID_TREE = 2,
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
/**
|
|
|
|
* Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid, no duplicate txids,
|
|
|
|
* sigops, size, merkle root. Implies all parents are at least TREE but not necessarily TRANSACTIONS. When all
|
|
|
|
* parent blocks also have TRANSACTIONS, CBlockIndex::nChainTx will be set.
|
|
|
|
*/
|
2014-07-11 15:02:35 -07:00
|
|
|
BLOCK_VALID_TRANSACTIONS = 3,
|
|
|
|
|
2015-04-28 07:47:17 -07:00
|
|
|
//! Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends, BIP30.
|
2014-10-25 01:46:54 -07:00
|
|
|
//! Implies all parents are also at least CHAIN.
|
2014-07-11 15:02:35 -07:00
|
|
|
BLOCK_VALID_CHAIN = 4,
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
|
2014-07-11 15:02:35 -07:00
|
|
|
BLOCK_VALID_SCRIPTS = 5,
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! All validity bits.
|
2014-09-02 17:20:09 -07:00
|
|
|
BLOCK_VALID_MASK = BLOCK_VALID_HEADER | BLOCK_VALID_TREE | BLOCK_VALID_TRANSACTIONS |
|
|
|
|
BLOCK_VALID_CHAIN | BLOCK_VALID_SCRIPTS,
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
BLOCK_HAVE_DATA = 8, //! full block available in blk*.dat
|
|
|
|
BLOCK_HAVE_UNDO = 16, //! undo data available in rev*.dat
|
2014-09-02 17:20:09 -07:00
|
|
|
BLOCK_HAVE_MASK = BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO,
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
BLOCK_FAILED_VALID = 32, //! stage after last reached validness failed
|
|
|
|
BLOCK_FAILED_CHILD = 64, //! descends from failed block
|
2014-09-02 17:20:09 -07:00
|
|
|
BLOCK_FAILED_MASK = BLOCK_FAILED_VALID | BLOCK_FAILED_CHILD,
|
2016-03-18 09:20:12 -07:00
|
|
|
|
2018-01-27 15:37:43 -08:00
|
|
|
BLOCK_ACTIVATES_UPGRADE = 128, //! block activates a network upgrade
|
2014-09-02 17:20:09 -07:00
|
|
|
};
|
|
|
|
|
2018-01-27 15:37:43 -08:00
|
|
|
//! Short-hand for the highest consensus validity we implement.
|
|
|
|
//! Blocks with this validity are assumed to satisfy all consensus rules.
|
|
|
|
static const BlockStatus BLOCK_VALID_CONSENSUS = BLOCK_VALID_SCRIPTS;
|
|
|
|
|
2014-09-02 17:20:09 -07:00
|
|
|
/** The block chain is a tree shaped structure starting with the
|
|
|
|
* genesis block at the root, with each block potentially having multiple
|
|
|
|
* candidates to be the next block. A blockindex may have multiple pprev pointing
|
|
|
|
* to it, but at most one of them can be part of the currently active branch.
|
|
|
|
*/
|
|
|
|
class CBlockIndex
|
|
|
|
{
|
|
|
|
public:
|
2014-11-25 09:54:36 -08:00
|
|
|
//! pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
|
2014-09-02 17:20:09 -07:00
|
|
|
const uint256* phashBlock;
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! pointer to the index of the predecessor of this block
|
2014-09-02 17:20:09 -07:00
|
|
|
CBlockIndex* pprev;
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! pointer to the index of some further predecessor of this block
|
2014-09-02 17:20:09 -07:00
|
|
|
CBlockIndex* pskip;
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! height of the entry in the chain. The genesis block has height 0
|
2014-09-02 17:20:09 -07:00
|
|
|
int nHeight;
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! Which # file this block is stored in (blk?????.dat)
|
2014-09-02 17:20:09 -07:00
|
|
|
int nFile;
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! Byte offset within blk?????.dat where this block's data is stored
|
2014-09-02 17:20:09 -07:00
|
|
|
unsigned int nDataPos;
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! Byte offset within rev?????.dat where this block's undo data is stored
|
2014-09-02 17:20:09 -07:00
|
|
|
unsigned int nUndoPos;
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
|
2014-12-16 06:43:03 -08:00
|
|
|
arith_uint256 nChainWork;
|
2014-09-02 17:20:09 -07:00
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! Number of transactions in this block.
|
|
|
|
//! Note: in a potential headers-first mode, this number cannot be relied upon
|
2014-09-02 17:20:09 -07:00
|
|
|
unsigned int nTx;
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! (memory only) Number of transactions in the chain up to and including this block.
|
|
|
|
//! This value will be non-zero only if and only if transactions for this block and all its parents are available.
|
|
|
|
//! Change to 64-bit type when necessary; won't happen before 2030
|
|
|
|
unsigned int nChainTx;
|
2014-09-02 17:20:09 -07:00
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! Verification status of this block. See enum BlockStatus
|
2014-09-02 17:20:09 -07:00
|
|
|
unsigned int nStatus;
|
|
|
|
|
2018-01-27 15:37:43 -08:00
|
|
|
//! Branch ID corresponding to the consensus rules used to validate this block.
|
2018-02-06 04:39:20 -08:00
|
|
|
//! Only cached if block validity is BLOCK_VALID_CONSENSUS.
|
2018-01-27 15:37:43 -08:00
|
|
|
//! Persisted at each activation height, memory-only for intervening blocks.
|
2020-10-20 17:44:15 -07:00
|
|
|
std::optional<uint32_t> nCachedBranchId;
|
2018-01-27 15:37:43 -08:00
|
|
|
|
2021-06-15 05:21:09 -07:00
|
|
|
//! Root of the ZIP 244 authorizing data commitment tree for this block.
|
|
|
|
//!
|
|
|
|
//! - For blocks prior to (not including) the NU5 activation block, this is always
|
|
|
|
//! null.
|
|
|
|
//! - For blocks including and after the NU5 activation block, this is only set once
|
|
|
|
//! a block has been connected to the main chain, and will be null otherwise.
|
|
|
|
uint256 hashAuthDataRoot;
|
|
|
|
|
2016-10-12 23:02:13 -07:00
|
|
|
//! The anchor for the tree state up to the start of this block
|
2018-04-25 18:15:30 -07:00
|
|
|
uint256 hashSproutAnchor;
|
2016-10-12 23:02:13 -07:00
|
|
|
|
2017-03-09 23:04:14 -08:00
|
|
|
//! (memory only) The anchor for the tree state up to the end of this block
|
2018-05-06 14:21:49 -07:00
|
|
|
uint256 hashFinalSproutRoot;
|
2017-03-09 23:04:14 -08:00
|
|
|
|
2021-03-18 05:34:55 -07:00
|
|
|
//! The change to the chain supply caused by this block. This is defined as
|
2023-01-21 21:35:33 -08:00
|
|
|
//! the value of the coinbase outputs (transparent and shielded) in this block,
|
|
|
|
//! minus fees not claimed by the miner.
|
2021-03-18 05:34:55 -07:00
|
|
|
//!
|
|
|
|
//! Will be std::nullopt under the following conditions:
|
|
|
|
//! - if the block has never been connected to a chain tip
|
|
|
|
//! - for older blocks until a reindex has taken place
|
|
|
|
std::optional<CAmount> nChainSupplyDelta;
|
|
|
|
|
|
|
|
//! (memory only) Total chain supply up to and including this block.
|
|
|
|
//!
|
2023-01-23 11:31:09 -08:00
|
|
|
//! Will be std::nullopt until a reindex has taken place.
|
|
|
|
//! Will be std::nullopt if nChainTx is zero, or if the block has never been
|
|
|
|
//! connected to a chain tip.
|
2021-03-18 05:34:55 -07:00
|
|
|
std::optional<CAmount> nChainTotalSupply;
|
|
|
|
|
|
|
|
//! Change in value in the transparent pool produced by the action of the
|
|
|
|
//! transparent inputs to and outputs from transactions in this block.
|
|
|
|
//!
|
|
|
|
//! Will be std::nullopt for older blocks until a reindex has taken place.
|
|
|
|
std::optional<CAmount> nTransparentValue;
|
|
|
|
|
|
|
|
//! (memory only) Total value of the transparent value pool up to and
|
|
|
|
//! including this block.
|
|
|
|
//!
|
|
|
|
//! Will be std::nullopt until a reindex has taken place.
|
|
|
|
//! Will be std::nullopt if nChainTx is zero.
|
|
|
|
std::optional<CAmount> nChainTransparentValue;
|
|
|
|
|
2017-11-09 14:09:54 -08:00
|
|
|
//! Change in value held by the Sprout circuit over this block.
|
2020-10-20 17:44:15 -07:00
|
|
|
//! Will be std::nullopt for older blocks on old nodes until a reindex has taken place.
|
|
|
|
std::optional<CAmount> nSproutValue;
|
2017-11-09 14:09:54 -08:00
|
|
|
|
|
|
|
//! (memory only) Total value held by the Sprout circuit up to and including this block.
|
2020-10-20 17:44:15 -07:00
|
|
|
//! Will be std::nullopt for on old nodes until a reindex has taken place.
|
|
|
|
//! Will be std::nullopt if nChainTx is zero.
|
|
|
|
std::optional<CAmount> nChainSproutValue;
|
2017-11-09 14:09:54 -08:00
|
|
|
|
2018-05-09 04:15:46 -07:00
|
|
|
//! Change in value held by the Sapling circuit over this block.
|
2020-10-20 17:44:15 -07:00
|
|
|
//! Not a std::optional because this was added before Sapling activated, so we can
|
2018-05-09 04:15:46 -07:00
|
|
|
//! rely on the invariant that every block before this was added had nSaplingValue = 0.
|
|
|
|
CAmount nSaplingValue;
|
|
|
|
|
|
|
|
//! (memory only) Total value held by the Sapling circuit up to and including this block.
|
2020-10-20 17:44:15 -07:00
|
|
|
//! Will be std::nullopt if nChainTx is zero.
|
|
|
|
std::optional<CAmount> nChainSaplingValue;
|
2018-05-09 04:15:46 -07:00
|
|
|
|
2021-06-15 03:45:24 -07:00
|
|
|
//! Change in value held by the Orchard circuit over this block.
|
|
|
|
//! Not a std::optional because this was added before Orchard activated, so we can
|
|
|
|
//! rely on the invariant that every block before this was added had nOrchardValue = 0.
|
|
|
|
CAmount nOrchardValue;
|
|
|
|
|
|
|
|
//! (memory only) Total value held by the Orchard circuit up to and including this block.
|
2021-06-21 09:08:22 -07:00
|
|
|
//! Will be std::nullopt if and only if nChainTx is zero.
|
2021-06-15 03:45:24 -07:00
|
|
|
std::optional<CAmount> nChainOrchardValue;
|
|
|
|
|
2020-04-16 00:10:39 -07:00
|
|
|
//! Root of the Sapling commitment tree as of the end of this block.
|
2020-04-01 03:52:37 -07:00
|
|
|
//!
|
2020-04-16 00:10:39 -07:00
|
|
|
//! - For blocks prior to (not including) the Heartwood activation block, this is
|
2021-06-14 16:50:17 -07:00
|
|
|
//! always equal to hashBlockCommitments.
|
2020-04-16 00:10:39 -07:00
|
|
|
//! - For blocks including and after the Heartwood activation block, this is only set
|
|
|
|
//! once a block has been connected to the main chain, and will be null otherwise.
|
2020-04-01 03:52:37 -07:00
|
|
|
uint256 hashFinalSaplingRoot;
|
|
|
|
|
2021-06-15 05:21:09 -07:00
|
|
|
//! Root of the Orchard commitment tree as of the end of this block.
|
|
|
|
//!
|
|
|
|
//! - For blocks prior to (not including) the NU5 activation block, this is always
|
|
|
|
//! null.
|
|
|
|
//! - For blocks including and after the NU5 activation block, this is only set
|
|
|
|
//! once a block has been connected to the main chain, and will be null otherwise.
|
|
|
|
uint256 hashFinalOrchardRoot;
|
|
|
|
|
2020-04-16 00:10:39 -07:00
|
|
|
//! Root of the ZIP 221 history tree as of the end of the previous block.
|
2020-04-01 03:52:37 -07:00
|
|
|
//!
|
2020-04-09 14:51:17 -07:00
|
|
|
//! - For blocks prior to and including the Heartwood activation block, this is
|
|
|
|
//! always null.
|
2021-06-15 05:21:09 -07:00
|
|
|
//! - For blocks after (not including) the Heartwood activation block, and prior to
|
|
|
|
//! (not including) the NU5 activation block, this is always equal to
|
|
|
|
//! hashBlockCommitments.
|
|
|
|
//! - For blocks including and after the NU5 activation block, this is only set
|
|
|
|
//! once a block has been connected to the main chain, and will be null otherwise.
|
2020-04-01 03:52:37 -07:00
|
|
|
uint256 hashChainHistoryRoot;
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! block header
|
2014-09-02 17:20:09 -07:00
|
|
|
int nVersion;
|
|
|
|
uint256 hashMerkleRoot;
|
2021-06-14 16:50:17 -07:00
|
|
|
uint256 hashBlockCommitments;
|
2014-09-02 17:20:09 -07:00
|
|
|
unsigned int nTime;
|
|
|
|
unsigned int nBits;
|
2016-02-28 12:19:07 -08:00
|
|
|
uint256 nNonce;
|
2022-10-07 06:40:07 -07:00
|
|
|
protected:
|
|
|
|
// The Equihash solution, if it is stored. Once we know that the block index
|
|
|
|
// entry is present in leveldb, this field can be cleared via the TrimSolution
|
|
|
|
// method to save memory.
|
2016-08-13 20:04:13 -07:00
|
|
|
std::vector<unsigned char> nSolution;
|
2014-09-02 17:20:09 -07:00
|
|
|
|
2022-10-07 06:40:07 -07:00
|
|
|
public:
|
2014-10-25 01:46:54 -07:00
|
|
|
//! (memory only) Sequential id assigned to distinguish order in which blocks are received.
|
2014-09-02 17:20:09 -07:00
|
|
|
uint32_t nSequenceId;
|
|
|
|
|
|
|
|
void SetNull()
|
|
|
|
{
|
|
|
|
phashBlock = NULL;
|
|
|
|
pprev = NULL;
|
|
|
|
pskip = NULL;
|
|
|
|
nHeight = 0;
|
|
|
|
nFile = 0;
|
|
|
|
nDataPos = 0;
|
|
|
|
nUndoPos = 0;
|
2014-12-16 06:43:03 -08:00
|
|
|
nChainWork = arith_uint256();
|
2014-09-02 17:20:09 -07:00
|
|
|
nTx = 0;
|
|
|
|
nChainTx = 0;
|
|
|
|
nStatus = 0;
|
2020-10-20 17:44:15 -07:00
|
|
|
nCachedBranchId = std::nullopt;
|
2021-06-15 05:21:09 -07:00
|
|
|
hashAuthDataRoot = uint256();
|
2018-04-25 18:15:30 -07:00
|
|
|
hashSproutAnchor = uint256();
|
2018-05-06 14:21:49 -07:00
|
|
|
hashFinalSproutRoot = uint256();
|
2021-06-15 05:21:09 -07:00
|
|
|
hashFinalSaplingRoot = uint256();
|
|
|
|
hashFinalOrchardRoot = uint256();
|
|
|
|
hashChainHistoryRoot = uint256();
|
2014-09-02 17:20:09 -07:00
|
|
|
nSequenceId = 0;
|
2021-03-18 05:34:55 -07:00
|
|
|
|
|
|
|
nChainSupplyDelta = std::nullopt;
|
|
|
|
nChainTotalSupply = std::nullopt;
|
|
|
|
nTransparentValue = std::nullopt;
|
|
|
|
nChainTransparentValue = std::nullopt;
|
2020-10-20 17:44:15 -07:00
|
|
|
nSproutValue = std::nullopt;
|
|
|
|
nChainSproutValue = std::nullopt;
|
2018-05-09 04:15:46 -07:00
|
|
|
nSaplingValue = 0;
|
2020-10-20 17:44:15 -07:00
|
|
|
nChainSaplingValue = std::nullopt;
|
2021-06-15 03:45:24 -07:00
|
|
|
nOrchardValue = 0;
|
|
|
|
nChainOrchardValue = std::nullopt;
|
2014-09-02 17:20:09 -07:00
|
|
|
|
|
|
|
nVersion = 0;
|
2014-12-15 00:11:16 -08:00
|
|
|
hashMerkleRoot = uint256();
|
2021-06-14 16:50:17 -07:00
|
|
|
hashBlockCommitments = uint256();
|
2014-09-02 17:20:09 -07:00
|
|
|
nTime = 0;
|
|
|
|
nBits = 0;
|
2016-02-28 12:19:07 -08:00
|
|
|
nNonce = uint256();
|
|
|
|
nSolution.clear();
|
2014-09-02 17:20:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CBlockIndex()
|
|
|
|
{
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
2014-07-11 15:02:35 -07:00
|
|
|
CBlockIndex(const CBlockHeader& block)
|
2014-09-02 17:20:09 -07:00
|
|
|
{
|
|
|
|
SetNull();
|
|
|
|
|
|
|
|
nVersion = block.nVersion;
|
|
|
|
hashMerkleRoot = block.hashMerkleRoot;
|
2021-06-14 16:50:17 -07:00
|
|
|
hashBlockCommitments = block.hashBlockCommitments;
|
2014-09-02 17:20:09 -07:00
|
|
|
nTime = block.nTime;
|
|
|
|
nBits = block.nBits;
|
|
|
|
nNonce = block.nNonce;
|
2016-02-28 12:19:07 -08:00
|
|
|
nSolution = block.nSolution;
|
2022-11-13 11:50:56 -08:00
|
|
|
MetricsIncrementCounter("zcashd.debug.memory.allocated_equihash_solutions");
|
2014-09-02 17:20:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CDiskBlockPos GetBlockPos() const {
|
|
|
|
CDiskBlockPos ret;
|
|
|
|
if (nStatus & BLOCK_HAVE_DATA) {
|
|
|
|
ret.nFile = nFile;
|
|
|
|
ret.nPos = nDataPos;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CDiskBlockPos GetUndoPos() const {
|
|
|
|
CDiskBlockPos ret;
|
|
|
|
if (nStatus & BLOCK_HAVE_UNDO) {
|
|
|
|
ret.nFile = nFile;
|
|
|
|
ret.nPos = nUndoPos;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-10-07 06:40:07 -07:00
|
|
|
//! Get the block header for this block index. Requires cs_main.
|
|
|
|
CBlockHeader GetBlockHeader() const;
|
|
|
|
|
|
|
|
//! Clear the Equihash solution to save memory. Requires cs_main.
|
|
|
|
void TrimSolution();
|
2014-09-02 17:20:09 -07:00
|
|
|
|
|
|
|
uint256 GetBlockHash() const
|
|
|
|
{
|
2022-10-13 12:13:19 -07:00
|
|
|
assert(phashBlock);
|
2014-09-02 17:20:09 -07:00
|
|
|
return *phashBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t GetBlockTime() const
|
|
|
|
{
|
|
|
|
return (int64_t)nTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum { nMedianTimeSpan=11 };
|
|
|
|
|
|
|
|
int64_t GetMedianTimePast() const
|
|
|
|
{
|
|
|
|
int64_t pmedian[nMedianTimeSpan];
|
|
|
|
int64_t* pbegin = &pmedian[nMedianTimeSpan];
|
|
|
|
int64_t* pend = &pmedian[nMedianTimeSpan];
|
|
|
|
|
|
|
|
const CBlockIndex* pindex = this;
|
|
|
|
for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev)
|
|
|
|
*(--pbegin) = pindex->GetBlockTime();
|
|
|
|
|
|
|
|
std::sort(pbegin, pend);
|
|
|
|
return pbegin[(pend - pbegin)/2];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToString() const
|
|
|
|
{
|
2022-10-07 06:40:07 -07:00
|
|
|
return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s, HasSolution=%s)",
|
2014-09-02 17:20:09 -07:00
|
|
|
pprev, nHeight,
|
|
|
|
hashMerkleRoot.ToString(),
|
2022-10-07 06:40:07 -07:00
|
|
|
phashBlock ? GetBlockHash().ToString() : "(nil)",
|
|
|
|
HasSolution());
|
2014-09-02 17:20:09 -07:00
|
|
|
}
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! Check whether this block index entry is valid up to the passed validity level.
|
2014-09-02 17:20:09 -07:00
|
|
|
bool IsValid(enum BlockStatus nUpTo = BLOCK_VALID_TRANSACTIONS) const
|
|
|
|
{
|
|
|
|
assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
|
|
|
|
if (nStatus & BLOCK_FAILED_MASK)
|
|
|
|
return false;
|
|
|
|
return ((nStatus & BLOCK_VALID_MASK) >= nUpTo);
|
|
|
|
}
|
|
|
|
|
2022-10-07 06:40:07 -07:00
|
|
|
//! Is the Equihash solution stored?
|
|
|
|
bool HasSolution() const
|
|
|
|
{
|
|
|
|
return !nSolution.empty();
|
|
|
|
}
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! Raise the validity level of this block index entry.
|
|
|
|
//! Returns true if the validity was changed.
|
2014-09-02 17:20:09 -07:00
|
|
|
bool RaiseValidity(enum BlockStatus nUpTo)
|
|
|
|
{
|
|
|
|
assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
|
|
|
|
if (nStatus & BLOCK_FAILED_MASK)
|
|
|
|
return false;
|
|
|
|
if ((nStatus & BLOCK_VALID_MASK) < nUpTo) {
|
|
|
|
nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! Build the skiplist pointer for this entry.
|
2014-09-02 17:20:09 -07:00
|
|
|
void BuildSkip();
|
|
|
|
|
2014-10-25 01:46:54 -07:00
|
|
|
//! Efficiently find an ancestor of this block.
|
2014-09-02 17:20:09 -07:00
|
|
|
CBlockIndex* GetAncestor(int height);
|
|
|
|
const CBlockIndex* GetAncestor(int height) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Used to marshal pointers into hashes for db storage. */
|
|
|
|
class CDiskBlockIndex : public CBlockIndex
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
uint256 hashPrev;
|
|
|
|
|
2020-05-04 19:59:22 -07:00
|
|
|
// This is the serialized `nVersion` of the block index, which is only set
|
|
|
|
// after the (de)serialization routine is called. This should only be used
|
|
|
|
// in LoadBlockIndexGuts (which is the only place where we read block index
|
|
|
|
// objects from disk anyway).
|
|
|
|
int nClientVersion = 0;
|
|
|
|
|
2014-09-02 17:20:09 -07:00
|
|
|
CDiskBlockIndex() {
|
2014-12-15 00:11:16 -08:00
|
|
|
hashPrev = uint256();
|
2014-09-02 17:20:09 -07:00
|
|
|
}
|
|
|
|
|
2022-10-07 06:40:07 -07:00
|
|
|
explicit CDiskBlockIndex(const CBlockIndex* pindex, std::function<std::vector<unsigned char>()> getSolution) : CBlockIndex(*pindex) {
|
2014-12-15 00:11:16 -08:00
|
|
|
hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
|
2022-10-07 06:40:07 -07:00
|
|
|
if (!HasSolution()) {
|
|
|
|
nSolution = getSolution();
|
|
|
|
}
|
2014-09-02 17:20:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
2016-10-28 16:29:17 -07:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
|
|
|
int nVersion = s.GetVersion();
|
|
|
|
if (!(s.GetType() & SER_GETHASH))
|
2014-09-02 17:20:09 -07:00
|
|
|
READWRITE(VARINT(nVersion));
|
2020-05-04 19:59:22 -07:00
|
|
|
nClientVersion = nVersion;
|
2014-09-02 17:20:09 -07:00
|
|
|
|
|
|
|
READWRITE(VARINT(nHeight));
|
|
|
|
READWRITE(VARINT(nStatus));
|
|
|
|
READWRITE(VARINT(nTx));
|
|
|
|
if (nStatus & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO))
|
|
|
|
READWRITE(VARINT(nFile));
|
|
|
|
if (nStatus & BLOCK_HAVE_DATA)
|
|
|
|
READWRITE(VARINT(nDataPos));
|
|
|
|
if (nStatus & BLOCK_HAVE_UNDO)
|
|
|
|
READWRITE(VARINT(nUndoPos));
|
2018-02-06 04:39:20 -08:00
|
|
|
if (nStatus & BLOCK_ACTIVATES_UPGRADE) {
|
|
|
|
if (ser_action.ForRead()) {
|
|
|
|
uint32_t branchId;
|
|
|
|
READWRITE(branchId);
|
|
|
|
nCachedBranchId = branchId;
|
|
|
|
} else {
|
|
|
|
// nCachedBranchId must always be set if BLOCK_ACTIVATES_UPGRADE is set.
|
|
|
|
assert(nCachedBranchId);
|
|
|
|
uint32_t branchId = *nCachedBranchId;
|
|
|
|
READWRITE(branchId);
|
|
|
|
}
|
|
|
|
}
|
2018-04-25 18:15:30 -07:00
|
|
|
READWRITE(hashSproutAnchor);
|
2014-09-02 17:20:09 -07:00
|
|
|
|
|
|
|
// block header
|
|
|
|
READWRITE(this->nVersion);
|
|
|
|
READWRITE(hashPrev);
|
|
|
|
READWRITE(hashMerkleRoot);
|
2021-06-14 16:50:17 -07:00
|
|
|
READWRITE(hashBlockCommitments);
|
2014-09-02 17:20:09 -07:00
|
|
|
READWRITE(nTime);
|
|
|
|
READWRITE(nBits);
|
|
|
|
READWRITE(nNonce);
|
2016-02-28 12:19:07 -08:00
|
|
|
READWRITE(nSolution);
|
2017-11-09 14:09:54 -08:00
|
|
|
|
2021-03-18 05:34:55 -07:00
|
|
|
// Only read/write nTransparentValue if the client version used to create
|
|
|
|
// this index was storing them.
|
|
|
|
if ((s.GetType() & SER_DISK) && (nVersion >= TRANSPARENT_VALUE_VERSION)) {
|
|
|
|
READWRITE(nChainSupplyDelta);
|
|
|
|
READWRITE(nTransparentValue);
|
|
|
|
}
|
|
|
|
|
2017-11-09 14:09:54 -08:00
|
|
|
// Only read/write nSproutValue if the client version used to create
|
|
|
|
// this index was storing them.
|
2018-04-15 20:09:19 -07:00
|
|
|
if ((s.GetType() & SER_DISK) && (nVersion >= SPROUT_VALUE_VERSION)) {
|
2017-11-09 14:09:54 -08:00
|
|
|
READWRITE(nSproutValue);
|
|
|
|
}
|
2018-05-09 04:15:46 -07:00
|
|
|
|
|
|
|
// Only read/write nSaplingValue if the client version used to create
|
|
|
|
// this index was storing them.
|
|
|
|
if ((s.GetType() & SER_DISK) && (nVersion >= SAPLING_VALUE_VERSION)) {
|
|
|
|
READWRITE(nSaplingValue);
|
|
|
|
}
|
2018-11-16 07:13:17 -08:00
|
|
|
|
2020-04-01 03:52:37 -07:00
|
|
|
// Only read/write hashFinalSaplingRoot and hashChainHistoryRoot if the
|
|
|
|
// client version used to create this index was storing them.
|
|
|
|
if ((s.GetType() & SER_DISK) && (nVersion >= CHAIN_HISTORY_ROOT_VERSION)) {
|
|
|
|
READWRITE(hashFinalSaplingRoot);
|
|
|
|
READWRITE(hashChainHistoryRoot);
|
|
|
|
} else if (ser_action.ForRead()) {
|
|
|
|
// For block indices written before the client was Heartwood-aware,
|
|
|
|
// these are always identical.
|
2021-06-14 16:50:17 -07:00
|
|
|
hashFinalSaplingRoot = hashBlockCommitments;
|
2020-04-01 03:52:37 -07:00
|
|
|
}
|
|
|
|
|
2021-06-15 05:21:09 -07:00
|
|
|
// Only read/write NU5 data if the client version used to create this
|
|
|
|
// index was storing them. For block indices written before the client
|
|
|
|
// was NU5-aware, these are always null / zero.
|
|
|
|
if ((s.GetType() & SER_DISK) && (nVersion >= NU5_DATA_VERSION)) {
|
|
|
|
READWRITE(hashAuthDataRoot);
|
|
|
|
READWRITE(hashFinalOrchardRoot);
|
2021-06-15 03:45:24 -07:00
|
|
|
READWRITE(nOrchardValue);
|
2021-06-15 05:21:09 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 07:13:17 -08:00
|
|
|
// If you have just added new serialized fields above, remember to add
|
|
|
|
// them to CBlockTreeDB::LoadBlockIndexGuts() in txdb.cpp :)
|
2014-09-02 17:20:09 -07:00
|
|
|
}
|
|
|
|
|
2022-10-07 06:40:07 -07:00
|
|
|
//! Get the block header for this block index.
|
|
|
|
CBlockHeader GetBlockHeader() const
|
|
|
|
{
|
|
|
|
CBlockHeader header;
|
|
|
|
header.nVersion = nVersion;
|
|
|
|
header.hashPrevBlock = hashPrev;
|
|
|
|
header.hashMerkleRoot = hashMerkleRoot;
|
|
|
|
header.hashBlockCommitments = hashBlockCommitments;
|
|
|
|
header.nTime = nTime;
|
|
|
|
header.nBits = nBits;
|
|
|
|
header.nNonce = nNonce;
|
|
|
|
header.nSolution = nSolution;
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
2014-09-02 17:20:09 -07:00
|
|
|
uint256 GetBlockHash() const
|
|
|
|
{
|
2022-10-07 06:40:07 -07:00
|
|
|
return GetBlockHeader().GetHash();
|
2014-09-02 17:20:09 -07:00
|
|
|
}
|
|
|
|
|
2022-10-07 06:40:07 -07:00
|
|
|
std::vector<unsigned char> GetSolution() const
|
|
|
|
{
|
|
|
|
assert(HasSolution());
|
|
|
|
return nSolution;
|
|
|
|
}
|
2014-09-02 17:20:09 -07:00
|
|
|
|
|
|
|
std::string ToString() const
|
|
|
|
{
|
|
|
|
std::string str = "CDiskBlockIndex(";
|
|
|
|
str += CBlockIndex::ToString();
|
|
|
|
str += strprintf("\n hashBlock=%s, hashPrev=%s)",
|
|
|
|
GetBlockHash().ToString(),
|
|
|
|
hashPrev.ToString());
|
|
|
|
return str;
|
|
|
|
}
|
2022-10-07 06:40:07 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
//! This method should not be called on a CDiskBlockIndex.
|
|
|
|
void TrimSolution()
|
|
|
|
{
|
|
|
|
assert(!"called CDiskBlockIndex::TrimSolution");
|
|
|
|
}
|
2014-09-02 17:20:09 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/** An in-memory indexed chain of blocks. */
|
|
|
|
class CChain {
|
|
|
|
private:
|
|
|
|
std::vector<CBlockIndex*> vChain;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/** Returns the index entry for the genesis block of this chain, or NULL if none. */
|
|
|
|
CBlockIndex *Genesis() const {
|
|
|
|
return vChain.size() > 0 ? vChain[0] : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the index entry for the tip of this chain, or NULL if none. */
|
|
|
|
CBlockIndex *Tip() const {
|
|
|
|
return vChain.size() > 0 ? vChain[vChain.size() - 1] : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the index entry at a particular height in this chain, or NULL if no such height exists. */
|
|
|
|
CBlockIndex *operator[](int nHeight) const {
|
|
|
|
if (nHeight < 0 || nHeight >= (int)vChain.size())
|
|
|
|
return NULL;
|
|
|
|
return vChain[nHeight];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Compare two chains efficiently. */
|
|
|
|
friend bool operator==(const CChain &a, const CChain &b) {
|
|
|
|
return a.vChain.size() == b.vChain.size() &&
|
|
|
|
a.vChain[a.vChain.size() - 1] == b.vChain[b.vChain.size() - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Efficiently check whether a block is present in this chain. */
|
|
|
|
bool Contains(const CBlockIndex *pindex) const {
|
|
|
|
return (*this)[pindex->nHeight] == pindex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Find the successor of a block in this chain, or NULL if the given index is not found or is the tip. */
|
|
|
|
CBlockIndex *Next(const CBlockIndex *pindex) const {
|
|
|
|
if (Contains(pindex))
|
|
|
|
return (*this)[pindex->nHeight + 1];
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the maximal height in the chain. Is equal to chain.Tip() ? chain.Tip()->nHeight : -1. */
|
|
|
|
int Height() const {
|
2020-08-27 12:50:37 -07:00
|
|
|
return int(vChain.size()) - 1;
|
2014-09-02 17:20:09 -07:00
|
|
|
}
|
|
|
|
|
2014-10-19 18:41:37 -07:00
|
|
|
/** Set/initialize a chain with a given tip. */
|
|
|
|
void SetTip(CBlockIndex *pindex);
|
2014-09-02 17:20:09 -07:00
|
|
|
|
|
|
|
/** Return a CBlockLocator that refers to a block in this chain (by default the tip). */
|
|
|
|
CBlockLocator GetLocator(const CBlockIndex *pindex = NULL) const;
|
|
|
|
|
|
|
|
/** Find the last common block between this chain and a block index entry. */
|
|
|
|
const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
|
|
|
|
};
|
|
|
|
|
2014-11-03 07:16:40 -08:00
|
|
|
#endif // BITCOIN_CHAIN_H
|