|
| 1 | +// Copyright (C) 2019-2020 Algorand, Inc. |
| 2 | +// This file is part of go-algorand |
| 3 | +// |
| 4 | +// go-algorand is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as |
| 6 | +// published by the Free Software Foundation, either version 3 of the |
| 7 | +// License, or (at your option) any later version. |
| 8 | +// |
| 9 | +// go-algorand 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 Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with go-algorand. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package merkletrie |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + |
| 25 | + "github.com/algorand/go-algorand/crypto" |
| 26 | +) |
| 27 | + |
| 28 | +func verifyCacheNodeCount(t *testing.T, trie *Trie) { |
| 29 | + count := 0 |
| 30 | + for _, pageNodes := range trie.cache.pageToNIDsPtr { |
| 31 | + count += len(pageNodes) |
| 32 | + } |
| 33 | + require.Equal(t, count, trie.cache.cachedNodeCount) |
| 34 | + |
| 35 | + // make sure that the pagesPrioritizationMap aligns with pagesPrioritizationList |
| 36 | + require.Equal(t, len(trie.cache.pagesPrioritizationMap), trie.cache.pagesPrioritizationList.Len()) |
| 37 | + |
| 38 | + // if we're not within a transaction, the following should also hold true: |
| 39 | + if !trie.cache.modified { |
| 40 | + require.Equal(t, len(trie.cache.pageToNIDsPtr), trie.cache.pagesPrioritizationList.Len()) |
| 41 | + } |
| 42 | + |
| 43 | + for e := trie.cache.pagesPrioritizationList.Back(); e != nil; e = e.Next() { |
| 44 | + page := e.Value.(uint64) |
| 45 | + _, has := trie.cache.pagesPrioritizationMap[page] |
| 46 | + require.True(t, has) |
| 47 | + _, has = trie.cache.pageToNIDsPtr[page] |
| 48 | + require.True(t, has) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestCacheEviction1(t *testing.T) { |
| 53 | + var memoryCommitter InMemoryCommitter |
| 54 | + mt1, _ := MakeTrie(&memoryCommitter, defaultTestEvictSize) |
| 55 | + // create 13000 hashes. |
| 56 | + leafsCount := 13000 |
| 57 | + hashes := make([]crypto.Digest, leafsCount) |
| 58 | + for i := 0; i < len(hashes); i++ { |
| 59 | + hashes[i] = crypto.Hash([]byte{byte(i % 256), byte((i / 256) % 256), byte(i / 65536)}) |
| 60 | + } |
| 61 | + |
| 62 | + for i := 0; i < defaultTestEvictSize; i++ { |
| 63 | + mt1.Add(hashes[i][:]) |
| 64 | + } |
| 65 | + |
| 66 | + for i := defaultTestEvictSize; i < len(hashes); i++ { |
| 67 | + mt1.Add(hashes[i][:]) |
| 68 | + mt1.Evict(true) |
| 69 | + require.GreaterOrEqual(t, defaultTestEvictSize, mt1.cache.cachedNodeCount) |
| 70 | + verifyCacheNodeCount(t, mt1) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestCacheEviction2(t *testing.T) { |
| 75 | + var memoryCommitter InMemoryCommitter |
| 76 | + mt1, _ := MakeTrie(&memoryCommitter, defaultTestEvictSize) |
| 77 | + // create 20000 hashes. |
| 78 | + leafsCount := 20000 |
| 79 | + hashes := make([]crypto.Digest, leafsCount) |
| 80 | + for i := 0; i < len(hashes); i++ { |
| 81 | + hashes[i] = crypto.Hash([]byte{byte(i % 256), byte((i / 256) % 256), byte(i / 65536)}) |
| 82 | + } |
| 83 | + |
| 84 | + for i := 0; i < defaultTestEvictSize; i++ { |
| 85 | + mt1.Add(hashes[i][:]) |
| 86 | + } |
| 87 | + |
| 88 | + for i := defaultTestEvictSize; i < len(hashes); i++ { |
| 89 | + mt1.Delete(hashes[i-2][:]) |
| 90 | + mt1.Add(hashes[i][:]) |
| 91 | + mt1.Add(hashes[i-2][:]) |
| 92 | + |
| 93 | + if i%(len(hashes)/20) == 0 { |
| 94 | + mt1.Evict(true) |
| 95 | + require.GreaterOrEqual(t, defaultTestEvictSize, mt1.cache.cachedNodeCount) |
| 96 | + verifyCacheNodeCount(t, mt1) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestCacheEviction3(t *testing.T) { |
| 102 | + var memoryCommitter InMemoryCommitter |
| 103 | + mt1, _ := MakeTrie(&memoryCommitter, defaultTestEvictSize) |
| 104 | + // create 200000 hashes. |
| 105 | + leafsCount := 200000 |
| 106 | + hashes := make([]crypto.Digest, leafsCount) |
| 107 | + for i := 0; i < len(hashes); i++ { |
| 108 | + hashes[i] = crypto.Hash([]byte{byte(i % 256), byte((i / 256) % 256), byte(i / 65536)}) |
| 109 | + } |
| 110 | + |
| 111 | + for i := 0; i < defaultTestEvictSize; i++ { |
| 112 | + mt1.Add(hashes[i][:]) |
| 113 | + } |
| 114 | + |
| 115 | + for i := defaultTestEvictSize; i < len(hashes); i++ { |
| 116 | + mt1.Delete(hashes[i-500][:]) |
| 117 | + mt1.Add(hashes[i][:]) |
| 118 | + |
| 119 | + if i%(len(hashes)/20) == 0 { |
| 120 | + mt1.Evict(true) |
| 121 | + require.GreaterOrEqual(t, defaultTestEvictSize, mt1.cache.cachedNodeCount) |
| 122 | + verifyCacheNodeCount(t, mt1) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// smallPageMemoryCommitter is an InMemoryCommitter, which has a custom page size, and knows how to "fail" per request. |
| 128 | +type smallPageMemoryCommitter struct { |
| 129 | + InMemoryCommitter |
| 130 | + pageSize int64 |
| 131 | + failStore int |
| 132 | + failLoad int |
| 133 | +} |
| 134 | + |
| 135 | +// GetNodesCountPerPage returns the page size ( number of nodes per page ) |
| 136 | +func (spmc *smallPageMemoryCommitter) GetNodesCountPerPage() (pageSize int64) { |
| 137 | + return spmc.pageSize |
| 138 | +} |
| 139 | + |
| 140 | +// StorePage stores a single page in an in-memory persistence. |
| 141 | +func (spmc *smallPageMemoryCommitter) StorePage(page uint64, content []byte) error { |
| 142 | + if spmc.failStore > 0 { |
| 143 | + spmc.failStore-- |
| 144 | + return fmt.Errorf("failStore>0") |
| 145 | + } |
| 146 | + return spmc.InMemoryCommitter.StorePage(page, content) |
| 147 | +} |
| 148 | + |
| 149 | +// LoadPage load a single page from an in-memory persistence. |
| 150 | +func (spmc *smallPageMemoryCommitter) LoadPage(page uint64) (content []byte, err error) { |
| 151 | + if spmc.failLoad > 0 { |
| 152 | + spmc.failLoad-- |
| 153 | + return nil, fmt.Errorf("failLoad>0") |
| 154 | + } |
| 155 | + return spmc.InMemoryCommitter.LoadPage(page) |
| 156 | +} |
| 157 | + |
| 158 | +func cacheEvictionFuzzer(t *testing.T, hashes []crypto.Digest, pageSize int64, evictSize int) { |
| 159 | + var memoryCommitter smallPageMemoryCommitter |
| 160 | + memoryCommitter.pageSize = pageSize |
| 161 | + mt1, _ := MakeTrie(&memoryCommitter, evictSize) |
| 162 | + |
| 163 | + // add the first 10 hashes. |
| 164 | + for i := 0; i < 10; i++ { |
| 165 | + mt1.Add(hashes[i][:]) |
| 166 | + } |
| 167 | + |
| 168 | + for i := 10; i < len(hashes)-10; i++ { |
| 169 | + for k := 0; k < int(hashes[i-2][0]%5); k++ { |
| 170 | + if hashes[i+k-3][0]%7 == 0 { |
| 171 | + memoryCommitter.failLoad++ |
| 172 | + } |
| 173 | + if hashes[i+k-4][0]%7 == 0 { |
| 174 | + memoryCommitter.failStore++ |
| 175 | + } |
| 176 | + if hashes[i+k][0]%7 == 0 { |
| 177 | + mt1.Delete(hashes[i+k-int(hashes[i][0]%7)][:]) |
| 178 | + } |
| 179 | + mt1.Add(hashes[i+k+3-int(hashes[i+k-1][0]%7)][:]) |
| 180 | + } |
| 181 | + if hashes[i][0]%5 == 0 { |
| 182 | + verifyCacheNodeCount(t, mt1) |
| 183 | + mt1.Evict(true) |
| 184 | + verifyCacheNodeCount(t, mt1) |
| 185 | + } |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +// TestCacheEvictionFuzzer generates bursts of random Add/Delete operations on the trie, and |
| 190 | +// testing the correctness of the cache internal buffers priodically. |
| 191 | +func TestCacheEvictionFuzzer(t *testing.T) { |
| 192 | + // create 2000 hashes. |
| 193 | + leafsCount := 2000 |
| 194 | + hashes := make([]crypto.Digest, leafsCount) |
| 195 | + for i := 0; i < len(hashes); i++ { |
| 196 | + hashes[i] = crypto.Hash([]byte{byte(i % 256), byte((i / 256) % 256), byte(i / 65536)}) |
| 197 | + } |
| 198 | + for _, pageSize := range []int64{2, 3, 8, 12, 17} { |
| 199 | + for _, evictSize := range []int{5, 10, 13, 30} { |
| 200 | + t.Run(fmt.Sprintf("Fuzzer-%d-%d", pageSize, evictSize), func(t *testing.T) { |
| 201 | + cacheEvictionFuzzer(t, hashes, pageSize, evictSize) |
| 202 | + }) |
| 203 | + } |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +// TestCacheEvictionFuzzer generates bursts of random Add/Delete operations on the trie, and |
| 208 | +// testing the correctness of the cache internal buffers priodically. |
| 209 | +func TestCacheEvictionFuzzer2(t *testing.T) { |
| 210 | + // create 1000 hashes. |
| 211 | + leafsCount := 1000 |
| 212 | + hashes := make([]crypto.Digest, leafsCount) |
| 213 | + for i := 0; i < len(hashes); i++ { |
| 214 | + hashes[i] = crypto.Hash([]byte{byte(i % 256), byte((i / 256) % 256), byte(i / 65536)}) |
| 215 | + } |
| 216 | + for i := 0; i < 80; i++ { |
| 217 | + pageSize := int64(1 + crypto.RandUint64()%101) |
| 218 | + evictSize := int(1 + crypto.RandUint64()%37) |
| 219 | + hashesCount := uint64(100) + crypto.RandUint64()%uint64(leafsCount-100) |
| 220 | + t.Run(fmt.Sprintf("Fuzzer-%d-%d", pageSize, evictSize), func(t *testing.T) { |
| 221 | + cacheEvictionFuzzer(t, hashes[:hashesCount], pageSize, evictSize) |
| 222 | + }) |
| 223 | + } |
| 224 | +} |
0 commit comments