Skip to content

Commit

Permalink
implement cosmos query feature from evm
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Mar 29, 2024
1 parent cb25119 commit 3161196
Show file tree
Hide file tree
Showing 22 changed files with 365 additions and 71 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ func NewMinitiaApp(
app.GRPCQueryRouter(),
authorityAddr,
evmConfig,
evmtypes.DefaultQueryCosmosWhitelist(),
)
*erc20Keeper = *app.EVMKeeper.ERC20Keeper().(*evmkeeper.ERC20Keeper)

Expand Down
1 change: 1 addition & 0 deletions app/ibc-hooks/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ func _createTestInput(
queryRouter,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
evmconfig.DefaultEVMConfig(),
evmtypes.DefaultQueryCosmosWhitelist(),
)
evmParams := evmtypes.DefaultParams()
require.NoError(t, evmKeeper.Params.Set(ctx, evmParams))
Expand Down
2 changes: 1 addition & 1 deletion client/docs/statik/statik.go

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions client/docs/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36646,6 +36646,8 @@ paths:
the LOG opcode and stored/indexed by the node.
trace_output:
type: string
error:
type: string
title: |-
QueryCallResponse is the response type for the Query/Call RPC
method
Expand Down Expand Up @@ -36683,7 +36685,7 @@ paths:
title: sender address
contract_addr:
type: string
description: It can be cosmos address or hex encoded address (0x prefixed).
description: It can be cosmos address or hex encoded address.
input:
type: string
title: hex encoded call input
Expand All @@ -36707,7 +36709,6 @@ paths:
properties:
code:
type: string
format: byte
title: |-
QueryCodeResponse is the response type for the Query/Code RPC
method
Expand Down Expand Up @@ -36825,7 +36826,7 @@ paths:
format: byte
parameters:
- name: contract_addr
description: It can be cosmos address or hex encoded address (0x prefixed).
description: It can be cosmos address or hex encoded address.
in: path
required: true
type: string
Expand Down Expand Up @@ -36928,7 +36929,7 @@ paths:
format: byte
parameters:
- name: contract_addr
description: It can be cosmos address or hex encoded address (0x prefixed).
description: It can be cosmos address or hex encoded address.
in: path
required: true
type: string
Expand Down Expand Up @@ -61132,7 +61133,7 @@ definitions:
title: sender address
contract_addr:
type: string
description: It can be cosmos address or hex encoded address (0x prefixed).
description: It can be cosmos address or hex encoded address.
input:
type: string
title: hex encoded call input
Expand Down Expand Up @@ -61172,6 +61173,8 @@ definitions:
the LOG opcode and stored/indexed by the node.
trace_output:
type: string
error:
type: string
title: |-
QueryCallResponse is the response type for the Query/Call RPC
method
Expand All @@ -61180,7 +61183,6 @@ definitions:
properties:
code:
type: string
format: byte
title: |-
QueryCodeResponse is the response type for the Query/Code RPC
method
Expand Down
2 changes: 1 addition & 1 deletion proto/minievm/evm/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ message QueryContractAddrByDenomResponse {
// QueryDenomRequest is the request type for the Query/Denom RPC
// method
message QueryDenomRequest {
// It can be cosmos address or hex encoded address (0x prefixed).
// It can be cosmos address or hex encoded address.
string contract_addr = 1;
}

Expand Down
4 changes: 2 additions & 2 deletions proto/minievm/evm/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ message MsgCreate {
message MsgCreateResponse {
string result = 1;

// hex encoded address (0x prefixed)
// hex encoded address
string contract_addr = 2;
}

Expand All @@ -52,7 +52,7 @@ message MsgCall {
string sender = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// ContractAddr is the contract address to be executed.
// It can be cosmos address or hex encoded address (0x prefixed).
// It can be cosmos address or hex encoded address.
string contract_addr = 2;

// Hex encoded execution input bytes.
Expand Down
2 changes: 2 additions & 0 deletions scripts/protoc-swagger-gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

set -eo pipefail

git config --global url."https://x-access-token:[email protected]/".insteadOf "https://github.com/"

# clone dependency proto files
COSMOS_URL=github.com/cosmos/cosmos-sdk
IBC_URL=github.com/cosmos/ibc-go
Expand Down
5 changes: 3 additions & 2 deletions x/bank/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
"github.com/initia-labs/minievm/x/bank"
custombankkeeper "github.com/initia-labs/minievm/x/bank/keeper"
"github.com/initia-labs/minievm/x/evm"
EVMConfig "github.com/initia-labs/minievm/x/evm/config"
evmconfig "github.com/initia-labs/minievm/x/evm/config"
evmkeeper "github.com/initia-labs/minievm/x/evm/keeper"
evmtypes "github.com/initia-labs/minievm/x/evm/types"
)
Expand Down Expand Up @@ -298,7 +298,8 @@ func _createTestInput(
msgRouter,
queryRouter,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
EVMConfig.DefaultEVMConfig(),
evmconfig.DefaultEVMConfig(),
evmtypes.DefaultQueryCosmosWhitelist(),
)
evmParams := evmtypes.DefaultParams()
require.NoError(t, evmKeeper.Params.Set(ctx, evmParams))
Expand Down
47 changes: 34 additions & 13 deletions x/evm/contracts/i_cosmos/ICosmos.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion x/evm/contracts/i_cosmos/ICosmos.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,15 @@ interface ICosmos {
// ]
// }
//
function execute_cosmos_message(string memory msg) external;
function execute_cosmos(string memory msg) external;

// query a whitelisted cosmos querys.
//
// example)
// path: "/slinky.oracle.v1.Query/GetPrices"
// req: {
// "currency_pair_ids": ["BITCOIN/USD", "ETHEREUM/USD"]
// }
//
function query_cosmos(string memory path, string memory req) external returns (string memory result);
}
10 changes: 8 additions & 2 deletions x/evm/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (

custombankkeeper "github.com/initia-labs/minievm/x/bank/keeper"
"github.com/initia-labs/minievm/x/evm"
EVMConfig "github.com/initia-labs/minievm/x/evm/config"
evmconfig "github.com/initia-labs/minievm/x/evm/config"
evmkeeper "github.com/initia-labs/minievm/x/evm/keeper"
evmtypes "github.com/initia-labs/minievm/x/evm/types"
)
Expand Down Expand Up @@ -261,7 +261,13 @@ func _createTestInput(
msgRouter,
queryRouter,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
EVMConfig.DefaultEVMConfig(),
evmconfig.DefaultEVMConfig(),
evmtypes.QueryCosmosWhitelist{
"/cosmos.bank.v1beta1.Query/Balance": {
Request: &banktypes.QueryBalanceRequest{},
Response: &banktypes.QueryBalanceResponse{},
},
},
)
evmParams := evmtypes.DefaultParams()
require.NoError(t, evmKeeper.Params.Set(ctx, evmParams))
Expand Down
37 changes: 36 additions & 1 deletion x/evm/keeper/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/vm"
Expand Down Expand Up @@ -100,7 +102,7 @@ func Test_ExecuteCosmosMessage(t *testing.T) {
abi, err := i_cosmos.ICosmosMetaData.GetAbi()
require.NoError(t, err)

inputBz, err := abi.Pack("execute_cosmos_message", fmt.Sprintf(`
inputBz, err := abi.Pack("execute_cosmos", fmt.Sprintf(`
{
"@type": "/cosmos.bank.v1beta1.MsgSend",
"from_address": "%s",
Expand All @@ -121,3 +123,36 @@ func Test_ExecuteCosmosMessage(t *testing.T) {
balance := input.BankKeeper.GetBalance(ctx, addr2, "bar")
require.Equal(t, math.NewInt(100), balance.Amount)
}

func Test_QueryCosmosMessage(t *testing.T) {
ctx, input := createDefaultTestInput(t)
_, _, addr := keyPubAddr()
evmAddr := common.BytesToAddress(addr.Bytes())

erc20Keeper, err := keeper.NewERC20Keeper(&input.EVMKeeper)
require.NoError(t, err)

// mint native coin
err = erc20Keeper.MintCoins(ctx, addr, sdk.NewCoins(
sdk.NewCoin("bar", math.NewInt(200)),
))
require.NoError(t, err)

abi, err := i_cosmos.ICosmosMetaData.GetAbi()
require.NoError(t, err)

inputBz, err := abi.Pack("query_cosmos", "/cosmos.bank.v1beta1.Query/Balance", fmt.Sprintf(`{
"address": "%s",
"denom": "bar"
}`, addr))
require.NoError(t, err)

retBz, _, err := input.EVMKeeper.EVMCall(ctx, evmAddr, types.CosmosPrecompileAddress, inputBz)
require.NoError(t, err)

var ret banktypes.QueryBalanceResponse
err = input.EncodingConfig.Codec.UnmarshalJSON(retBz, &ret)
require.NoError(t, err)

require.Equal(t, math.NewInt(200), ret.Balance.Amount)
}
23 changes: 13 additions & 10 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ type Keeper struct {
erc20Keeper types.IERC20Keeper
erc20StoresKeeper types.IERC20StoresKeeper

// grpc server routers
// grpc routers
msgRouter baseapp.MessageRouter
grpcRouter *baseapp.GRPCQueryRouter
grpcRouter types.GRPCRouter

config evmconfig.EVMConfig

Expand All @@ -47,7 +47,8 @@ type Keeper struct {
ERC20DenomsByContractAddr collections.Map[[]byte, string]
ERC20ContractAddrsByDenom collections.Map[string, []byte]

precompiles precompiles
precompiles precompiles
queryCosmosWhitelist types.QueryCosmosWhitelist
}

func NewKeeper(
Expand All @@ -59,16 +60,17 @@ func NewKeeper(
msgRouter baseapp.MessageRouter,
grpcRouter *baseapp.GRPCQueryRouter,
authority string,
EVMConfig evmconfig.EVMConfig,
evmConfig evmconfig.EVMConfig,
queryCosmosWhitelist types.QueryCosmosWhitelist,
) *Keeper {
sb := collections.NewSchemaBuilder(storeService)

if EVMConfig.ContractSimulationGasLimit == 0 {
EVMConfig.ContractSimulationGasLimit = evmconfig.DefaultContractSimulationGasLimit
if evmConfig.ContractSimulationGasLimit == 0 {
evmConfig.ContractSimulationGasLimit = evmconfig.DefaultContractSimulationGasLimit
}

if EVMConfig.ContractQueryGasLimit == 0 {
EVMConfig.ContractQueryGasLimit = evmconfig.DefaultContractQueryGasLimit
if evmConfig.ContractQueryGasLimit == 0 {
evmConfig.ContractQueryGasLimit = evmconfig.DefaultContractQueryGasLimit
}

k := &Keeper{
Expand All @@ -82,7 +84,7 @@ func NewKeeper(
msgRouter: msgRouter,
grpcRouter: grpcRouter,

config: EVMConfig,
config: evmConfig,

Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
VMRoot: collections.NewItem(sb, types.VMRootKey, "vm_root", collections.BytesValue),
Expand All @@ -93,7 +95,8 @@ func NewKeeper(
ERC20DenomsByContractAddr: collections.NewMap(sb, types.ERC20DenomsByContractAddrPrefix, "erc20_denoms_by_contract_addr", collections.BytesKey, collections.StringValue),
ERC20ContractAddrsByDenom: collections.NewMap(sb, types.ERC20ContractAddrsByDenomPrefix, "erc20_contract_addrs_by_denom", collections.StringKey, collections.BytesValue),

precompiles: []precompile{},
precompiles: []precompile{},
queryCosmosWhitelist: queryCosmosWhitelist,
}

// setup schema
Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/precompiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (k *Keeper) loadPrecompiles() error {
return err
}

cosmosPrecompile, err := cosmosprecompile.NewCosmosPrecompile(k.cdc, k.ac, k.accountKeeper)
cosmosPrecompile, err := cosmosprecompile.NewCosmosPrecompile(k.cdc, k.ac, k.accountKeeper, k.grpcRouter, k.queryCosmosWhitelist)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 3161196

Please sign in to comment.