-
Notifications
You must be signed in to change notification settings - Fork 181
/
utils.go
86 lines (68 loc) · 2.45 KB
/
utils.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
package genutil
import (
"encoding/json"
"path/filepath"
"time"
cfg "github.com/okex/exchain/libs/tendermint/config"
"github.com/okex/exchain/libs/tendermint/crypto"
tmos "github.com/okex/exchain/libs/tendermint/libs/os"
"github.com/okex/exchain/libs/tendermint/p2p"
"github.com/okex/exchain/libs/tendermint/privval"
tmtypes "github.com/okex/exchain/libs/tendermint/types"
)
// ExportGenesisFile creates and writes the genesis configuration to disk. An
// error is returned if building or writing the configuration to file fails.
func ExportGenesisFile(genDoc *tmtypes.GenesisDoc, genFile string) error {
if err := genDoc.ValidateAndComplete(); err != nil {
return err
}
return genDoc.SaveAs(genFile)
}
// ExportGenesisFileWithTime creates and writes the genesis configuration to disk.
// An error is returned if building or writing the configuration to file fails.
func ExportGenesisFileWithTime(
genFile, chainID string, validators []tmtypes.GenesisValidator,
appState json.RawMessage, genTime time.Time,
) error {
genDoc := tmtypes.GenesisDoc{
GenesisTime: genTime,
ChainID: chainID,
Validators: validators,
AppState: appState,
}
if err := genDoc.ValidateAndComplete(); err != nil {
return err
}
return genDoc.SaveAs(genFile)
}
func InitializeNodeValidatorFilesByIndex(config *cfg.Config, index int) (nodeID string, valPubKey crypto.PubKey, err error) {
nodeKey, err := p2p.LoadOrGenNodeKeyByIndex(config.NodeKeyFile(), index)
if err != nil {
return "", nil, err
}
return initValidatorFiles(config, nodeKey)
}
// InitializeNodeValidatorFiles creates private validator and p2p configuration files.
func InitializeNodeValidatorFiles(config *cfg.Config) (nodeID string, valPubKey crypto.PubKey, err error) {
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
if err != nil {
return "", nil, err
}
return initValidatorFiles(config, nodeKey)
}
func initValidatorFiles(config *cfg.Config, nodeKey *p2p.NodeKey) (nodeID string, valPubKey crypto.PubKey, err error) {
nodeID = string(nodeKey.ID())
pvKeyFile := config.PrivValidatorKeyFile()
if err := tmos.EnsureDir(filepath.Dir(pvKeyFile), 0777); err != nil {
return "", nil, err
}
pvStateFile := config.PrivValidatorStateFile()
if err := tmos.EnsureDir(filepath.Dir(pvStateFile), 0777); err != nil {
return "", nil, err
}
valPubKey, err = privval.LoadOrGenFilePV(pvKeyFile, pvStateFile).GetPubKey()
if err != nil {
return "", nil, err
}
return nodeID, valPubKey, nil
}