1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 The Limenka developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 6 #ifndef LIMENKA_CONSENSUS_PARAMS_H
7 #define LIMENKA_CONSENSUS_PARAMS_H
8 9 #include <uint256.h>
10 11 #include <chrono>
12 #include <cstdint>
13 #include <limits>
14 #include <map>
15 #include <vector>
16 17 namespace Consensus {
18 19 // Fork identifier committed to sighash preimage for replay protection.
20 21 /**
22 * A buried deployment is one where the height of the activation has been hardcoded into
23 * the client implementation long after the consensus change has activated. See BIP 90.
24 */
25 enum BuriedDeployment : int16_t {
26 // buried deployments get negative values to avoid overlap with DeploymentPos
27 DEPLOYMENT_HEIGHTINCB = std::numeric_limits<int16_t>::min(),
28 DEPLOYMENT_CLTV,
29 DEPLOYMENT_DERSIG,
30 DEPLOYMENT_CSV,
31 DEPLOYMENT_SEGWIT,
32 };
33 constexpr bool ValidDeployment(BuriedDeployment dep) { return dep <= DEPLOYMENT_SEGWIT; }
34 35 enum DeploymentPos : uint16_t {
36 DEPLOYMENT_TESTDUMMY,
37 DEPLOYMENT_TAPROOT, // Deployment of Schnorr/Taproot (BIPs 340-342)
38 DEPLOYMENT_REDUCED_DATA, // ReducedData Temporary Softfork (RDTS)
39 DEPLOYMENT_P2SPKH,
40 // NOTE: Also add new deployments to VersionBitsDeploymentInfo in deploymentinfo.cpp
41 MAX_VERSION_BITS_DEPLOYMENTS
42 };
43 constexpr bool ValidDeployment(DeploymentPos dep) { return dep < MAX_VERSION_BITS_DEPLOYMENTS; }
44 45 /**
46 * Struct for each individual consensus rule change using BIP9.
47 */
48 struct BIP9Deployment {
49 /** Bit position to select the particular bit in nVersion. */
50 int bit{28};
51 /** Start MedianTime for version bits miner confirmation. Can be a date in the past */
52 int64_t nStartTime{NEVER_ACTIVE};
53 /** Timeout/expiry MedianTime for the deployment attempt. */
54 int64_t nTimeout{NEVER_ACTIVE};
55 /** If lock in occurs, delay activation until at least this block
56 * height. Note that activation will only occur on a retarget
57 * boundary.
58 */
59 int min_activation_height{0};
60 /** Maximum height for activation. If less than INT_MAX, the deployment will activate
61 * at this height regardless of signaling (similar to BIP8 flag day).
62 * std::numeric_limits<int>::max() means no maximum (activation only via signaling). */
63 int max_activation_height{std::numeric_limits<int>::max()};
64 /** For temporary softforks: number of blocks the deployment remains active after activation.
65 * std::numeric_limits<int>::max() means permanent (never expires). */
66 int active_duration{std::numeric_limits<int>::max()};
67 /** Per-deployment activation threshold. If 0, uses the global nRuleChangeActivationThreshold.
68 * Otherwise, specifies the number of blocks required for this specific deployment. */
69 int threshold{0};
70 71 /** Constant for nTimeout very far in the future. */
72 static constexpr int64_t NO_TIMEOUT = std::numeric_limits<int64_t>::max();
73 74 /** Special value for nStartTime indicating that the deployment is always active.
75 * This is useful for testing, as it means tests don't need to deal with the activation
76 * process (which takes at least 3 BIP9 intervals). Only tests that specifically test the
77 * behaviour during activation cannot use this. */
78 static constexpr int64_t ALWAYS_ACTIVE = -1;
79 80 /** Special value for nStartTime indicating that the deployment is never active.
81 * This is useful for integrating the code changes for a new feature
82 * prior to deploying it on some or all networks. */
83 static constexpr int64_t NEVER_ACTIVE = -2;
84 };
85 86 /**
87 * Parameters that influence chain consensus.
88 */
89 struct Params {
90 uint256 hashGenesisBlock;
91 int nSubsidyHalvingInterval;
92 /**
93 * Hashes of blocks that
94 * - are known to be consensus valid, and
95 * - buried in the chain, and
96 * - fail if the default script verify flags are applied.
97 */
98 std::map<uint256, uint32_t> script_flag_exceptions;
99 /** Block height and hash at which BIP34 becomes active */
100 int BIP34Height;
101 uint256 BIP34Hash;
102 /** Block height at which BIP65 becomes active */
103 int BIP65Height;
104 /** Block height at which BIP66 becomes active */
105 int BIP66Height;
106 /** Block height at which CSV (BIP68, BIP112 and BIP113) becomes active */
107 int CSVHeight;
108 /** Block height at which Segwit (BIP141, BIP143 and BIP147) becomes active.
109 * Note that segwit v0 script rules are enforced on all blocks except the
110 * BIP 16 exception blocks. */
111 int SegwitHeight;
112 /** Don't warn about unknown BIP 9 activations below this height.
113 * This prevents us from warning about the CSV and segwit activations. */
114 int MinBIP9WarningHeight;
115 /**
116 * Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,
117 * (nPowTargetTimespan / nPowTargetSpacing) which is also used for BIP9 deployments.
118 * Examples: 1916 for 95%, 1512 for testchains.
119 */
120 uint32_t nRuleChangeActivationThreshold;
121 uint32_t nMinerConfirmationWindow;
122 BIP9Deployment vDeployments[MAX_VERSION_BITS_DEPLOYMENTS];
123 /** Proof of work parameters */
124 uint256 powLimit;
125 bool fPowAllowMinDifficultyBlocks;
126 /**
127 * Enforce BIP94 timewarp attack mitigation. On testnet4 this also enforces
128 * the block storm mitigation.
129 */
130 bool enforce_BIP94;
131 bool fPowNoRetargeting;
132 int64_t nPowTargetSpacing;
133 int64_t nPowTargetTimespan;
134 std::chrono::seconds PowTargetSpacing() const
135 {
136 return std::chrono::seconds{nPowTargetSpacing};
137 }
138 int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
139 /** The best chain should have at least this much work */
140 uint256 nMinimumChainWork;
141 /** By default assume that the signatures in ancestors of this block are valid */
142 uint256 defaultAssumeValid;
143 144 /**
145 * If true, witness commitments contain a payload equal to a Limenka Script solution
146 * to the signet challenge. See BIP325.
147 */
148 bool signet_blocks{false};
149 std::vector<uint8_t> signet_challenge;
150 151 // Fork chain parameters (only meaningful when ChainType == FORK)
152 int64_t nForkActivationMTP{std::numeric_limits<int64_t>::max()}; // MTP-based activation timestamp (2026-10-18 09:00 EDT = 1792328400)
153 int64_t nForkActivationBias{30000}; // median-lag compensation: 50 blocks * 600s
154 int32_t nForkMTPWindow{101}; // activation gate median window
155 int64_t nForkFutureLimit{60}; // fork block future-time limit (down from the parent's 2h)
156 int64_t nForkIntervalTarget{617}; // calibrated controller setpoint (lands the cadence at ~600s)
157 uint64_t nForkDelaySteps{0}; // long-division delay: sequential steps per block (2^33)
158 // PI gains (corrected naming: P = instantaneous error, I = EMA
159 // accumulator - the 2021 lineage had these swapped), scaled by 1e6.
160 int64_t nForkKp{}; // proportional, symmetric
161 int64_t nForkKiUp{}; // integral when blocks are early (raise slow, flood-proof)
162 int64_t nForkKiDown{}; // integral when blocks are late (lower fast, quick restore)
163 164 int DeploymentHeight(BuriedDeployment dep) const
165 {
166 switch (dep) {
167 case DEPLOYMENT_HEIGHTINCB:
168 return BIP34Height;
169 case DEPLOYMENT_CLTV:
170 return BIP65Height;
171 case DEPLOYMENT_DERSIG:
172 return BIP66Height;
173 case DEPLOYMENT_CSV:
174 return CSVHeight;
175 case DEPLOYMENT_SEGWIT:
176 return SegwitHeight;
177 } // no default case, so the compiler can warn about missing cases
178 return std::numeric_limits<int>::max();
179 }
180 };
181 182 } // namespace Consensus
183 184 class CBlockIndex;
185 186 // Returns true if fork rules are active for a block built on pindexPrev:
187 // the fork activates when MTP(pindexPrev) (nForkMTPWindow-window median,
188 // compensated by nForkActivationBias for the median's ~50-block lag)
189 // reaches nForkActivationMTP. Blocks below the gate use parent chain
190 // rules. Defined in chain.h after CBlockIndex.
191 bool IsForkActive(const CBlockIndex* pindexPrev, const Consensus::Params& params);
192 193 #endif // LIMENKA_CONSENSUS_PARAMS_H
194