1 // Package chaincfg provides basic parameters for bitcoin chain and testnets.
2 package chaincfg
3 4 import (
5 "math/big"
6 "time"
7 8 "next.orly.dev/pkg/nostr/crypto/ec/chainhash"
9 "next.orly.dev/pkg/nostr/crypto/ec/wire"
10 )
11 12 var (
13 // bigOne is 1 represented as a big.Int. It is defined here to avoid
14 // the overhead of creating it multiple times.
15 bigOne = big.NewInt(1)
16 17 // mainPowLimit is the highest proof of work value a Bitcoin block can
18 // have for the main network. It is the value 2^224 - 1.
19 mainPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne)
20 )
21 22 // Constants that define the deployment offset in the deployments field of the
23 // parameters for each deployment. This is useful to be able to get the details
24 // of a specific deployment by name.
25 const (
26 // DeploymentTestDummy defines the rule change deployment ID for testing
27 // purposes.
28 DeploymentTestDummy = iota
29 30 // DeploymentTestDummyMinActivation defines the rule change deployment
31 // ID for testing purposes. This differs from the DeploymentTestDummy
32 // in that it specifies the newer params the taproot fork used for
33 // activation: a custom threshold and a min activation height.
34 DeploymentTestDummyMinActivation
35 36 // DeploymentCSV defines the rule change deployment ID for the CSV
37 // soft-fork package. The CSV package includes the deployment of BIPS
38 // 68, 112, and 113.
39 DeploymentCSV
40 41 // DeploymentSegwit defines the rule change deployment ID for the
42 // Segregated Witness (segwit) soft-fork package. The segwit package
43 // includes the deployment of BIPS 141, 142, 144, 145, 147 and 173.
44 DeploymentSegwit
45 46 // DeploymentTaproot defines the rule change deployment ID for the
47 // Taproot (+Schnorr) soft-fork package. The taproot package includes
48 // the deployment of BIPS 340, 341 and 342.
49 DeploymentTaproot
50 51 // NOTE: DefinedDeployments must always come last since it is used to
52 // determine how many defined deployments there currently are.
53 54 // DefinedDeployments is the number of currently defined deployments.
55 DefinedDeployments
56 )
57 58 // ConsensusDeployment defines details related to a specific consensus rule
59 // change that is voted in. This is part of BIP0009.
60 type ConsensusDeployment struct {
61 // BitNumber defines the specific bit number within the block version
62 // this particular soft-fork deployment refers to.
63 BitNumber uint8
64 65 // MinActivationHeight is an optional field that when set (default
66 // value being zero), modifies the traditional BIP 9 state machine by
67 // only transitioning from LockedIn to Active once the block height is
68 // greater than (or equal to) thus specified height.
69 MinActivationHeight uint32
70 71 // CustomActivationThreshold if set (non-zero), will _override_ the
72 // existing RuleChangeActivationThreshold value set at the
73 // network/chain level. This value divided by the active
74 // MinerConfirmationWindow denotes the threshold required for
75 // activation. A value of 1815 block denotes a 90% threshold.
76 CustomActivationThreshold uint32
77 78 // DeploymentStarter is used to determine if the given
79 // ConsensusDeployment has started or not.
80 DeploymentStarter ConsensusDeploymentStarter
81 82 // DeploymentEnder is used to determine if the given
83 // ConsensusDeployment has ended or not.
84 DeploymentEnder ConsensusDeploymentEnder
85 }
86 87 // Checkpoint identifies a known good point in the block chain. Using
88 // checkpoints allows a few optimizations for old blocks during initial download
89 // and also prevents forks from old blocks.
90 //
91 // Each checkpoint is selected based upon several factors. See the
92 // documentation for blockchain.IsCheckpointCandidate for details on the
93 // selection criteria.
94 type Checkpoint struct {
95 Height int32
96 Hash *chainhash.Hash
97 }
98 99 // DNSSeed identifies a DNS seed.
100 type DNSSeed struct {
101 // Host defines the hostname of the seed.
102 Host string
103 104 // HasFiltering defines whether the seed supports filtering
105 // by service flags (wire.ServiceFlag).
106 HasFiltering bool
107 }
108 109 // Params defines a Bitcoin network by its parameters. These parameters may be
110 // used by Bitcoin applications to differentiate networks as well as addresses
111 // and keys for one network from those intended for use on another network.
112 type Params struct {
113 // Name defines a human-readable identifier for the network.
114 Name string
115 116 // Net defines the magic bytes used to identify the network.
117 Net wire.BitcoinNet
118 119 // DefaultPort defines the default peer-to-peer port for the network.
120 DefaultPort string
121 122 // DNSSeeds defines a list of DNS seeds for the network that are used
123 // as one method to discover peers.
124 DNSSeeds []DNSSeed
125 126 // GenesisBlock defines the first block of the chain.
127 GenesisBlock *wire.MsgBlock
128 129 // GenesisHash is the starting block hash.
130 GenesisHash *chainhash.Hash
131 132 // PowLimit defines the highest allowed proof of work value for a block
133 // as a uint256.
134 PowLimit *big.Int
135 136 // PowLimitBits defines the highest allowed proof of work value for a
137 // block in compact form.
138 PowLimitBits uint32
139 140 // PoWNoRetargeting defines whether the network has difficulty
141 // retargeting enabled or not. This should only be set to true for
142 // regtest like networks.
143 PoWNoRetargeting bool
144 145 // These fields define the block heights at which the specified softfork
146 // BIP became active.
147 BIP0034Height int32
148 BIP0065Height int32
149 BIP0066Height int32
150 151 // CoinbaseMaturity is the number of blocks required before newly mined
152 // coins (coinbase transactions) can be spent.
153 CoinbaseMaturity uint16
154 155 // SubsidyReductionInterval is the interval of blocks before the subsidy
156 // is reduced.
157 SubsidyReductionInterval int32
158 159 // TargetTimespan is the desired amount of time that should elapse
160 // before the block difficulty requirement is examined to determine how
161 // it should be changed in order to maintain the desired block
162 // generation rate.
163 TargetTimespan time.Duration
164 165 // TargetTimePerBlock is the desired amount of time to generate each
166 // block.
167 TargetTimePerBlock time.Duration
168 169 // RetargetAdjustmentFactor is the adjustment factor used to limit
170 // the minimum and maximum amount of adjustment that can occur between
171 // difficulty retargets.
172 RetargetAdjustmentFactor int64
173 174 // ReduceMinDifficulty defines whether the network should reduce the
175 // minimum required difficulty after a long enough period of time has
176 // passed without finding a block. This is really only useful for test
177 // networks and should not be set on a main network.
178 ReduceMinDifficulty bool
179 180 // MinDiffReductionTime is the amount of time after which the minimum
181 // required difficulty should be reduced when a block hasn't been found.
182 //
183 // NOTE: This only applies if ReduceMinDifficulty is true.
184 MinDiffReductionTime time.Duration
185 186 // GenerateSupported specifies whether or not CPU mining is allowed.
187 GenerateSupported bool
188 189 // Checkpoints ordered from oldest to newest.
190 Checkpoints []Checkpoint
191 192 // These fields are related to voting on consensus rule changes as
193 // defined by BIP0009.
194 //
195 // RuleChangeActivationThreshold is the number of blocks in a threshold
196 // state retarget window for which a positive vote for a rule change
197 // must be cast in order to lock in a rule change. It should typically
198 // be 95% for the main network and 75% for test networks.
199 //
200 // MinerConfirmationWindow is the number of blocks in each threshold
201 // state retarget window.
202 //
203 // Deployments define the specific consensus rule changes to be voted
204 // on.
205 RuleChangeActivationThreshold uint32
206 MinerConfirmationWindow uint32
207 Deployments [DefinedDeployments]ConsensusDeployment
208 209 // Mempool parameters
210 RelayNonStdTxs bool
211 212 // Human-readable part for Bech32 encoded segwit addresses, as defined
213 // in BIP 173.
214 Bech32HRPSegwit []byte
215 216 // Address encoding magics
217 PubKeyHashAddrID byte // First byte of a P2PKH address
218 ScriptHashAddrID byte // First byte of a P2SH address
219 PrivateKeyID byte // First byte of a WIF private key
220 WitnessPubKeyHashAddrID byte // First byte of a P2WPKH address
221 WitnessScriptHashAddrID byte // First byte of a P2WSH address
222 223 // BIP32 hierarchical deterministic extended key magics
224 HDPrivateKeyID [4]byte
225 HDPublicKeyID [4]byte
226 227 // BIP44 coin type used in the hierarchical deterministic path for
228 // address generation.
229 HDCoinType uint32
230 }
231 232 // MainNetParams defines the network parameters for the main Bitcoin network.
233 var MainNetParams = Params{
234 Name: "mainnet",
235 Net: wire.MainNet,
236 DefaultPort: "8333",
237 DNSSeeds: []DNSSeed{
238 {"seed.bitcoin.sipa.be", true},
239 {"dnsseed.bluematt.me", true},
240 {"dnsseed.bitcoin.dashjr.org", false},
241 {"seed.bitcoinstats.com", true},
242 {"seed.bitnodes.io", false},
243 {"seed.bitcoin.jonasschnelli.ch", true},
244 },
245 246 // Chain parameters
247 GenesisBlock: &genesisBlock,
248 GenesisHash: &genesisHash,
249 PowLimit: mainPowLimit,
250 PowLimitBits: 0x1d00ffff,
251 BIP0034Height: 227931, // 000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8
252 BIP0065Height: 388381, // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0
253 BIP0066Height: 363725, // 00000000000000000379eaa19dce8c9b722d46ae6a57c2f1a988119488b50931
254 CoinbaseMaturity: 100,
255 SubsidyReductionInterval: 210000,
256 TargetTimespan: time.Hour * 24 * 14, // 14 days
257 TargetTimePerBlock: time.Minute * 10, // 10 minutes
258 RetargetAdjustmentFactor: 4, // 25% less, 400% more
259 ReduceMinDifficulty: false,
260 MinDiffReductionTime: 0,
261 GenerateSupported: false,
262 263 // Checkpoints ordered from oldest to newest.
264 Checkpoints: []Checkpoint{
265 {
266 11111,
267 newHashFromStr("0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d"),
268 },
269 {
270 33333,
271 newHashFromStr("000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6"),
272 },
273 {
274 74000,
275 newHashFromStr("0000000000573993a3c9e41ce34471c079dcf5f52a0e824a81e7f953b8661a20"),
276 },
277 {
278 105000,
279 newHashFromStr("00000000000291ce28027faea320c8d2b054b2e0fe44a773f3eefb151d6bdc97"),
280 },
281 {
282 134444,
283 newHashFromStr("00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe"),
284 },
285 {
286 168000,
287 newHashFromStr("000000000000099e61ea72015e79632f216fe6cb33d7899acb35b75c8303b763"),
288 },
289 {
290 193000,
291 newHashFromStr("000000000000059f452a5f7340de6682a977387c17010ff6e6c3bd83ca8b1317"),
292 },
293 {
294 210000,
295 newHashFromStr("000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e"),
296 },
297 {
298 216116,
299 newHashFromStr("00000000000001b4f4b433e81ee46494af945cf96014816a4e2370f11b23df4e"),
300 },
301 {
302 225430,
303 newHashFromStr("00000000000001c108384350f74090433e7fcf79a606b8e797f065b130575932"),
304 },
305 {
306 250000,
307 newHashFromStr("000000000000003887df1f29024b06fc2200b55f8af8f35453d7be294df2d214"),
308 },
309 {
310 267300,
311 newHashFromStr("000000000000000a83fbd660e918f218bf37edd92b748ad940483c7c116179ac"),
312 },
313 {
314 279000,
315 newHashFromStr("0000000000000001ae8c72a0b0c301f67e3afca10e819efa9041e458e9bd7e40"),
316 },
317 {
318 300255,
319 newHashFromStr("0000000000000000162804527c6e9b9f0563a280525f9d08c12041def0a0f3b2"),
320 },
321 {
322 319400,
323 newHashFromStr("000000000000000021c6052e9becade189495d1c539aa37c58917305fd15f13b"),
324 },
325 {
326 343185,
327 newHashFromStr("0000000000000000072b8bf361d01a6ba7d445dd024203fafc78768ed4368554"),
328 },
329 {
330 352940,
331 newHashFromStr("000000000000000010755df42dba556bb72be6a32f3ce0b6941ce4430152c9ff"),
332 },
333 {
334 382320,
335 newHashFromStr("00000000000000000a8dc6ed5b133d0eb2fd6af56203e4159789b092defd8ab2"),
336 },
337 {
338 400000,
339 newHashFromStr("000000000000000004ec466ce4732fe6f1ed1cddc2ed4b328fff5224276e3f6f"),
340 },
341 {
342 430000,
343 newHashFromStr("000000000000000001868b2bb3a285f3cc6b33ea234eb70facf4dcdf22186b87"),
344 },
345 {
346 460000,
347 newHashFromStr("000000000000000000ef751bbce8e744ad303c47ece06c8d863e4d417efc258c"),
348 },
349 {
350 490000,
351 newHashFromStr("000000000000000000de069137b17b8d5a3dfbd5b145b2dcfb203f15d0c4de90"),
352 },
353 {
354 520000,
355 newHashFromStr("0000000000000000000d26984c0229c9f6962dc74db0a6d525f2f1640396f69c"),
356 },
357 {
358 550000,
359 newHashFromStr("000000000000000000223b7a2298fb1c6c75fb0efc28a4c56853ff4112ec6bc9"),
360 },
361 {
362 560000,
363 newHashFromStr("0000000000000000002c7b276daf6efb2b6aa68e2ce3be67ef925b3264ae7122"),
364 },
365 {
366 563378,
367 newHashFromStr("0000000000000000000f1c54590ee18d15ec70e68c8cd4cfbadb1b4f11697eee"),
368 },
369 {
370 597379,
371 newHashFromStr("00000000000000000005f8920febd3925f8272a6a71237563d78c2edfdd09ddf"),
372 },
373 {
374 623950,
375 newHashFromStr("0000000000000000000f2adce67e49b0b6bdeb9de8b7c3d7e93b21e7fc1e819d"),
376 },
377 {
378 654683,
379 newHashFromStr("0000000000000000000b9d2ec5a352ecba0592946514a92f14319dc2b367fc72"),
380 },
381 {
382 691719,
383 newHashFromStr("00000000000000000008a89e854d57e5667df88f1cdef6fde2fbca1de5b639ad"),
384 },
385 {
386 724466,
387 newHashFromStr("000000000000000000052d314a259755ca65944e68df6b12a067ea8f1f5a7091"),
388 },
389 {
390 751565,
391 newHashFromStr("00000000000000000009c97098b5295f7e5f183ac811fb5d1534040adb93cabd"),
392 },
393 },
394 395 // Consensus rule change deployments.
396 //
397 // The miner confirmation window is defined as:
398 // target proof of work timespan / target proof of work spacing
399 RuleChangeActivationThreshold: 1916, // 95% of MinerConfirmationWindow
400 MinerConfirmationWindow: 2016, //
401 Deployments: [DefinedDeployments]ConsensusDeployment{
402 DeploymentTestDummy: {
403 BitNumber: 28,
404 DeploymentStarter: NewMedianTimeDeploymentStarter(
405 time.Unix(11991456010, 0), // January 1, 2008 UTC
406 ),
407 DeploymentEnder: NewMedianTimeDeploymentEnder(
408 time.Unix(1230767999, 0), // December 31, 2008 UTC
409 ),
410 },
411 DeploymentTestDummyMinActivation: {
412 BitNumber: 22,
413 CustomActivationThreshold: 1815, // Only needs 90% hash rate.
414 MinActivationHeight: 10_0000, // Can only activate after height 10k.
415 DeploymentStarter: NewMedianTimeDeploymentStarter(
416 time.Time{}, // Always available for vote
417 ),
418 DeploymentEnder: NewMedianTimeDeploymentEnder(
419 time.Time{}, // Never expires
420 ),
421 },
422 DeploymentCSV: {
423 BitNumber: 0,
424 DeploymentStarter: NewMedianTimeDeploymentStarter(
425 time.Unix(1462060800, 0), // May 1st, 2016
426 ),
427 DeploymentEnder: NewMedianTimeDeploymentEnder(
428 time.Unix(1493596800, 0), // May 1st, 2017
429 ),
430 },
431 DeploymentSegwit: {
432 BitNumber: 1,
433 DeploymentStarter: NewMedianTimeDeploymentStarter(
434 time.Unix(1479168000, 0), // November 15, 2016 UTC
435 ),
436 DeploymentEnder: NewMedianTimeDeploymentEnder(
437 time.Unix(1510704000, 0), // November 15, 2017 UTC.
438 ),
439 },
440 DeploymentTaproot: {
441 BitNumber: 2,
442 DeploymentStarter: NewMedianTimeDeploymentStarter(
443 time.Unix(1619222400, 0), // April 24th, 2021 UTC.
444 ),
445 DeploymentEnder: NewMedianTimeDeploymentEnder(
446 time.Unix(1628640000, 0), // August 11th, 2021 UTC.
447 ),
448 CustomActivationThreshold: 1815, // 90%
449 MinActivationHeight: 709_632,
450 },
451 },
452 453 // Mempool parameters
454 RelayNonStdTxs: false,
455 456 // Human-readable part for Bech32 encoded segwit addresses, as defined in
457 // BIP 173.
458 Bech32HRPSegwit: []byte("bc"), // always bc for main net
459 460 // Address encoding magics
461 PubKeyHashAddrID: 0x00, // starts with 1
462 ScriptHashAddrID: 0x05, // starts with 3
463 PrivateKeyID: 0x80, // starts with 5 (uncompressed) or K (compressed)
464 WitnessPubKeyHashAddrID: 0x06, // starts with p2
465 WitnessScriptHashAddrID: 0x0A, // starts with 7Xh
466 467 // BIP32 hierarchical deterministic extended key magics
468 HDPrivateKeyID: [4]byte{0x04, 0x88, 0xad, 0xe4}, // starts with xprv
469 HDPublicKeyID: [4]byte{0x04, 0x88, 0xb2, 0x1e}, // starts with xpub
470 471 // BIP44 coin type used in the hierarchical deterministic path for
472 // address generation.
473 HDCoinType: 0,
474 }
475 476 // newHashFromStr converts the passed big-endian hex string into a
477 // chainhash.Hash. It only differs from the one available in chainhash in that
478 // it panics on an error since it will only (and must only) be called with
479 // hard-coded, and therefore known good, hashes.
480 func newHashFromStr(hexStr string) *chainhash.Hash {
481 hash, err := chainhash.NewHashFromStr(hexStr)
482 if err != nil {
483 // Ordinarily I don't like panics in library code since it
484 // can take applications down without them having a chance to
485 // recover which is extremely annoying, however an exception is
486 // being made in this case because the only way this can panic
487 // is if there is an error in the hard-coded hashes. Thus it
488 // will only ever potentially panic on init and therefore is
489 // 100% predictable.
490 panic(err)
491 }
492 return hash
493 }
494