github.com/theQRL/go-zond@v0.1.1/cmd/devp2p/internal/ethtest/chain_test.go (about)

     1  // Copyright 2020 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum 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-ethereum 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-ethereum. 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/stretchr/testify/assert"
    25  	"github.com/theQRL/go-zond/core/types"
    26  	"github.com/theQRL/go-zond/zond/protocols/zond"
    27  	"github.com/theQRL/go-zond/p2p"
    28  )
    29  
    30  // TestEthProtocolNegotiation tests whether the test suite
    31  // can negotiate the highest zond protocol in a status message exchange
    32  func TestEthProtocolNegotiation(t *testing.T) {
    33  	var tests = []struct {
    34  		conn     *Conn
    35  		caps     []p2p.Cap
    36  		expected uint32
    37  	}{
    38  		{
    39  			conn: &Conn{
    40  				ourHighestProtoVersion: 65,
    41  			},
    42  			caps: []p2p.Cap{
    43  				{Name: "zond", Version: 63},
    44  				{Name: "zond", Version: 64},
    45  				{Name: "zond", Version: 65},
    46  			},
    47  			expected: uint32(65),
    48  		},
    49  		{
    50  			conn: &Conn{
    51  				ourHighestProtoVersion: 65,
    52  			},
    53  			caps: []p2p.Cap{
    54  				{Name: "zond", Version: 63},
    55  				{Name: "zond", Version: 64},
    56  				{Name: "zond", Version: 65},
    57  			},
    58  			expected: uint32(65),
    59  		},
    60  		{
    61  			conn: &Conn{
    62  				ourHighestProtoVersion: 65,
    63  			},
    64  			caps: []p2p.Cap{
    65  				{Name: "zond", Version: 63},
    66  				{Name: "zond", Version: 64},
    67  				{Name: "zond", Version: 65},
    68  			},
    69  			expected: uint32(65),
    70  		},
    71  		{
    72  			conn: &Conn{
    73  				ourHighestProtoVersion: 64,
    74  			},
    75  			caps: []p2p.Cap{
    76  				{Name: "zond", Version: 63},
    77  				{Name: "zond", Version: 64},
    78  				{Name: "zond", Version: 65},
    79  			},
    80  			expected: 64,
    81  		},
    82  		{
    83  			conn: &Conn{
    84  				ourHighestProtoVersion: 65,
    85  			},
    86  			caps: []p2p.Cap{
    87  				{Name: "zond", Version: 0},
    88  				{Name: "zond", Version: 89},
    89  				{Name: "zond", Version: 65},
    90  			},
    91  			expected: uint32(65),
    92  		},
    93  		{
    94  			conn: &Conn{
    95  				ourHighestProtoVersion: 64,
    96  			},
    97  			caps: []p2p.Cap{
    98  				{Name: "zond", Version: 63},
    99  				{Name: "zond", Version: 64},
   100  				{Name: "wrongProto", Version: 65},
   101  			},
   102  			expected: uint32(64),
   103  		},
   104  		{
   105  			conn: &Conn{
   106  				ourHighestProtoVersion: 65,
   107  			},
   108  			caps: []p2p.Cap{
   109  				{Name: "zond", Version: 63},
   110  				{Name: "zond", Version: 64},
   111  				{Name: "wrongProto", Version: 65},
   112  			},
   113  			expected: uint32(64),
   114  		},
   115  	}
   116  
   117  	for i, tt := range tests {
   118  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   119  			tt.conn.negotiateEthProtocol(tt.caps)
   120  			assert.Equal(t, tt.expected, uint32(tt.conn.negotiatedProtoVersion))
   121  		})
   122  	}
   123  }
   124  
   125  // TestChain_GetHeaders tests whether the test suite can correctly
   126  // respond to a GetBlockHeaders request from a node.
   127  func TestChain_GetHeaders(t *testing.T) {
   128  	chainFile, err := filepath.Abs("./testdata/chain.rlp")
   129  	if err != nil {
   130  		t.Fatal(err)
   131  	}
   132  	genesisFile, err := filepath.Abs("./testdata/genesis.json")
   133  	if err != nil {
   134  		t.Fatal(err)
   135  	}
   136  
   137  	chain, err := loadChain(chainFile, genesisFile)
   138  	if err != nil {
   139  		t.Fatal(err)
   140  	}
   141  
   142  	var tests = []struct {
   143  		req      GetBlockHeaders
   144  		expected []*types.Header
   145  	}{
   146  		{
   147  			req: GetBlockHeaders{
   148  				GetBlockHeadersPacket: &zond.GetBlockHeadersPacket{
   149  					Origin:  zond.HashOrNumber{Number: uint64(2)},
   150  					Amount:  uint64(5),
   151  					Skip:    1,
   152  					Reverse: false,
   153  				},
   154  			},
   155  			expected: []*types.Header{
   156  				chain.blocks[2].Header(),
   157  				chain.blocks[4].Header(),
   158  				chain.blocks[6].Header(),
   159  				chain.blocks[8].Header(),
   160  				chain.blocks[10].Header(),
   161  			},
   162  		},
   163  		{
   164  			req: GetBlockHeaders{
   165  				GetBlockHeadersPacket: &zond.GetBlockHeadersPacket{
   166  					Origin:  zond.HashOrNumber{Number: uint64(chain.Len() - 1)},
   167  					Amount:  uint64(3),
   168  					Skip:    0,
   169  					Reverse: true,
   170  				},
   171  			},
   172  			expected: []*types.Header{
   173  				chain.blocks[chain.Len()-1].Header(),
   174  				chain.blocks[chain.Len()-2].Header(),
   175  				chain.blocks[chain.Len()-3].Header(),
   176  			},
   177  		},
   178  		{
   179  			req: GetBlockHeaders{
   180  				GetBlockHeadersPacket: &zond.GetBlockHeadersPacket{
   181  					Origin:  zond.HashOrNumber{Hash: chain.Head().Hash()},
   182  					Amount:  uint64(1),
   183  					Skip:    0,
   184  					Reverse: false,
   185  				},
   186  			},
   187  			expected: []*types.Header{
   188  				chain.Head().Header(),
   189  			},
   190  		},
   191  	}
   192  
   193  	for i, tt := range tests {
   194  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   195  			headers, err := chain.GetHeaders(&tt.req)
   196  			if err != nil {
   197  				t.Fatal(err)
   198  			}
   199  			assert.Equal(t, headers, tt.expected)
   200  		})
   201  	}
   202  }