Skip to content

Commit

Permalink
chore: add unittest for amino msg.
Browse files Browse the repository at this point in the history
  • Loading branch information
zemyblue committed May 11, 2024
1 parent 3a046da commit aac46c7
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 16 deletions.
4 changes: 2 additions & 2 deletions x/fbridge/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
}

// Deprecated: Route does nothing.
func (am AppModule) Route() sdk.Route { return sdk.NewRoute("", nil) }
func (am AppModule) Route() sdk.Route { return sdk.NewRoute(types.RouterKey, nil) }

// Deprecated: QuerierRoute does nothing.
func (am AppModule) QuerierRoute() string { return "" }
func (am AppModule) QuerierRoute() string { return types.QuerierRoute }

// Deprecated: LegacyQuerierHandler does nothing.
func (am AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier { return nil }
Expand Down
6 changes: 6 additions & 0 deletions x/fbridge/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const (
// StoreKey is the store key string for fbridge
StoreKey = ModuleName

// RouterKey is the message route for fbridge
RouterKey = ModuleName

// QuerierRoute defines the module's query routing key
QuerierRoute = ModuleName

// MemStoreKey is the in-memory store key string for fbridge
MemStoreKey = "mem_" + StoreKey
)
Expand Down
30 changes: 20 additions & 10 deletions x/fbridge/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ func (m MsgUpdateParams) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}

// Type implements the LegacyMsg.Type method.
func (m MsgUpdateParams) Type() string {
return sdk.MsgTypeURL(&m)
}

// Route implements the LegacyMsg.Route method.
func (m MsgUpdateParams) Route() string {
return RouterKey
}

func (m MsgTransfer) ValidateBasic() error { return nil }

func (m MsgTransfer) GetSigners() []sdk.AccAddress {
Expand All @@ -44,7 +54,7 @@ func (m MsgTransfer) Type() string {

// Route implements the LegacyMsg.Route method.
func (m MsgTransfer) Route() string {
return sdk.MsgTypeURL(&m)
return RouterKey
}

func (m MsgProvision) ValidateBasic() error { return nil }
Expand All @@ -65,7 +75,7 @@ func (m MsgProvision) Type() string {

// Route implements the LegacyMsg.Route method.
func (m MsgProvision) Route() string {
return sdk.MsgTypeURL(&m)
return RouterKey

Check warning on line 78 in x/fbridge/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/types/msgs.go#L77-L78

Added lines #L77 - L78 were not covered by tests
}

func (m MsgHoldTransfer) ValidateBasic() error { return nil }
Expand All @@ -86,7 +96,7 @@ func (m MsgHoldTransfer) Type() string {

// Route implements the LegacyMsg.Route method.
func (m MsgHoldTransfer) Route() string {
return sdk.MsgTypeURL(&m)
return RouterKey

Check warning on line 99 in x/fbridge/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/types/msgs.go#L98-L99

Added lines #L98 - L99 were not covered by tests
}

func (m MsgReleaseTransfer) ValidateBasic() error { return nil }
Expand All @@ -107,7 +117,7 @@ func (m MsgReleaseTransfer) Type() string {

// Route implements the LegacyMsg.Route method.
func (m MsgReleaseTransfer) Route() string {
return sdk.MsgTypeURL(&m)
return RouterKey

Check warning on line 120 in x/fbridge/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/types/msgs.go#L119-L120

Added lines #L119 - L120 were not covered by tests
}

func (m MsgRemoveProvision) ValidateBasic() error { return nil }
Expand All @@ -128,7 +138,7 @@ func (m MsgRemoveProvision) Type() string {

// Route implements the LegacyMsg.Route method.
func (m MsgRemoveProvision) Route() string {
return sdk.MsgTypeURL(&m)
return RouterKey

Check warning on line 141 in x/fbridge/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/types/msgs.go#L140-L141

Added lines #L140 - L141 were not covered by tests
}

func (m MsgClaimBatch) ValidateBasic() error { return nil }
Expand All @@ -149,7 +159,7 @@ func (m MsgClaimBatch) Type() string {

// Route implements the LegacyMsg.Route method.
func (m MsgClaimBatch) Route() string {
return sdk.MsgTypeURL(&m)
return RouterKey

Check warning on line 162 in x/fbridge/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/types/msgs.go#L161-L162

Added lines #L161 - L162 were not covered by tests
}

func (m MsgClaim) ValidateBasic() error { return nil }
Expand All @@ -170,7 +180,7 @@ func (m MsgClaim) Type() string {

// Route implements the LegacyMsg.Route method.
func (m MsgClaim) Route() string {
return sdk.MsgTypeURL(&m)
return RouterKey

Check warning on line 183 in x/fbridge/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/types/msgs.go#L182-L183

Added lines #L182 - L183 were not covered by tests
}

func (m MsgSuggestRole) ValidateBasic() error { return nil }
Expand All @@ -191,7 +201,7 @@ func (m MsgSuggestRole) Type() string {

// Route implements the LegacyMsg.Route method.
func (m MsgSuggestRole) Route() string {
return sdk.MsgTypeURL(&m)
return RouterKey
}

func (m MsgAddVoteForRole) ValidateBasic() error { return nil }
Expand All @@ -212,7 +222,7 @@ func (m MsgAddVoteForRole) Type() string {

// Route implements the LegacyMsg.Route method.
func (m MsgAddVoteForRole) Route() string {
return sdk.MsgTypeURL(&m)
return RouterKey
}

func (m MsgSetBridgeStatus) ValidateBasic() error { return nil }
Expand All @@ -233,5 +243,5 @@ func (m MsgSetBridgeStatus) Type() string {

// Route implements the LegacyMsg.Route method.
func (m MsgSetBridgeStatus) Route() string {
return sdk.MsgTypeURL(&m)
return RouterKey
}
97 changes: 97 additions & 0 deletions x/fbridge/types/msgs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package types_test

import (
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/x/auth/legacy/legacytx"
fbridgetypes "github.com/Finschia/finschia-sdk/x/fbridge/types"
)

func TestAminoJSON(t *testing.T) {
tx := legacytx.StdTx{}

addrs := []string{
"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze",
"link1aydj7vdxljxq7cn2dlkrhrcwf5py8dnuqp32qd",
}
toAddr := "0xf7bAc63fc7CEaCf0589F25454Ecf5C2ce904997c"

testCase := map[string]struct {
msg legacytx.LegacyMsg
expectedType string
expected string
}{
"MsgUpdateParam": {
&fbridgetypes.MsgUpdateParams{
Authority: addrs[0],
Params: fbridgetypes.Params{
OperatorTrustLevel: fbridgetypes.Fraction{
Numerator: uint64(2),
Denominator: uint64(3),
},
GuardianTrustLevel: fbridgetypes.Fraction{
Numerator: uint64(2),
Denominator: uint64(3),
},
JudgeTrustLevel: fbridgetypes.Fraction{
Numerator: uint64(2),
Denominator: uint64(3),
},
TimelockPeriod: uint64(86400000000000),
ProposalPeriod: uint64(3600000000000),
TargetDenom: "kaia",
},
},
"/lbm.fbridge.v1.MsgUpdateParams",
"{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/fbridge/MsgUpdateParams\",\"value\":{\"authority\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\",\"params\":{\"guardian_trust_level\":{\"denominator\":\"3\",\"numerator\":\"2\"},\"judge_trust_level\":{\"denominator\":\"3\",\"numerator\":\"2\"},\"operator_trust_level\":{\"denominator\":\"3\",\"numerator\":\"2\"},\"proposal_period\":\"3600000000000\",\"target_denom\":\"kaia\",\"timelock_period\":\"86400000000000\"}}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}",
},
"MsgTransfer": {
&fbridgetypes.MsgTransfer{
Sender: addrs[0],
Receiver: toAddr,
Amount: sdk.NewInt(1000000),
},
"/lbm.fbridge.v1.MsgTransfer",
"{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgTransfer\",\"value\":{\"amount\":\"1000000\",\"receiver\":\"0xf7bAc63fc7CEaCf0589F25454Ecf5C2ce904997c\",\"sender\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\"}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}",
},
"MsgSuggestRole": {
&fbridgetypes.MsgSuggestRole{
From: addrs[0],
Target: addrs[1],
Role: fbridgetypes.RoleGuardian,
},
"/lbm.fbridge.v1.MsgSuggestRole",
"{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgSuggestRole\",\"value\":{\"from\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\",\"role\":1,\"target\":\"link1aydj7vdxljxq7cn2dlkrhrcwf5py8dnuqp32qd\"}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}",
},
"MsgAddVoteForRole": {
&fbridgetypes.MsgAddVoteForRole{
From: addrs[0],
ProposalId: 0,
Option: fbridgetypes.OptionYes,
},
"/lbm.fbridge.v1.MsgAddVoteForRole",
"{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgAddVoteForRole\",\"value\":{\"from\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\",\"option\":1}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}",
},
"MsgSetBridgeStatus": {
&fbridgetypes.MsgSetBridgeStatus{
Guardian: addrs[0],
Status: fbridgetypes.StatusActive,
},
"/lbm.fbridge.v1.MsgSetBridgeStatus",
"{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgSetBridgeStatus\",\"value\":{\"guardian\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\",\"status\":1}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}",
},
}

for name, tc := range testCase {
tc := tc
t.Run(name, func(t *testing.T) {
tx.Msgs = []sdk.Msg{tc.msg}
require.Equal(t, fbridgetypes.RouterKey, tc.msg.Route())
require.Equal(t, tc.expectedType, tc.msg.Type())
require.Equal(t, tc.expected, string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{tc.msg}, "memo")))
})
}
}
2 changes: 1 addition & 1 deletion x/fswap/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const (
// StoreKey defines the primary module store key
StoreKey = ModuleName

// RouterKey is the message route for slashing
// RouterKey is the message route for fswap
RouterKey = ModuleName

// QuerierRoute defines the module's query routing key
Expand Down
6 changes: 3 additions & 3 deletions x/fswap/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (m *MsgSwap) Type() string {

// Route implements the LegacyMsg.Route method.
func (m *MsgSwap) Route() string {
return sdk.MsgTypeURL(m)
return RouterKey
}

func (m *MsgSwap) GetSignerBytes() []byte {
Expand Down Expand Up @@ -96,7 +96,7 @@ func (m *MsgSwapAll) Type() string {

// Route implements the LegacyMsg.Route method.
func (m *MsgSwapAll) Route() string {
return sdk.MsgTypeURL(m)
return RouterKey
}

// GetSignBytes implements the LegacyMsg.GetSignBytes method.
Expand Down Expand Up @@ -134,7 +134,7 @@ func (m *MsgSetSwap) Type() string {

// Route implements the LegacyMsg.Route method.
func (m *MsgSetSwap) Route() string {
return sdk.MsgTypeURL(m)
return RouterKey
}

// GetSignBytes implements the LegacyMsg.GetSignBytes method.
Expand Down
85 changes: 85 additions & 0 deletions x/fswap/types/msgs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package types_test

import (
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/x/auth/legacy/legacytx"
banktypes "github.com/Finschia/finschia-sdk/x/bank/types"
fswaptypes "github.com/Finschia/finschia-sdk/x/fswap/types"
)

func TestAminoJSON(t *testing.T) {
tx := legacytx.StdTx{}

sender := "link15sdc7wdajsps42fky3j6mnvr4tj9fv6w3hkqkj"

swapRate, _ := sdk.NewDecFromStr("148.079656")

testCase := map[string]struct {
msg legacytx.LegacyMsg
expectedType string
expected string
}{
"MsgSwap": {
&fswaptypes.MsgSwap{
FromAddress: sender,
FromCoinAmount: sdk.Coin{
Denom: "cony",
Amount: sdk.NewInt(100000),
},
ToDenom: "pdt",
},
"/lbm.fswap.v1.MsgSwap",
"{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgSwap\",\"value\":{\"from_address\":\"link15sdc7wdajsps42fky3j6mnvr4tj9fv6w3hkqkj\",\"from_coin_amount\":{\"amount\":\"100000\",\"denom\":\"cony\"},\"to_denom\":\"pdt\"}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}",
},
"MsgSwapAll": {
&fswaptypes.MsgSwapAll{
FromAddress: sender,
FromDenom: "cony",
ToDenom: "pdt",
},
"/lbm.fswap.v1.MsgSwapAll",
"{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgSwapAll\",\"value\":{\"from_address\":\"link15sdc7wdajsps42fky3j6mnvr4tj9fv6w3hkqkj\",\"from_denom\":\"cony\",\"to_denom\":\"pdt\"}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}",
},
"MsgSetSwap": {
&fswaptypes.MsgSetSwap{
Authority: sender,
Swap: fswaptypes.Swap{
FromDenom: "cony",
ToDenom: "pdt",
AmountCapForToDenom: sdk.NewInt(1000000000000000),
SwapRate: swapRate,
},
ToDenomMetadata: banktypes.Metadata{
Description: "test",
DenomUnits: []*banktypes.DenomUnit{
{
Denom: "kaia",
Exponent: 0,
Aliases: nil,
},
},
Base: "kaia",
Display: "KAIA",
Name: "Kaia",
Symbol: "KAIA",
},
},
"/lbm.fswap.v1.MsgSetSwap",
"{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgSetSwap\",\"value\":{\"authority\":\"link15sdc7wdajsps42fky3j6mnvr4tj9fv6w3hkqkj\",\"swap\":{\"amount_cap_for_to_denom\":\"1000000000000000\",\"from_denom\":\"cony\",\"swap_rate\":\"148.079656000000000000\",\"to_denom\":\"pdt\"},\"to_denom_metadata\":{\"base\":\"kaia\",\"denom_units\":[{\"denom\":\"kaia\"}],\"description\":\"test\",\"display\":\"KAIA\",\"name\":\"Kaia\",\"symbol\":\"KAIA\"}}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}",
},
}

for name, tc := range testCase {
tc := tc
t.Run(name, func(t *testing.T) {
tx.Msgs = []sdk.Msg{tc.msg}
require.Equal(t, fswaptypes.RouterKey, tc.msg.Route())
require.Equal(t, tc.expectedType, tc.msg.Type())
require.Equal(t, tc.expected, string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{tc.msg}, "memo")))
})
}
}

0 comments on commit aac46c7

Please sign in to comment.