Skip to content

Commit

Permalink
KIP-87:Ethereum RPC Abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
kobs30 committed Dec 20, 2024
1 parent ae084bf commit f5001d5
Show file tree
Hide file tree
Showing 24 changed files with 3,108 additions and 2 deletions.
13 changes: 11 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"github.com/KiraCore/sekai/x/distributor"
distributorkeeper "github.com/KiraCore/sekai/x/distributor/keeper"
distributortypes "github.com/KiraCore/sekai/x/distributor/types"
"github.com/KiraCore/sekai/x/ethereum"
ethereumkeeper "github.com/KiraCore/sekai/x/ethereum/keeper"
ethereumtypes "github.com/KiraCore/sekai/x/ethereum/types"
"github.com/KiraCore/sekai/x/evidence"
evidencekeeper "github.com/KiraCore/sekai/x/evidence/keeper"
evidencetypes "github.com/KiraCore/sekai/x/evidence/types"
Expand Down Expand Up @@ -128,6 +131,7 @@ var (
collectives.AppModuleBasic{},
layer2.AppModuleBasic{},
consensus.AppModuleBasic{},
ethereum.AppModuleBasic{},
)

// module account permissions
Expand Down Expand Up @@ -184,6 +188,7 @@ type SekaiApp struct {
CollectivesKeeper collectiveskeeper.Keeper
Layer2Keeper layer2keeper.Keeper
ConsensusParamsKeeper consensusparamkeeper.Keeper
EthereumKeeper ethereumkeeper.Keeper

// Module Manager
mm *module.Manager
Expand Down Expand Up @@ -237,6 +242,7 @@ func NewInitApp(
collectivestypes.ModuleName,
layer2types.StoreKey,
consensusparamtypes.StoreKey,
ethereumtypes.StoreKey,
)
tKeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)

Expand Down Expand Up @@ -364,6 +370,7 @@ func NewInitApp(
app.DistrKeeper,
app.TokensKeeper,
)
app.EthereumKeeper = ethereumkeeper.NewKeeper(keys[ethereumtypes.StoreKey], appCodec, app.CustomGovKeeper, app.BankKeeper)

proposalRouter := govtypes.NewProposalRouter(
[]govtypes.ProposalHandler{
Expand Down Expand Up @@ -439,6 +446,7 @@ func NewInitApp(
collectives.NewAppModule(app.CollectivesKeeper),
layer2.NewAppModule(app.Layer2Keeper),
consensus.NewAppModule(appCodec, app.ConsensusParamsKeeper),
ethereum.NewAppModule(app.EthereumKeeper, app.CustomGovKeeper, app.BankKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand All @@ -451,8 +459,7 @@ func NewInitApp(
evidencetypes.ModuleName, stakingtypes.ModuleName,
spendingtypes.ModuleName, ubitypes.ModuleName,
distributortypes.ModuleName, multistakingtypes.ModuleName, custodytypes.ModuleName,
baskettypes.ModuleName,
distributortypes.ModuleName, multistakingtypes.ModuleName, custodytypes.ModuleName,
ethereumtypes.ModuleName,
baskettypes.ModuleName,
collectivestypes.ModuleName,
layer2types.ModuleName,
Expand All @@ -467,6 +474,7 @@ func NewInitApp(
feeprocessingtypes.ModuleName,
spendingtypes.ModuleName, ubitypes.ModuleName,
distributortypes.ModuleName, multistakingtypes.ModuleName, custodytypes.ModuleName,
ethereumtypes.ModuleName,
baskettypes.ModuleName,
collectivestypes.ModuleName,
layer2types.ModuleName,
Expand Down Expand Up @@ -495,6 +503,7 @@ func NewInitApp(
paramstypes.ModuleName,
distributortypes.ModuleName,
custodytypes.ModuleName,
ethereumtypes.ModuleName,
multistakingtypes.ModuleName,
baskettypes.ModuleName,
collectivestypes.ModuleName,
Expand Down
21 changes: 21 additions & 0 deletions proto/kira/ethereum/ethereum.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";
package kira.ethereum;

import "gogoproto/gogo.proto";
import "google/protobuf/any.proto";

option go_package = "github.com/KiraCore/sekai/x/ethereum/types";

message EVMTx {
string From = 1;
string To = 2;
string Value = 3;
string Gas = 4;
string GasPrice = 5;
string Nonce = 6;
string Data = 7;
int64 ChainId = 8;
string V = 9;
string R = 10;
string S = 11;
}
8 changes: 8 additions & 0 deletions proto/kira/ethereum/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";
package kira.ethereum;

import "kira/ethereum/ethereum.proto";

option go_package = "github.com/KiraCore/sekai/x/ethereum/types";

message GenesisState { }
26 changes: 26 additions & 0 deletions proto/kira/ethereum/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
syntax = "proto3";
package kira.ethereum;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "kira/ethereum/ethereum.proto";
import "kira/ethereum/tx.proto";

option go_package = "github.com/KiraCore/sekai/x/ethereum/types";

service Query {
rpc RelayByAddress (RelayByAddressRequest) returns (RelayByAddressResponse) {
option (google.api.http).get = "/kira/ethereum/relay/{addr}";
}
}

message RelayByAddressRequest {
bytes addr = 1 [
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress",
(gogoproto.moretags) = "yaml:\"addr\""
];
}

message RelayByAddressResponse {
MsgRelay msg_relay = 1;
}
31 changes: 31 additions & 0 deletions proto/kira/ethereum/tx.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
syntax = "proto3";
package kira.ethereum;

import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";
import "cosmos/bank/v1beta1/bank.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/msg/v1/msg.proto";
import "amino/amino.proto";
import "kira/ethereum/ethereum.proto";

option go_package = "github.com/KiraCore/sekai/x/ethereum/types";

// Msg defines the ethereum Msg service.
service Msg {
rpc Relay(MsgRelay) returns (MsgRelayResponse);
}

message MsgRelay {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

bytes address = 1 [
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress",
(gogoproto.moretags) = "yaml:\"address\""
];

string data = 2;
}

message MsgRelayResponse {}
16 changes: 16 additions & 0 deletions x/ethereum/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cli

import (
"github.com/KiraCore/sekai/x/ethereum/types"
"github.com/spf13/cobra"
)

// NewQueryCmd returns a root CLI command handler for all x/ethereum transaction commands.
func NewQueryCmd() *cobra.Command {
queryCmd := &cobra.Command{
Use: types.RouterKey,
Short: "query commands for the ethereum module",
}

return queryCmd
}
50 changes: 50 additions & 0 deletions x/ethereum/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cli

import (
"github.com/KiraCore/sekai/x/ethereum/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/spf13/cobra"
)

// NewTxCmd returns a root CLI command handler for all x/ethereum transaction commands.
func NewTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "ethereum sub commands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

txCmd.AddCommand(GetTxRelay())

return txCmd
}

func GetTxRelay() *cobra.Command {
cmd := &cobra.Command{
Use: "relay",
Short: "New relay from ETHEREUM to COSMOS",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgRelay(
clientCtx.FromAddress,
args[0],
)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)
cmd.MarkFlagRequired(flags.FlagFrom)

return cmd
}
27 changes: 27 additions & 0 deletions x/ethereum/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ethereum

import (
"github.com/KiraCore/sekai/x/ethereum/keeper"
"github.com/KiraCore/sekai/x/ethereum/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
)

// NewHandler returns new instance of handler
func NewHandler(ck keeper.Keeper, cgk types.CustomGovKeeper, bk types.BankKeeper) sdk.Handler {
msgServer := keeper.NewMsgServerImpl(ck, cgk, bk)

return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())

switch msg := msg.(type) {
case *types.MsgRelay:
{
res, err := msgServer.Relay(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
}
default:
return nil, errors.Wrapf(errors.ErrUnknownRequest, "unrecognized %s message type: %T", types.ModuleName, msg)
}
}
}
28 changes: 28 additions & 0 deletions x/ethereum/keeper/ethereum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package keeper

import (
"github.com/KiraCore/sekai/x/ethereum/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k Keeper) SetRelay(ctx sdk.Context, record *types.MsgRelay) {
store := ctx.KVStore(k.storeKey)
key := append([]byte(types.PrefixKeyRelay), record.Address...)

store.Set(key, k.cdc.MustMarshal(record))
}

func (k Keeper) GetRelayByAddress(ctx sdk.Context, address sdk.AccAddress) *types.MsgRelay {
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.PrefixKeyRelay))
bz := prefixStore.Get(address)

if bz == nil {
return nil
}

info := new(types.MsgRelay)
k.cdc.MustUnmarshal(bz, info)

return info
}
25 changes: 25 additions & 0 deletions x/ethereum/keeper/grpc_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package keeper

import (
"context"
"github.com/KiraCore/sekai/x/ethereum/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type Querier struct {
keeper Keeper
}

func NewQuerier(keeper Keeper) types.QueryServer {
return &Querier{keeper: keeper}
}

var _ types.QueryServer = Querier{}

func (q Querier) RelayByAddress(goCtx context.Context, request *types.RelayByAddressRequest) (*types.RelayByAddressResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

return &types.RelayByAddressResponse{
MsgRelay: q.keeper.GetRelayByAddress(ctx, request.Addr),
}, nil
}
37 changes: 37 additions & 0 deletions x/ethereum/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package keeper

import (
appparams "github.com/KiraCore/sekai/app/params"
"github.com/KiraCore/sekai/x/ethereum/types"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Keeper is for managing token module
type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
gk types.CustomGovKeeper
bk types.BankKeeper
}

// NewKeeper returns instance of a keeper
func NewKeeper(storeKey storetypes.StoreKey, cdc codec.BinaryCodec, gk types.CustomGovKeeper, bk types.BankKeeper) Keeper {
return Keeper{
cdc: cdc,
storeKey: storeKey,
gk: gk,
bk: bk,
}
}

// DefaultDenom returns the denom that is basically used for fee payment
func (k Keeper) DefaultDenom(ctx sdk.Context) string {
return appparams.DefaultDenom
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
Loading

0 comments on commit f5001d5

Please sign in to comment.