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