-
Notifications
You must be signed in to change notification settings - Fork 176
/
file.go
46 lines (37 loc) · 1.13 KB
/
file.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
package utils
import (
"encoding/json"
"fmt"
"path/filepath"
"github.com/onflow/flow-go/engine/common/rpc/convert"
model "github.com/onflow/flow-go/model/bootstrap"
"github.com/onflow/flow-go/state/protocol/inmem"
io "github.com/onflow/flow-go/utils/io"
)
func ReadRootProtocolSnapshot(bootDir string) (*inmem.Snapshot, error) {
// default path root snapshot is written to
snapshotPath := filepath.Join(bootDir, model.PathRootProtocolStateSnapshot)
// read snapshot file bytes
bz, err := io.ReadFile(snapshotPath)
if err != nil {
return nil, fmt.Errorf("could not read snapshot: %w", err)
}
// convert bytes to snapshot struct
snapshot, err := convert.BytesToInmemSnapshot(bz)
if err != nil {
return nil, fmt.Errorf("could not convert bytes to snapshot: %w", err)
}
return snapshot, nil
}
func ReadData[T any](path string) (*T, error) {
bytes, err := io.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("could not read data from file: %w", err)
}
var encodable T
err = json.Unmarshal(bytes, &encodable)
if err != nil {
return nil, fmt.Errorf("could not unmarshal root block: %w", err)
}
return &encodable, nil
}