2020-01-17 23:16:01 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-01-16 23:21:49 +00:00
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-20 20:33:18 +00:00
|
|
|
"errors"
|
2020-09-25 13:58:18 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2020-01-21 19:12:35 +00:00
|
|
|
"math/big"
|
2020-09-25 13:58:18 +00:00
|
|
|
"time"
|
|
|
|
|
2020-01-21 19:12:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-01-16 23:21:49 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2020-10-20 20:33:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2020-10-28 03:04:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2020-01-21 19:12:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2020-10-28 03:04:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-10-26 13:58:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
2020-10-20 20:33:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2020-10-28 03:04:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2020-01-16 23:21:49 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2020-10-20 20:33:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/vulcanize/ipld-eth-indexer/pkg/eth"
|
|
|
|
"github.com/vulcanize/ipld-eth-server/pkg/shared"
|
2020-01-16 23:21:49 +00:00
|
|
|
)
|
|
|
|
|
2020-06-30 00:16:52 +00:00
|
|
|
// APIName is the namespace for the watcher's eth api
|
2020-01-16 23:21:49 +00:00
|
|
|
const APIName = "eth"
|
|
|
|
|
2020-06-30 00:16:52 +00:00
|
|
|
// APIVersion is the version of the watcher's eth api
|
2020-01-16 23:21:49 +00:00
|
|
|
const APIVersion = "0.0.1"
|
|
|
|
|
|
|
|
type PublicEthAPI struct {
|
2020-10-26 13:58:37 +00:00
|
|
|
// Local db backend
|
2020-05-01 21:25:58 +00:00
|
|
|
B *Backend
|
2020-10-26 13:58:37 +00:00
|
|
|
|
|
|
|
// Remote node for forwarding cache misses
|
|
|
|
rpc *rpc.Client
|
|
|
|
ethClient *ethclient.Client
|
2020-01-16 23:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPublicEthAPI creates a new PublicEthAPI with the provided underlying Backend
|
2020-10-26 13:58:37 +00:00
|
|
|
func NewPublicEthAPI(b *Backend, client *rpc.Client) *PublicEthAPI {
|
2020-10-27 17:42:38 +00:00
|
|
|
var ethClient *ethclient.Client
|
|
|
|
if client != nil {
|
|
|
|
ethClient = ethclient.NewClient(client)
|
|
|
|
}
|
2020-01-16 23:21:49 +00:00
|
|
|
return &PublicEthAPI{
|
2020-10-26 13:58:37 +00:00
|
|
|
B: b,
|
|
|
|
rpc: client,
|
2020-10-27 17:42:38 +00:00
|
|
|
ethClient: ethClient,
|
2020-01-16 23:21:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 03:04:19 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
Headers and blocks
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// GetHeaderByNumber returns the requested canonical block header.
|
|
|
|
// * When blockNr is -1 the chain head is returned.
|
|
|
|
// * We cannot support pending block calls since we do not have an active miner
|
|
|
|
func (pea *PublicEthAPI) GetHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (map[string]interface{}, error) {
|
|
|
|
header, err := pea.B.HeaderByNumber(ctx, number)
|
|
|
|
if header != nil && err == nil {
|
|
|
|
return pea.rpcMarshalHeader(header)
|
|
|
|
}
|
|
|
|
if pea.ethClient != nil {
|
|
|
|
if header, err := pea.ethClient.HeaderByNumber(ctx, big.NewInt(number.Int64())); header != nil && err == nil {
|
|
|
|
return pea.rpcMarshalHeader(header)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeaderByHash returns the requested header by hash.
|
|
|
|
func (pea *PublicEthAPI) GetHeaderByHash(ctx context.Context, hash common.Hash) map[string]interface{} {
|
|
|
|
header, err := pea.B.HeaderByHash(ctx, hash)
|
|
|
|
if header != nil && err == nil {
|
|
|
|
if res, err := pea.rpcMarshalHeader(header); err != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pea.ethClient != nil {
|
|
|
|
if header, err := pea.ethClient.HeaderByHash(ctx, hash); header != nil && err == nil {
|
|
|
|
if res, err := pea.rpcMarshalHeader(header); err != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// rpcMarshalHeader uses the generalized output filler, then adds the total difficulty field
|
|
|
|
func (pea *PublicEthAPI) rpcMarshalHeader(header *types.Header) (map[string]interface{}, error) {
|
|
|
|
fields := RPCMarshalHeader(header)
|
|
|
|
td, err := pea.B.GetTd(header.Hash())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fields["totalDifficulty"] = (*hexutil.Big)(td)
|
|
|
|
return fields, nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 23:21:49 +00:00
|
|
|
// BlockNumber returns the block number of the chain head.
|
|
|
|
func (pea *PublicEthAPI) BlockNumber() hexutil.Uint64 {
|
2020-05-01 21:25:58 +00:00
|
|
|
number, _ := pea.B.Retriever.RetrieveLastBlockNumber()
|
2020-01-16 23:21:49 +00:00
|
|
|
return hexutil.Uint64(number)
|
|
|
|
}
|
|
|
|
|
2020-10-28 03:04:19 +00:00
|
|
|
// GetBlockByNumber returns the requested canonical block.
|
|
|
|
// * When blockNr is -1 the chain head is returned.
|
|
|
|
// * We cannot support pending block calls since we do not have an active miner
|
|
|
|
// * When fullTx is true all transactions in the block are returned, otherwise
|
|
|
|
// only the transaction hash is returned.
|
|
|
|
func (pea *PublicEthAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) {
|
|
|
|
block, err := pea.B.BlockByNumber(ctx, number)
|
|
|
|
if block != nil && err == nil {
|
|
|
|
return pea.rpcMarshalBlock(block, true, fullTx)
|
|
|
|
}
|
|
|
|
if pea.ethClient != nil {
|
|
|
|
if block, err := pea.ethClient.BlockByNumber(ctx, big.NewInt(number.Int64())); block != nil && err == nil {
|
|
|
|
return pea.rpcMarshalBlock(block, true, fullTx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockByHash returns the requested block. When fullTx is true all transactions in the block are returned in full
|
|
|
|
// detail, otherwise only the transaction hash is returned.
|
|
|
|
func (pea *PublicEthAPI) GetBlockByHash(ctx context.Context, hash common.Hash, fullTx bool) (map[string]interface{}, error) {
|
|
|
|
block, err := pea.B.BlockByHash(ctx, hash)
|
|
|
|
if block != nil && err == nil {
|
|
|
|
return pea.rpcMarshalBlock(block, true, fullTx)
|
|
|
|
}
|
|
|
|
if pea.ethClient != nil {
|
|
|
|
if block, err := pea.ethClient.BlockByHash(ctx, hash); block != nil && err == nil {
|
|
|
|
return pea.rpcMarshalBlock(block, true, fullTx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Uncles
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// GetUncleByBlockNumberAndIndex returns the uncle block for the given block hash and index. When fullTx is true
|
|
|
|
// all transactions in the block are returned in full detail, otherwise only the transaction hash is returned.
|
|
|
|
func (pea *PublicEthAPI) GetUncleByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) (map[string]interface{}, error) {
|
|
|
|
block, err := pea.B.BlockByNumber(ctx, blockNr)
|
|
|
|
if block != nil {
|
|
|
|
uncles := block.Uncles()
|
|
|
|
if index >= hexutil.Uint(len(uncles)) {
|
|
|
|
logrus.Debugf("uncle with index %s request at block number %d was not found", index.String(), blockNr.Int64())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
block = types.NewBlockWithHeader(uncles[index])
|
|
|
|
return pea.rpcMarshalBlock(block, false, false)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUncleByBlockHashAndIndex returns the uncle block for the given block hash and index. When fullTx is true
|
|
|
|
// all transactions in the block are returned in full detail, otherwise only the transaction hash is returned.
|
|
|
|
func (pea *PublicEthAPI) GetUncleByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index hexutil.Uint) (map[string]interface{}, error) {
|
|
|
|
block, err := pea.B.BlockByHash(ctx, blockHash)
|
|
|
|
if block != nil {
|
|
|
|
uncles := block.Uncles()
|
|
|
|
if index >= hexutil.Uint(len(uncles)) {
|
|
|
|
logrus.Debugf("uncle with index %s request at block hash %s was not found", index.String(), blockHash.Hex())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
block = types.NewBlockWithHeader(uncles[index])
|
|
|
|
return pea.rpcMarshalBlock(block, false, false)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUncleCountByBlockNumber returns number of uncles in the block for the given block number
|
|
|
|
func (pea *PublicEthAPI) GetUncleCountByBlockNumber(ctx context.Context, blockNr rpc.BlockNumber) *hexutil.Uint {
|
|
|
|
if block, _ := pea.B.BlockByNumber(ctx, blockNr); block != nil {
|
|
|
|
n := hexutil.Uint(len(block.Uncles()))
|
|
|
|
return &n
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUncleCountByBlockHash returns number of uncles in the block for the given block hash
|
|
|
|
func (pea *PublicEthAPI) GetUncleCountByBlockHash(ctx context.Context, blockHash common.Hash) *hexutil.Uint {
|
|
|
|
if block, _ := pea.B.BlockByHash(ctx, blockHash); block != nil {
|
|
|
|
n := hexutil.Uint(len(block.Uncles()))
|
|
|
|
return &n
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Transactions
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// GetTransactionCount returns the number of transactions the given address has sent for the given block number
|
|
|
|
func (pea *PublicEthAPI) GetTransactionCount(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Uint64, error) {
|
|
|
|
// Resolve block number and use its state to ask for the nonce
|
|
|
|
state, _, err := pea.B.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
|
|
|
|
if state == nil || err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nonce := state.GetNonce(address)
|
|
|
|
return (*hexutil.Uint64)(&nonce), state.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockTransactionCountByNumber returns the number of transactions in the block with the given block number.
|
|
|
|
func (pea *PublicEthAPI) GetBlockTransactionCountByNumber(ctx context.Context, blockNr rpc.BlockNumber) *hexutil.Uint {
|
|
|
|
if block, _ := pea.B.BlockByNumber(ctx, blockNr); block != nil {
|
|
|
|
n := hexutil.Uint(len(block.Transactions()))
|
|
|
|
return &n
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockTransactionCountByHash returns the number of transactions in the block with the given hash.
|
|
|
|
func (pea *PublicEthAPI) GetBlockTransactionCountByHash(ctx context.Context, blockHash common.Hash) *hexutil.Uint {
|
|
|
|
if block, _ := pea.B.BlockByHash(ctx, blockHash); block != nil {
|
|
|
|
n := hexutil.Uint(len(block.Transactions()))
|
|
|
|
return &n
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionByBlockNumberAndIndex returns the transaction for the given block number and index.
|
|
|
|
func (pea *PublicEthAPI) GetTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) *RPCTransaction {
|
|
|
|
if block, _ := pea.B.BlockByNumber(ctx, blockNr); block != nil {
|
|
|
|
return newRPCTransactionFromBlockIndex(block, uint64(index))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionByBlockHashAndIndex returns the transaction for the given block hash and index.
|
|
|
|
func (pea *PublicEthAPI) GetTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index hexutil.Uint) *RPCTransaction {
|
|
|
|
if block, _ := pea.B.BlockByHash(ctx, blockHash); block != nil {
|
|
|
|
return newRPCTransactionFromBlockIndex(block, uint64(index))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawTransactionByBlockNumberAndIndex returns the bytes of the transaction for the given block number and index.
|
|
|
|
func (pea *PublicEthAPI) GetRawTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) hexutil.Bytes {
|
|
|
|
if block, _ := pea.B.BlockByNumber(ctx, blockNr); block != nil {
|
|
|
|
return newRPCRawTransactionFromBlockIndex(block, uint64(index))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawTransactionByBlockHashAndIndex returns the bytes of the transaction for the given block hash and index.
|
|
|
|
func (pea *PublicEthAPI) GetRawTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index hexutil.Uint) hexutil.Bytes {
|
|
|
|
if block, _ := pea.B.BlockByHash(ctx, blockHash); block != nil {
|
|
|
|
return newRPCRawTransactionFromBlockIndex(block, uint64(index))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionByHash returns the transaction for the given hash
|
|
|
|
// eth ipld-eth-server cannot currently handle pending/tx_pool txs
|
|
|
|
func (pea *PublicEthAPI) GetTransactionByHash(ctx context.Context, hash common.Hash) (*RPCTransaction, error) {
|
|
|
|
tx, blockHash, blockNumber, index, err := pea.B.GetTransaction(ctx, hash)
|
|
|
|
if tx != nil && err == nil {
|
|
|
|
return NewRPCTransaction(tx, blockHash, blockNumber, index), nil
|
|
|
|
}
|
|
|
|
if pea.rpc != nil {
|
|
|
|
if tx, err := pea.remoteGetTransactionByHash(ctx, hash); tx != nil && err == nil {
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pea *PublicEthAPI) remoteGetTransactionByHash(ctx context.Context, hash common.Hash) (*RPCTransaction, error) {
|
|
|
|
var tx *RPCTransaction
|
|
|
|
if err := pea.rpc.CallContext(ctx, &tx, "eth_getTransactionByHash", hash); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawTransactionByHash returns the bytes of the transaction for the given hash.
|
|
|
|
func (pea *PublicEthAPI) GetRawTransactionByHash(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) {
|
|
|
|
// Retrieve a finalized transaction, or a pooled otherwise
|
|
|
|
tx, _, _, _, err := pea.B.GetTransaction(ctx, hash)
|
|
|
|
if tx != nil && err == nil {
|
|
|
|
return rlp.EncodeToBytes(tx)
|
|
|
|
}
|
|
|
|
if pea.rpc != nil {
|
|
|
|
if tx, err := pea.remoteGetRawTransactionByHash(ctx, hash); tx != nil && err == nil {
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pea *PublicEthAPI) remoteGetRawTransactionByHash(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) {
|
|
|
|
var tx hexutil.Bytes
|
|
|
|
if err := pea.rpc.CallContext(ctx, &tx, "eth_getRawTransactionByHash", hash); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Receipts and Logs
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// GetTransactionReceipt returns the transaction receipt for the given transaction hash.
|
|
|
|
func (pea *PublicEthAPI) GetTransactionReceipt(ctx context.Context, hash common.Hash) (map[string]interface{}, error) {
|
|
|
|
tx, blockHash, blockNumber, index := rawdb.ReadTransaction(pea.B.ChainDb(), hash)
|
|
|
|
if tx == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
receipts, err := pea.B.GetReceipts(ctx, blockHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(receipts) <= int(index) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
receipt := receipts[index]
|
|
|
|
|
|
|
|
var signer types.Signer = types.FrontierSigner{}
|
|
|
|
if tx.Protected() {
|
|
|
|
signer = types.NewEIP155Signer(tx.ChainId())
|
|
|
|
}
|
|
|
|
from, _ := types.Sender(signer, tx)
|
|
|
|
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"blockHash": blockHash,
|
|
|
|
"blockNumber": hexutil.Uint64(blockNumber),
|
|
|
|
"transactionHash": hash,
|
|
|
|
"transactionIndex": hexutil.Uint64(index),
|
|
|
|
"from": from,
|
|
|
|
"to": tx.To(),
|
|
|
|
"gasUsed": hexutil.Uint64(receipt.GasUsed),
|
|
|
|
"cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed),
|
|
|
|
"contractAddress": nil,
|
|
|
|
"logs": receipt.Logs,
|
|
|
|
"logsBloom": receipt.Bloom,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign receipt status or post state.
|
|
|
|
if len(receipt.PostState) > 0 {
|
|
|
|
fields["root"] = hexutil.Bytes(receipt.PostState)
|
|
|
|
} else {
|
|
|
|
fields["status"] = hexutil.Uint(receipt.Status)
|
|
|
|
}
|
|
|
|
if receipt.Logs == nil {
|
|
|
|
fields["logs"] = [][]*types.Log{}
|
|
|
|
}
|
|
|
|
// If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation
|
|
|
|
if receipt.ContractAddress != (common.Address{}) {
|
|
|
|
fields["contractAddress"] = receipt.ContractAddress
|
|
|
|
}
|
|
|
|
return fields, nil
|
|
|
|
}
|
|
|
|
|
2020-01-21 19:12:35 +00:00
|
|
|
// GetLogs returns logs matching the given argument that are stored within the state.
|
|
|
|
//
|
|
|
|
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getlogs
|
|
|
|
func (pea *PublicEthAPI) GetLogs(ctx context.Context, crit ethereum.FilterQuery) ([]*types.Log, error) {
|
2020-10-26 13:58:37 +00:00
|
|
|
logs, err := pea.getLogs(ctx, crit)
|
|
|
|
if err != nil && pea.rpc != nil {
|
|
|
|
if arg, err := toFilterArg(crit); err == nil {
|
|
|
|
var result []*types.Log
|
|
|
|
if err := pea.rpc.CallContext(ctx, &result, "eth_getLogs", arg); err == nil {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return logs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pea *PublicEthAPI) getLogs(ctx context.Context, crit ethereum.FilterQuery) ([]*types.Log, error) {
|
2020-01-21 19:12:35 +00:00
|
|
|
// Convert FilterQuery into ReceiptFilter
|
|
|
|
addrStrs := make([]string, len(crit.Addresses))
|
|
|
|
for i, addr := range crit.Addresses {
|
|
|
|
addrStrs[i] = addr.String()
|
|
|
|
}
|
|
|
|
topicStrSets := make([][]string, 4)
|
|
|
|
for i, topicSet := range crit.Topics {
|
|
|
|
if i > 3 {
|
2020-02-20 22:13:19 +00:00
|
|
|
// don't allow more than 4 topics
|
2020-01-21 19:12:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
for _, topic := range topicSet {
|
|
|
|
topicStrSets[i] = append(topicStrSets[i], topic.String())
|
|
|
|
}
|
|
|
|
}
|
2020-02-03 18:22:29 +00:00
|
|
|
filter := ReceiptFilter{
|
2020-04-02 03:34:06 +00:00
|
|
|
LogAddresses: addrStrs,
|
|
|
|
Topics: topicStrSets,
|
2020-01-21 19:12:35 +00:00
|
|
|
}
|
2020-05-01 21:25:58 +00:00
|
|
|
|
|
|
|
// Begin tx
|
|
|
|
tx, err := pea.B.DB.Beginx()
|
2020-01-21 19:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-01 21:25:58 +00:00
|
|
|
defer func() {
|
|
|
|
if p := recover(); p != nil {
|
|
|
|
shared.Rollback(tx)
|
|
|
|
panic(p)
|
|
|
|
} else if err != nil {
|
|
|
|
shared.Rollback(tx)
|
|
|
|
} else {
|
|
|
|
err = tx.Commit()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-01-21 19:12:35 +00:00
|
|
|
// If we have a blockhash to filter on, fire off single retrieval query
|
|
|
|
if crit.BlockHash != nil {
|
2020-05-01 21:25:58 +00:00
|
|
|
rctCIDs, err := pea.B.Retriever.RetrieveRctCIDs(tx, filter, 0, crit.BlockHash, nil)
|
2020-01-21 19:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-01 21:25:58 +00:00
|
|
|
rctIPLDs, err := pea.B.Fetcher.FetchRcts(tx, rctCIDs)
|
2020-01-21 19:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-01 21:25:58 +00:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
return extractLogsOfInterest(rctIPLDs, filter.Topics)
|
|
|
|
}
|
|
|
|
// Otherwise, create block range from criteria
|
|
|
|
// nil values are filled in; to request a single block have both ToBlock and FromBlock equal that number
|
|
|
|
startingBlock := crit.FromBlock
|
|
|
|
endingBlock := crit.ToBlock
|
|
|
|
if startingBlock == nil {
|
2020-10-26 13:58:37 +00:00
|
|
|
startingBlock = common.Big0
|
2020-01-21 19:12:35 +00:00
|
|
|
}
|
|
|
|
if endingBlock == nil {
|
2020-05-01 21:25:58 +00:00
|
|
|
endingBlockInt, err := pea.B.Retriever.RetrieveLastBlockNumber()
|
2020-01-21 19:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
endingBlock = big.NewInt(endingBlockInt)
|
|
|
|
}
|
|
|
|
start := startingBlock.Int64()
|
|
|
|
end := endingBlock.Int64()
|
2020-08-31 15:47:06 +00:00
|
|
|
allRctCIDs := make([]eth.ReceiptModel, 0)
|
2020-01-21 19:12:35 +00:00
|
|
|
for i := start; i <= end; i++ {
|
2020-05-01 21:25:58 +00:00
|
|
|
rctCIDs, err := pea.B.Retriever.RetrieveRctCIDs(tx, filter, i, nil, nil)
|
2020-01-21 19:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
allRctCIDs = append(allRctCIDs, rctCIDs...)
|
|
|
|
}
|
2020-05-01 21:25:58 +00:00
|
|
|
rctIPLDs, err := pea.B.Fetcher.FetchRcts(tx, allRctCIDs)
|
2020-05-01 16:07:47 +00:00
|
|
|
if err != nil {
|
2020-01-21 19:12:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-01 16:07:47 +00:00
|
|
|
if err := tx.Commit(); err != nil {
|
2020-01-21 19:12:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-01 21:25:58 +00:00
|
|
|
logs, err := extractLogsOfInterest(rctIPLDs, filter.Topics)
|
|
|
|
return logs, err // need to return err variable so that we return the err = tx.Commit() assignment in the defer
|
2020-01-21 19:12:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 03:04:19 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
State and Storage
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// GetBalance returns the amount of wei for the given address in the state of the
|
|
|
|
// given block number. The rpc.LatestBlockNumber and rpc.PendingBlockNumber meta
|
|
|
|
// block numbers are also allowed.
|
|
|
|
func (pea *PublicEthAPI) GetBalance(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Big, error) {
|
|
|
|
state, _, err := pea.B.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
|
|
|
|
if state == nil || err != nil {
|
|
|
|
return nil, err
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
2020-10-28 03:04:19 +00:00
|
|
|
return (*hexutil.Big)(state.GetBalance(address)), state.Error()
|
2020-01-26 19:55:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 03:04:19 +00:00
|
|
|
// GetStorageAt returns the storage from the state at the given address, key and
|
|
|
|
// block number. The rpc.LatestBlockNumber and rpc.PendingBlockNumber meta block
|
|
|
|
// numbers are also allowed.
|
|
|
|
func (pea *PublicEthAPI) GetStorageAt(ctx context.Context, address common.Address, key string, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) {
|
|
|
|
state, _, err := pea.B.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
|
|
|
|
if state == nil || err != nil {
|
|
|
|
return nil, err
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
2020-10-28 03:04:19 +00:00
|
|
|
res := state.GetState(address, common.HexToHash(key))
|
|
|
|
return res[:], state.Error()
|
2020-01-26 19:55:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 03:04:19 +00:00
|
|
|
// GetCode returns the code stored at the given address in the state for the given block number.
|
|
|
|
func (pea *PublicEthAPI) GetCode(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) {
|
|
|
|
state, _, err := pea.B.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
|
|
|
|
if state == nil || err != nil {
|
|
|
|
return nil, err
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
2020-10-28 03:04:19 +00:00
|
|
|
code := state.GetCode(address)
|
|
|
|
return code, state.Error()
|
2020-01-26 19:55:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 03:04:19 +00:00
|
|
|
// Result structs for GetProof
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
type StorageResult struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
Value *hexutil.Big `json:"value"`
|
|
|
|
Proof []string `json:"proof"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProof returns the Merkle-proof for a given account and optionally some storage keys.
|
|
|
|
func (pea *PublicEthAPI) GetProof(ctx context.Context, address common.Address, storageKeys []string, blockNrOrHash rpc.BlockNumberOrHash) (*AccountResult, error) {
|
|
|
|
state, _, err := pea.B.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
|
|
|
|
if state == nil || err != nil {
|
|
|
|
return nil, err
|
2020-01-26 19:55:26 +00:00
|
|
|
}
|
2020-10-28 03:04:19 +00:00
|
|
|
|
|
|
|
storageTrie := state.StorageTrie(address)
|
|
|
|
storageHash := types.EmptyRootHash
|
|
|
|
codeHash := state.GetCodeHash(address)
|
|
|
|
storageProof := make([]StorageResult, len(storageKeys))
|
|
|
|
|
|
|
|
// if we have a storageTrie, (which means the account exists), we can update the storagehash
|
|
|
|
if storageTrie != nil {
|
|
|
|
storageHash = storageTrie.Hash()
|
|
|
|
} else {
|
|
|
|
// no storageTrie means the account does not exist, so the codeHash is the hash of an empty bytearray.
|
|
|
|
codeHash = crypto.Keccak256Hash(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the proof for the storageKeys
|
|
|
|
for i, key := range storageKeys {
|
|
|
|
if storageTrie != nil {
|
|
|
|
proof, storageError := state.GetStorageProof(address, common.HexToHash(key))
|
|
|
|
if storageError != nil {
|
|
|
|
return nil, storageError
|
|
|
|
}
|
|
|
|
storageProof[i] = StorageResult{key, (*hexutil.Big)(state.GetState(address, common.HexToHash(key)).Big()), common.ToHexArray(proof)}
|
|
|
|
} else {
|
|
|
|
storageProof[i] = StorageResult{key, &hexutil.Big{}, []string{}}
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 03:04:19 +00:00
|
|
|
// create the accountProof
|
|
|
|
accountProof, proofErr := state.GetProof(address)
|
|
|
|
if proofErr != nil {
|
|
|
|
return nil, proofErr
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
2020-10-28 03:04:19 +00:00
|
|
|
|
|
|
|
return &AccountResult{
|
|
|
|
Address: address,
|
|
|
|
AccountProof: common.ToHexArray(accountProof),
|
|
|
|
Balance: (*hexutil.Big)(state.GetBalance(address)),
|
|
|
|
CodeHash: codeHash,
|
|
|
|
Nonce: hexutil.Uint64(state.GetNonce(address)),
|
|
|
|
StorageHash: storageHash,
|
|
|
|
StorageProof: storageProof,
|
|
|
|
}, state.Error()
|
2020-01-26 19:55:26 +00:00
|
|
|
}
|
2020-09-25 13:58:18 +00:00
|
|
|
|
2020-10-20 20:33:18 +00:00
|
|
|
// Call executes the given transaction on the state for the given block number.
|
|
|
|
//
|
|
|
|
// Additionally, the caller can specify a batch of contract for fields overriding.
|
|
|
|
//
|
|
|
|
// Note, this function doesn't make and changes in the state/blockchain and is
|
|
|
|
// useful to execute and retrieve values.
|
|
|
|
func (pea *PublicEthAPI) Call(ctx context.Context, args CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *map[common.Address]account) (hexutil.Bytes, error) {
|
|
|
|
var accounts map[common.Address]account
|
|
|
|
if overrides != nil {
|
|
|
|
accounts = *overrides
|
|
|
|
}
|
|
|
|
result, _, failed, err := DoCall(ctx, pea.B, args, blockNrOrHash, accounts, 5*time.Second, pea.B.Config.RPCGasCap)
|
2020-10-26 13:58:37 +00:00
|
|
|
if (failed || err != nil) && pea.rpc != nil {
|
|
|
|
if res, err := pea.remoteCall(ctx, args, blockNrOrHash, overrides); res != nil && err == nil {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
2020-10-20 20:33:18 +00:00
|
|
|
if failed && err == nil {
|
|
|
|
return nil, errors.New("eth_call failed without error")
|
|
|
|
}
|
|
|
|
return (hexutil.Bytes)(result), err
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:58:37 +00:00
|
|
|
func (pea *PublicEthAPI) remoteCall(ctx context.Context, msg CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *map[common.Address]account) (hexutil.Bytes, error) {
|
|
|
|
var hex hexutil.Bytes
|
|
|
|
if err := pea.rpc.CallContext(ctx, &hex, "eth_call", msg, blockNrOrHash, overrides); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return hex, nil
|
|
|
|
}
|
|
|
|
|
2020-09-25 13:58:18 +00:00
|
|
|
// CallArgs represents the arguments for a call.
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-10-20 20:33:18 +00:00
|
|
|
func DoCall(ctx context.Context, b *Backend, args CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides map[common.Address]account, timeout time.Duration, globalGasCap *big.Int) ([]byte, uint64, bool, error) {
|
2020-09-25 13:58:18 +00:00
|
|
|
defer func(start time.Time) {
|
2020-10-20 20:33:18 +00:00
|
|
|
logrus.Debugf("Executing EVM call finished %s runtime %s", time.Now().String(), time.Since(start).String())
|
2020-09-25 13:58:18 +00:00
|
|
|
}(time.Now())
|
|
|
|
state, header, err := b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
|
|
|
|
if state == nil || err != nil {
|
|
|
|
return nil, 0, false, err
|
|
|
|
}
|
|
|
|
// Set sender address or use a default if none specified
|
|
|
|
var addr common.Address
|
|
|
|
if args.From == nil {
|
2020-10-20 20:33:18 +00:00
|
|
|
if b.Config.DefaultSender != nil {
|
|
|
|
addr = *b.Config.DefaultSender
|
2020-09-25 13:58:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addr = *args.From
|
|
|
|
}
|
|
|
|
// Override the fields of specified contracts before execution.
|
|
|
|
for addr, account := range overrides {
|
|
|
|
// Override account nonce.
|
|
|
|
if account.Nonce != nil {
|
|
|
|
state.SetNonce(addr, uint64(*account.Nonce))
|
|
|
|
}
|
|
|
|
// Override account(contract) code.
|
|
|
|
if account.Code != nil {
|
|
|
|
state.SetCode(addr, *account.Code)
|
|
|
|
}
|
|
|
|
// Override account balance.
|
|
|
|
if account.Balance != nil {
|
|
|
|
state.SetBalance(addr, (*big.Int)(*account.Balance))
|
|
|
|
}
|
|
|
|
if account.State != nil && account.StateDiff != nil {
|
|
|
|
return nil, 0, false, fmt.Errorf("account %s has both 'state' and 'stateDiff'", addr.Hex())
|
|
|
|
}
|
|
|
|
// Replace entire state if caller requires.
|
|
|
|
if account.State != nil {
|
|
|
|
state.SetStorage(addr, *account.State)
|
|
|
|
}
|
|
|
|
// Apply state diff into specified accounts.
|
|
|
|
if account.StateDiff != nil {
|
|
|
|
for key, value := range *account.StateDiff {
|
|
|
|
state.SetState(addr, key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set default gas & gas price if none were set
|
|
|
|
gas := uint64(math.MaxUint64 / 2)
|
|
|
|
if args.Gas != nil {
|
|
|
|
gas = uint64(*args.Gas)
|
|
|
|
}
|
|
|
|
if globalGasCap != nil && globalGasCap.Uint64() < gas {
|
2020-10-20 20:33:18 +00:00
|
|
|
logrus.Warnf("Caller gas above allowance, capping; requested: %d, cap: %d", gas, globalGasCap)
|
2020-09-25 13:58:18 +00:00
|
|
|
gas = globalGasCap.Uint64()
|
|
|
|
}
|
2020-10-20 20:33:18 +00:00
|
|
|
gasPrice := new(big.Int).SetUint64(params.GWei)
|
2020-09-25 13:58:18 +00:00
|
|
|
if args.GasPrice != nil {
|
|
|
|
gasPrice = args.GasPrice.ToInt()
|
|
|
|
}
|
|
|
|
|
|
|
|
value := new(big.Int)
|
|
|
|
if args.Value != nil {
|
|
|
|
value = args.Value.ToInt()
|
|
|
|
}
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
if args.Data != nil {
|
|
|
|
data = []byte(*args.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new call message
|
|
|
|
msg := types.NewMessage(addr, args.To, 0, value, gas, gasPrice, data, false)
|
|
|
|
|
|
|
|
// Setup context so it may be cancelled the call has completed
|
|
|
|
// or, in case of unmetered gas, setup a context with a timeout.
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
if timeout > 0 {
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
|
|
|
} else {
|
|
|
|
ctx, cancel = context.WithCancel(ctx)
|
|
|
|
}
|
|
|
|
// Make sure the context is cancelled when the call has completed
|
|
|
|
// this makes sure resources are cleaned up.
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Get a new instance of the EVM.
|
2020-10-20 20:33:18 +00:00
|
|
|
evm, err := b.GetEVM(ctx, msg, state, header)
|
2020-09-25 13:58:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, false, err
|
|
|
|
}
|
|
|
|
// Wait for the context to be done and cancel the evm. Even if the
|
|
|
|
// EVM has finished, cancelling may be done (repeatedly)
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
evm.Cancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Setup the gas pool (also for unmetered requests)
|
|
|
|
// and apply the message.
|
|
|
|
gp := new(core.GasPool).AddGas(math.MaxUint64)
|
|
|
|
res, gas, failed, err := core.ApplyMessage(evm, msg, gp)
|
|
|
|
// If the timer caused an abort, return an appropriate error message
|
|
|
|
if evm.Cancelled() {
|
|
|
|
return nil, 0, false, fmt.Errorf("execution aborted (timeout = %v)", timeout)
|
|
|
|
}
|
|
|
|
return res, gas, failed, err
|
|
|
|
}
|
2020-10-26 13:58:37 +00:00
|
|
|
|
|
|
|
func toFilterArg(q ethereum.FilterQuery) (interface{}, error) {
|
|
|
|
arg := map[string]interface{}{
|
|
|
|
"address": q.Addresses,
|
|
|
|
"topics": q.Topics,
|
|
|
|
}
|
|
|
|
if q.BlockHash != nil {
|
|
|
|
arg["blockHash"] = *q.BlockHash
|
|
|
|
if q.FromBlock != nil || q.ToBlock != nil {
|
|
|
|
return nil, fmt.Errorf("cannot specify both BlockHash and FromBlock/ToBlock")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if q.FromBlock == nil {
|
|
|
|
arg["fromBlock"] = "0x0"
|
|
|
|
} else {
|
|
|
|
arg["fromBlock"] = toBlockNumArg(q.FromBlock)
|
|
|
|
}
|
|
|
|
arg["toBlock"] = toBlockNumArg(q.ToBlock)
|
|
|
|
}
|
|
|
|
return arg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toBlockNumArg(number *big.Int) string {
|
|
|
|
if number == nil {
|
|
|
|
return "latest"
|
|
|
|
}
|
|
|
|
return hexutil.EncodeBig(number)
|
|
|
|
}
|