2020-10-26 13:58:37 +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/>.
|
|
|
|
|
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
2020-10-28 13:22:57 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-10-26 13:58:37 +00:00
|
|
|
"math/big"
|
|
|
|
|
2020-10-28 13:22:57 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
2020-10-26 13:58:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2021-07-27 12:07:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
2020-10-26 13:58:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2021-06-18 06:42:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2020-10-26 13:58:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2021-06-18 06:42:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2021-08-12 06:23:41 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/ipfs"
|
2020-10-26 13:58:37 +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...
|
|
|
|
func RPCMarshalHeader(head *types.Header) map[string]interface{} {
|
2021-07-27 12:07:50 +00:00
|
|
|
headerMap := map[string]interface{}{
|
2020-10-26 13:58:37 +00:00
|
|
|
"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,
|
|
|
|
}
|
2021-07-27 12:07:50 +00:00
|
|
|
|
|
|
|
if head.BaseFee != nil {
|
|
|
|
headerMap["baseFee"] = head.BaseFee
|
|
|
|
}
|
|
|
|
return headerMap
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
return NewRPCTransactionFromBlockHash(block, tx.Hash()), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-10-28 13:22:57 +00:00
|
|
|
// RPCMarshalBlockWithUncleHashes marshals the block with the provided uncle hashes
|
|
|
|
func RPCMarshalBlockWithUncleHashes(block *types.Block, uncleHashes []common.Hash, 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) {
|
|
|
|
return NewRPCTransactionFromBlockHash(block, tx.Hash()), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
fields["uncles"] = uncleHashes
|
|
|
|
|
|
|
|
return fields, nil
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:58:37 +00:00
|
|
|
// NewRPCTransactionFromBlockHash returns a transaction that will serialize to the RPC representation.
|
|
|
|
func NewRPCTransactionFromBlockHash(b *types.Block, hash common.Hash) *RPCTransaction {
|
|
|
|
for idx, tx := range b.Transactions() {
|
|
|
|
if tx.Hash() == hash {
|
|
|
|
return newRPCTransactionFromBlockIndex(b, uint64(idx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRPCTransaction returns a transaction that will serialize to the RPC
|
|
|
|
// representation, with the given location metadata set (if available).
|
2021-07-27 12:07:50 +00:00
|
|
|
func NewRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber uint64, index uint64, baseFee *big.Int) *RPCTransaction {
|
|
|
|
var signer types.Signer
|
2020-10-26 13:58:37 +00:00
|
|
|
if tx.Protected() {
|
2021-07-27 12:07:50 +00:00
|
|
|
signer = types.LatestSignerForChainID(tx.ChainId())
|
|
|
|
} else {
|
|
|
|
signer = types.HomesteadSigner{}
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
|
|
|
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(),
|
|
|
|
Input: hexutil.Bytes(tx.Data()), // somehow this is ending up `nil`
|
|
|
|
Nonce: hexutil.Uint64(tx.Nonce()),
|
|
|
|
To: tx.To(),
|
|
|
|
Value: (*hexutil.Big)(tx.Value()),
|
2021-07-27 12:07:50 +00:00
|
|
|
Type: hexutil.Uint64(tx.Type()),
|
2020-10-26 13:58:37 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-07-27 12:07:50 +00:00
|
|
|
switch tx.Type() {
|
|
|
|
case types.AccessListTxType:
|
|
|
|
al := tx.AccessList()
|
|
|
|
result.Accesses = &al
|
|
|
|
result.ChainID = (*hexutil.Big)(tx.ChainId())
|
|
|
|
case types.DynamicFeeTxType:
|
|
|
|
al := tx.AccessList()
|
|
|
|
result.Accesses = &al
|
|
|
|
result.ChainID = (*hexutil.Big)(tx.ChainId())
|
|
|
|
result.GasFeeCap = (*hexutil.Big)(tx.GasFeeCap())
|
|
|
|
result.GasTipCap = (*hexutil.Big)(tx.GasTipCap())
|
|
|
|
// if the transaction has been mined, compute the effective gas price
|
|
|
|
if baseFee != nil && blockHash != (common.Hash{}) {
|
|
|
|
// price = min(tip, gasFeeCap - baseFee) + baseFee
|
|
|
|
price := math.BigMin(new(big.Int).Add(tx.GasTipCap(), baseFee), tx.GasFeeCap())
|
|
|
|
result.GasPrice = (*hexutil.Big)(price)
|
|
|
|
} else {
|
|
|
|
result.GasPrice = nil
|
|
|
|
}
|
|
|
|
}
|
2020-10-26 13:58:37 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-10-28 13:22:57 +00:00
|
|
|
type rpcBlock struct {
|
|
|
|
Hash common.Hash `json:"hash"`
|
|
|
|
Transactions []rpcTransaction `json:"transactions"`
|
|
|
|
UncleHashes []common.Hash `json:"uncles"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rpcTransaction struct {
|
|
|
|
tx *types.Transaction
|
|
|
|
txExtraInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type txExtraInfo struct {
|
|
|
|
BlockNumber *string `json:"blockNumber,omitempty"`
|
|
|
|
BlockHash *common.Hash `json:"blockHash,omitempty"`
|
|
|
|
From *common.Address `json:"from,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *rpcTransaction) UnmarshalJSON(msg []byte) error {
|
|
|
|
if err := json.Unmarshal(msg, &tx.tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.Unmarshal(msg, &tx.txExtraInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBlockAndUncleHashes(cli *rpc.Client, ctx context.Context, method string, args ...interface{}) (*types.Block, []common.Hash, error) {
|
|
|
|
var raw json.RawMessage
|
|
|
|
err := cli.CallContext(ctx, &raw, method, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
} else if len(raw) == 0 {
|
|
|
|
return nil, nil, ethereum.NotFound
|
|
|
|
}
|
|
|
|
// Decode header and transactions.
|
|
|
|
var head *types.Header
|
|
|
|
var body rpcBlock
|
|
|
|
if err := json.Unmarshal(raw, &head); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(raw, &body); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
// Quick-verify transaction and uncle lists. This mostly helps with debugging the server.
|
|
|
|
if head.UncleHash == types.EmptyUncleHash && len(body.UncleHashes) > 0 {
|
|
|
|
return nil, nil, fmt.Errorf("server returned non-empty uncle list but block header indicates no uncles")
|
|
|
|
}
|
|
|
|
if head.UncleHash != types.EmptyUncleHash && len(body.UncleHashes) == 0 {
|
|
|
|
return nil, nil, fmt.Errorf("server returned empty uncle list but block header indicates uncles")
|
|
|
|
}
|
|
|
|
if head.TxHash == types.EmptyRootHash && len(body.Transactions) > 0 {
|
|
|
|
return nil, nil, fmt.Errorf("server returned non-empty transaction list but block header indicates no transactions")
|
|
|
|
}
|
|
|
|
if head.TxHash != types.EmptyRootHash && len(body.Transactions) == 0 {
|
|
|
|
return nil, nil, fmt.Errorf("server returned empty transaction list but block header indicates transactions")
|
|
|
|
}
|
|
|
|
txs := make([]*types.Transaction, len(body.Transactions))
|
|
|
|
for i, tx := range body.Transactions {
|
|
|
|
txs[i] = tx.tx
|
|
|
|
}
|
|
|
|
return types.NewBlockWithHeader(head).WithBody(txs, nil), body.UncleHashes, nil
|
|
|
|
}
|
|
|
|
|
2020-10-28 03:04:19 +00:00
|
|
|
// newRPCRawTransactionFromBlockIndex returns the bytes of a transaction given a block and a transaction index.
|
|
|
|
func newRPCRawTransactionFromBlockIndex(b *types.Block, index uint64) hexutil.Bytes {
|
|
|
|
txs := b.Transactions()
|
|
|
|
if index >= uint64(len(txs)) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
blob, _ := rlp.EncodeToBytes(txs[index])
|
|
|
|
return blob
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:58:37 +00:00
|
|
|
// 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
|
|
|
|
}
|
2021-07-27 12:07:50 +00:00
|
|
|
return NewRPCTransaction(txs[index], b.Hash(), b.NumberU64(), index, b.BaseFee())
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// extractLogsOfInterest returns logs from the receipt IPLD
|
2021-06-09 03:42:46 +00:00
|
|
|
func extractLogsOfInterest(config *params.ChainConfig, blockHash common.Hash, blockNumber uint64,
|
|
|
|
txs types.Transactions, rctIPLDs []ipfs.BlockModel, filter ReceiptFilter) ([]*types.Log, error) {
|
2021-04-29 13:33:27 +00:00
|
|
|
receipts := make(types.Receipts, len(rctIPLDs))
|
|
|
|
|
|
|
|
for i, rctBytes := range rctIPLDs {
|
|
|
|
rct := new(types.Receipt)
|
2021-07-27 12:07:50 +00:00
|
|
|
if err := rct.UnmarshalBinary(rctBytes.Data); err != nil {
|
2020-10-26 13:58:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-29 13:33:27 +00:00
|
|
|
receipts[i] = rct
|
|
|
|
}
|
|
|
|
|
|
|
|
err := receipts.DeriveFields(config, blockHash, blockNumber, txs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-09 03:42:46 +00:00
|
|
|
var unfilteredLogs []*types.Log
|
2021-04-29 13:33:27 +00:00
|
|
|
for _, receipt := range receipts {
|
2021-06-09 03:42:46 +00:00
|
|
|
unfilteredLogs = append(unfilteredLogs, receipt.Logs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
adders := make([]common.Address, len(filter.LogAddresses))
|
|
|
|
for i, addr := range filter.LogAddresses {
|
|
|
|
adders[i] = common.HexToAddress(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
topics := make([][]common.Hash, len(filter.Topics))
|
|
|
|
for i, v := range filter.Topics {
|
|
|
|
topics[i] = make([]common.Hash, len(v))
|
|
|
|
for j, topic := range v {
|
|
|
|
topics[i][j] = common.HexToHash(topic)
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-29 13:33:27 +00:00
|
|
|
|
2021-06-09 03:42:46 +00:00
|
|
|
logs := filterLogs(unfilteredLogs, nil, nil, adders, topics)
|
2020-10-26 13:58:37 +00:00
|
|
|
return logs, nil
|
|
|
|
}
|
|
|
|
|
2021-06-09 03:42:46 +00:00
|
|
|
func includes(addresses []common.Address, a common.Address) bool {
|
|
|
|
for _, addr := range addresses {
|
|
|
|
if addr == a {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterLogs creates a slice of logs matching the given criteria.
|
|
|
|
func filterLogs(logs []*types.Log, fromBlock, toBlock *big.Int, addresses []common.Address, topics [][]common.Hash) []*types.Log {
|
|
|
|
var ret []*types.Log
|
|
|
|
Logs:
|
|
|
|
for _, log := range logs {
|
|
|
|
if fromBlock != nil && fromBlock.Int64() >= 0 && fromBlock.Uint64() > log.BlockNumber {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if toBlock != nil && toBlock.Int64() >= 0 && toBlock.Uint64() < log.BlockNumber {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addresses) > 0 && !includes(addresses, log.Address) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// If the to filtered topics is greater than the amount of topics in logs, skip.
|
|
|
|
if len(topics) > len(log.Topics) {
|
2021-06-18 06:42:29 +00:00
|
|
|
continue
|
2021-06-09 03:42:46 +00:00
|
|
|
}
|
|
|
|
for i, sub := range topics {
|
|
|
|
match := len(sub) == 0 // empty rule set == wildcard
|
|
|
|
for _, topic := range sub {
|
|
|
|
if log.Topics[i] == topic {
|
|
|
|
match = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
continue Logs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = append(ret, log)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:58:37 +00:00
|
|
|
// returns true if the log matches on the filter
|
|
|
|
func wantedLog(wantedTopics [][]string, actualTopics []common.Hash) bool {
|
|
|
|
// actualTopics will always have length <= 4
|
|
|
|
// wantedTopics will always have length 4
|
|
|
|
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-10-28 13:22:57 +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)
|
|
|
|
}
|