mirror of
https://github.com/fluencelabs/tendermint
synced 2025-07-30 19:51:58 +00:00
change genesis & privValidator JSON to use binary/reflect
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"time"
|
||||
|
||||
@@ -16,12 +13,12 @@ import (
|
||||
)
|
||||
|
||||
type GenesisAccount struct {
|
||||
Address string
|
||||
Address []byte
|
||||
Amount uint64
|
||||
}
|
||||
|
||||
type GenesisValidator struct {
|
||||
PubKey string
|
||||
PubKey PubKeyEd25519
|
||||
Amount uint64
|
||||
UnbondTo []GenesisAccount
|
||||
}
|
||||
@@ -33,7 +30,8 @@ type GenesisDoc struct {
|
||||
}
|
||||
|
||||
func GenesisDocFromJSON(jsonBlob []byte) (genState *GenesisDoc) {
|
||||
err := json.Unmarshal(jsonBlob, &genState)
|
||||
var err error
|
||||
ReadJSON(&genState, jsonBlob, &err)
|
||||
if err != nil {
|
||||
panic(Fmt("Couldn't read GenesisDoc: %v", err))
|
||||
}
|
||||
@@ -61,32 +59,20 @@ func MakeGenesisState(db db_.DB, genDoc *GenesisDoc) *State {
|
||||
// Make accounts state tree
|
||||
accounts := merkle.NewIAVLTree(BasicCodec, AccountCodec, defaultAccountsCacheCapacity, db)
|
||||
for _, acc := range genDoc.Accounts {
|
||||
address, err := hex.DecodeString(acc.Address)
|
||||
if err != nil {
|
||||
Exit(Fmt("Invalid account address: %v", acc.Address))
|
||||
}
|
||||
account := &Account{
|
||||
Address: address,
|
||||
Address: acc.Address,
|
||||
PubKey: PubKeyNil{},
|
||||
Sequence: 0,
|
||||
Balance: acc.Amount,
|
||||
}
|
||||
accounts.Set(address, account)
|
||||
accounts.Set(acc.Address, account)
|
||||
}
|
||||
|
||||
// Make validatorInfos state tree && validators slice
|
||||
validatorInfos := merkle.NewIAVLTree(BasicCodec, ValidatorInfoCodec, 0, db)
|
||||
validators := make([]*Validator, len(genDoc.Validators))
|
||||
for i, val := range genDoc.Validators {
|
||||
pubKeyBytes, err := hex.DecodeString(val.PubKey)
|
||||
if err != nil {
|
||||
Exit(Fmt("Invalid validator pubkey: %v", val.PubKey))
|
||||
}
|
||||
pubKey := ReadBinary(PubKeyEd25519{},
|
||||
bytes.NewBuffer(pubKeyBytes), new(int64), &err).(PubKeyEd25519)
|
||||
if err != nil {
|
||||
Exit(Fmt("Invalid validator pubkey: %v", val.PubKey))
|
||||
}
|
||||
pubKey := val.PubKey
|
||||
address := pubKey.Address()
|
||||
|
||||
// Make ValidatorInfo
|
||||
@@ -98,12 +84,8 @@ func MakeGenesisState(db db_.DB, genDoc *GenesisDoc) *State {
|
||||
FirstBondAmount: val.Amount,
|
||||
}
|
||||
for i, unbondTo := range val.UnbondTo {
|
||||
address, err := hex.DecodeString(unbondTo.Address)
|
||||
if err != nil {
|
||||
Exit(Fmt("Invalid unbond-to address: %v", unbondTo.Address))
|
||||
}
|
||||
valInfo.UnbondTo[i] = &TxOutput{
|
||||
Address: address,
|
||||
Address: unbondTo.Address,
|
||||
Amount: unbondTo.Amount,
|
||||
}
|
||||
}
|
||||
|
@@ -3,9 +3,6 @@ package state
|
||||
// TODO: This logic is crude. Should be more transactional.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
@@ -74,50 +71,16 @@ func GenPrivValidator() *PrivValidator {
|
||||
}
|
||||
}
|
||||
|
||||
type PrivValidatorJSON struct {
|
||||
Address string
|
||||
PubKey string
|
||||
PrivKey string
|
||||
LastHeight uint
|
||||
LastRound uint
|
||||
LastStep uint8
|
||||
}
|
||||
|
||||
func LoadPrivValidator(filename string) *PrivValidator {
|
||||
privValJSONBytes, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
privValJSON := PrivValidatorJSON{}
|
||||
err = json.Unmarshal(privValJSONBytes, &privValJSON)
|
||||
privVal := ReadJSON(&PrivValidator{}, privValJSONBytes, &err).(*PrivValidator)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
address, err := hex.DecodeString(privValJSON.Address)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
pubKeyBytes, err := hex.DecodeString(privValJSON.PubKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
privKeyBytes, err := hex.DecodeString(privValJSON.PrivKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
n := new(int64)
|
||||
privVal := &PrivValidator{
|
||||
Address: address,
|
||||
PubKey: ReadBinary(PubKeyEd25519{}, bytes.NewReader(pubKeyBytes), n, &err).(PubKeyEd25519),
|
||||
PrivKey: ReadBinary(PrivKeyEd25519{}, bytes.NewReader(privKeyBytes), n, &err).(PrivKeyEd25519),
|
||||
LastHeight: privValJSON.LastHeight,
|
||||
LastRound: privValJSON.LastRound,
|
||||
LastStep: privValJSON.LastStep,
|
||||
filename: filename,
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
Exit(Fmt("Error reading PrivValidator from %v: %v\n", filename, err))
|
||||
}
|
||||
privVal.filename = filename
|
||||
return privVal
|
||||
}
|
||||
|
||||
@@ -135,21 +98,7 @@ func (privVal *PrivValidator) save() {
|
||||
}
|
||||
}
|
||||
|
||||
func (privVal *PrivValidator) JSONBytes() []byte {
|
||||
privValJSON := PrivValidatorJSON{
|
||||
Address: hex.EncodeToString(privVal.Address),
|
||||
PubKey: hex.EncodeToString(BinaryBytes(privVal.PubKey)),
|
||||
PrivKey: hex.EncodeToString(BinaryBytes(privVal.PrivKey)),
|
||||
LastHeight: privVal.LastHeight,
|
||||
LastRound: privVal.LastRound,
|
||||
LastStep: privVal.LastStep,
|
||||
}
|
||||
privValJSONBytes, err := json.MarshalIndent(privValJSON, "", " ")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return privValJSONBytes
|
||||
}
|
||||
func (privVal *PrivValidator) JSONBytes() []byte { return JSONBytes(privVal) }
|
||||
|
||||
// TODO: test
|
||||
func (privVal *PrivValidator) SignVote(vote *Vote) error {
|
||||
|
@@ -2,11 +2,9 @@ package state
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"sort"
|
||||
|
||||
. "github.com/tendermint/tendermint/account"
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
. "github.com/tendermint/tendermint/block"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
db_ "github.com/tendermint/tendermint/db"
|
||||
@@ -65,7 +63,7 @@ func RandGenesisState(numAccounts int, randBalance bool, minBalance uint64, numV
|
||||
for i := 0; i < numAccounts; i++ {
|
||||
account, privAccount := RandAccount(randBalance, minBalance)
|
||||
accounts[i] = GenesisAccount{
|
||||
Address: hex.EncodeToString(account.Address),
|
||||
Address: account.Address,
|
||||
Amount: account.Balance,
|
||||
}
|
||||
privAccounts[i] = privAccount
|
||||
@@ -75,11 +73,11 @@ func RandGenesisState(numAccounts int, randBalance bool, minBalance uint64, numV
|
||||
for i := 0; i < numValidators; i++ {
|
||||
valInfo, privVal := RandValidator(randBonded, minBonded)
|
||||
validators[i] = GenesisValidator{
|
||||
PubKey: hex.EncodeToString(BinaryBytes(valInfo.PubKey)),
|
||||
PubKey: valInfo.PubKey,
|
||||
Amount: valInfo.FirstBondAmount,
|
||||
UnbondTo: []GenesisAccount{
|
||||
{
|
||||
Address: hex.EncodeToString(valInfo.PubKey.Address()),
|
||||
Address: valInfo.PubKey.Address(),
|
||||
Amount: valInfo.FirstBondAmount,
|
||||
},
|
||||
},
|
||||
|
Reference in New Issue
Block a user