-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wasm bindings for evm support (#477)
- Loading branch information
Showing
6 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,8 @@ | ||
package bindings | ||
|
||
import storagetypes "github.com/jackalLabs/canine-chain/v4/x/storage/types" | ||
|
||
type JackalMsg struct { | ||
/// Contracts can make Files | ||
PostFile *storagetypes.MsgPostFile `json:"post_file,omitempty"` | ||
} |
This file contains 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,92 @@ | ||
package wasmbinding | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
wasmvmtypes "github.com/CosmWasm/wasmvm/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/jackalLabs/canine-chain/v4/wasmbinding/bindings" | ||
filetreekeeper "github.com/jackalLabs/canine-chain/v4/x/filetree/keeper" | ||
|
||
storagekeeper "github.com/jackalLabs/canine-chain/v4/x/storage/keeper" | ||
storagetypes "github.com/jackalLabs/canine-chain/v4/x/storage/types" | ||
) | ||
|
||
// CustomMessageDecorator returns decorator for custom CosmWasm bindings messages | ||
func CustomMessageDecorator(filetree *filetreekeeper.Keeper, storage *storagekeeper.Keeper) func(wasmkeeper.Messenger) wasmkeeper.Messenger { | ||
return func(old wasmkeeper.Messenger) wasmkeeper.Messenger { | ||
return &CustomMessenger{ | ||
wrapped: old, | ||
filetree: filetree, | ||
storage: storage, | ||
} | ||
} | ||
} | ||
|
||
type CustomMessenger struct { | ||
wrapped wasmkeeper.Messenger | ||
filetree *filetreekeeper.Keeper | ||
storage *storagekeeper.Keeper | ||
} | ||
|
||
var _ wasmkeeper.Messenger = (*CustomMessenger)(nil) | ||
|
||
// DispatchMsg executes on the contractMsg. | ||
// NOTE: did we ever use the 'contractIBCPortID' before? | ||
// If we can give each bindings contract--owned by a user--an IBC port ID, perhaps we can use that for authenticating the sender? | ||
|
||
// NOTE: I think the CosmWasm bindings contract can call this multiple times in a single contract.execute() | ||
// This would be great because we wouldn't need to change the chain code too much | ||
func (m *CustomMessenger) DispatchMsg(ctx sdk.Context, contractAddr sdk.AccAddress, contractIBCPortID string, msg wasmvmtypes.CosmosMsg) ([]sdk.Event, [][]byte, error) { | ||
// If the factory contract calls one of its 'child' bindings contracts, the 'sender' field will automatically be filled in with the factory contract's address | ||
|
||
if msg.Custom != nil { | ||
// only handle the happy path where this is really posting files | ||
// leave everything else for the wrapped version | ||
|
||
var contractMsg bindings.JackalMsg | ||
|
||
if err := json.Unmarshal(msg.Custom, &contractMsg); err != nil { | ||
return nil, nil, sdkerrors.Wrap(err, "Failed to unmarshal CosmosMsg enum variant 'Custom' into jackal msg") | ||
} | ||
|
||
if contractMsg.PostFile != nil { | ||
return m.postFile(ctx, contractAddr, contractMsg.PostFile) | ||
} | ||
} | ||
return m.wrapped.DispatchMsg(ctx, contractAddr, contractIBCPortID, msg) | ||
} | ||
|
||
// postFile posts a File to the storage module | ||
func (m *CustomMessenger) postFile(ctx sdk.Context, contractAddr sdk.AccAddress, postFile *storagetypes.MsgPostFile) ([]sdk.Event, [][]byte, error) { | ||
err := PerformPostFile(m.storage, ctx, contractAddr, postFile) | ||
if err != nil { | ||
return nil, nil, sdkerrors.Wrap(err, "perform post file") | ||
} | ||
return nil, nil, nil | ||
} | ||
|
||
func PerformPostFile(s *storagekeeper.Keeper, ctx sdk.Context, contractAddr sdk.AccAddress, postFile *storagetypes.MsgPostFile) error { | ||
if postFile == nil { | ||
return wasmvmtypes.InvalidRequest{Err: "post file null error"} | ||
} | ||
|
||
if postFile.Creator != contractAddr.String() { | ||
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "creator of bindings is not bindings contract address") | ||
} | ||
|
||
if err := postFile.ValidateBasic(); err != nil { | ||
return err | ||
} | ||
|
||
msgServer := storagekeeper.NewMsgServerImpl(*s) | ||
_, err := msgServer.PostFile(sdk.WrapSDKContext(ctx), postFile) | ||
if err != nil { | ||
return sdkerrors.Wrap(err, "post file error from message") | ||
} | ||
|
||
return nil | ||
} |
This file contains 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,25 @@ | ||
package wasmbinding | ||
|
||
import ( | ||
"github.com/CosmWasm/wasmd/x/wasm" | ||
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" | ||
filetreekeeper "github.com/jackalLabs/canine-chain/v4/x/filetree/keeper" | ||
storagekeeper "github.com/jackalLabs/canine-chain/v4/x/storage/keeper" | ||
) | ||
|
||
// WARNING: we're using wasmd from confio and not our fork atm | ||
// wasmd sends custom CosmWasm messages WITHOUT ensuring that the broadcaster is in fact the 'sender' arg of the msg. | ||
|
||
func RegisterCustomPlugins( | ||
// We can add in more keepers kere if needed | ||
filetree *filetreekeeper.Keeper, | ||
storage *storagekeeper.Keeper, | ||
) []wasmkeeper.Option { | ||
messengerDecoratorOpt := wasmkeeper.WithMessageHandlerDecorator( | ||
CustomMessageDecorator(filetree, storage), | ||
) | ||
|
||
return []wasm.Option{ | ||
messengerDecoratorOpt, | ||
} | ||
} |