2015-11-01 11:34:08 -08:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2017-03-21 22:46:15 +01:00
|
|
|
"encoding/json"
|
2017-06-26 16:16:54 -04:00
|
|
|
"io/ioutil"
|
2015-11-01 11:34:08 -08:00
|
|
|
"time"
|
|
|
|
|
2017-06-26 16:16:54 -04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2015-11-01 11:34:08 -08:00
|
|
|
"github.com/tendermint/go-crypto"
|
2017-04-27 19:01:18 -04:00
|
|
|
"github.com/tendermint/go-wire/data"
|
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
2015-11-01 11:34:08 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
// core types for a genesis definition
|
|
|
|
|
2017-06-26 17:20:27 -04: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"`
|
|
|
|
Amount int64 `json:"amount"`
|
|
|
|
Name string `json:"name"`
|
2015-11-01 11:34:08 -08:00
|
|
|
}
|
|
|
|
|
2017-06-26 17:20:27 -04: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 {
|
|
|
|
GenesisTime time.Time `json:"genesis_time"`
|
|
|
|
ChainID string `json:"chain_id"`
|
|
|
|
Validators []GenesisValidator `json:"validators"`
|
2017-04-27 19:01:18 -04:00
|
|
|
AppHash data.Bytes `json:"app_hash"`
|
2015-11-01 11:34:08 -08:00
|
|
|
}
|
|
|
|
|
2017-06-26 17:20:27 -04: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 {
|
2017-03-21 22:46:15 +01:00
|
|
|
genDocBytes, err := json.Marshal(genDoc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-27 19:01:18 -04:00
|
|
|
return cmn.WriteFile(file, genDocBytes, 0644)
|
2015-12-03 09:51:10 -08:00
|
|
|
}
|
|
|
|
|
2017-06-26 17:20:27 -04:00
|
|
|
// ValidatorHash returns the hash of the validator set contained in the GenesisDoc
|
2017-06-26 17:06:49 +02:00
|
|
|
func (genDoc *GenesisDoc) ValidatorHash() []byte {
|
|
|
|
vals := make([]*Validator, len(genDoc.Validators))
|
|
|
|
for i, v := range genDoc.Validators {
|
|
|
|
vals[i] = NewValidator(v.PubKey, v.Amount)
|
|
|
|
}
|
|
|
|
vset := NewValidatorSet(vals)
|
|
|
|
return vset.Hash()
|
|
|
|
}
|
|
|
|
|
2015-11-01 11:34:08 -08:00
|
|
|
//------------------------------------------------------------
|
|
|
|
// Make genesis state from file
|
|
|
|
|
2017-06-26 16:16:54 -04:00
|
|
|
// GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc.
|
2017-03-21 22:46:15 +01:00
|
|
|
func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
|
|
|
|
genDoc := GenesisDoc{}
|
|
|
|
err := json.Unmarshal(jsonBlob, &genDoc)
|
|
|
|
return &genDoc, err
|
2015-11-01 11:34:08 -08:00
|
|
|
}
|
2017-06-26 16:16:54 -04: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 {
|
|
|
|
return nil, errors.Wrap(err, "Couldn't read GenesisDoc file")
|
|
|
|
}
|
|
|
|
genDoc, err := GenesisDocFromJSON(jsonBlob)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Error reading GenesisDoc")
|
|
|
|
}
|
|
|
|
if genDoc.ChainID == "" {
|
|
|
|
return nil, errors.Errorf("Genesis doc %v must include non-empty chain_id", genDocFile)
|
|
|
|
}
|
|
|
|
return genDoc, nil
|
|
|
|
}
|