github.com/klaytn/klaytn@v1.12.1/node/cn/snap/range_test.go (about) 1 // Modifications Copyright 2022 The klaytn Authors 2 // Copyright 2021 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from eth/protocols/snap/range_test.go (2022/06/29). 19 // Modified and improved for the klaytn development. 20 21 package snap 22 23 import ( 24 "testing" 25 26 "github.com/klaytn/klaytn/common" 27 ) 28 29 // Tests that given a starting hash and a density, the hash ranger can correctly 30 // split up the remaining hash space into a fixed number of chunks. 31 func TestHashRanges(t *testing.T) { 32 tests := []struct { 33 head common.Hash 34 chunks uint64 35 starts []common.Hash 36 ends []common.Hash 37 }{ 38 // Simple test case to split the entire hash range into 4 chunks 39 { 40 head: common.Hash{}, 41 chunks: 4, 42 starts: []common.Hash{ 43 {}, 44 common.HexToHash("0x4000000000000000000000000000000000000000000000000000000000000000"), 45 common.HexToHash("0x8000000000000000000000000000000000000000000000000000000000000000"), 46 common.HexToHash("0xc000000000000000000000000000000000000000000000000000000000000000"), 47 }, 48 ends: []common.Hash{ 49 common.HexToHash("0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 50 common.HexToHash("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 51 common.HexToHash("0xbfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 52 common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 53 }, 54 }, 55 // Split a divisible part of the hash range up into 2 chunks 56 { 57 head: common.HexToHash("0x2000000000000000000000000000000000000000000000000000000000000000"), 58 chunks: 2, 59 starts: []common.Hash{ 60 {}, 61 common.HexToHash("0x9000000000000000000000000000000000000000000000000000000000000000"), 62 }, 63 ends: []common.Hash{ 64 common.HexToHash("0x8fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 65 common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 66 }, 67 }, 68 // Split the entire hash range into a non divisible 3 chunks 69 { 70 head: common.Hash{}, 71 chunks: 3, 72 starts: []common.Hash{ 73 {}, 74 common.HexToHash("0x5555555555555555555555555555555555555555555555555555555555555556"), 75 common.HexToHash("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac"), 76 }, 77 ends: []common.Hash{ 78 common.HexToHash("0x5555555555555555555555555555555555555555555555555555555555555555"), 79 common.HexToHash("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"), 80 common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 81 }, 82 }, 83 // Split a part of hash range into a non divisible 3 chunks 84 { 85 head: common.HexToHash("0x2000000000000000000000000000000000000000000000000000000000000000"), 86 chunks: 3, 87 starts: []common.Hash{ 88 {}, 89 common.HexToHash("0x6aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"), 90 common.HexToHash("0xb555555555555555555555555555555555555555555555555555555555555556"), 91 }, 92 ends: []common.Hash{ 93 common.HexToHash("0x6aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), 94 common.HexToHash("0xb555555555555555555555555555555555555555555555555555555555555555"), 95 common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 96 }, 97 }, 98 // Split a part of hash range into a non divisible 3 chunks, but with a 99 // meaningful space size for manual verification. 100 // - The head being 0xff...f0, we have 14 hashes left in the space 101 // - Chunking up 14 into 3 pieces is 4.(6), but we need the ceil of 5 to avoid a micro-last-chunk 102 // - Since the range is not divisible, the last interval will be shrter, capped at 0xff...f 103 // - The chunk ranges thus needs to be [..0, ..5], [..6, ..b], [..c, ..f] 104 { 105 head: common.HexToHash("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0"), 106 chunks: 3, 107 starts: []common.Hash{ 108 {}, 109 common.HexToHash("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff6"), 110 common.HexToHash("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc"), 111 }, 112 ends: []common.Hash{ 113 common.HexToHash("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff5"), 114 common.HexToHash("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb"), 115 common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 116 }, 117 }, 118 } 119 for i, tt := range tests { 120 r := newHashRange(tt.head, tt.chunks) 121 122 var ( 123 starts = []common.Hash{{}} 124 ends = []common.Hash{r.End()} 125 ) 126 for r.Next() { 127 starts = append(starts, r.Start()) 128 ends = append(ends, r.End()) 129 } 130 if len(starts) != len(tt.starts) { 131 t.Errorf("test %d: starts count mismatch: have %d, want %d", i, len(starts), len(tt.starts)) 132 } 133 for j := 0; j < len(starts) && j < len(tt.starts); j++ { 134 if starts[j] != tt.starts[j] { 135 t.Errorf("test %d, start %d: hash mismatch: have %x, want %x", i, j, starts[j], tt.starts[j]) 136 } 137 } 138 if len(ends) != len(tt.ends) { 139 t.Errorf("test %d: ends count mismatch: have %d, want %d", i, len(ends), len(tt.ends)) 140 } 141 for j := 0; j < len(ends) && j < len(tt.ends); j++ { 142 if ends[j] != tt.ends[j] { 143 t.Errorf("test %d, end %d: hash mismatch: have %x, want %x", i, j, ends[j], tt.ends[j]) 144 } 145 } 146 } 147 }