This repository was archived by the owner on Dec 12, 2024. It is now read-only.
forked from crypto-org-chain/ethermint
-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: RPC server with evmID (ethermint side) #5
Open
kenta-mori3322
wants to merge
7
commits into
develop
Choose a base branch
from
feat/rpc
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c22b501
chore: update rpc server
kenta-mori3322 7586a15
chore: remove ctx map in keeper as ctx is being handled in rpc server
kenta-mori3322 2ebadfe
chore: remove rpc server start
kenta-mori3322 dc44609
chore: rpc run in etheremint
kenta-mori3322 54f35e7
chore: add stopEVM
kenta-mori3322 fceaefb
chore: code refactor
kenta-mori3322 edc8ab4
chore: code refactor
kenta-mori3322 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -17,10 +17,12 @@ | |||||
|
||||||
import ( | ||||||
"context" | ||||||
"errors" | ||||||
"fmt" | ||||||
"io" | ||||||
"net" | ||||||
"net/http" | ||||||
"net/rpc" | ||||||
"os" | ||||||
"path/filepath" | ||||||
"runtime/pprof" | ||||||
|
@@ -69,8 +71,14 @@ | |||||
"github.com/evmos/ethermint/server/config" | ||||||
srvflags "github.com/evmos/ethermint/server/flags" | ||||||
ethermint "github.com/evmos/ethermint/types" | ||||||
evmKeeper "github.com/evmos/ethermint/x/evm/keeper" | ||||||
) | ||||||
|
||||||
type IncoApp interface { | ||||||
// Return evm keeper which is used in ethermint side to get evm keeper access | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Use go-style godocs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||||||
GetEvmKeeper() *evmKeeper.Keeper | ||||||
} | ||||||
|
||||||
// DBOpener is a function to open `application.db`, potentially with customized options. | ||||||
type DBOpener func(opts types.AppOptions, rootDir string, backend dbm.BackendType) (dbm.DB, error) | ||||||
|
||||||
|
@@ -488,9 +496,66 @@ | |||||
return err | ||||||
} | ||||||
|
||||||
// Start running rpc server for sgx binary access on the evm Keeper statedb | ||||||
listener, err := startRpcServer(svrCtx, g, app) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
defer func() { | ||||||
// TODO: Graceful stop | ||||||
listener.Close() | ||||||
}() | ||||||
|
||||||
return g.Wait() | ||||||
} | ||||||
|
||||||
func startRpcServer( | ||||||
svrCtx *server.Context, | ||||||
g *errgroup.Group, | ||||||
app types.Application, | ||||||
) (listener net.Listener, err error) { | ||||||
ethApp := app.(IncoApp) | ||||||
evmKeeper := ethApp.GetEvmKeeper() | ||||||
if evmKeeper == nil { | ||||||
return nil, errors.New("evm keeper is invalid") | ||||||
} | ||||||
|
||||||
g.Go(func() error { | ||||||
listener, err = runRPCServer(svrCtx, evmKeeper) | ||||||
return err | ||||||
}) | ||||||
|
||||||
return | ||||||
} | ||||||
|
||||||
func runRPCServer(svrCtx *server.Context, keeper *evmKeeper.Keeper) (net.Listener, error) { | ||||||
defer func() { | ||||||
if r := recover(); r != nil { | ||||||
svrCtx.Logger.Error("recovered from panic", "error", r) | ||||||
} | ||||||
}() | ||||||
|
||||||
// Run a persistent RPC server for sgx binary can access to evm keeper statedb | ||||||
srv := &evmKeeper.EthmRpcServer{Keeper: keeper} | ||||||
err := rpc.Register(srv) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
rpc.HandleHTTP() | ||||||
|
||||||
// TODO handle port customization | ||||||
l, err := net.Listen("tcp", ":9093") | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
go http.Serve(l, nil) | ||||||
|
||||||
return l, nil | ||||||
} | ||||||
|
||||||
func openDB(_ types.AppOptions, rootDir string, backendType dbm.BackendType) (dbm.DB, error) { | ||||||
dataDir := filepath.Join(rootDir, "data") | ||||||
return dbm.NewDB("application", backendType, dataDir) | ||||||
|
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 | ||||
---|---|---|---|---|---|---|
|
@@ -41,8 +41,12 @@ | |||||
return err | ||||||
} | ||||||
|
||||||
func (c *sgxRPCClient) PrepareTx(args PrepareTxArgs, reply *PrepareTxReply) error { | ||||||
return c.doCall("SgxRpcServer.PrepareTx", args, reply) | ||||||
func (c *sgxRPCClient) StartEVM(args StartEVMArgs, reply *StartEVMReply) error { | ||||||
return c.doCall("SgxRpcServer.StartEVM", args, reply) | ||||||
} | ||||||
|
||||||
func (c *sgxRPCClient) InitFhevm(args InitFhevmArgs, reply *InitFhevmReply) error { | ||||||
return c.doCall("SgxRpcServer.InitFhevm", args, reply) | ||||||
} | ||||||
|
||||||
func (c *sgxRPCClient) Call(args CallArgs, reply *CallReply) error { | ||||||
|
@@ -85,15 +89,19 @@ | |||||
return c.doCall("SgxRpcServer.StateDBGetLogs", args, reply) | ||||||
} | ||||||
|
||||||
// PrepareTxEVMConfig only contains the fields from EVMConfig that are needed | ||||||
func (c *sgxRPCClient) StopEVM(args StopEVMArgs, reply *StopEVMReply) error { | ||||||
return c.doCall("SgxRpcServer.StopEVM", args, reply) | ||||||
} | ||||||
|
||||||
// StartEVMTxEVMConfig 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 { | ||||||
type StartEVMTxEVMConfig struct { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||||||
// 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 | ||||||
|
@@ -111,24 +119,35 @@ | |||||
Overrides string | ||||||
} | ||||||
|
||||||
// PrepareTxArgs is the argument struct for the SgxRpcServer.PrepareTx RPC method. | ||||||
type PrepareTxArgs struct { | ||||||
// StartEVMArgs is the argument struct for the SgxRpcServer.StartEVM RPC method. | ||||||
type StartEVMArgs 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 | ||||||
EvmConfig StartEVMTxEVMConfig | ||||||
} | ||||||
|
||||||
// StartEVMReply is the reply struct for the SgxRpcServer.StartEVM RPC method. | ||||||
type StartEVMReply struct { | ||||||
EvmId uint64 | ||||||
} | ||||||
|
||||||
// PrepareTxArgs is the reply struct for the SgxRpcServer.PrepareTx RPC method. | ||||||
type PrepareTxReply struct { | ||||||
// InitFhevmArgs is the arg struct for the SgxRpcServer.InitFhevm RPC method. | ||||||
type InitFhevmArgs struct { | ||||||
EvmId uint64 | ||||||
} | ||||||
|
||||||
// InitFhevmReply is the reply struct for the SgxRpcServer.InitFhevm RPC method. | ||||||
type InitFhevmReply struct { | ||||||
} | ||||||
|
||||||
// CallArgs is the argument struct for the SgxRpcServer.Call RPC method. | ||||||
type CallArgs struct { | ||||||
EvmId uint64 | ||||||
Caller vm.AccountRef | ||||||
Addr common.Address | ||||||
Input []byte | ||||||
|
@@ -144,6 +163,7 @@ | |||||
|
||||||
// CreateArgs is the argument struct for the SgxRpcServer.Create RPC method. | ||||||
type CreateArgs struct { | ||||||
EvmId uint64 | ||||||
Caller vm.AccountRef | ||||||
Code []byte | ||||||
Gas uint64 | ||||||
|
@@ -159,72 +179,87 @@ | |||||
|
||||||
// CommitArgs is the argument struct for the SgxRpcServer.Commit RPC method. | ||||||
type CommitArgs struct { | ||||||
Commit bool | ||||||
EvmId uint64 | ||||||
} | ||||||
|
||||||
// CommitReply is the reply struct for the SgxRpcServer.Commit RPC method. | ||||||
type CommitReply struct { | ||||||
} | ||||||
|
||||||
// CommitArgs is the argument struct for the SgxRpcServer.StateDBSubBalance RPC method. | ||||||
type StateDBSubBalanceArgs struct { | ||||||
EvmId uint64 | ||||||
Caller vm.AccountRef | ||||||
Msg core.Message | ||||||
} | ||||||
|
||||||
// CommitReply is the reply struct for the SgxRpcServer.StateDBSubBalance RPC method. | ||||||
type StateDBSubBalanceReply struct { | ||||||
} | ||||||
|
||||||
// CommitArgs is the argument struct for the SgxRpcServer.StateDSetNonce RPC method. | ||||||
type StateDBSetNonceArgs struct { | ||||||
EvmId uint64 | ||||||
Caller vm.AccountRef | ||||||
Nonce uint64 | ||||||
} | ||||||
|
||||||
// CommitReply is the reply struct for the SgxRpcServer.StateDSetNonce RPC method. | ||||||
type StateDBSetNonceReply struct { | ||||||
} | ||||||
|
||||||
// StateDBAddBalanceArgs is the argument struct for the SgxRpcServer.StateDBAddBalance RPC method. | ||||||
type StateDBAddBalanceArgs struct { | ||||||
EvmId uint64 | ||||||
Caller vm.AccountRef | ||||||
Msg core.Message | ||||||
LeftoverGas uint64 | ||||||
} | ||||||
|
||||||
// StateDBAddBalanceReply is the reply struct for the SgxRpcServer.StateDBAddBalance RPC method. | ||||||
type StateDBAddBalanceReply struct { | ||||||
} | ||||||
|
||||||
type StateDBPrepareArgs struct { | ||||||
Msg core.Message | ||||||
Rules params.Rules | ||||||
EvmId uint64 | ||||||
Msg core.Message | ||||||
Rules params.Rules | ||||||
CoinBase common.Address | ||||||
} | ||||||
|
||||||
type StateDBPrepareReply struct { | ||||||
} | ||||||
|
||||||
// StateDBIncreaseNonceArgs is the argument struct for the SgxRpcServer.StateDBIncreaseNonce RPC method. | ||||||
type StateDBIncreaseNonceArgs struct { | ||||||
EvmId uint64 | ||||||
Caller vm.AccountRef | ||||||
Msg core.Message | ||||||
} | ||||||
|
||||||
// StateDBIncreaseNonceReply is the reply struct for the SgxRpcServer.StateDBIncreaseNonce RPC method. | ||||||
type StateDBIncreaseNonceReply struct { | ||||||
} | ||||||
|
||||||
type StateDBGetRefundArgs struct { | ||||||
EvmId uint64 | ||||||
} | ||||||
|
||||||
type StateDBGetRefundReply struct { | ||||||
Refund uint64 | ||||||
} | ||||||
|
||||||
type StateDBGetLogsArgs struct { | ||||||
EvmId uint64 | ||||||
} | ||||||
|
||||||
type StateDBGetLogsReply struct { | ||||||
Logs []*ethtypes.Log | ||||||
} | ||||||
|
||||||
type StopEVMArgs struct { | ||||||
EvmId uint64 | ||||||
} | ||||||
|
||||||
type StopEVMReply struct { | ||||||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.