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 (
|
|
|
|
"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
|
|
|
|
2021-06-23 06:38:05 +00:00
|
|
|
"github.com/tharsis/ethermint/ethereum/rpc/backend"
|
2021-07-20 12:50:17 +00:00
|
|
|
"github.com/tharsis/ethermint/ethereum/rpc/namespaces/debug"
|
2021-06-23 06:38:05 +00:00
|
|
|
"github.com/tharsis/ethermint/ethereum/rpc/namespaces/eth"
|
|
|
|
"github.com/tharsis/ethermint/ethereum/rpc/namespaces/eth/filters"
|
2021-08-04 09:18:22 +00:00
|
|
|
"github.com/tharsis/ethermint/ethereum/rpc/namespaces/miner"
|
2021-06-23 06:38:05 +00:00
|
|
|
"github.com/tharsis/ethermint/ethereum/rpc/namespaces/net"
|
|
|
|
"github.com/tharsis/ethermint/ethereum/rpc/namespaces/personal"
|
|
|
|
"github.com/tharsis/ethermint/ethereum/rpc/namespaces/txpool"
|
|
|
|
"github.com/tharsis/ethermint/ethereum/rpc/namespaces/web3"
|
2021-06-22 10:49:18 +00:00
|
|
|
"github.com/tharsis/ethermint/ethereum/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 (
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetRPCAPIs returns the list of all APIs
|
2021-07-26 11:15:55 +00:00
|
|
|
func GetRPCAPIs(ctx *server.Context, clientCtx client.Context, tmWSClient *rpcclient.WSClient, selectedAPIs []string) []rpc.API {
|
2021-04-18 16:39:15 +00:00
|
|
|
nonceLock := new(types.AddrLocker)
|
2021-08-18 14:11:51 +00:00
|
|
|
evmBackend := backend.NewEVMBackend(ctx, ctx.Logger, clientCtx)
|
2021-07-26 11:15:55 +00:00
|
|
|
|
|
|
|
var apis []rpc.API
|
|
|
|
// remove duplicates
|
|
|
|
selectedAPIs = unique(selectedAPIs)
|
|
|
|
|
|
|
|
for index := range selectedAPIs {
|
|
|
|
switch selectedAPIs[index] {
|
|
|
|
case EthNamespace:
|
|
|
|
apis = append(apis,
|
|
|
|
rpc.API{
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
rpc.API{
|
|
|
|
Namespace: EthNamespace,
|
|
|
|
Version: apiVersion,
|
|
|
|
Service: filters.NewPublicAPI(ctx.Logger, tmWSClient, evmBackend),
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
case Web3Namespace:
|
|
|
|
apis = append(apis,
|
|
|
|
rpc.API{
|
|
|
|
Namespace: Web3Namespace,
|
|
|
|
Version: apiVersion,
|
|
|
|
Service: web3.NewPublicAPI(),
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
case NetNamespace:
|
|
|
|
apis = append(apis,
|
|
|
|
rpc.API{
|
|
|
|
Namespace: NetNamespace,
|
|
|
|
Version: apiVersion,
|
|
|
|
Service: net.NewPublicAPI(clientCtx),
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
case PersonalNamespace:
|
|
|
|
apis = append(apis,
|
|
|
|
rpc.API{
|
|
|
|
Namespace: PersonalNamespace,
|
|
|
|
Version: apiVersion,
|
2021-08-10 10:04:16 +00:00
|
|
|
Service: personal.NewAPI(ctx.Logger, clientCtx, evmBackend),
|
2021-07-26 11:15:55 +00:00
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
case TxPoolNamespace:
|
|
|
|
apis = append(apis,
|
|
|
|
rpc.API{
|
|
|
|
Namespace: TxPoolNamespace,
|
|
|
|
Version: apiVersion,
|
|
|
|
Service: txpool.NewPublicAPI(ctx.Logger),
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
case DebugNamespace:
|
|
|
|
apis = append(apis,
|
|
|
|
rpc.API{
|
|
|
|
Namespace: DebugNamespace,
|
|
|
|
Version: apiVersion,
|
|
|
|
Service: debug.NewInternalAPI(ctx),
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
)
|
2021-08-04 09:18:22 +00:00
|
|
|
case MinerNamespace:
|
|
|
|
apis = append(apis,
|
|
|
|
rpc.API{
|
|
|
|
Namespace: MinerNamespace,
|
|
|
|
Version: apiVersion,
|
2021-08-10 10:04:16 +00:00
|
|
|
Service: miner.NewMinerAPI(ctx, clientCtx, evmBackend),
|
2021-08-04 09:18:22 +00:00
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
)
|
2021-07-26 11:15:55 +00:00
|
|
|
default:
|
|
|
|
ctx.Logger.Error("invalid namespace value", "namespace", selectedAPIs[index])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return apis
|
|
|
|
}
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-07-26 11:15:55 +00:00
|
|
|
func unique(intSlice []string) []string {
|
|
|
|
keys := make(map[string]bool)
|
|
|
|
var list []string
|
|
|
|
for _, entry := range intSlice {
|
|
|
|
if _, value := keys[entry]; !value {
|
|
|
|
keys[entry] = true
|
|
|
|
list = append(list, entry)
|
|
|
|
}
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
2021-07-26 11:15:55 +00:00
|
|
|
return list
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|