github.com/theQRL/go-zond@v0.2.1/cmd/devp2p/internal/zondtest/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 zondtest 18 19 // TODO(now.youtrack.cloud/issue/TGZ-6) 20 /* 21 import ( 22 "strconv" 23 "testing" 24 25 "github.com/stretchr/testify/assert" 26 "github.com/theQRL/go-zond/p2p" 27 ) 28 29 // TestEthProtocolNegotiation tests whether the test suite 30 // can negotiate the highest zond 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: "zond", Version: 63}, 43 {Name: "zond", Version: 64}, 44 {Name: "zond", Version: 65}, 45 }, 46 expected: uint32(65), 47 }, 48 { 49 conn: &Conn{ 50 ourHighestProtoVersion: 65, 51 }, 52 caps: []p2p.Cap{ 53 {Name: "zond", Version: 63}, 54 {Name: "zond", Version: 64}, 55 {Name: "zond", Version: 65}, 56 }, 57 expected: uint32(65), 58 }, 59 { 60 conn: &Conn{ 61 ourHighestProtoVersion: 65, 62 }, 63 caps: []p2p.Cap{ 64 {Name: "zond", Version: 63}, 65 {Name: "zond", Version: 64}, 66 {Name: "zond", Version: 65}, 67 }, 68 expected: uint32(65), 69 }, 70 { 71 conn: &Conn{ 72 ourHighestProtoVersion: 64, 73 }, 74 caps: []p2p.Cap{ 75 {Name: "zond", Version: 63}, 76 {Name: "zond", Version: 64}, 77 {Name: "zond", Version: 65}, 78 }, 79 expected: 64, 80 }, 81 { 82 conn: &Conn{ 83 ourHighestProtoVersion: 65, 84 }, 85 caps: []p2p.Cap{ 86 {Name: "zond", Version: 0}, 87 {Name: "zond", Version: 89}, 88 {Name: "zond", Version: 65}, 89 }, 90 expected: uint32(65), 91 }, 92 { 93 conn: &Conn{ 94 ourHighestProtoVersion: 64, 95 }, 96 caps: []p2p.Cap{ 97 {Name: "zond", Version: 63}, 98 {Name: "zond", 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: "zond", Version: 63}, 109 {Name: "zond", 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 /* 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 } 203 */