OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / btcsuite / btcd / chaincfg / params.go
1 // Copyright (c) 2014-2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
4
5 package chaincfg
6
7 import (
8         "errors"
9         "math"
10         "math/big"
11         "strings"
12         "time"
13
14         "github.com/btcsuite/btcd/chaincfg/chainhash"
15         "github.com/btcsuite/btcd/wire"
16 )
17
18 // These variables are the chain proof-of-work limit parameters for each default
19 // network.
20 var (
21         // bigOne is 1 represented as a big.Int.  It is defined here to avoid
22         // the overhead of creating it multiple times.
23         bigOne = big.NewInt(1)
24
25         // mainPowLimit is the highest proof of work value a Bitcoin block can
26         // have for the main network.  It is the value 2^224 - 1.
27         mainPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne)
28
29         // regressionPowLimit is the highest proof of work value a Bitcoin block
30         // can have for the regression test network.  It is the value 2^255 - 1.
31         regressionPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 255), bigOne)
32
33         // testNet3PowLimit is the highest proof of work value a Bitcoin block
34         // can have for the test network (version 3).  It is the value
35         // 2^224 - 1.
36         testNet3PowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne)
37
38         // simNetPowLimit is the highest proof of work value a Bitcoin block
39         // can have for the simulation test network.  It is the value 2^255 - 1.
40         simNetPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 255), bigOne)
41 )
42
43 // Checkpoint identifies a known good point in the block chain.  Using
44 // checkpoints allows a few optimizations for old blocks during initial download
45 // and also prevents forks from old blocks.
46 //
47 // Each checkpoint is selected based upon several factors.  See the
48 // documentation for blockchain.IsCheckpointCandidate for details on the
49 // selection criteria.
50 type Checkpoint struct {
51         Height int32
52         Hash   *chainhash.Hash
53 }
54
55 // DNSSeed identifies a DNS seed.
56 type DNSSeed struct {
57         // Host defines the hostname of the seed.
58         Host string
59
60         // HasFiltering defines whether the seed supports filtering
61         // by service flags (wire.ServiceFlag).
62         HasFiltering bool
63 }
64
65 // ConsensusDeployment defines details related to a specific consensus rule
66 // change that is voted in.  This is part of BIP0009.
67 type ConsensusDeployment struct {
68         // BitNumber defines the specific bit number within the block version
69         // this particular soft-fork deployment refers to.
70         BitNumber uint8
71
72         // StartTime is the median block time after which voting on the
73         // deployment starts.
74         StartTime uint64
75
76         // ExpireTime is the median block time after which the attempted
77         // deployment expires.
78         ExpireTime uint64
79 }
80
81 // Constants that define the deployment offset in the deployments field of the
82 // parameters for each deployment.  This is useful to be able to get the details
83 // of a specific deployment by name.
84 const (
85         // DeploymentTestDummy defines the rule change deployment ID for testing
86         // purposes.
87         DeploymentTestDummy = iota
88
89         // DeploymentCSV defines the rule change deployment ID for the CSV
90         // soft-fork package. The CSV package includes the depolyment of BIPS
91         // 68, 112, and 113.
92         DeploymentCSV
93
94         // DeploymentSegwit defines the rule change deployment ID for the
95         // Segragated Witness (segwit) soft-fork package. The segwit package
96         // includes the deployment of BIPS 141, 142, 144, 145, 147 and 173.
97         DeploymentSegwit
98
99         // NOTE: DefinedDeployments must always come last since it is used to
100         // determine how many defined deployments there currently are.
101
102         // DefinedDeployments is the number of currently defined deployments.
103         DefinedDeployments
104 )
105
106 // Params defines a Bitcoin network by its parameters.  These parameters may be
107 // used by Bitcoin applications to differentiate networks as well as addresses
108 // and keys for one network from those intended for use on another network.
109 type Params struct {
110         // Name defines a human-readable identifier for the network.
111         Name string
112
113         // Net defines the magic bytes used to identify the network.
114         Net wire.BitcoinNet
115
116         // DefaultPort defines the default peer-to-peer port for the network.
117         DefaultPort string
118
119         // DNSSeeds defines a list of DNS seeds for the network that are used
120         // as one method to discover peers.
121         DNSSeeds []DNSSeed
122
123         // GenesisBlock defines the first block of the chain.
124         GenesisBlock *wire.MsgBlock
125
126         // GenesisHash is the starting block hash.
127         GenesisHash *chainhash.Hash
128
129         // PowLimit defines the highest allowed proof of work value for a block
130         // as a uint256.
131         PowLimit *big.Int
132
133         // PowLimitBits defines the highest allowed proof of work value for a
134         // block in compact form.
135         PowLimitBits uint32
136
137         // These fields define the block heights at which the specified softfork
138         // BIP became active.
139         BIP0034Height int32
140         BIP0065Height int32
141         BIP0066Height int32
142
143         // CoinbaseMaturity is the number of blocks required before newly mined
144         // coins (coinbase transactions) can be spent.
145         CoinbaseMaturity uint16
146
147         // SubsidyReductionInterval is the interval of blocks before the subsidy
148         // is reduced.
149         SubsidyReductionInterval int32
150
151         // TargetTimespan is the desired amount of time that should elapse
152         // before the block difficulty requirement is examined to determine how
153         // it should be changed in order to maintain the desired block
154         // generation rate.
155         TargetTimespan time.Duration
156
157         // TargetTimePerBlock is the desired amount of time to generate each
158         // block.
159         TargetTimePerBlock time.Duration
160
161         // RetargetAdjustmentFactor is the adjustment factor used to limit
162         // the minimum and maximum amount of adjustment that can occur between
163         // difficulty retargets.
164         RetargetAdjustmentFactor int64
165
166         // ReduceMinDifficulty defines whether the network should reduce the
167         // minimum required difficulty after a long enough period of time has
168         // passed without finding a block.  This is really only useful for test
169         // networks and should not be set on a main network.
170         ReduceMinDifficulty bool
171
172         // MinDiffReductionTime is the amount of time after which the minimum
173         // required difficulty should be reduced when a block hasn't been found.
174         //
175         // NOTE: This only applies if ReduceMinDifficulty is true.
176         MinDiffReductionTime time.Duration
177
178         // GenerateSupported specifies whether or not CPU mining is allowed.
179         GenerateSupported bool
180
181         // Checkpoints ordered from oldest to newest.
182         Checkpoints []Checkpoint
183
184         // These fields are related to voting on consensus rule changes as
185         // defined by BIP0009.
186         //
187         // RuleChangeActivationThreshold is the number of blocks in a threshold
188         // state retarget window for which a positive vote for a rule change
189         // must be cast in order to lock in a rule change. It should typically
190         // be 95% for the main network and 75% for test networks.
191         //
192         // MinerConfirmationWindow is the number of blocks in each threshold
193         // state retarget window.
194         //
195         // Deployments define the specific consensus rule changes to be voted
196         // on.
197         RuleChangeActivationThreshold uint32
198         MinerConfirmationWindow       uint32
199         Deployments                   [DefinedDeployments]ConsensusDeployment
200
201         // Mempool parameters
202         RelayNonStdTxs bool
203
204         // Human-readable part for Bech32 encoded segwit addresses, as defined
205         // in BIP 173.
206         Bech32HRPSegwit string
207
208         // Address encoding magics
209         PubKeyHashAddrID        byte // First byte of a P2PKH address
210         ScriptHashAddrID        byte // First byte of a P2SH address
211         PrivateKeyID            byte // First byte of a WIF private key
212         WitnessPubKeyHashAddrID byte // First byte of a P2WPKH address
213         WitnessScriptHashAddrID byte // First byte of a P2WSH address
214
215         // BIP32 hierarchical deterministic extended key magics
216         HDPrivateKeyID [4]byte
217         HDPublicKeyID  [4]byte
218
219         // BIP44 coin type used in the hierarchical deterministic path for
220         // address generation.
221         HDCoinType uint32
222 }
223
224 // MainNetParams defines the network parameters for the main Bitcoin network.
225 var MainNetParams = Params{
226         Name:        "mainnet",
227         Net:         wire.MainNet,
228         DefaultPort: "8333",
229         DNSSeeds: []DNSSeed{
230                 {"seed.bitcoin.sipa.be", true},
231                 {"dnsseed.bluematt.me", true},
232                 {"dnsseed.bitcoin.dashjr.org", false},
233                 {"seed.bitcoinstats.com", true},
234                 {"seed.bitnodes.io", false},
235                 {"seed.bitcoin.jonasschnelli.ch", true},
236         },
237
238         // Chain parameters
239         GenesisBlock:             &genesisBlock,
240         GenesisHash:              &genesisHash,
241         PowLimit:                 mainPowLimit,
242         PowLimitBits:             0x1d00ffff,
243         BIP0034Height:            227931, // 000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8
244         BIP0065Height:            388381, // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0
245         BIP0066Height:            363725, // 00000000000000000379eaa19dce8c9b722d46ae6a57c2f1a988119488b50931
246         CoinbaseMaturity:         100,
247         SubsidyReductionInterval: 210000,
248         TargetTimespan:           time.Hour * 24 * 14, // 14 days
249         TargetTimePerBlock:       time.Minute * 10,    // 10 minutes
250         RetargetAdjustmentFactor: 4,                   // 25% less, 400% more
251         ReduceMinDifficulty:      false,
252         MinDiffReductionTime:     0,
253         GenerateSupported:        false,
254
255         // Checkpoints ordered from oldest to newest.
256         Checkpoints: []Checkpoint{
257                 {11111, newHashFromStr("0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d")},
258                 {33333, newHashFromStr("000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6")},
259                 {74000, newHashFromStr("0000000000573993a3c9e41ce34471c079dcf5f52a0e824a81e7f953b8661a20")},
260                 {105000, newHashFromStr("00000000000291ce28027faea320c8d2b054b2e0fe44a773f3eefb151d6bdc97")},
261                 {134444, newHashFromStr("00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe")},
262                 {168000, newHashFromStr("000000000000099e61ea72015e79632f216fe6cb33d7899acb35b75c8303b763")},
263                 {193000, newHashFromStr("000000000000059f452a5f7340de6682a977387c17010ff6e6c3bd83ca8b1317")},
264                 {210000, newHashFromStr("000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e")},
265                 {216116, newHashFromStr("00000000000001b4f4b433e81ee46494af945cf96014816a4e2370f11b23df4e")},
266                 {225430, newHashFromStr("00000000000001c108384350f74090433e7fcf79a606b8e797f065b130575932")},
267                 {250000, newHashFromStr("000000000000003887df1f29024b06fc2200b55f8af8f35453d7be294df2d214")},
268                 {267300, newHashFromStr("000000000000000a83fbd660e918f218bf37edd92b748ad940483c7c116179ac")},
269                 {279000, newHashFromStr("0000000000000001ae8c72a0b0c301f67e3afca10e819efa9041e458e9bd7e40")},
270                 {300255, newHashFromStr("0000000000000000162804527c6e9b9f0563a280525f9d08c12041def0a0f3b2")},
271                 {319400, newHashFromStr("000000000000000021c6052e9becade189495d1c539aa37c58917305fd15f13b")},
272                 {343185, newHashFromStr("0000000000000000072b8bf361d01a6ba7d445dd024203fafc78768ed4368554")},
273                 {352940, newHashFromStr("000000000000000010755df42dba556bb72be6a32f3ce0b6941ce4430152c9ff")},
274                 {382320, newHashFromStr("00000000000000000a8dc6ed5b133d0eb2fd6af56203e4159789b092defd8ab2")},
275         },
276
277         // Consensus rule change deployments.
278         //
279         // The miner confirmation window is defined as:
280         //   target proof of work timespan / target proof of work spacing
281         RuleChangeActivationThreshold: 1916, // 95% of MinerConfirmationWindow
282         MinerConfirmationWindow:       2016, //
283         Deployments: [DefinedDeployments]ConsensusDeployment{
284                 DeploymentTestDummy: {
285                         BitNumber:  28,
286                         StartTime:  1199145601, // January 1, 2008 UTC
287                         ExpireTime: 1230767999, // December 31, 2008 UTC
288                 },
289                 DeploymentCSV: {
290                         BitNumber:  0,
291                         StartTime:  1462060800, // May 1st, 2016
292                         ExpireTime: 1493596800, // May 1st, 2017
293                 },
294                 DeploymentSegwit: {
295                         BitNumber:  1,
296                         StartTime:  1479168000, // November 15, 2016 UTC
297                         ExpireTime: 1510704000, // November 15, 2017 UTC.
298                 },
299         },
300
301         // Mempool parameters
302         RelayNonStdTxs: false,
303
304         // Human-readable part for Bech32 encoded segwit addresses, as defined in
305         // BIP 173.
306         Bech32HRPSegwit: "bc", // always bc for main net
307
308         // Address encoding magics
309         PubKeyHashAddrID:        0x00, // starts with 1
310         ScriptHashAddrID:        0x05, // starts with 3
311         PrivateKeyID:            0x80, // starts with 5 (uncompressed) or K (compressed)
312         WitnessPubKeyHashAddrID: 0x06, // starts with p2
313         WitnessScriptHashAddrID: 0x0A, // starts with 7Xh
314
315         // BIP32 hierarchical deterministic extended key magics
316         HDPrivateKeyID: [4]byte{0x04, 0x88, 0xad, 0xe4}, // starts with xprv
317         HDPublicKeyID:  [4]byte{0x04, 0x88, 0xb2, 0x1e}, // starts with xpub
318
319         // BIP44 coin type used in the hierarchical deterministic path for
320         // address generation.
321         HDCoinType: 0,
322 }
323
324 // RegressionNetParams defines the network parameters for the regression test
325 // Bitcoin network.  Not to be confused with the test Bitcoin network (version
326 // 3), this network is sometimes simply called "testnet".
327 var RegressionNetParams = Params{
328         Name:        "regtest",
329         Net:         wire.TestNet,
330         DefaultPort: "18444",
331         DNSSeeds:    []DNSSeed{},
332
333         // Chain parameters
334         GenesisBlock:             &regTestGenesisBlock,
335         GenesisHash:              &regTestGenesisHash,
336         PowLimit:                 regressionPowLimit,
337         PowLimitBits:             0x207fffff,
338         CoinbaseMaturity:         100,
339         BIP0034Height:            100000000, // Not active - Permit ver 1 blocks
340         BIP0065Height:            1351,      // Used by regression tests
341         BIP0066Height:            1251,      // Used by regression tests
342         SubsidyReductionInterval: 150,
343         TargetTimespan:           time.Hour * 24 * 14, // 14 days
344         TargetTimePerBlock:       time.Minute * 10,    // 10 minutes
345         RetargetAdjustmentFactor: 4,                   // 25% less, 400% more
346         ReduceMinDifficulty:      true,
347         MinDiffReductionTime:     time.Minute * 20, // TargetTimePerBlock * 2
348         GenerateSupported:        true,
349
350         // Checkpoints ordered from oldest to newest.
351         Checkpoints: nil,
352
353         // Consensus rule change deployments.
354         //
355         // The miner confirmation window is defined as:
356         //   target proof of work timespan / target proof of work spacing
357         RuleChangeActivationThreshold: 108, // 75%  of MinerConfirmationWindow
358         MinerConfirmationWindow:       144,
359         Deployments: [DefinedDeployments]ConsensusDeployment{
360                 DeploymentTestDummy: {
361                         BitNumber:  28,
362                         StartTime:  0,             // Always available for vote
363                         ExpireTime: math.MaxInt64, // Never expires
364                 },
365                 DeploymentCSV: {
366                         BitNumber:  0,
367                         StartTime:  0,             // Always available for vote
368                         ExpireTime: math.MaxInt64, // Never expires
369                 },
370                 DeploymentSegwit: {
371                         BitNumber:  1,
372                         StartTime:  0,             // Always available for vote
373                         ExpireTime: math.MaxInt64, // Never expires.
374                 },
375         },
376
377         // Mempool parameters
378         RelayNonStdTxs: true,
379
380         // Human-readable part for Bech32 encoded segwit addresses, as defined in
381         // BIP 173.
382         Bech32HRPSegwit: "tb", // always tb for test net
383
384         // Address encoding magics
385         PubKeyHashAddrID: 0x6f, // starts with m or n
386         ScriptHashAddrID: 0xc4, // starts with 2
387         PrivateKeyID:     0xef, // starts with 9 (uncompressed) or c (compressed)
388
389         // BIP32 hierarchical deterministic extended key magics
390         HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94}, // starts with tprv
391         HDPublicKeyID:  [4]byte{0x04, 0x35, 0x87, 0xcf}, // starts with tpub
392
393         // BIP44 coin type used in the hierarchical deterministic path for
394         // address generation.
395         HDCoinType: 1,
396 }
397
398 // TestNet3Params defines the network parameters for the test Bitcoin network
399 // (version 3).  Not to be confused with the regression test network, this
400 // network is sometimes simply called "testnet".
401 var TestNet3Params = Params{
402         Name:        "testnet3",
403         Net:         wire.TestNet3,
404         DefaultPort: "18333",
405         DNSSeeds: []DNSSeed{
406                 {"testnet-seed.bitcoin.jonasschnelli.ch", true},
407                 {"testnet-seed.bitcoin.schildbach.de", false},
408                 {"seed.tbtc.petertodd.org", true},
409                 {"testnet-seed.bluematt.me", false},
410         },
411
412         // Chain parameters
413         GenesisBlock:             &testNet3GenesisBlock,
414         GenesisHash:              &testNet3GenesisHash,
415         PowLimit:                 testNet3PowLimit,
416         PowLimitBits:             0x1d00ffff,
417         BIP0034Height:            21111,  // 0000000023b3a96d3484e5abb3755c413e7d41500f8e2a5c3f0dd01299cd8ef8
418         BIP0065Height:            581885, // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6
419         BIP0066Height:            330776, // 000000002104c8c45e99a8853285a3b592602a3ccde2b832481da85e9e4ba182
420         CoinbaseMaturity:         100,
421         SubsidyReductionInterval: 210000,
422         TargetTimespan:           time.Hour * 24 * 14, // 14 days
423         TargetTimePerBlock:       time.Minute * 10,    // 10 minutes
424         RetargetAdjustmentFactor: 4,                   // 25% less, 400% more
425         ReduceMinDifficulty:      true,
426         MinDiffReductionTime:     time.Minute * 20, // TargetTimePerBlock * 2
427         GenerateSupported:        false,
428
429         // Checkpoints ordered from oldest to newest.
430         Checkpoints: []Checkpoint{
431                 {546, newHashFromStr("000000002a936ca763904c3c35fce2f3556c559c0214345d31b1bcebf76acb70")},
432                 {100000, newHashFromStr("00000000009e2958c15ff9290d571bf9459e93b19765c6801ddeccadbb160a1e")},
433                 {200000, newHashFromStr("0000000000287bffd321963ef05feab753ebe274e1d78b2fd4e2bfe9ad3aa6f2")},
434                 {300001, newHashFromStr("0000000000004829474748f3d1bc8fcf893c88be255e6d7f571c548aff57abf4")},
435                 {400002, newHashFromStr("0000000005e2c73b8ecb82ae2dbc2e8274614ebad7172b53528aba7501f5a089")},
436                 {500011, newHashFromStr("00000000000929f63977fbac92ff570a9bd9e7715401ee96f2848f7b07750b02")},
437                 {600002, newHashFromStr("000000000001f471389afd6ee94dcace5ccc44adc18e8bff402443f034b07240")},
438                 {700000, newHashFromStr("000000000000406178b12a4dea3b27e13b3c4fe4510994fd667d7c1e6a3f4dc1")},
439                 {800010, newHashFromStr("000000000017ed35296433190b6829db01e657d80631d43f5983fa403bfdb4c1")},
440                 {900000, newHashFromStr("0000000000356f8d8924556e765b7a94aaebc6b5c8685dcfa2b1ee8b41acd89b")},
441                 {1000007, newHashFromStr("00000000001ccb893d8a1f25b70ad173ce955e5f50124261bbbc50379a612ddf")},
442         },
443
444         // Consensus rule change deployments.
445         //
446         // The miner confirmation window is defined as:
447         //   target proof of work timespan / target proof of work spacing
448         RuleChangeActivationThreshold: 1512, // 75% of MinerConfirmationWindow
449         MinerConfirmationWindow:       2016,
450         Deployments: [DefinedDeployments]ConsensusDeployment{
451                 DeploymentTestDummy: {
452                         BitNumber:  28,
453                         StartTime:  1199145601, // January 1, 2008 UTC
454                         ExpireTime: 1230767999, // December 31, 2008 UTC
455                 },
456                 DeploymentCSV: {
457                         BitNumber:  0,
458                         StartTime:  1456790400, // March 1st, 2016
459                         ExpireTime: 1493596800, // May 1st, 2017
460                 },
461                 DeploymentSegwit: {
462                         BitNumber:  1,
463                         StartTime:  1462060800, // May 1, 2016 UTC
464                         ExpireTime: 1493596800, // May 1, 2017 UTC.
465                 },
466         },
467
468         // Mempool parameters
469         RelayNonStdTxs: true,
470
471         // Human-readable part for Bech32 encoded segwit addresses, as defined in
472         // BIP 173.
473         Bech32HRPSegwit: "tb", // always tb for test net
474
475         // Address encoding magics
476         PubKeyHashAddrID:        0x6f, // starts with m or n
477         ScriptHashAddrID:        0xc4, // starts with 2
478         WitnessPubKeyHashAddrID: 0x03, // starts with QW
479         WitnessScriptHashAddrID: 0x28, // starts with T7n
480         PrivateKeyID:            0xef, // starts with 9 (uncompressed) or c (compressed)
481
482         // BIP32 hierarchical deterministic extended key magics
483         HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94}, // starts with tprv
484         HDPublicKeyID:  [4]byte{0x04, 0x35, 0x87, 0xcf}, // starts with tpub
485
486         // BIP44 coin type used in the hierarchical deterministic path for
487         // address generation.
488         HDCoinType: 1,
489 }
490
491 // SimNetParams defines the network parameters for the simulation test Bitcoin
492 // network.  This network is similar to the normal test network except it is
493 // intended for private use within a group of individuals doing simulation
494 // testing.  The functionality is intended to differ in that the only nodes
495 // which are specifically specified are used to create the network rather than
496 // following normal discovery rules.  This is important as otherwise it would
497 // just turn into another public testnet.
498 var SimNetParams = Params{
499         Name:        "simnet",
500         Net:         wire.SimNet,
501         DefaultPort: "18555",
502         DNSSeeds:    []DNSSeed{}, // NOTE: There must NOT be any seeds.
503
504         // Chain parameters
505         GenesisBlock:             &simNetGenesisBlock,
506         GenesisHash:              &simNetGenesisHash,
507         PowLimit:                 simNetPowLimit,
508         PowLimitBits:             0x207fffff,
509         BIP0034Height:            0, // Always active on simnet
510         BIP0065Height:            0, // Always active on simnet
511         BIP0066Height:            0, // Always active on simnet
512         CoinbaseMaturity:         100,
513         SubsidyReductionInterval: 210000,
514         TargetTimespan:           time.Hour * 24 * 14, // 14 days
515         TargetTimePerBlock:       time.Minute * 10,    // 10 minutes
516         RetargetAdjustmentFactor: 4,                   // 25% less, 400% more
517         ReduceMinDifficulty:      true,
518         MinDiffReductionTime:     time.Minute * 20, // TargetTimePerBlock * 2
519         GenerateSupported:        true,
520
521         // Checkpoints ordered from oldest to newest.
522         Checkpoints: nil,
523
524         // Consensus rule change deployments.
525         //
526         // The miner confirmation window is defined as:
527         //   target proof of work timespan / target proof of work spacing
528         RuleChangeActivationThreshold: 75, // 75% of MinerConfirmationWindow
529         MinerConfirmationWindow:       100,
530         Deployments: [DefinedDeployments]ConsensusDeployment{
531                 DeploymentTestDummy: {
532                         BitNumber:  28,
533                         StartTime:  0,             // Always available for vote
534                         ExpireTime: math.MaxInt64, // Never expires
535                 },
536                 DeploymentCSV: {
537                         BitNumber:  0,
538                         StartTime:  0,             // Always available for vote
539                         ExpireTime: math.MaxInt64, // Never expires
540                 },
541                 DeploymentSegwit: {
542                         BitNumber:  1,
543                         StartTime:  0,             // Always available for vote
544                         ExpireTime: math.MaxInt64, // Never expires.
545                 },
546         },
547
548         // Mempool parameters
549         RelayNonStdTxs: true,
550
551         // Human-readable part for Bech32 encoded segwit addresses, as defined in
552         // BIP 173.
553         Bech32HRPSegwit: "sb", // always sb for sim net
554
555         // Address encoding magics
556         PubKeyHashAddrID:        0x3f, // starts with S
557         ScriptHashAddrID:        0x7b, // starts with s
558         PrivateKeyID:            0x64, // starts with 4 (uncompressed) or F (compressed)
559         WitnessPubKeyHashAddrID: 0x19, // starts with Gg
560         WitnessScriptHashAddrID: 0x28, // starts with ?
561
562         // BIP32 hierarchical deterministic extended key magics
563         HDPrivateKeyID: [4]byte{0x04, 0x20, 0xb9, 0x00}, // starts with sprv
564         HDPublicKeyID:  [4]byte{0x04, 0x20, 0xbd, 0x3a}, // starts with spub
565
566         // BIP44 coin type used in the hierarchical deterministic path for
567         // address generation.
568         HDCoinType: 115, // ASCII for s
569 }
570
571 var (
572         // ErrDuplicateNet describes an error where the parameters for a Bitcoin
573         // network could not be set due to the network already being a standard
574         // network or previously-registered into this package.
575         ErrDuplicateNet = errors.New("duplicate Bitcoin network")
576
577         // ErrUnknownHDKeyID describes an error where the provided id which
578         // is intended to identify the network for a hierarchical deterministic
579         // private extended key is not registered.
580         ErrUnknownHDKeyID = errors.New("unknown hd private extended key bytes")
581 )
582
583 var (
584         registeredNets       = make(map[wire.BitcoinNet]struct{})
585         pubKeyHashAddrIDs    = make(map[byte]struct{})
586         scriptHashAddrIDs    = make(map[byte]struct{})
587         bech32SegwitPrefixes = make(map[string]struct{})
588         hdPrivToPubKeyIDs    = make(map[[4]byte][]byte)
589 )
590
591 // String returns the hostname of the DNS seed in human-readable form.
592 func (d DNSSeed) String() string {
593         return d.Host
594 }
595
596 // Register registers the network parameters for a Bitcoin network.  This may
597 // error with ErrDuplicateNet if the network is already registered (either
598 // due to a previous Register call, or the network being one of the default
599 // networks).
600 //
601 // Network parameters should be registered into this package by a main package
602 // as early as possible.  Then, library packages may lookup networks or network
603 // parameters based on inputs and work regardless of the network being standard
604 // or not.
605 func Register(params *Params) error {
606         if _, ok := registeredNets[params.Net]; ok {
607                 return ErrDuplicateNet
608         }
609         registeredNets[params.Net] = struct{}{}
610         pubKeyHashAddrIDs[params.PubKeyHashAddrID] = struct{}{}
611         scriptHashAddrIDs[params.ScriptHashAddrID] = struct{}{}
612         hdPrivToPubKeyIDs[params.HDPrivateKeyID] = params.HDPublicKeyID[:]
613
614         // A valid Bech32 encoded segwit address always has as prefix the
615         // human-readable part for the given net followed by '1'.
616         bech32SegwitPrefixes[params.Bech32HRPSegwit+"1"] = struct{}{}
617         return nil
618 }
619
620 // mustRegister performs the same function as Register except it panics if there
621 // is an error.  This should only be called from package init functions.
622 func mustRegister(params *Params) {
623         if err := Register(params); err != nil {
624                 panic("failed to register network: " + err.Error())
625         }
626 }
627
628 // IsPubKeyHashAddrID returns whether the id is an identifier known to prefix a
629 // pay-to-pubkey-hash address on any default or registered network.  This is
630 // used when decoding an address string into a specific address type.  It is up
631 // to the caller to check both this and IsScriptHashAddrID and decide whether an
632 // address is a pubkey hash address, script hash address, neither, or
633 // undeterminable (if both return true).
634 func IsPubKeyHashAddrID(id byte) bool {
635         _, ok := pubKeyHashAddrIDs[id]
636         return ok
637 }
638
639 // IsScriptHashAddrID returns whether the id is an identifier known to prefix a
640 // pay-to-script-hash address on any default or registered network.  This is
641 // used when decoding an address string into a specific address type.  It is up
642 // to the caller to check both this and IsPubKeyHashAddrID and decide whether an
643 // address is a pubkey hash address, script hash address, neither, or
644 // undeterminable (if both return true).
645 func IsScriptHashAddrID(id byte) bool {
646         _, ok := scriptHashAddrIDs[id]
647         return ok
648 }
649
650 // IsBech32SegwitPrefix returns whether the prefix is a known prefix for segwit
651 // addresses on any default or registered network.  This is used when decoding
652 // an address string into a specific address type.
653 func IsBech32SegwitPrefix(prefix string) bool {
654         prefix = strings.ToLower(prefix)
655         _, ok := bech32SegwitPrefixes[prefix]
656         return ok
657 }
658
659 // HDPrivateKeyToPublicKeyID accepts a private hierarchical deterministic
660 // extended key id and returns the associated public key id.  When the provided
661 // id is not registered, the ErrUnknownHDKeyID error will be returned.
662 func HDPrivateKeyToPublicKeyID(id []byte) ([]byte, error) {
663         if len(id) != 4 {
664                 return nil, ErrUnknownHDKeyID
665         }
666
667         var key [4]byte
668         copy(key[:], id)
669         pubBytes, ok := hdPrivToPubKeyIDs[key]
670         if !ok {
671                 return nil, ErrUnknownHDKeyID
672         }
673
674         return pubBytes, nil
675 }
676
677 // newHashFromStr converts the passed big-endian hex string into a
678 // chainhash.Hash.  It only differs from the one available in chainhash in that
679 // it panics on an error since it will only (and must only) be called with
680 // hard-coded, and therefore known good, hashes.
681 func newHashFromStr(hexStr string) *chainhash.Hash {
682         hash, err := chainhash.NewHashFromStr(hexStr)
683         if err != nil {
684                 // Ordinarily I don't like panics in library code since it
685                 // can take applications down without them having a chance to
686                 // recover which is extremely annoying, however an exception is
687                 // being made in this case because the only way this can panic
688                 // is if there is an error in the hard-coded hashes.  Thus it
689                 // will only ever potentially panic on init and therefore is
690                 // 100% predictable.
691                 panic(err)
692         }
693         return hash
694 }
695
696 func init() {
697         // Register all default networks when the package is initialized.
698         mustRegister(&MainNetParams)
699         mustRegister(&TestNet3Params)
700         mustRegister(&RegressionNetParams)
701         mustRegister(&SimNetParams)
702 }