-
Notifications
You must be signed in to change notification settings - Fork 98
feat: add genmsg module #387
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
62d8fb9
add genesis messages skeleton and functionality
fdymylja cedd6bf
add testing and fix some things
fdymylja 5f05577
add integration test
fdymylja a5155f1
chore lint
fdymylja 3268fc0
Merge remote-tracking branch 'origin/main' into fdymylja/genesis_mess…
fdymylja 5954745
Merge branch 'main' into fdymylja/genesis_messages
fdymylja 6f3bf75
fmt
fdymylja ee46545
add adr
fdymylja a09196d
lint
fdymylja 8a083bb
fix go.sum
fdymylja 4dc1d59
chore: CHANGELOG.md
fdymylja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| syntax = "proto3"; | ||
| package archway.genmsg.v1; | ||
|
|
||
| import "google/protobuf/any.proto"; | ||
|
|
||
|
|
||
| option go_package = "github.com/archway-network/archway/x/genmsg/v1"; | ||
|
|
||
| // GenesisState represents the messages to be processed during genesis by the genmsg module. | ||
| message GenesisState { | ||
| repeated google.protobuf.Any messages = 1; | ||
| } |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package genmsg | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
|
|
||
| v1 "github.com/archway-network/archway/x/genmsg/v1" | ||
| "github.com/cosmos/cosmos-sdk/codec" | ||
| "github.com/cosmos/cosmos-sdk/codec/types" | ||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
| ) | ||
|
|
||
| func anyToMsg(ir types.InterfaceRegistry, anyMsg *types.Any) (sdk.Msg, error) { | ||
| var sdkMsg sdk.Msg | ||
| err := ir.UnpackAny(anyMsg, &sdkMsg) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err = sdkMsg.ValidateBasic(); err != nil { | ||
| return nil, err | ||
| } | ||
| return sdkMsg, nil | ||
| } | ||
|
|
||
| func validateGenesis(cdc codec.JSONCodec, genesis *v1.GenesisState) error { | ||
| interfaceRegistryProvider, ok := cdc.(interface { | ||
| InterfaceRegistry() types.InterfaceRegistry | ||
| }) | ||
| if !ok { | ||
| return fmt.Errorf("codec does not implement InterfaceRegistry") | ||
| } | ||
| interfaceRegistry := interfaceRegistryProvider.InterfaceRegistry() | ||
| // check if all messages are known by the codec | ||
| for i, anyMsg := range genesis.Messages { | ||
| if _, err := anyToMsg(interfaceRegistry, anyMsg); err != nil { | ||
| return fmt.Errorf("at index %d: %w", i, err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func initGenesis(context sdk.Context, cdc codec.JSONCodec, router MessageRouter, genesis *v1.GenesisState) error { | ||
| interfaceRegistryProvider, ok := cdc.(interface { | ||
| InterfaceRegistry() types.InterfaceRegistry | ||
| }) | ||
| if !ok { | ||
| return fmt.Errorf("codec does not implement InterfaceRegistry") | ||
| } | ||
| interfaceRegistry := interfaceRegistryProvider.InterfaceRegistry() | ||
|
|
||
| // execute all messages in order | ||
| for i, anyMsg := range genesis.Messages { | ||
| msg, err := anyToMsg(interfaceRegistry, anyMsg) | ||
| if err != nil { | ||
| return fmt.Errorf("at index %d: message decoding: %w", i, err) | ||
| } | ||
| handler := router.Handler(msg) | ||
| if handler == nil { | ||
| return fmt.Errorf("at index %d: no handler for message %T %s", i, msg, msg) | ||
| } | ||
| resp, err := handler(context, msg) | ||
| if err != nil { | ||
| return fmt.Errorf("at index %d: message processing: %w", i, err) | ||
| } | ||
| log.Printf("message %d processed %s: %s", i, msg, resp.String()) | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package genmsg | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| v1 "github.com/archway-network/archway/x/genmsg/v1" | ||
| "github.com/cosmos/cosmos-sdk/baseapp" | ||
| "github.com/cosmos/cosmos-sdk/codec" | ||
| "github.com/cosmos/cosmos-sdk/codec/types" | ||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
| banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
| "github.com/gogo/protobuf/proto" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type mockRouter struct { | ||
| handler func(msg sdk.Msg) baseapp.MsgServiceHandler | ||
| } | ||
|
|
||
| func (m mockRouter) Handler(msg sdk.Msg) baseapp.MsgServiceHandler { return m.handler(msg) } | ||
|
|
||
| func makeCodec(_ *testing.T) codec.JSONCodec { | ||
| ir := types.NewInterfaceRegistry() | ||
| cdc := codec.NewProtoCodec(ir) | ||
| ir.RegisterInterface(sdk.MsgInterfaceProtoName, (*sdk.Msg)(nil), &banktypes.MsgSend{}) | ||
| return cdc | ||
| } | ||
|
|
||
| func newGenesisFromMsgs(t *testing.T, cdc codec.JSONCodec, msgs ...proto.Message) *v1.GenesisState { | ||
| genesis := new(v1.GenesisState) | ||
| for _, msg := range msgs { | ||
| anyProto, err := types.NewAnyWithValue(msg) | ||
| require.NoError(t, err) | ||
| genesis.Messages = append(genesis.Messages, anyProto) | ||
| } | ||
| genesisJSON, err := cdc.MarshalJSON(genesis) | ||
| require.NoError(t, err) | ||
| genesis = new(v1.GenesisState) | ||
| require.NoError(t, cdc.UnmarshalJSON(genesisJSON, genesis)) | ||
| return genesis | ||
| } | ||
|
|
||
| func Test_initGenesis(t *testing.T) { | ||
| cdc := makeCodec(t) | ||
| ctx := sdk.Context{} | ||
|
|
||
| t.Run("works - no msgs", func(t *testing.T) { | ||
| r := mockRouter{func(msg sdk.Msg) baseapp.MsgServiceHandler { | ||
| return func(ctx sdk.Context, req sdk.Msg) (*sdk.Result, error) { | ||
| return &sdk.Result{}, nil | ||
| } | ||
| }} | ||
|
|
||
| err := initGenesis(ctx, cdc, r, newGenesisFromMsgs(t, cdc)) | ||
| require.NoError(t, err) | ||
| }) | ||
|
|
||
| t.Run("works - with message", func(t *testing.T) { | ||
| called := false | ||
| r := mockRouter{func(msg sdk.Msg) baseapp.MsgServiceHandler { | ||
| return func(ctx sdk.Context, req sdk.Msg) (*sdk.Result, error) { | ||
| called = true | ||
| return &sdk.Result{}, nil | ||
| } | ||
| }} | ||
|
|
||
| err := initGenesis(ctx, cdc, r, newGenesisFromMsgs(t, cdc, &banktypes.MsgSend{ | ||
| FromAddress: sdk.AccAddress("a").String(), | ||
| ToAddress: sdk.AccAddress("b").String(), | ||
| Amount: sdk.NewCoins(sdk.NewInt64Coin("test", 1000)), | ||
| })) | ||
| require.NoError(t, err) | ||
| require.True(t, called) | ||
| }) | ||
|
|
||
| t.Run("fails - handler is nil", func(t *testing.T) { | ||
| r := mockRouter{func(msg sdk.Msg) baseapp.MsgServiceHandler { | ||
| return nil | ||
| }} | ||
|
|
||
| err := initGenesis(ctx, cdc, r, newGenesisFromMsgs(t, cdc, &banktypes.MsgSend{ | ||
| FromAddress: sdk.AccAddress("a").String(), | ||
| ToAddress: sdk.AccAddress("b").String(), | ||
| Amount: sdk.NewCoins(sdk.NewInt64Coin("test", 1000)), | ||
| })) | ||
| require.Error(t, err) | ||
| }) | ||
| } | ||
|
|
||
| func Test_validateGenesis(t *testing.T) { | ||
| cdc := makeCodec(t) | ||
| t.Run("works - empty", func(t *testing.T) { | ||
| err := validateGenesis(cdc, &v1.GenesisState{}) | ||
| require.NoError(t, err) | ||
| }) | ||
| t.Run("works - with messages", func(t *testing.T) { | ||
| genesis := newGenesisFromMsgs(t, cdc, &banktypes.MsgSend{ | ||
| FromAddress: sdk.AccAddress("sender").String(), | ||
| ToAddress: sdk.AccAddress("receiver").String(), | ||
| Amount: sdk.NewCoins(sdk.NewInt64Coin("test", 1000)), | ||
| }) | ||
| err := validateGenesis(cdc, genesis) | ||
| require.NoError(t, err) | ||
| }) | ||
| t.Run("fails - validate basic", func(t *testing.T) { | ||
| genesis := newGenesisFromMsgs(t, cdc, &banktypes.MsgSend{}) | ||
| err := validateGenesis(cdc, genesis) | ||
| require.Error(t, err) | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.