-
Notifications
You must be signed in to change notification settings - Fork 370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(evmutil): register MsgConvertCosmosCoinToERC20 on amino #1599
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,17 @@ package app | |
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"sort" | ||
"testing" | ||
"time" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx" | ||
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" | ||
evmtypes "github.com/evmos/ethermint/x/evm/types" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
|
@@ -63,6 +69,64 @@ func TestExport(t *testing.T) { | |
assert.Len(t, exportedApp.Validators, 1) // no validators set in default genesis | ||
} | ||
|
||
// TestLegacyMsgAreAminoRegistered checks if all known msg types are registered on the app's amino codec. | ||
// It doesn't check if they are registered on the module codecs used for signature checking. | ||
func TestLegacyMsgAreAminoRegistered(t *testing.T) { | ||
tApp := NewTestApp() | ||
|
||
lcdc := tApp.LegacyAmino() | ||
|
||
// Use the proto codec as the canonical list of msg types. | ||
protoCodec := tApp.AppCodec().(*codec.ProtoCodec) | ||
protoRegisteredMsgs := protoCodec.InterfaceRegistry().ListImplementations(sdk.MsgInterfaceProtoName) | ||
|
||
for i, msgName := range protoRegisteredMsgs { | ||
// Skip msgs from dependencies that were never amino registered. | ||
if msgName == sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'd love if this was a list called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agreed, it's a bit confusing, changed to |
||
msgName == sdk.MsgTypeURL(&vestingtypes.MsgCreatePeriodicVestingAccount{}) { | ||
continue | ||
} | ||
|
||
// Create an encoded json msg, then unmarshal it to instantiate the msg type. | ||
jsonMsg := []byte(fmt.Sprintf(`{"@type": "%s"}`, msgName)) | ||
|
||
var msg sdk.Msg | ||
err := protoCodec.UnmarshalInterfaceJSON(jsonMsg, &msg) | ||
require.NoError(t, err) | ||
|
||
// Only check legacy msgs for amino registration. | ||
// Only legacy msg can be signed with amino. | ||
_, ok := msg.(legacytx.LegacyMsg) | ||
if !ok { | ||
continue | ||
} | ||
|
||
// Check the msg is registered in amino by checking a repeat registration call panics. | ||
panicValue, ok := catchPanic(func() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. super clever way to check the messages are registered 👍 🥇 nit: you can probably use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, I started with |
||
lcdc.RegisterConcrete(interface{}(msg), fmt.Sprintf("aUniqueRegistrationName%d", i), nil) | ||
}) | ||
assert.True(t, ok, "registration did not panic, msg %s is not registered in amino", msgName) | ||
if ok { | ||
require.IsTypef(t, "", panicValue, "msg %s amino registration panicked with unexpected type", msgName) | ||
aminoErrMsgPrefix := "TypeInfo already exists for" | ||
require.Containsf(t, panicValue, aminoErrMsgPrefix, "msg %s amino registration panicked for unexpected reason", msgName) | ||
} | ||
} | ||
} | ||
|
||
// catchPanic returns the panic value of the passed function. The second return indicates if the function panicked. | ||
func catchPanic(f func()) (panicValue interface{}, didPanic bool) { | ||
didPanic = true | ||
|
||
defer func() { | ||
panicValue = recover() | ||
}() | ||
|
||
f() | ||
didPanic = false | ||
return | ||
} | ||
|
||
// unmarshalJSONKeys extracts keys from the top level of a json blob. | ||
func unmarshalJSONKeys(jsonBytes []byte) ([]string, error) { | ||
var jsonMap map[string]json.RawMessage | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is refers to the
ModuleCdc
right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I was thinking we could add tests to the modules to cover them. But if we replace them with a global codec as they've done in v0.46 (cosmos/cosmos-sdk#11240) we could run this test on that global codec.