Browse Source

types: bring back json.Marshal/Unmarshal for genesis/priv_val

pull/1265/head
Ethan Buchman 6 years ago
parent
commit
6596bff8ec
3 changed files with 17 additions and 18 deletions
  1. +2
    -3
      types/genesis.go
  2. +4
    -4
      types/genesis_test.go
  3. +11
    -11
      types/priv_validator.go

+ 2
- 3
types/genesis.go View File

@ -8,7 +8,6 @@ import (
"github.com/pkg/errors"
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/tendermint/wire"
cmn "github.com/tendermint/tmlibs/common"
)
@ -34,7 +33,7 @@ type GenesisDoc struct {
// SaveAs is a utility method for saving GenensisDoc as a JSON file.
func (genDoc *GenesisDoc) SaveAs(file string) error {
genDocBytes, err := wire.MarshalJSON(genDoc)
genDocBytes, err := json.Marshal(genDoc)
if err != nil {
return err
}
@ -90,7 +89,7 @@ func (genDoc *GenesisDoc) ValidateAndComplete() error {
// GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc.
func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
genDoc := GenesisDoc{}
err := wire.UnmarshalJSON(jsonBlob, &genDoc)
err := json.Unmarshal(jsonBlob, &genDoc)
if err != nil {
return nil, err
}


+ 4
- 4
types/genesis_test.go View File

@ -1,12 +1,12 @@
package types
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
crypto "github.com/tendermint/go-crypto"
wire "github.com/tendermint/tendermint/wire"
)
func TestGenesis(t *testing.T) {
@ -42,7 +42,7 @@ func TestGenesis(t *testing.T) {
ChainID: "abc",
Validators: []GenesisValidator{{crypto.GenPrivKeyEd25519().PubKey(), 10, "myval"}},
}
genDocBytes, err = wire.MarshalJSON(baseGenDoc)
genDocBytes, err = json.Marshal(baseGenDoc)
assert.NoError(t, err, "error marshalling genDoc")
// test base gendoc and check consensus params were filled
@ -51,14 +51,14 @@ func TestGenesis(t *testing.T) {
assert.NotNil(t, genDoc.ConsensusParams, "expected consensus params to be filled in")
// create json with consensus params filled
genDocBytes, err = wire.MarshalJSON(genDoc)
genDocBytes, err = json.Marshal(genDoc)
assert.NoError(t, err, "error marshalling genDoc")
genDoc, err = GenesisDocFromJSON(genDocBytes)
assert.NoError(t, err, "expected no error for valid genDoc json")
// test with invalid consensus params
genDoc.ConsensusParams.BlockSize.MaxBytes = 0
genDocBytes, err = wire.MarshalJSON(genDoc)
genDocBytes, err = json.Marshal(genDoc)
assert.NoError(t, err, "error marshalling genDoc")
genDoc, err = GenesisDocFromJSON(genDocBytes)
assert.Error(t, err, "expected error for genDoc json with block size of 0")


+ 11
- 11
types/priv_validator.go View File

@ -2,6 +2,7 @@ package types
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
@ -9,7 +10,6 @@ import (
"time"
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/tendermint/wire"
cmn "github.com/tendermint/tmlibs/common"
)
@ -198,7 +198,7 @@ func LoadPrivValidatorFSWithSigner(filePath string, signerFunc func(PrivValidato
cmn.Exit(err.Error())
}
privVal := &PrivValidatorFS{}
err = wire.UnmarshalJSON(privValJSONBytes, &privVal)
err = json.Unmarshal(privValJSONBytes, &privVal)
if err != nil {
cmn.Exit(cmn.Fmt("Error reading PrivValidator from %v: %v\n", filePath, err))
}
@ -219,7 +219,7 @@ func (privVal *PrivValidatorFS) save() {
if privVal.filePath == "" {
cmn.PanicSanity("Cannot save PrivValidator: filePath not set")
}
jsonBytes, err := wire.MarshalJSON(privVal)
jsonBytes, err := json.Marshal(privVal)
if err != nil {
// `@; BOOM!!!
cmn.PanicCrisis(err)
@ -422,10 +422,10 @@ func (pvs PrivValidatorsByAddress) Swap(i, j int) {
// returns true if the only difference in the votes is their timestamp.
func checkVotesOnlyDifferByTimestamp(lastSignBytes, newSignBytes []byte) (time.Time, bool) {
var lastVote, newVote CanonicalJSONOnceVote
if err := wire.UnmarshalJSON(lastSignBytes, &lastVote); err != nil {
if err := json.Unmarshal(lastSignBytes, &lastVote); err != nil {
panic(fmt.Sprintf("LastSignBytes cannot be unmarshalled into vote: %v", err))
}
if err := wire.UnmarshalJSON(newSignBytes, &newVote); err != nil {
if err := json.Unmarshal(newSignBytes, &newVote); err != nil {
panic(fmt.Sprintf("signBytes cannot be unmarshalled into vote: %v", err))
}
@ -438,8 +438,8 @@ func checkVotesOnlyDifferByTimestamp(lastSignBytes, newSignBytes []byte) (time.T
now := CanonicalTime(time.Now())
lastVote.Vote.Timestamp = now
newVote.Vote.Timestamp = now
lastVoteBytes, _ := wire.MarshalJSON(lastVote)
newVoteBytes, _ := wire.MarshalJSON(newVote)
lastVoteBytes, _ := json.Marshal(lastVote)
newVoteBytes, _ := json.Marshal(newVote)
return lastTime, bytes.Equal(newVoteBytes, lastVoteBytes)
}
@ -448,10 +448,10 @@ func checkVotesOnlyDifferByTimestamp(lastSignBytes, newSignBytes []byte) (time.T
// returns true if the only difference in the proposals is their timestamp
func checkProposalsOnlyDifferByTimestamp(lastSignBytes, newSignBytes []byte) (time.Time, bool) {
var lastProposal, newProposal CanonicalJSONOnceProposal
if err := wire.UnmarshalJSON(lastSignBytes, &lastProposal); err != nil {
if err := json.Unmarshal(lastSignBytes, &lastProposal); err != nil {
panic(fmt.Sprintf("LastSignBytes cannot be unmarshalled into proposal: %v", err))
}
if err := wire.UnmarshalJSON(newSignBytes, &newProposal); err != nil {
if err := json.Unmarshal(newSignBytes, &newProposal); err != nil {
panic(fmt.Sprintf("signBytes cannot be unmarshalled into proposal: %v", err))
}
@ -464,8 +464,8 @@ func checkProposalsOnlyDifferByTimestamp(lastSignBytes, newSignBytes []byte) (ti
now := CanonicalTime(time.Now())
lastProposal.Proposal.Timestamp = now
newProposal.Proposal.Timestamp = now
lastProposalBytes, _ := wire.MarshalJSON(lastProposal)
newProposalBytes, _ := wire.MarshalJSON(newProposal)
lastProposalBytes, _ := json.Marshal(lastProposal)
newProposalBytes, _ := json.Marshal(newProposal)
return lastTime, bytes.Equal(newProposalBytes, lastProposalBytes)
}

Loading…
Cancel
Save