-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathgenesis.go
44 lines (35 loc) · 1.11 KB
/
genesis.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
package utils
import (
"fmt"
tmjson "github.com/cometbft/cometbft/libs/json"
tmos "github.com/cometbft/cometbft/libs/os"
tmtypes "github.com/cometbft/cometbft/types"
"github.com/forbole/juno/v5/node"
"github.com/forbole/juno/v5/types/config"
)
// ReadGenesis reads the genesis data based on the given config
func ReadGenesis(config config.Config, node node.Node) (*tmtypes.GenesisDoc, error) {
if config.Parser.GenesisFilePath != "" {
return readGenesisFromFilePath(config.Parser.GenesisFilePath)
}
return readGenesisFromNode(node)
}
func readGenesisFromFilePath(path string) (*tmtypes.GenesisDoc, error) {
bz, err := tmos.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read genesis file: %s", err)
}
var genDoc tmtypes.GenesisDoc
err = tmjson.Unmarshal(bz, &genDoc)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal genesis doc: %s", err)
}
return &genDoc, nil
}
func readGenesisFromNode(node node.Node) (*tmtypes.GenesisDoc, error) {
response, err := node.Genesis()
if err != nil {
return nil, fmt.Errorf("failed to get genesis: %s", err)
}
return response.Genesis, nil
}