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