github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/p2p/discover/v4wire/v4wire_test.go (about) 1 // Copyright 2019 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 v4wire 18 19 import ( 20 "encoding/hex" 21 "net" 22 "reflect" 23 "testing" 24 25 "github.com/davecgh/go-spew/spew" 26 27 "github.com/scroll-tech/go-ethereum/crypto" 28 "github.com/scroll-tech/go-ethereum/rlp" 29 ) 30 31 // EIP-8 test vectors. 32 var testPackets = []struct { 33 input string 34 wantPacket interface{} 35 }{ 36 { 37 input: "71dbda3a79554728d4f94411e42ee1f8b0d561c10e1e5f5893367948c6a7d70bb87b235fa28a77070271b6c164a2dce8c7e13a5739b53b5e96f2e5acb0e458a02902f5965d55ecbeb2ebb6cabb8b2b232896a36b737666c55265ad0a68412f250001ea04cb847f000001820cfa8215a8d790000000000000000000000000000000018208ae820d058443b9a355", 38 wantPacket: &Ping{ 39 Version: 4, 40 From: Endpoint{net.ParseIP("127.0.0.1").To4(), 3322, 5544}, 41 To: Endpoint{net.ParseIP("::1"), 2222, 3333}, 42 Expiration: 1136239445, 43 }, 44 }, 45 { 46 input: "e9614ccfd9fc3e74360018522d30e1419a143407ffcce748de3e22116b7e8dc92ff74788c0b6663aaa3d67d641936511c8f8d6ad8698b820a7cf9e1be7155e9a241f556658c55428ec0563514365799a4be2be5a685a80971ddcfa80cb422cdd0101ec04cb847f000001820cfa8215a8d790000000000000000000000000000000018208ae820d058443b9a3550102", 47 wantPacket: &Ping{ 48 Version: 4, 49 From: Endpoint{net.ParseIP("127.0.0.1").To4(), 3322, 5544}, 50 To: Endpoint{net.ParseIP("::1"), 2222, 3333}, 51 Expiration: 1136239445, 52 ENRSeq: 1, 53 Rest: []rlp.RawValue{{0x02}}, 54 }, 55 }, 56 { 57 input: "c7c44041b9f7c7e41934417ebac9a8e1a4c6298f74553f2fcfdcae6ed6fe53163eb3d2b52e39fe91831b8a927bf4fc222c3902202027e5e9eb812195f95d20061ef5cd31d502e47ecb61183f74a504fe04c51e73df81f25c4d506b26db4517490103f84eb840ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31387574077f301b421bc84df7266c44e9e6d569fc56be00812904767bf5ccd1fc7f8443b9a35582999983999999280dc62cc8255c73471e0a61da0c89acdc0e035e260add7fc0c04ad9ebf3919644c91cb247affc82b69bd2ca235c71eab8e49737c937a2c396", 58 wantPacket: &Findnode{ 59 Target: hexPubkey("ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31387574077f301b421bc84df7266c44e9e6d569fc56be00812904767bf5ccd1fc7f"), 60 Expiration: 1136239445, 61 Rest: []rlp.RawValue{{0x82, 0x99, 0x99}, {0x83, 0x99, 0x99, 0x99}}, 62 }, 63 }, 64 { 65 input: "c679fc8fe0b8b12f06577f2e802d34f6fa257e6137a995f6f4cbfc9ee50ed3710faf6e66f932c4c8d81d64343f429651328758b47d3dbc02c4042f0fff6946a50f4a49037a72bb550f3a7872363a83e1b9ee6469856c24eb4ef80b7535bcf99c0004f9015bf90150f84d846321163782115c82115db8403155e1427f85f10a5c9a7755877748041af1bcd8d474ec065eb33df57a97babf54bfd2103575fa829115d224c523596b401065a97f74010610fce76382c0bf32f84984010203040101b840312c55512422cf9b8a4097e9a6ad79402e87a15ae909a4bfefa22398f03d20951933beea1e4dfa6f968212385e829f04c2d314fc2d4e255e0d3bc08792b069dbf8599020010db83c4d001500000000abcdef12820d05820d05b84038643200b172dcfef857492156971f0e6aa2c538d8b74010f8e140811d53b98c765dd2d96126051913f44582e8c199ad7c6d6819e9a56483f637feaac9448aacf8599020010db885a308d313198a2e037073488203e78203e8b8408dcab8618c3253b558d459da53bd8fa68935a719aff8b811197101a4b2b47dd2d47295286fc00cc081bb542d760717d1bdd6bec2c37cd72eca367d6dd3b9df738443b9a355010203b525a138aa34383fec3d2719a0", 66 wantPacket: &Neighbors{ 67 Nodes: []Node{ 68 { 69 ID: hexPubkey("3155e1427f85f10a5c9a7755877748041af1bcd8d474ec065eb33df57a97babf54bfd2103575fa829115d224c523596b401065a97f74010610fce76382c0bf32"), 70 IP: net.ParseIP("99.33.22.55").To4(), 71 UDP: 4444, 72 TCP: 4445, 73 }, 74 { 75 ID: hexPubkey("312c55512422cf9b8a4097e9a6ad79402e87a15ae909a4bfefa22398f03d20951933beea1e4dfa6f968212385e829f04c2d314fc2d4e255e0d3bc08792b069db"), 76 IP: net.ParseIP("1.2.3.4").To4(), 77 UDP: 1, 78 TCP: 1, 79 }, 80 { 81 ID: hexPubkey("38643200b172dcfef857492156971f0e6aa2c538d8b74010f8e140811d53b98c765dd2d96126051913f44582e8c199ad7c6d6819e9a56483f637feaac9448aac"), 82 IP: net.ParseIP("2001:db8:3c4d:15::abcd:ef12"), 83 UDP: 3333, 84 TCP: 3333, 85 }, 86 { 87 ID: hexPubkey("8dcab8618c3253b558d459da53bd8fa68935a719aff8b811197101a4b2b47dd2d47295286fc00cc081bb542d760717d1bdd6bec2c37cd72eca367d6dd3b9df73"), 88 IP: net.ParseIP("2001:db8:85a3:8d3:1319:8a2e:370:7348"), 89 UDP: 999, 90 TCP: 1000, 91 }, 92 }, 93 Expiration: 1136239445, 94 Rest: []rlp.RawValue{{0x01}, {0x02}, {0x03}}, 95 }, 96 }, 97 } 98 99 // This test checks that the decoder accepts packets according to EIP-8. 100 func TestForwardCompatibility(t *testing.T) { 101 testkey, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 102 wantNodeKey := EncodePubkey(&testkey.PublicKey) 103 104 for _, test := range testPackets { 105 input, err := hex.DecodeString(test.input) 106 if err != nil { 107 t.Fatalf("invalid hex: %s", test.input) 108 } 109 packet, nodekey, _, err := Decode(input) 110 if err != nil { 111 t.Errorf("did not accept packet %s\n%v", test.input, err) 112 continue 113 } 114 if !reflect.DeepEqual(packet, test.wantPacket) { 115 t.Errorf("got %s\nwant %s", spew.Sdump(packet), spew.Sdump(test.wantPacket)) 116 } 117 if nodekey != wantNodeKey { 118 t.Errorf("got id %v\nwant id %v", nodekey, wantNodeKey) 119 } 120 } 121 } 122 123 func hexPubkey(h string) (ret Pubkey) { 124 b, err := hex.DecodeString(h) 125 if err != nil { 126 panic(err) 127 } 128 if len(b) != len(ret) { 129 panic("invalid length") 130 } 131 copy(ret[:], b) 132 return ret 133 }