Skip to content

Commit 153aa3c

Browse files
DCG-ClaudePastaPastaPasta
authored andcommitted
Merge 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#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#25290, bitcoin#25487, bitcoin#25527 and 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 Dash adaptations: - src/kernel/chainparams.{h,cpp}: moves Dash's CChainParams and network classes (all DIP, LLMQ, masternode and spork parameters), not Bitcoin's - src/kernel/chainparams.{h,cpp}: adds CChainParams::DevNet(DevNetOptions) next to Main()/TestNet()/RegTest() for Dash's fourth network - src/kernel/chainparams.h: VersionBitsParameters, RegTestOptions and DevNetOptions carry Dash's extra -vbparams fields and regtest/devnet overrides - src/kernel/chainparams.cpp: LLMQ overrides arrive as quorum names and are resolved in the kernel constructors, which own the per-chain LLMQ list - src/kernel/chainparams.cpp: the devnet name comes from DevNetOptions::name instead of gArgs.GetDevNetName(), keeping the kernel gArgs-free - src/kernel/chainparams.cpp: the moved classes keep develop's current values (EvoSnapshotHash, V24 bit 13, EVO_SHARES; no fRequireStandard after bitcoin#28354) - src/deploymentinfo.cpp: GetBuriedDeployment() maps the 12 names -testactivationheight already accepted (bip147 ... mn_rr) instead of upstream's segwit - src/chainparams.cpp: Dash's arg readers stay split into ReadRegTest*/ReadDevNet* helpers, called from ReadRegTestArgs()/ReadDevNetArgs() - src/chainparams.cpp: -minimumdifficultyblocks/-highsubsidyblocks/-highsubsidyfactor are read as separate optionals, so they log three lines instead of one - src/validation.h: upstream's <fs.h> is not added; Dash uses util/fs.h - src/active/dkgsession.cpp, src/chainlock/signing.cpp, src/instantsend/net_instantsend.cpp and Dash tests: include <chainparams.h>, which validation.h no longer provides Not applicable to Dash (intentionally omitted): - src/kernel/chainparams.cpp: CSigNetParams and CChainParams::SigNet(); Dash has no signet - src/kernel/chainparams.h: SigNetOptions and the SigNet() declaration; Dash has no signet - src/chainparams.cpp: ReadSigNetArgs() and the SIGNET branch of CreateChainParams(); Dash has no signet - src/kernel/chainparams.cpp: segwit/taproot fields, script_flag_exceptions and bech32_hrp of Bitcoin's chain classes; Dash's classes have none - src/deploymentinfo.cpp: the "segwit" -> DEPLOYMENT_SEGWIT mapping; Dash has no segwit deployment
1 parent 0981a58 commit 153aa3c

23 files changed

Lines changed: 1631 additions & 1377 deletions

‎src/Makefile.am‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ BITCOIN_CORE_H = \
287287
interfaces/wallet.h \
288288
kernel/blockmanager_opts.h \
289289
kernel/chain.h \
290+
kernel/chainparams.h \
290291
kernel/chainstatemanager_opts.h \
291292
kernel/checks.h \
292293
kernel/coinstats.h \
@@ -992,6 +993,7 @@ libbitcoin_common_a_SOURCES = \
992993
external_signer.cpp \
993994
governance/common.cpp \
994995
init/common.cpp \
996+
kernel/chainparams.cpp \
995997
key.cpp \
996998
key_io.cpp \
997999
llmq/core_write.cpp \
@@ -1297,6 +1299,7 @@ libdashkernel_la_SOURCES = \
12971299
instantsend/lock.cpp \
12981300
kernel/chain.cpp \
12991301
kernel/checks.cpp \
1302+
kernel/chainparams.cpp \
13001303
kernel/coinstats.cpp \
13011304
kernel/context.cpp \
13021305
kernel/cs_main.cpp \

‎src/active/dkgsession.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <util/helpers.h>
1717

1818
#include <chain.h>
19+
#include <chainparams.h>
1920
#include <deploymentstatus.h>
2021
#include <validation.h>
2122

‎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>
@@ -61,7 +62,7 @@ int main(int argc, char* argv[])
6162

6263
// SETUP: Misc Globals
6364
SelectParams(CBaseChainParams::MAIN);
64-
const CChainParams& chainparams = Params();
65+
auto chainparams = CChainParams::Main();
6566

6667
kernel::Context kernel_context{};
6768
// We can't use a goto here, but we can use an assert since none of the
@@ -90,7 +91,7 @@ int main(int argc, char* argv[])
9091

9192
// SETUP: Chainstate
9293
const ChainstateManager::Options chainman_opts{
93-
.chainparams = chainparams,
94+
.chainparams = *chainparams,
9495
};
9596
ChainstateManager chainman{chainman_opts};
9697

‎src/chainlock/signing.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <chainlock/clsig.h>
88
#include <chainlock/handler.h>
9+
#include <chainparams.h>
910
#include <instantsend/instantsend.h>
1011
#include <llmq/signing_shares.h>
1112
#include <masternode/sync.h>

‎src/chainparams.cpp‎

Lines changed: 92 additions & 1192 deletions
Large diffs are not rendered by default.

‎src/chainparams.h‎

Lines changed: 4 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
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 <llmq/params.h>
@@ -14,189 +16,13 @@
1416
#include <protocol.h>
1517
#include <util/hash_type.h>
1618

19+
#include <cstdint>
1720
#include <memory>
1821
#include <optional>
1922
#include <string>
23+
#include <unordered_map>
2024
#include <vector>
2125

22-
typedef std::map<int, uint256> MapCheckpoints;
23-
24-
struct CCheckpointData {
25-
MapCheckpoints mapCheckpoints;
26-
27-
int GetHeight() const {
28-
const auto& final_checkpoint = mapCheckpoints.rbegin();
29-
return final_checkpoint->first /* height */;
30-
}
31-
};
32-
33-
struct AssumeutxoHash : public BaseHash<uint256> {
34-
explicit AssumeutxoHash(const uint256& hash) : BaseHash(hash) {}
35-
};
36-
37-
struct EvoSnapshotHash : public BaseHash<uint256> {
38-
explicit EvoSnapshotHash(const uint256& hash) : BaseHash(hash) {}
39-
};
40-
41-
/**
42-
* Holds configuration for use during UTXO snapshot load and validation. The contents
43-
* here are security critical, since they dictate which UTXO snapshots are recognized
44-
* as valid.
45-
*/
46-
struct AssumeutxoData {
47-
//! The expected hash of the deserialized UTXO set.
48-
const AssumeutxoHash hash_serialized;
49-
50-
//! The expected single-SHA256 hash of the canonical Dash evo section.
51-
const EvoSnapshotHash evo_hash;
52-
53-
//! Used to populate the nChainTx value, which is used during BlockManager::LoadBlockIndex().
54-
//!
55-
//! We need to hardcode the value here because this is computed cumulatively using block data,
56-
//! which we do not necessarily have at the time of snapshot load.
57-
const unsigned int nChainTx;
58-
};
59-
60-
using MapAssumeutxo = std::map<int, const AssumeutxoData>;
61-
62-
/**
63-
* Holds various statistics on transactions within a chain. Used to estimate
64-
* verification progress during chain sync.
65-
*
66-
* See also: CChainParams::TxData, GuessVerificationProgress.
67-
*/
68-
struct ChainTxData {
69-
int64_t nTime;
70-
int64_t nTxCount;
71-
double dTxRate;
72-
};
73-
74-
/**
75-
* CChainParams defines various tweakable parameters of a given instance of the
76-
* Dash system.
77-
*/
78-
class CChainParams
79-
{
80-
public:
81-
enum Base58Type {
82-
PUBKEY_ADDRESS,
83-
SCRIPT_ADDRESS,
84-
SECRET_KEY, // BIP16
85-
EXT_PUBLIC_KEY, // BIP32
86-
EXT_SECRET_KEY, // BIP32
87-
88-
MAX_BASE58_TYPES
89-
};
90-
91-
const Consensus::Params& GetConsensus() const { return consensus; }
92-
const CMessageHeader::MessageStartChars& MessageStart() const { return pchMessageStart; }
93-
uint16_t GetDefaultPort() const { return nDefaultPort; }
94-
uint16_t GetDefaultPort(Network net) const
95-
{
96-
return net == NET_I2P ? I2P_SAM31_PORT : GetDefaultPort();
97-
}
98-
uint16_t GetDefaultPort(const std::string& addr) const
99-
{
100-
CNetAddr a;
101-
return a.SetSpecial(addr) ? GetDefaultPort(a.GetNetwork()) : GetDefaultPort();
102-
}
103-
uint16_t GetDefaultPlatformP2PPort() const { return nDefaultPlatformP2PPort; }
104-
uint16_t GetDefaultPlatformHTTPPort() const { return nDefaultPlatformHTTPPort; }
105-
106-
const CBlock& GenesisBlock() const { return genesis; }
107-
const CBlock& DevNetGenesisBlock() const { return devnetGenesis; }
108-
/** Default value for -checkmempool and -checkblockindex argument */
109-
bool DefaultConsistencyChecks() const { return fDefaultConsistencyChecks; }
110-
/** Require addresses specified with "-externalip" parameter to be routable */
111-
bool RequireRoutableExternalIP() const { return fRequireRoutableExternalIP; }
112-
/** If this chain allows time to be mocked */
113-
bool IsMockableChain() const { return m_is_mockable_chain; }
114-
/** If this chain is exclusively used for testing */
115-
bool IsTestChain() const { return m_is_test_chain; }
116-
uint64_t PruneAfterHeight() const { return nPruneAfterHeight; }
117-
/** Minimum free space (in GB) needed for data directory */
118-
uint64_t AssumedBlockchainSize() const { return m_assumed_blockchain_size; }
119-
/** Minimum free space (in GB) needed for data directory when pruned; Does not include prune target*/
120-
uint64_t AssumedChainStateSize() const { return m_assumed_chain_state_size; }
121-
/** Whether it is possible to mine blocks on demand (no retargeting) */
122-
bool MineBlocksOnDemand() const { return consensus.fPowNoRetargeting; }
123-
/** Allow multiple addresses to be selected from the same network group (e.g. 192.168.x.x) */
124-
bool AllowMultipleAddressesFromGroup() const { return fAllowMultipleAddressesFromGroup; }
125-
/** How long to wait until we allow retrying of a LLMQ connection */
126-
int LLMQConnectionRetryTimeout() const { return nLLMQConnectionRetryTimeout; }
127-
/** Return the network string */
128-
std::string NetworkIDString() const { return strNetworkID; }
129-
/** Return the list of hostnames to look up for DNS seeds */
130-
const std::vector<std::string>& DNSSeeds() const { return vSeeds; }
131-
const std::vector<unsigned char>& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; }
132-
/** DIP-18 Platform address bech32m HRP: "dash" on mainnet, "tdash" on test chains */
133-
const std::string& Bech32PlatformHRP() const { return bech32_platform_hrp; }
134-
int ExtCoinType() const { return nExtCoinType; }
135-
const std::vector<uint8_t>& FixedSeeds() const { return vFixedSeeds; }
136-
const CCheckpointData& Checkpoints() const { return checkpointData; }
137-
138-
//! Get allowed assumeutxo configuration.
139-
//! @see ChainstateManager
140-
const MapAssumeutxo& Assumeutxo() const { return m_assumeutxo_data; }
141-
142-
const ChainTxData& TxData() const { return chainTxData; }
143-
void UpdateDIP3Parameters(int nActivationHeight, int nEnforcementHeight);
144-
void UpdateDIP8Parameters(int nActivationHeight);
145-
void UpdateBudgetParameters(int nMasternodePaymentsStartBlock, int nBudgetPaymentsStartBlock, int nSuperblockStartBlock);
146-
void UpdateLLMQInstantSend(Consensus::LLMQType llmqType);
147-
/**
148-
* Validate params for Masternodes EHF
149-
*
150-
* @param[in] nBit The version bit to update
151-
* @param[in] timePast The block time to validate if release is already time-outed
152-
* @return Whether params are legit and params are updated (if release is known)
153-
*/
154-
bool IsValidMNActivation(int nBit, int64_t timePast) const;
155-
int PoolMinParticipants() const { return nPoolMinParticipants; }
156-
int PoolMaxParticipants() const { return nPoolMaxParticipants; }
157-
int FulfilledRequestExpireTime() const { return nFulfilledRequestExpireTime; }
158-
const std::string& SporkAddress() const { return strSporkAddress; }
159-
int CreditPoolPeriodBlocks() const { return nCreditPoolPeriodBlocks; }
160-
[[nodiscard]] std::optional<Consensus::LLMQParams> GetLLMQ(Consensus::LLMQType llmqType) const;
161-
162-
protected:
163-
CChainParams() {}
164-
165-
Consensus::Params consensus;
166-
CMessageHeader::MessageStartChars pchMessageStart;
167-
uint16_t nDefaultPort;
168-
uint64_t nPruneAfterHeight;
169-
uint64_t m_assumed_blockchain_size;
170-
uint64_t m_assumed_chain_state_size;
171-
std::vector<std::string> vSeeds;
172-
std::vector<unsigned char> base58Prefixes[MAX_BASE58_TYPES];
173-
std::string bech32_platform_hrp;
174-
int nExtCoinType;
175-
std::string strNetworkID;
176-
CBlock genesis;
177-
CBlock devnetGenesis;
178-
std::vector<uint8_t> vFixedSeeds;
179-
bool fDefaultConsistencyChecks;
180-
bool fRequireRoutableExternalIP;
181-
bool m_is_test_chain;
182-
bool fAllowMultipleAddressesFromGroup;
183-
bool m_is_mockable_chain;
184-
int nLLMQConnectionRetryTimeout;
185-
CCheckpointData checkpointData;
186-
MapAssumeutxo m_assumeutxo_data;
187-
ChainTxData chainTxData;
188-
int nPoolMinParticipants;
189-
int nPoolMaxParticipants;
190-
int nFulfilledRequestExpireTime;
191-
std::string strSporkAddress;
192-
uint16_t nDefaultPlatformP2PPort;
193-
uint16_t nDefaultPlatformHTTPPort;
194-
/// The number of blocks the credit pool tracks; 576 (one day) on mainnet, reduced on regtest
195-
int nCreditPoolPeriodBlocks;
196-
197-
void AddLLMQ(Consensus::LLMQType llmqType);
198-
};
199-
20026
/**
20127
* Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
20228
* @returns a CChainParams* of the chosen chain.

‎src/deploymentinfo.cpp‎

Lines changed: 32 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",
@@ -58,3 +60,33 @@ std::string DeploymentName(Consensus::BuriedDeployment dep)
5860
} // no default case, so the compiler can warn about missing cases
5961
return "";
6062
}
63+
64+
std::optional<Consensus::BuriedDeployment> GetBuriedDeployment(const std::string_view name)
65+
{
66+
if (name == "bip147") {
67+
return Consensus::BuriedDeployment::DEPLOYMENT_BIP147;
68+
} else if (name == "bip34") {
69+
return Consensus::BuriedDeployment::DEPLOYMENT_HEIGHTINCB;
70+
} else if (name == "dersig") {
71+
return Consensus::BuriedDeployment::DEPLOYMENT_DERSIG;
72+
} else if (name == "cltv") {
73+
return Consensus::BuriedDeployment::DEPLOYMENT_CLTV;
74+
} else if (name == "csv") {
75+
return Consensus::BuriedDeployment::DEPLOYMENT_CSV;
76+
} else if (name == "brr") {
77+
return Consensus::BuriedDeployment::DEPLOYMENT_BRR;
78+
} else if (name == "dip0001") {
79+
return Consensus::BuriedDeployment::DEPLOYMENT_DIP0001;
80+
} else if (name == "dip0008") {
81+
return Consensus::BuriedDeployment::DEPLOYMENT_DIP0008;
82+
} else if (name == "dip0024") {
83+
return Consensus::BuriedDeployment::DEPLOYMENT_DIP0024;
84+
} else if (name == "v19") {
85+
return Consensus::BuriedDeployment::DEPLOYMENT_V19;
86+
} else if (name == "v20") {
87+
return Consensus::BuriedDeployment::DEPLOYMENT_V20;
88+
} else if (name == "mn_rr") {
89+
return Consensus::BuriedDeployment::DEPLOYMENT_MN_RR;
90+
}
91+
return std::nullopt;
92+
}

‎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

‎src/instantsend/net_instantsend.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <bls/bls_batchverifier.h>
88
#include <chainlock/chainlock.h>
9+
#include <chainparams.h>
910
#include <consensus/params.h>
1011
#include <cxxtimer.hpp>
1112
#include <instantsend/instantsend.h>

0 commit comments

Comments
 (0)