difflayer_journal.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 (
  41. number uint64
  42. root common.Hash
  43. )
  44. if err := r.Decode(&number); err != nil {
  45. // The first read may fail with EOF, marking the end of the journal
  46. if err == io.EOF {
  47. return parent, nil
  48. }
  49. return nil, fmt.Errorf("load diff number: %v", err)
  50. }
  51. if err := r.Decode(&root); err != nil {
  52. return nil, fmt.Errorf("load diff root: %v", err)
  53. }
  54. var accounts []journalAccount
  55. if err := r.Decode(&accounts); err != nil {
  56. return nil, fmt.Errorf("load diff accounts: %v", err)
  57. }
  58. accountData := make(map[common.Hash][]byte)
  59. for _, entry := range accounts {
  60. accountData[entry.Hash] = entry.Blob
  61. }
  62. var storage []journalStorage
  63. if err := r.Decode(&storage); err != nil {
  64. return nil, fmt.Errorf("load diff storage: %v", err)
  65. }
  66. storageData := make(map[common.Hash]map[common.Hash][]byte)
  67. for _, entry := range storage {
  68. slots := make(map[common.Hash][]byte)
  69. for i, key := range entry.Keys {
  70. slots[key] = entry.Vals[i]
  71. }
  72. storageData[entry.Hash] = slots
  73. }
  74. // Validate the block number to avoid state corruption
  75. if parent, ok := parent.(*diffLayer); ok {
  76. if number != parent.number+1 {
  77. return nil, fmt.Errorf("snapshot chain broken: block #%d after #%d", number, parent.number)
  78. }
  79. }
  80. return loadDiffLayer(newDiffLayer(parent, number, root, accountData, storageData), r)
  81. }
  82. // journal is the internal version of Journal that also returns the journal file
  83. // so subsequent layers know where to write to.
  84. func (dl *diffLayer) journal() (io.WriteCloser, error) {
  85. // If we've reached the bottom, open the journal
  86. var writer io.WriteCloser
  87. if parent, ok := dl.parent.(*diskLayer); ok {
  88. file, err := os.Create(parent.journal)
  89. if err != nil {
  90. return nil, err
  91. }
  92. writer = file
  93. }
  94. // If we haven't reached the bottom yet, journal the parent first
  95. if writer == nil {
  96. file, err := dl.parent.(*diffLayer).journal()
  97. if err != nil {
  98. return nil, err
  99. }
  100. writer = file
  101. }
  102. dl.lock.RLock()
  103. defer dl.lock.RUnlock()
  104. if dl.stale {
  105. writer.Close()
  106. return nil, ErrSnapshotStale
  107. }
  108. buf := bufio.NewWriter(writer)
  109. // Everything below was journalled, persist this layer too
  110. if err := rlp.Encode(buf, dl.number); err != nil {
  111. buf.Flush()
  112. writer.Close()
  113. return nil, err
  114. }
  115. if err := rlp.Encode(buf, dl.root); err != nil {
  116. buf.Flush()
  117. writer.Close()
  118. return nil, err
  119. }
  120. accounts := make([]journalAccount, 0, len(dl.accountData))
  121. for hash, blob := range dl.accountData {
  122. accounts = append(accounts, journalAccount{Hash: hash, Blob: blob})
  123. }
  124. if err := rlp.Encode(buf, accounts); err != nil {
  125. buf.Flush()
  126. writer.Close()
  127. return nil, err
  128. }
  129. storage := make([]journalStorage, 0, len(dl.storageData))
  130. for hash, slots := range dl.storageData {
  131. keys := make([]common.Hash, 0, len(slots))
  132. vals := make([][]byte, 0, len(slots))
  133. for key, val := range slots {
  134. keys = append(keys, key)
  135. vals = append(vals, val)
  136. }
  137. storage = append(storage, journalStorage{Hash: hash, Keys: keys, Vals: vals})
  138. }
  139. if err := rlp.Encode(buf, storage); err != nil {
  140. buf.Flush()
  141. writer.Close()
  142. return nil, err
  143. }
  144. buf.Flush()
  145. return writer, nil
  146. }