2018-08-09 10:36:47 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2019-09-18 18:45:21 +00:00
|
|
|
"bytes"
|
2020-04-01 18:49:21 +00:00
|
|
|
"errors"
|
2019-07-25 20:38:55 +00:00
|
|
|
"fmt"
|
2019-10-22 16:40:34 +00:00
|
|
|
"log"
|
2019-08-28 17:43:34 +00:00
|
|
|
"math/big"
|
2020-07-08 07:57:19 +00:00
|
|
|
"strconv"
|
2020-04-22 19:26:01 +00:00
|
|
|
"strings"
|
2019-10-31 15:09:40 +00:00
|
|
|
"sync"
|
2019-08-28 17:43:34 +00:00
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
"github.com/gogo/protobuf/jsonpb"
|
2020-04-01 18:49:21 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
"github.com/cosmos/ethermint/codec"
|
2019-09-18 18:45:21 +00:00
|
|
|
emintcrypto "github.com/cosmos/ethermint/crypto"
|
2019-10-30 18:30:24 +00:00
|
|
|
params "github.com/cosmos/ethermint/rpc/args"
|
2019-10-22 16:40:34 +00:00
|
|
|
emint "github.com/cosmos/ethermint/types"
|
2019-09-26 15:36:23 +00:00
|
|
|
"github.com/cosmos/ethermint/utils"
|
2018-08-09 10:36:47 +00:00
|
|
|
"github.com/cosmos/ethermint/version"
|
2020-06-26 22:26:55 +00:00
|
|
|
evmtypes "github.com/cosmos/ethermint/x/evm/types"
|
2019-09-18 18:45:21 +00:00
|
|
|
|
2020-05-12 19:12:52 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
"github.com/tendermint/tendermint/crypto/merkle"
|
2019-10-16 00:46:50 +00:00
|
|
|
"github.com/tendermint/tendermint/rpc/client"
|
2019-09-24 18:39:17 +00:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
|
2019-09-18 18:45:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
2018-08-09 10:36:47 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2019-10-04 19:32:56 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2019-09-18 20:14:39 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2019-09-20 13:30:20 +00:00
|
|
|
|
2019-09-24 18:39:17 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2019-09-20 13:30:20 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2020-04-22 19:26:01 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2019-09-24 18:39:17 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-22 19:26:01 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2020-05-12 19:12:52 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2020-04-22 19:26:01 +00:00
|
|
|
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
|
2019-10-22 16:40:34 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2018-08-09 10:36:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PublicEthAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec.
|
2019-07-25 20:38:55 +00:00
|
|
|
type PublicEthAPI struct {
|
2020-06-08 18:33:48 +00:00
|
|
|
cliCtx context.CLIContext
|
|
|
|
backend Backend
|
2020-07-06 17:03:34 +00:00
|
|
|
keys []emintcrypto.PrivKeySecp256k1
|
2020-06-08 18:33:48 +00:00
|
|
|
nonceLock *AddrLocker
|
|
|
|
keybaseLock sync.Mutex
|
2019-07-15 14:13:59 +00:00
|
|
|
}
|
2018-08-09 10:36:47 +00:00
|
|
|
|
|
|
|
// NewPublicEthAPI creates an instance of the public ETH Web3 API.
|
2020-04-02 00:43:59 +00:00
|
|
|
func NewPublicEthAPI(cliCtx context.CLIContext, backend Backend, nonceLock *AddrLocker,
|
2020-07-06 17:03:34 +00:00
|
|
|
key []emintcrypto.PrivKeySecp256k1) *PublicEthAPI {
|
2019-09-20 13:30:20 +00:00
|
|
|
|
2019-07-15 14:13:59 +00:00
|
|
|
return &PublicEthAPI{
|
2019-09-20 13:30:20 +00:00
|
|
|
cliCtx: cliCtx,
|
2020-04-02 00:43:59 +00:00
|
|
|
backend: backend,
|
2020-07-06 17:03:34 +00:00
|
|
|
keys: key,
|
2019-09-20 13:30:20 +00:00
|
|
|
nonceLock: nonceLock,
|
2019-07-15 14:13:59 +00:00
|
|
|
}
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProtocolVersion returns the supported Ethereum protocol version.
|
2019-09-24 14:49:40 +00:00
|
|
|
func (e *PublicEthAPI) ProtocolVersion() hexutil.Uint {
|
|
|
|
return hexutil.Uint(version.ProtocolVersion)
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 07:57:19 +00:00
|
|
|
// ChainId returns the chain's identifier in hex format
|
|
|
|
func (e *PublicEthAPI) ChainId() (hexutil.Uint, error) { // nolint
|
|
|
|
// parse the chainID from a integer string
|
|
|
|
intChainID, err := strconv.ParseUint(e.cliCtx.ChainID, 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("invalid chainID: %s, must be integer format", e.cliCtx.ChainID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hexutil.Uint(intChainID), nil
|
|
|
|
}
|
|
|
|
|
2018-08-09 10:36:47 +00:00
|
|
|
// Syncing returns whether or not the current node is syncing with other peers. Returns false if not, or a struct
|
|
|
|
// outlining the state of the sync if it is.
|
2019-09-26 15:33:25 +00:00
|
|
|
func (e *PublicEthAPI) Syncing() (interface{}, error) {
|
|
|
|
status, err := e.cliCtx.Client.Status()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !status.SyncInfo.CatchingUp {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return map[string]interface{}{
|
|
|
|
// "startingBlock": nil, // NA
|
|
|
|
"currentBlock": hexutil.Uint64(status.SyncInfo.LatestBlockHeight),
|
|
|
|
// "highestBlock": nil, // NA
|
|
|
|
// "pulledStates": nil, // NA
|
|
|
|
// "knownStates": nil, // NA
|
|
|
|
}, nil
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 16:43:37 +00:00
|
|
|
// Coinbase is the address that staking rewards will be send to (alias for Etherbase).
|
|
|
|
func (e *PublicEthAPI) Coinbase() (common.Address, error) {
|
|
|
|
node, err := e.cliCtx.GetNode()
|
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err := node.Status()
|
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return common.BytesToAddress(status.ValidatorInfo.Address.Bytes()), nil
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mining returns whether or not this node is currently mining. Always false.
|
|
|
|
func (e *PublicEthAPI) Mining() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hashrate returns the current node's hashrate. Always 0.
|
|
|
|
func (e *PublicEthAPI) Hashrate() hexutil.Uint64 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// GasPrice returns the current gas price based on Ethermint's gas price oracle.
|
|
|
|
func (e *PublicEthAPI) GasPrice() *hexutil.Big {
|
|
|
|
out := big.NewInt(0)
|
|
|
|
return (*hexutil.Big)(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accounts returns the list of accounts available to this node.
|
2019-09-18 13:58:48 +00:00
|
|
|
func (e *PublicEthAPI) Accounts() ([]common.Address, error) {
|
2020-06-08 18:33:48 +00:00
|
|
|
e.keybaseLock.Lock()
|
2019-10-31 15:09:40 +00:00
|
|
|
|
2019-09-18 13:58:48 +00:00
|
|
|
addresses := make([]common.Address, 0) // return [] instead of nil if empty
|
2020-04-22 19:26:01 +00:00
|
|
|
|
2020-06-08 18:33:48 +00:00
|
|
|
keybase, err := keyring.NewKeyring(
|
2020-04-22 19:26:01 +00:00
|
|
|
sdk.KeyringServiceName(),
|
|
|
|
viper.GetString(flags.FlagKeyringBackend),
|
|
|
|
viper.GetString(flags.FlagHome),
|
|
|
|
e.cliCtx.Input,
|
|
|
|
)
|
2019-09-18 13:58:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return addresses, err
|
|
|
|
}
|
|
|
|
|
2020-06-08 18:33:48 +00:00
|
|
|
infos, err := keybase.List()
|
2019-09-18 13:58:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return addresses, err
|
|
|
|
}
|
|
|
|
|
2020-06-08 18:33:48 +00:00
|
|
|
e.keybaseLock.Unlock()
|
2019-10-31 15:09:40 +00:00
|
|
|
|
2019-09-18 13:58:48 +00:00
|
|
|
for _, info := range infos {
|
|
|
|
addressBytes := info.GetPubKey().Address().Bytes()
|
|
|
|
addresses = append(addresses, common.BytesToAddress(addressBytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses, nil
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlockNumber returns the current block number.
|
2019-09-24 14:49:40 +00:00
|
|
|
func (e *PublicEthAPI) BlockNumber() (hexutil.Uint64, error) {
|
2020-04-02 00:43:59 +00:00
|
|
|
return e.backend.BlockNumber()
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBalance returns the provided account's balance up to the provided block number.
|
2019-09-24 21:38:58 +00:00
|
|
|
func (e *PublicEthAPI) GetBalance(address common.Address, blockNum BlockNumber) (*hexutil.Big, error) {
|
2019-08-28 17:43:34 +00:00
|
|
|
ctx := e.cliCtx.WithHeight(blockNum.Int64())
|
2020-06-26 22:26:55 +00:00
|
|
|
res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/balance/%s", evmtypes.ModuleName, address.Hex()), nil)
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
2019-09-03 12:48:30 +00:00
|
|
|
return nil, err
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
var out evmtypes.QueryResBalance
|
2019-07-25 20:38:55 +00:00
|
|
|
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
|
2019-09-26 15:36:23 +00:00
|
|
|
val, err := utils.UnmarshalBigInt(out.Balance)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-24 14:49:40 +00:00
|
|
|
|
2019-09-26 15:36:23 +00:00
|
|
|
return (*hexutil.Big)(val), nil
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetStorageAt returns the contract storage at the given address, block number, and key.
|
2019-09-24 21:38:58 +00:00
|
|
|
func (e *PublicEthAPI) GetStorageAt(address common.Address, key string, blockNum BlockNumber) (hexutil.Bytes, error) {
|
2019-08-28 17:43:34 +00:00
|
|
|
ctx := e.cliCtx.WithHeight(blockNum.Int64())
|
2020-06-26 22:26:55 +00:00
|
|
|
res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/storage/%s/%s", evmtypes.ModuleName, address.Hex(), key), nil)
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
2019-09-03 12:48:30 +00:00
|
|
|
return nil, err
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
var out evmtypes.QueryResStorage
|
2019-07-25 20:38:55 +00:00
|
|
|
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
|
2019-09-26 15:36:23 +00:00
|
|
|
return out.Value, nil
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionCount returns the number of transactions at the given address up to the given block number.
|
2019-09-24 21:38:58 +00:00
|
|
|
func (e *PublicEthAPI) GetTransactionCount(address common.Address, blockNum BlockNumber) (*hexutil.Uint64, error) {
|
2019-09-06 14:46:26 +00:00
|
|
|
ctx := e.cliCtx.WithHeight(blockNum.Int64())
|
2020-05-17 15:15:32 +00:00
|
|
|
|
|
|
|
// Get nonce (sequence) from account
|
|
|
|
from := sdk.AccAddress(address.Bytes())
|
|
|
|
authclient.Codec = codec.NewAppCodec(ctx.Codec)
|
|
|
|
accRet := authtypes.NewAccountRetriever(authclient.Codec, ctx)
|
|
|
|
|
2020-06-26 18:15:54 +00:00
|
|
|
err := accRet.EnsureExists(from)
|
|
|
|
if err != nil {
|
|
|
|
// account doesn't exist yet, return 0
|
|
|
|
n := hexutil.Uint64(0)
|
|
|
|
return &n, nil
|
|
|
|
}
|
|
|
|
|
2020-05-17 15:15:32 +00:00
|
|
|
_, nonce, err := accRet.GetAccountNumberSequence(from)
|
2019-09-06 14:46:26 +00:00
|
|
|
if err != nil {
|
2019-09-24 14:49:40 +00:00
|
|
|
return nil, err
|
2019-09-06 14:46:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 15:15:32 +00:00
|
|
|
n := hexutil.Uint64(nonce)
|
|
|
|
return &n, nil
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockTransactionCountByHash returns the number of transactions in the block identified by hash.
|
2019-09-29 20:43:26 +00:00
|
|
|
func (e *PublicEthAPI) GetBlockTransactionCountByHash(hash common.Hash) *hexutil.Uint {
|
2020-06-26 22:26:55 +00:00
|
|
|
res, _, err := e.cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s", evmtypes.ModuleName, evmtypes.QueryHashToHeight, hash.Hex()))
|
2019-09-03 00:42:58 +00:00
|
|
|
if err != nil {
|
2019-09-29 20:43:26 +00:00
|
|
|
// Return nil if block does not exist
|
|
|
|
return nil
|
2019-09-03 00:42:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
var out evmtypes.QueryResBlockNumber
|
2019-09-29 20:43:26 +00:00
|
|
|
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
|
|
|
|
return e.getBlockTransactionCountByNumber(out.Number)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockTransactionCountByNumber returns the number of transactions in the block identified by number.
|
|
|
|
func (e *PublicEthAPI) GetBlockTransactionCountByNumber(blockNum BlockNumber) *hexutil.Uint {
|
2019-09-03 00:42:58 +00:00
|
|
|
height := blockNum.Int64()
|
2019-09-29 20:43:26 +00:00
|
|
|
return e.getBlockTransactionCountByNumber(height)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *PublicEthAPI) getBlockTransactionCountByNumber(number int64) *hexutil.Uint {
|
|
|
|
block, err := e.cliCtx.Client.Block(&number)
|
2019-09-03 00:42:58 +00:00
|
|
|
if err != nil {
|
2019-09-29 20:43:26 +00:00
|
|
|
// Return nil if block doesn't exist
|
|
|
|
return nil
|
2019-09-03 00:42:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
n := hexutil.Uint(len(block.Block.Txs))
|
2019-09-29 20:43:26 +00:00
|
|
|
return &n
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUncleCountByBlockHash returns the number of uncles in the block idenfied by hash. Always zero.
|
|
|
|
func (e *PublicEthAPI) GetUncleCountByBlockHash(hash common.Hash) hexutil.Uint {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUncleCountByBlockNumber returns the number of uncles in the block idenfied by number. Always zero.
|
2019-09-24 21:38:58 +00:00
|
|
|
func (e *PublicEthAPI) GetUncleCountByBlockNumber(blockNum BlockNumber) hexutil.Uint {
|
2018-08-09 10:36:47 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCode returns the contract code at the given address and block number.
|
2019-09-24 21:38:58 +00:00
|
|
|
func (e *PublicEthAPI) GetCode(address common.Address, blockNumber BlockNumber) (hexutil.Bytes, error) {
|
2019-08-28 17:43:34 +00:00
|
|
|
ctx := e.cliCtx.WithHeight(blockNumber.Int64())
|
2020-06-26 22:26:55 +00:00
|
|
|
res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", evmtypes.ModuleName, evmtypes.QueryCode, address.Hex()), nil)
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
2019-09-03 12:48:30 +00:00
|
|
|
return nil, err
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
var out evmtypes.QueryResCode
|
2019-07-25 20:38:55 +00:00
|
|
|
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
|
2019-09-26 15:36:23 +00:00
|
|
|
return out.Code, nil
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:02:26 +00:00
|
|
|
// GetTransactionLogs returns the logs given a transaction hash.
|
|
|
|
func (e *PublicEthAPI) GetTransactionLogs(txHash common.Hash) ([]*ethtypes.Log, error) {
|
|
|
|
return e.backend.GetTransactionLogs(txHash)
|
2020-04-02 00:43:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 18:23:36 +00:00
|
|
|
// ExportAccount exports an account's balance, code, and storage at the given block number
|
|
|
|
// TODO: deprecate this once the export genesis command works
|
|
|
|
func (e *PublicEthAPI) ExportAccount(address common.Address, blockNumber BlockNumber) (string, error) {
|
|
|
|
ctx := e.cliCtx.WithHeight(blockNumber.Int64())
|
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", evmtypes.ModuleName, evmtypes.QueryExportAccount, address.Hex()), nil)
|
2020-06-17 18:23:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(res), nil
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:03:34 +00:00
|
|
|
func checkKeyInKeyring(keys []emintcrypto.PrivKeySecp256k1, address common.Address) (key emintcrypto.PrivKeySecp256k1, exist bool) {
|
|
|
|
if len(keys) > 0 {
|
|
|
|
for _, key := range keys {
|
|
|
|
if bytes.Equal(key.PubKey().Address().Bytes(), address.Bytes()) {
|
|
|
|
return key, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2018-08-09 10:36:47 +00:00
|
|
|
// Sign signs the provided data using the private key of address via Geth's signature standard.
|
2019-09-18 18:45:21 +00:00
|
|
|
func (e *PublicEthAPI) Sign(address common.Address, data hexutil.Bytes) (hexutil.Bytes, error) {
|
|
|
|
// TODO: Change this functionality to find an unlocked account by address
|
2020-07-06 17:03:34 +00:00
|
|
|
|
|
|
|
key, exist := checkKeyInKeyring(e.keys, address)
|
|
|
|
if !exist {
|
2019-09-18 18:45:21 +00:00
|
|
|
return nil, keystore.ErrLocked
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign the requested hash with the wallet
|
2020-07-06 17:03:34 +00:00
|
|
|
signature, err := key.Sign(data)
|
2019-09-18 18:45:21 +00:00
|
|
|
if err == nil {
|
|
|
|
signature[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
|
|
|
|
}
|
|
|
|
|
|
|
|
return signature, err
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendTransaction sends an Ethereum transaction.
|
2019-10-30 18:30:24 +00:00
|
|
|
func (e *PublicEthAPI) SendTransaction(args params.SendTxArgs) (common.Hash, error) {
|
2019-09-20 13:30:20 +00:00
|
|
|
// TODO: Change this functionality to find an unlocked account by address
|
2020-07-06 17:03:34 +00:00
|
|
|
|
|
|
|
key, exist := checkKeyInKeyring(e.keys, args.From)
|
|
|
|
if !exist {
|
2019-09-20 13:30:20 +00:00
|
|
|
return common.Hash{}, keystore.ErrLocked
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutex lock the address' nonce to avoid assigning it to multiple requests
|
|
|
|
if args.Nonce == nil {
|
|
|
|
e.nonceLock.LockAddr(args.From)
|
|
|
|
defer e.nonceLock.UnlockAddr(args.From)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assemble transaction from fields
|
2019-11-15 17:02:13 +00:00
|
|
|
tx, err := e.generateFromArgs(args)
|
2019-09-20 13:30:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChainID must be set as flag to send transaction
|
|
|
|
chainID := viper.GetString(flags.FlagChainID)
|
|
|
|
// parse the chainID from a string to a base-10 integer
|
|
|
|
intChainID, ok := new(big.Int).SetString(chainID, 10)
|
|
|
|
if !ok {
|
2020-04-01 18:49:21 +00:00
|
|
|
return common.Hash{}, fmt.Errorf("invalid chainID: %s, must be integer format", chainID)
|
2019-09-20 13:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sign transaction
|
2020-07-06 17:03:34 +00:00
|
|
|
if err := tx.Sign(intChainID, key.ToECDSA()); err != nil {
|
2020-04-23 15:49:25 +00:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
2019-09-20 13:30:20 +00:00
|
|
|
|
|
|
|
// Encode transaction by default Tx encoder
|
2020-04-22 19:26:01 +00:00
|
|
|
txEncoder := authclient.GetTxEncoder(e.cliCtx.Codec)
|
2019-09-20 13:30:20 +00:00
|
|
|
txBytes, err := txEncoder(tx)
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2020-05-03 01:56:18 +00:00
|
|
|
// Broadcast transaction in sync mode (default)
|
2019-09-20 13:30:20 +00:00
|
|
|
res, err := e.cliCtx.BroadcastTx(txBytes)
|
|
|
|
// If error is encountered on the node, the broadcast will not return an error
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2019-09-24 18:41:59 +00:00
|
|
|
// Return transaction hash
|
|
|
|
return common.HexToHash(res.TxHash), nil
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendRawTransaction send a raw Ethereum transaction.
|
2019-09-18 20:14:39 +00:00
|
|
|
func (e *PublicEthAPI) SendRawTransaction(data hexutil.Bytes) (common.Hash, error) {
|
2020-06-26 22:26:55 +00:00
|
|
|
tx := new(evmtypes.MsgEthereumTx)
|
2019-09-18 20:14:39 +00:00
|
|
|
|
|
|
|
// RLP decode raw transaction bytes
|
|
|
|
if err := rlp.DecodeBytes(data, tx); err != nil {
|
|
|
|
// Return nil is for when gasLimit overflows uint64
|
|
|
|
return common.Hash{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode transaction by default Tx encoder
|
2020-04-22 19:26:01 +00:00
|
|
|
txEncoder := authclient.GetTxEncoder(e.cliCtx.Codec)
|
2019-09-18 20:14:39 +00:00
|
|
|
txBytes, err := txEncoder(tx)
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Possibly log the contract creation address (if recipient address is nil) or tx data
|
|
|
|
// If error is encountered on the node, the broadcast will not return an error
|
2020-04-22 19:26:01 +00:00
|
|
|
res, err := e.cliCtx.BroadcastTx(txBytes)
|
2019-09-18 20:14:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
2019-09-24 18:41:59 +00:00
|
|
|
// Return transaction hash
|
|
|
|
return common.HexToHash(res.TxHash), nil
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 16:40:34 +00:00
|
|
|
// CallArgs represents the arguments for a call.
|
2018-08-09 10:36:47 +00:00
|
|
|
type CallArgs struct {
|
2019-10-22 16:40:34 +00:00
|
|
|
From *common.Address `json:"from"`
|
|
|
|
To *common.Address `json:"to"`
|
|
|
|
Gas *hexutil.Uint64 `json:"gas"`
|
|
|
|
GasPrice *hexutil.Big `json:"gasPrice"`
|
|
|
|
Value *hexutil.Big `json:"value"`
|
|
|
|
Data *hexutil.Bytes `json:"data"`
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call performs a raw contract call.
|
2020-07-03 15:40:00 +00:00
|
|
|
func (e *PublicEthAPI) Call(args CallArgs, blockNr BlockNumber, overrides *map[common.Address]account) (hexutil.Bytes, error) {
|
2020-04-22 19:26:01 +00:00
|
|
|
simRes, err := e.doCall(args, blockNr, big.NewInt(emint.DefaultRPCGasLimit))
|
2019-10-30 18:30:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
data, err := evmtypes.DecodeResultData(simRes.Result.Data)
|
2020-04-01 18:49:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
2019-10-30 18:30:24 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
return (hexutil.Bytes)(data.Ret), nil
|
2019-10-22 16:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// account indicates the overriding fields of account during the execution of
|
|
|
|
// a message call.
|
|
|
|
// Note, state and stateDiff can't be specified at the same time. If state is
|
|
|
|
// set, message execution will only use the data in the given state. Otherwise
|
|
|
|
// if statDiff is set, all diff will be applied first and then execute the call
|
|
|
|
// message.
|
|
|
|
type account struct {
|
|
|
|
Nonce *hexutil.Uint64 `json:"nonce"`
|
|
|
|
Code *hexutil.Bytes `json:"code"`
|
|
|
|
Balance **hexutil.Big `json:"balance"`
|
|
|
|
State *map[common.Hash]common.Hash `json:"state"`
|
|
|
|
StateDiff *map[common.Hash]common.Hash `json:"stateDiff"`
|
|
|
|
}
|
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
// DoCall performs a simulated call operation through the evmtypes. It returns the
|
2020-04-01 18:49:21 +00:00
|
|
|
// estimated gas used on the operation or an error if fails.
|
2020-04-22 19:26:01 +00:00
|
|
|
func (e *PublicEthAPI) doCall(
|
2020-07-03 15:40:00 +00:00
|
|
|
args CallArgs, blockNr BlockNumber, globalGasCap *big.Int,
|
2020-04-22 19:26:01 +00:00
|
|
|
) (*sdk.SimulationResponse, error) {
|
2019-10-22 16:40:34 +00:00
|
|
|
// Set height for historical queries
|
2019-11-04 16:59:16 +00:00
|
|
|
ctx := e.cliCtx
|
2020-04-22 19:26:01 +00:00
|
|
|
|
2019-11-04 16:59:16 +00:00
|
|
|
if blockNr.Int64() != 0 {
|
|
|
|
ctx = e.cliCtx.WithHeight(blockNr.Int64())
|
|
|
|
}
|
2019-10-22 16:40:34 +00:00
|
|
|
|
|
|
|
// Set sender address or use a default if none specified
|
|
|
|
var addr common.Address
|
2020-07-06 17:03:34 +00:00
|
|
|
|
2019-10-22 16:40:34 +00:00
|
|
|
if args.From == nil {
|
2020-07-06 17:03:34 +00:00
|
|
|
key, exist := checkKeyInKeyring(e.keys, *args.From)
|
|
|
|
if exist {
|
|
|
|
addr = common.BytesToAddress(key.PubKey().Address().Bytes())
|
2019-10-22 16:40:34 +00:00
|
|
|
}
|
|
|
|
// No error handled here intentionally to match geth behaviour
|
|
|
|
} else {
|
|
|
|
addr = *args.From
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set default gas & gas price if none were set
|
|
|
|
// Change this to uint64(math.MaxUint64 / 2) if gas cap can be configured
|
|
|
|
gas := uint64(emint.DefaultRPCGasLimit)
|
|
|
|
if args.Gas != nil {
|
|
|
|
gas = uint64(*args.Gas)
|
|
|
|
}
|
|
|
|
if globalGasCap != nil && globalGasCap.Uint64() < gas {
|
|
|
|
log.Println("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap)
|
|
|
|
gas = globalGasCap.Uint64()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set gas price using default or parameter if passed in
|
|
|
|
gasPrice := new(big.Int).SetUint64(emint.DefaultGasPrice)
|
|
|
|
if args.GasPrice != nil {
|
|
|
|
gasPrice = args.GasPrice.ToInt()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set value for transaction
|
|
|
|
value := new(big.Int)
|
|
|
|
if args.Value != nil {
|
|
|
|
value = args.Value.ToInt()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set Data if provided
|
|
|
|
var data []byte
|
|
|
|
if args.Data != nil {
|
|
|
|
data = []byte(*args.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set destination address for call
|
|
|
|
var toAddr sdk.AccAddress
|
|
|
|
if args.To != nil {
|
|
|
|
toAddr = sdk.AccAddress(args.To.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new call message
|
2020-06-26 22:26:55 +00:00
|
|
|
msg := evmtypes.NewMsgEthermint(0, &toAddr, sdk.NewIntFromBigInt(value), gas,
|
2019-10-22 16:40:34 +00:00
|
|
|
sdk.NewIntFromBigInt(gasPrice), data, sdk.AccAddress(addr.Bytes()))
|
|
|
|
|
|
|
|
// Generate tx to be used to simulate (signature isn't needed)
|
2019-11-01 15:26:53 +00:00
|
|
|
var stdSig authtypes.StdSignature
|
|
|
|
tx := authtypes.NewStdTx([]sdk.Msg{msg}, authtypes.StdFee{}, []authtypes.StdSignature{stdSig}, "")
|
2019-10-22 16:40:34 +00:00
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
txEncoder := authclient.GetTxEncoder(ctx.Codec)
|
2019-10-22 16:40:34 +00:00
|
|
|
txBytes, err := txEncoder(tx)
|
|
|
|
if err != nil {
|
2020-04-01 18:49:21 +00:00
|
|
|
return nil, err
|
2019-10-22 16:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transaction simulation through query
|
|
|
|
res, _, err := ctx.QueryWithData("app/simulate", txBytes)
|
|
|
|
if err != nil {
|
2020-04-01 18:49:21 +00:00
|
|
|
return nil, err
|
2019-10-22 16:40:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
var simResponse sdk.SimulationResponse
|
|
|
|
if err := jsonpb.Unmarshal(strings.NewReader(string(res)), &simResponse); err != nil {
|
2020-04-01 18:49:21 +00:00
|
|
|
return nil, err
|
2019-10-22 16:40:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
return &simResponse, nil
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
// EstimateGas returns an estimate of gas usage for the given smart contract call.
|
|
|
|
// It adds 1,000 gas to the returned value instead of using the gas adjustment
|
|
|
|
// param from the SDK.
|
2020-06-01 18:12:34 +00:00
|
|
|
func (e *PublicEthAPI) EstimateGas(args CallArgs) (hexutil.Uint64, error) {
|
2020-04-22 19:26:01 +00:00
|
|
|
simResponse, err := e.doCall(args, 0, big.NewInt(emint.DefaultRPCGasLimit))
|
2019-10-30 18:30:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
// TODO: change 1000 buffer for more accurate buffer (eg: SDK's gasAdjusted)
|
|
|
|
estimatedGas := simResponse.GasInfo.GasUsed
|
|
|
|
gas := estimatedGas + 1000
|
2020-06-01 18:12:34 +00:00
|
|
|
|
|
|
|
return hexutil.Uint64(gas), nil
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockByHash returns the block identified by hash.
|
2019-09-27 14:08:45 +00:00
|
|
|
func (e *PublicEthAPI) GetBlockByHash(hash common.Hash, fullTx bool) (map[string]interface{}, error) {
|
2020-04-13 19:18:50 +00:00
|
|
|
return e.backend.GetBlockByHash(hash, fullTx)
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockByNumber returns the block identified by number.
|
2019-09-24 21:38:58 +00:00
|
|
|
func (e *PublicEthAPI) GetBlockByNumber(blockNum BlockNumber, fullTx bool) (map[string]interface{}, error) {
|
2020-04-02 00:43:59 +00:00
|
|
|
return e.backend.GetBlockByNumber(blockNum, fullTx)
|
2019-09-24 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func formatBlock(
|
|
|
|
header tmtypes.Header, size int, gasLimit int64,
|
2020-04-13 19:18:50 +00:00
|
|
|
gasUsed *big.Int, transactions interface{}, bloom ethtypes.Bloom,
|
2019-09-24 18:39:17 +00:00
|
|
|
) map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"number": hexutil.Uint64(header.Height),
|
2019-09-27 14:08:45 +00:00
|
|
|
"hash": hexutil.Bytes(header.Hash()),
|
2019-09-24 18:39:17 +00:00
|
|
|
"parentHash": hexutil.Bytes(header.LastBlockID.Hash),
|
|
|
|
"nonce": nil, // PoW specific
|
|
|
|
"sha3Uncles": nil, // No uncles in Tendermint
|
2019-10-04 19:32:56 +00:00
|
|
|
"logsBloom": bloom,
|
2019-09-24 18:39:17 +00:00
|
|
|
"transactionsRoot": hexutil.Bytes(header.DataHash),
|
|
|
|
"stateRoot": hexutil.Bytes(header.AppHash),
|
2019-11-07 18:02:35 +00:00
|
|
|
"miner": common.Address{},
|
2019-09-24 18:39:17 +00:00
|
|
|
"difficulty": nil,
|
|
|
|
"totalDifficulty": nil,
|
2020-06-17 16:14:21 +00:00
|
|
|
"extraData": hexutil.Uint64(0),
|
2019-09-24 18:39:17 +00:00
|
|
|
"size": hexutil.Uint64(size),
|
|
|
|
"gasLimit": hexutil.Uint64(gasLimit), // Static gas limit
|
|
|
|
"gasUsed": (*hexutil.Big)(gasUsed),
|
|
|
|
"timestamp": hexutil.Uint64(header.Time.Unix()),
|
2020-04-13 19:18:50 +00:00
|
|
|
"transactions": transactions.([]common.Hash),
|
2020-06-18 20:35:06 +00:00
|
|
|
"uncles": []string{},
|
2019-09-24 18:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 19:18:50 +00:00
|
|
|
func convertTransactionsToRPC(cliCtx context.CLIContext, txs []tmtypes.Tx, blockHash common.Hash, height uint64) ([]common.Hash, *big.Int, error) {
|
|
|
|
transactions := make([]common.Hash, len(txs))
|
2019-09-24 18:39:17 +00:00
|
|
|
gasUsed := big.NewInt(0)
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2019-09-24 18:39:17 +00:00
|
|
|
for i, tx := range txs {
|
2019-09-25 01:58:29 +00:00
|
|
|
ethTx, err := bytesToEthTx(cliCtx, tx)
|
|
|
|
if err != nil {
|
2020-07-03 15:40:00 +00:00
|
|
|
// continue to next transaction in case it's not a MsgEthereumTx
|
|
|
|
continue
|
2019-09-24 18:39:17 +00:00
|
|
|
}
|
|
|
|
// TODO: Remove gas usage calculation if saving gasUsed per block
|
|
|
|
gasUsed.Add(gasUsed, ethTx.Fee())
|
2020-04-13 23:08:43 +00:00
|
|
|
tx, err := newRPCTransaction(*ethTx, common.BytesToHash(tx.Hash()), blockHash, &height, uint64(i))
|
2020-04-01 18:49:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-04-13 19:18:50 +00:00
|
|
|
transactions[i] = tx.Hash
|
2019-09-24 18:39:17 +00:00
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
|
|
|
return transactions, gasUsed, nil
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transaction represents a transaction returned to RPC clients.
|
|
|
|
type Transaction struct {
|
2019-09-24 18:39:17 +00:00
|
|
|
BlockHash *common.Hash `json:"blockHash"`
|
2018-08-09 10:36:47 +00:00
|
|
|
BlockNumber *hexutil.Big `json:"blockNumber"`
|
|
|
|
From common.Address `json:"from"`
|
|
|
|
Gas hexutil.Uint64 `json:"gas"`
|
|
|
|
GasPrice *hexutil.Big `json:"gasPrice"`
|
|
|
|
Hash common.Hash `json:"hash"`
|
|
|
|
Input hexutil.Bytes `json:"input"`
|
|
|
|
Nonce hexutil.Uint64 `json:"nonce"`
|
|
|
|
To *common.Address `json:"to"`
|
2019-09-24 18:39:17 +00:00
|
|
|
TransactionIndex *hexutil.Uint64 `json:"transactionIndex"`
|
2018-08-09 10:36:47 +00:00
|
|
|
Value *hexutil.Big `json:"value"`
|
|
|
|
V *hexutil.Big `json:"v"`
|
|
|
|
R *hexutil.Big `json:"r"`
|
|
|
|
S *hexutil.Big `json:"s"`
|
|
|
|
}
|
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
func bytesToEthTx(cliCtx context.CLIContext, bz []byte) (*evmtypes.MsgEthereumTx, error) {
|
2019-09-25 01:58:29 +00:00
|
|
|
var stdTx sdk.Tx
|
2020-04-22 19:26:01 +00:00
|
|
|
err := cliCtx.Codec.UnmarshalBinaryBare(bz, &stdTx)
|
2020-04-01 18:49:21 +00:00
|
|
|
if err != nil {
|
2020-04-22 19:26:01 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
ethTx, ok := stdTx.(evmtypes.MsgEthereumTx)
|
2020-04-01 18:49:21 +00:00
|
|
|
if !ok {
|
2020-07-03 15:40:00 +00:00
|
|
|
return nil, fmt.Errorf("invalid transaction type %T, expected MsgEthereumTx", stdTx)
|
2019-09-25 01:58:29 +00:00
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
return ðTx, nil
|
2019-09-25 01:58:29 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 18:39:17 +00:00
|
|
|
// newRPCTransaction returns a transaction that will serialize to the RPC
|
|
|
|
// representation, with the given location metadata set (if available).
|
2020-06-26 22:26:55 +00:00
|
|
|
func newRPCTransaction(tx evmtypes.MsgEthereumTx, txHash, blockHash common.Hash, blockNumber *uint64, index uint64) (*Transaction, error) {
|
2019-09-24 18:39:17 +00:00
|
|
|
// Verify signature and retrieve sender address
|
2020-04-01 18:49:21 +00:00
|
|
|
from, err := tx.VerifySig(tx.ChainID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-24 18:39:17 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
result := Transaction{
|
2019-09-24 18:39:17 +00:00
|
|
|
From: from,
|
|
|
|
Gas: hexutil.Uint64(tx.Data.GasLimit),
|
|
|
|
GasPrice: (*hexutil.Big)(tx.Data.Price),
|
2020-04-13 23:08:43 +00:00
|
|
|
Hash: txHash,
|
2019-09-24 18:39:17 +00:00
|
|
|
Input: hexutil.Bytes(tx.Data.Payload),
|
|
|
|
Nonce: hexutil.Uint64(tx.Data.AccountNonce),
|
|
|
|
To: tx.To(),
|
|
|
|
Value: (*hexutil.Big)(tx.Data.Amount),
|
|
|
|
V: (*hexutil.Big)(tx.Data.V),
|
|
|
|
R: (*hexutil.Big)(tx.Data.R),
|
|
|
|
S: (*hexutil.Big)(tx.Data.S),
|
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2019-09-24 18:39:17 +00:00
|
|
|
if blockHash != (common.Hash{}) {
|
|
|
|
result.BlockHash = &blockHash
|
2019-10-18 23:23:09 +00:00
|
|
|
result.BlockNumber = (*hexutil.Big)(new(big.Int).SetUint64(*blockNumber))
|
2019-09-24 18:39:17 +00:00
|
|
|
result.TransactionIndex = (*hexutil.Uint64)(&index)
|
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
|
|
|
return &result, nil
|
2019-09-24 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 10:36:47 +00:00
|
|
|
// GetTransactionByHash returns the transaction identified by hash.
|
2019-09-25 01:58:29 +00:00
|
|
|
func (e *PublicEthAPI) GetTransactionByHash(hash common.Hash) (*Transaction, error) {
|
|
|
|
tx, err := e.cliCtx.Client.Tx(hash.Bytes(), false)
|
|
|
|
if err != nil {
|
|
|
|
// Return nil for transaction when not found
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can either cache or just leave this out if not necessary
|
|
|
|
block, err := e.cliCtx.Client.Block(&tx.Height)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
blockHash := common.BytesToHash(block.Block.Header.Hash())
|
2019-09-25 01:58:29 +00:00
|
|
|
|
|
|
|
ethTx, err := bytesToEthTx(e.cliCtx, tx.Tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:23:09 +00:00
|
|
|
height := uint64(tx.Height)
|
2020-04-13 23:08:43 +00:00
|
|
|
return newRPCTransaction(*ethTx, common.BytesToHash(tx.Tx.Hash()), blockHash, &height, uint64(tx.Index))
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionByBlockHashAndIndex returns the transaction identified by hash and index.
|
2019-09-29 20:46:10 +00:00
|
|
|
func (e *PublicEthAPI) GetTransactionByBlockHashAndIndex(hash common.Hash, idx hexutil.Uint) (*Transaction, error) {
|
2020-06-26 22:26:55 +00:00
|
|
|
res, _, err := e.cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s", evmtypes.ModuleName, evmtypes.QueryHashToHeight, hash.Hex()))
|
2019-09-29 20:46:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
var out evmtypes.QueryResBlockNumber
|
2019-09-29 20:46:10 +00:00
|
|
|
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
|
|
|
|
return e.getTransactionByBlockNumberAndIndex(out.Number, idx)
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionByBlockNumberAndIndex returns the transaction identified by number and index.
|
2019-09-25 13:26:42 +00:00
|
|
|
func (e *PublicEthAPI) GetTransactionByBlockNumberAndIndex(blockNum BlockNumber, idx hexutil.Uint) (*Transaction, error) {
|
|
|
|
value := blockNum.Int64()
|
2019-09-29 20:46:10 +00:00
|
|
|
return e.getTransactionByBlockNumberAndIndex(value, idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *PublicEthAPI) getTransactionByBlockNumberAndIndex(number int64, idx hexutil.Uint) (*Transaction, error) {
|
|
|
|
block, err := e.cliCtx.Client.Block(&number)
|
2019-09-25 13:26:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
header := block.Block.Header
|
2019-09-25 13:26:42 +00:00
|
|
|
|
|
|
|
txs := block.Block.Txs
|
|
|
|
if uint64(idx) >= uint64(len(txs)) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
ethTx, err := bytesToEthTx(e.cliCtx, txs[idx])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:23:09 +00:00
|
|
|
height := uint64(header.Height)
|
2020-04-13 23:08:43 +00:00
|
|
|
return newRPCTransaction(*ethTx, common.BytesToHash(txs[idx].Hash()), common.BytesToHash(header.Hash()), &height, uint64(idx))
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionReceipt returns the transaction receipt identified by hash.
|
2019-09-25 18:38:33 +00:00
|
|
|
func (e *PublicEthAPI) GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error) {
|
|
|
|
tx, err := e.cliCtx.Client.Tx(hash.Bytes(), false)
|
|
|
|
if err != nil {
|
|
|
|
// Return nil for transaction when not found
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query block for consensus hash
|
|
|
|
block, err := e.cliCtx.Client.Block(&tx.Height)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
blockHash := common.BytesToHash(block.Block.Header.Hash())
|
2019-09-25 18:38:33 +00:00
|
|
|
|
|
|
|
// Convert tx bytes to eth transaction
|
|
|
|
ethTx, err := bytesToEthTx(e.cliCtx, tx.Tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
from, err := ethTx.VerifySig(ethTx.ChainID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-25 18:38:33 +00:00
|
|
|
|
|
|
|
// Set status codes based on tx result
|
|
|
|
var status hexutil.Uint
|
|
|
|
if tx.TxResult.IsOK() {
|
|
|
|
status = hexutil.Uint(1)
|
|
|
|
} else {
|
|
|
|
status = hexutil.Uint(0)
|
|
|
|
}
|
|
|
|
|
2019-10-22 16:04:51 +00:00
|
|
|
txData := tx.TxResult.GetData()
|
2020-05-02 17:39:26 +00:00
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
data, err := evmtypes.DecodeResultData(txData)
|
2020-04-01 18:49:21 +00:00
|
|
|
if err != nil {
|
2020-05-02 17:39:26 +00:00
|
|
|
status = 0 // transaction failed
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
2019-10-04 19:32:56 +00:00
|
|
|
|
2020-05-14 20:55:33 +00:00
|
|
|
if data.Logs == nil {
|
|
|
|
data.Logs = []*ethtypes.Log{}
|
|
|
|
}
|
|
|
|
|
2020-05-02 17:39:26 +00:00
|
|
|
receipt := map[string]interface{}{
|
2020-05-03 01:56:18 +00:00
|
|
|
// Consensus fields: These fields are defined by the Yellow Paper
|
|
|
|
"status": status,
|
2019-09-25 18:38:33 +00:00
|
|
|
"cumulativeGasUsed": nil, // ignore until needed
|
2020-04-01 18:49:21 +00:00
|
|
|
"logsBloom": data.Bloom,
|
2020-05-03 01:56:18 +00:00
|
|
|
"logs": data.Logs,
|
|
|
|
|
|
|
|
// Implementation fields: These fields are added by geth when processing a transaction.
|
|
|
|
// They are stored in the chain database.
|
|
|
|
"transactionHash": hash,
|
|
|
|
"contractAddress": data.ContractAddress,
|
|
|
|
"gasUsed": hexutil.Uint64(tx.TxResult.GasUsed),
|
|
|
|
|
|
|
|
// Inclusion information: These fields provide information about the inclusion of the
|
|
|
|
// transaction corresponding to this receipt.
|
|
|
|
"blockHash": blockHash,
|
|
|
|
"blockNumber": hexutil.Uint64(tx.Height),
|
|
|
|
"transactionIndex": hexutil.Uint64(tx.Index),
|
|
|
|
|
|
|
|
// sender and receiver (contract or EOA) addreses
|
|
|
|
"from": from,
|
|
|
|
"to": ethTx.To(),
|
2019-09-25 18:38:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 17:39:26 +00:00
|
|
|
return receipt, nil
|
2018-08-09 10:36:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 23:23:09 +00:00
|
|
|
// PendingTransactions returns the transactions that are in the transaction pool
|
|
|
|
// and have a from address that is one of the accounts this node manages.
|
|
|
|
func (e *PublicEthAPI) PendingTransactions() ([]*Transaction, error) {
|
2020-04-02 00:43:59 +00:00
|
|
|
return e.backend.PendingTransactions()
|
2019-10-18 23:23:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 10:36:47 +00:00
|
|
|
// GetUncleByBlockHashAndIndex returns the uncle identified by hash and index. Always returns nil.
|
|
|
|
func (e *PublicEthAPI) GetUncleByBlockHashAndIndex(hash common.Hash, idx hexutil.Uint) map[string]interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUncleByBlockNumberAndIndex returns the uncle identified by number and index. Always returns nil.
|
|
|
|
func (e *PublicEthAPI) GetUncleByBlockNumberAndIndex(number hexutil.Uint, idx hexutil.Uint) map[string]interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-24 18:39:17 +00:00
|
|
|
|
2020-05-12 19:12:52 +00:00
|
|
|
// Copied the Account and StorageResult types since they are registered under an
|
|
|
|
// internal pkg on geth.
|
|
|
|
|
2019-10-16 00:46:50 +00:00
|
|
|
// AccountResult struct for account proof
|
|
|
|
type AccountResult struct {
|
|
|
|
Address common.Address `json:"address"`
|
|
|
|
AccountProof []string `json:"accountProof"`
|
|
|
|
Balance *hexutil.Big `json:"balance"`
|
|
|
|
CodeHash common.Hash `json:"codeHash"`
|
|
|
|
Nonce hexutil.Uint64 `json:"nonce"`
|
|
|
|
StorageHash common.Hash `json:"storageHash"`
|
|
|
|
StorageProof []StorageResult `json:"storageProof"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageResult defines the format for storage proof return
|
|
|
|
type StorageResult struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
Value *hexutil.Big `json:"value"`
|
|
|
|
Proof []string `json:"proof"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProof returns an account object with proof and any storage proofs
|
|
|
|
func (e *PublicEthAPI) GetProof(address common.Address, storageKeys []string, block BlockNumber) (*AccountResult, error) {
|
2020-05-12 19:12:52 +00:00
|
|
|
e.cliCtx = e.cliCtx.WithHeight(int64(block))
|
2020-06-26 22:26:55 +00:00
|
|
|
path := fmt.Sprintf("custom/%s/%s/%s", evmtypes.ModuleName, evmtypes.QueryAccount, address.Hex())
|
2020-05-12 19:12:52 +00:00
|
|
|
|
|
|
|
// query eth account at block height
|
|
|
|
resBz, _, err := e.cliCtx.Query(path)
|
2019-10-16 00:46:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
var account evmtypes.QueryResAccount
|
2020-05-12 19:12:52 +00:00
|
|
|
e.cliCtx.Codec.MustUnmarshalJSON(resBz, &account)
|
2019-10-16 00:46:50 +00:00
|
|
|
|
|
|
|
storageProofs := make([]StorageResult, len(storageKeys))
|
2020-05-12 19:12:52 +00:00
|
|
|
opts := client.ABCIQueryOptions{Height: int64(block), Prove: true}
|
2019-10-16 00:46:50 +00:00
|
|
|
for i, k := range storageKeys {
|
|
|
|
// Get value for key
|
2020-06-26 22:26:55 +00:00
|
|
|
vPath := fmt.Sprintf("custom/%s/%s/%s/%s", evmtypes.ModuleName, evmtypes.QueryStorage, address, k)
|
2019-10-16 00:46:50 +00:00
|
|
|
vRes, err := e.cliCtx.Client.ABCIQueryWithOptions(vPath, nil, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-12 19:12:52 +00:00
|
|
|
|
2020-06-26 22:26:55 +00:00
|
|
|
var value evmtypes.QueryResStorage
|
2019-10-16 00:46:50 +00:00
|
|
|
e.cliCtx.Codec.MustUnmarshalJSON(vRes.Response.GetValue(), &value)
|
|
|
|
|
2020-05-12 19:12:52 +00:00
|
|
|
// check for proof
|
|
|
|
proof := vRes.Response.GetProof()
|
|
|
|
proofStr := new(merkle.Proof).String()
|
|
|
|
if proof != nil {
|
|
|
|
proofStr = proof.String()
|
|
|
|
}
|
|
|
|
|
2019-10-16 00:46:50 +00:00
|
|
|
storageProofs[i] = StorageResult{
|
|
|
|
Key: k,
|
|
|
|
Value: (*hexutil.Big)(common.BytesToHash(value.Value).Big()),
|
2020-05-12 19:12:52 +00:00
|
|
|
Proof: []string{proofStr},
|
2019-10-16 00:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 19:12:52 +00:00
|
|
|
req := abci.RequestQuery{
|
|
|
|
Path: fmt.Sprintf("store/%s/key", auth.StoreKey),
|
|
|
|
Data: auth.AddressStoreKey(sdk.AccAddress(address.Bytes())),
|
|
|
|
Height: int64(block),
|
|
|
|
Prove: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := e.cliCtx.QueryABCI(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for proof
|
|
|
|
accountProof := res.GetProof()
|
|
|
|
accProofStr := new(merkle.Proof).String()
|
|
|
|
if accountProof != nil {
|
|
|
|
accProofStr = accountProof.String()
|
|
|
|
}
|
|
|
|
|
2019-10-16 00:46:50 +00:00
|
|
|
return &AccountResult{
|
|
|
|
Address: address,
|
2020-05-12 19:12:52 +00:00
|
|
|
AccountProof: []string{accProofStr},
|
2019-10-16 00:46:50 +00:00
|
|
|
Balance: (*hexutil.Big)(utils.MustUnmarshalBigInt(account.Balance)),
|
|
|
|
CodeHash: common.BytesToHash(account.CodeHash),
|
|
|
|
Nonce: hexutil.Uint64(account.Nonce),
|
|
|
|
StorageHash: common.Hash{}, // Ethermint doesn't have a storage hash
|
|
|
|
StorageProof: storageProofs,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-11-15 17:02:13 +00:00
|
|
|
// generateFromArgs populates tx message with args (used in RPC API)
|
2020-06-26 22:26:55 +00:00
|
|
|
func (e *PublicEthAPI) generateFromArgs(args params.SendTxArgs) (*evmtypes.MsgEthereumTx, error) {
|
2020-04-01 18:49:21 +00:00
|
|
|
var (
|
|
|
|
nonce uint64
|
|
|
|
gasLimit uint64
|
|
|
|
err error
|
|
|
|
)
|
2019-10-30 18:30:24 +00:00
|
|
|
|
|
|
|
amount := (*big.Int)(args.Value)
|
|
|
|
gasPrice := (*big.Int)(args.GasPrice)
|
|
|
|
|
|
|
|
if args.GasPrice == nil {
|
|
|
|
|
|
|
|
// Set default gas price
|
|
|
|
// TODO: Change to min gas price from context once available through server/daemon
|
2020-03-16 22:53:24 +00:00
|
|
|
gasPrice = big.NewInt(emint.DefaultGasPrice)
|
2019-10-30 18:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if args.Nonce == nil {
|
|
|
|
// Get nonce (sequence) from account
|
|
|
|
from := sdk.AccAddress(args.From.Bytes())
|
2020-04-22 19:26:01 +00:00
|
|
|
authclient.Codec = codec.NewAppCodec(e.cliCtx.Codec)
|
|
|
|
accRet := authtypes.NewAccountRetriever(authclient.Codec, e.cliCtx)
|
|
|
|
|
2020-06-26 18:15:54 +00:00
|
|
|
err = accRet.EnsureExists(from)
|
|
|
|
if err != nil {
|
|
|
|
// account doesn't exist
|
|
|
|
return nil, fmt.Errorf("nonexistent account %s: %s", args.From.Hex(), err)
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
_, nonce, err = accRet.GetAccountNumberSequence(from)
|
2019-10-30 18:30:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nonce = (uint64)(*args.Nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.Data != nil && args.Input != nil && !bytes.Equal(*args.Data, *args.Input) {
|
2020-04-01 18:49:21 +00:00
|
|
|
return nil, errors.New(`both "data" and "input" are set and not equal. Please use "input" to pass transaction call data`)
|
2019-10-30 18:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sets input to either Input or Data, if both are set and not equal error above returns
|
|
|
|
var input []byte
|
|
|
|
if args.Input != nil {
|
|
|
|
input = *args.Input
|
|
|
|
} else if args.Data != nil {
|
|
|
|
input = *args.Data
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
if args.To == nil && len(input) == 0 {
|
2019-10-30 18:30:24 +00:00
|
|
|
// Contract creation
|
2020-04-22 19:26:01 +00:00
|
|
|
return nil, fmt.Errorf("contract creation without any data provided")
|
2019-10-30 18:30:24 +00:00
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2019-10-30 18:30:24 +00:00
|
|
|
if args.Gas == nil {
|
|
|
|
callArgs := CallArgs{
|
|
|
|
From: &args.From,
|
|
|
|
To: args.To,
|
|
|
|
Gas: args.Gas,
|
|
|
|
GasPrice: args.GasPrice,
|
|
|
|
Value: args.Value,
|
|
|
|
Data: args.Data,
|
|
|
|
}
|
2020-06-01 18:12:34 +00:00
|
|
|
gl, err := e.EstimateGas(callArgs)
|
2020-04-01 18:49:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-01 18:12:34 +00:00
|
|
|
gasLimit = uint64(gl)
|
2019-10-30 18:30:24 +00:00
|
|
|
} else {
|
|
|
|
gasLimit = (uint64)(*args.Gas)
|
|
|
|
}
|
2020-06-26 22:26:55 +00:00
|
|
|
msg := evmtypes.NewMsgEthereumTx(nonce, args.To, amount, gasLimit, gasPrice, input)
|
2019-10-30 18:30:24 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
return &msg, nil
|
2019-10-30 18:30:24 +00:00
|
|
|
}
|