github.com/lbryio/lbcd@v0.22.119/wire/msgheaders_test.go (about)

     1  // Copyright (c) 2013-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 wire
     6  
     7  import (
     8  	"bytes"
     9  	"io"
    10  	"reflect"
    11  	"testing"
    12  
    13  	"github.com/davecgh/go-spew/spew"
    14  )
    15  
    16  // TestHeaders tests the MsgHeaders API.
    17  func TestHeaders(t *testing.T) {
    18  	pver := uint32(60002)
    19  
    20  	// Ensure the command is expected value.
    21  	wantCmd := "headers"
    22  	msg := NewMsgHeaders()
    23  	if cmd := msg.Command(); cmd != wantCmd {
    24  		t.Errorf("NewMsgHeaders: wrong command - got %v want %v",
    25  			cmd, wantCmd)
    26  	}
    27  
    28  	// Ensure max payload is expected value for latest protocol version.
    29  	// Num headers (varInt) + max allowed headers (header length + 1 byte
    30  	// for the number of transactions which is always 0).
    31  	wantPayload := uint32(226009)
    32  	maxPayload := msg.MaxPayloadLength(pver)
    33  	if maxPayload != wantPayload {
    34  		t.Errorf("MaxPayloadLength: wrong max payload length for "+
    35  			"protocol version %d - got %v, want %v", pver,
    36  			maxPayload, wantPayload)
    37  	}
    38  
    39  	// Ensure headers are added properly.
    40  	bh := &blockOne.Header
    41  	msg.AddBlockHeader(bh)
    42  	if !reflect.DeepEqual(msg.Headers[0], bh) {
    43  		t.Errorf("AddHeader: wrong header - got %v, want %v",
    44  			spew.Sdump(msg.Headers),
    45  			spew.Sdump(bh))
    46  	}
    47  
    48  	// Ensure adding more than the max allowed headers per message returns
    49  	// error.
    50  	var err error
    51  	for i := 0; i < MaxBlockHeadersPerMsg+1; i++ {
    52  		err = msg.AddBlockHeader(bh)
    53  	}
    54  	if reflect.TypeOf(err) != reflect.TypeOf(&MessageError{}) {
    55  		t.Errorf("AddBlockHeader: expected error on too many headers " +
    56  			"not received")
    57  	}
    58  }
    59  
    60  // TestHeadersWire tests the MsgHeaders wire encode and decode for various
    61  // numbers of headers and protocol versions.
    62  func TestHeadersWire(t *testing.T) {
    63  	hash := mainNetGenesisHash
    64  	merkleHash := blockOne.Header.MerkleRoot
    65  	bits := uint32(0x1d00ffff)
    66  	nonce := uint32(0x9962e301)
    67  	bh := NewBlockHeader(1, &hash, &merkleHash, &merkleHash, bits, nonce)
    68  	bh.Version = blockOne.Header.Version
    69  	bh.Timestamp = blockOne.Header.Timestamp
    70  
    71  	// Empty headers message.
    72  	noHeaders := NewMsgHeaders()
    73  	noHeadersEncoded := []byte{
    74  		0x00, // Varint for number of headers
    75  	}
    76  
    77  	// Headers message with one header.
    78  	oneHeader := NewMsgHeaders()
    79  	oneHeader.AddBlockHeader(bh)
    80  	oneHeaderEncoded := []byte{
    81  		0x01,                   // VarInt for number of headers.
    82  		0x01, 0x00, 0x00, 0x00, // Version 1
    83  		0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
    84  		0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
    85  		0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
    86  		0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock
    87  		0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44,
    88  		0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67,
    89  		0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1,
    90  		0xcd, 0xb6, 0x06, 0xe8, 0x57, 0x23, 0x3e, 0x0e, // MerkleRoot
    91  		0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44,
    92  		0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67,
    93  		0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1,
    94  		0xcd, 0xb6, 0x06, 0xe8, 0x57, 0x23, 0x3e, 0x0e, // ClaimTrie
    95  		0x61, 0xbc, 0x66, 0x49, // Timestamp
    96  		0xff, 0xff, 0x00, 0x1d, // Bits
    97  		0x01, 0xe3, 0x62, 0x99, // Nonce
    98  		0x00, // TxnCount (0 for headers message)
    99  	}
   100  
   101  	tests := []struct {
   102  		in   *MsgHeaders     // Message to encode
   103  		out  *MsgHeaders     // Expected decoded message
   104  		buf  []byte          // Wire encoding
   105  		pver uint32          // Protocol version for wire encoding
   106  		enc  MessageEncoding // Message encoding format
   107  	}{
   108  		// Latest protocol version with no headers.
   109  		{
   110  			noHeaders,
   111  			noHeaders,
   112  			noHeadersEncoded,
   113  			ProtocolVersion,
   114  			BaseEncoding,
   115  		},
   116  
   117  		// Latest protocol version with one header.
   118  		{
   119  			oneHeader,
   120  			oneHeader,
   121  			oneHeaderEncoded,
   122  			ProtocolVersion,
   123  			BaseEncoding,
   124  		},
   125  
   126  		// Protocol version BIP0035Version with no headers.
   127  		{
   128  			noHeaders,
   129  			noHeaders,
   130  			noHeadersEncoded,
   131  			BIP0035Version,
   132  			BaseEncoding,
   133  		},
   134  
   135  		// Protocol version BIP0035Version with one header.
   136  		{
   137  			oneHeader,
   138  			oneHeader,
   139  			oneHeaderEncoded,
   140  			BIP0035Version,
   141  			BaseEncoding,
   142  		},
   143  
   144  		// Protocol version BIP0031Version with no headers.
   145  		{
   146  			noHeaders,
   147  			noHeaders,
   148  			noHeadersEncoded,
   149  			BIP0031Version,
   150  			BaseEncoding,
   151  		},
   152  
   153  		// Protocol version BIP0031Version with one header.
   154  		{
   155  			oneHeader,
   156  			oneHeader,
   157  			oneHeaderEncoded,
   158  			BIP0031Version,
   159  			BaseEncoding,
   160  		},
   161  		// Protocol version NetAddressTimeVersion with no headers.
   162  		{
   163  			noHeaders,
   164  			noHeaders,
   165  			noHeadersEncoded,
   166  			NetAddressTimeVersion,
   167  			BaseEncoding,
   168  		},
   169  
   170  		// Protocol version NetAddressTimeVersion with one header.
   171  		{
   172  			oneHeader,
   173  			oneHeader,
   174  			oneHeaderEncoded,
   175  			NetAddressTimeVersion,
   176  			BaseEncoding,
   177  		},
   178  
   179  		// Protocol version MultipleAddressVersion with no headers.
   180  		{
   181  			noHeaders,
   182  			noHeaders,
   183  			noHeadersEncoded,
   184  			MultipleAddressVersion,
   185  			BaseEncoding,
   186  		},
   187  
   188  		// Protocol version MultipleAddressVersion with one header.
   189  		{
   190  			oneHeader,
   191  			oneHeader,
   192  			oneHeaderEncoded,
   193  			MultipleAddressVersion,
   194  			BaseEncoding,
   195  		},
   196  	}
   197  
   198  	t.Logf("Running %d tests", len(tests))
   199  	for i, test := range tests {
   200  		// Encode the message to wire format.
   201  		var buf bytes.Buffer
   202  		err := test.in.BtcEncode(&buf, test.pver, test.enc)
   203  		if err != nil {
   204  			t.Errorf("BtcEncode #%d error %v", i, err)
   205  			continue
   206  		}
   207  		if !bytes.Equal(buf.Bytes(), test.buf) {
   208  			t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
   209  				spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
   210  			continue
   211  		}
   212  
   213  		// Decode the message from wire format.
   214  		var msg MsgHeaders
   215  		rbuf := bytes.NewReader(test.buf)
   216  		err = msg.BtcDecode(rbuf, test.pver, test.enc)
   217  		if err != nil {
   218  			t.Errorf("BtcDecode #%d error %v", i, err)
   219  			continue
   220  		}
   221  		if !reflect.DeepEqual(&msg, test.out) {
   222  			t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
   223  				spew.Sdump(&msg), spew.Sdump(test.out))
   224  			continue
   225  		}
   226  	}
   227  }
   228  
   229  // TestHeadersWireErrors performs negative tests against wire encode and decode
   230  // of MsgHeaders to confirm error paths work correctly.
   231  func TestHeadersWireErrors(t *testing.T) {
   232  	pver := ProtocolVersion
   233  	wireErr := &MessageError{}
   234  
   235  	hash := mainNetGenesisHash
   236  	merkleHash := blockOne.Header.MerkleRoot
   237  	bits := uint32(0x1d00ffff)
   238  	nonce := uint32(0x9962e301)
   239  	bh := NewBlockHeader(1, &hash, &merkleHash, &merkleHash, bits, nonce)
   240  	bh.Version = blockOne.Header.Version
   241  	bh.Timestamp = blockOne.Header.Timestamp
   242  
   243  	// Headers message with one header.
   244  	oneHeader := NewMsgHeaders()
   245  	oneHeader.AddBlockHeader(bh)
   246  	oneHeaderEncoded := []byte{
   247  		0x01,                   // VarInt for number of headers.
   248  		0x01, 0x00, 0x00, 0x00, // Version 1
   249  		0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
   250  		0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
   251  		0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
   252  		0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock
   253  		0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44,
   254  		0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67,
   255  		0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1,
   256  		0xcd, 0xb6, 0x06, 0xe8, 0x57, 0x23, 0x3e, 0x0e, // MerkleRoot
   257  		0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44,
   258  		0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67,
   259  		0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1,
   260  		0xcd, 0xb6, 0x06, 0xe8, 0x57, 0x23, 0x3e, 0x0e, // ClaimTrie
   261  		0x61, 0xbc, 0x66, 0x49, // Timestamp
   262  		0xff, 0xff, 0x00, 0x1d, // Bits
   263  		0x01, 0xe3, 0x62, 0x99, // Nonce
   264  		0x00, // TxnCount (0 for headers message)
   265  	}
   266  
   267  	// Message that forces an error by having more than the max allowed
   268  	// headers.
   269  	maxHeaders := NewMsgHeaders()
   270  	for i := 0; i < MaxBlockHeadersPerMsg; i++ {
   271  		maxHeaders.AddBlockHeader(bh)
   272  	}
   273  	maxHeaders.Headers = append(maxHeaders.Headers, bh)
   274  	maxHeadersEncoded := []byte{
   275  		0xfd, 0xd1, 0x07, // Varint for number of addresses (2001)7D1
   276  	}
   277  
   278  	// Intentionally invalid block header that has a transaction count used
   279  	// to force errors.
   280  	bhTrans := NewBlockHeader(1, &hash, &merkleHash, &merkleHash, bits, nonce)
   281  	bhTrans.Version = blockOne.Header.Version
   282  	bhTrans.Timestamp = blockOne.Header.Timestamp
   283  
   284  	transHeader := NewMsgHeaders()
   285  	transHeader.AddBlockHeader(bhTrans)
   286  	transHeaderEncoded := []byte{
   287  		0x01,                   // VarInt for number of headers.
   288  		0x01, 0x00, 0x00, 0x00, // Version 1
   289  		0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
   290  		0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
   291  		0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
   292  		0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock
   293  		0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44,
   294  		0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67,
   295  		0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1,
   296  		0xcd, 0xb6, 0x06, 0xe8, 0x57, 0x23, 0x3e, 0x0e, // MerkleRoot
   297  		0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44,
   298  		0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67,
   299  		0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1,
   300  		0xcd, 0xb6, 0x06, 0xe8, 0x57, 0x23, 0x3e, 0x0e, // ClaimTrie
   301  		0x61, 0xbc, 0x66, 0x49, // Timestamp
   302  		0xff, 0xff, 0x00, 0x1d, // Bits
   303  		0x01, 0xe3, 0x62, 0x99, // Nonce
   304  		0x01, // TxnCount (should be 0 for headers message, but 1 to force error)
   305  	}
   306  
   307  	tests := []struct {
   308  		in       *MsgHeaders     // Value to encode
   309  		buf      []byte          // Wire encoding
   310  		pver     uint32          // Protocol version for wire encoding
   311  		enc      MessageEncoding // Message encoding format
   312  		max      int             // Max size of fixed buffer to induce errors
   313  		writeErr error           // Expected write error
   314  		readErr  error           // Expected read error
   315  	}{
   316  		// Latest protocol version with intentional read/write errors.
   317  		// Force error in header count.
   318  		{oneHeader, oneHeaderEncoded, pver, BaseEncoding, 0, io.ErrShortWrite, io.EOF},
   319  		// Force error in block header.
   320  		{oneHeader, oneHeaderEncoded, pver, BaseEncoding, 5, io.ErrShortWrite, io.EOF},
   321  		// Force error with greater than max headers.
   322  		{maxHeaders, maxHeadersEncoded, pver, BaseEncoding, 3, wireErr, wireErr},
   323  		// Force error with number of transactions.
   324  		{transHeader, transHeaderEncoded, pver, BaseEncoding, 81 + 32, io.ErrShortWrite, io.EOF},
   325  		// Force error with included transactions.
   326  		{transHeader, transHeaderEncoded, pver, BaseEncoding, len(transHeaderEncoded), nil, wireErr},
   327  	}
   328  
   329  	t.Logf("Running %d tests", len(tests))
   330  	for i, test := range tests {
   331  		// Encode to wire format.
   332  		w := newFixedWriter(test.max)
   333  		err := test.in.BtcEncode(w, test.pver, test.enc)
   334  		if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) {
   335  			t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
   336  				i, err, test.writeErr)
   337  			continue
   338  		}
   339  
   340  		// For errors which are not of type MessageError, check them for
   341  		// equality.
   342  		if _, ok := err.(*MessageError); !ok {
   343  			if err != test.writeErr {
   344  				t.Errorf("BtcEncode #%d wrong error got: %v, "+
   345  					"want: %v", i, err, test.writeErr)
   346  				continue
   347  			}
   348  		}
   349  
   350  		// Decode from wire format.
   351  		var msg MsgHeaders
   352  		r := newFixedReader(test.max, test.buf)
   353  		err = msg.BtcDecode(r, test.pver, test.enc)
   354  		if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
   355  			t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
   356  				i, err, test.readErr)
   357  			continue
   358  		}
   359  
   360  		// For errors which are not of type MessageError, check them for
   361  		// equality.
   362  		if _, ok := err.(*MessageError); !ok {
   363  			if err != test.readErr {
   364  				t.Errorf("BtcDecode #%d wrong error got: %v, "+
   365  					"want: %v", i, err, test.readErr)
   366  				continue
   367  			}
   368  		}
   369  
   370  	}
   371  }