-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathgenesis.go
235 lines (199 loc) · 6.41 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package types
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"time"
cmtjson "github.com/cometbft/cometbft/libs/json"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
cmttypes "github.com/cometbft/cometbft/types"
cmttime "github.com/cometbft/cometbft/types/time"
"github.com/cosmos/cosmos-sdk/version"
)
const (
// MaxChainIDLen is the maximum length of a chain ID.
MaxChainIDLen = cmttypes.MaxChainIDLen
)
// AppGenesis defines the app's genesis.
type AppGenesis struct {
AppName string `json:"app_name"`
AppVersion string `json:"app_version"`
GenesisTime time.Time `json:"genesis_time"`
ChainID string `json:"chain_id"`
InitialHeight int64 `json:"initial_height"`
AppHash []byte `json:"app_hash"`
AppState json.RawMessage `json:"app_state,omitempty"`
Consensus *ConsensusGenesis `json:"consensus,omitempty"`
}
// NewAppGenesisWithVersion returns a new AppGenesis with the app name and app version already.
func NewAppGenesisWithVersion(chainID string, appState json.RawMessage) *AppGenesis {
return &AppGenesis{
AppName: version.AppName,
AppVersion: version.Version,
ChainID: chainID,
AppState: appState,
Consensus: &ConsensusGenesis{
Validators: nil,
},
}
}
// ValidateAndComplete performs validation and completes the AppGenesis.
func (ag *AppGenesis) ValidateAndComplete() error {
if ag.ChainID == "" {
return errors.New("genesis doc must include non-empty chain_id")
}
if len(ag.ChainID) > MaxChainIDLen {
return fmt.Errorf("chain_id in genesis doc is too long (max: %d)", MaxChainIDLen)
}
if ag.InitialHeight < 0 {
return fmt.Errorf("initial_height cannot be negative (got %v)", ag.InitialHeight)
}
if ag.InitialHeight == 0 {
ag.InitialHeight = 1
}
if ag.GenesisTime.IsZero() {
ag.GenesisTime = cmttime.Now()
}
if err := ag.Consensus.ValidateAndComplete(); err != nil {
return err
}
return nil
}
// SaveAs is a utility method for saving AppGenesis as a JSON file.
func (ag *AppGenesis) SaveAs(file string) error {
appGenesisBytes, err := json.MarshalIndent(ag, "", " ")
if err != nil {
return err
}
return os.WriteFile(file, appGenesisBytes, 0o600)
}
// AppGenesisFromReader reads the AppGenesis from the reader.
func AppGenesisFromReader(reader io.Reader) (*AppGenesis, error) {
jsonBlob, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
var appGenesis AppGenesis
if err := json.Unmarshal(jsonBlob, &appGenesis); err != nil {
// fallback to CometBFT genesis
var ctmGenesis cmttypes.GenesisDoc
if err2 := cmtjson.Unmarshal(jsonBlob, &ctmGenesis); err2 != nil {
return nil, fmt.Errorf("error unmarshalling AppGenesis: %w\n failed fallback to CometBFT GenDoc: %w", err, err2)
}
appGenesis = AppGenesis{
AppName: version.AppName,
// AppVersion is not filled as we do not know it from a CometBFT genesis
GenesisTime: ctmGenesis.GenesisTime,
ChainID: ctmGenesis.ChainID,
InitialHeight: ctmGenesis.InitialHeight,
AppHash: ctmGenesis.AppHash,
AppState: ctmGenesis.AppState,
Consensus: &ConsensusGenesis{
Validators: ctmGenesis.Validators,
Params: ctmGenesis.ConsensusParams,
},
}
}
return &appGenesis, nil
}
// AppGenesisFromFile reads the AppGenesis from the provided file.
func AppGenesisFromFile(genFile string) (*AppGenesis, error) {
file, err := os.Open(filepath.Clean(genFile))
if err != nil {
return nil, err
}
appGenesis, err := AppGenesisFromReader(bufio.NewReader(file))
if err != nil {
return nil, fmt.Errorf("failed to read genesis from file %s: %w", genFile, err)
}
if err := file.Close(); err != nil {
return nil, err
}
return appGenesis, nil
}
// --------------------------
// CometBFT Genesis Handling
// --------------------------
// ToGenesisDoc converts the AppGenesis to a CometBFT GenesisDoc.
func (ag *AppGenesis) ToGenesisDoc() (*cmttypes.GenesisDoc, error) {
return &cmttypes.GenesisDoc{
GenesisTime: ag.GenesisTime,
ChainID: ag.ChainID,
InitialHeight: ag.InitialHeight,
AppHash: ag.AppHash,
AppState: ag.AppState,
Validators: ag.Consensus.Validators,
ConsensusParams: ag.Consensus.Params,
}, nil
}
// ConsensusGenesis defines the consensus layer's genesis.
// TODO(@julienrbrt) eventually abstract from CometBFT types
type ConsensusGenesis struct {
Validators []cmttypes.GenesisValidator `json:"validators,omitempty"`
Params *cmttypes.ConsensusParams `json:"params,omitempty"`
}
// NewConsensusGenesis returns a ConsensusGenesis with given values.
// It takes a proto consensus params so it can called from server export command.
func NewConsensusGenesis(params cmtproto.ConsensusParams, validators []cmttypes.GenesisValidator) *ConsensusGenesis {
return &ConsensusGenesis{
Params: &cmttypes.ConsensusParams{
Block: cmttypes.BlockParams{
MaxBytes: params.Block.MaxBytes,
MaxGas: params.Block.MaxGas,
},
Evidence: cmttypes.EvidenceParams{
MaxAgeNumBlocks: params.Evidence.MaxAgeNumBlocks,
MaxAgeDuration: params.Evidence.MaxAgeDuration,
MaxBytes: params.Evidence.MaxBytes,
},
Validator: cmttypes.ValidatorParams{
PubKeyTypes: params.Validator.PubKeyTypes,
},
},
Validators: validators,
}
}
func (cs *ConsensusGenesis) MarshalJSON() ([]byte, error) {
type Alias ConsensusGenesis
return cmtjson.Marshal(&Alias{
Validators: cs.Validators,
Params: cs.Params,
})
}
func (cs *ConsensusGenesis) UnmarshalJSON(b []byte) error {
type Alias ConsensusGenesis
result := Alias{}
if err := cmtjson.Unmarshal(b, &result); err != nil {
return err
}
cs.Params = result.Params
cs.Validators = result.Validators
return nil
}
func (cs *ConsensusGenesis) ValidateAndComplete() error {
if cs == nil {
return fmt.Errorf("consensus genesis cannot be nil")
}
if cs.Params == nil {
cs.Params = cmttypes.DefaultConsensusParams()
} else if err := cs.Params.ValidateBasic(); err != nil {
return err
}
for i, v := range cs.Validators {
if v.Power == 0 {
return fmt.Errorf("the genesis file cannot contain validators with no voting power: %v", v)
}
if len(v.Address) > 0 && !bytes.Equal(v.PubKey.Address(), v.Address) {
return fmt.Errorf("incorrect address for validator %v in the genesis file, should be %v", v, v.PubKey.Address())
}
if len(v.Address) == 0 {
cs.Validators[i].Address = v.PubKey.Address()
}
}
return nil
}