2018-08-09 10:36:47 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2019-09-18 18:45:21 +00:00
|
|
|
"bytes"
|
2019-07-25 20:38:55 +00:00
|
|
|
"fmt"
|
2019-08-28 17:43:34 +00:00
|
|
|
"math/big"
|
2019-10-04 19:32:56 +00:00
|
|
|
"strconv"
|
2019-08-28 17:43:34 +00:00
|
|
|
|
2019-09-18 18:45:21 +00:00
|
|
|
emintcrypto "github.com/cosmos/ethermint/crypto"
|
2019-09-18 13:58:48 +00:00
|
|
|
emintkeys "github.com/cosmos/ethermint/keys"
|
2019-09-20 13:30:20 +00:00
|
|
|
"github.com/cosmos/ethermint/rpc/args"
|
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"
|
2019-09-27 14:08:45 +00:00
|
|
|
"github.com/cosmos/ethermint/x/evm"
|
2019-07-25 20:38:55 +00:00
|
|
|
"github.com/cosmos/ethermint/x/evm/types"
|
2019-09-18 18:45:21 +00:00
|
|
|
|
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"
|
2019-09-24 18:39:17 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
authutils "github.com/cosmos/cosmos-sdk/x/auth/client/utils"
|
|
|
|
|
2019-09-20 13:30:20 +00:00
|
|
|
"github.com/spf13/viper"
|
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 {
|
2019-09-20 13:30:20 +00:00
|
|
|
cliCtx context.CLIContext
|
|
|
|
key emintcrypto.PrivKeySecp256k1
|
|
|
|
nonceLock *AddrLocker
|
2019-09-24 18:39:17 +00:00
|
|
|
gasLimit *int64
|
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.
|
2019-09-20 13:30:20 +00:00
|
|
|
func NewPublicEthAPI(cliCtx context.CLIContext, nonceLock *AddrLocker,
|
|
|
|
key emintcrypto.PrivKeySecp256k1) *PublicEthAPI {
|
|
|
|
|
2019-07-15 14:13:59 +00:00
|
|
|
return &PublicEthAPI{
|
2019-09-20 13:30:20 +00:00
|
|
|
cliCtx: cliCtx,
|
|
|
|
key: key,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
// Coinbase returns this node's coinbase address. Not used in Ethermint.
|
|
|
|
func (e *PublicEthAPI) Coinbase() (addr common.Address) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
addresses := make([]common.Address, 0) // return [] instead of nil if empty
|
|
|
|
keybase, err := emintkeys.NewKeyBaseFromHomeFlag()
|
|
|
|
if err != nil {
|
|
|
|
return addresses, err
|
|
|
|
}
|
|
|
|
|
|
|
|
infos, err := keybase.List()
|
|
|
|
if err != nil {
|
|
|
|
return addresses, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2019-07-25 20:38:55 +00:00
|
|
|
res, _, err := e.cliCtx.QueryWithData(fmt.Sprintf("custom/%s/blockNumber", types.ModuleName), nil)
|
|
|
|
if err != nil {
|
2019-09-24 14:49:40 +00:00
|
|
|
return hexutil.Uint64(0), err
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 15:36:23 +00:00
|
|
|
var out types.QueryResBlockNumber
|
|
|
|
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
|
|
|
|
return hexutil.Uint64(out.Number), nil
|
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())
|
2019-09-26 15:36:23 +00:00
|
|
|
res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/balance/%s", types.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
|
|
|
}
|
|
|
|
|
2019-09-26 15:36:23 +00:00
|
|
|
var out types.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())
|
2019-09-26 15:36:23 +00:00
|
|
|
res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/storage/%s/%s", types.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
|
|
|
}
|
|
|
|
|
2019-09-26 15:36:23 +00:00
|
|
|
var out types.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())
|
2019-09-26 15:36:23 +00:00
|
|
|
res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/nonce/%s", types.ModuleName, address.Hex()), nil)
|
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
|
|
|
}
|
|
|
|
|
2019-09-26 15:36:23 +00:00
|
|
|
var out types.QueryResNonce
|
|
|
|
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
|
|
|
|
return (*hexutil.Uint64)(&out.Nonce), 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 {
|
|
|
|
res, _, err := e.cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s", types.ModuleName, evm.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
|
|
|
}
|
|
|
|
|
2019-09-29 20:43:26 +00:00
|
|
|
var out types.QueryResBlockNumber
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-09-29 20:43:26 +00:00
|
|
|
n := hexutil.Uint(block.Block.NumTxs)
|
|
|
|
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())
|
2019-09-26 15:36:23 +00:00
|
|
|
res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/code/%s", types.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
|
|
|
}
|
|
|
|
|
2019-09-26 15:36:23 +00:00
|
|
|
var out types.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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if e.key == nil || !bytes.Equal(e.key.PubKey().Address().Bytes(), address.Bytes()) {
|
|
|
|
return nil, keystore.ErrLocked
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign the requested hash with the wallet
|
|
|
|
signature, err := e.key.Sign(data)
|
|
|
|
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-09-20 13:30:20 +00:00
|
|
|
func (e *PublicEthAPI) SendTransaction(args args.SendTxArgs) (common.Hash, error) {
|
|
|
|
// TODO: Change this functionality to find an unlocked account by address
|
|
|
|
if e.key == nil || !bytes.Equal(e.key.PubKey().Address().Bytes(), args.From.Bytes()) {
|
|
|
|
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
|
|
|
|
tx, err := types.GenerateFromArgs(args, e.cliCtx)
|
|
|
|
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 {
|
|
|
|
return common.Hash{}, fmt.Errorf(
|
|
|
|
fmt.Sprintf("Invalid chainID: %s, must be integer format", chainID))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign transaction
|
|
|
|
tx.Sign(intChainID, e.key.ToECDSA())
|
|
|
|
|
|
|
|
// Encode transaction by default Tx encoder
|
|
|
|
txEncoder := authutils.GetTxEncoder(e.cliCtx.Codec)
|
|
|
|
txBytes, err := txEncoder(tx)
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Broadcast transaction
|
|
|
|
res, err := e.cliCtx.BroadcastTx(txBytes)
|
|
|
|
// If error is encountered on the node, the broadcast will not return an error
|
|
|
|
// TODO: Remove res log
|
|
|
|
fmt.Println(res.RawLog)
|
|
|
|
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) {
|
|
|
|
tx := new(types.EthereumTxMsg)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
txEncoder := authutils.GetTxEncoder(e.cliCtx.Codec)
|
|
|
|
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
|
|
|
|
res, err := e.cliCtx.BroadcastTx(txBytes)
|
|
|
|
// If error is encountered on the node, the broadcast will not return an error
|
2019-09-20 13:30:20 +00:00
|
|
|
// TODO: Remove res log
|
2019-09-18 20:14:39 +00:00
|
|
|
fmt.Println(res.RawLog)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// CallArgs represents arguments to a smart contract call as provided by RPC clients.
|
|
|
|
type CallArgs struct {
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call performs a raw contract call.
|
2019-09-24 21:38:58 +00:00
|
|
|
func (e *PublicEthAPI) Call(args CallArgs, blockNum BlockNumber) hexutil.Bytes {
|
2018-08-09 10:36:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EstimateGas estimates gas usage for the given smart contract call.
|
2019-09-24 21:38:58 +00:00
|
|
|
func (e *PublicEthAPI) EstimateGas(args CallArgs, blockNum BlockNumber) hexutil.Uint64 {
|
2018-08-09 10:36:47 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
res, _, err := e.cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s", types.ModuleName, evm.QueryHashToHeight, hash.Hex()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var out types.QueryResBlockNumber
|
|
|
|
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
|
|
|
|
return e.getEthBlockByNumber(out.Number, 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) {
|
2019-09-24 18:39:17 +00:00
|
|
|
value := blockNum.Int64()
|
2019-09-27 14:08:45 +00:00
|
|
|
return e.getEthBlockByNumber(value, fullTx)
|
|
|
|
}
|
2019-09-24 21:38:58 +00:00
|
|
|
|
2019-09-27 14:08:45 +00:00
|
|
|
func (e *PublicEthAPI) getEthBlockByNumber(value int64, fullTx bool) (map[string]interface{}, error) {
|
2019-09-24 21:38:58 +00:00
|
|
|
// Remove this check when 0 query is fixed ref: (https://github.com/tendermint/tendermint/issues/4014)
|
|
|
|
var blkNumPtr *int64
|
|
|
|
if value != 0 {
|
|
|
|
blkNumPtr = &value
|
|
|
|
}
|
|
|
|
|
|
|
|
block, err := e.cliCtx.Client.Block(blkNumPtr)
|
2019-09-24 18:39:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
header := block.BlockMeta.Header
|
|
|
|
|
|
|
|
gasLimit, err := e.getGasLimit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var gasUsed *big.Int
|
|
|
|
var transactions []interface{}
|
|
|
|
|
|
|
|
if fullTx {
|
|
|
|
// Populate full transaction data
|
|
|
|
transactions, gasUsed = convertTransactionsToRPC(e.cliCtx, block.Block.Txs,
|
2019-09-27 14:08:45 +00:00
|
|
|
common.BytesToHash(header.Hash()), uint64(header.Height))
|
2019-09-24 18:39:17 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Gas used not saved and cannot be calculated by hashes
|
|
|
|
// Return slice of transaction hashes
|
|
|
|
transactions = make([]interface{}, len(block.Block.Txs))
|
|
|
|
for i, tx := range block.Block.Txs {
|
|
|
|
transactions[i] = common.BytesToHash(tx.Hash())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 19:32:56 +00:00
|
|
|
res, _, err := e.cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s", types.ModuleName, evm.QueryLogsBloom, strconv.FormatInt(block.Block.Height, 10)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var out types.QueryBloomFilter
|
|
|
|
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
|
|
|
|
|
|
|
|
return formatBlock(header, block.Block.Size(), gasLimit, gasUsed, transactions, out.Bloom), nil
|
2019-09-24 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func formatBlock(
|
|
|
|
header tmtypes.Header, size int, gasLimit int64,
|
2019-10-04 19:32:56 +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),
|
|
|
|
"miner": hexutil.Bytes(header.ValidatorsHash),
|
|
|
|
"difficulty": nil,
|
|
|
|
"totalDifficulty": nil,
|
|
|
|
"extraData": nil,
|
|
|
|
"size": hexutil.Uint64(size),
|
|
|
|
"gasLimit": hexutil.Uint64(gasLimit), // Static gas limit
|
|
|
|
"gasUsed": (*hexutil.Big)(gasUsed),
|
|
|
|
"timestamp": hexutil.Uint64(header.Time.Unix()),
|
|
|
|
"transactions": transactions,
|
|
|
|
"uncles": nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertTransactionsToRPC(cliCtx context.CLIContext, txs []tmtypes.Tx, blockHash common.Hash, height uint64) ([]interface{}, *big.Int) {
|
|
|
|
transactions := make([]interface{}, len(txs))
|
|
|
|
gasUsed := big.NewInt(0)
|
|
|
|
for i, tx := range txs {
|
2019-09-25 01:58:29 +00:00
|
|
|
ethTx, err := bytesToEthTx(cliCtx, tx)
|
|
|
|
if err != nil {
|
2019-09-24 18:39:17 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// TODO: Remove gas usage calculation if saving gasUsed per block
|
|
|
|
gasUsed.Add(gasUsed, ethTx.Fee())
|
|
|
|
transactions[i] = newRPCTransaction(ethTx, blockHash, height, uint64(i))
|
|
|
|
}
|
|
|
|
return transactions, gasUsed
|
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"`
|
|
|
|
}
|
|
|
|
|
2019-09-25 01:58:29 +00:00
|
|
|
func bytesToEthTx(cliCtx context.CLIContext, bz []byte) (*types.EthereumTxMsg, error) {
|
|
|
|
var stdTx sdk.Tx
|
|
|
|
err := cliCtx.Codec.UnmarshalBinaryLengthPrefixed(bz, &stdTx)
|
|
|
|
ethTx, ok := stdTx.(*types.EthereumTxMsg)
|
|
|
|
if !ok || err != nil {
|
|
|
|
return nil, fmt.Errorf("Invalid transaction type, must be an amino encoded Ethereum transaction")
|
|
|
|
}
|
|
|
|
return ethTx, nil
|
|
|
|
}
|
|
|
|
|
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).
|
|
|
|
func newRPCTransaction(tx *types.EthereumTxMsg, blockHash common.Hash, blockNumber uint64, index uint64) *Transaction {
|
|
|
|
// Verify signature and retrieve sender address
|
|
|
|
from, _ := tx.VerifySig(tx.ChainID())
|
|
|
|
|
|
|
|
result := &Transaction{
|
|
|
|
From: from,
|
|
|
|
Gas: hexutil.Uint64(tx.Data.GasLimit),
|
|
|
|
GasPrice: (*hexutil.Big)(tx.Data.Price),
|
|
|
|
Hash: tx.Hash(),
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
if blockHash != (common.Hash{}) {
|
|
|
|
result.BlockHash = &blockHash
|
|
|
|
result.BlockNumber = (*hexutil.Big)(new(big.Int).SetUint64(blockNumber))
|
|
|
|
result.TransactionIndex = (*hexutil.Uint64)(&index)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2019-09-27 14:08:45 +00:00
|
|
|
blockHash := common.BytesToHash(block.BlockMeta.Header.Hash())
|
2019-09-25 01:58:29 +00:00
|
|
|
|
|
|
|
ethTx, err := bytesToEthTx(e.cliCtx, tx.Tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newRPCTransaction(ethTx, blockHash, uint64(tx.Height), uint64(tx.Index)), nil
|
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) {
|
|
|
|
res, _, err := e.cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s", types.ModuleName, evm.QueryHashToHeight, hash.Hex()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var out types.QueryResBlockNumber
|
|
|
|
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
|
|
|
|
}
|
|
|
|
header := block.BlockMeta.Header
|
|
|
|
|
|
|
|
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-09-27 14:08:45 +00:00
|
|
|
transaction := newRPCTransaction(ethTx, common.BytesToHash(header.Hash()), uint64(header.Height), uint64(idx))
|
2019-09-25 13:26:42 +00:00
|
|
|
return transaction, nil
|
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
|
|
|
|
}
|
2019-09-27 14:08:45 +00:00
|
|
|
blockHash := common.BytesToHash(block.BlockMeta.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
|
|
|
|
}
|
|
|
|
|
|
|
|
from, _ := ethTx.VerifySig(ethTx.ChainID())
|
|
|
|
|
|
|
|
// 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-04 19:32:56 +00:00
|
|
|
res, _, err := e.cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s", types.ModuleName, evm.QueryTxLogs, hash.Hex()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var logs types.QueryTxLogs
|
|
|
|
e.cliCtx.Codec.MustUnmarshalJSON(res, &logs)
|
|
|
|
|
|
|
|
// TODO: change hard coded indexing of bytes
|
|
|
|
bloomFilter := ethtypes.BytesToBloom(tx.TxResult.GetData()[20:])
|
|
|
|
|
2019-09-25 18:38:33 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"blockHash": blockHash,
|
|
|
|
"blockNumber": hexutil.Uint64(tx.Height),
|
|
|
|
"transactionHash": hash,
|
|
|
|
"transactionIndex": hexutil.Uint64(tx.Index),
|
|
|
|
"from": from,
|
|
|
|
"to": ethTx.To(),
|
|
|
|
"gasUsed": hexutil.Uint64(tx.TxResult.GasUsed),
|
|
|
|
"cumulativeGasUsed": nil, // ignore until needed
|
|
|
|
"contractAddress": nil,
|
2019-10-04 19:32:56 +00:00
|
|
|
"logs": logs.Logs,
|
|
|
|
"logsBloom": bloomFilter,
|
2019-09-25 18:38:33 +00:00
|
|
|
"status": status,
|
|
|
|
}
|
|
|
|
|
2019-10-04 19:32:56 +00:00
|
|
|
contractAddress := common.BytesToAddress(tx.TxResult.GetData()[:20])
|
|
|
|
if contractAddress != (common.Address{}) {
|
|
|
|
// TODO: change hard coded indexing of first 20 bytes
|
|
|
|
fields["contractAddress"] = contractAddress
|
2019-09-25 18:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fields, nil
|
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
|
|
|
|
|
|
|
// getGasLimit returns the gas limit per block set in genesis
|
|
|
|
func (e *PublicEthAPI) getGasLimit() (int64, error) {
|
|
|
|
// Retrieve from gasLimit variable cache
|
|
|
|
if e.gasLimit != nil {
|
|
|
|
return *e.gasLimit, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query genesis block if hasn't been retrieved yet
|
|
|
|
genesis, err := e.cliCtx.Client.Genesis()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save value to gasLimit cached value
|
|
|
|
gasLimit := genesis.Genesis.ConsensusParams.Block.MaxGas
|
|
|
|
e.gasLimit = &gasLimit
|
|
|
|
return gasLimit, nil
|
|
|
|
}
|