2015-11-01 11:34:08 -08:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2017-03-21 14:46:15 -07:00
|
|
|
"encoding/json"
|
2017-06-26 13:16:54 -07:00
|
|
|
"io/ioutil"
|
2015-11-01 11:34:08 -08:00
|
|
|
"time"
|
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
"github.com/tendermint/go-crypto"
|
2017-04-27 16:01:18 -07:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
2015-11-01 11:34:08 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
// core types for a genesis definition
|
|
|
|
|
2017-06-26 14:20:27 -07:00
|
|
|
// GenesisValidator is an initial validator.
|
2015-11-01 11:34:08 -08:00
|
|
|
type GenesisValidator struct {
|
2015-12-21 14:48:44 -08:00
|
|
|
PubKey crypto.PubKey `json:"pub_key"`
|
2017-09-21 11:37:34 -07:00
|
|
|
Power int64 `json:"power"`
|
2015-12-21 14:48:44 -08:00
|
|
|
Name string `json:"name"`
|
2015-11-01 11:34:08 -08:00
|
|
|
}
|
|
|
|
|
2017-06-26 14:20:27 -07:00
|
|
|
// GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set.
|
2015-11-01 11:34:08 -08:00
|
|
|
type GenesisDoc struct {
|
2017-09-12 12:20:19 -07:00
|
|
|
GenesisTime time.Time `json:"genesis_time"`
|
|
|
|
ChainID string `json:"chain_id"`
|
2017-09-21 12:22:58 -07:00
|
|
|
ConsensusParams *ConsensusParams `json:"consensus_params,omitempty"`
|
2017-09-12 12:20:19 -07:00
|
|
|
Validators []GenesisValidator `json:"validators"`
|
2018-02-03 00:42:59 -08:00
|
|
|
AppHash cmn.HexBytes `json:"app_hash"`
|
2018-02-08 12:30:43 -08:00
|
|
|
AppState json.RawMessage `json:"app_state,omitempty"`
|
2015-11-01 11:34:08 -08:00
|
|
|
}
|
|
|
|
|
2017-06-26 14:20:27 -07:00
|
|
|
// SaveAs is a utility method for saving GenensisDoc as a JSON file.
|
2015-12-03 09:51:10 -08:00
|
|
|
func (genDoc *GenesisDoc) SaveAs(file string) error {
|
2018-03-30 15:18:43 -07:00
|
|
|
genDocBytes, err := cdc.MarshalJSON(genDoc)
|
2017-03-21 14:46:15 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-27 16:01:18 -07:00
|
|
|
return cmn.WriteFile(file, genDocBytes, 0644)
|
2015-12-03 09:51:10 -08:00
|
|
|
}
|
|
|
|
|
2017-06-26 14:20:27 -07:00
|
|
|
// ValidatorHash returns the hash of the validator set contained in the GenesisDoc
|
2017-06-26 08:06:49 -07:00
|
|
|
func (genDoc *GenesisDoc) ValidatorHash() []byte {
|
|
|
|
vals := make([]*Validator, len(genDoc.Validators))
|
|
|
|
for i, v := range genDoc.Validators {
|
2017-09-21 11:37:34 -07:00
|
|
|
vals[i] = NewValidator(v.PubKey, v.Power)
|
2017-06-26 08:06:49 -07:00
|
|
|
}
|
|
|
|
vset := NewValidatorSet(vals)
|
|
|
|
return vset.Hash()
|
|
|
|
}
|
|
|
|
|
2017-09-11 13:48:41 -07:00
|
|
|
// ValidateAndComplete checks that all necessary fields are present
|
|
|
|
// and fills in defaults for optional fields left empty
|
|
|
|
func (genDoc *GenesisDoc) ValidateAndComplete() error {
|
2017-09-11 13:28:00 -07:00
|
|
|
|
|
|
|
if genDoc.ChainID == "" {
|
2018-03-30 15:18:43 -07:00
|
|
|
return cmn.NewError("Genesis doc must include non-empty chain_id")
|
2017-09-11 13:28:00 -07:00
|
|
|
}
|
2017-09-11 13:48:41 -07:00
|
|
|
|
2017-09-21 12:22:58 -07:00
|
|
|
if genDoc.ConsensusParams == nil {
|
2017-09-12 12:20:19 -07:00
|
|
|
genDoc.ConsensusParams = DefaultConsensusParams()
|
2017-09-11 13:28:00 -07:00
|
|
|
} else {
|
|
|
|
if err := genDoc.ConsensusParams.Validate(); err != nil {
|
2017-09-11 13:48:41 -07:00
|
|
|
return err
|
2017-09-11 13:28:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-11 13:48:41 -07:00
|
|
|
if len(genDoc.Validators) == 0 {
|
2018-03-30 15:18:43 -07:00
|
|
|
return cmn.NewError("The genesis file must have at least one validator")
|
2017-09-11 13:48:41 -07:00
|
|
|
}
|
|
|
|
|
2017-09-21 11:37:34 -07:00
|
|
|
for _, v := range genDoc.Validators {
|
|
|
|
if v.Power == 0 {
|
2018-03-30 15:18:43 -07:00
|
|
|
return cmn.NewError("The genesis file cannot contain validators with no voting power: %v", v)
|
2017-09-21 11:37:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-11 13:48:41 -07:00
|
|
|
if genDoc.GenesisTime.IsZero() {
|
|
|
|
genDoc.GenesisTime = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
// Make genesis state from file
|
|
|
|
|
|
|
|
// GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc.
|
|
|
|
func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
|
|
|
|
genDoc := GenesisDoc{}
|
2018-03-30 15:18:43 -07:00
|
|
|
err := cdc.UnmarshalJSON(jsonBlob, &genDoc)
|
2017-09-11 13:48:41 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := genDoc.ValidateAndComplete(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-21 14:46:15 -07:00
|
|
|
return &genDoc, err
|
2015-11-01 11:34:08 -08:00
|
|
|
}
|
2017-06-26 13:16:54 -07:00
|
|
|
|
|
|
|
// GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc.
|
|
|
|
func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) {
|
|
|
|
jsonBlob, err := ioutil.ReadFile(genDocFile)
|
|
|
|
if err != nil {
|
2018-03-30 15:18:43 -07:00
|
|
|
return nil, cmn.ErrorWrap(err, "Couldn't read GenesisDoc file")
|
2017-06-26 13:16:54 -07:00
|
|
|
}
|
|
|
|
genDoc, err := GenesisDocFromJSON(jsonBlob)
|
|
|
|
if err != nil {
|
2018-03-30 15:18:43 -07:00
|
|
|
return nil, cmn.ErrorWrap(err, cmn.Fmt("Error reading GenesisDoc at %v", genDocFile))
|
2017-06-26 13:16:54 -07:00
|
|
|
}
|
|
|
|
return genDoc, nil
|
|
|
|
}
|