Skip to content

Commit

Permalink
Add query for pointer in registry (#1508)
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen authored Apr 5, 2024
1 parent 0c08270 commit f87a819
Show file tree
Hide file tree
Showing 13 changed files with 884 additions and 108 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ require (
github.com/fzipp/gocyclo v0.5.1 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-critic/go-critic v0.6.3 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
Expand Down Expand Up @@ -172,6 +173,7 @@ require (
github.com/gostaticanalysis/nilerr v0.1.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-bexpr v0.1.10 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 h1:BAIP2Gihuqh
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46/go.mod h1:QNpY22eby74jVhqH4WhDLDwxc/vqsern6pW+u2kbkpc=
github.com/getkin/kin-openapi v0.53.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
github.com/ghemawat/stream v0.0.0-20171120220530-696b145b53b9/go.mod h1:106OIgooyS7OzLDOpUGgm9fA3bQENb/cFSyyBmMoJDs=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand Down Expand Up @@ -765,6 +766,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
github.com/grpc-ecosystem/grpc-gateway v1.12.1/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 h1:/c3QmbOGMGTOumP2iT/rCwB7b0QDGLKzqOmktBjT+Is=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1/go.mod h1:5SN9VR2LTsRFsrEC6FHgRbTWrTHu6tqPeKxEQv15giM=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0=
github.com/guptarohit/asciigraph v0.5.5/go.mod h1:dYl5wwK4gNsnFf9Zp+l06rFiDZ5YtXM6x7SRWZ3KGag=
Expand Down
12 changes: 12 additions & 0 deletions proto/evm/enums.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";
package seiprotocol.seichain.evm;

option go_package = "github.com/sei-protocol/sei-chain/x/evm/types";

enum PointerType {
ERC20 = 0;
ERC721 = 1;
NATIVE = 2;
CW20 = 3;
CW721 = 4;
}
16 changes: 16 additions & 0 deletions proto/evm/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ syntax = "proto3";
package seiprotocol.seichain.evm;

import "google/api/annotations.proto";
import "evm/enums.proto";

option go_package = "github.com/sei-protocol/sei-chain/x/evm/types";

Expand All @@ -18,6 +19,10 @@ service Query {
rpc StaticCall(QueryStaticCallRequest) returns (QueryStaticCallResponse) {
option (google.api.http).get = "/sei-protocol/seichain/evm/static_call";
}

rpc Pointer(QueryPointerRequest) returns (QueryPointerResponse) {
option (google.api.http).get = "/sei-protocol/seichain/evm/pointer";
}
}

message QuerySeiAddressByEVMAddressRequest {
Expand Down Expand Up @@ -46,3 +51,14 @@ message QueryStaticCallRequest {
message QueryStaticCallResponse {
bytes data = 1;
}

message QueryPointerRequest {
PointerType pointer_type = 1;
string pointee = 2;
}

message QueryPointerResponse {
string pointer = 1;
uint32 version = 2;
bool exists = 3;
}
6 changes: 1 addition & 5 deletions proto/evm/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package seiprotocol.seichain.evm;
import "google/protobuf/any.proto";
import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";
import "evm/enums.proto";

option go_package = "github.com/sei-protocol/sei-chain/x/evm/types";

Expand Down Expand Up @@ -55,11 +56,6 @@ message MsgSend {

message MsgSendResponse {}

enum PointerType {
ERC20 = 0;
ERC721 = 1;
}

message MsgRegisterPointer {
string sender = 1;
PointerType pointer_type = 2;
Expand Down
1 change: 1 addition & 0 deletions x/evm/ante/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (gl BasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, n
return next(ctx, tx, simulate)
}

//nolint:deadcode
func validateBlobSidecar(hashes []common.Hash, sidecar *ethtypes.BlobTxSidecar) error {
if len(sidecar.Blobs) != len(hashes) {
return fmt.Errorf("invalid number of %d blobs compared to %d blob hashes", len(sidecar.Blobs), len(hashes))
Expand Down
27 changes: 27 additions & 0 deletions x/evm/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func GetQueryCmd(_ string) *cobra.Command {
cmd.AddCommand(CmdQueryERC721Payload())
cmd.AddCommand(CmdQueryERC20())
cmd.AddCommand(CmdQueryPayload())
cmd.AddCommand(CmdQueryPointer())

return cmd
}
Expand Down Expand Up @@ -339,3 +340,29 @@ func CmdQueryERC721Payload() *cobra.Command {

return cmd
}

func CmdQueryPointer() *cobra.Command {
cmd := &cobra.Command{
Use: "pointer [type] [pointee]",
Short: "get pointer address of the specified type (one of [NATIVE, CW20, CW721, ERC20, ERC721]) and pointee",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.Pointer(context.Background(), &types.QueryPointerRequest{
PointerType: types.PointerType(types.PointerType_value[args[0]]), Pointee: args[1],
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
43 changes: 43 additions & 0 deletions x/evm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,46 @@ func (q Querier) StaticCall(c context.Context, req *types.QueryStaticCallRequest
}
return &types.QueryStaticCallResponse{Data: res}, nil
}

func (q Querier) Pointer(c context.Context, req *types.QueryPointerRequest) (*types.QueryPointerResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
switch req.PointerType {
case types.PointerType_NATIVE:
p, v, e := q.Keeper.GetERC20NativePointer(ctx, req.Pointee)
return &types.QueryPointerResponse{
Pointer: p.Hex(),
Version: uint32(v),
Exists: e,
}, nil
case types.PointerType_CW20:
p, v, e := q.Keeper.GetERC20CW20Pointer(ctx, req.Pointee)
return &types.QueryPointerResponse{
Pointer: p.Hex(),
Version: uint32(v),
Exists: e,
}, nil
case types.PointerType_CW721:
p, v, e := q.Keeper.GetERC721CW721Pointer(ctx, req.Pointee)
return &types.QueryPointerResponse{
Pointer: p.Hex(),
Version: uint32(v),
Exists: e,
}, nil
case types.PointerType_ERC20:
p, v, e := q.Keeper.GetCW20ERC20Pointer(ctx, common.HexToAddress(req.Pointee))
return &types.QueryPointerResponse{
Pointer: p.String(),
Version: uint32(v),
Exists: e,
}, nil
case types.PointerType_ERC721:
p, v, e := q.Keeper.GetCW721ERC721Pointer(ctx, common.HexToAddress(req.Pointee))
return &types.QueryPointerResponse{
Pointer: p.String(),
Version: uint32(v),
Exists: e,
}, nil
default:
return nil, errors.ErrUnsupported
}
}
47 changes: 47 additions & 0 deletions x/evm/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package keeper_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/cw20"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/cw721"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/erc20"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/erc721"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/native"
"github.com/sei-protocol/sei-chain/x/evm/keeper"
"github.com/sei-protocol/sei-chain/x/evm/types"
"github.com/stretchr/testify/require"
)

func TestQueryPointer(t *testing.T) {
k, ctx := testkeeper.MockEVMKeeper()
seiAddr1, evmAddr1 := testkeeper.MockAddressPair()
seiAddr2, evmAddr2 := testkeeper.MockAddressPair()
seiAddr3, evmAddr3 := testkeeper.MockAddressPair()
seiAddr4, evmAddr4 := testkeeper.MockAddressPair()
seiAddr5, evmAddr5 := testkeeper.MockAddressPair()
goCtx := sdk.WrapSDKContext(ctx)
k.SetERC20NativePointer(ctx, seiAddr1.String(), evmAddr1)
k.SetERC20CW20Pointer(ctx, seiAddr2.String(), evmAddr2)
k.SetERC721CW721Pointer(ctx, seiAddr3.String(), evmAddr3)
k.SetCW20ERC20Pointer(ctx, evmAddr4, seiAddr4.String())
k.SetCW721ERC721Pointer(ctx, evmAddr5, seiAddr5.String())
q := keeper.Querier{k}
res, err := q.Pointer(goCtx, &types.QueryPointerRequest{PointerType: types.PointerType_NATIVE, Pointee: seiAddr1.String()})
require.Nil(t, err)
require.Equal(t, types.QueryPointerResponse{Pointer: evmAddr1.Hex(), Version: uint32(native.CurrentVersion), Exists: true}, *res)
res, err = q.Pointer(goCtx, &types.QueryPointerRequest{PointerType: types.PointerType_CW20, Pointee: seiAddr2.String()})
require.Nil(t, err)
require.Equal(t, types.QueryPointerResponse{Pointer: evmAddr2.Hex(), Version: uint32(cw20.CurrentVersion), Exists: true}, *res)
res, err = q.Pointer(goCtx, &types.QueryPointerRequest{PointerType: types.PointerType_CW721, Pointee: seiAddr3.String()})
require.Nil(t, err)
require.Equal(t, types.QueryPointerResponse{Pointer: evmAddr3.Hex(), Version: uint32(cw721.CurrentVersion), Exists: true}, *res)
res, err = q.Pointer(goCtx, &types.QueryPointerRequest{PointerType: types.PointerType_ERC20, Pointee: evmAddr4.Hex()})
require.Nil(t, err)
require.Equal(t, types.QueryPointerResponse{Pointer: seiAddr4.String(), Version: uint32(erc20.CurrentVersion), Exists: true}, *res)
res, err = q.Pointer(goCtx, &types.QueryPointerRequest{PointerType: types.PointerType_ERC721, Pointee: evmAddr5.Hex()})
require.Nil(t, err)
require.Equal(t, types.QueryPointerResponse{Pointer: seiAddr5.String(), Version: uint32(erc721.CurrentVersion), Exists: true}, *res)
}
78 changes: 78 additions & 0 deletions x/evm/types/enums.pb.go

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

Loading

0 comments on commit f87a819

Please sign in to comment.