github.com/CommerciumBlockchain/go-commercium@v0.0.0-20220709212705-b46438a77516/cmd/devp2p/internal/ethtest/chain_test.go (about)

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