Skip to content

Commit e695d85

Browse files
committed
Merge bitcoin/bitcoin#26177: refactor / kernel: Move non-gArgs chainparams functionality to kernel
b3e78dc refactor: Don't use global chainparams in chainstatemanager method (TheCharlatan) 382b692 Split non/kernel chainparams (Carl Dong) edabbc7 Add factory functions for Main/Test/Sig/Reg chainparams (Carl Dong) d938098 Remove UpdateVersionBitsParameters (Carl Dong) 84b8578 Decouple RegTestChainParams from ArgsManager (Carl Dong) 76cd4e7 Decouple SigNetChainParams from ArgsManager (Carl Dong) Pull request description: This pull request is part of the `libbitcoinkernel` project bitcoin/bitcoin#24303 https://github.com/bitcoin/bitcoin/projects/18 and more specifically its "Step 2: Decouple most non-consensus code from libbitcoinkernel". dongcarl is the original author of this patchset, these commits were taken from https://github.com/dongcarl/bitcoin/tree/2022-03-libbitcoinkernel-chainparams-args-only. #### Context The bitcoin kernel library currently relies on code containing user configurations through the `ArgsManager`. This is not optimal, since as a stand-alone library it should not rely on bitcoind's argument parsing logic. Instead, its interfaces should accept control and options structs that control the kernel library's desired configuration. Similar work towards decoupling the `ArgsManager` from the kernel has been done in bitcoin/bitcoin#25290, bitcoin/bitcoin#25487, bitcoin/bitcoin#25527 and bitcoin/bitcoin#25862. #### Changes By moving the `CChainParams` class definition into the kernel and giving it new factory functions `CChainParams::{RegTest,SigNet,Main,TestNet}`it can be constructed without an `ArgsManager` reference, unlike the current factory function `CreateChainParams`. The first few commits remove uses of `ArgsManager` within `CChainParams`. Then the `CChainParams` definition is moved to a new file in the `kernel/` subdirectory. ACKs for top commit: MarcoFalke: re-ACK b3e78dc 🛁 ryanofsky: Code review ACK b3e78dc. Only changes since last review were recent review suggestions. ajtowns: ACK b3e78dc Tree-SHA512: 3835aca1d3e3c75cc3303dd584bab3a77e58f6c678724a5e359fe4b0e17e0763a00931ee6191f516b9fde50496f59cc691f0709c0254206db3863bbf7ab2cacd
2 parents ebb15ea + b3e78dc commit e695d85

12 files changed

+792
-636
lines changed

src/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ BITCOIN_CORE_H = \
176176
interfaces/wallet.h \
177177
kernel/blockmanager_opts.h \
178178
kernel/chain.h \
179+
kernel/chainparams.h \
179180
kernel/chainstatemanager_opts.h \
180181
kernel/checks.h \
181182
kernel/coinstats.h \
@@ -654,6 +655,7 @@ libbitcoin_common_a_SOURCES = \
654655
deploymentinfo.cpp \
655656
external_signer.cpp \
656657
init/common.cpp \
658+
kernel/chainparams.cpp \
657659
key.cpp \
658660
key_io.cpp \
659661
merkleblock.cpp \
@@ -910,6 +912,7 @@ libbitcoinkernel_la_SOURCES = \
910912
hash.cpp \
911913
kernel/chain.cpp \
912914
kernel/checks.cpp \
915+
kernel/chainparams.cpp \
913916
kernel/coinstats.cpp \
914917
kernel/context.cpp \
915918
kernel/cs_main.cpp \

src/bitcoin-chainstate.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//
1212
// It is part of the libbitcoinkernel project.
1313

14+
#include <kernel/chainparams.h>
1415
#include <kernel/checks.h>
1516
#include <kernel/context.h>
1617
#include <kernel/validation_cache_sizes.h>
@@ -52,7 +53,7 @@ int main(int argc, char* argv[])
5253

5354
// SETUP: Misc Globals
5455
SelectParams(CBaseChainParams::MAIN);
55-
const CChainParams& chainparams = Params();
56+
auto chainparams = CChainParams::Main();
5657

5758
kernel::Context kernel_context{};
5859
// We can't use a goto here, but we can use an assert since none of the
@@ -81,7 +82,7 @@ int main(int argc, char* argv[])
8182

8283
// SETUP: Chainstate
8384
const ChainstateManager::Options chainman_opts{
84-
.chainparams = chainparams,
85+
.chainparams = *chainparams,
8586
.datadir = gArgs.GetDataDirNet(),
8687
.adjusted_time_callback = NodeClock::now,
8788
};

src/chainparams.cpp

Lines changed: 36 additions & 499 deletions
Large diffs are not rendered by default.

src/chainparams.h

Lines changed: 4 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -6,146 +6,21 @@
66
#ifndef BITCOIN_CHAINPARAMS_H
77
#define BITCOIN_CHAINPARAMS_H
88

9+
#include <kernel/chainparams.h>
10+
911
#include <chainparamsbase.h>
1012
#include <consensus/params.h>
1113
#include <netaddress.h>
1214
#include <primitives/block.h>
1315
#include <protocol.h>
1416
#include <util/hash_type.h>
1517

18+
#include <cstdint>
1619
#include <memory>
1720
#include <string>
21+
#include <unordered_map>
1822
#include <vector>
1923

20-
typedef std::map<int, uint256> MapCheckpoints;
21-
22-
struct CCheckpointData {
23-
MapCheckpoints mapCheckpoints;
24-
25-
int GetHeight() const {
26-
const auto& final_checkpoint = mapCheckpoints.rbegin();
27-
return final_checkpoint->first /* height */;
28-
}
29-
};
30-
31-
struct AssumeutxoHash : public BaseHash<uint256> {
32-
explicit AssumeutxoHash(const uint256& hash) : BaseHash(hash) {}
33-
};
34-
35-
/**
36-
* Holds configuration for use during UTXO snapshot load and validation. The contents
37-
* here are security critical, since they dictate which UTXO snapshots are recognized
38-
* as valid.
39-
*/
40-
struct AssumeutxoData {
41-
//! The expected hash of the deserialized UTXO set.
42-
const AssumeutxoHash hash_serialized;
43-
44-
//! Used to populate the nChainTx value, which is used during BlockManager::LoadBlockIndex().
45-
//!
46-
//! We need to hardcode the value here because this is computed cumulatively using block data,
47-
//! which we do not necessarily have at the time of snapshot load.
48-
const unsigned int nChainTx;
49-
};
50-
51-
using MapAssumeutxo = std::map<int, const AssumeutxoData>;
52-
53-
/**
54-
* Holds various statistics on transactions within a chain. Used to estimate
55-
* verification progress during chain sync.
56-
*
57-
* See also: CChainParams::TxData, GuessVerificationProgress.
58-
*/
59-
struct ChainTxData {
60-
int64_t nTime; //!< UNIX timestamp of last known number of transactions
61-
int64_t nTxCount; //!< total number of transactions between genesis and that timestamp
62-
double dTxRate; //!< estimated number of transactions per second after that timestamp
63-
};
64-
65-
/**
66-
* CChainParams defines various tweakable parameters of a given instance of the
67-
* Bitcoin system.
68-
*/
69-
class CChainParams
70-
{
71-
public:
72-
enum Base58Type {
73-
PUBKEY_ADDRESS,
74-
SCRIPT_ADDRESS,
75-
SECRET_KEY,
76-
EXT_PUBLIC_KEY,
77-
EXT_SECRET_KEY,
78-
79-
MAX_BASE58_TYPES
80-
};
81-
82-
const Consensus::Params& GetConsensus() const { return consensus; }
83-
const CMessageHeader::MessageStartChars& MessageStart() const { return pchMessageStart; }
84-
uint16_t GetDefaultPort() const { return nDefaultPort; }
85-
uint16_t GetDefaultPort(Network net) const
86-
{
87-
return net == NET_I2P ? I2P_SAM31_PORT : GetDefaultPort();
88-
}
89-
uint16_t GetDefaultPort(const std::string& addr) const
90-
{
91-
CNetAddr a;
92-
return a.SetSpecial(addr) ? GetDefaultPort(a.GetNetwork()) : GetDefaultPort();
93-
}
94-
95-
const CBlock& GenesisBlock() const { return genesis; }
96-
/** Default value for -checkmempool and -checkblockindex argument */
97-
bool DefaultConsistencyChecks() const { return fDefaultConsistencyChecks; }
98-
/** Policy: Filter transactions that do not match well-defined patterns */
99-
bool RequireStandard() const { return fRequireStandard; }
100-
/** If this chain is exclusively used for testing */
101-
bool IsTestChain() const { return m_is_test_chain; }
102-
/** If this chain allows time to be mocked */
103-
bool IsMockableChain() const { return m_is_mockable_chain; }
104-
uint64_t PruneAfterHeight() const { return nPruneAfterHeight; }
105-
/** Minimum free space (in GB) needed for data directory */
106-
uint64_t AssumedBlockchainSize() const { return m_assumed_blockchain_size; }
107-
/** Minimum free space (in GB) needed for data directory when pruned; Does not include prune target*/
108-
uint64_t AssumedChainStateSize() const { return m_assumed_chain_state_size; }
109-
/** Whether it is possible to mine blocks on demand (no retargeting) */
110-
bool MineBlocksOnDemand() const { return consensus.fPowNoRetargeting; }
111-
/** Return the network string */
112-
std::string NetworkIDString() const { return strNetworkID; }
113-
/** Return the list of hostnames to look up for DNS seeds */
114-
const std::vector<std::string>& DNSSeeds() const { return vSeeds; }
115-
const std::vector<unsigned char>& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; }
116-
const std::string& Bech32HRP() const { return bech32_hrp; }
117-
const std::vector<uint8_t>& FixedSeeds() const { return vFixedSeeds; }
118-
const CCheckpointData& Checkpoints() const { return checkpointData; }
119-
120-
//! Get allowed assumeutxo configuration.
121-
//! @see ChainstateManager
122-
const MapAssumeutxo& Assumeutxo() const { return m_assumeutxo_data; }
123-
124-
const ChainTxData& TxData() const { return chainTxData; }
125-
protected:
126-
CChainParams() {}
127-
128-
Consensus::Params consensus;
129-
CMessageHeader::MessageStartChars pchMessageStart;
130-
uint16_t nDefaultPort;
131-
uint64_t nPruneAfterHeight;
132-
uint64_t m_assumed_blockchain_size;
133-
uint64_t m_assumed_chain_state_size;
134-
std::vector<std::string> vSeeds;
135-
std::vector<unsigned char> base58Prefixes[MAX_BASE58_TYPES];
136-
std::string bech32_hrp;
137-
std::string strNetworkID;
138-
CBlock genesis;
139-
std::vector<uint8_t> vFixedSeeds;
140-
bool fDefaultConsistencyChecks;
141-
bool fRequireStandard;
142-
bool m_is_test_chain;
143-
bool m_is_mockable_chain;
144-
CCheckpointData checkpointData;
145-
MapAssumeutxo m_assumeutxo_data;
146-
ChainTxData chainTxData;
147-
};
148-
14924
/**
15025
* Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
15126
* @returns a CChainParams* of the chosen chain.

src/deploymentinfo.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <consensus/params.h>
88

9+
#include <string_view>
10+
911
const struct VBDeploymentInfo VersionBitsDeploymentInfo[Consensus::MAX_VERSION_BITS_DEPLOYMENTS] = {
1012
{
1113
/*.name =*/ "testdummy",
@@ -34,3 +36,19 @@ std::string DeploymentName(Consensus::BuriedDeployment dep)
3436
} // no default case, so the compiler can warn about missing cases
3537
return "";
3638
}
39+
40+
std::optional<Consensus::BuriedDeployment> GetBuriedDeployment(const std::string_view name)
41+
{
42+
if (name == "segwit") {
43+
return Consensus::BuriedDeployment::DEPLOYMENT_SEGWIT;
44+
} else if (name == "bip34") {
45+
return Consensus::BuriedDeployment::DEPLOYMENT_HEIGHTINCB;
46+
} else if (name == "dersig") {
47+
return Consensus::BuriedDeployment::DEPLOYMENT_DERSIG;
48+
} else if (name == "cltv") {
49+
return Consensus::BuriedDeployment::DEPLOYMENT_CLTV;
50+
} else if (name == "csv") {
51+
return Consensus::BuriedDeployment::DEPLOYMENT_CSV;
52+
}
53+
return std::nullopt;
54+
}

src/deploymentinfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <consensus/params.h>
99

10+
#include <optional>
1011
#include <string>
1112

1213
struct VBDeploymentInfo {
@@ -26,4 +27,6 @@ inline std::string DeploymentName(Consensus::DeploymentPos pos)
2627
return VersionBitsDeploymentInfo[pos].name;
2728
}
2829

30+
std::optional<Consensus::BuriedDeployment> GetBuriedDeployment(const std::string_view deployment_name);
31+
2932
#endif // BITCOIN_DEPLOYMENTINFO_H

0 commit comments

Comments
 (0)