1// Copyright (c) 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
5package fullblocktests
6
7import (
8	"encoding/hex"
9	"math/big"
10	"time"
11
12	"github.com/btcsuite/btcd/chaincfg"
13	"github.com/btcsuite/btcd/chaincfg/chainhash"
14	"github.com/btcsuite/btcd/wire"
15)
16
17// newHashFromStr converts the passed big-endian hex string into a
18// wire.Hash.  It only differs from the one available in chainhash in that
19// it panics on an error since it will only (and must only) be called with
20// hard-coded, and therefore known good, hashes.
21func newHashFromStr(hexStr string) *chainhash.Hash {
22	hash, err := chainhash.NewHashFromStr(hexStr)
23	if err != nil {
24		panic(err)
25	}
26	return hash
27}
28
29// fromHex converts the passed hex string into a byte slice and will panic if
30// there is an error.  This is only provided for the hard-coded constants so
31// errors in the source code can be detected. It will only (and must only) be
32// called for initialization purposes.
33func fromHex(s string) []byte {
34	r, err := hex.DecodeString(s)
35	if err != nil {
36		panic("invalid hex in source file: " + s)
37	}
38	return r
39}
40
41var (
42	// bigOne is 1 represented as a big.Int.  It is defined here to avoid
43	// the overhead of creating it multiple times.
44	bigOne = big.NewInt(1)
45
46	// regressionPowLimit is the highest proof of work value a Bitcoin block
47	// can have for the regression test network.  It is the value 2^255 - 1.
48	regressionPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 255), bigOne)
49
50	// regTestGenesisBlock defines the genesis block of the block chain which serves
51	// as the public transaction ledger for the regression test network.
52	regTestGenesisBlock = wire.MsgBlock{
53		Header: wire.BlockHeader{
54			Version:    1,
55			PrevBlock:  *newHashFromStr("0000000000000000000000000000000000000000000000000000000000000000"),
56			MerkleRoot: *newHashFromStr("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"),
57			Timestamp:  time.Unix(1296688602, 0), // 2011-02-02 23:16:42 +0000 UTC
58			Bits:       0x207fffff,               // 545259519 [7fffff0000000000000000000000000000000000000000000000000000000000]
59			Nonce:      2,
60		},
61		Transactions: []*wire.MsgTx{{
62			Version: 1,
63			TxIn: []*wire.TxIn{{
64				PreviousOutPoint: wire.OutPoint{
65					Hash:  chainhash.Hash{},
66					Index: 0xffffffff,
67				},
68				SignatureScript: fromHex("04ffff001d010445" +
69					"5468652054696d65732030332f4a616e2f" +
70					"32303039204368616e63656c6c6f72206f" +
71					"6e206272696e6b206f66207365636f6e64" +
72					"206261696c6f757420666f72206261686b73"),
73				Sequence: 0xffffffff,
74			}},
75			TxOut: []*wire.TxOut{{
76				Value: 0,
77				PkScript: fromHex("4104678afdb0fe5548271967f1" +
78					"a67130b7105cd6a828e03909a67962e0ea1f" +
79					"61deb649f6bc3f4cef38c4f35504e51ec138" +
80					"c4f35504e51ec112de5c384df7ba0b8d578a" +
81					"4c702b6bf11d5fac"),
82			}},
83			LockTime: 0,
84		}},
85	}
86)
87
88// regressionNetParams defines the network parameters for the regression test
89// network.
90//
91// NOTE: The test generator intentionally does not use the existing definitions
92// in the chaincfg package since the intent is to be able to generate known
93// good tests which exercise that code.  Using the chaincfg parameters would
94// allow them to change out from under the tests potentially invalidating them.
95var regressionNetParams = &chaincfg.Params{
96	Name:        "regtest",
97	Net:         wire.TestNet,
98	DefaultPort: "18444",
99
100	// Chain parameters
101	GenesisBlock:             &regTestGenesisBlock,
102	GenesisHash:              newHashFromStr("5bec7567af40504e0994db3b573c186fffcc4edefe096ff2e58d00523bd7e8a6"),
103	PowLimit:                 regressionPowLimit,
104	PowLimitBits:             0x207fffff,
105	CoinbaseMaturity:         100,
106	BIP0034Height:            100000000, // Not active - Permit ver 1 blocks
107	BIP0065Height:            1351,      // Used by regression tests
108	BIP0066Height:            1251,      // Used by regression tests
109	SubsidyReductionInterval: 150,
110	TargetTimespan:           time.Hour * 24 * 14, // 14 days
111	TargetTimePerBlock:       time.Minute * 10,    // 10 minutes
112	RetargetAdjustmentFactor: 4,                   // 25% less, 400% more
113	ReduceMinDifficulty:      true,
114	MinDiffReductionTime:     time.Minute * 20, // TargetTimePerBlock * 2
115	GenerateSupported:        true,
116
117	// Checkpoints ordered from oldest to newest.
118	Checkpoints: nil,
119
120	// Mempool parameters
121	RelayNonStdTxs: true,
122
123	// Address encoding magics
124	PubKeyHashAddrID: 0x6f, // starts with m or n
125	ScriptHashAddrID: 0xc4, // starts with 2
126	PrivateKeyID:     0xef, // starts with 9 (uncompressed) or c (compressed)
127
128	// BIP32 hierarchical deterministic extended key magics
129	HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94}, // starts with tprv
130	HDPublicKeyID:  [4]byte{0x04, 0x35, 0x87, 0xcf}, // starts with tpub
131
132	// BIP44 coin type used in the hierarchical deterministic path for
133	// address generation.
134	HDCoinType: 1,
135}
136