-
Notifications
You must be signed in to change notification settings - Fork 11
/
genesis.go
287 lines (248 loc) · 9.52 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package types
import (
"encoding/json"
"errors"
"fmt"
"os"
"time"
"github.com/dashpay/dashd-go/btcjson"
"github.com/dashpay/tenderdash/crypto"
"github.com/dashpay/tenderdash/crypto/bls12381"
"github.com/dashpay/tenderdash/internal/jsontypes"
tmbytes "github.com/dashpay/tenderdash/libs/bytes"
tmtime "github.com/dashpay/tenderdash/libs/time"
tmproto "github.com/dashpay/tenderdash/proto/tendermint/types"
)
const (
// MaxChainIDLen is a maximum length of the chain ID.
MaxChainIDLen = 50
)
//------------------------------------------------------------
// core types for a genesis definition
// NOTE: any changes to the genesis definition should
// be reflected in the documentation:
// docs/tendermint-core/using-tendermint.md
// GenesisValidator is an initial validator.
type GenesisValidator struct {
PubKey crypto.PubKey
Power int64
Name string
ProTxHash crypto.ProTxHash
}
type genesisValidatorJSON struct {
PubKey json.RawMessage `json:"pub_key"`
Power int64 `json:"power"`
Name string `json:"name"`
ProTxHash crypto.ProTxHash `json:"pro_tx_hash"`
}
func (g GenesisValidator) MarshalJSON() ([]byte, error) {
pk, err := jsontypes.Marshal(g.PubKey)
if err != nil {
return nil, err
}
return json.Marshal(genesisValidatorJSON{
ProTxHash: g.ProTxHash, PubKey: pk, Power: g.Power, Name: g.Name,
})
}
func (g *GenesisValidator) UnmarshalJSON(data []byte) error {
var gv genesisValidatorJSON
if err := json.Unmarshal(data, &gv); err != nil {
return fmt.Errorf("unmarshal validator: %w", err)
}
if err := jsontypes.Unmarshal(gv.PubKey, &g.PubKey); err != nil {
return fmt.Errorf("unmarshal validator %s key: %w", gv.ProTxHash.ShortString(), err)
}
g.Power = gv.Power
g.Name = gv.Name
g.ProTxHash = gv.ProTxHash
return nil
}
// GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set.
type GenesisDoc struct {
GenesisTime time.Time
ChainID string
InitialHeight int64
ConsensusParams *ConsensusParams
Validators []GenesisValidator
AppHash tmbytes.HexBytes
AppState []byte
// dash fields
InitialCoreChainLockedHeight uint32 `json:"initial_core_chain_locked_height"`
InitialProposalCoreChainLock *tmproto.CoreChainLock `json:"initial_proposal_core_chain_lock"`
ThresholdPublicKey crypto.PubKey `json:"validator_quorum_threshold_public_key"`
QuorumType btcjson.LLMQType `json:"validator_quorum_type"`
QuorumHash crypto.QuorumHash `json:"validator_quorum_hash"`
}
type genesisDocJSON struct {
GenesisTime time.Time `json:"genesis_time,omitempty"`
ChainID string `json:"chain_id"`
InitialHeight int64 `json:"initial_height,string,omitempty"`
ConsensusParams *ConsensusParams `json:"consensus_params,omitempty"`
Validators []GenesisValidator `json:"validators,omitempty"`
AppHash tmbytes.HexBytes `json:"app_hash,omitempty"`
AppState []byte `json:"app_state,omitempty"`
// dash fields
InitialCoreChainLockedHeight uint32 `json:"initial_core_chain_locked_height,omitempty"`
InitialProposalCoreChainLock *tmproto.CoreChainLock `json:"initial_proposal_core_chain_lock,omitempty"`
ThresholdPublicKey json.RawMessage `json:"validator_quorum_threshold_public_key,omitempty"`
QuorumType btcjson.LLMQType `json:"validator_quorum_type,omitempty"`
QuorumHash crypto.QuorumHash `json:"validator_quorum_hash,omitempty"`
// Deprecated fields, to be removed
DeprecatedThresholdPublicKey json.RawMessage `json:"threshold_public_key,omitempty"`
DeprecatedQuorumType btcjson.LLMQType `json:"quorum_type,omitempty"`
DeprecatedQuorumHash crypto.QuorumHash `json:"quorum_hash,omitempty"`
}
func (genDoc GenesisDoc) MarshalJSON() ([]byte, error) {
tpk, err := jsontypes.Marshal(genDoc.ThresholdPublicKey)
if err != nil {
return nil, err
}
return json.Marshal(genesisDocJSON{
GenesisTime: genDoc.GenesisTime,
ChainID: genDoc.ChainID,
InitialHeight: genDoc.InitialHeight,
ConsensusParams: genDoc.ConsensusParams,
Validators: genDoc.Validators,
AppHash: genDoc.AppHash,
AppState: genDoc.AppState,
InitialCoreChainLockedHeight: genDoc.InitialCoreChainLockedHeight,
InitialProposalCoreChainLock: genDoc.InitialProposalCoreChainLock,
ThresholdPublicKey: tpk,
QuorumType: genDoc.QuorumType,
QuorumHash: genDoc.QuorumHash,
})
}
func (genDoc *GenesisDoc) UnmarshalJSON(data []byte) error {
var g genesisDocJSON
if err := json.Unmarshal(data, &g); err != nil {
return err
}
if err := jsontypes.Unmarshal(g.ThresholdPublicKey, &genDoc.ThresholdPublicKey); err != nil {
return err
}
if len(g.DeprecatedQuorumHash) != 0 {
return fmt.Errorf("genesis.json: replace quorum_hash with validator_quorum_hash")
}
if g.DeprecatedQuorumType != 0 {
return fmt.Errorf("genesis.json: replace quorum_type with validator_quorum_type")
}
if len(g.DeprecatedThresholdPublicKey) != 0 {
return fmt.Errorf("genesis.json: replace threshold_public_key with validator_quorum_threshold_public_key")
}
genDoc.GenesisTime = g.GenesisTime
genDoc.ChainID = g.ChainID
genDoc.InitialHeight = g.InitialHeight
genDoc.ConsensusParams = g.ConsensusParams
genDoc.Validators = g.Validators
genDoc.AppHash = g.AppHash
genDoc.AppState = g.AppState
genDoc.InitialCoreChainLockedHeight = g.InitialCoreChainLockedHeight
genDoc.InitialProposalCoreChainLock = g.InitialProposalCoreChainLock
genDoc.QuorumType = g.QuorumType
genDoc.QuorumHash = g.QuorumHash
return nil
}
// SaveAs is a utility method for saving GenensisDoc as a JSON file.
func (genDoc *GenesisDoc) SaveAs(file string) error {
genDocBytes, err := json.MarshalIndent(genDoc, "", " ")
if err != nil {
return err
}
return os.WriteFile(file, genDocBytes, 0644) //nolint:gosec
}
// ValidatorHash returns the hash of the validator set contained in the GenesisDoc
func (genDoc *GenesisDoc) ValidatorHash() []byte {
if genDoc.QuorumHash == nil {
panic("quorum hash should not be nil")
}
return genDoc.QuorumHash
}
// ValidateAndComplete checks that all necessary fields are present
// and fills in defaults for optional fields left empty
func (genDoc *GenesisDoc) ValidateAndComplete() error {
if genDoc.ChainID == "" {
return errors.New("genesis doc must include non-empty chain_id")
}
if len(genDoc.ChainID) > MaxChainIDLen {
return fmt.Errorf("chain_id in genesis doc is too long (max: %d)", MaxChainIDLen)
}
if genDoc.InitialHeight < 0 {
return fmt.Errorf("initial_height cannot be negative (got %v)", genDoc.InitialHeight)
}
if genDoc.InitialHeight == 0 {
genDoc.InitialHeight = 1
}
// TODO: user LLMQType.Validate()
if genDoc.QuorumType == 0 {
return fmt.Errorf("validator_quorum_type must be provided")
}
if genDoc.InitialProposalCoreChainLock != nil &&
genDoc.InitialProposalCoreChainLock.CoreBlockHeight <= genDoc.InitialCoreChainLockedHeight {
return fmt.Errorf("if set the initial proposal core chain locked block height %d"+
" must be superior to the initial core chain locked height %d",
genDoc.InitialProposalCoreChainLock.CoreBlockHeight, genDoc.InitialCoreChainLockedHeight)
}
if genDoc.ConsensusParams == nil {
genDoc.ConsensusParams = DefaultConsensusParams()
}
genDoc.ConsensusParams.Complete()
if err := genDoc.ConsensusParams.ValidateConsensusParams(); err != nil {
return err
}
lenVals := len(genDoc.Validators)
for _, v := range genDoc.Validators {
if v.Power == 0 {
return fmt.Errorf("the genesis file cannot contain validators with no voting power: %v", v)
}
if len(v.ProTxHash) != crypto.ProTxHashSize {
return fmt.Errorf("validators must all contain a pro_tx_hash of size 32")
}
}
if lenVals > 0 && genDoc.ThresholdPublicKey == nil {
return fmt.Errorf("the threshold public key must be set if there are validators (%d Validator(s))",
len(genDoc.Validators))
}
if lenVals > 0 && len(genDoc.ThresholdPublicKey.Bytes()) != bls12381.PubKeySize {
return fmt.Errorf("the threshold public key must be 48 bytes for BLS")
}
if lenVals > 0 && len(genDoc.QuorumHash.Bytes()) != crypto.QuorumHashSize {
return fmt.Errorf("the quorum hash must be base64-encoded and %d bytes long, is %d bytes (%d Validator(s))",
crypto.QuorumHashSize,
len(genDoc.QuorumHash.Bytes()),
len(genDoc.Validators))
}
if genDoc.QuorumType == 0 {
return fmt.Errorf("the quorum type must not be 0 (%d Validator(s))", len(genDoc.Validators))
}
if genDoc.GenesisTime.IsZero() {
genDoc.GenesisTime = tmtime.Now()
}
return nil
}
//------------------------------------------------------------
// Make genesis state from file
// GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc.
func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
genDoc := GenesisDoc{}
err := json.Unmarshal(jsonBlob, &genDoc)
if err != nil {
return nil, err
}
if err := genDoc.ValidateAndComplete(); err != nil {
return nil, err
}
return &genDoc, err
}
// GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc.
func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) {
jsonBlob, err := os.ReadFile(genDocFile)
if err != nil {
return nil, fmt.Errorf("couldn't read GenesisDoc file: %w", err)
}
genDoc, err := GenesisDocFromJSON(jsonBlob)
if err != nil {
fmt.Printf("gendoc %v\n", genDoc)
return nil, fmt.Errorf("error reading GenesisDoc at %s: %w", genDocFile, err)
}
return genDoc, nil
}