2022-05-02 06:26:24 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2022-08-09 05:52:28 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2022-05-02 06:26:24 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/server"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2022-08-09 05:52:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/signer/core/apitypes"
|
|
|
|
rpctypes "github.com/evmos/ethermint/rpc/types"
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/server/config"
|
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
2022-05-02 06:26:24 +00:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BackendI implements the Cosmos and EVM backend.
|
2022-08-08 08:17:10 +00:00
|
|
|
type BackendI interface { //nolint: revive
|
2022-05-02 06:26:24 +00:00
|
|
|
CosmosBackend
|
|
|
|
EVMBackend
|
|
|
|
}
|
|
|
|
|
|
|
|
// CosmosBackend implements the functionality shared within cosmos namespaces
|
|
|
|
// as defined by Wallet Connect V2: https://docs.walletconnect.com/2.0/json-rpc/cosmos.
|
|
|
|
// Implemented by Backend.
|
2022-06-05 09:22:33 +00:00
|
|
|
type CosmosBackend interface { // TODO: define
|
2022-05-02 06:26:24 +00:00
|
|
|
// GetAccounts()
|
|
|
|
// SignDirect()
|
|
|
|
// SignAmino()
|
|
|
|
}
|
|
|
|
|
|
|
|
// EVMBackend implements the functionality shared within ethereum namespaces
|
|
|
|
// as defined by EIP-1474: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md
|
|
|
|
// Implemented by Backend.
|
|
|
|
type EVMBackend interface {
|
2022-08-09 05:52:28 +00:00
|
|
|
// Node specific queries
|
|
|
|
Accounts() ([]common.Address, error)
|
|
|
|
Syncing() (interface{}, error)
|
|
|
|
SetEtherbase(etherbase common.Address) bool
|
2022-08-12 06:23:13 +00:00
|
|
|
SetGasPrice(gasPrice hexutil.Big) bool
|
2022-08-09 05:52:28 +00:00
|
|
|
ImportRawKey(privkey, password string) (common.Address, error)
|
|
|
|
ListAccounts() ([]common.Address, error)
|
|
|
|
NewMnemonic(uid string, language keyring.Language, hdPath, bip39Passphrase string, algo keyring.SignatureAlgo) (*keyring.Record, error)
|
|
|
|
UnprotectedAllowed() bool
|
2022-05-02 06:26:24 +00:00
|
|
|
RPCGasCap() uint64 // global gas cap for eth_call over rpc: DoS protection
|
|
|
|
RPCEVMTimeout() time.Duration // global timeout for eth_call over rpc: DoS protection
|
|
|
|
RPCTxFeeCap() float64 // RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for send-transaction variants. The unit is ether.
|
|
|
|
RPCMinGasPrice() int64
|
|
|
|
|
2022-08-09 05:52:28 +00:00
|
|
|
// Sign Tx
|
|
|
|
Sign(address common.Address, data hexutil.Bytes) (hexutil.Bytes, error)
|
|
|
|
SendTransaction(args evmtypes.TransactionArgs) (common.Hash, error)
|
|
|
|
SignTypedData(address common.Address, typedData apitypes.TypedData) (hexutil.Bytes, error)
|
|
|
|
|
|
|
|
// Blocks Info
|
2022-05-02 06:26:24 +00:00
|
|
|
BlockNumber() (hexutil.Uint64, error)
|
2022-08-09 05:52:28 +00:00
|
|
|
GetBlockByNumber(blockNum rpctypes.BlockNumber, fullTx bool) (map[string]interface{}, error)
|
2022-05-02 06:26:24 +00:00
|
|
|
GetBlockByHash(hash common.Hash, fullTx bool) (map[string]interface{}, error)
|
2022-08-09 05:52:28 +00:00
|
|
|
GetBlockTransactionCountByHash(hash common.Hash) *hexutil.Uint
|
|
|
|
GetBlockTransactionCountByNumber(blockNum rpctypes.BlockNumber) *hexutil.Uint
|
2022-09-05 14:07:56 +00:00
|
|
|
TendermintBlockByNumber(blockNum rpctypes.BlockNumber) (*tmrpctypes.ResultBlock, error)
|
|
|
|
TendermintBlockResultByNumber(height *int64) (*tmrpctypes.ResultBlockResults, error)
|
|
|
|
TendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.ResultBlock, error)
|
|
|
|
BlockNumberFromTendermint(blockNrOrHash rpctypes.BlockNumberOrHash) (rpctypes.BlockNumber, error)
|
|
|
|
BlockNumberFromTendermintByHash(blockHash common.Hash) (*big.Int, error)
|
|
|
|
EthMsgsFromTendermintBlock(block *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults) []*evmtypes.MsgEthereumTx
|
2022-08-09 05:52:28 +00:00
|
|
|
BlockBloom(blockRes *tmrpctypes.ResultBlockResults) (ethtypes.Bloom, error)
|
|
|
|
HeaderByNumber(blockNum rpctypes.BlockNumber) (*ethtypes.Header, error)
|
|
|
|
HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error)
|
2022-09-05 14:07:56 +00:00
|
|
|
RPCBlockFromTendermintBlock(resBlock *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults, fullTx bool) (map[string]interface{}, error)
|
|
|
|
EthBlockByNumber(blockNum rpctypes.BlockNumber) (*ethtypes.Block, error)
|
|
|
|
EthBlockFromTendermintBlock(resBlock *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults) (*ethtypes.Block, error)
|
2022-08-09 05:52:28 +00:00
|
|
|
|
|
|
|
// Account Info
|
|
|
|
GetCode(address common.Address, blockNrOrHash rpctypes.BlockNumberOrHash) (hexutil.Bytes, error)
|
|
|
|
GetBalance(address common.Address, blockNrOrHash rpctypes.BlockNumberOrHash) (*hexutil.Big, error)
|
|
|
|
GetStorageAt(address common.Address, key string, blockNrOrHash rpctypes.BlockNumberOrHash) (hexutil.Bytes, error)
|
|
|
|
GetProof(address common.Address, storageKeys []string, blockNrOrHash rpctypes.BlockNumberOrHash) (*rpctypes.AccountResult, error)
|
|
|
|
GetTransactionCount(address common.Address, blockNum rpctypes.BlockNumber) (*hexutil.Uint64, error)
|
|
|
|
|
|
|
|
// Chain Info
|
|
|
|
ChainID() (*hexutil.Big, error)
|
|
|
|
ChainConfig() *params.ChainConfig
|
|
|
|
GlobalMinGasPrice() (sdk.Dec, error)
|
|
|
|
BaseFee(blockRes *tmrpctypes.ResultBlockResults) (*big.Int, error)
|
|
|
|
CurrentHeader() *ethtypes.Header
|
2022-05-02 06:26:24 +00:00
|
|
|
PendingTransactions() ([]*sdk.Tx, error)
|
|
|
|
GetCoinbase() (sdk.AccAddress, error)
|
2022-08-09 05:52:28 +00:00
|
|
|
FeeHistory(blockCount rpc.DecimalOrHex, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*rpctypes.FeeHistoryResult, error)
|
|
|
|
SuggestGasTipCap(baseFee *big.Int) (*big.Int, error)
|
|
|
|
|
|
|
|
// Tx Info
|
|
|
|
GetTransactionByHash(txHash common.Hash) (*rpctypes.RPCTransaction, error)
|
2022-08-11 20:49:05 +00:00
|
|
|
GetTxByEthHash(txHash common.Hash) (*ethermint.TxResult, error)
|
|
|
|
GetTxByTxIndex(height int64, txIndex uint) (*ethermint.TxResult, error)
|
2022-08-09 05:52:28 +00:00
|
|
|
GetTransactionByBlockAndIndex(block *tmrpctypes.ResultBlock, idx hexutil.Uint) (*rpctypes.RPCTransaction, error)
|
|
|
|
GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error)
|
|
|
|
GetTransactionByBlockHashAndIndex(hash common.Hash, idx hexutil.Uint) (*rpctypes.RPCTransaction, error)
|
|
|
|
GetTransactionByBlockNumberAndIndex(blockNum rpctypes.BlockNumber, idx hexutil.Uint) (*rpctypes.RPCTransaction, error)
|
2022-05-02 06:26:24 +00:00
|
|
|
|
2022-08-09 05:52:28 +00:00
|
|
|
// Send Transaction
|
|
|
|
Resend(args evmtypes.TransactionArgs, gasPrice *hexutil.Big, gasLimit *hexutil.Uint64) (common.Hash, error)
|
|
|
|
SendRawTransaction(data hexutil.Bytes) (common.Hash, error)
|
|
|
|
SetTxDefaults(args evmtypes.TransactionArgs) (evmtypes.TransactionArgs, error)
|
|
|
|
EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *rpctypes.BlockNumber) (hexutil.Uint64, error)
|
|
|
|
DoCall(args evmtypes.TransactionArgs, blockNr rpctypes.BlockNumber) (*evmtypes.MsgEthereumTxResponse, error)
|
2022-09-05 14:07:56 +00:00
|
|
|
GasPrice() (*hexutil.Big, error)
|
2022-05-02 06:26:24 +00:00
|
|
|
|
|
|
|
// Filter API
|
|
|
|
GetLogs(hash common.Hash) ([][]*ethtypes.Log, error)
|
|
|
|
GetLogsByHeight(height *int64) ([][]*ethtypes.Log, error)
|
2022-08-09 05:52:28 +00:00
|
|
|
BloomStatus() (uint64, uint64)
|
|
|
|
|
|
|
|
// Tracing
|
|
|
|
TraceTransaction(hash common.Hash, config *evmtypes.TraceConfig) (interface{}, error)
|
|
|
|
TraceBlock(height rpctypes.BlockNumber, config *evmtypes.TraceConfig, block *tmrpctypes.ResultBlock) ([]*evmtypes.TxTraceResult, error)
|
2022-05-02 06:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ BackendI = (*Backend)(nil)
|
|
|
|
|
2022-08-09 05:52:28 +00:00
|
|
|
var bAttributeKeyEthereumBloom = []byte(evmtypes.AttributeKeyEthereumBloom)
|
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
// Backend implements the BackendI interface
|
|
|
|
type Backend struct {
|
2022-06-22 10:50:36 +00:00
|
|
|
ctx context.Context
|
|
|
|
clientCtx client.Context
|
2022-08-09 05:52:28 +00:00
|
|
|
queryClient *rpctypes.QueryClient // gRPC query client
|
2022-06-22 10:50:36 +00:00
|
|
|
logger log.Logger
|
|
|
|
chainID *big.Int
|
|
|
|
cfg config.Config
|
|
|
|
allowUnprotectedTxs bool
|
2022-08-11 20:49:05 +00:00
|
|
|
indexer ethermint.EVMTxIndexer
|
2022-05-02 06:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBackend creates a new Backend instance for cosmos and ethereum namespaces
|
2022-08-11 20:49:05 +00:00
|
|
|
func NewBackend(
|
|
|
|
ctx *server.Context,
|
|
|
|
logger log.Logger,
|
|
|
|
clientCtx client.Context,
|
|
|
|
allowUnprotectedTxs bool,
|
|
|
|
indexer ethermint.EVMTxIndexer,
|
|
|
|
) *Backend {
|
2022-05-02 06:26:24 +00:00
|
|
|
chainID, err := ethermint.ParseChainID(clientCtx.ChainID)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-08-25 06:06:45 +00:00
|
|
|
appConf, err := config.GetConfig(ctx.Viper)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-05-02 06:26:24 +00:00
|
|
|
|
|
|
|
return &Backend{
|
2022-06-22 10:50:36 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
clientCtx: clientCtx,
|
2022-08-09 05:52:28 +00:00
|
|
|
queryClient: rpctypes.NewQueryClient(clientCtx),
|
2022-06-22 10:50:36 +00:00
|
|
|
logger: logger.With("module", "backend"),
|
|
|
|
chainID: chainID,
|
|
|
|
cfg: appConf,
|
|
|
|
allowUnprotectedTxs: allowUnprotectedTxs,
|
2022-08-11 20:49:05 +00:00
|
|
|
indexer: indexer,
|
2022-05-02 06:26:24 +00:00
|
|
|
}
|
|
|
|
}
|