forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathutil.go
159 lines (142 loc) · 4 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package spectest
import (
"fmt"
"io/fs"
"os"
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"
"gopkg.in/yaml.v3"
"github.com/ledgerwatch/erigon-lib/types/ssz"
)
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
}
func ReadSsz(root fs.FS, version clparams2.StateVersion, name string, obj ssz.Unmarshaler) error {
bts, err := fs.ReadFile(root, name)
if err != nil {
return fmt.Errorf("couldnt read meta: %w", err)
}
return utils.DecodeSSZSnappy(obj, bts, int(version))
}
func ReadSszOld(root fs.FS, obj ssz.Unmarshaler, version clparams2.StateVersion, name string) error {
return ReadSsz(root, version, name, obj)
}
func ReadBeaconState(root fs.FS, version clparams2.StateVersion, name string) (*state.CachingBeaconState, error) {
sszSnappy, err := fs.ReadFile(root, name)
if err != nil {
return nil, err
}
config := clparams2.MainnetBeaconConfig
testState := state.New(&config)
if err := utils.DecodeSSZSnappy(testState, sszSnappy, int(version)); err != nil {
return nil, err
}
return testState, nil
}
func ReadBlock(root fs.FS, version clparams2.StateVersion, index int) (*cltypes.SignedBeaconBlock, error) {
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
}
blk := cltypes.NewSignedBeaconBlock(&clparams2.MainnetBeaconConfig)
if err = utils.DecodeSSZSnappy(blk, blockBytes, int(version)); err != nil {
return nil, err
}
return blk, nil
}
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
}
func ReadAnchorBlock(root fs.FS, version clparams2.StateVersion, name string) (*cltypes.BeaconBlock, error) {
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
}
blk := cltypes.NewBeaconBlock(&clparams2.MainnetBeaconConfig)
if err = utils.DecodeSSZSnappy(blk, blockBytes, int(version)); err != nil {
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
}
func ReadBlocks(root fs.FS, version clparams2.StateVersion) ([]*cltypes.SignedBeaconBlock, error) {
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
}
blk := cltypes.NewSignedBeaconBlock(&clparams2.MainnetBeaconConfig)
if err = utils.DecodeSSZSnappy(blk, blockBytes, int(version)); err != nil {
return nil, err
}
blocks = append(blocks, blk)
i++
}
if os.IsNotExist(err) {
err = nil
}
return blocks, err
}