github.com/core-coin/go-core/v2@v2.1.9/cmd/devp2p/internal/xcbtest/chain_test.go (about)

     1  // Copyright 2020 by the Authors
     2  // This file is part of go-core.
     3  //
     4  // go-core is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-core is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-core. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package xcbtest
    18  
    19  import (
    20  	"path/filepath"
    21  	"strconv"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  
    26  	"github.com/core-coin/go-core/v2/common"
    27  	"github.com/core-coin/go-core/v2/p2p"
    28  )
    29  
    30  // TestXcbProtocolNegotiation tests whether the test suite
    31  // can negotiate the highest xcb protocol in a status message exchange
    32  func TestXcbProtocolNegotiation(t *testing.T) {
    33  	var tests = []struct {
    34  		conn     *Conn
    35  		caps     []p2p.Cap
    36  		expected uint32
    37  	}{
    38  		{
    39  			conn: &Conn{},
    40  			caps: []p2p.Cap{
    41  				{Name: "xcb", Version: 63},
    42  				{Name: "xcb", Version: 64},
    43  				{Name: "xcb", Version: 65},
    44  			},
    45  			expected: uint32(65),
    46  		},
    47  		{
    48  			conn: &Conn{},
    49  			caps: []p2p.Cap{
    50  				{Name: "xcb", Version: 0},
    51  				{Name: "xcb", Version: 89},
    52  				{Name: "xcb", Version: 65},
    53  			},
    54  			expected: uint32(65),
    55  		},
    56  		{
    57  			conn: &Conn{},
    58  			caps: []p2p.Cap{
    59  				{Name: "xcb", Version: 63},
    60  				{Name: "xcb", Version: 64},
    61  				{Name: "wrongProto", Version: 65},
    62  			},
    63  			expected: uint32(64),
    64  		},
    65  	}
    66  
    67  	for i, tt := range tests {
    68  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    69  			tt.conn.negotiateXcbProtocol(tt.caps)
    70  			assert.Equal(t, tt.expected, uint32(tt.conn.xcbProtocolVersion))
    71  		})
    72  	}
    73  }
    74  
    75  // TestChain_GetHeaders tests whether the test suite can correctly
    76  // respond to a GetBlockHeaders request from a node.
    77  func TestChain_GetHeaders(t *testing.T) {
    78  	common.DefaultNetworkID = 19763
    79  	chainFile, err := filepath.Abs("./testdata/chain.rlp")
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  	genesisFile, err := filepath.Abs("./testdata/genesis.json")
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  
    88  	chain, err := loadChain(chainFile, genesisFile)
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  
    93  	var tests = []struct {
    94  		req      GetBlockHeaders
    95  		expected BlockHeaders
    96  	}{
    97  		{
    98  			req: GetBlockHeaders{
    99  				Origin: hashOrNumber{
   100  					Number: uint64(2),
   101  				},
   102  				Amount:  uint64(5),
   103  				Skip:    1,
   104  				Reverse: false,
   105  			},
   106  			expected: BlockHeaders{
   107  				chain.blocks[2].Header(),
   108  				chain.blocks[4].Header(),
   109  				chain.blocks[6].Header(),
   110  				chain.blocks[8].Header(),
   111  				chain.blocks[10].Header(),
   112  			},
   113  		},
   114  		{
   115  			req: GetBlockHeaders{
   116  				Origin: hashOrNumber{
   117  					Number: uint64(chain.Len() - 1),
   118  				},
   119  				Amount:  uint64(3),
   120  				Skip:    0,
   121  				Reverse: true,
   122  			},
   123  			expected: BlockHeaders{
   124  				chain.blocks[chain.Len()-1].Header(),
   125  				chain.blocks[chain.Len()-2].Header(),
   126  				chain.blocks[chain.Len()-3].Header(),
   127  			},
   128  		},
   129  		{
   130  			req: GetBlockHeaders{
   131  				Origin: hashOrNumber{
   132  					Hash: chain.Head().Hash(),
   133  				},
   134  				Amount:  uint64(1),
   135  				Skip:    0,
   136  				Reverse: false,
   137  			},
   138  			expected: BlockHeaders{
   139  				chain.Head().Header(),
   140  			},
   141  		},
   142  	}
   143  
   144  	for i, tt := range tests {
   145  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   146  			headers, err := chain.GetHeaders(tt.req)
   147  			if err != nil {
   148  				t.Fatal(err)
   149  			}
   150  			assert.Equal(t, headers, tt.expected)
   151  		})
   152  	}
   153  	common.DefaultNetworkID = 1
   154  }