github.com/MetalBlockchain/subnet-evm@v0.4.9/core/state/snapshot/iterator_fast.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2019 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package snapshot 28 29 import ( 30 "bytes" 31 "fmt" 32 "sort" 33 34 "github.com/ethereum/go-ethereum/common" 35 ) 36 37 // weightedIterator is a iterator with an assigned weight. It is used to prioritise 38 // which account or storage slot is the correct one if multiple iterators find the 39 // same one (modified in multiple consecutive blocks). 40 type weightedIterator struct { 41 it Iterator 42 priority int 43 } 44 45 // weightedIterators is a set of iterators implementing the sort.Interface. 46 type weightedIterators []*weightedIterator 47 48 // Len implements sort.Interface, returning the number of active iterators. 49 func (its weightedIterators) Len() int { return len(its) } 50 51 // Less implements sort.Interface, returning which of two iterators in the stack 52 // is before the other. 53 func (its weightedIterators) Less(i, j int) bool { 54 // Order the iterators primarily by the account hashes 55 hashI := its[i].it.Hash() 56 hashJ := its[j].it.Hash() 57 58 switch bytes.Compare(hashI[:], hashJ[:]) { 59 case -1: 60 return true 61 case 1: 62 return false 63 } 64 // Same account/storage-slot in multiple layers, split by priority 65 return its[i].priority < its[j].priority 66 } 67 68 // Swap implements sort.Interface, swapping two entries in the iterator stack. 69 func (its weightedIterators) Swap(i, j int) { 70 its[i], its[j] = its[j], its[i] 71 } 72 73 // fastIterator is a more optimized multi-layer iterator which maintains a 74 // direct mapping of all iterators leading down to the bottom layer. 75 type fastIterator struct { 76 tree *Tree // Snapshot tree to reinitialize stale sub-iterators with 77 root common.Hash // Root hash to reinitialize stale sub-iterators through 78 79 curAccount []byte 80 curSlot []byte 81 82 iterators weightedIterators 83 initiated bool 84 account bool 85 fail error 86 } 87 88 // newFastIterator creates a new hierarchical account or storage iterator with one 89 // element per diff layer. The returned combo iterator can be used to walk over 90 // the entire snapshot diff stack simultaneously. 91 func newFastIterator(tree *Tree, root common.Hash, account common.Hash, seek common.Hash, accountIterator bool, holdsTreeLock bool) (*fastIterator, error) { 92 current := tree.getSnapshot(root, holdsTreeLock) 93 if current == nil { 94 return nil, fmt.Errorf("unknown snapshot: %x", root) 95 } 96 fi := &fastIterator{ 97 tree: tree, 98 root: root, 99 account: accountIterator, 100 } 101 for depth := 0; current != nil; depth++ { 102 if accountIterator { 103 fi.iterators = append(fi.iterators, &weightedIterator{ 104 it: current.AccountIterator(seek), 105 priority: depth, 106 }) 107 } else { 108 // If the whole storage is destructed in this layer, don't 109 // bother deeper layer anymore. But we should still keep 110 // the iterator for this layer, since the iterator can contain 111 // some valid slots which belongs to the re-created account. 112 it, destructed := current.StorageIterator(account, seek) 113 fi.iterators = append(fi.iterators, &weightedIterator{ 114 it: it, 115 priority: depth, 116 }) 117 if destructed { 118 break 119 } 120 } 121 current = current.Parent() 122 } 123 fi.init() 124 return fi, nil 125 } 126 127 // init walks over all the iterators and resolves any clashes between them, after 128 // which it prepares the stack for step-by-step iteration. 129 func (fi *fastIterator) init() { 130 // Track which account hashes are iterators positioned on 131 var positioned = make(map[common.Hash]int) 132 133 // Position all iterators and track how many remain live 134 for i := 0; i < len(fi.iterators); i++ { 135 // Retrieve the first element and if it clashes with a previous iterator, 136 // advance either the current one or the old one. Repeat until nothing is 137 // clashing any more. 138 it := fi.iterators[i] 139 for { 140 // If the iterator is exhausted, drop it off the end 141 if !it.it.Next() { 142 it.it.Release() 143 last := len(fi.iterators) - 1 144 145 fi.iterators[i] = fi.iterators[last] 146 fi.iterators[last] = nil 147 fi.iterators = fi.iterators[:last] 148 149 i-- 150 break 151 } 152 // The iterator is still alive, check for collisions with previous ones 153 hash := it.it.Hash() 154 if other, exist := positioned[hash]; !exist { 155 positioned[hash] = i 156 break 157 } else { 158 // Iterators collide, one needs to be progressed, use priority to 159 // determine which. 160 // 161 // This whole else-block can be avoided, if we instead 162 // do an initial priority-sort of the iterators. If we do that, 163 // then we'll only wind up here if a lower-priority (preferred) iterator 164 // has the same value, and then we will always just continue. 165 // However, it costs an extra sort, so it's probably not better 166 if fi.iterators[other].priority < it.priority { 167 // The 'it' should be progressed 168 continue 169 } else { 170 // The 'other' should be progressed, swap them 171 it = fi.iterators[other] 172 fi.iterators[other], fi.iterators[i] = fi.iterators[i], fi.iterators[other] 173 continue 174 } 175 } 176 } 177 } 178 // Re-sort the entire list 179 sort.Sort(fi.iterators) 180 fi.initiated = false 181 } 182 183 // Next steps the iterator forward one element, returning false if exhausted. 184 func (fi *fastIterator) Next() bool { 185 if len(fi.iterators) == 0 { 186 return false 187 } 188 if !fi.initiated { 189 // Don't forward first time -- we had to 'Next' once in order to 190 // do the sorting already 191 fi.initiated = true 192 if fi.account { 193 fi.curAccount = fi.iterators[0].it.(AccountIterator).Account() 194 } else { 195 fi.curSlot = fi.iterators[0].it.(StorageIterator).Slot() 196 } 197 if innerErr := fi.iterators[0].it.Error(); innerErr != nil { 198 fi.fail = innerErr 199 return false 200 } 201 if fi.curAccount != nil || fi.curSlot != nil { 202 return true 203 } 204 // Implicit else: we've hit a nil-account or nil-slot, and need to 205 // fall through to the loop below to land on something non-nil 206 } 207 // If an account or a slot is deleted in one of the layers, the key will 208 // still be there, but the actual value will be nil. However, the iterator 209 // should not export nil-values (but instead simply omit the key), so we 210 // need to loop here until we either 211 // - get a non-nil value, 212 // - hit an error, 213 // - or exhaust the iterator 214 for { 215 if !fi.next(0) { 216 return false // exhausted 217 } 218 if fi.account { 219 fi.curAccount = fi.iterators[0].it.(AccountIterator).Account() 220 } else { 221 fi.curSlot = fi.iterators[0].it.(StorageIterator).Slot() 222 } 223 if innerErr := fi.iterators[0].it.Error(); innerErr != nil { 224 fi.fail = innerErr 225 return false // error 226 } 227 if fi.curAccount != nil || fi.curSlot != nil { 228 break // non-nil value found 229 } 230 } 231 return true 232 } 233 234 // next handles the next operation internally and should be invoked when we know 235 // that two elements in the list may have the same value. 236 // 237 // For example, if the iterated hashes become [2,3,5,5,8,9,10], then we should 238 // invoke next(3), which will call Next on elem 3 (the second '5') and will 239 // cascade along the list, applying the same operation if needed. 240 func (fi *fastIterator) next(idx int) bool { 241 // If this particular iterator got exhausted, remove it and return true (the 242 // next one is surely not exhausted yet, otherwise it would have been removed 243 // already). 244 if it := fi.iterators[idx].it; !it.Next() { 245 it.Release() 246 247 fi.iterators = append(fi.iterators[:idx], fi.iterators[idx+1:]...) 248 return len(fi.iterators) > 0 249 } 250 // If there's no one left to cascade into, return 251 if idx == len(fi.iterators)-1 { 252 return true 253 } 254 // We next-ed the iterator at 'idx', now we may have to re-sort that element 255 var ( 256 cur, next = fi.iterators[idx], fi.iterators[idx+1] 257 curHash, nextHash = cur.it.Hash(), next.it.Hash() 258 ) 259 if diff := bytes.Compare(curHash[:], nextHash[:]); diff < 0 { 260 // It is still in correct place 261 return true 262 } else if diff == 0 && cur.priority < next.priority { 263 // So still in correct place, but we need to iterate on the next 264 fi.next(idx + 1) 265 return true 266 } 267 // At this point, the iterator is in the wrong location, but the remaining 268 // list is sorted. Find out where to move the item. 269 clash := -1 270 index := sort.Search(len(fi.iterators), func(n int) bool { 271 // The iterator always advances forward, so anything before the old slot 272 // is known to be behind us, so just skip them altogether. This actually 273 // is an important clause since the sort order got invalidated. 274 if n < idx { 275 return false 276 } 277 if n == len(fi.iterators)-1 { 278 // Can always place an elem last 279 return true 280 } 281 nextHash := fi.iterators[n+1].it.Hash() 282 if diff := bytes.Compare(curHash[:], nextHash[:]); diff < 0 { 283 return true 284 } else if diff > 0 { 285 return false 286 } 287 // The elem we're placing it next to has the same value, 288 // so whichever winds up on n+1 will need further iteraton 289 clash = n + 1 290 291 return cur.priority < fi.iterators[n+1].priority 292 }) 293 fi.move(idx, index) 294 if clash != -1 { 295 fi.next(clash) 296 } 297 return true 298 } 299 300 // move advances an iterator to another position in the list. 301 func (fi *fastIterator) move(index, newpos int) { 302 elem := fi.iterators[index] 303 copy(fi.iterators[index:], fi.iterators[index+1:newpos+1]) 304 fi.iterators[newpos] = elem 305 } 306 307 // Error returns any failure that occurred during iteration, which might have 308 // caused a premature iteration exit (e.g. snapshot stack becoming stale). 309 func (fi *fastIterator) Error() error { 310 return fi.fail 311 } 312 313 // Hash returns the current key 314 func (fi *fastIterator) Hash() common.Hash { 315 return fi.iterators[0].it.Hash() 316 } 317 318 // Account returns the current account blob. 319 // Note the returned account is not a copy, please don't modify it. 320 func (fi *fastIterator) Account() []byte { 321 return fi.curAccount 322 } 323 324 // Slot returns the current storage slot. 325 // Note the returned slot is not a copy, please don't modify it. 326 func (fi *fastIterator) Slot() []byte { 327 return fi.curSlot 328 } 329 330 // Release iterates over all the remaining live layer iterators and releases each 331 // of them individually. 332 func (fi *fastIterator) Release() { 333 for _, it := range fi.iterators { 334 it.it.Release() 335 } 336 fi.iterators = nil 337 } 338 339 // Debug is a convenience helper during testing 340 func (fi *fastIterator) Debug() { 341 for _, it := range fi.iterators { 342 fmt.Printf("[p=%v v=%v] ", it.priority, it.it.Hash()[0]) 343 } 344 fmt.Println() 345 } 346 347 // newFastAccountIterator creates a new hierarchical account iterator with one 348 // element per diff layer. The returned combo iterator can be used to walk over 349 // the entire snapshot diff stack simultaneously. 350 func newFastAccountIterator(tree *Tree, root common.Hash, seek common.Hash, holdsTreeLock bool) (AccountIterator, error) { 351 return newFastIterator(tree, root, common.Hash{}, seek, true, holdsTreeLock) 352 } 353 354 // newFastStorageIterator creates a new hierarchical storage iterator with one 355 // element per diff layer. The returned combo iterator can be used to walk over 356 // the entire snapshot diff stack simultaneously. 357 func newFastStorageIterator(tree *Tree, root common.Hash, account common.Hash, seek common.Hash, holdsTreeLock bool) (StorageIterator, error) { 358 return newFastIterator(tree, root, account, seek, false, holdsTreeLock) 359 }