Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
chore: use rpc client
Browse files Browse the repository at this point in the history
  • Loading branch information
kenta-mori3322 committed May 6, 2024
1 parent 9644662 commit 945a476
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 27 deletions.
128 changes: 128 additions & 0 deletions x/evm/keeper/rpc_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package keeper

import (
"fmt"
"math/big"
"net/rpc"

"cosmossdk.io/log"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/params"
"github.com/evmos/ethermint/x/evm/statedb"
)

type sgxRPCClient struct {
logger log.Logger
cl *rpc.Client
}

// newSgxRPCClient creates a new RPC client to communicate with the SGX binary.
func newSgxRPCClient(logger log.Logger) (*sgxRPCClient, error) {
// TODO Make ports configurable
cl, err := rpc.DialHTTP("tcp", "localhost"+":9092")
if err != nil {
return nil, err
}

return &sgxRPCClient{
logger: logger,
cl: cl,
}, nil
}

func (c *sgxRPCClient) doCall(method string, req any) (any, error) {
c.logger.Debug(fmt.Sprintf("RPC call %s", method), "args", args)
resp, err := c.cl.Call(method, req)
c.logger.Debug(fmt.Sprintf("RPC call %s", method), "reply", resp)
return resp, err
}

func (c *sgxRPCClient) StartEVM(req *sgx.StartEVMRequest) (*sgx.StartEVMResponse, error) {

Check failure on line 44 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / build

undefined: sgx

Check failure on line 44 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-importer

undefined: sgx

Check failure on line 44 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-random-genesis-fast

undefined: sgx

Check failure on line 44 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-nondeterminism

undefined: sgx

Check failure on line 44 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

undefined: sgx

Check failure on line 44 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-rpc

undefined: sgx
return c.doCall("SgxRpcServer.StartEVM", req)
}

func (c *sgxRPCClient) InitFhevm(req *sgx.InitFhevmRequest) (*sgx.InitFhevmResponse, error) {

Check failure on line 48 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / build

undefined: sgx

Check failure on line 48 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-importer

undefined: sgx

Check failure on line 48 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-random-genesis-fast

undefined: sgx

Check failure on line 48 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-nondeterminism

undefined: sgx

Check failure on line 48 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

undefined: sgx

Check failure on line 48 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-rpc

undefined: sgx
return c.doCall("SgxRpcServer.InitFhevm", req)
}

func (c *sgxRPCClient) Create(req *sgx.CreateRequest) (*sgx.CreateResponse, error) {

Check failure on line 52 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / build

undefined: sgx

Check failure on line 52 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-importer

undefined: sgx

Check failure on line 52 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-random-genesis-fast

undefined: sgx

Check failure on line 52 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-nondeterminism

undefined: sgx

Check failure on line 52 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

undefined: sgx

Check failure on line 52 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-rpc

undefined: sgx
return c.doCall("SgxRpcServer.Create", args, reply)
}

func (c *sgxRPCClient) Call(req *sgx.CallRequest) (*sgx.CallResponse, error) {

Check failure on line 56 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / build

undefined: sgx

Check failure on line 56 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-importer

undefined: sgx

Check failure on line 56 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-random-genesis-fast

undefined: sgx

Check failure on line 56 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-nondeterminism

undefined: sgx

Check failure on line 56 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

undefined: sgx

Check failure on line 56 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-rpc

undefined: sgx
return c.doCall("SgxRpcServer.Call", req)
}

func (c *sgxRPCClient) Commit(req *sgx.CommitRequest) (*sgx.CommitResponse, error) {

Check failure on line 60 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / build

undefined: sgx

Check failure on line 60 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-importer

undefined: sgx

Check failure on line 60 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-random-genesis-fast

undefined: sgx

Check failure on line 60 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-nondeterminism

undefined: sgx

Check failure on line 60 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

undefined: sgx

Check failure on line 60 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-rpc

undefined: sgx
return c.doCall("SgxRpcServer.Commit", req)
}

func (c *sgxRPCClient) StateDBAddBalance(req *sgx.StateDBAddBalanceRequest) (*sgx.StateDBAddBalanceResponse, error) {

Check failure on line 64 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / build

undefined: sgx

Check failure on line 64 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-importer

undefined: sgx

Check failure on line 64 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-random-genesis-fast

undefined: sgx

Check failure on line 64 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-nondeterminism

undefined: sgx

Check failure on line 64 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

undefined: sgx

Check failure on line 64 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-rpc

undefined: sgx
return c.doCall("SgxRpcServer.StateDBAddBalance", req)
}

func (c *sgxRPCClient) StateDBSubBalance(req *sgx.StateDBSubBalanceRequest) (*sgx.StateDBSubBalanceResponse, error) {

Check failure on line 68 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / build

undefined: sgx

Check failure on line 68 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-importer

undefined: sgx

Check failure on line 68 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-random-genesis-fast

undefined: sgx

Check failure on line 68 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-nondeterminism

undefined: sgx

Check failure on line 68 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

undefined: sgx

Check failure on line 68 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-rpc

undefined: sgx
return c.doCall("SgxRpcServer.StateDBSubBalance", req)
}

func (c *sgxRPCClient) StateDBSetNonce(req *sgx.StateDBSetNonceRequest) (*sgx.StateDBSetNonceResponse, error) {

Check failure on line 72 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / build

undefined: sgx

Check failure on line 72 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-importer

undefined: sgx

Check failure on line 72 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-random-genesis-fast

undefined: sgx

Check failure on line 72 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-nondeterminism

undefined: sgx

Check failure on line 72 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

undefined: sgx

Check failure on line 72 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-rpc

undefined: sgx
return c.doCall("SgxRpcServer.StateDBSetNonce", req)
}

func (c *sgxRPCClient) StateDBIncreaseNonce(req *sgx.StateDBIncreaseNonceRequest) (*sgx.StateDBIncreaseNonceResponse, error) {

Check failure on line 76 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / build

undefined: sgx

Check failure on line 76 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-importer

undefined: sgx

Check failure on line 76 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-random-genesis-fast

undefined: sgx

Check failure on line 76 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-nondeterminism

undefined: sgx

Check failure on line 76 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

undefined: sgx

Check failure on line 76 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-rpc

undefined: sgx
return c.doCall("SgxRpcServer.StateDBIncreaseNonce", req)
}

func (c *sgxRPCClient) StateDBPrepare(req *sgx.StateDBPrepareRequest) (*sgx.StateDBPrepareResponse, error) {

Check failure on line 80 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / build

undefined: sgx

Check failure on line 80 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-importer

undefined: sgx

Check failure on line 80 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-random-genesis-fast

undefined: sgx

Check failure on line 80 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-sim-nondeterminism

undefined: sgx

Check failure on line 80 in x/evm/keeper/rpc_client.go

View workflow job for this annotation

GitHub Actions / test-rpc

undefined: sgx
return c.doCall("SgxRpcServer.StateDBPrepare", req)
}

func (c *sgxRPCClient) StateDBGetRefund(req *sgx.StateDBGetRefundRequest) (*sgx.StateDBGetRefundResponse, error) {
return c.doCall("SgxRpcServer.StateDBGetRefund", req)
}

func (c *sgxRPCClient) StateDBGetLogs(req *sgx.StateDBGetLogsRequest) (*sgx.StateDBGetLogsResponse, error) {
return c.doCall("SgxRpcServer.StateDBGetLogs", req)
}

// PrepareTxEVMConfig only contains the fields from EVMConfig that are needed
// to create a new EVM instance. This is used to pass the EVM configuration
// over RPC to the SGX binary.
type PrepareTxEVMConfig struct {
// ChainConfig is the EVM chain configuration in JSON format. Since the
// underlying params.ChainConfig struct contains pointer fields, they are
// not serializable over RPC with gob. Instead, the JSON representation is
// used.
ChainConfigJson []byte

// Fields from EVMConfig
CoinBase common.Address
BaseFee *big.Int
TxConfig statedb.TxConfig
DebugTrace bool

// Fields from EVMConfig.FeeMarketParams struct
NoBaseFee bool

// Fields from EVMConfig.Params struct
EvmDenom string
ExtraEips []int
// *rpctypes.StateOverride : original type
Overrides string
}

// PrepareTxArgs is the argument struct for the SgxRpcServer.PrepareTx RPC method.
type PrepareTxArgs struct {
TxHash []byte
// Header is the Tendermint header of the block in which the transaction
// will be executed.
Header cmtproto.Header
// Msg is the EVM transaction message to run on the EVM.
Msg core.Message
// EvmConfig is the EVM configuration to set.
EvmConfig PrepareTxEVMConfig
}
47 changes: 20 additions & 27 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,19 +303,12 @@ func (k *Keeper) ApplyMessageWithConfig(
return nil, errorsmod.Wrap(types.ErrCallDisabled, "failed to call contract")
}

rpcConn, err := grpc.Dial("localhost:9092",
grpc.WithInsecure(),
grpc.WithContextDialer(func(ctx context.Context, url string) (net.Conn, error) {
return net.Dial("tcp", url)
}),
)
sgxRPCClient, err := newSgxRPCClient(k.Logger(ctx))
if err != nil {
return nil, errorsmod.Wrap(err, "failed to connect to localhost:9092")
return nil, errorsmod.Wrap(err, "failed to create new SGX rpc client")
}

sgxGrpcClient := sgx.NewQueryServiceClient(rpcConn)

handlerId, err := k.startEVM(ctx, msg, cfg, sgxGrpcClient)
handlerId, err := k.startEVM(ctx, msg, cfg, sgxRPCClient)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to create new RPC server")
}
Expand All @@ -330,7 +323,7 @@ func (k *Keeper) ApplyMessageWithConfig(

// Ethermint original code:
// stateDB.SubBalance(sender.Address(), new(big.Int).Mul(msg.GasPrice, new(big.Int).SetUint64(msg.GasLimit)))
_, err := sgxGrpcClient.StateDBSubBalance(ctx, &sgx.StateDBSubBalanceRequest{
_, err := sgxRpcClient.StateDBSubBalance(ctx, &sgx.StateDBSubBalanceRequest{
HandlerId: handlerId,
Caller: sender.Address().Bytes(),
Amount: new(big.Int).Mul(msg.GasPrice, new(big.Int).SetUint64(msg.GasLimit)).String(),
Expand All @@ -345,7 +338,7 @@ func (k *Keeper) ApplyMessageWithConfig(

// Ethermint original code:
// stateDB.SetNonce(sender.Address(), stateDB.GetNonce(sender.Address())+1)
_, err = sgxGrpcClient.StateDBIncreaseNonce(ctx, &sgx.StateDBIncreaseNonceRequest{
_, err = sgxRpcClient.StateDBIncreaseNonce(ctx, &sgx.StateDBIncreaseNonceRequest{
HandlerId: handlerId,
Caller: sender.Address().Bytes(),
})
Expand All @@ -362,7 +355,7 @@ func (k *Keeper) ApplyMessageWithConfig(
if cfg.DebugTrace {
// Ethermint original code:
// stateDB.AddBalance(sender.Address(), new(big.Int).Mul(msg.GasPrice, new(big.Int).SetUint64(leftoverGas)))
_, err := sgxGrpcClient.StateDBAddBalance(ctx, &sgx.StateDBAddBalanceRequest{
_, err := sgxRpcClient.StateDBAddBalance(ctx, &sgx.StateDBAddBalanceRequest{
HandlerId: handlerId,
Caller: sender.Address().Bytes(),
Amount: new(big.Int).Mul(msg.GasPrice, new(big.Int).SetUint64(leftoverGas)).String(),
Expand Down Expand Up @@ -437,7 +430,7 @@ func (k *Keeper) ApplyMessageWithConfig(
})
}

_, err = sgxGrpcClient.StateDBPrepare(ctx, dbPrepareReq)
_, err = sgxRpcClient.StateDBPrepare(ctx, dbPrepareReq)
if err != nil {
// panic cosmos if sgx isn't available.
if isSgxDownError(err) {
Expand All @@ -454,7 +447,7 @@ func (k *Keeper) ApplyMessageWithConfig(

// Ethermint original code:
// stateDB.SetNonce(sender.Address(), msg.Nonce)
_, err := sgxGrpcClient.StateDBSetNonce(ctx, &sgx.StateDBSetNonceRequest{
_, err := sgxRpcClient.StateDBSetNonce(ctx, &sgx.StateDBSetNonceRequest{
HandlerId: handlerId,
Caller: msg.From.Bytes(),
Nonce: msg.Nonce,
Expand All @@ -471,7 +464,7 @@ func (k *Keeper) ApplyMessageWithConfig(
// Ethermint original code:
// ret, _, leftoverGas, vmErr = evm.Create(sender, msg.Data, leftoverGas, msg.Value)
var resp *sgx.CreateResponse
resp, vmErr = sgxGrpcClient.Create(ctx, &sgx.CreateRequest{
resp, vmErr = sgxRpcClient.Create(ctx, &sgx.CreateRequest{
HandlerId: handlerId,
Caller: msg.From.Bytes(),
Code: msg.Data,
Expand All @@ -490,7 +483,7 @@ func (k *Keeper) ApplyMessageWithConfig(

// Ethermint original code:
// stateDB.SetNonce(sender.Address(), msg.Nonce+1)
_, err = sgxGrpcClient.StateDBSetNonce(ctx, &sgx.StateDBSetNonceRequest{
_, err = sgxRpcClient.StateDBSetNonce(ctx, &sgx.StateDBSetNonceRequest{
HandlerId: handlerId,
Caller: msg.From.Bytes(),
Nonce: msg.Nonce + 1,
Expand All @@ -507,7 +500,7 @@ func (k *Keeper) ApplyMessageWithConfig(
// Ethermint original code:
// ret, leftoverGas, vmErr = evm.Call(sender, *msg.To, msg.Data, leftoverGas, msg.Value)
var resp *sgx.CallResponse
resp, vmErr = sgxGrpcClient.Call(ctx, &sgx.CallRequest{
resp, vmErr = sgxRpcClient.Call(ctx, &sgx.CallRequest{
HandlerId: handlerId,
Caller: msg.From.Bytes(),
Addr: safeAddress2Bytes(msg.To),
Expand Down Expand Up @@ -544,7 +537,7 @@ func (k *Keeper) ApplyMessageWithConfig(

// Ethermint original code:
// leftoverGas += GasToRefund(stateDB.GetRefund(), temporaryGasUsed, refundQuotient)
resp, err := sgxGrpcClient.StateDBGetRefund(ctx, &sgx.StateDBGetRefundRequest{
resp, err := sgxRpcClient.StateDBGetRefund(ctx, &sgx.StateDBGetRefundRequest{
HandlerId: handlerId,
})
if err != nil {
Expand All @@ -571,7 +564,7 @@ func (k *Keeper) ApplyMessageWithConfig(
// if err := stateDB.Commit(); err != nil {
// return nil, errorsmod.Wrap(err, "failed to commit stateDB")
// }
_, err := sgxGrpcClient.Commit(ctx, &sgx.CommitRequest{HandlerId: handlerId})
_, err := sgxRpcClient.Commit(ctx, &sgx.CommitRequest{HandlerId: handlerId})
if err != nil {
// panic cosmos if sgx isn't available.
if isSgxDownError(err) {
Expand Down Expand Up @@ -602,7 +595,7 @@ func (k *Keeper) ApplyMessageWithConfig(

// Ethermint original code:
// Logs: types.NewLogsFromEth(stateDB.Logs()),
logsResp, err := sgxGrpcClient.StateDBGetLogs(ctx, &sgx.StateDBGetLogsRequest{HandlerId: handlerId})
logsResp, err := sgxRpcClient.StateDBGetLogs(ctx, &sgx.StateDBGetLogsRequest{HandlerId: handlerId})
if err != nil {
// panic cosmos if sgx isn't available.
if isSgxDownError(err) {
Expand All @@ -622,7 +615,7 @@ func (k *Keeper) ApplyMessageWithConfig(
ethlogs[i] = &l
}

err = k.stopEVM(ctx, handlerId, sgxGrpcClient)
err = k.stopEVM(ctx, handlerId, sgxRpcClient)
if err != nil {
return nil, err
}
Expand All @@ -639,7 +632,7 @@ func (k *Keeper) ApplyMessageWithConfig(

// startEVM notifies the TEE enclave to create a new EVM. It returns the
// unique handler id for the EVM.
func (k *Keeper) startEVM(ctx sdk.Context, msg core.Message, cfg *EVMConfig, sgxGrpcClient sgx.QueryServiceClient) (uint64, error) {
func (k *Keeper) startEVM(ctx sdk.Context, msg core.Message, cfg *EVMConfig, sgxRpcClient sgxRPCClient) (uint64, error) {
chainConfigJSON, err := json.Marshal(cfg.ChainConfig)
if err != nil {
return 0, err
Expand Down Expand Up @@ -682,7 +675,7 @@ func (k *Keeper) startEVM(ctx sdk.Context, msg core.Message, cfg *EVMConfig, sgx
}

header := ctx.BlockHeader()
resp, err := sgxGrpcClient.StartEVM(ctx, &sgx.StartEVMRequest{
resp, err := sgxRpcClient.StartEVM(ctx, &sgx.StartEVMRequest{
Header: &header,
MsgJson: msgJSON,
EvmConfig: &evmConfig,
Expand Down Expand Up @@ -710,7 +703,7 @@ func (k *Keeper) startEVM(ctx sdk.Context, msg core.Message, cfg *EVMConfig, sgx
// ref: https://github.com/Inco-fhevm/zbc-go-ethereum/pull/2
//
// We are doing step 2 here.
_, err = sgxGrpcClient.InitFhevm(ctx, &sgx.InitFhevmRequest{
_, err = sgxRpcClient.InitFhevm(ctx, &sgx.InitFhevmRequest{
HandlerId: resp.HandlerId,
})
if err != nil {
Expand All @@ -720,8 +713,8 @@ func (k *Keeper) startEVM(ctx sdk.Context, msg core.Message, cfg *EVMConfig, sgx
return resp.HandlerId, err
}

func (k *Keeper) stopEVM(ctx sdk.Context, handlerId uint64, sgxGrpcClient sgx.QueryServiceClient) error {
_, err := sgxGrpcClient.StopEVM(ctx, &sgx.StopEVMRequest{HandlerId: handlerId})
func (k *Keeper) stopEVM(ctx sdk.Context, handlerId uint64, sgxRpcClient sgxRPCClient) error {
_, err := sgxRpcClient.StopEVM(ctx, &sgx.StopEVMRequest{HandlerId: handlerId})
if err != nil {
// panic cosmos if sgx isn't available.
if isSgxDownError(err) {
Expand Down

0 comments on commit 945a476

Please sign in to comment.