1 // Copyright (c) 2013-2017 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
13 "github.com/btcsuite/btcd/chaincfg"
14 "github.com/btcsuite/btcd/chaincfg/chainhash"
15 "github.com/btcsuite/btcd/wire"
16 "github.com/btcsuite/btcutil"
19 // TestSequenceLocksActive tests the SequenceLockActive function to ensure it
20 // works as expected in all possible combinations/scenarios.
21 func TestSequenceLocksActive(t *testing.T) {
22 seqLock := func(h int32, s int64) *SequenceLock {
36 // Block based sequence lock with equal block height.
37 {seqLock: seqLock(1000, -1), blockHeight: 1001, mtp: time.Unix(9, 0), want: true},
39 // Time based sequence lock with mtp past the absolute time.
40 {seqLock: seqLock(-1, 30), blockHeight: 2, mtp: time.Unix(31, 0), want: true},
42 // Block based sequence lock with current height below seq lock block height.
43 {seqLock: seqLock(1000, -1), blockHeight: 90, mtp: time.Unix(9, 0), want: false},
45 // Time based sequence lock with current time before lock time.
46 {seqLock: seqLock(-1, 30), blockHeight: 2, mtp: time.Unix(29, 0), want: false},
48 // Block based sequence lock at the same height, so shouldn't yet be active.
49 {seqLock: seqLock(1000, -1), blockHeight: 1000, mtp: time.Unix(9, 0), want: false},
51 // Time based sequence lock with current time equal to lock time, so shouldn't yet be active.
52 {seqLock: seqLock(-1, 30), blockHeight: 2, mtp: time.Unix(30, 0), want: false},
55 t.Logf("Running %d sequence locks tests", len(tests))
56 for i, test := range tests {
57 got := SequenceLockActive(test.seqLock,
58 test.blockHeight, test.mtp)
60 t.Fatalf("SequenceLockActive #%d got %v want %v", i,
66 // TestCheckConnectBlock tests the CheckConnectBlock function to ensure it
68 func TestCheckConnectBlock(t *testing.T) {
69 // Create a new database and chain instance to run tests against.
70 chain, teardownFunc, err := chainSetup("checkconnectblock",
71 &chaincfg.MainNetParams)
73 t.Errorf("Failed to setup chain instance: %v", err)
78 // The genesis block should fail to connect since it's already inserted.
79 genesisBlock := chaincfg.MainNetParams.GenesisBlock
80 err = chain.CheckConnectBlock(btcutil.NewBlock(genesisBlock))
82 t.Errorf("CheckConnectBlock: Did not received expected error")
86 // TestCheckBlockSanity tests the CheckBlockSanity function to ensure it works
88 func TestCheckBlockSanity(t *testing.T) {
89 powLimit := chaincfg.MainNetParams.PowLimit
90 block := btcutil.NewBlock(&Block100000)
91 timeSource := NewMedianTime()
92 err := CheckBlockSanity(block, powLimit, timeSource)
94 t.Errorf("CheckBlockSanity: %v", err)
97 // Ensure a block that has a timestamp with a precision higher than one
99 timestamp := block.MsgBlock().Header.Timestamp
100 block.MsgBlock().Header.Timestamp = timestamp.Add(time.Nanosecond)
101 err = CheckBlockSanity(block, powLimit, timeSource)
103 t.Errorf("CheckBlockSanity: error is nil when it shouldn't be")
107 // TestCheckSerializedHeight tests the checkSerializedHeight function with
108 // various serialized heights and also does negative tests to ensure errors
109 // and handled properly.
110 func TestCheckSerializedHeight(t *testing.T) {
111 // Create an empty coinbase template to be used in the tests below.
112 coinbaseOutpoint := wire.NewOutPoint(&chainhash.Hash{}, math.MaxUint32)
113 coinbaseTx := wire.NewMsgTx(1)
114 coinbaseTx.AddTxIn(wire.NewTxIn(coinbaseOutpoint, nil, nil))
116 // Expected rule errors.
117 missingHeightError := RuleError{
118 ErrorCode: ErrMissingCoinbaseHeight,
120 badHeightError := RuleError{
121 ErrorCode: ErrBadCoinbaseHeight,
125 sigScript []byte // Serialized data
126 wantHeight int32 // Expected height
127 err error // Expected error type
129 // No serialized height length.
130 {[]byte{}, 0, missingHeightError},
131 // Serialized height length with no height bytes.
132 {[]byte{0x02}, 0, missingHeightError},
133 // Serialized height length with too few height bytes.
134 {[]byte{0x02, 0x4a}, 0, missingHeightError},
135 // Serialized height that needs 2 bytes to encode.
136 {[]byte{0x02, 0x4a, 0x52}, 21066, nil},
137 // Serialized height that needs 2 bytes to encode, but backwards
139 {[]byte{0x02, 0x4a, 0x52}, 19026, badHeightError},
140 // Serialized height that needs 3 bytes to encode.
141 {[]byte{0x03, 0x40, 0x0d, 0x03}, 200000, nil},
142 // Serialized height that needs 3 bytes to encode, but backwards
144 {[]byte{0x03, 0x40, 0x0d, 0x03}, 1074594560, badHeightError},
147 t.Logf("Running %d tests", len(tests))
148 for i, test := range tests {
149 msgTx := coinbaseTx.Copy()
150 msgTx.TxIn[0].SignatureScript = test.sigScript
151 tx := btcutil.NewTx(msgTx)
153 err := checkSerializedHeight(tx, test.wantHeight)
154 if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
155 t.Errorf("checkSerializedHeight #%d wrong error type "+
156 "got: %v <%T>, want: %T", i, err, err, test.err)
160 if rerr, ok := err.(RuleError); ok {
161 trerr := test.err.(RuleError)
162 if rerr.ErrorCode != trerr.ErrorCode {
163 t.Errorf("checkSerializedHeight #%d wrong "+
164 "error code got: %v, want: %v", i,
165 rerr.ErrorCode, trerr.ErrorCode)
172 // Block100000 defines block 100,000 of the block chain. It is used to
173 // test Block operations.
174 var Block100000 = wire.MsgBlock{
175 Header: wire.BlockHeader{
177 PrevBlock: chainhash.Hash([32]byte{ // Make go vet happy.
178 0x50, 0x12, 0x01, 0x19, 0x17, 0x2a, 0x61, 0x04,
179 0x21, 0xa6, 0xc3, 0x01, 0x1d, 0xd3, 0x30, 0xd9,
180 0xdf, 0x07, 0xb6, 0x36, 0x16, 0xc2, 0xcc, 0x1f,
181 0x1c, 0xd0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
182 }), // 000000000002d01c1fccc21636b607dfd930d31d01c3a62104612a1719011250
183 MerkleRoot: chainhash.Hash([32]byte{ // Make go vet happy.
184 0x66, 0x57, 0xa9, 0x25, 0x2a, 0xac, 0xd5, 0xc0,
185 0xb2, 0x94, 0x09, 0x96, 0xec, 0xff, 0x95, 0x22,
186 0x28, 0xc3, 0x06, 0x7c, 0xc3, 0x8d, 0x48, 0x85,
187 0xef, 0xb5, 0xa4, 0xac, 0x42, 0x47, 0xe9, 0xf3,
188 }), // f3e94742aca4b5ef85488dc37c06c3282295ffec960994b2c0d5ac2a25a95766
189 Timestamp: time.Unix(1293623863, 0), // 2010-12-29 11:57:43 +0000 UTC
190 Bits: 0x1b04864c, // 453281356
191 Nonce: 0x10572b0f, // 274148111
193 Transactions: []*wire.MsgTx{
198 PreviousOutPoint: wire.OutPoint{
199 Hash: chainhash.Hash{},
202 SignatureScript: []byte{
203 0x04, 0x4c, 0x86, 0x04, 0x1b, 0x02, 0x06, 0x02,
205 Sequence: 0xffffffff,
208 TxOut: []*wire.TxOut{
210 Value: 0x12a05f200, // 5000000000
213 0x04, 0x1b, 0x0e, 0x8c, 0x25, 0x67, 0xc1, 0x25,
214 0x36, 0xaa, 0x13, 0x35, 0x7b, 0x79, 0xa0, 0x73,
215 0xdc, 0x44, 0x44, 0xac, 0xb8, 0x3c, 0x4e, 0xc7,
216 0xa0, 0xe2, 0xf9, 0x9d, 0xd7, 0x45, 0x75, 0x16,
217 0xc5, 0x81, 0x72, 0x42, 0xda, 0x79, 0x69, 0x24,
218 0xca, 0x4e, 0x99, 0x94, 0x7d, 0x08, 0x7f, 0xed,
219 0xf9, 0xce, 0x46, 0x7c, 0xb9, 0xf7, 0xc6, 0x28,
220 0x70, 0x78, 0xf8, 0x01, 0xdf, 0x27, 0x6f, 0xdf,
221 0x84, // 65-byte signature
232 PreviousOutPoint: wire.OutPoint{
233 Hash: chainhash.Hash([32]byte{ // Make go vet happy.
234 0x03, 0x2e, 0x38, 0xe9, 0xc0, 0xa8, 0x4c, 0x60,
235 0x46, 0xd6, 0x87, 0xd1, 0x05, 0x56, 0xdc, 0xac,
236 0xc4, 0x1d, 0x27, 0x5e, 0xc5, 0x5f, 0xc0, 0x07,
237 0x79, 0xac, 0x88, 0xfd, 0xf3, 0x57, 0xa1, 0x87,
238 }), // 87a157f3fd88ac7907c05fc55e271dc4acdc5605d187d646604ca8c0e9382e03
241 SignatureScript: []byte{
243 0x30, 0x46, 0x02, 0x21, 0x00, 0xc3, 0x52, 0xd3,
244 0xdd, 0x99, 0x3a, 0x98, 0x1b, 0xeb, 0xa4, 0xa6,
245 0x3a, 0xd1, 0x5c, 0x20, 0x92, 0x75, 0xca, 0x94,
246 0x70, 0xab, 0xfc, 0xd5, 0x7d, 0xa9, 0x3b, 0x58,
247 0xe4, 0xeb, 0x5d, 0xce, 0x82, 0x02, 0x21, 0x00,
248 0x84, 0x07, 0x92, 0xbc, 0x1f, 0x45, 0x60, 0x62,
249 0x81, 0x9f, 0x15, 0xd3, 0x3e, 0xe7, 0x05, 0x5c,
250 0xf7, 0xb5, 0xee, 0x1a, 0xf1, 0xeb, 0xcc, 0x60,
251 0x28, 0xd9, 0xcd, 0xb1, 0xc3, 0xaf, 0x77, 0x48,
252 0x01, // 73-byte signature
254 0x04, 0xf4, 0x6d, 0xb5, 0xe9, 0xd6, 0x1a, 0x9d,
255 0xc2, 0x7b, 0x8d, 0x64, 0xad, 0x23, 0xe7, 0x38,
256 0x3a, 0x4e, 0x6c, 0xa1, 0x64, 0x59, 0x3c, 0x25,
257 0x27, 0xc0, 0x38, 0xc0, 0x85, 0x7e, 0xb6, 0x7e,
258 0xe8, 0xe8, 0x25, 0xdc, 0xa6, 0x50, 0x46, 0xb8,
259 0x2c, 0x93, 0x31, 0x58, 0x6c, 0x82, 0xe0, 0xfd,
260 0x1f, 0x63, 0x3f, 0x25, 0xf8, 0x7c, 0x16, 0x1b,
261 0xc6, 0xf8, 0xa6, 0x30, 0x12, 0x1d, 0xf2, 0xb3,
262 0xd3, // 65-byte pubkey
264 Sequence: 0xffffffff,
267 TxOut: []*wire.TxOut{
269 Value: 0x2123e300, // 556000000
274 0xc3, 0x98, 0xef, 0xa9, 0xc3, 0x92, 0xba, 0x60,
275 0x13, 0xc5, 0xe0, 0x4e, 0xe7, 0x29, 0x75, 0x5e,
276 0xf7, 0xf5, 0x8b, 0x32,
277 0x88, // OP_EQUALVERIFY
282 Value: 0x108e20f00, // 4444000000
287 0x94, 0x8c, 0x76, 0x5a, 0x69, 0x14, 0xd4, 0x3f,
288 0x2a, 0x7a, 0xc1, 0x77, 0xda, 0x2c, 0x2f, 0x6b,
289 0x52, 0xde, 0x3d, 0x7c,
290 0x88, // OP_EQUALVERIFY
301 PreviousOutPoint: wire.OutPoint{
302 Hash: chainhash.Hash([32]byte{ // Make go vet happy.
303 0xc3, 0x3e, 0xbf, 0xf2, 0xa7, 0x09, 0xf1, 0x3d,
304 0x9f, 0x9a, 0x75, 0x69, 0xab, 0x16, 0xa3, 0x27,
305 0x86, 0xaf, 0x7d, 0x7e, 0x2d, 0xe0, 0x92, 0x65,
306 0xe4, 0x1c, 0x61, 0xd0, 0x78, 0x29, 0x4e, 0xcf,
307 }), // cf4e2978d0611ce46592e02d7e7daf8627a316ab69759a9f3df109a7f2bf3ec3
310 SignatureScript: []byte{
312 0x30, 0x44, 0x02, 0x20, 0x03, 0x2d, 0x30, 0xdf,
313 0x5e, 0xe6, 0xf5, 0x7f, 0xa4, 0x6c, 0xdd, 0xb5,
314 0xeb, 0x8d, 0x0d, 0x9f, 0xe8, 0xde, 0x6b, 0x34,
315 0x2d, 0x27, 0x94, 0x2a, 0xe9, 0x0a, 0x32, 0x31,
316 0xe0, 0xba, 0x33, 0x3e, 0x02, 0x20, 0x3d, 0xee,
317 0xe8, 0x06, 0x0f, 0xdc, 0x70, 0x23, 0x0a, 0x7f,
318 0x5b, 0x4a, 0xd7, 0xd7, 0xbc, 0x3e, 0x62, 0x8c,
319 0xbe, 0x21, 0x9a, 0x88, 0x6b, 0x84, 0x26, 0x9e,
320 0xae, 0xb8, 0x1e, 0x26, 0xb4, 0xfe, 0x01,
322 0x04, 0xae, 0x31, 0xc3, 0x1b, 0xf9, 0x12, 0x78,
323 0xd9, 0x9b, 0x83, 0x77, 0xa3, 0x5b, 0xbc, 0xe5,
324 0xb2, 0x7d, 0x9f, 0xff, 0x15, 0x45, 0x68, 0x39,
325 0xe9, 0x19, 0x45, 0x3f, 0xc7, 0xb3, 0xf7, 0x21,
326 0xf0, 0xba, 0x40, 0x3f, 0xf9, 0x6c, 0x9d, 0xee,
327 0xb6, 0x80, 0xe5, 0xfd, 0x34, 0x1c, 0x0f, 0xc3,
328 0xa7, 0xb9, 0x0d, 0xa4, 0x63, 0x1e, 0xe3, 0x95,
329 0x60, 0x63, 0x9d, 0xb4, 0x62, 0xe9, 0xcb, 0x85,
330 0x0f, // 65-byte pubkey
332 Sequence: 0xffffffff,
335 TxOut: []*wire.TxOut{
337 Value: 0xf4240, // 1000000
342 0xb0, 0xdc, 0xbf, 0x97, 0xea, 0xbf, 0x44, 0x04,
343 0xe3, 0x1d, 0x95, 0x24, 0x77, 0xce, 0x82, 0x2d,
344 0xad, 0xbe, 0x7e, 0x10,
345 0x88, // OP_EQUALVERIFY
350 Value: 0x11d260c0, // 299000000
355 0x6b, 0x12, 0x81, 0xee, 0xc2, 0x5a, 0xb4, 0xe1,
356 0xe0, 0x79, 0x3f, 0xf4, 0xe0, 0x8a, 0xb1, 0xab,
357 0xb3, 0x40, 0x9c, 0xd9,
358 0x88, // OP_EQUALVERIFY
369 PreviousOutPoint: wire.OutPoint{
370 Hash: chainhash.Hash([32]byte{ // Make go vet happy.
371 0x0b, 0x60, 0x72, 0xb3, 0x86, 0xd4, 0xa7, 0x73,
372 0x23, 0x52, 0x37, 0xf6, 0x4c, 0x11, 0x26, 0xac,
373 0x3b, 0x24, 0x0c, 0x84, 0xb9, 0x17, 0xa3, 0x90,
374 0x9b, 0xa1, 0xc4, 0x3d, 0xed, 0x5f, 0x51, 0xf4,
375 }), // f4515fed3dc4a19b90a317b9840c243bac26114cf637522373a7d486b372600b
378 SignatureScript: []byte{
380 0x30, 0x46, 0x02, 0x21, 0x00, 0xbb, 0x1a, 0xd2,
381 0x6d, 0xf9, 0x30, 0xa5, 0x1c, 0xce, 0x11, 0x0c,
382 0xf4, 0x4f, 0x7a, 0x48, 0xc3, 0xc5, 0x61, 0xfd,
383 0x97, 0x75, 0x00, 0xb1, 0xae, 0x5d, 0x6b, 0x6f,
384 0xd1, 0x3d, 0x0b, 0x3f, 0x4a, 0x02, 0x21, 0x00,
385 0xc5, 0xb4, 0x29, 0x51, 0xac, 0xed, 0xff, 0x14,
386 0xab, 0xba, 0x27, 0x36, 0xfd, 0x57, 0x4b, 0xdb,
387 0x46, 0x5f, 0x3e, 0x6f, 0x8d, 0xa1, 0x2e, 0x2c,
388 0x53, 0x03, 0x95, 0x4a, 0xca, 0x7f, 0x78, 0xf3,
389 0x01, // 73-byte signature
391 0x04, 0xa7, 0x13, 0x5b, 0xfe, 0x82, 0x4c, 0x97,
392 0xec, 0xc0, 0x1e, 0xc7, 0xd7, 0xe3, 0x36, 0x18,
393 0x5c, 0x81, 0xe2, 0xaa, 0x2c, 0x41, 0xab, 0x17,
394 0x54, 0x07, 0xc0, 0x94, 0x84, 0xce, 0x96, 0x94,
395 0xb4, 0x49, 0x53, 0xfc, 0xb7, 0x51, 0x20, 0x65,
396 0x64, 0xa9, 0xc2, 0x4d, 0xd0, 0x94, 0xd4, 0x2f,
397 0xdb, 0xfd, 0xd5, 0xaa, 0xd3, 0xe0, 0x63, 0xce,
398 0x6a, 0xf4, 0xcf, 0xaa, 0xea, 0x4e, 0xa1, 0x4f,
399 0xbb, // 65-byte pubkey
401 Sequence: 0xffffffff,
404 TxOut: []*wire.TxOut{
406 Value: 0xf4240, // 1000000
411 0x39, 0xaa, 0x3d, 0x56, 0x9e, 0x06, 0xa1, 0xd7,
412 0x92, 0x6d, 0xc4, 0xbe, 0x11, 0x93, 0xc9, 0x9b,
413 0xf2, 0xeb, 0x9e, 0xe0,
414 0x88, // OP_EQUALVERIFY