Skip to content

Commit

Permalink
Merge pull request cosmos#939 from cosmos/jae/fixratmarshalamino
Browse files Browse the repository at this point in the history
Use GoAmino 0.9.9 and change impl of Rational.MarshalAmino
  • Loading branch information
rigelrozanski authored May 1, 2018
2 parents 7ef5e90 + dd9b5e7 commit a6245c3
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 49 deletions.
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

[[constraint]]
name = "github.com/tendermint/go-amino"
version = "~0.9.8"
version = "~0.9.9"

[[constraint]]
name = "github.com/tendermint/iavl"
Expand Down
3 changes: 2 additions & 1 deletion cmd/gaia/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/ibc"
Expand Down Expand Up @@ -107,7 +108,7 @@ func setGenesis(gapp *GaiaApp, accs ...*auth.BaseAccount) error {
StakeData: stake.GetDefaultGenesisState(),
}

stateBytes, err := json.MarshalIndent(genesisState, "", "\t")
stateBytes, err := wire.MarshalJSONIndent(gapp.cdc, genesisState)
if err != nil {
return err
}
Expand Down
21 changes: 5 additions & 16 deletions types/rational.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"bytes"
"fmt"
"math/big"
"strconv"
Expand Down Expand Up @@ -153,26 +152,16 @@ func (r Rat) ToLeftPadded(totalDigits int8) string {

//___________________________________________________________________________________

//Wraps r.MarshalText() in quotes to make it a valid JSON string.
func (r Rat) MarshalJSON() ([]byte, error) {
//Wraps r.MarshalText().
func (r Rat) MarshalAmino() (string, error) {
bz, err := (&(r.Rat)).MarshalText()
if err != nil {
return nil, err
}
return []byte(fmt.Sprintf("\"%s\"", bz)), nil
return string(bz), err
}

// Requires a valid JSON string - strings quotes and calls UnmarshalText
func (r *Rat) UnmarshalJSON(data []byte) (err error) {
quote := []byte(`"`)
if len(data) < 2 ||
!bytes.HasPrefix(data, quote) ||
!bytes.HasSuffix(data, quote) {
return fmt.Errorf("JSON encoded Rat must be a quote-delimitted string, have %v", string(data))
}
data = bytes.Trim(data, `"`)
func (r *Rat) UnmarshalAmino(text string) (err error) {
tempRat := big.NewRat(0, 1)
err = tempRat.UnmarshalText(data)
err = tempRat.UnmarshalText([]byte(text))
if err != nil {
return err
}
Expand Down
17 changes: 14 additions & 3 deletions types/rational_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,34 @@ func TestSerializationText(t *testing.T) {
bz, err := r.MarshalText()
require.NoError(t, err)

r2 := NewRat(0, 1)
var r2 Rat
err = r2.UnmarshalText(bz)
require.NoError(t, err)
assert.True(t, r.Equal(r2), "original: %v, unmarshalled: %v", r, r2)
}

func TestSerializationGoWire(t *testing.T) {
func TestSerializationGoWireJSON(t *testing.T) {
r := NewRat(1, 3)
bz, err := cdc.MarshalJSON(r)
require.NoError(t, err)

r2 := NewRat(0, 1)
var r2 Rat
err = cdc.UnmarshalJSON(bz, &r2)
require.NoError(t, err)
assert.True(t, r.Equal(r2), "original: %v, unmarshalled: %v", r, r2)
}

func TestSerializationGoWireBinary(t *testing.T) {
r := NewRat(1, 3)
bz, err := cdc.MarshalBinary(r)
require.NoError(t, err)

var r2 Rat
err = cdc.UnmarshalBinary(bz, &r2)
require.NoError(t, err)
assert.True(t, r.Equal(r2), "original: %v, unmarshalled: %v", r, r2)
}

type testEmbedStruct struct {
Field1 string `json:"f1"`
Field2 int `json:"f2"`
Expand Down
4 changes: 2 additions & 2 deletions x/stake/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func GetCmdQueryCandidate(storeName string, cdc *wire.Codec) *cobra.Command {

// parse out the candidate
candidate := new(stake.Candidate)
err = cdc.UnmarshalJSON(res, candidate)
err = cdc.UnmarshalBinary(res, candidate)
if err != nil {
return err
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func GetCmdQueryDelegatorBond(storeName string, cdc *wire.Codec) *cobra.Command

// parse out the bond
bond := new(stake.DelegatorBond)
err = cdc.UnmarshalJSON(res, bond)
err = cdc.UnmarshalBinary(res, bond)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion x/stake/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func BondingStatusHandlerFn(storeName string, cdc *wire.Codec, kb keys.Keybase,
}

var bond stake.DelegatorBond
err = cdc.UnmarshalJSON(res, &bond)
err = cdc.UnmarshalBinary(res, &bond)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Couldn't decode bond. Error: %s", err.Error())))
Expand Down
44 changes: 22 additions & 22 deletions x/stake/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (k Keeper) getCounter(ctx sdk.Context) int16 {
return 0
}
var counter int16
err := k.cdc.UnmarshalJSON(b, &counter)
err := k.cdc.UnmarshalBinary(b, &counter)
if err != nil {
panic(err)
}
Expand All @@ -51,7 +51,7 @@ func (k Keeper) getCounter(ctx sdk.Context) int16 {
// set the current in-block validator operation counter
func (k Keeper) setCounter(ctx sdk.Context, counter int16) {
store := ctx.KVStore(k.storeKey)
bz, err := k.cdc.MarshalJSON(counter)
bz, err := k.cdc.MarshalBinary(counter)
if err != nil {
panic(err)
}
Expand All @@ -67,7 +67,7 @@ func (k Keeper) GetCandidate(ctx sdk.Context, addr sdk.Address) (candidate Candi
if b == nil {
return candidate, false
}
err := k.cdc.UnmarshalJSON(b, &candidate)
err := k.cdc.UnmarshalBinary(b, &candidate)
if err != nil {
panic(err)
}
Expand All @@ -88,7 +88,7 @@ func (k Keeper) GetCandidates(ctx sdk.Context, maxRetrieve int16) (candidates Ca
}
bz := iterator.Value()
var candidate Candidate
err := k.cdc.UnmarshalJSON(bz, &candidate)
err := k.cdc.UnmarshalBinary(bz, &candidate)
if err != nil {
panic(err)
}
Expand All @@ -112,7 +112,7 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) {
}

// marshal the candidate record and add to the state
bz, err := k.cdc.MarshalJSON(candidate)
bz, err := k.cdc.MarshalBinary(candidate)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -145,15 +145,15 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) {
}

// update the candidate record
bz, err = k.cdc.MarshalJSON(candidate)
bz, err = k.cdc.MarshalBinary(candidate)
if err != nil {
panic(err)
}
store.Set(GetCandidateKey(candidate.Address), bz)

// marshal the new validator record
validator := candidate.validator()
bz, err = k.cdc.MarshalJSON(validator)
bz, err = k.cdc.MarshalBinary(validator)
if err != nil {
panic(err)
}
Expand All @@ -171,7 +171,7 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) {
setAcc = true
}
if setAcc {
bz, err = k.cdc.MarshalJSON(validator.abciValidator(k.cdc))
bz, err = k.cdc.MarshalBinary(validator.abciValidator(k.cdc))
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -200,7 +200,7 @@ func (k Keeper) removeCandidate(ctx sdk.Context, address sdk.Address) {
if store.Get(GetRecentValidatorKey(address)) == nil {
return
}
bz, err := k.cdc.MarshalJSON(candidate.validator().abciValidatorZero(k.cdc))
bz, err := k.cdc.MarshalBinary(candidate.validator().abciValidatorZero(k.cdc))
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -242,7 +242,7 @@ func (k Keeper) GetValidators(ctx sdk.Context) (validators []Validator) {
}
bz := iterator.Value()
var validator Validator
err := k.cdc.UnmarshalJSON(bz, &validator)
err := k.cdc.UnmarshalBinary(bz, &validator)
if err != nil {
panic(err)
}
Expand All @@ -266,11 +266,11 @@ func (k Keeper) GetValidators(ctx sdk.Context) (validators []Validator) {
// get the zero abci validator from the ToKickOut iterator value
bz := iterator.Value()
var validator Validator
err := k.cdc.UnmarshalJSON(bz, &validator)
err := k.cdc.UnmarshalBinary(bz, &validator)
if err != nil {
panic(err)
}
bz, err = k.cdc.MarshalJSON(validator.abciValidatorZero(k.cdc))
bz, err = k.cdc.MarshalBinary(validator.abciValidatorZero(k.cdc))
if err != nil {
panic(err)
}
Expand All @@ -297,7 +297,7 @@ func (k Keeper) isNewValidator(ctx sdk.Context, store sdk.KVStore, address sdk.A
}
bz := iterator.Value()
var val Validator
err := k.cdc.UnmarshalJSON(bz, &val)
err := k.cdc.UnmarshalBinary(bz, &val)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -330,7 +330,7 @@ func (k Keeper) getAccUpdateValidators(ctx sdk.Context) (updates []abci.Validato
for ; iterator.Valid(); iterator.Next() {
valBytes := iterator.Value()
var val abci.Validator
err := k.cdc.UnmarshalJSON(valBytes, &val)
err := k.cdc.UnmarshalBinary(valBytes, &val)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -364,7 +364,7 @@ func (k Keeper) GetDelegatorBond(ctx sdk.Context,
return bond, false
}

err := k.cdc.UnmarshalJSON(delegatorBytes, &bond)
err := k.cdc.UnmarshalBinary(delegatorBytes, &bond)
if err != nil {
panic(err)
}
Expand All @@ -385,7 +385,7 @@ func (k Keeper) getBonds(ctx sdk.Context, maxRetrieve int16) (bonds []DelegatorB
}
bondBytes := iterator.Value()
var bond DelegatorBond
err := k.cdc.UnmarshalJSON(bondBytes, &bond)
err := k.cdc.UnmarshalBinary(bondBytes, &bond)
if err != nil {
panic(err)
}
Expand All @@ -410,7 +410,7 @@ func (k Keeper) GetDelegatorBonds(ctx sdk.Context, delegator sdk.Address, maxRet
}
bondBytes := iterator.Value()
var bond DelegatorBond
err := k.cdc.UnmarshalJSON(bondBytes, &bond)
err := k.cdc.UnmarshalBinary(bondBytes, &bond)
if err != nil {
panic(err)
}
Expand All @@ -422,7 +422,7 @@ func (k Keeper) GetDelegatorBonds(ctx sdk.Context, delegator sdk.Address, maxRet

func (k Keeper) setDelegatorBond(ctx sdk.Context, bond DelegatorBond) {
store := ctx.KVStore(k.storeKey)
b, err := k.cdc.MarshalJSON(bond)
b, err := k.cdc.MarshalBinary(bond)
if err != nil {
panic(err)
}
Expand All @@ -448,15 +448,15 @@ func (k Keeper) GetParams(ctx sdk.Context) (params Params) {
panic("Stored params should not have been nil")
}

err := k.cdc.UnmarshalJSON(b, &params)
err := k.cdc.UnmarshalBinary(b, &params)
if err != nil {
panic(err)
}
return
}
func (k Keeper) setParams(ctx sdk.Context, params Params) {
store := ctx.KVStore(k.storeKey)
b, err := k.cdc.MarshalJSON(params)
b, err := k.cdc.MarshalBinary(params)
if err != nil {
panic(err)
}
Expand All @@ -477,7 +477,7 @@ func (k Keeper) GetPool(ctx sdk.Context) (pool Pool) {
if b == nil {
panic("Stored pool should not have been nil")
}
err := k.cdc.UnmarshalJSON(b, &pool)
err := k.cdc.UnmarshalBinary(b, &pool)
if err != nil {
panic(err) // This error should never occur big problem if does
}
Expand All @@ -486,7 +486,7 @@ func (k Keeper) GetPool(ctx sdk.Context) (pool Pool) {

func (k Keeper) setPool(ctx sdk.Context, p Pool) {
store := ctx.KVStore(k.storeKey)
b, err := k.cdc.MarshalJSON(p)
b, err := k.cdc.MarshalBinary(p)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit a6245c3

Please sign in to comment.