Merge pull request #1072 from ValarDragon/dev/tx_encoding

Change to go-wire UnmarshalJSON for bank transactions
This commit is contained in:
Christopher Goes
2018-05-31 23:51:27 +02:00
committed by GitHub
12 changed files with 65 additions and 31 deletions
+12
View File
@@ -1,5 +1,17 @@
# Changelog
## Pending
BREAKING CHANGES
FEATURES
IMPROVEMENTS
* bank module uses go-wire codec instead of 'encoding/json'
* auth module uses go-wire codec instead of 'encoding/json'
FIXES
## 0.18.1
BREAKING CHANGES
+1 -3
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)
}
+2 -4
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(),
+7
View File
@@ -10,3 +10,10 @@ 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)
wire.RegisterCrypto(msgCdc)
}
+8 -1
View File
@@ -11,6 +11,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"
)
@@ -29,6 +30,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) {
@@ -43,7 +50,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()))
+2 -4
View File
@@ -1,8 +1,6 @@
package bank
import (
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@@ -55,7 +53,7 @@ func (msg MsgSend) ValidateBasic() sdk.Error {
// Implements Msg.
func (msg MsgSend) 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)
}
@@ -104,7 +102,7 @@ func (msg MsgIssue) ValidateBasic() sdk.Error {
// Implements Msg.
func (msg MsgIssue) 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)
}
+14 -2
View File
@@ -186,8 +186,14 @@ func TestMsgSendGetSignBytes(t *testing.T) {
Outputs: []Output{NewOutput(addr2, coins)},
}
res := msg.GetSignBytes()
unmarshaledMsg := &MsgSend{}
msgCdc.UnmarshalJSON(res, unmarshaledMsg)
assert.Equal(t, &msg, unmarshaledMsg)
// TODO bad results
assert.Equal(t, string(res), `{"inputs":[{"address":"696E707574","coins":[{"denom":"atom","amount":10}]}],"outputs":[{"address":"6F7574707574","coins":[{"denom":"atom","amount":10}]}]}`)
expected := `{"type":"EAFDE32A2C87F8","value":{"inputs":[{"address":"696E707574","coins":[{"denom":"atom","amount":10}]}],"outputs":[{"address":"6F7574707574","coins":[{"denom":"atom","amount":10}]}]}}`
assert.Equal(t, expected, string(res))
}
func TestMsgSendGetSigners(t *testing.T) {
@@ -255,8 +261,14 @@ func TestMsgIssueGetSignBytes(t *testing.T) {
Outputs: []Output{NewOutput(addr, coins)},
}
res := msg.GetSignBytes()
unmarshaledMsg := &MsgIssue{}
msgCdc.UnmarshalJSON(res, unmarshaledMsg)
assert.Equal(t, &msg, unmarshaledMsg)
// TODO bad results
assert.Equal(t, string(res), `{"banker":"696E707574","outputs":[{"address":"6C6F616E2D66726F6D2D62616E6B","coins":[{"denom":"atom","amount":10}]}]}`)
expected := `{"type":"72E617C06ABAD0","value":{"banker":"696E707574","outputs":[{"address":"6C6F616E2D66726F6D2D62616E6B","coins":[{"denom":"atom","amount":10}]}]}}`
assert.Equal(t, expected, string(res))
}
func TestMsgIssueGetSigners(t *testing.T) {
+6
View File
@@ -9,3 +9,9 @@ func RegisterWire(cdc *wire.Codec) {
cdc.RegisterConcrete(MsgSend{}, "cosmos-sdk/Send", nil)
cdc.RegisterConcrete(MsgIssue{}, "cosmos-sdk/Issue", nil)
}
var msgCdc = wire.NewCodec()
func init() {
RegisterWire(msgCdc)
}
+3 -12
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
@@ -98,7 +89,7 @@ func (msg MsgEditCandidacy) GetSigners() []sdk.Address {
// 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)
}
@@ -142,7 +133,7 @@ func (msg MsgDelegate) GetSigners() []sdk.Address {
// 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)
}
@@ -189,7 +180,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)
}
+2 -2
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)
}
+2 -2
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)
}
+6 -1
View File
@@ -12,4 +12,9 @@ func RegisterWire(cdc *wire.Codec) {
cdc.RegisterConcrete(MsgUnbond{}, "cosmos-sdk/MsgUnbond", nil)
}
var cdcEmpty = wire.NewCodec()
var msgCdc = wire.NewCodec()
func init() {
RegisterWire(msgCdc)
wire.RegisterCrypto(msgCdc)
}