range_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2021 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. package snap
  17. import (
  18. "testing"
  19. "github.com/ethereum/go-ethereum/common"
  20. )
  21. // Tests that given a starting hash and a density, the hash ranger can correctly
  22. // split up the remaining hash space into a fixed number of chunks.
  23. func TestHashRanges(t *testing.T) {
  24. tests := []struct {
  25. head common.Hash
  26. chunks uint64
  27. starts []common.Hash
  28. ends []common.Hash
  29. }{
  30. // Simple test case to split the entire hash range into 4 chunks
  31. {
  32. head: common.Hash{},
  33. chunks: 4,
  34. starts: []common.Hash{
  35. {},
  36. common.HexToHash("0x4000000000000000000000000000000000000000000000000000000000000000"),
  37. common.HexToHash("0x8000000000000000000000000000000000000000000000000000000000000000"),
  38. common.HexToHash("0xc000000000000000000000000000000000000000000000000000000000000000"),
  39. },
  40. ends: []common.Hash{
  41. common.HexToHash("0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
  42. common.HexToHash("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
  43. common.HexToHash("0xbfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
  44. common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
  45. },
  46. },
  47. // Split a divisible part of the hash range up into 2 chunks
  48. {
  49. head: common.HexToHash("0x2000000000000000000000000000000000000000000000000000000000000000"),
  50. chunks: 2,
  51. starts: []common.Hash{
  52. {},
  53. common.HexToHash("0x9000000000000000000000000000000000000000000000000000000000000000"),
  54. },
  55. ends: []common.Hash{
  56. common.HexToHash("0x8fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
  57. common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
  58. },
  59. },
  60. // Split the entire hash range into a non divisible 3 chunks
  61. {
  62. head: common.Hash{},
  63. chunks: 3,
  64. starts: []common.Hash{
  65. {},
  66. common.HexToHash("0x5555555555555555555555555555555555555555555555555555555555555556"),
  67. common.HexToHash("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac"),
  68. },
  69. ends: []common.Hash{
  70. common.HexToHash("0x5555555555555555555555555555555555555555555555555555555555555555"),
  71. common.HexToHash("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"),
  72. common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
  73. },
  74. },
  75. // Split a part of hash range into a non divisible 3 chunks
  76. {
  77. head: common.HexToHash("0x2000000000000000000000000000000000000000000000000000000000000000"),
  78. chunks: 3,
  79. starts: []common.Hash{
  80. {},
  81. common.HexToHash("0x6aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"),
  82. common.HexToHash("0xb555555555555555555555555555555555555555555555555555555555555556"),
  83. },
  84. ends: []common.Hash{
  85. common.HexToHash("0x6aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
  86. common.HexToHash("0xb555555555555555555555555555555555555555555555555555555555555555"),
  87. common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
  88. },
  89. },
  90. // Split a part of hash range into a non divisible 3 chunks, but with a
  91. // meaningful space size for manual verification.
  92. // - The head being 0xff...f0, we have 14 hashes left in the space
  93. // - Chunking up 14 into 3 pieces is 4.(6), but we need the ceil of 5 to avoid a micro-last-chunk
  94. // - Since the range is not divisible, the last interval will be shrter, capped at 0xff...f
  95. // - The chunk ranges thus needs to be [..0, ..5], [..6, ..b], [..c, ..f]
  96. {
  97. head: common.HexToHash("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0"),
  98. chunks: 3,
  99. starts: []common.Hash{
  100. {},
  101. common.HexToHash("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff6"),
  102. common.HexToHash("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc"),
  103. },
  104. ends: []common.Hash{
  105. common.HexToHash("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff5"),
  106. common.HexToHash("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb"),
  107. common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
  108. },
  109. },
  110. }
  111. for i, tt := range tests {
  112. r := newHashRange(tt.head, tt.chunks)
  113. var (
  114. starts = []common.Hash{{}}
  115. ends = []common.Hash{r.End()}
  116. )
  117. for r.Next() {
  118. starts = append(starts, r.Start())
  119. ends = append(ends, r.End())
  120. }
  121. if len(starts) != len(tt.starts) {
  122. t.Errorf("test %d: starts count mismatch: have %d, want %d", i, len(starts), len(tt.starts))
  123. }
  124. for j := 0; j < len(starts) && j < len(tt.starts); j++ {
  125. if starts[j] != tt.starts[j] {
  126. t.Errorf("test %d, start %d: hash mismatch: have %x, want %x", i, j, starts[j], tt.starts[j])
  127. }
  128. }
  129. if len(ends) != len(tt.ends) {
  130. t.Errorf("test %d: ends count mismatch: have %d, want %d", i, len(ends), len(tt.ends))
  131. }
  132. for j := 0; j < len(ends) && j < len(tt.ends); j++ {
  133. if ends[j] != tt.ends[j] {
  134. t.Errorf("test %d, end %d: hash mismatch: have %x, want %x", i, j, ends[j], tt.ends[j])
  135. }
  136. }
  137. }
  138. }