Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rpc service #40

Merged
merged 1 commit into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ require (

require gopkg.in/yaml.v2 v2.4.0

require github.com/spf13/viper v1.9.0
require (
github.com/davecgh/go-spew v1.1.1
github.com/ethereum/go-ethereum v1.10.11
github.com/gorilla/websocket v1.4.2
github.com/improbable-eng/grpc-web v0.15.0
github.com/pkg/errors v0.9.1
github.com/rs/cors v1.8.0
github.com/spf13/viper v1.9.0
github.com/stretchr/testify v1.7.0
)

require (
filippo.io/edwards25519 v1.0.0-beta.2 // indirect
Expand All @@ -50,7 +59,6 @@ require (
github.com/cosmos/ledger-cosmos-go v0.11.1 // indirect
github.com/cosmos/ledger-go v0.9.2 // indirect
github.com/danieljoos/wincred v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea // indirect
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/dgraph-io/badger/v2 v2.2007.2 // indirect
Expand All @@ -59,7 +67,6 @@ require (
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/dvsekhvalnov/jose2go v0.0.0-20200901110807-248326c1351b // indirect
github.com/edsrzf/mmap-go v1.0.0 // indirect
github.com/ethereum/go-ethereum v1.10.11 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect
Expand All @@ -75,7 +82,6 @@ require (
github.com/google/orderedcode v0.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.0.1 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
Expand All @@ -89,7 +95,6 @@ require (
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.0 // indirect
github.com/huin/goupnp v1.0.2 // indirect
github.com/improbable-eng/grpc-web v0.15.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2-0.20160603034137-1fa385a6f458 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
Expand All @@ -108,7 +113,6 @@ require (
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand All @@ -118,14 +122,12 @@ require (
github.com/rakyll/statik v0.1.7 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rjeczalik/notify v0.9.1 // indirect
github.com/rs/cors v1.8.0 // indirect
github.com/rs/zerolog v1.26.0 // indirect
github.com/sasha-s/go-deadlock v0.2.1-0.20190427202633-1595213edefa // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/status-im/keycard-go v0.0.0-20200402102358-957c09536969 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
Expand Down
11 changes: 11 additions & 0 deletions rpc/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/oracleNetworkProtocol/plugchain/rpc/ethereum/namespaces/miner"
"github.com/oracleNetworkProtocol/plugchain/rpc/ethereum/namespaces/net"
"github.com/oracleNetworkProtocol/plugchain/rpc/ethereum/namespaces/personal"
plugchainrpc "github.com/oracleNetworkProtocol/plugchain/rpc/ethereum/namespaces/rpc"
"github.com/oracleNetworkProtocol/plugchain/rpc/ethereum/namespaces/txpool"
"github.com/oracleNetworkProtocol/plugchain/rpc/ethereum/namespaces/web3"
"github.com/oracleNetworkProtocol/plugchain/rpc/ethereum/types"
Expand All @@ -31,6 +32,7 @@ const (
TxPoolNamespace = "txpool"
DebugNamespace = "debug"
MinerNamespace = "miner"
RpcNamespace = "rpc"

apiVersion = "1.0"
)
Expand Down Expand Up @@ -115,6 +117,15 @@ func GetRPCAPIs(ctx *server.Context, clientCtx client.Context, tmWSClient *rpccl
Public: false,
},
)
case RpcNamespace:
apis = append(apis,
rpc.API{
Namespace: RpcNamespace,
Version: apiVersion,
Service: plugchainrpc.NewPublicAPI(ctx.Logger, clientCtx, evmBackend, nonceLock),
Public: true,
},
)
default:
ctx.Logger.Error("invalid namespace value", "namespace", selectedAPIs[index])
}
Expand Down
86 changes: 86 additions & 0 deletions rpc/ethereum/namespaces/rpc/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package rpc

import (
"context"
"math/big"

"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"

ethtypes "github.com/ethereum/go-ethereum/core/types"

"github.com/oracleNetworkProtocol/plugchain/rpc/ethereum/backend"
rpctypes "github.com/oracleNetworkProtocol/plugchain/rpc/ethereum/types"
"github.com/tharsis/ethermint/crypto/hd"
ethermint "github.com/tharsis/ethermint/types"
evmtypes "github.com/tharsis/ethermint/x/evm/types"
)

// PublicAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec.
type PublicAPI struct {
ctx context.Context
logger log.Logger
backend backend.Backend
clientCtx client.Context
queryClient *rpctypes.QueryClient
chainIDEpoch *big.Int
nonceLock *rpctypes.AddrLocker
signer ethtypes.Signer
}

// NewPublicAPI creates an instance of the public ETH Web3 API.
func NewPublicAPI(
logger log.Logger,
clientCtx client.Context,
backend backend.Backend,
nonceLock *rpctypes.AddrLocker,
) *PublicAPI {
eip155ChainID, err := ethermint.ParseChainID(clientCtx.ChainID)
if err != nil {
panic(err)
}

algos, _ := clientCtx.Keyring.SupportedAlgorithms()

if !algos.Contains(hd.EthSecp256k1) {
kr, err := keyring.New(
sdk.KeyringServiceName(),
viper.GetString(flags.FlagKeyringBackend),
clientCtx.KeyringDir,
clientCtx.Input,
hd.EthSecp256k1Option(),
)
if err != nil {
panic(err)
}

clientCtx = clientCtx.WithKeyring(kr)
}

// The signer used by the API should always be the 'latest' known one because we expect
// signers to be backwards-compatible with old transactions.
cfg := backend.ChainConfig()
if cfg == nil {
cfg = evmtypes.DefaultChainConfig().EthereumConfig(eip155ChainID)
}

signer := ethtypes.LatestSigner(cfg)

api := &PublicAPI{
ctx: context.Background(),
clientCtx: clientCtx,
queryClient: rpctypes.NewQueryClient(clientCtx),
chainIDEpoch: eip155ChainID,
logger: logger.With("module", "rpc"),
backend: backend,
nonceLock: nonceLock,
signer: signer,
}

return api
}
4 changes: 2 additions & 2 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ func (c EVMConfig) Validate() error {

// GetDefaultAPINamespaces returns the default list of JSON-RPC namespaces that should be enabled
func GetDefaultAPINamespaces() []string {
return []string{"eth", "net", "web3"}
return []string{"eth", "net", "web3", "rpc"}
}

// GetAPINamespaces returns the all the available JSON-RPC API namespaces.
func GetAPINamespaces() []string {
return []string{"web3", "eth", "personal", "net", "txpool", "debug", "miner"}
return []string{"web3", "eth", "personal", "net", "txpool", "debug", "miner", "rpc"}
}

// DefaultJSONRPCConfig returns an EVM config with the JSON-RPC API enabled by default
Expand Down