2021-04-18 16:39:15 +00:00
|
|
|
// Package rpc contains RPC handler methods and utilities to start
|
|
|
|
// Ethermint's Web3-compatibly JSON-RPC server.
|
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2022-04-19 12:49:05 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2021-07-12 18:39:35 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/server"
|
2021-08-19 16:55:13 +00:00
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2021-08-19 16:55:13 +00:00
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/rpc/backend"
|
|
|
|
"github.com/evmos/ethermint/rpc/namespaces/ethereum/debug"
|
|
|
|
"github.com/evmos/ethermint/rpc/namespaces/ethereum/eth"
|
|
|
|
"github.com/evmos/ethermint/rpc/namespaces/ethereum/eth/filters"
|
|
|
|
"github.com/evmos/ethermint/rpc/namespaces/ethereum/miner"
|
|
|
|
"github.com/evmos/ethermint/rpc/namespaces/ethereum/net"
|
|
|
|
"github.com/evmos/ethermint/rpc/namespaces/ethereum/personal"
|
|
|
|
"github.com/evmos/ethermint/rpc/namespaces/ethereum/txpool"
|
|
|
|
"github.com/evmos/ethermint/rpc/namespaces/ethereum/web3"
|
|
|
|
"github.com/evmos/ethermint/rpc/types"
|
2021-04-19 10:49:55 +00:00
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
rpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RPC namespaces and API version
|
|
|
|
const (
|
2022-05-02 06:26:24 +00:00
|
|
|
// Cosmos namespaces
|
|
|
|
|
|
|
|
CosmosNamespace = "cosmos"
|
|
|
|
|
|
|
|
// Ethereum namespaces
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
Web3Namespace = "web3"
|
|
|
|
EthNamespace = "eth"
|
|
|
|
PersonalNamespace = "personal"
|
|
|
|
NetNamespace = "net"
|
2021-06-21 14:06:30 +00:00
|
|
|
TxPoolNamespace = "txpool"
|
2021-07-20 12:50:17 +00:00
|
|
|
DebugNamespace = "debug"
|
2021-08-04 09:18:22 +00:00
|
|
|
MinerNamespace = "miner"
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
apiVersion = "1.0"
|
|
|
|
)
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
// APICreator creates the JSON-RPC API implementations.
|
2022-04-19 12:49:05 +00:00
|
|
|
type APICreator = func(*server.Context, client.Context, *rpcclient.WSClient) []rpc.API
|
2021-07-26 11:15:55 +00:00
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
// apiCreators defines the JSON-RPC API namespaces.
|
2022-04-19 12:49:05 +00:00
|
|
|
var apiCreators map[string]APICreator
|
2021-07-26 11:15:55 +00:00
|
|
|
|
2022-04-19 12:49:05 +00:00
|
|
|
func init() {
|
|
|
|
apiCreators = map[string]APICreator{
|
|
|
|
EthNamespace: func(ctx *server.Context, clientCtx client.Context, tmWSClient *rpcclient.WSClient) []rpc.API {
|
|
|
|
nonceLock := new(types.AddrLocker)
|
2022-05-02 06:26:24 +00:00
|
|
|
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx)
|
2022-04-19 12:49:05 +00:00
|
|
|
return []rpc.API{
|
|
|
|
{
|
2021-07-26 11:15:55 +00:00
|
|
|
Namespace: EthNamespace,
|
|
|
|
Version: apiVersion,
|
2021-08-10 10:04:16 +00:00
|
|
|
Service: eth.NewPublicAPI(ctx.Logger, clientCtx, evmBackend, nonceLock),
|
2021-07-26 11:15:55 +00:00
|
|
|
Public: true,
|
|
|
|
},
|
2022-04-19 12:49:05 +00:00
|
|
|
{
|
2021-07-26 11:15:55 +00:00
|
|
|
Namespace: EthNamespace,
|
|
|
|
Version: apiVersion,
|
2022-03-24 07:56:59 +00:00
|
|
|
Service: filters.NewPublicAPI(ctx.Logger, clientCtx, tmWSClient, evmBackend),
|
2021-07-26 11:15:55 +00:00
|
|
|
Public: true,
|
|
|
|
},
|
2022-04-19 12:49:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Web3Namespace: func(*server.Context, client.Context, *rpcclient.WSClient) []rpc.API {
|
|
|
|
return []rpc.API{
|
|
|
|
{
|
2021-07-26 11:15:55 +00:00
|
|
|
Namespace: Web3Namespace,
|
|
|
|
Version: apiVersion,
|
|
|
|
Service: web3.NewPublicAPI(),
|
|
|
|
Public: true,
|
|
|
|
},
|
2022-04-19 12:49:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
NetNamespace: func(_ *server.Context, clientCtx client.Context, _ *rpcclient.WSClient) []rpc.API {
|
|
|
|
return []rpc.API{
|
|
|
|
{
|
2021-07-26 11:15:55 +00:00
|
|
|
Namespace: NetNamespace,
|
|
|
|
Version: apiVersion,
|
|
|
|
Service: net.NewPublicAPI(clientCtx),
|
|
|
|
Public: true,
|
|
|
|
},
|
2022-04-19 12:49:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
PersonalNamespace: func(ctx *server.Context, clientCtx client.Context, _ *rpcclient.WSClient) []rpc.API {
|
2022-05-02 06:26:24 +00:00
|
|
|
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx)
|
2022-04-19 12:49:05 +00:00
|
|
|
return []rpc.API{
|
|
|
|
{
|
2021-07-26 11:15:55 +00:00
|
|
|
Namespace: PersonalNamespace,
|
|
|
|
Version: apiVersion,
|
2021-08-10 10:04:16 +00:00
|
|
|
Service: personal.NewAPI(ctx.Logger, clientCtx, evmBackend),
|
2021-09-04 10:18:32 +00:00
|
|
|
Public: false,
|
2021-07-26 11:15:55 +00:00
|
|
|
},
|
2022-04-19 12:49:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
TxPoolNamespace: func(ctx *server.Context, _ client.Context, _ *rpcclient.WSClient) []rpc.API {
|
|
|
|
return []rpc.API{
|
|
|
|
{
|
2021-07-26 11:15:55 +00:00
|
|
|
Namespace: TxPoolNamespace,
|
|
|
|
Version: apiVersion,
|
|
|
|
Service: txpool.NewPublicAPI(ctx.Logger),
|
|
|
|
Public: true,
|
|
|
|
},
|
2022-04-19 12:49:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
DebugNamespace: func(ctx *server.Context, clientCtx client.Context, _ *rpcclient.WSClient) []rpc.API {
|
2022-05-02 06:26:24 +00:00
|
|
|
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx)
|
2022-04-19 12:49:05 +00:00
|
|
|
return []rpc.API{
|
|
|
|
{
|
2021-07-26 11:15:55 +00:00
|
|
|
Namespace: DebugNamespace,
|
|
|
|
Version: apiVersion,
|
2021-09-04 20:33:06 +00:00
|
|
|
Service: debug.NewAPI(ctx, evmBackend, clientCtx),
|
2021-07-26 11:15:55 +00:00
|
|
|
Public: true,
|
|
|
|
},
|
2022-04-19 12:49:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
MinerNamespace: func(ctx *server.Context, clientCtx client.Context, _ *rpcclient.WSClient) []rpc.API {
|
2022-05-02 06:26:24 +00:00
|
|
|
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx)
|
2022-04-19 12:49:05 +00:00
|
|
|
return []rpc.API{
|
|
|
|
{
|
2021-08-04 09:18:22 +00:00
|
|
|
Namespace: MinerNamespace,
|
|
|
|
Version: apiVersion,
|
2021-09-04 10:18:32 +00:00
|
|
|
Service: miner.NewPrivateAPI(ctx, clientCtx, evmBackend),
|
|
|
|
Public: false,
|
2021-08-04 09:18:22 +00:00
|
|
|
},
|
2022-04-19 12:49:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRPCAPIs returns the list of all APIs
|
|
|
|
func GetRPCAPIs(ctx *server.Context, clientCtx client.Context, tmWSClient *rpcclient.WSClient, selectedAPIs []string) []rpc.API {
|
|
|
|
var apis []rpc.API
|
|
|
|
|
|
|
|
for _, ns := range selectedAPIs {
|
|
|
|
if creator, ok := apiCreators[ns]; ok {
|
|
|
|
apis = append(apis, creator(ctx, clientCtx, tmWSClient)...)
|
|
|
|
} else {
|
|
|
|
ctx.Logger.Error("invalid namespace value", "namespace", ns)
|
2021-07-26 11:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return apis
|
|
|
|
}
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2022-04-19 12:49:05 +00:00
|
|
|
// RegisterAPINamespace registers a new API namespace with the API creator.
|
|
|
|
// This function fails if the namespace is already registered.
|
|
|
|
func RegisterAPINamespace(ns string, creator APICreator) error {
|
|
|
|
if _, ok := apiCreators[ns]; ok {
|
|
|
|
return fmt.Errorf("duplicated api namespace %s", ns)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
2022-04-19 12:49:05 +00:00
|
|
|
apiCreators[ns] = creator
|
|
|
|
return nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|