Switch the default modules to use wire.codec's MarshalJSON instead of json.Marshal

This commit is contained in:
ValarDragon 2018-05-31 11:51:15 -07:00
parent 2d87563856
commit e9545d69ed
11 changed files with 37 additions and 37 deletions

View File

@ -1,8 +1,6 @@
package auth
import (
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types"
crypto "github.com/tendermint/go-crypto"
)
@ -30,7 +28,7 @@ func (msg MsgChangeKey) ValidateBasic() sdk.Error {
// Implements Msg.
func (msg MsgChangeKey) GetSignBytes() []byte {
b, err := json.Marshal(msg) // XXX: ensure some canonical form
b, err := msgCdc.MarshalJSON(msg) // XXX: ensure some canonical form
if err != nil {
panic(err)
}

View File

@ -1,8 +1,6 @@
package auth
import (
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types"
crypto "github.com/tendermint/go-crypto"
)
@ -70,7 +68,7 @@ func (fee StdFee) Bytes() []byte {
if len(fee.Amount) == 0 {
fee.Amount = sdk.Coins{}
}
bz, err := json.Marshal(fee) // TODO
bz, err := msgCdc.MarshalJSON(fee) // TODO
if err != nil {
panic(err)
}
@ -95,7 +93,7 @@ type StdSignDoc struct {
// StdSignBytes returns the bytes to sign for a transaction.
// TODO: change the API to just take a chainID and StdTx ?
func StdSignBytes(chainID string, sequences []int64, fee StdFee, msg sdk.Msg) []byte {
bz, err := json.Marshal(StdSignDoc{
bz, err := msgCdc.MarshalJSON(StdSignDoc{
ChainID: chainID,
Sequences: sequences,
FeeBytes: fee.Bytes(),

View File

@ -10,3 +10,9 @@ func RegisterWire(cdc *wire.Codec) {
cdc.RegisterConcrete(&BaseAccount{}, "auth/Account", nil)
cdc.RegisterConcrete(MsgChangeKey{}, "auth/ChangeKey", nil)
}
var msgCdc = wire.NewCodec()
func init() {
RegisterWire(msgCdc)
}

View File

@ -12,6 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/bank/client"
)
@ -30,6 +31,12 @@ type sendBody struct {
Sequence int64 `json:"sequence"`
}
var msgCdc = wire.NewCodec()
func init() {
bank.RegisterWire(msgCdc)
}
// SendRequestHandlerFn - http request handler to send coins to a address
func SendRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
@ -44,7 +51,7 @@ func SendRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, ctx context.CoreCont
w.Write([]byte(err.Error()))
return
}
err = json.Unmarshal(body, &m)
err = msgCdc.UnmarshalJSON(body, &m)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))

View File

@ -53,8 +53,7 @@ func (msg MsgSend) ValidateBasic() sdk.Error {
// Implements Msg.
func (msg MsgSend) GetSignBytes() []byte {
cdc := getCodec()
b, err := cdc.MarshalJSON(msg) // XXX: ensure some canonical form
b, err := msgCdc.MarshalJSON(msg) // XXX: ensure some canonical form
if err != nil {
panic(err)
}
@ -103,8 +102,7 @@ func (msg MsgIssue) ValidateBasic() sdk.Error {
// Implements Msg.
func (msg MsgIssue) GetSignBytes() []byte {
cdc := getCodec()
b, err := cdc.MarshalJSON(msg) // XXX: ensure some canonical form
b, err := msgCdc.MarshalJSON(msg) // XXX: ensure some canonical form
if err != nil {
panic(err)
}

View File

@ -187,9 +187,8 @@ func TestMsgSendGetSignBytes(t *testing.T) {
}
res := msg.GetSignBytes()
cdc := getCodec()
unmarshaledMsg := &MsgSend{}
cdc.UnmarshalJSON(res, unmarshaledMsg)
msgCdc.UnmarshalJSON(res, unmarshaledMsg)
assert.Equal(t, &msg, unmarshaledMsg)
// TODO bad results
@ -263,9 +262,8 @@ func TestMsgIssueGetSignBytes(t *testing.T) {
}
res := msg.GetSignBytes()
cdc := getCodec()
unmarshaledMsg := &MsgIssue{}
cdc.UnmarshalJSON(res, unmarshaledMsg)
msgCdc.UnmarshalJSON(res, unmarshaledMsg)
assert.Equal(t, &msg, unmarshaledMsg)
// TODO bad results

View File

@ -10,8 +10,8 @@ func RegisterWire(cdc *wire.Codec) {
cdc.RegisterConcrete(MsgIssue{}, "cosmos-sdk/Issue", nil)
}
func getCodec() *wire.Codec {
cdc := wire.NewCodec()
RegisterWire(cdc)
return cdc
var msgCdc = wire.NewCodec()
func init() {
RegisterWire(msgCdc)
}

View File

@ -1,10 +1,7 @@
package stake
import (
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
crypto "github.com/tendermint/go-crypto"
)
@ -20,12 +17,6 @@ const StakingToken = "steak"
//Verify interface at compile time
var _, _, _, _ sdk.Msg = &MsgDeclareCandidacy{}, &MsgEditCandidacy{}, &MsgDelegate{}, &MsgUnbond{}
var msgCdc = wire.NewCodec()
func init() {
wire.RegisterCrypto(msgCdc)
}
//______________________________________________________________________
// MsgDeclareCandidacy - struct for unbonding transactions
@ -94,7 +85,7 @@ func (msg MsgEditCandidacy) GetSigners() []sdk.Address { return []sdk.Address{ms
// get the bytes for the message signer to sign on
func (msg MsgEditCandidacy) GetSignBytes() []byte {
b, err := json.Marshal(msg)
b, err := msgCdc.MarshalJSON(msg)
if err != nil {
panic(err)
}
@ -136,7 +127,7 @@ func (msg MsgDelegate) GetSigners() []sdk.Address { return []sdk.Address{msg.Del
// get the bytes for the message signer to sign on
func (msg MsgDelegate) GetSignBytes() []byte {
b, err := json.Marshal(msg)
b, err := msgCdc.MarshalJSON(msg)
if err != nil {
panic(err)
}
@ -183,7 +174,7 @@ func (msg MsgUnbond) GetSigners() []sdk.Address { return []sdk.Address{msg.Deleg
// get the bytes for the message signer to sign on
func (msg MsgUnbond) GetSignBytes() []byte {
b, err := json.Marshal(msg)
b, err := msgCdc.MarshalJSON(msg)
if err != nil {
panic(err)
}

View File

@ -18,8 +18,8 @@ type Params struct {
}
func (p Params) equal(p2 Params) bool {
bz1 := cdcEmpty.MustMarshalBinary(&p)
bz2 := cdcEmpty.MustMarshalBinary(&p2)
bz1 := msgCdc.MustMarshalBinary(&p)
bz2 := msgCdc.MustMarshalBinary(&p2)
return bytes.Equal(bz1, bz2)
}

View File

@ -25,8 +25,8 @@ type Pool struct {
}
func (p Pool) equal(p2 Pool) bool {
bz1 := cdcEmpty.MustMarshalBinary(&p)
bz2 := cdcEmpty.MustMarshalBinary(&p2)
bz1 := msgCdc.MustMarshalBinary(&p)
bz2 := msgCdc.MustMarshalBinary(&p2)
return bytes.Equal(bz1, bz2)
}

View File

@ -12,4 +12,8 @@ func RegisterWire(cdc *wire.Codec) {
cdc.RegisterConcrete(MsgUnbond{}, "cosmos-sdk/MsgUnbond", nil)
}
var cdcEmpty = wire.NewCodec()
var msgCdc = wire.NewCodec()
func init() {
wire.RegisterCrypto(msgCdc)
}