github.com/gilgames000/kcc-geth@v1.0.6/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/ethereum/go-ethereum/eth/protocols/eth" 25 "github.com/ethereum/go-ethereum/p2p" 26 "github.com/stretchr/testify/assert" 27 ) 28 29 // TestEthProtocolNegotiation tests whether the test suite 30 // can negotiate the highest eth protocol in a status message exchange 31 func TestEthProtocolNegotiation(t *testing.T) { 32 var tests = []struct { 33 conn *Conn 34 caps []p2p.Cap 35 expected uint32 36 }{ 37 { 38 conn: &Conn{}, 39 caps: []p2p.Cap{ 40 {Name: "eth", Version: 63}, 41 {Name: "eth", Version: 64}, 42 {Name: "eth", Version: 65}, 43 }, 44 expected: uint32(65), 45 }, 46 { 47 conn: &Conn{}, 48 caps: []p2p.Cap{ 49 {Name: "eth", Version: 0}, 50 {Name: "eth", Version: 89}, 51 {Name: "eth", Version: 65}, 52 }, 53 expected: uint32(65), 54 }, 55 { 56 conn: &Conn{}, 57 caps: []p2p.Cap{ 58 {Name: "eth", Version: 63}, 59 {Name: "eth", Version: 64}, 60 {Name: "wrongProto", Version: 65}, 61 }, 62 expected: uint32(64), 63 }, 64 } 65 66 for i, tt := range tests { 67 t.Run(strconv.Itoa(i), func(t *testing.T) { 68 tt.conn.negotiateEthProtocol(tt.caps) 69 assert.Equal(t, tt.expected, uint32(tt.conn.ethProtocolVersion)) 70 }) 71 } 72 } 73 74 // TestChain_GetHeaders tests whether the test suite can correctly 75 // respond to a GetBlockHeaders request from a node. 76 func TestChain_GetHeaders(t *testing.T) { 77 chainFile, err := filepath.Abs("./testdata/chain.rlp") 78 if err != nil { 79 t.Fatal(err) 80 } 81 genesisFile, err := filepath.Abs("./testdata/genesis.json") 82 if err != nil { 83 t.Fatal(err) 84 } 85 86 chain, err := loadChain(chainFile, genesisFile) 87 if err != nil { 88 t.Fatal(err) 89 } 90 91 var tests = []struct { 92 req GetBlockHeaders 93 expected BlockHeaders 94 }{ 95 { 96 req: GetBlockHeaders{ 97 Origin: eth.HashOrNumber{ 98 Number: uint64(2), 99 }, 100 Amount: uint64(5), 101 Skip: 1, 102 Reverse: false, 103 }, 104 expected: BlockHeaders{ 105 chain.blocks[2].Header(), 106 chain.blocks[4].Header(), 107 chain.blocks[6].Header(), 108 chain.blocks[8].Header(), 109 chain.blocks[10].Header(), 110 }, 111 }, 112 { 113 req: GetBlockHeaders{ 114 Origin: eth.HashOrNumber{ 115 Number: uint64(chain.Len() - 1), 116 }, 117 Amount: uint64(3), 118 Skip: 0, 119 Reverse: true, 120 }, 121 expected: BlockHeaders{ 122 chain.blocks[chain.Len()-1].Header(), 123 chain.blocks[chain.Len()-2].Header(), 124 chain.blocks[chain.Len()-3].Header(), 125 }, 126 }, 127 { 128 req: GetBlockHeaders{ 129 Origin: eth.HashOrNumber{ 130 Hash: chain.Head().Hash(), 131 }, 132 Amount: uint64(1), 133 Skip: 0, 134 Reverse: false, 135 }, 136 expected: BlockHeaders{ 137 chain.Head().Header(), 138 }, 139 }, 140 } 141 142 for i, tt := range tests { 143 t.Run(strconv.Itoa(i), func(t *testing.T) { 144 headers, err := chain.GetHeaders(tt.req) 145 if err != nil { 146 t.Fatal(err) 147 } 148 assert.Equal(t, headers, tt.expected) 149 }) 150 } 151 }