2020-10-13 12:56:16 +00:00
|
|
|
package verify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-11-28 14:26:28 +00:00
|
|
|
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/rlp"
|
2021-07-29 10:23:23 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2020-10-13 12:56:16 +00:00
|
|
|
)
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
func HeadersSnapshot(logger log.Logger, snapshotPath string) error {
|
2021-07-28 03:43:51 +00:00
|
|
|
snKV := mdbx.NewMDBX(logger).Path(snapshotPath).Readonly().WithTablessCfg(func(defaultBuckets kv.TableCfg) kv.TableCfg {
|
2021-07-28 02:47:38 +00:00
|
|
|
return kv.TableCfg{
|
2021-07-28 03:43:51 +00:00
|
|
|
kv.Headers: kv.TableCfgItem{},
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
}).MustOpen()
|
|
|
|
var prevHeader *types.Header
|
2021-07-28 02:47:38 +00:00
|
|
|
err := snKV.View(context.Background(), func(tx kv.Tx) error {
|
|
|
|
c, err := tx.Cursor(kv.Headers)
|
2021-04-02 06:36:49 +00:00
|
|
|
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
|
|
|
|
}
|