2020-10-13 12:56:16 +00:00
|
|
|
package verify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-11-28 14:26:28 +00:00
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/ethdb"
|
2021-06-19 08:21:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/ethdb/kv"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/log"
|
|
|
|
"github.com/ledgerwatch/erigon/rlp"
|
2020-10-13 12:56:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func HeadersSnapshot(snapshotPath string) error {
|
2021-06-19 08:21:53 +00:00
|
|
|
snKV := kv.NewMDBX().Path(snapshotPath).Readonly().WithBucketsConfig(func(defaultBuckets dbutils.BucketsCfg) dbutils.BucketsCfg {
|
2020-10-13 12:56:16 +00:00
|
|
|
return dbutils.BucketsCfg{
|
2021-07-06 22:33:26 +00:00
|
|
|
dbutils.HeadersBucket: dbutils.BucketConfigItem{},
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
}).MustOpen()
|
|
|
|
var prevHeader *types.Header
|
|
|
|
err := snKV.View(context.Background(), func(tx ethdb.Tx) error {
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := tx.Cursor(dbutils.HeadersBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-13 12:56:16 +00:00
|
|
|
k, v, innerErr := c.First()
|
|
|
|
for {
|
|
|
|
if len(k) == 0 && len(v) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if innerErr != nil {
|
|
|
|
return innerErr
|
|
|
|
}
|
|
|
|
|
|
|
|
header := new(types.Header)
|
|
|
|
innerErr := rlp.DecodeBytes(v, header)
|
|
|
|
if innerErr != nil {
|
|
|
|
return innerErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if prevHeader != nil {
|
|
|
|
if prevHeader.Number.Uint64()+1 != header.Number.Uint64() {
|
|
|
|
log.Error("invalid header number", "p", prevHeader.Number.Uint64(), "c", header.Number.Uint64())
|
|
|
|
return errors.New("invalid header number")
|
|
|
|
}
|
2021-03-05 20:34:23 +00:00
|
|
|
if prevHeader.Hash() != header.ParentHash {
|
|
|
|
log.Error("invalid parent hash", "p", prevHeader.Hash(), "c", header.ParentHash)
|
2020-10-13 12:56:16 +00:00
|
|
|
return errors.New("invalid parent hash")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
k, v, innerErr = c.Next() //nolint
|
|
|
|
prevHeader = header
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|