2023-05-02 14:19:22 +00:00
|
|
|
package spectest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-01-15 14:01:33 +00:00
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
|
2023-10-21 23:17:18 +00:00
|
|
|
clparams2 "github.com/ledgerwatch/erigon/cl/clparams"
|
|
|
|
"github.com/ledgerwatch/erigon/cl/cltypes"
|
|
|
|
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
|
|
|
|
"github.com/ledgerwatch/erigon/cl/utils"
|
2023-05-02 14:19:22 +00:00
|
|
|
|
2023-06-09 06:46:58 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
2023-05-11 11:54:20 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/types/ssz"
|
2023-05-02 14:19:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ReadMeta(root fs.FS, name string, obj any) error {
|
|
|
|
bts, err := fs.ReadFile(root, name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldnt read meta: %w", err)
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(bts, obj)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldnt parse meta: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadYml(root fs.FS, name string, obj any) error {
|
|
|
|
bts, err := fs.ReadFile(root, name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldnt read meta: %w", err)
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(bts, obj)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldnt parse meta: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-21 23:17:18 +00:00
|
|
|
func ReadSsz(root fs.FS, version clparams2.StateVersion, name string, obj ssz.Unmarshaler) error {
|
2023-05-02 14:19:22 +00:00
|
|
|
bts, err := fs.ReadFile(root, name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldnt read meta: %w", err)
|
|
|
|
}
|
2023-05-05 09:19:24 +00:00
|
|
|
return utils.DecodeSSZSnappy(obj, bts, int(version))
|
2023-05-02 14:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 23:17:18 +00:00
|
|
|
func ReadSszOld(root fs.FS, obj ssz.Unmarshaler, version clparams2.StateVersion, name string) error {
|
2023-05-02 14:19:22 +00:00
|
|
|
return ReadSsz(root, version, name, obj)
|
|
|
|
}
|
|
|
|
|
2023-10-21 23:17:18 +00:00
|
|
|
func ReadBeaconState(root fs.FS, version clparams2.StateVersion, name string) (*state.CachingBeaconState, error) {
|
2023-05-02 14:19:22 +00:00
|
|
|
sszSnappy, err := fs.ReadFile(root, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-21 23:17:18 +00:00
|
|
|
config := clparams2.MainnetBeaconConfig
|
2023-05-02 14:19:22 +00:00
|
|
|
testState := state.New(&config)
|
2023-05-05 09:19:24 +00:00
|
|
|
if err := utils.DecodeSSZSnappy(testState, sszSnappy, int(version)); err != nil {
|
2023-05-02 14:19:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return testState, nil
|
|
|
|
}
|
|
|
|
|
2023-10-21 23:17:18 +00:00
|
|
|
func ReadBlock(root fs.FS, version clparams2.StateVersion, index int) (*cltypes.SignedBeaconBlock, error) {
|
2023-05-02 14:19:22 +00:00
|
|
|
var blockBytes []byte
|
|
|
|
var err error
|
|
|
|
blockBytes, err = fs.ReadFile(root, fmt.Sprintf("blocks_%d.ssz_snappy", index))
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-21 23:17:18 +00:00
|
|
|
blk := cltypes.NewSignedBeaconBlock(&clparams2.MainnetBeaconConfig)
|
2023-05-05 09:19:24 +00:00
|
|
|
if err = utils.DecodeSSZSnappy(blk, blockBytes, int(version)); err != nil {
|
2023-05-02 14:19:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blk, nil
|
|
|
|
}
|
2024-01-15 14:01:33 +00:00
|
|
|
|
|
|
|
func ReadBlockByPath(root fs.FS, version clparams2.StateVersion, path string) (*cltypes.SignedBeaconBlock, error) {
|
|
|
|
var blockBytes []byte
|
|
|
|
var err error
|
|
|
|
blockBytes, err = fs.ReadFile(root, path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
blk := cltypes.NewSignedBeaconBlock(&clparams2.MainnetBeaconConfig)
|
|
|
|
if err = utils.DecodeSSZSnappy(blk, blockBytes, int(version)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blk, nil
|
|
|
|
}
|
|
|
|
|
2023-10-21 23:17:18 +00:00
|
|
|
func ReadAnchorBlock(root fs.FS, version clparams2.StateVersion, name string) (*cltypes.BeaconBlock, error) {
|
2023-05-02 14:19:22 +00:00
|
|
|
var blockBytes []byte
|
|
|
|
var err error
|
|
|
|
blockBytes, err = fs.ReadFile(root, name)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-21 23:17:18 +00:00
|
|
|
blk := cltypes.NewBeaconBlock(&clparams2.MainnetBeaconConfig)
|
2023-05-05 09:19:24 +00:00
|
|
|
if err = utils.DecodeSSZSnappy(blk, blockBytes, int(version)); err != nil {
|
2023-05-02 14:19:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadBlockSlot(root fs.FS, index int) (uint64, error) {
|
|
|
|
var blockBytes []byte
|
|
|
|
var err error
|
|
|
|
blockBytes, err = fs.ReadFile(root, fmt.Sprintf("blocks_%d.ssz_snappy", index))
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blockBytes, err = utils.DecompressSnappy(blockBytes)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return ssz.UnmarshalUint64SSZ(blockBytes[100:108]), nil
|
|
|
|
}
|
2023-10-21 23:17:18 +00:00
|
|
|
func ReadBlocks(root fs.FS, version clparams2.StateVersion) ([]*cltypes.SignedBeaconBlock, error) {
|
2023-05-02 14:19:22 +00:00
|
|
|
i := 0
|
|
|
|
blocks := []*cltypes.SignedBeaconBlock{}
|
|
|
|
var err error
|
|
|
|
for {
|
|
|
|
var blockBytes []byte
|
|
|
|
blockBytes, err = fs.ReadFile(root, fmt.Sprintf("blocks_%d.ssz_snappy", i))
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2023-10-21 23:17:18 +00:00
|
|
|
blk := cltypes.NewSignedBeaconBlock(&clparams2.MainnetBeaconConfig)
|
2023-05-05 09:19:24 +00:00
|
|
|
if err = utils.DecodeSSZSnappy(blk, blockBytes, int(version)); err != nil {
|
2023-05-02 14:19:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
blocks = append(blocks, blk)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return blocks, err
|
|
|
|
}
|