github.com/haliliceylan/bsc@v1.1.10-0.20220501224556-eb78d644ebcb/core/state/snapshot/snapshot.go (about) 1 // Copyright 2019 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 snapshot implements a journalled, dynamic state dump. 18 package snapshot 19 20 import ( 21 "bytes" 22 "errors" 23 "fmt" 24 "sync" 25 "sync/atomic" 26 27 "github.com/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/core/rawdb" 29 "github.com/ethereum/go-ethereum/crypto" 30 "github.com/ethereum/go-ethereum/ethdb" 31 "github.com/ethereum/go-ethereum/log" 32 "github.com/ethereum/go-ethereum/metrics" 33 "github.com/ethereum/go-ethereum/rlp" 34 "github.com/ethereum/go-ethereum/trie" 35 ) 36 37 var ( 38 snapshotCleanAccountHitMeter = metrics.NewRegisteredMeter("state/snapshot/clean/account/hit", nil) 39 snapshotCleanAccountMissMeter = metrics.NewRegisteredMeter("state/snapshot/clean/account/miss", nil) 40 snapshotCleanAccountInexMeter = metrics.NewRegisteredMeter("state/snapshot/clean/account/inex", nil) 41 snapshotCleanAccountReadMeter = metrics.NewRegisteredMeter("state/snapshot/clean/account/read", nil) 42 snapshotCleanAccountWriteMeter = metrics.NewRegisteredMeter("state/snapshot/clean/account/write", nil) 43 44 snapshotCleanStorageHitMeter = metrics.NewRegisteredMeter("state/snapshot/clean/storage/hit", nil) 45 snapshotCleanStorageMissMeter = metrics.NewRegisteredMeter("state/snapshot/clean/storage/miss", nil) 46 snapshotCleanStorageInexMeter = metrics.NewRegisteredMeter("state/snapshot/clean/storage/inex", nil) 47 snapshotCleanStorageReadMeter = metrics.NewRegisteredMeter("state/snapshot/clean/storage/read", nil) 48 snapshotCleanStorageWriteMeter = metrics.NewRegisteredMeter("state/snapshot/clean/storage/write", nil) 49 50 snapshotDirtyAccountHitMeter = metrics.NewRegisteredMeter("state/snapshot/dirty/account/hit", nil) 51 snapshotDirtyAccountMissMeter = metrics.NewRegisteredMeter("state/snapshot/dirty/account/miss", nil) 52 snapshotDirtyAccountInexMeter = metrics.NewRegisteredMeter("state/snapshot/dirty/account/inex", nil) 53 snapshotDirtyAccountReadMeter = metrics.NewRegisteredMeter("state/snapshot/dirty/account/read", nil) 54 snapshotDirtyAccountWriteMeter = metrics.NewRegisteredMeter("state/snapshot/dirty/account/write", nil) 55 56 snapshotDirtyStorageHitMeter = metrics.NewRegisteredMeter("state/snapshot/dirty/storage/hit", nil) 57 snapshotDirtyStorageMissMeter = metrics.NewRegisteredMeter("state/snapshot/dirty/storage/miss", nil) 58 snapshotDirtyStorageInexMeter = metrics.NewRegisteredMeter("state/snapshot/dirty/storage/inex", nil) 59 snapshotDirtyStorageReadMeter = metrics.NewRegisteredMeter("state/snapshot/dirty/storage/read", nil) 60 snapshotDirtyStorageWriteMeter = metrics.NewRegisteredMeter("state/snapshot/dirty/storage/write", nil) 61 62 snapshotDirtyAccountHitDepthHist = metrics.NewRegisteredHistogram("state/snapshot/dirty/account/hit/depth", nil, metrics.NewExpDecaySample(1028, 0.015)) 63 64 snapshotFlushAccountItemMeter = metrics.NewRegisteredMeter("state/snapshot/flush/account/item", nil) 65 snapshotFlushAccountSizeMeter = metrics.NewRegisteredMeter("state/snapshot/flush/account/size", nil) 66 snapshotFlushStorageItemMeter = metrics.NewRegisteredMeter("state/snapshot/flush/storage/item", nil) 67 snapshotFlushStorageSizeMeter = metrics.NewRegisteredMeter("state/snapshot/flush/storage/size", nil) 68 69 snapshotBloomIndexTimer = metrics.NewRegisteredResettingTimer("state/snapshot/bloom/index", nil) 70 snapshotBloomErrorGauge = metrics.NewRegisteredGaugeFloat64("state/snapshot/bloom/error", nil) 71 72 snapshotBloomAccountTrueHitMeter = metrics.NewRegisteredMeter("state/snapshot/bloom/account/truehit", nil) 73 snapshotBloomAccountFalseHitMeter = metrics.NewRegisteredMeter("state/snapshot/bloom/account/falsehit", nil) 74 snapshotBloomAccountMissMeter = metrics.NewRegisteredMeter("state/snapshot/bloom/account/miss", nil) 75 76 snapshotBloomStorageTrueHitMeter = metrics.NewRegisteredMeter("state/snapshot/bloom/storage/truehit", nil) 77 snapshotBloomStorageFalseHitMeter = metrics.NewRegisteredMeter("state/snapshot/bloom/storage/falsehit", nil) 78 snapshotBloomStorageMissMeter = metrics.NewRegisteredMeter("state/snapshot/bloom/storage/miss", nil) 79 80 // ErrSnapshotStale is returned from data accessors if the underlying snapshot 81 // layer had been invalidated due to the chain progressing forward far enough 82 // to not maintain the layer's original state. 83 ErrSnapshotStale = errors.New("snapshot stale") 84 85 // ErrNotCoveredYet is returned from data accessors if the underlying snapshot 86 // is being generated currently and the requested data item is not yet in the 87 // range of accounts covered. 88 ErrNotCoveredYet = errors.New("not covered yet") 89 90 // ErrNotConstructed is returned if the callers want to iterate the snapshot 91 // while the generation is not finished yet. 92 ErrNotConstructed = errors.New("snapshot is not constructed") 93 94 // errSnapshotCycle is returned if a snapshot is attempted to be inserted 95 // that forms a cycle in the snapshot tree. 96 errSnapshotCycle = errors.New("snapshot cycle") 97 ) 98 99 // Snapshot represents the functionality supported by a snapshot storage layer. 100 type Snapshot interface { 101 // Root returns the root hash for which this snapshot was made. 102 Root() common.Hash 103 104 // WaitAndGetVerifyRes will wait until the snapshot been verified and return verification result 105 WaitAndGetVerifyRes() bool 106 107 // Verified returns whether the snapshot is verified 108 Verified() bool 109 110 // MarkValid stores the verification result 111 MarkValid() 112 113 // CorrectAccounts updates account data for storing the correct data during pipecommit 114 CorrectAccounts(map[common.Hash][]byte) 115 116 // AccountsCorrected checks whether the account data has been corrected during pipecommit 117 AccountsCorrected() bool 118 119 // Account directly retrieves the account associated with a particular hash in 120 // the snapshot slim data format. 121 Account(hash common.Hash) (*Account, error) 122 123 // Accounts directly retrieves all accounts in current snapshot in 124 // the snapshot slim data format. 125 Accounts() (map[common.Hash]*Account, error) 126 127 // AccountRLP directly retrieves the account RLP associated with a particular 128 // hash in the snapshot slim data format. 129 AccountRLP(hash common.Hash) ([]byte, error) 130 131 // Storage directly retrieves the storage data associated with a particular hash, 132 // within a particular account. 133 Storage(accountHash, storageHash common.Hash) ([]byte, error) 134 } 135 136 // snapshot is the internal version of the snapshot data layer that supports some 137 // additional methods compared to the public API. 138 type snapshot interface { 139 Snapshot 140 141 // Parent returns the subsequent layer of a snapshot, or nil if the base was 142 // reached. 143 // 144 // Note, the method is an internal helper to avoid type switching between the 145 // disk and diff layers. There is no locking involved. 146 Parent() snapshot 147 148 // Update creates a new layer on top of the existing snapshot diff tree with 149 // the specified data items. 150 // 151 // Note, the maps are retained by the method to avoid copying everything. 152 Update(blockRoot common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte, verified chan struct{}) *diffLayer 153 154 // Journal commits an entire diff hierarchy to disk into a single journal entry. 155 // This is meant to be used during shutdown to persist the snapshot without 156 // flattening everything down (bad for reorgs). 157 Journal(buffer *bytes.Buffer) (common.Hash, error) 158 159 // Stale return whether this layer has become stale (was flattened across) or 160 // if it's still live. 161 Stale() bool 162 163 // AccountIterator creates an account iterator over an arbitrary layer. 164 AccountIterator(seek common.Hash) AccountIterator 165 166 // StorageIterator creates a storage iterator over an arbitrary layer. 167 StorageIterator(account common.Hash, seek common.Hash) (StorageIterator, bool) 168 } 169 170 // Tree is an Ethereum state snapshot tree. It consists of one persistent base 171 // layer backed by a key-value store, on top of which arbitrarily many in-memory 172 // diff layers are topped. The memory diffs can form a tree with branching, but 173 // the disk layer is singleton and common to all. If a reorg goes deeper than the 174 // disk layer, everything needs to be deleted. 175 // 176 // The goal of a state snapshot is twofold: to allow direct access to account and 177 // storage data to avoid expensive multi-level trie lookups; and to allow sorted, 178 // cheap iteration of the account/storage tries for sync aid. 179 type Tree struct { 180 diskdb ethdb.KeyValueStore // Persistent database to store the snapshot 181 triedb *trie.Database // In-memory cache to access the trie through 182 cache int // Megabytes permitted to use for read caches 183 layers map[common.Hash]snapshot // Collection of all known layers 184 lock sync.RWMutex 185 capLimit int 186 } 187 188 // New attempts to load an already existing snapshot from a persistent key-value 189 // store (with a number of memory layers from a journal), ensuring that the head 190 // of the snapshot matches the expected one. 191 // 192 // If the snapshot is missing or the disk layer is broken, the entire is deleted 193 // and will be reconstructed from scratch based on the tries in the key-value 194 // store, on a background thread. If the memory layers from the journal is not 195 // continuous with disk layer or the journal is missing, all diffs will be discarded 196 // iff it's in "recovery" mode, otherwise rebuild is mandatory. 197 func New(diskdb ethdb.KeyValueStore, triedb *trie.Database, cache, cap int, root common.Hash, async bool, rebuild bool, recovery bool) (*Tree, error) { 198 // Create a new, empty snapshot tree 199 snap := &Tree{ 200 diskdb: diskdb, 201 triedb: triedb, 202 cache: cache, 203 capLimit: cap, 204 layers: make(map[common.Hash]snapshot), 205 } 206 if !async { 207 defer snap.waitBuild() 208 } 209 // Attempt to load a previously persisted snapshot and rebuild one if failed 210 head, disabled, err := loadSnapshot(diskdb, triedb, cache, root, recovery) 211 if disabled { 212 log.Warn("Snapshot maintenance disabled (syncing)") 213 return snap, nil 214 } 215 if err != nil { 216 if rebuild { 217 log.Warn("Failed to load snapshot, regenerating", "err", err) 218 snap.Rebuild(root) 219 return snap, nil 220 } 221 return nil, err // Bail out the error, don't rebuild automatically. 222 } 223 // Existing snapshot loaded, seed all the layers 224 for head != nil { 225 snap.layers[head.Root()] = head 226 head = head.Parent() 227 } 228 log.Info("Snapshot loaded", "diskRoot", snap.diskRoot(), "root", root) 229 return snap, nil 230 } 231 232 // waitBuild blocks until the snapshot finishes rebuilding. This method is meant 233 // to be used by tests to ensure we're testing what we believe we are. 234 func (t *Tree) waitBuild() { 235 // Find the rebuild termination channel 236 var done chan struct{} 237 238 t.lock.RLock() 239 for _, layer := range t.layers { 240 if layer, ok := layer.(*diskLayer); ok { 241 done = layer.genPending 242 break 243 } 244 } 245 t.lock.RUnlock() 246 247 // Wait until the snapshot is generated 248 if done != nil { 249 <-done 250 } 251 } 252 253 // Layers returns the number of layers 254 func (t *Tree) Layers() int { 255 return len(t.layers) 256 } 257 258 // Disable interrupts any pending snapshot generator, deletes all the snapshot 259 // layers in memory and marks snapshots disabled globally. In order to resume 260 // the snapshot functionality, the caller must invoke Rebuild. 261 func (t *Tree) Disable() { 262 // Interrupt any live snapshot layers 263 t.lock.Lock() 264 defer t.lock.Unlock() 265 266 for _, layer := range t.layers { 267 switch layer := layer.(type) { 268 case *diskLayer: 269 // If the base layer is generating, abort it 270 if layer.genAbort != nil { 271 abort := make(chan *generatorStats) 272 layer.genAbort <- abort 273 <-abort 274 } 275 // Layer should be inactive now, mark it as stale 276 layer.lock.Lock() 277 layer.stale = true 278 layer.lock.Unlock() 279 280 case *diffLayer: 281 // If the layer is a simple diff, simply mark as stale 282 layer.lock.Lock() 283 atomic.StoreUint32(&layer.stale, 1) 284 layer.lock.Unlock() 285 286 default: 287 panic(fmt.Sprintf("unknown layer type: %T", layer)) 288 } 289 } 290 t.layers = map[common.Hash]snapshot{} 291 292 // Delete all snapshot liveness information from the database 293 batch := t.diskdb.NewBatch() 294 295 rawdb.WriteSnapshotDisabled(batch) 296 rawdb.DeleteSnapshotRoot(batch) 297 rawdb.DeleteSnapshotJournal(batch) 298 rawdb.DeleteSnapshotGenerator(batch) 299 rawdb.DeleteSnapshotRecoveryNumber(batch) 300 // Note, we don't delete the sync progress 301 302 if err := batch.Write(); err != nil { 303 log.Crit("Failed to disable snapshots", "err", err) 304 } 305 } 306 307 // Snapshot retrieves a snapshot belonging to the given block root, or nil if no 308 // snapshot is maintained for that block. 309 func (t *Tree) Snapshot(blockRoot common.Hash) Snapshot { 310 t.lock.RLock() 311 defer t.lock.RUnlock() 312 313 return t.layers[blockRoot] 314 } 315 316 // Snapshots returns all visited layers from the topmost layer with specific 317 // root and traverses downward. The layer amount is limited by the given number. 318 // If nodisk is set, then disk layer is excluded. 319 func (t *Tree) Snapshots(root common.Hash, limits int, nodisk bool) []Snapshot { 320 t.lock.RLock() 321 defer t.lock.RUnlock() 322 323 if limits == 0 { 324 return nil 325 } 326 layer := t.layers[root] 327 if layer == nil { 328 return nil 329 } 330 var ret []Snapshot 331 for { 332 if _, isdisk := layer.(*diskLayer); isdisk && nodisk { 333 break 334 } 335 ret = append(ret, layer) 336 limits -= 1 337 if limits == 0 { 338 break 339 } 340 parent := layer.Parent() 341 if parent == nil { 342 break 343 } 344 layer = parent 345 } 346 return ret 347 } 348 349 func (t *Tree) Update(blockRoot common.Hash, parentRoot common.Hash, destructs map[common.Address]struct{}, accounts map[common.Address][]byte, storage map[common.Address]map[string][]byte, verified chan struct{}) error { 350 hashDestructs, hashAccounts, hashStorage := transformSnapData(destructs, accounts, storage) 351 return t.update(blockRoot, parentRoot, hashDestructs, hashAccounts, hashStorage, verified) 352 } 353 354 // Update adds a new snapshot into the tree, if that can be linked to an existing 355 // old parent. It is disallowed to insert a disk layer (the origin of all). 356 func (t *Tree) update(blockRoot common.Hash, parentRoot common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte, verified chan struct{}) error { 357 // Reject noop updates to avoid self-loops in the snapshot tree. This is a 358 // special case that can only happen for Clique networks where empty blocks 359 // don't modify the state (0 block subsidy). 360 // 361 // Although we could silently ignore this internally, it should be the caller's 362 // responsibility to avoid even attempting to insert such a snapshot. 363 if blockRoot == parentRoot { 364 return errSnapshotCycle 365 } 366 // Generate a new snapshot on top of the parent 367 parent := t.Snapshot(parentRoot) 368 if parent == nil { 369 return fmt.Errorf("parent [%#x] snapshot missing", parentRoot) 370 } 371 snap := parent.(snapshot).Update(blockRoot, destructs, accounts, storage, verified) 372 373 // Save the new snapshot for later 374 t.lock.Lock() 375 defer t.lock.Unlock() 376 377 t.layers[snap.root] = snap 378 log.Debug("Snapshot updated", "blockRoot", blockRoot) 379 return nil 380 } 381 382 func (t *Tree) CapLimit() int { 383 return t.capLimit 384 } 385 386 // Cap traverses downwards the snapshot tree from a head block hash until the 387 // number of allowed layers are crossed. All layers beyond the permitted number 388 // are flattened downwards. 389 // 390 // Note, the final diff layer count in general will be one more than the amount 391 // requested. This happens because the bottom-most diff layer is the accumulator 392 // which may or may not overflow and cascade to disk. Since this last layer's 393 // survival is only known *after* capping, we need to omit it from the count if 394 // we want to ensure that *at least* the requested number of diff layers remain. 395 func (t *Tree) Cap(root common.Hash, layers int) error { 396 // Retrieve the head snapshot to cap from 397 snap := t.Snapshot(root) 398 if snap == nil { 399 return fmt.Errorf("snapshot [%#x] missing", root) 400 } 401 diff, ok := snap.(*diffLayer) 402 if !ok { 403 return fmt.Errorf("snapshot [%#x] is disk layer", root) 404 } 405 // If the generator is still running, use a more aggressive cap 406 diff.origin.lock.RLock() 407 if diff.origin.genMarker != nil && layers > 8 { 408 layers = 8 409 } 410 diff.origin.lock.RUnlock() 411 412 // Run the internal capping and discard all stale layers 413 t.lock.Lock() 414 defer t.lock.Unlock() 415 416 // Flattening the bottom-most diff layer requires special casing since there's 417 // no child to rewire to the grandparent. In that case we can fake a temporary 418 // child for the capping and then remove it. 419 if layers == 0 { 420 // If full commit was requested, flatten the diffs and merge onto disk 421 diff.lock.RLock() 422 base := diffToDisk(diff.flatten().(*diffLayer)) 423 diff.lock.RUnlock() 424 425 // Replace the entire snapshot tree with the flat base 426 t.layers = map[common.Hash]snapshot{base.root: base} 427 return nil 428 } 429 persisted := t.cap(diff, layers) 430 431 // Remove any layer that is stale or links into a stale layer 432 children := make(map[common.Hash][]common.Hash) 433 for root, snap := range t.layers { 434 if diff, ok := snap.(*diffLayer); ok { 435 parent := diff.parent.Root() 436 children[parent] = append(children[parent], root) 437 } 438 } 439 var remove func(root common.Hash) 440 remove = func(root common.Hash) { 441 delete(t.layers, root) 442 for _, child := range children[root] { 443 remove(child) 444 } 445 delete(children, root) 446 } 447 for root, snap := range t.layers { 448 if snap.Stale() { 449 remove(root) 450 } 451 } 452 // If the disk layer was modified, regenerate all the cumulative blooms 453 if persisted != nil { 454 var rebloom func(root common.Hash) 455 rebloom = func(root common.Hash) { 456 if diff, ok := t.layers[root].(*diffLayer); ok { 457 diff.rebloom(persisted) 458 } 459 for _, child := range children[root] { 460 rebloom(child) 461 } 462 } 463 rebloom(persisted.root) 464 } 465 log.Debug("Snapshot capped", "root", root) 466 return nil 467 } 468 469 // cap traverses downwards the diff tree until the number of allowed layers are 470 // crossed. All diffs beyond the permitted number are flattened downwards. If the 471 // layer limit is reached, memory cap is also enforced (but not before). 472 // 473 // The method returns the new disk layer if diffs were persisted into it. 474 // 475 // Note, the final diff layer count in general will be one more than the amount 476 // requested. This happens because the bottom-most diff layer is the accumulator 477 // which may or may not overflow and cascade to disk. Since this last layer's 478 // survival is only known *after* capping, we need to omit it from the count if 479 // we want to ensure that *at least* the requested number of diff layers remain. 480 func (t *Tree) cap(diff *diffLayer, layers int) *diskLayer { 481 // Dive until we run out of layers or reach the persistent database 482 for i := 0; i < layers-1; i++ { 483 // If we still have diff layers below, continue down 484 if parent, ok := diff.parent.(*diffLayer); ok { 485 diff = parent 486 } else { 487 // Diff stack too shallow, return without modifications 488 return nil 489 } 490 } 491 // We're out of layers, flatten anything below, stopping if it's the disk or if 492 // the memory limit is not yet exceeded. 493 switch parent := diff.parent.(type) { 494 case *diskLayer: 495 return nil 496 497 case *diffLayer: 498 // Flatten the parent into the grandparent. The flattening internally obtains a 499 // write lock on grandparent. 500 flattened := parent.flatten().(*diffLayer) 501 t.layers[flattened.root] = flattened 502 503 diff.lock.Lock() 504 defer diff.lock.Unlock() 505 506 diff.parent = flattened 507 if flattened.memory < aggregatorMemoryLimit { 508 // Accumulator layer is smaller than the limit, so we can abort, unless 509 // there's a snapshot being generated currently. In that case, the trie 510 // will move fron underneath the generator so we **must** merge all the 511 // partial data down into the snapshot and restart the generation. 512 if flattened.parent.(*diskLayer).genAbort == nil { 513 return nil 514 } 515 } 516 default: 517 panic(fmt.Sprintf("unknown data layer: %T", parent)) 518 } 519 // If the bottom-most layer is larger than our memory cap, persist to disk 520 bottom := diff.parent.(*diffLayer) 521 522 bottom.lock.RLock() 523 base := diffToDisk(bottom) 524 bottom.lock.RUnlock() 525 526 t.layers[base.root] = base 527 diff.parent = base 528 return base 529 } 530 531 // diffToDisk merges a bottom-most diff into the persistent disk layer underneath 532 // it. The method will panic if called onto a non-bottom-most diff layer. 533 // 534 // The disk layer persistence should be operated in an atomic way. All updates should 535 // be discarded if the whole transition if not finished. 536 func diffToDisk(bottom *diffLayer) *diskLayer { 537 var ( 538 base = bottom.parent.(*diskLayer) 539 batch = base.diskdb.NewBatch() 540 stats *generatorStats 541 ) 542 // If the disk layer is running a snapshot generator, abort it 543 if base.genAbort != nil { 544 abort := make(chan *generatorStats) 545 base.genAbort <- abort 546 stats = <-abort 547 } 548 // Put the deletion in the batch writer, flush all updates in the final step. 549 rawdb.DeleteSnapshotRoot(batch) 550 551 // Mark the original base as stale as we're going to create a new wrapper 552 base.lock.Lock() 553 if base.stale { 554 panic("parent disk layer is stale") // we've committed into the same base from two children, boo 555 } 556 base.stale = true 557 base.lock.Unlock() 558 559 // Destroy all the destructed accounts from the database 560 for hash := range bottom.destructSet { 561 // Skip any account not covered yet by the snapshot 562 if base.genMarker != nil && bytes.Compare(hash[:], base.genMarker) > 0 { 563 continue 564 } 565 // Remove all storage slots 566 rawdb.DeleteAccountSnapshot(batch, hash) 567 base.cache.Set(hash[:], nil) 568 569 it := rawdb.IterateStorageSnapshots(base.diskdb, hash) 570 for it.Next() { 571 if key := it.Key(); len(key) == 65 { // TODO(karalabe): Yuck, we should move this into the iterator 572 batch.Delete(key) 573 base.cache.Del(key[1:]) 574 snapshotFlushStorageItemMeter.Mark(1) 575 576 // Ensure we don't delete too much data blindly (contract can be 577 // huge). It's ok to flush, the root will go missing in case of a 578 // crash and we'll detect and regenerate the snapshot. 579 if batch.ValueSize() > ethdb.IdealBatchSize { 580 if err := batch.Write(); err != nil { 581 log.Crit("Failed to write storage deletions", "err", err) 582 } 583 batch.Reset() 584 } 585 } 586 } 587 it.Release() 588 } 589 // Push all updated accounts into the database 590 for hash, data := range bottom.accountData { 591 // Skip any account not covered yet by the snapshot 592 if base.genMarker != nil && bytes.Compare(hash[:], base.genMarker) > 0 { 593 continue 594 } 595 // Push the account to disk 596 rawdb.WriteAccountSnapshot(batch, hash, data) 597 base.cache.Set(hash[:], data) 598 snapshotCleanAccountWriteMeter.Mark(int64(len(data))) 599 600 snapshotFlushAccountItemMeter.Mark(1) 601 snapshotFlushAccountSizeMeter.Mark(int64(len(data))) 602 603 // Ensure we don't write too much data blindly. It's ok to flush, the 604 // root will go missing in case of a crash and we'll detect and regen 605 // the snapshot. 606 if batch.ValueSize() > ethdb.IdealBatchSize { 607 if err := batch.Write(); err != nil { 608 log.Crit("Failed to write storage deletions", "err", err) 609 } 610 batch.Reset() 611 } 612 } 613 // Push all the storage slots into the database 614 for accountHash, storage := range bottom.storageData { 615 // Skip any account not covered yet by the snapshot 616 if base.genMarker != nil && bytes.Compare(accountHash[:], base.genMarker) > 0 { 617 continue 618 } 619 // Generation might be mid-account, track that case too 620 midAccount := base.genMarker != nil && bytes.Equal(accountHash[:], base.genMarker[:common.HashLength]) 621 622 for storageHash, data := range storage { 623 // Skip any slot not covered yet by the snapshot 624 if midAccount && bytes.Compare(storageHash[:], base.genMarker[common.HashLength:]) > 0 { 625 continue 626 } 627 if len(data) > 0 { 628 rawdb.WriteStorageSnapshot(batch, accountHash, storageHash, data) 629 base.cache.Set(append(accountHash[:], storageHash[:]...), data) 630 snapshotCleanStorageWriteMeter.Mark(int64(len(data))) 631 } else { 632 rawdb.DeleteStorageSnapshot(batch, accountHash, storageHash) 633 base.cache.Set(append(accountHash[:], storageHash[:]...), nil) 634 } 635 snapshotFlushStorageItemMeter.Mark(1) 636 snapshotFlushStorageSizeMeter.Mark(int64(len(data))) 637 } 638 } 639 // Update the snapshot block marker and write any remainder data 640 rawdb.WriteSnapshotRoot(batch, bottom.root) 641 642 // Write out the generator progress marker and report 643 journalProgress(batch, base.genMarker, stats) 644 645 // Flush all the updates in the single db operation. Ensure the 646 // disk layer transition is atomic. 647 if err := batch.Write(); err != nil { 648 log.Crit("Failed to write leftover snapshot", "err", err) 649 } 650 log.Debug("Journalled disk layer", "root", bottom.root, "complete", base.genMarker == nil) 651 res := &diskLayer{ 652 root: bottom.root, 653 cache: base.cache, 654 diskdb: base.diskdb, 655 triedb: base.triedb, 656 genMarker: base.genMarker, 657 genPending: base.genPending, 658 } 659 // If snapshot generation hasn't finished yet, port over all the starts and 660 // continue where the previous round left off. 661 // 662 // Note, the `base.genAbort` comparison is not used normally, it's checked 663 // to allow the tests to play with the marker without triggering this path. 664 if base.genMarker != nil && base.genAbort != nil { 665 res.genMarker = base.genMarker 666 res.genAbort = make(chan chan *generatorStats) 667 go res.generate(stats) 668 } 669 return res 670 } 671 672 // Journal commits an entire diff hierarchy to disk into a single journal entry. 673 // This is meant to be used during shutdown to persist the snapshot without 674 // flattening everything down (bad for reorgs). 675 // 676 // The method returns the root hash of the base layer that needs to be persisted 677 // to disk as a trie too to allow continuing any pending generation op. 678 func (t *Tree) Journal(root common.Hash) (common.Hash, error) { 679 // Retrieve the head snapshot to journal from var snap snapshot 680 snap := t.Snapshot(root) 681 if snap == nil { 682 return common.Hash{}, fmt.Errorf("snapshot [%#x] missing", root) 683 } 684 // Wait the snapshot(difflayer) is verified, it means the account data also been refreshed with the correct data 685 if !snap.WaitAndGetVerifyRes() { 686 return common.Hash{}, ErrSnapshotStale 687 } 688 689 // Run the journaling 690 t.lock.Lock() 691 defer t.lock.Unlock() 692 693 // Firstly write out the metadata of journal 694 journal := new(bytes.Buffer) 695 if err := rlp.Encode(journal, journalVersion); err != nil { 696 return common.Hash{}, err 697 } 698 diskroot := t.diskRoot() 699 if diskroot == (common.Hash{}) { 700 return common.Hash{}, errors.New("invalid disk root") 701 } 702 // Secondly write out the disk layer root, ensure the 703 // diff journal is continuous with disk. 704 if err := rlp.Encode(journal, diskroot); err != nil { 705 return common.Hash{}, err 706 } 707 // Finally write out the journal of each layer in reverse order. 708 base, err := snap.(snapshot).Journal(journal) 709 if err != nil { 710 return common.Hash{}, err 711 } 712 // Store the journal into the database and return 713 rawdb.WriteSnapshotJournal(t.diskdb, journal.Bytes()) 714 return base, nil 715 } 716 717 // Rebuild wipes all available snapshot data from the persistent database and 718 // discard all caches and diff layers. Afterwards, it starts a new snapshot 719 // generator with the given root hash. 720 func (t *Tree) Rebuild(root common.Hash) { 721 t.lock.Lock() 722 defer t.lock.Unlock() 723 724 // Firstly delete any recovery flag in the database. Because now we are 725 // building a brand new snapshot. Also reenable the snapshot feature. 726 rawdb.DeleteSnapshotRecoveryNumber(t.diskdb) 727 rawdb.DeleteSnapshotDisabled(t.diskdb) 728 729 // Iterate over and mark all layers stale 730 for _, layer := range t.layers { 731 switch layer := layer.(type) { 732 case *diskLayer: 733 // If the base layer is generating, abort it and save 734 if layer.genAbort != nil { 735 abort := make(chan *generatorStats) 736 layer.genAbort <- abort 737 <-abort 738 } 739 // Layer should be inactive now, mark it as stale 740 layer.lock.Lock() 741 layer.stale = true 742 layer.lock.Unlock() 743 744 case *diffLayer: 745 // If the layer is a simple diff, simply mark as stale 746 layer.lock.Lock() 747 atomic.StoreUint32(&layer.stale, 1) 748 layer.lock.Unlock() 749 750 default: 751 panic(fmt.Sprintf("unknown layer type: %T", layer)) 752 } 753 } 754 // Start generating a new snapshot from scratch on a background thread. The 755 // generator will run a wiper first if there's not one running right now. 756 log.Info("Rebuilding state snapshot") 757 t.layers = map[common.Hash]snapshot{ 758 root: generateSnapshot(t.diskdb, t.triedb, t.cache, root), 759 } 760 } 761 762 // AccountIterator creates a new account iterator for the specified root hash and 763 // seeks to a starting account hash. 764 func (t *Tree) AccountIterator(root common.Hash, seek common.Hash) (AccountIterator, error) { 765 ok, err := t.generating() 766 if err != nil { 767 return nil, err 768 } 769 if ok { 770 return nil, ErrNotConstructed 771 } 772 return newFastAccountIterator(t, root, seek) 773 } 774 775 // StorageIterator creates a new storage iterator for the specified root hash and 776 // account. The iterator will be move to the specific start position. 777 func (t *Tree) StorageIterator(root common.Hash, account common.Hash, seek common.Hash) (StorageIterator, error) { 778 ok, err := t.generating() 779 if err != nil { 780 return nil, err 781 } 782 if ok { 783 return nil, ErrNotConstructed 784 } 785 return newFastStorageIterator(t, root, account, seek) 786 } 787 788 // Verify iterates the whole state(all the accounts as well as the corresponding storages) 789 // with the specific root and compares the re-computed hash with the original one. 790 func (t *Tree) Verify(root common.Hash) error { 791 acctIt, err := t.AccountIterator(root, common.Hash{}) 792 if err != nil { 793 return err 794 } 795 defer acctIt.Release() 796 797 got, err := generateTrieRoot(nil, acctIt, common.Hash{}, stackTrieGenerate, func(db ethdb.KeyValueWriter, accountHash, codeHash common.Hash, stat *generateStats) (common.Hash, error) { 798 storageIt, err := t.StorageIterator(root, accountHash, common.Hash{}) 799 if err != nil { 800 return common.Hash{}, err 801 } 802 defer storageIt.Release() 803 804 hash, err := generateTrieRoot(nil, storageIt, accountHash, stackTrieGenerate, nil, stat, false) 805 if err != nil { 806 return common.Hash{}, err 807 } 808 return hash, nil 809 }, newGenerateStats(), true) 810 811 if err != nil { 812 return err 813 } 814 if got != root { 815 return fmt.Errorf("state root hash mismatch: got %x, want %x", got, root) 816 } 817 return nil 818 } 819 820 // disklayer is an internal helper function to return the disk layer. 821 // The lock of snapTree is assumed to be held already. 822 func (t *Tree) disklayer() *diskLayer { 823 var snap snapshot 824 for _, s := range t.layers { 825 snap = s 826 break 827 } 828 if snap == nil { 829 return nil 830 } 831 switch layer := snap.(type) { 832 case *diskLayer: 833 return layer 834 case *diffLayer: 835 return layer.origin 836 default: 837 panic(fmt.Sprintf("%T: undefined layer", snap)) 838 } 839 } 840 841 // diskRoot is a internal helper function to return the disk layer root. 842 // The lock of snapTree is assumed to be held already. 843 func (t *Tree) diskRoot() common.Hash { 844 disklayer := t.disklayer() 845 if disklayer == nil { 846 return common.Hash{} 847 } 848 return disklayer.Root() 849 } 850 851 // generating is an internal helper function which reports whether the snapshot 852 // is still under the construction. 853 func (t *Tree) generating() (bool, error) { 854 t.lock.Lock() 855 defer t.lock.Unlock() 856 857 layer := t.disklayer() 858 if layer == nil { 859 return false, errors.New("disk layer is missing") 860 } 861 layer.lock.RLock() 862 defer layer.lock.RUnlock() 863 return layer.genMarker != nil, nil 864 } 865 866 // diskRoot is a external helper function to return the disk layer root. 867 func (t *Tree) DiskRoot() common.Hash { 868 t.lock.Lock() 869 defer t.lock.Unlock() 870 871 return t.diskRoot() 872 } 873 874 // TODO we can further improve it when the set is very large 875 func transformSnapData(destructs map[common.Address]struct{}, accounts map[common.Address][]byte, 876 storage map[common.Address]map[string][]byte) (map[common.Hash]struct{}, map[common.Hash][]byte, 877 map[common.Hash]map[common.Hash][]byte) { 878 hasher := crypto.NewKeccakState() 879 hashDestructs := make(map[common.Hash]struct{}, len(destructs)) 880 hashAccounts := make(map[common.Hash][]byte, len(accounts)) 881 hashStorages := make(map[common.Hash]map[common.Hash][]byte, len(storage)) 882 for addr := range destructs { 883 hashDestructs[crypto.Keccak256Hash(addr[:])] = struct{}{} 884 } 885 for addr, account := range accounts { 886 hashAccounts[crypto.Keccak256Hash(addr[:])] = account 887 } 888 for addr, accountStore := range storage { 889 hashStorage := make(map[common.Hash][]byte, len(accountStore)) 890 for k, v := range accountStore { 891 hashStorage[crypto.HashData(hasher, []byte(k))] = v 892 } 893 hashStorages[crypto.Keccak256Hash(addr[:])] = hashStorage 894 } 895 return hashDestructs, hashAccounts, hashStorages 896 }