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-01-21 19:12:35 +00:00
|
|
|
"math/big"
|
2020-01-16 23:21:49 +00:00
|
|
|
|
2020-02-20 22:13:19 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/ipfs"
|
|
|
|
|
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-01-21 19:12:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2020-01-16 23:21:49 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// APIName is the namespace for the super node's eth api
|
2020-01-16 23:21:49 +00:00
|
|
|
const APIName = "eth"
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// APIVersion is the version of the super node's eth api
|
2020-01-16 23:21:49 +00:00
|
|
|
const APIVersion = "0.0.1"
|
|
|
|
|
|
|
|
type PublicEthAPI struct {
|
2020-01-17 23:16:01 +00:00
|
|
|
b *Backend
|
2020-01-16 23:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPublicEthAPI creates a new PublicEthAPI with the provided underlying Backend
|
2020-01-17 23:16:01 +00:00
|
|
|
func NewPublicEthAPI(b *Backend) *PublicEthAPI {
|
2020-01-16 23:21:49 +00:00
|
|
|
return &PublicEthAPI{
|
|
|
|
b: b,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockNumber returns the block number of the chain head.
|
|
|
|
func (pea *PublicEthAPI) BlockNumber() hexutil.Uint64 {
|
2020-02-20 22:12:52 +00:00
|
|
|
number, _ := pea.b.Retriever.RetrieveLastBlockNumber()
|
2020-01-16 23:21:49 +00:00
|
|
|
return hexutil.Uint64(number)
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
// 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-01-21 19:12:35 +00:00
|
|
|
Contracts: addrStrs,
|
|
|
|
Topics: topicStrSets,
|
|
|
|
}
|
2020-02-20 22:12:52 +00:00
|
|
|
tx, err := pea.b.DB.Beginx()
|
2020-01-21 19:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// If we have a blockhash to filter on, fire off single retrieval query
|
|
|
|
if crit.BlockHash != nil {
|
2020-02-20 22:12:52 +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
|
|
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-20 22:12:52 +00:00
|
|
|
rctIPLDs, err := pea.b.Fetcher.FetchRcts(rctCIDs)
|
2020-01-21 19:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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-02-20 22:12:52 +00:00
|
|
|
startingBlockInt, err := pea.b.Retriever.RetrieveFirstBlockNumber()
|
2020-01-21 19:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
startingBlock = big.NewInt(startingBlockInt)
|
|
|
|
}
|
|
|
|
if endingBlock == nil {
|
2020-02-20 22:12:52 +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()
|
|
|
|
allRctCIDs := make([]ReceiptModel, 0)
|
|
|
|
for i := start; i <= end; i++ {
|
2020-02-20 22:12:52 +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...)
|
|
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-20 22:12:52 +00:00
|
|
|
rctIPLDs, err := pea.b.Fetcher.FetchRcts(allRctCIDs)
|
2020-01-21 19:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return extractLogsOfInterest(rctIPLDs, filter.Topics)
|
|
|
|
}
|
|
|
|
|
2020-01-26 19:55:26 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return pea.rpcMarshalBlock(block, true, fullTx)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionByHash returns the transaction for the given hash
|
|
|
|
// SuperNode cannot currently handle pending/tx_pool txs
|
|
|
|
func (pea *PublicEthAPI) GetTransactionByHash(ctx context.Context, hash common.Hash) (*RPCTransaction, error) {
|
|
|
|
// Try to return an already finalized transaction
|
|
|
|
tx, blockHash, blockNumber, index, err := pea.b.GetTransaction(ctx, hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if tx != nil {
|
2020-02-20 22:13:19 +00:00
|
|
|
return NewRPCTransaction(tx, blockHash, blockNumber, index), nil
|
2020-01-26 19:55:26 +00:00
|
|
|
}
|
|
|
|
// Transaction unknown, return as such
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractLogsOfInterest returns logs from the receipt IPLD
|
2020-02-20 22:13:19 +00:00
|
|
|
func extractLogsOfInterest(rctIPLDs []ipfs.BlockModel, wantedTopics [][]string) ([]*types.Log, error) {
|
2020-01-21 19:12:35 +00:00
|
|
|
var logs []*types.Log
|
|
|
|
for _, rctIPLD := range rctIPLDs {
|
2020-02-20 22:12:52 +00:00
|
|
|
rctRLP := rctIPLD
|
2020-01-26 19:55:26 +00:00
|
|
|
var rct types.Receipt
|
2020-02-20 22:13:19 +00:00
|
|
|
if err := rlp.DecodeBytes(rctRLP.Data, &rct); err != nil {
|
2020-01-21 19:12:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, log := range rct.Logs {
|
|
|
|
if wanted := wantedLog(wantedTopics, log.Topics); wanted == true {
|
|
|
|
logs = append(logs, log)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return logs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns true if the log matches on the filter
|
|
|
|
func wantedLog(wantedTopics [][]string, actualTopics []common.Hash) bool {
|
|
|
|
// actualTopics will always have length <= 4
|
2020-02-20 22:13:19 +00:00
|
|
|
// wantedTopics will always have length 4
|
2020-01-21 19:12:35 +00:00
|
|
|
matches := 0
|
|
|
|
for i, actualTopic := range actualTopics {
|
|
|
|
// If we have topics in this filter slot, count as a match if the actualTopic matches one of the ones in this filter slot
|
|
|
|
if len(wantedTopics[i]) > 0 {
|
|
|
|
matches += sliceContainsHash(wantedTopics[i], actualTopic)
|
|
|
|
} else {
|
|
|
|
// Filter slot is empty, not matching any topics at this slot => counts as a match
|
|
|
|
matches++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if matches == len(actualTopics) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns 1 if the slice contains the hash, 0 if it does not
|
|
|
|
func sliceContainsHash(slice []string, hash common.Hash) int {
|
|
|
|
for _, str := range slice {
|
|
|
|
if str == hash.String() {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-01-16 23:21:49 +00:00
|
|
|
// rpcMarshalHeader uses the generalized output filler, then adds the total difficulty field, which requires
|
2020-01-21 19:12:35 +00:00
|
|
|
// a `PublicEthAPI`.
|
|
|
|
func (pea *PublicEthAPI) rpcMarshalHeader(header *types.Header) (map[string]interface{}, error) {
|
2020-01-16 23:21:49 +00:00
|
|
|
fields := RPCMarshalHeader(header)
|
2020-01-21 19:12:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-26 19:55:26 +00:00
|
|
|
// RPCMarshalHeader converts the given header to the RPC output.
|
|
|
|
// This function is eth/internal so we have to make our own version here...
|
2020-01-16 23:21:49 +00:00
|
|
|
func RPCMarshalHeader(head *types.Header) map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"number": (*hexutil.Big)(head.Number),
|
|
|
|
"hash": head.Hash(),
|
|
|
|
"parentHash": head.ParentHash,
|
|
|
|
"nonce": head.Nonce,
|
|
|
|
"mixHash": head.MixDigest,
|
|
|
|
"sha3Uncles": head.UncleHash,
|
|
|
|
"logsBloom": head.Bloom,
|
|
|
|
"stateRoot": head.Root,
|
|
|
|
"miner": head.Coinbase,
|
|
|
|
"difficulty": (*hexutil.Big)(head.Difficulty),
|
|
|
|
"extraData": hexutil.Bytes(head.Extra),
|
|
|
|
"size": hexutil.Uint64(head.Size()),
|
|
|
|
"gasLimit": hexutil.Uint64(head.GasLimit),
|
|
|
|
"gasUsed": hexutil.Uint64(head.GasUsed),
|
|
|
|
"timestamp": hexutil.Uint64(head.Time),
|
|
|
|
"transactionsRoot": head.TxHash,
|
|
|
|
"receiptsRoot": head.ReceiptHash,
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 19:55:26 +00:00
|
|
|
|
|
|
|
// rpcMarshalBlock uses the generalized output filler, then adds the total difficulty field, which requires
|
|
|
|
// a `PublicBlockchainAPI`.
|
|
|
|
func (pea *PublicEthAPI) rpcMarshalBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) {
|
|
|
|
fields, err := RPCMarshalBlock(b, inclTx, fullTx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
td, err := pea.b.GetTd(b.Hash())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fields["totalDifficulty"] = (*hexutil.Big)(td)
|
|
|
|
return fields, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RPCMarshalBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are
|
|
|
|
// returned. When fullTx is true the returned block contains full transaction details, otherwise it will only contain
|
|
|
|
// transaction hashes.
|
|
|
|
func RPCMarshalBlock(block *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) {
|
|
|
|
fields := RPCMarshalHeader(block.Header())
|
|
|
|
fields["size"] = hexutil.Uint64(block.Size())
|
|
|
|
|
|
|
|
if inclTx {
|
|
|
|
formatTx := func(tx *types.Transaction) (interface{}, error) {
|
|
|
|
return tx.Hash(), nil
|
|
|
|
}
|
|
|
|
if fullTx {
|
|
|
|
formatTx = func(tx *types.Transaction) (interface{}, error) {
|
2020-02-20 22:13:19 +00:00
|
|
|
return NewRPCTransactionFromBlockHash(block, tx.Hash()), nil
|
2020-01-26 19:55:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
txs := block.Transactions()
|
|
|
|
transactions := make([]interface{}, len(txs))
|
|
|
|
var err error
|
|
|
|
for i, tx := range txs {
|
|
|
|
if transactions[i], err = formatTx(tx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fields["transactions"] = transactions
|
|
|
|
}
|
|
|
|
uncles := block.Uncles()
|
|
|
|
uncleHashes := make([]common.Hash, len(uncles))
|
|
|
|
for i, uncle := range uncles {
|
|
|
|
uncleHashes[i] = uncle.Hash()
|
|
|
|
}
|
|
|
|
fields["uncles"] = uncleHashes
|
|
|
|
|
|
|
|
return fields, nil
|
|
|
|
}
|
|
|
|
|
2020-02-20 22:13:19 +00:00
|
|
|
// NewRPCTransactionFromBlockHash returns a transaction that will serialize to the RPC representation.
|
|
|
|
func NewRPCTransactionFromBlockHash(b *types.Block, hash common.Hash) *RPCTransaction {
|
2020-01-26 19:55:26 +00:00
|
|
|
for idx, tx := range b.Transactions() {
|
|
|
|
if tx.Hash() == hash {
|
|
|
|
return newRPCTransactionFromBlockIndex(b, uint64(idx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newRPCTransactionFromBlockIndex returns a transaction that will serialize to the RPC representation.
|
|
|
|
func newRPCTransactionFromBlockIndex(b *types.Block, index uint64) *RPCTransaction {
|
|
|
|
txs := b.Transactions()
|
|
|
|
if index >= uint64(len(txs)) {
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
return NewRPCTransaction(txs[index], b.Hash(), b.NumberU64(), index)
|
2020-01-26 19:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
|
|
|
|
type RPCTransaction struct {
|
|
|
|
BlockHash *common.Hash `json:"blockHash"`
|
|
|
|
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"`
|
|
|
|
TransactionIndex *hexutil.Uint64 `json:"transactionIndex"`
|
|
|
|
Value *hexutil.Big `json:"value"`
|
|
|
|
V *hexutil.Big `json:"v"`
|
|
|
|
R *hexutil.Big `json:"r"`
|
|
|
|
S *hexutil.Big `json:"s"`
|
|
|
|
}
|
|
|
|
|
2020-02-20 22:13:19 +00:00
|
|
|
// NewRPCTransaction returns a transaction that will serialize to the RPC
|
2020-01-26 19:55:26 +00:00
|
|
|
// representation, with the given location metadata set (if available).
|
2020-02-20 22:13:19 +00:00
|
|
|
func NewRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber uint64, index uint64) *RPCTransaction {
|
2020-01-26 19:55:26 +00:00
|
|
|
var signer types.Signer = types.FrontierSigner{}
|
|
|
|
if tx.Protected() {
|
|
|
|
signer = types.NewEIP155Signer(tx.ChainId())
|
|
|
|
}
|
|
|
|
from, _ := types.Sender(signer, tx)
|
|
|
|
v, r, s := tx.RawSignatureValues()
|
|
|
|
|
|
|
|
result := &RPCTransaction{
|
|
|
|
From: from,
|
|
|
|
Gas: hexutil.Uint64(tx.Gas()),
|
|
|
|
GasPrice: (*hexutil.Big)(tx.GasPrice()),
|
|
|
|
Hash: tx.Hash(),
|
2020-02-20 22:13:19 +00:00
|
|
|
Input: hexutil.Bytes(tx.Data()), // somehow this is ending up `nil`
|
2020-01-26 19:55:26 +00:00
|
|
|
Nonce: hexutil.Uint64(tx.Nonce()),
|
|
|
|
To: tx.To(),
|
|
|
|
Value: (*hexutil.Big)(tx.Value()),
|
|
|
|
V: (*hexutil.Big)(v),
|
|
|
|
R: (*hexutil.Big)(r),
|
|
|
|
S: (*hexutil.Big)(s),
|
|
|
|
}
|
|
|
|
if blockHash != (common.Hash{}) {
|
|
|
|
result.BlockHash = &blockHash
|
|
|
|
result.BlockNumber = (*hexutil.Big)(new(big.Int).SetUint64(blockNumber))
|
|
|
|
result.TransactionIndex = (*hexutil.Uint64)(&index)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|