github.com/bcnmy/go-ethereum@v1.10.27/eth/protocols/snap/sort_test.go (about) 1 // Copyright 2022 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 snap 18 19 import ( 20 "bytes" 21 "fmt" 22 "testing" 23 24 "github.com/ethereum/go-ethereum/common" 25 ) 26 27 func hexToNibbles(s string) []byte { 28 if len(s) >= 2 && s[0] == '0' && s[1] == 'x' { 29 s = s[2:] 30 } 31 var s2 []byte 32 for _, ch := range []byte(s) { 33 s2 = append(s2, '0') 34 s2 = append(s2, ch) 35 } 36 return common.Hex2Bytes(string(s2)) 37 } 38 39 func TestRequestSorting(t *testing.T) { 40 // - Path 0x9 -> {0x19} 41 // - Path 0x99 -> {0x0099} 42 // - Path 0x01234567890123456789012345678901012345678901234567890123456789019 -> {0x0123456789012345678901234567890101234567890123456789012345678901, 0x19} 43 // - Path 0x012345678901234567890123456789010123456789012345678901234567890199 -> {0x0123456789012345678901234567890101234567890123456789012345678901, 0x0099} 44 var f = func(path string) string { 45 data := hexToNibbles(path) 46 return string(data) 47 } 48 var ( 49 hashes []common.Hash 50 paths []string 51 ) 52 for _, x := range []string{ 53 "0x9", 54 "0x012345678901234567890123456789010123456789012345678901234567890195", 55 "0x012345678901234567890123456789010123456789012345678901234567890197", 56 "0x012345678901234567890123456789010123456789012345678901234567890196", 57 "0x99", 58 "0x012345678901234567890123456789010123456789012345678901234567890199", 59 "0x01234567890123456789012345678901012345678901234567890123456789019", 60 "0x0123456789012345678901234567890101234567890123456789012345678901", 61 "0x01234567890123456789012345678901012345678901234567890123456789010", 62 "0x01234567890123456789012345678901012345678901234567890123456789011", 63 } { 64 paths = append(paths, f(x)) 65 hashes = append(hashes, common.Hash{}) 66 } 67 _, _, syncPaths, pathsets := sortByAccountPath(paths, hashes) 68 { 69 var b = new(bytes.Buffer) 70 for i := 0; i < len(syncPaths); i++ { 71 fmt.Fprintf(b, "\n%d. paths %x", i, syncPaths[i]) 72 } 73 want := ` 74 0. paths [0099] 75 1. paths [0123456789012345678901234567890101234567890123456789012345678901 00] 76 2. paths [0123456789012345678901234567890101234567890123456789012345678901 0095] 77 3. paths [0123456789012345678901234567890101234567890123456789012345678901 0096] 78 4. paths [0123456789012345678901234567890101234567890123456789012345678901 0097] 79 5. paths [0123456789012345678901234567890101234567890123456789012345678901 0099] 80 6. paths [0123456789012345678901234567890101234567890123456789012345678901 10] 81 7. paths [0123456789012345678901234567890101234567890123456789012345678901 11] 82 8. paths [0123456789012345678901234567890101234567890123456789012345678901 19] 83 9. paths [19]` 84 if have := b.String(); have != want { 85 t.Errorf("have:%v\nwant:%v\n", have, want) 86 } 87 } 88 { 89 var b = new(bytes.Buffer) 90 for i := 0; i < len(pathsets); i++ { 91 fmt.Fprintf(b, "\n%d. pathset %x", i, pathsets[i]) 92 } 93 want := ` 94 0. pathset [0099] 95 1. pathset [0123456789012345678901234567890101234567890123456789012345678901 00 0095 0096 0097 0099 10 11 19] 96 2. pathset [19]` 97 if have := b.String(); have != want { 98 t.Errorf("have:%v\nwant:%v\n", have, want) 99 } 100 } 101 }