@@ -19,6 +19,7 @@ package snapshot
1919import (
2020 "bytes"
2121 "encoding/binary"
22+ "fmt"
2223 "math/big"
2324 "time"
2425
@@ -116,6 +117,38 @@ func generateSnapshot(diskdb ethdb.KeyValueStore, triedb *trie.Database, cache i
116117 return base
117118}
118119
120+ // journalProgress persists the generator stats into the database to resume later.
121+ func journalProgress (db ethdb.KeyValueWriter , marker []byte , stats * generatorStats ) {
122+ // Write out the generator marker. Note it's a standalone disk layer generator
123+ // which is not mixed with journal. It's ok if the generator is persisted while
124+ // journal is not.
125+ entry := journalGenerator {
126+ Done : marker == nil ,
127+ Marker : marker ,
128+ }
129+ if stats != nil {
130+ entry .Wiping = (stats .wiping != nil )
131+ entry .Accounts = stats .accounts
132+ entry .Slots = stats .slots
133+ entry .Storage = uint64 (stats .storage )
134+ }
135+ blob , err := rlp .EncodeToBytes (entry )
136+ if err != nil {
137+ panic (err ) // Cannot happen, here to catch dev errors
138+ }
139+ var logstr string
140+ switch len (marker ) {
141+ case 0 :
142+ logstr = "done"
143+ case common .HashLength :
144+ logstr = fmt .Sprintf ("%#x" , marker )
145+ default :
146+ logstr = fmt .Sprintf ("%#x:%#x" , marker [:common .HashLength ], marker [common .HashLength :])
147+ }
148+ log .Debug ("Journalled generator progress" , "progress" , logstr )
149+ rawdb .WriteSnapshotGenerator (db , blob )
150+ }
151+
119152// generate is a background thread that iterates over the state and storage tries,
120153// constructing the state snapshot. All the arguments are purely for statistics
121154// gethering and logging, since the method surfs the blocks as they arrive, often
@@ -187,11 +220,15 @@ func (dl *diskLayer) generate(stats *generatorStats) {
187220 if batch .ValueSize () > ethdb .IdealBatchSize || abort != nil {
188221 // Only write and set the marker if we actually did something useful
189222 if batch .ValueSize () > 0 {
223+ // Ensure the generator entry is in sync with the data
224+ marker := accountHash [:]
225+ journalProgress (batch , marker , stats )
226+
190227 batch .Write ()
191228 batch .Reset ()
192229
193230 dl .lock .Lock ()
194- dl .genMarker = accountHash [:]
231+ dl .genMarker = marker
195232 dl .lock .Unlock ()
196233 }
197234 if abort != nil {
@@ -228,11 +265,15 @@ func (dl *diskLayer) generate(stats *generatorStats) {
228265 if batch .ValueSize () > ethdb .IdealBatchSize || abort != nil {
229266 // Only write and set the marker if we actually did something useful
230267 if batch .ValueSize () > 0 {
268+ // Ensure the generator entry is in sync with the data
269+ marker := append (accountHash [:], storeIt .Key ... )
270+ journalProgress (batch , marker , stats )
271+
231272 batch .Write ()
232273 batch .Reset ()
233274
234275 dl .lock .Lock ()
235- dl .genMarker = append ( accountHash [:], storeIt . Key ... )
276+ dl .genMarker = marker
236277 dl .lock .Unlock ()
237278 }
238279 if abort != nil {
@@ -264,6 +305,9 @@ func (dl *diskLayer) generate(stats *generatorStats) {
264305 }
265306 // Snapshot fully generated, set the marker to nil
266307 if batch .ValueSize () > 0 {
308+ // Ensure the generator entry is in sync with the data
309+ journalProgress (batch , nil , stats )
310+
267311 batch .Write ()
268312 }
269313 log .Info ("Generated state snapshot" , "accounts" , stats .accounts , "slots" , stats .slots ,
0 commit comments