@@ -3,9 +3,10 @@ package commands
33import (
44 "bytes"
55 "context"
6+ "errors"
67 "fmt"
7-
88 "github.com/ledgerwatch/turbo-geth/cmd/utils"
9+ "github.com/ledgerwatch/turbo-geth/common/dbutils"
910
1011 "github.com/ledgerwatch/turbo-geth/common"
1112 "github.com/ledgerwatch/turbo-geth/common/changeset"
@@ -120,7 +121,8 @@ func syncBySmallSteps(ctx context.Context, chaindata string) error {
120121 }
121122
122123 // All stages forward to `execStage + unwindEvery` block
123- execToBlock := progress (stages .Execution ).BlockNumber + unwindEvery
124+ execAtBlock := progress (stages .Execution ).BlockNumber
125+ execToBlock := execAtBlock + unwindEvery
124126 if execToBlock > stopAt {
125127 execToBlock = stopAt + 1
126128 unwind = 0
@@ -146,6 +148,13 @@ func syncBySmallSteps(ctx context.Context, chaindata string) error {
146148 delete (expectedStorageChanges , blockN )
147149 }
148150
151+ if err := checkHistory (tx , dbutils .AccountChangeSetBucket , execAtBlock ); err != nil {
152+ return err
153+ }
154+ if err := checkHistory (tx , dbutils .StorageChangeSetBucket , execAtBlock ); err != nil {
155+ return err
156+ }
157+
149158 // Unwind all stages to `execStage - unwind` block
150159 if unwind == 0 {
151160 continue
@@ -249,3 +258,47 @@ func checkChangeSet(db ethdb.Getter, blockNum uint64, expectedAccountChanges []b
249258 }
250259 return nil
251260}
261+
262+ func checkHistory (db ethdb.Getter , changeSetBucket string , blockNum uint64 ) error {
263+ currentKey := dbutils .EncodeTimestamp (blockNum )
264+
265+ var walker func ([]byte ) changeset.Walker
266+ if dbutils .AccountChangeSetBucket == changeSetBucket {
267+ walker = func (cs []byte ) changeset.Walker {
268+ return changeset .AccountChangeSetBytes (cs )
269+ }
270+ }
271+
272+ if dbutils .StorageChangeSetBucket == changeSetBucket {
273+ walker = func (cs []byte ) changeset.Walker {
274+ return changeset .StorageChangeSetBytes (cs )
275+ }
276+ }
277+
278+ vv , ok := changeset .Mapper [changeSetBucket ]
279+ if ! ok {
280+ return errors .New ("unknown bucket type" )
281+ }
282+
283+ if err := db .Walk (changeSetBucket , currentKey , 0 , func (k , v []byte ) (b bool , e error ) {
284+ blockNum , _ := dbutils .DecodeTimestamp (k )
285+ if err := walker (v ).Walk (func (key , val []byte ) error {
286+ indexBytes , innerErr := db .GetIndexChunk (vv .IndexBucket , key , blockNum )
287+ if innerErr != nil {
288+ return innerErr
289+ }
290+
291+ index := dbutils .WrapHistoryIndex (indexBytes )
292+ if findVal , _ , ok := index .Search (blockNum ); ! ok {
293+ return fmt .Errorf ("%v,%v,%v" , blockNum , findVal , common .Bytes2Hex (key ))
294+ }
295+ return nil
296+ }); err != nil {
297+ return false , err
298+ }
299+ return true , nil
300+ }); err != nil {
301+ return err
302+ }
303+ return nil
304+ }
0 commit comments