Skip to content

Commit

Permalink
feat: adding Route, Type, GetSignBytes for all messages (#743)
Browse files Browse the repository at this point in the history
* feat: adding Route, Type, GetSignBytes for all messages

* tests: adding tests for Route/Type/GetSignBytes
  • Loading branch information
seantking authored Jan 21, 2022
1 parent 16e452b commit b16353e
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
33 changes: 32 additions & 1 deletion modules/apps/29-fee/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (

// msg types
const (
TypeMsgRegisterCounterpartyAddress = "registerCounterpartyAddress"
TypeMsgPayPacketFee = "payPacketFee"
TypeMsgPayPacketFeeAsync = "payPacketFeeAsync"
)

// NewMsgRegisterCounterpartyAddress creates a new instance of MsgRegisterCounterpartyAddress
Expand Down Expand Up @@ -101,6 +102,21 @@ func (msg MsgPayPacketFee) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{signer}
}

// Route implements sdk.Msg
func (msg MsgPayPacketFee) Route() string {
return RouterKey
}

// Type implements sdk.Msg
func (msg MsgPayPacketFee) Type() string {
return TypeMsgPayPacketFee
}

// GetSignBytes implements sdk.Msg.
func (msg MsgPayPacketFee) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}

// NewMsgPayPacketAsync creates a new instance of MsgPayPacketFee
func NewMsgPayPacketFeeAsync(identifiedPacketFee IdentifiedPacketFee) *MsgPayPacketFeeAsync {
return &MsgPayPacketFeeAsync{
Expand Down Expand Up @@ -133,6 +149,21 @@ func (msg MsgPayPacketFeeAsync) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{signer}
}

// Route implements sdk.Msg
func (msg MsgPayPacketFeeAsync) Route() string {
return RouterKey
}

// Type implements sdk.Msg
func (msg MsgPayPacketFeeAsync) Type() string {
return TypeMsgPayPacketFeeAsync
}

// GetSignBytes implements sdk.Msg.
func (msg MsgPayPacketFeeAsync) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}

func NewIdentifiedPacketFee(packetId channeltypes.PacketId, fee Fee, refundAddr string, relayers []string) IdentifiedPacketFee {
return IdentifiedPacketFee{
PacketId: packetId,
Expand Down
94 changes: 94 additions & 0 deletions modules/apps/29-fee/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,50 @@ func TestPayPacketFeeGetSigners(t *testing.T) {
require.Equal(t, []sdk.AccAddress{addr}, res)
}

// TestMsgPayPacketFeeRoute tests Route for MsgPayPacketFee
func TestMsgPayPacketFeeRoute(t *testing.T) {
addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())

// build message
signer := addr.String()
channelID := validChannelID
portID := validPortID
fee := Fee{validCoins, validCoins, validCoins}
msg := NewMsgPayPacketFee(fee, portID, channelID, signer, nil)

require.Equal(t, RouterKey, msg.Route())
}

// TestMsgPayPacketFeeType tests Type for MsgPayPacketFee
func TestMsgPayPacketFeeType(t *testing.T) {
addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())

// build message
signer := addr.String()
channelID := validChannelID
portID := validPortID
fee := Fee{validCoins, validCoins, validCoins}
msg := NewMsgPayPacketFee(fee, portID, channelID, signer, nil)

require.Equal(t, "payPacketFee", msg.Type())
}

// TestMsgPayPacketFeeGetSignBytes tests that GetSignBytes does not panic
func TestMsgPayPacketFeeGetSignBytes(t *testing.T) {
addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())

// build message
signer := addr.String()
channelID := validChannelID
portID := validPortID
fee := Fee{validCoins, validCoins, validCoins}
msg := NewMsgPayPacketFee(fee, portID, channelID, signer, nil)

require.NotPanics(t, func() {
_ = msg.GetSignBytes()
})
}

// TestMsgPayPacketFeeAsyncValidation tests ValidateBasic
func TestMsgPayPacketFeeAsyncValidation(t *testing.T) {
var (
Expand Down Expand Up @@ -351,3 +395,53 @@ func TestPayPacketFeeAsyncGetSigners(t *testing.T) {

require.Equal(t, []sdk.AccAddress{addr}, res)
}

// TestMsgPayPacketFeeAsyncRoute tests Route for MsgPayPacketFeeAsync
func TestMsgPayPacketFeeAsyncRoute(t *testing.T) {
addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())

// build message
channelID := validChannelID
portID := validPortID
fee := Fee{validCoins, validCoins, validCoins}
seq := uint64(1)
packetId := channeltypes.NewPacketId(channelID, portID, seq)
identifiedPacketFee := IdentifiedPacketFee{PacketId: packetId, Fee: fee, RefundAddress: addr.String(), Relayers: nil}
msg := NewMsgPayPacketFeeAsync(identifiedPacketFee)

require.Equal(t, RouterKey, msg.Route())
}

// TestMsgPayPacketFeeAsyncType tests Type for MsgPayPacketFeeAsync
func TestMsgPayPacketFeeAsyncType(t *testing.T) {
addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())

// build message
channelID := validChannelID
portID := validPortID
fee := Fee{validCoins, validCoins, validCoins}
seq := uint64(1)
packetId := channeltypes.NewPacketId(channelID, portID, seq)
identifiedPacketFee := IdentifiedPacketFee{PacketId: packetId, Fee: fee, RefundAddress: addr.String(), Relayers: nil}
msg := NewMsgPayPacketFeeAsync(identifiedPacketFee)

require.Equal(t, "payPacketFeeAsync", msg.Type())
}

// TestMsgPayPacketFeeAsyncGetSignBytes tests that GetSignBytes does not panic
func TestMsgPayPacketFeeAsyncGetSignBytes(t *testing.T) {
addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())

// build message
channelID := validChannelID
portID := validPortID
fee := Fee{validCoins, validCoins, validCoins}
seq := uint64(1)
packetId := channeltypes.NewPacketId(channelID, portID, seq)
identifiedPacketFee := IdentifiedPacketFee{PacketId: packetId, Fee: fee, RefundAddress: addr.String(), Relayers: nil}
msg := NewMsgPayPacketFeeAsync(identifiedPacketFee)

require.NotPanics(t, func() {
_ = msg.GetSignBytes()
})
}

0 comments on commit b16353e

Please sign in to comment.