github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/blockchain/validate_test.go (about)

     1  // Copyright (c) 2013-2016 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package blockchain_test
     7  
     8  import (
     9  	"math"
    10  	"reflect"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/dashpay/godash/blockchain"
    15  	"github.com/dashpay/godash/chaincfg"
    16  	"github.com/dashpay/godash/wire"
    17  	"github.com/dashpay/godashutil"
    18  )
    19  
    20  // TestCheckConnectBlock tests the CheckConnectBlock function to ensure it
    21  // fails.
    22  func TestCheckConnectBlock(t *testing.T) {
    23  	// Create a new database and chain instance to run tests against.
    24  	chain, teardownFunc, err := chainSetup("checkconnectblock")
    25  	if err != nil {
    26  		t.Errorf("Failed to setup chain instance: %v", err)
    27  		return
    28  	}
    29  	defer teardownFunc()
    30  
    31  	// The genesis block should fail to connect since it's already inserted.
    32  	genesisBlock := chaincfg.MainNetParams.GenesisBlock
    33  	err = chain.CheckConnectBlock(godashutil.NewBlock(genesisBlock))
    34  	if err == nil {
    35  		t.Errorf("CheckConnectBlock: Did not received expected error")
    36  	}
    37  }
    38  
    39  // TestCheckBlockSanity tests the CheckBlockSanity function to ensure it works
    40  // as expected.
    41  func TestCheckBlockSanity(t *testing.T) {
    42  	powLimit := chaincfg.MainNetParams.PowLimit
    43  	block := godashutil.NewBlock(&Block100000)
    44  	timeSource := blockchain.NewMedianTime()
    45  	err := blockchain.CheckBlockSanity(block, powLimit, timeSource)
    46  	if err != nil {
    47  		t.Errorf("CheckBlockSanity: %v", err)
    48  	}
    49  
    50  	// Ensure a block that has a timestamp with a precision higher than one
    51  	// second fails.
    52  	timestamp := block.MsgBlock().Header.Timestamp
    53  	block.MsgBlock().Header.Timestamp = timestamp.Add(time.Nanosecond)
    54  	err = blockchain.CheckBlockSanity(block, powLimit, timeSource)
    55  	if err == nil {
    56  		t.Errorf("CheckBlockSanity: error is nil when it shouldn't be")
    57  	}
    58  }
    59  
    60  // TestCheckSerializedHeight tests the checkSerializedHeight function with
    61  // various serialized heights and also does negative tests to ensure errors
    62  // and handled properly.
    63  func TestCheckSerializedHeight(t *testing.T) {
    64  	// Create an empty coinbase template to be used in the tests below.
    65  	coinbaseOutpoint := wire.NewOutPoint(&wire.ShaHash{}, math.MaxUint32)
    66  	coinbaseTx := wire.NewMsgTx()
    67  	coinbaseTx.Version = 2
    68  	coinbaseTx.AddTxIn(wire.NewTxIn(coinbaseOutpoint, nil))
    69  
    70  	// Expected rule errors.
    71  	missingHeightError := blockchain.RuleError{
    72  		ErrorCode: blockchain.ErrMissingCoinbaseHeight,
    73  	}
    74  	badHeightError := blockchain.RuleError{
    75  		ErrorCode: blockchain.ErrBadCoinbaseHeight,
    76  	}
    77  
    78  	tests := []struct {
    79  		sigScript  []byte // Serialized data
    80  		wantHeight int32  // Expected height
    81  		err        error  // Expected error type
    82  	}{
    83  		// No serialized height length.
    84  		{[]byte{}, 0, missingHeightError},
    85  		// Serialized height length with no height bytes.
    86  		{[]byte{0x02}, 0, missingHeightError},
    87  		// Serialized height length with too few height bytes.
    88  		{[]byte{0x02, 0x4a}, 0, missingHeightError},
    89  		// Serialized height that needs 2 bytes to encode.
    90  		{[]byte{0x02, 0x4a, 0x52}, 21066, nil},
    91  		// Serialized height that needs 2 bytes to encode, but backwards
    92  		// endianness.
    93  		{[]byte{0x02, 0x4a, 0x52}, 19026, badHeightError},
    94  		// Serialized height that needs 3 bytes to encode.
    95  		{[]byte{0x03, 0x40, 0x0d, 0x03}, 200000, nil},
    96  		// Serialized height that needs 3 bytes to encode, but backwards
    97  		// endianness.
    98  		{[]byte{0x03, 0x40, 0x0d, 0x03}, 1074594560, badHeightError},
    99  	}
   100  
   101  	t.Logf("Running %d tests", len(tests))
   102  	for i, test := range tests {
   103  		msgTx := coinbaseTx.Copy()
   104  		msgTx.TxIn[0].SignatureScript = test.sigScript
   105  		tx := godashutil.NewTx(msgTx)
   106  
   107  		err := blockchain.TstCheckSerializedHeight(tx, test.wantHeight)
   108  		if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
   109  			t.Errorf("checkSerializedHeight #%d wrong error type "+
   110  				"got: %v <%T>, want: %T", i, err, err, test.err)
   111  			continue
   112  		}
   113  
   114  		if rerr, ok := err.(blockchain.RuleError); ok {
   115  			trerr := test.err.(blockchain.RuleError)
   116  			if rerr.ErrorCode != trerr.ErrorCode {
   117  				t.Errorf("checkSerializedHeight #%d wrong "+
   118  					"error code got: %v, want: %v", i,
   119  					rerr.ErrorCode, trerr.ErrorCode)
   120  				continue
   121  			}
   122  		}
   123  	}
   124  }
   125  
   126  // Block100000 defines block 100,000 of the block chain.  It is used to
   127  // test Block operations.
   128  var Block100000 = wire.MsgBlock{
   129  	Header: wire.BlockHeader{
   130  		Version: 1,
   131  		PrevBlock: wire.ShaHash([32]byte{ // Make go vet happy.
   132  			0x50, 0x12, 0x01, 0x19, 0x17, 0x2a, 0x61, 0x04,
   133  			0x21, 0xa6, 0xc3, 0x01, 0x1d, 0xd3, 0x30, 0xd9,
   134  			0xdf, 0x07, 0xb6, 0x36, 0x16, 0xc2, 0xcc, 0x1f,
   135  			0x1c, 0xd0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
   136  		}), // 000000000002d01c1fccc21636b607dfd930d31d01c3a62104612a1719011250
   137  		MerkleRoot: wire.ShaHash([32]byte{ // Make go vet happy.
   138  			0x66, 0x57, 0xa9, 0x25, 0x2a, 0xac, 0xd5, 0xc0,
   139  			0xb2, 0x94, 0x09, 0x96, 0xec, 0xff, 0x95, 0x22,
   140  			0x28, 0xc3, 0x06, 0x7c, 0xc3, 0x8d, 0x48, 0x85,
   141  			0xef, 0xb5, 0xa4, 0xac, 0x42, 0x47, 0xe9, 0xf3,
   142  		}), // f3e94742aca4b5ef85488dc37c06c3282295ffec960994b2c0d5ac2a25a95766
   143  		Timestamp: time.Unix(1293623863, 0), // 2010-12-29 11:57:43 +0000 UTC
   144  		Bits:      0x1b04864c,               // 453281356
   145  		Nonce:     0x10572b0f,               // 274148111
   146  	},
   147  	Transactions: []*wire.MsgTx{
   148  		{
   149  			Version: 1,
   150  			TxIn: []*wire.TxIn{
   151  				{
   152  					PreviousOutPoint: wire.OutPoint{
   153  						Hash:  wire.ShaHash{},
   154  						Index: 0xffffffff,
   155  					},
   156  					SignatureScript: []byte{
   157  						0x04, 0x4c, 0x86, 0x04, 0x1b, 0x02, 0x06, 0x02,
   158  					},
   159  					Sequence: 0xffffffff,
   160  				},
   161  			},
   162  			TxOut: []*wire.TxOut{
   163  				{
   164  					Value: 0x12a05f200, // 5000000000
   165  					PkScript: []byte{
   166  						0x41, // OP_DATA_65
   167  						0x04, 0x1b, 0x0e, 0x8c, 0x25, 0x67, 0xc1, 0x25,
   168  						0x36, 0xaa, 0x13, 0x35, 0x7b, 0x79, 0xa0, 0x73,
   169  						0xdc, 0x44, 0x44, 0xac, 0xb8, 0x3c, 0x4e, 0xc7,
   170  						0xa0, 0xe2, 0xf9, 0x9d, 0xd7, 0x45, 0x75, 0x16,
   171  						0xc5, 0x81, 0x72, 0x42, 0xda, 0x79, 0x69, 0x24,
   172  						0xca, 0x4e, 0x99, 0x94, 0x7d, 0x08, 0x7f, 0xed,
   173  						0xf9, 0xce, 0x46, 0x7c, 0xb9, 0xf7, 0xc6, 0x28,
   174  						0x70, 0x78, 0xf8, 0x01, 0xdf, 0x27, 0x6f, 0xdf,
   175  						0x84, // 65-byte signature
   176  						0xac, // OP_CHECKSIG
   177  					},
   178  				},
   179  			},
   180  			LockTime: 0,
   181  		},
   182  		{
   183  			Version: 1,
   184  			TxIn: []*wire.TxIn{
   185  				{
   186  					PreviousOutPoint: wire.OutPoint{
   187  						Hash: wire.ShaHash([32]byte{ // Make go vet happy.
   188  							0x03, 0x2e, 0x38, 0xe9, 0xc0, 0xa8, 0x4c, 0x60,
   189  							0x46, 0xd6, 0x87, 0xd1, 0x05, 0x56, 0xdc, 0xac,
   190  							0xc4, 0x1d, 0x27, 0x5e, 0xc5, 0x5f, 0xc0, 0x07,
   191  							0x79, 0xac, 0x88, 0xfd, 0xf3, 0x57, 0xa1, 0x87,
   192  						}), // 87a157f3fd88ac7907c05fc55e271dc4acdc5605d187d646604ca8c0e9382e03
   193  						Index: 0,
   194  					},
   195  					SignatureScript: []byte{
   196  						0x49, // OP_DATA_73
   197  						0x30, 0x46, 0x02, 0x21, 0x00, 0xc3, 0x52, 0xd3,
   198  						0xdd, 0x99, 0x3a, 0x98, 0x1b, 0xeb, 0xa4, 0xa6,
   199  						0x3a, 0xd1, 0x5c, 0x20, 0x92, 0x75, 0xca, 0x94,
   200  						0x70, 0xab, 0xfc, 0xd5, 0x7d, 0xa9, 0x3b, 0x58,
   201  						0xe4, 0xeb, 0x5d, 0xce, 0x82, 0x02, 0x21, 0x00,
   202  						0x84, 0x07, 0x92, 0xbc, 0x1f, 0x45, 0x60, 0x62,
   203  						0x81, 0x9f, 0x15, 0xd3, 0x3e, 0xe7, 0x05, 0x5c,
   204  						0xf7, 0xb5, 0xee, 0x1a, 0xf1, 0xeb, 0xcc, 0x60,
   205  						0x28, 0xd9, 0xcd, 0xb1, 0xc3, 0xaf, 0x77, 0x48,
   206  						0x01, // 73-byte signature
   207  						0x41, // OP_DATA_65
   208  						0x04, 0xf4, 0x6d, 0xb5, 0xe9, 0xd6, 0x1a, 0x9d,
   209  						0xc2, 0x7b, 0x8d, 0x64, 0xad, 0x23, 0xe7, 0x38,
   210  						0x3a, 0x4e, 0x6c, 0xa1, 0x64, 0x59, 0x3c, 0x25,
   211  						0x27, 0xc0, 0x38, 0xc0, 0x85, 0x7e, 0xb6, 0x7e,
   212  						0xe8, 0xe8, 0x25, 0xdc, 0xa6, 0x50, 0x46, 0xb8,
   213  						0x2c, 0x93, 0x31, 0x58, 0x6c, 0x82, 0xe0, 0xfd,
   214  						0x1f, 0x63, 0x3f, 0x25, 0xf8, 0x7c, 0x16, 0x1b,
   215  						0xc6, 0xf8, 0xa6, 0x30, 0x12, 0x1d, 0xf2, 0xb3,
   216  						0xd3, // 65-byte pubkey
   217  					},
   218  					Sequence: 0xffffffff,
   219  				},
   220  			},
   221  			TxOut: []*wire.TxOut{
   222  				{
   223  					Value: 0x2123e300, // 556000000
   224  					PkScript: []byte{
   225  						0x76, // OP_DUP
   226  						0xa9, // OP_HASH160
   227  						0x14, // OP_DATA_20
   228  						0xc3, 0x98, 0xef, 0xa9, 0xc3, 0x92, 0xba, 0x60,
   229  						0x13, 0xc5, 0xe0, 0x4e, 0xe7, 0x29, 0x75, 0x5e,
   230  						0xf7, 0xf5, 0x8b, 0x32,
   231  						0x88, // OP_EQUALVERIFY
   232  						0xac, // OP_CHECKSIG
   233  					},
   234  				},
   235  				{
   236  					Value: 0x108e20f00, // 4444000000
   237  					PkScript: []byte{
   238  						0x76, // OP_DUP
   239  						0xa9, // OP_HASH160
   240  						0x14, // OP_DATA_20
   241  						0x94, 0x8c, 0x76, 0x5a, 0x69, 0x14, 0xd4, 0x3f,
   242  						0x2a, 0x7a, 0xc1, 0x77, 0xda, 0x2c, 0x2f, 0x6b,
   243  						0x52, 0xde, 0x3d, 0x7c,
   244  						0x88, // OP_EQUALVERIFY
   245  						0xac, // OP_CHECKSIG
   246  					},
   247  				},
   248  			},
   249  			LockTime: 0,
   250  		},
   251  		{
   252  			Version: 1,
   253  			TxIn: []*wire.TxIn{
   254  				{
   255  					PreviousOutPoint: wire.OutPoint{
   256  						Hash: wire.ShaHash([32]byte{ // Make go vet happy.
   257  							0xc3, 0x3e, 0xbf, 0xf2, 0xa7, 0x09, 0xf1, 0x3d,
   258  							0x9f, 0x9a, 0x75, 0x69, 0xab, 0x16, 0xa3, 0x27,
   259  							0x86, 0xaf, 0x7d, 0x7e, 0x2d, 0xe0, 0x92, 0x65,
   260  							0xe4, 0x1c, 0x61, 0xd0, 0x78, 0x29, 0x4e, 0xcf,
   261  						}), // cf4e2978d0611ce46592e02d7e7daf8627a316ab69759a9f3df109a7f2bf3ec3
   262  						Index: 1,
   263  					},
   264  					SignatureScript: []byte{
   265  						0x47, // OP_DATA_71
   266  						0x30, 0x44, 0x02, 0x20, 0x03, 0x2d, 0x30, 0xdf,
   267  						0x5e, 0xe6, 0xf5, 0x7f, 0xa4, 0x6c, 0xdd, 0xb5,
   268  						0xeb, 0x8d, 0x0d, 0x9f, 0xe8, 0xde, 0x6b, 0x34,
   269  						0x2d, 0x27, 0x94, 0x2a, 0xe9, 0x0a, 0x32, 0x31,
   270  						0xe0, 0xba, 0x33, 0x3e, 0x02, 0x20, 0x3d, 0xee,
   271  						0xe8, 0x06, 0x0f, 0xdc, 0x70, 0x23, 0x0a, 0x7f,
   272  						0x5b, 0x4a, 0xd7, 0xd7, 0xbc, 0x3e, 0x62, 0x8c,
   273  						0xbe, 0x21, 0x9a, 0x88, 0x6b, 0x84, 0x26, 0x9e,
   274  						0xae, 0xb8, 0x1e, 0x26, 0xb4, 0xfe, 0x01,
   275  						0x41, // OP_DATA_65
   276  						0x04, 0xae, 0x31, 0xc3, 0x1b, 0xf9, 0x12, 0x78,
   277  						0xd9, 0x9b, 0x83, 0x77, 0xa3, 0x5b, 0xbc, 0xe5,
   278  						0xb2, 0x7d, 0x9f, 0xff, 0x15, 0x45, 0x68, 0x39,
   279  						0xe9, 0x19, 0x45, 0x3f, 0xc7, 0xb3, 0xf7, 0x21,
   280  						0xf0, 0xba, 0x40, 0x3f, 0xf9, 0x6c, 0x9d, 0xee,
   281  						0xb6, 0x80, 0xe5, 0xfd, 0x34, 0x1c, 0x0f, 0xc3,
   282  						0xa7, 0xb9, 0x0d, 0xa4, 0x63, 0x1e, 0xe3, 0x95,
   283  						0x60, 0x63, 0x9d, 0xb4, 0x62, 0xe9, 0xcb, 0x85,
   284  						0x0f, // 65-byte pubkey
   285  					},
   286  					Sequence: 0xffffffff,
   287  				},
   288  			},
   289  			TxOut: []*wire.TxOut{
   290  				{
   291  					Value: 0xf4240, // 1000000
   292  					PkScript: []byte{
   293  						0x76, // OP_DUP
   294  						0xa9, // OP_HASH160
   295  						0x14, // OP_DATA_20
   296  						0xb0, 0xdc, 0xbf, 0x97, 0xea, 0xbf, 0x44, 0x04,
   297  						0xe3, 0x1d, 0x95, 0x24, 0x77, 0xce, 0x82, 0x2d,
   298  						0xad, 0xbe, 0x7e, 0x10,
   299  						0x88, // OP_EQUALVERIFY
   300  						0xac, // OP_CHECKSIG
   301  					},
   302  				},
   303  				{
   304  					Value: 0x11d260c0, // 299000000
   305  					PkScript: []byte{
   306  						0x76, // OP_DUP
   307  						0xa9, // OP_HASH160
   308  						0x14, // OP_DATA_20
   309  						0x6b, 0x12, 0x81, 0xee, 0xc2, 0x5a, 0xb4, 0xe1,
   310  						0xe0, 0x79, 0x3f, 0xf4, 0xe0, 0x8a, 0xb1, 0xab,
   311  						0xb3, 0x40, 0x9c, 0xd9,
   312  						0x88, // OP_EQUALVERIFY
   313  						0xac, // OP_CHECKSIG
   314  					},
   315  				},
   316  			},
   317  			LockTime: 0,
   318  		},
   319  		{
   320  			Version: 1,
   321  			TxIn: []*wire.TxIn{
   322  				{
   323  					PreviousOutPoint: wire.OutPoint{
   324  						Hash: wire.ShaHash([32]byte{ // Make go vet happy.
   325  							0x0b, 0x60, 0x72, 0xb3, 0x86, 0xd4, 0xa7, 0x73,
   326  							0x23, 0x52, 0x37, 0xf6, 0x4c, 0x11, 0x26, 0xac,
   327  							0x3b, 0x24, 0x0c, 0x84, 0xb9, 0x17, 0xa3, 0x90,
   328  							0x9b, 0xa1, 0xc4, 0x3d, 0xed, 0x5f, 0x51, 0xf4,
   329  						}), // f4515fed3dc4a19b90a317b9840c243bac26114cf637522373a7d486b372600b
   330  						Index: 0,
   331  					},
   332  					SignatureScript: []byte{
   333  						0x49, // OP_DATA_73
   334  						0x30, 0x46, 0x02, 0x21, 0x00, 0xbb, 0x1a, 0xd2,
   335  						0x6d, 0xf9, 0x30, 0xa5, 0x1c, 0xce, 0x11, 0x0c,
   336  						0xf4, 0x4f, 0x7a, 0x48, 0xc3, 0xc5, 0x61, 0xfd,
   337  						0x97, 0x75, 0x00, 0xb1, 0xae, 0x5d, 0x6b, 0x6f,
   338  						0xd1, 0x3d, 0x0b, 0x3f, 0x4a, 0x02, 0x21, 0x00,
   339  						0xc5, 0xb4, 0x29, 0x51, 0xac, 0xed, 0xff, 0x14,
   340  						0xab, 0xba, 0x27, 0x36, 0xfd, 0x57, 0x4b, 0xdb,
   341  						0x46, 0x5f, 0x3e, 0x6f, 0x8d, 0xa1, 0x2e, 0x2c,
   342  						0x53, 0x03, 0x95, 0x4a, 0xca, 0x7f, 0x78, 0xf3,
   343  						0x01, // 73-byte signature
   344  						0x41, // OP_DATA_65
   345  						0x04, 0xa7, 0x13, 0x5b, 0xfe, 0x82, 0x4c, 0x97,
   346  						0xec, 0xc0, 0x1e, 0xc7, 0xd7, 0xe3, 0x36, 0x18,
   347  						0x5c, 0x81, 0xe2, 0xaa, 0x2c, 0x41, 0xab, 0x17,
   348  						0x54, 0x07, 0xc0, 0x94, 0x84, 0xce, 0x96, 0x94,
   349  						0xb4, 0x49, 0x53, 0xfc, 0xb7, 0x51, 0x20, 0x65,
   350  						0x64, 0xa9, 0xc2, 0x4d, 0xd0, 0x94, 0xd4, 0x2f,
   351  						0xdb, 0xfd, 0xd5, 0xaa, 0xd3, 0xe0, 0x63, 0xce,
   352  						0x6a, 0xf4, 0xcf, 0xaa, 0xea, 0x4e, 0xa1, 0x4f,
   353  						0xbb, // 65-byte pubkey
   354  					},
   355  					Sequence: 0xffffffff,
   356  				},
   357  			},
   358  			TxOut: []*wire.TxOut{
   359  				{
   360  					Value: 0xf4240, // 1000000
   361  					PkScript: []byte{
   362  						0x76, // OP_DUP
   363  						0xa9, // OP_HASH160
   364  						0x14, // OP_DATA_20
   365  						0x39, 0xaa, 0x3d, 0x56, 0x9e, 0x06, 0xa1, 0xd7,
   366  						0x92, 0x6d, 0xc4, 0xbe, 0x11, 0x93, 0xc9, 0x9b,
   367  						0xf2, 0xeb, 0x9e, 0xe0,
   368  						0x88, // OP_EQUALVERIFY
   369  						0xac, // OP_CHECKSIG
   370  					},
   371  				},
   372  			},
   373  			LockTime: 0,
   374  		},
   375  	},
   376  }