1717package snapshot
1818
1919import (
20- "bufio "
20+ "bytes "
2121 "encoding/binary"
2222 "errors"
2323 "fmt"
2424 "io"
25- "os"
2625 "time"
2726
2827 "github.com/VictoriaMetrics/fastcache"
@@ -58,7 +57,7 @@ type journalStorage struct {
5857}
5958
6059// loadSnapshot loads a pre-existing state snapshot backed by a key-value store.
61- func loadSnapshot (diskdb ethdb.KeyValueStore , triedb * trie.Database , journal string , cache int , root common.Hash ) (snapshot , error ) {
60+ func loadSnapshot (diskdb ethdb.KeyValueStore , triedb * trie.Database , cache int , root common.Hash ) (snapshot , error ) {
6261 // Retrieve the block number and hash of the snapshot, failing if no snapshot
6362 // is present in the database (or crashed mid-update).
6463 baseRoot := rawdb .ReadSnapshotRoot (diskdb )
@@ -71,13 +70,13 @@ func loadSnapshot(diskdb ethdb.KeyValueStore, triedb *trie.Database, journal str
7170 cache : fastcache .New (cache * 1024 * 1024 ),
7271 root : baseRoot ,
7372 }
74- // Open the journal, it must exist since even for 0 layer it stores whether
73+ // Retrieve the journal, it must exist since even for 0 layer it stores whether
7574 // we've already generated the snapshot or are in progress only
76- file , err := os . Open ( journal )
77- if err != nil {
78- return nil , err
75+ journal := rawdb . ReadSnapshotJournal ( diskdb )
76+ if len ( journal ) == 0 {
77+ return nil , errors . New ( "missing or corrupted snapshot journal" )
7978 }
80- r := rlp .NewStream (file , 0 )
79+ r := rlp .NewStream (bytes . NewReader ( journal ) , 0 )
8180
8281 // Read the snapshot generation progress for the disk layer
8382 var generator journalGenerator
@@ -162,9 +161,9 @@ func loadDiffLayer(parent snapshot, r *rlp.Stream) (snapshot, error) {
162161 return loadDiffLayer (newDiffLayer (parent , root , accountData , storageData ), r )
163162}
164163
165- // Journal is the internal version of Journal that also returns the journal file
166- // so subsequent layers know where to write to .
167- func (dl * diskLayer ) Journal (path string ) (io. WriteCloser , common.Hash , error ) {
164+ // Journal writes the persistent layer generator stats into a buffer to be stored
165+ // in the database as the snapshot journal .
166+ func (dl * diskLayer ) Journal (buffer * bytes. Buffer ) (common.Hash , error ) {
168167 // If the snapshot is currenty being generated, abort it
169168 var stats * generatorStats
170169 if dl .genAbort != nil {
@@ -180,12 +179,7 @@ func (dl *diskLayer) Journal(path string) (io.WriteCloser, common.Hash, error) {
180179 defer dl .lock .RUnlock ()
181180
182181 if dl .stale {
183- return nil , common.Hash {}, ErrSnapshotStale
184- }
185- // We've reached the bottom, open the journal
186- file , err := os .Create (path )
187- if err != nil {
188- return nil , common.Hash {}, err
182+ return common.Hash {}, ErrSnapshotStale
189183 }
190184 // Write out the generator marker
191185 entry := journalGenerator {
@@ -198,44 +192,37 @@ func (dl *diskLayer) Journal(path string) (io.WriteCloser, common.Hash, error) {
198192 entry .Slots = stats .slots
199193 entry .Storage = uint64 (stats .storage )
200194 }
201- if err := rlp .Encode (file , entry ); err != nil {
202- file .Close ()
203- return nil , common.Hash {}, err
195+ if err := rlp .Encode (buffer , entry ); err != nil {
196+ return common.Hash {}, err
204197 }
205- return file , dl .root , nil
198+ return dl .root , nil
206199}
207200
208- // Journal is the internal version of Journal that also returns the journal file
209- // so subsequent layers know where to write to .
210- func (dl * diffLayer ) Journal (path string ) (io. WriteCloser , common.Hash , error ) {
201+ // Journal writes the memory layer contents into a buffer to be stored in the
202+ // database as the snapshot journal .
203+ func (dl * diffLayer ) Journal (buffer * bytes. Buffer ) (common.Hash , error ) {
211204 // Journal the parent first
212- writer , base , err := dl .parent .Journal (path )
205+ base , err := dl .parent .Journal (buffer )
213206 if err != nil {
214- return nil , common.Hash {}, err
207+ return common.Hash {}, err
215208 }
216209 // Ensure the layer didn't get stale
217210 dl .lock .RLock ()
218211 defer dl .lock .RUnlock ()
219212
220213 if dl .stale {
221- writer .Close ()
222- return nil , common.Hash {}, ErrSnapshotStale
214+ return common.Hash {}, ErrSnapshotStale
223215 }
224216 // Everything below was journalled, persist this layer too
225- buf := bufio .NewWriter (writer )
226- if err := rlp .Encode (buf , dl .root ); err != nil {
227- buf .Flush ()
228- writer .Close ()
229- return nil , common.Hash {}, err
217+ if err := rlp .Encode (buffer , dl .root ); err != nil {
218+ return common.Hash {}, err
230219 }
231220 accounts := make ([]journalAccount , 0 , len (dl .accountData ))
232221 for hash , blob := range dl .accountData {
233222 accounts = append (accounts , journalAccount {Hash : hash , Blob : blob })
234223 }
235- if err := rlp .Encode (buf , accounts ); err != nil {
236- buf .Flush ()
237- writer .Close ()
238- return nil , common.Hash {}, err
224+ if err := rlp .Encode (buffer , accounts ); err != nil {
225+ return common.Hash {}, err
239226 }
240227 storage := make ([]journalStorage , 0 , len (dl .storageData ))
241228 for hash , slots := range dl .storageData {
@@ -247,11 +234,8 @@ func (dl *diffLayer) Journal(path string) (io.WriteCloser, common.Hash, error) {
247234 }
248235 storage = append (storage , journalStorage {Hash : hash , Keys : keys , Vals : vals })
249236 }
250- if err := rlp .Encode (buf , storage ); err != nil {
251- buf .Flush ()
252- writer .Close ()
253- return nil , common.Hash {}, err
237+ if err := rlp .Encode (buffer , storage ); err != nil {
238+ return common.Hash {}, err
254239 }
255- buf .Flush ()
256- return writer , base , nil
240+ return base , nil
257241}
0 commit comments