difflayer_journal.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. package snapshot
  17. import (
  18. "bufio"
  19. "fmt"
  20. "io"
  21. "os"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. )
  25. // journalAccount is an account entry in a diffLayer's disk journal.
  26. type journalAccount struct {
  27. Hash common.Hash
  28. Blob []byte
  29. }
  30. // journalStorage is an account's storage map in a diffLayer's disk journal.
  31. type journalStorage struct {
  32. Hash common.Hash
  33. Keys []common.Hash
  34. Vals [][]byte
  35. }
  36. // loadDiffLayer reads the next sections of a snapshot journal, reconstructing a new
  37. // diff and verifying that it can be linked to the requested parent.
  38. func loadDiffLayer(parent snapshot, r *rlp.Stream) (snapshot, error) {
  39. // Read the next diff journal entry
  40. var root common.Hash
  41. if err := r.Decode(&root); err != nil {
  42. // The first read may fail with EOF, marking the end of the journal
  43. if err == io.EOF {
  44. return parent, nil
  45. }
  46. return nil, fmt.Errorf("load diff root: %v", err)
  47. }
  48. var accounts []journalAccount
  49. if err := r.Decode(&accounts); err != nil {
  50. return nil, fmt.Errorf("load diff accounts: %v", err)
  51. }
  52. accountData := make(map[common.Hash][]byte)
  53. for _, entry := range accounts {
  54. accountData[entry.Hash] = entry.Blob
  55. }
  56. var storage []journalStorage
  57. if err := r.Decode(&storage); err != nil {
  58. return nil, fmt.Errorf("load diff storage: %v", err)
  59. }
  60. storageData := make(map[common.Hash]map[common.Hash][]byte)
  61. for _, entry := range storage {
  62. slots := make(map[common.Hash][]byte)
  63. for i, key := range entry.Keys {
  64. slots[key] = entry.Vals[i]
  65. }
  66. storageData[entry.Hash] = slots
  67. }
  68. return loadDiffLayer(newDiffLayer(parent, root, accountData, storageData), r)
  69. }
  70. // journal is the internal version of Journal that also returns the journal file
  71. // so subsequent layers know where to write to.
  72. func (dl *diffLayer) journal() (io.WriteCloser, error) {
  73. // If we've reached the bottom, open the journal
  74. var writer io.WriteCloser
  75. if parent, ok := dl.parent.(*diskLayer); ok {
  76. file, err := os.Create(parent.journal)
  77. if err != nil {
  78. return nil, err
  79. }
  80. writer = file
  81. }
  82. // If we haven't reached the bottom yet, journal the parent first
  83. if writer == nil {
  84. file, err := dl.parent.(*diffLayer).journal()
  85. if err != nil {
  86. return nil, err
  87. }
  88. writer = file
  89. }
  90. dl.lock.RLock()
  91. defer dl.lock.RUnlock()
  92. if dl.stale {
  93. writer.Close()
  94. return nil, ErrSnapshotStale
  95. }
  96. // Everything below was journalled, persist this layer too
  97. buf := bufio.NewWriter(writer)
  98. if err := rlp.Encode(buf, dl.root); err != nil {
  99. buf.Flush()
  100. writer.Close()
  101. return nil, err
  102. }
  103. accounts := make([]journalAccount, 0, len(dl.accountData))
  104. for hash, blob := range dl.accountData {
  105. accounts = append(accounts, journalAccount{Hash: hash, Blob: blob})
  106. }
  107. if err := rlp.Encode(buf, accounts); err != nil {
  108. buf.Flush()
  109. writer.Close()
  110. return nil, err
  111. }
  112. storage := make([]journalStorage, 0, len(dl.storageData))
  113. for hash, slots := range dl.storageData {
  114. keys := make([]common.Hash, 0, len(slots))
  115. vals := make([][]byte, 0, len(slots))
  116. for key, val := range slots {
  117. keys = append(keys, key)
  118. vals = append(vals, val)
  119. }
  120. storage = append(storage, journalStorage{Hash: hash, Keys: keys, Vals: vals})
  121. }
  122. if err := rlp.Encode(buf, storage); err != nil {
  123. buf.Flush()
  124. writer.Close()
  125. return nil, err
  126. }
  127. buf.Flush()
  128. return writer, nil
  129. }