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

fix(rpc): reduce Ethereum rpc API and improve websocket API #1106

Merged
merged 8 commits into from
Sep 20, 2023
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
129 changes: 62 additions & 67 deletions rpc/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,14 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"

"github.com/ethereum/go-ethereum/rpc"

ethermint "github.com/evmos/ethermint/types"
rpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
lumtis marked this conversation as resolved.
Show resolved Hide resolved
"github.com/zeta-chain/zetacore/rpc/backend"
"github.com/zeta-chain/zetacore/rpc/namespaces/ethereum/debug"
"github.com/zeta-chain/zetacore/rpc/namespaces/ethereum/eth"
"github.com/zeta-chain/zetacore/rpc/namespaces/ethereum/eth/filters"
"github.com/zeta-chain/zetacore/rpc/namespaces/ethereum/miner"
"github.com/zeta-chain/zetacore/rpc/namespaces/ethereum/net"
"github.com/zeta-chain/zetacore/rpc/namespaces/ethereum/personal"
"github.com/zeta-chain/zetacore/rpc/namespaces/ethereum/txpool"
"github.com/zeta-chain/zetacore/rpc/namespaces/ethereum/web3"

rpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
)

// RPC namespaces and API version
Expand All @@ -47,8 +40,8 @@ const (

Web3Namespace = "web3"
EthNamespace = "eth"
PersonalNamespace = "personal"
NetNamespace = "net"
PersonalNamespace = "personal"
TxPoolNamespace = "txpool"
DebugNamespace = "debug"
MinerNamespace = "miner"
Expand Down Expand Up @@ -112,64 +105,66 @@ func init() {
},
}
},
PersonalNamespace: func(ctx *server.Context,
clientCtx client.Context,
_ *rpcclient.WSClient,
allowUnprotectedTxs bool,
indexer ethermint.EVMTxIndexer,
) []rpc.API {
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
return []rpc.API{
{
Namespace: PersonalNamespace,
Version: apiVersion,
Service: personal.NewAPI(ctx.Logger, evmBackend),
Public: false,
},
}
},
TxPoolNamespace: func(ctx *server.Context, _ client.Context, _ *rpcclient.WSClient, _ bool, _ ethermint.EVMTxIndexer) []rpc.API {
return []rpc.API{
{
Namespace: TxPoolNamespace,
Version: apiVersion,
Service: txpool.NewPublicAPI(ctx.Logger),
Public: true,
},
}
},
DebugNamespace: func(ctx *server.Context,
clientCtx client.Context,
_ *rpcclient.WSClient,
allowUnprotectedTxs bool,
indexer ethermint.EVMTxIndexer,
) []rpc.API {
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
return []rpc.API{
{
Namespace: DebugNamespace,
Version: apiVersion,
Service: debug.NewAPI(ctx, evmBackend),
Public: true,
},
}
},
MinerNamespace: func(ctx *server.Context,
clientCtx client.Context,
_ *rpcclient.WSClient,
allowUnprotectedTxs bool,
indexer ethermint.EVMTxIndexer,
) []rpc.API {
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
return []rpc.API{
{
Namespace: MinerNamespace,
Version: apiVersion,
Service: miner.NewPrivateAPI(ctx, evmBackend),
Public: false,
},
}
},
// Disabled
// NOTE: Public field of API is deprecated and defined only for compatibility.
//PersonalNamespace: func(ctx *server.Context,
// clientCtx client.Context,
// _ *rpcclient.WSClient,
// allowUnprotectedTxs bool,
// indexer ethermint.EVMTxIndexer,
//) []rpc.API {
// evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
// return []rpc.API{
// {
// Namespace: PersonalNamespace,
// Version: apiVersion,
// Service: personal.NewAPI(ctx.Logger, evmBackend),
// Public: false,
// },
// }
//},
//TxPoolNamespace: func(ctx *server.Context, _ client.Context, _ *rpcclient.WSClient, _ bool, _ ethermint.EVMTxIndexer) []rpc.API {
// return []rpc.API{
// {
// Namespace: TxPoolNamespace,
// Version: apiVersion,
// Service: txpool.NewPublicAPI(ctx.Logger),
// Public: true,
// },
// }
//},
//DebugNamespace: func(ctx *server.Context,
// clientCtx client.Context,
// _ *rpcclient.WSClient,
// allowUnprotectedTxs bool,
// indexer ethermint.EVMTxIndexer,
//) []rpc.API {
// evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
// return []rpc.API{
// {
// Namespace: DebugNamespace,
// Version: apiVersion,
// Service: debug.NewAPI(ctx, evmBackend),
// Public: true,
// },
// }
//},
//MinerNamespace: func(ctx *server.Context,
// clientCtx client.Context,
// _ *rpcclient.WSClient,
// allowUnprotectedTxs bool,
// indexer ethermint.EVMTxIndexer,
//) []rpc.API {
// evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
// return []rpc.API{
// {
// Namespace: MinerNamespace,
// Version: apiVersion,
// Service: miner.NewPrivateAPI(ctx, evmBackend),
// Public: false,
// },
// }
//},
}
}

Expand Down
10 changes: 3 additions & 7 deletions rpc/namespaces/ethereum/debug/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,14 @@ import (
"sync"
"time"

"github.com/davecgh/go-spew/spew"

evmtypes "github.com/evmos/ethermint/x/evm/types"

stderrors "github.com/pkg/errors"

"github.com/cosmos/cosmos-sdk/server"

"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/rlp"
evmtypes "github.com/evmos/ethermint/x/evm/types"
stderrors "github.com/pkg/errors"
"github.com/tendermint/tendermint/libs/log"
"github.com/zeta-chain/zetacore/rpc/backend"
rpctypes "github.com/zeta-chain/zetacore/rpc/types"
Expand Down
60 changes: 30 additions & 30 deletions rpc/namespaces/ethereum/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,14 @@ package eth
import (
"context"

"github.com/ethereum/go-ethereum/signer/core/apitypes"

"github.com/ethereum/go-ethereum/rpc"

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

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

"github.com/zeta-chain/zetacore/rpc/backend"

"github.com/ethereum/go-ethereum/rpc"
ethermint "github.com/evmos/ethermint/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/zeta-chain/zetacore/rpc/backend"
rpctypes "github.com/zeta-chain/zetacore/rpc/types"
)

Expand Down Expand Up @@ -67,7 +61,6 @@ type EthereumAPI interface {
// Allows developers to both send ETH from one address to another, write data
// on-chain, and interact with smart contracts.
SendRawTransaction(data hexutil.Bytes) (common.Hash, error)
SendTransaction(args evmtypes.TransactionArgs) (common.Hash, error)
// eth_sendPrivateTransaction
// eth_cancel PrivateTransaction

Expand Down Expand Up @@ -111,9 +104,7 @@ type EthereumAPI interface {
// Other
Syncing() (interface{}, error)
Coinbase() (string, error)
Sign(address common.Address, data hexutil.Bytes) (hexutil.Bytes, error)
GetTransactionLogs(txHash common.Hash) ([]*ethtypes.Log, error)
SignTypedData(address common.Address, typedData apitypes.TypedData) (hexutil.Bytes, error)
FillTransaction(args evmtypes.TransactionArgs) (*rpctypes.SignTransactionResult, error)
Resend(ctx context.Context, args evmtypes.TransactionArgs, gasPrice *hexutil.Big, gasLimit *hexutil.Uint64) (common.Hash, error)
GetPendingTransactions() ([]*rpctypes.RPCTransaction, error)
Expand All @@ -125,6 +116,11 @@ type EthereumAPI interface {
// eth_getWork (on Ethereum.org)
// eth_submitWork (on Ethereum.org)
// eth_submitHashrate (on Ethereum.org)

// Disabled APIs for security reasons
//SendTransaction(args evmtypes.TransactionArgs) (common.Hash, error)
//Sign(address common.Address, data hexutil.Bytes) (hexutil.Bytes, error)
//SignTypedData(address common.Address, typedData apitypes.TypedData) (hexutil.Bytes, error)
}

var _ EthereumAPI = (*PublicAPI)(nil)
Expand Down Expand Up @@ -230,12 +226,6 @@ func (e *PublicAPI) SendRawTransaction(data hexutil.Bytes) (common.Hash, error)
return e.backend.SendRawTransaction(data)
}

// SendTransaction sends an Ethereum transaction.
func (e *PublicAPI) SendTransaction(args evmtypes.TransactionArgs) (common.Hash, error) {
e.logger.Debug("eth_sendTransaction", "args", args.String())
return e.backend.SendTransaction(args)
}

///////////////////////////////////////////////////////////////////////////////
/// Account Information ///
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -416,12 +406,6 @@ func (e *PublicAPI) Coinbase() (string, error) {
return ethAddr.Hex(), nil
}

// Sign signs the provided data using the private key of address via Geth's signature standard.
func (e *PublicAPI) Sign(address common.Address, data hexutil.Bytes) (hexutil.Bytes, error) {
e.logger.Debug("eth_sign", "address", address.Hex(), "data", common.Bytes2Hex(data))
return e.backend.Sign(address, data)
}

// GetTransactionLogs returns the logs given a transaction hash.
func (e *PublicAPI) GetTransactionLogs(txHash common.Hash) ([]*ethtypes.Log, error) {
e.logger.Debug("eth_getTransactionLogs", "hash", txHash)
Expand All @@ -448,12 +432,6 @@ func (e *PublicAPI) GetTransactionLogs(txHash common.Hash) ([]*ethtypes.Log, err
return backend.TxLogsFromEvents(resBlockResult.TxsResults[res.TxIndex].Events, int(res.MsgIndex))
}

// SignTypedData signs EIP-712 conformant typed data
func (e *PublicAPI) SignTypedData(address common.Address, typedData apitypes.TypedData) (hexutil.Bytes, error) {
e.logger.Debug("eth_signTypedData", "address", address.Hex(), "data", typedData)
return e.backend.SignTypedData(address, typedData)
}

// FillTransaction fills the defaults (nonce, gas, gasPrice or 1559 fields)
// on a given unsigned transaction, and returns it to the caller for further
// processing (signing + broadcast).
Expand Down Expand Up @@ -527,3 +505,25 @@ func (e *PublicAPI) GetPendingTransactions() ([]*rpctypes.RPCTransaction, error)

return result, nil
}

///////////////////////////////////////////////////////////////////////////////
/// Disabled ///
///////////////////////////////////////////////////////////////////////////////

//// SendTransaction sends an Ethereum transaction.
//func (e *PublicAPI) SendTransaction(args evmtypes.TransactionArgs) (common.Hash, error) {
// e.logger.Debug("eth_sendTransaction", "args", args.String())
// return e.backend.SendTransaction(args)
//}
//
//// Sign signs the provided data using the private key of address via Geth's signature standard.
//func (e *PublicAPI) Sign(address common.Address, data hexutil.Bytes) (hexutil.Bytes, error) {
// e.logger.Debug("eth_sign", "address", address.Hex(), "data", common.Bytes2Hex(data))
// return e.backend.Sign(address, data)
//}
//
//// SignTypedData signs EIP-712 conformant typed data
//func (e *PublicAPI) SignTypedData(address common.Address, typedData apitypes.TypedData) (hexutil.Bytes, error) {
// e.logger.Debug("eth_signTypedData", "address", address.Hex(), "data", typedData)
// return e.backend.SignTypedData(address, typedData)
//}
14 changes: 5 additions & 9 deletions rpc/namespaces/ethereum/eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,16 @@ import (
"time"

"github.com/cosmos/cosmos-sdk/client"
"github.com/zeta-chain/zetacore/rpc/types"

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

coretypes "github.com/tendermint/tendermint/rpc/core/types"
rpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/filters"
"github.com/ethereum/go-ethereum/rpc"

evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/tendermint/tendermint/libs/log"
coretypes "github.com/tendermint/tendermint/rpc/core/types"
rpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/zeta-chain/zetacore/rpc/types"
)

// FilterAPI gathers
Expand Down
16 changes: 6 additions & 10 deletions rpc/namespaces/ethereum/eth/filters/filter_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,19 @@ import (
"sync"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/filters"
"github.com/ethereum/go-ethereum/rpc"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/pkg/errors"

tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/libs/log"
tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
coretypes "github.com/tendermint/tendermint/rpc/core/types"
rpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/filters"
"github.com/ethereum/go-ethereum/rpc"

sdk "github.com/cosmos/cosmos-sdk/types"

evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/zeta-chain/zetacore/rpc/ethereum/pubsub"
)

Expand Down
12 changes: 5 additions & 7 deletions rpc/namespaces/ethereum/eth/filters/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ import (
"fmt"
"math/big"

"github.com/zeta-chain/zetacore/rpc/backend"
"github.com/zeta-chain/zetacore/rpc/types"

"github.com/pkg/errors"
"github.com/tendermint/tendermint/libs/log"
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/filters"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/libs/log"
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/zeta-chain/zetacore/rpc/backend"
"github.com/zeta-chain/zetacore/rpc/types"
)

// BloomIV represents the bit indexes and value inside the bloom filter that belong
Expand Down
3 changes: 0 additions & 3 deletions rpc/namespaces/ethereum/miner/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ package miner

import (
"github.com/cosmos/cosmos-sdk/server"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"

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

"github.com/zeta-chain/zetacore/rpc/backend"
)

Expand Down
Loading
Loading