2020-08-01 07:39:04 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-08 19:39:31 +00:00
|
|
|
"math/big"
|
2020-08-22 20:13:38 +00:00
|
|
|
|
2020-09-03 07:51:19 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/eth/filters"
|
|
|
|
|
2020-08-01 07:39:04 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/hexutil"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/internal/ethapi"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EthAPI is a collection of functions that are exposed in the
|
|
|
|
type EthAPI interface {
|
2020-10-12 08:39:33 +00:00
|
|
|
// Block related (proposed file: ./eth_blocks.go)
|
2020-08-01 07:39:04 +00:00
|
|
|
GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error)
|
2020-11-06 07:59:50 +00:00
|
|
|
GetBlockByHash(ctx context.Context, hash rpc.BlockNumberOrHash, fullTx bool) (map[string]interface{}, error)
|
2020-08-29 21:28:09 +00:00
|
|
|
GetBlockTransactionCountByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*hexutil.Uint, error)
|
|
|
|
GetBlockTransactionCountByHash(ctx context.Context, blockHash common.Hash) (*hexutil.Uint, error)
|
2020-10-12 08:39:33 +00:00
|
|
|
|
2020-10-14 15:59:42 +00:00
|
|
|
// Transaction related (see ./eth_txs.go)
|
2020-09-08 19:39:31 +00:00
|
|
|
GetTransactionByHash(ctx context.Context, hash common.Hash) (*RPCTransaction, error)
|
|
|
|
GetTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, txIndex hexutil.Uint64) (*RPCTransaction, error)
|
|
|
|
GetTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, txIndex hexutil.Uint) (*RPCTransaction, error)
|
2020-10-14 15:59:42 +00:00
|
|
|
|
|
|
|
// Receipt related (see ./eth_receipts.go)
|
2020-10-12 08:39:33 +00:00
|
|
|
GetTransactionReceipt(ctx context.Context, hash common.Hash) (map[string]interface{}, error)
|
2020-10-14 15:59:42 +00:00
|
|
|
GetLogs(ctx context.Context, crit filters.FilterCriteria) ([]*types.Log, error)
|
2020-10-12 08:39:33 +00:00
|
|
|
|
2020-10-14 15:59:42 +00:00
|
|
|
// Uncle related (see ./eth_uncles.go)
|
2020-09-14 06:59:07 +00:00
|
|
|
GetUncleByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) (map[string]interface{}, error)
|
|
|
|
GetUncleByBlockHashAndIndex(ctx context.Context, hash common.Hash, index hexutil.Uint) (map[string]interface{}, error)
|
2020-10-24 17:03:52 +00:00
|
|
|
GetUncleCountByBlockNumber(ctx context.Context, number rpc.BlockNumber) (*hexutil.Uint, error)
|
2020-10-10 12:24:56 +00:00
|
|
|
GetUncleCountByBlockHash(ctx context.Context, hash common.Hash) (*hexutil.Uint, error)
|
2020-10-12 08:39:33 +00:00
|
|
|
|
2020-10-14 15:59:42 +00:00
|
|
|
// Filter related (see ./eth_filters.go)
|
2020-10-12 08:39:33 +00:00
|
|
|
// newPendingTransactionFilter(ctx context.Context) (string, error)
|
|
|
|
// newBlockFilter(ctx context.Context) (string, error)
|
|
|
|
// newFilter(ctx context.Context) (string, error)
|
|
|
|
// uninstallFilter(ctx context.Context) (string, error)
|
|
|
|
// getFilterChanges(ctx context.Context) (string, error)
|
|
|
|
|
2020-10-14 15:59:42 +00:00
|
|
|
// Account related (see ./eth_accounts.go)
|
2020-10-24 17:03:52 +00:00
|
|
|
Accounts(ctx context.Context) ([]common.Address, error)
|
2020-10-12 08:39:33 +00:00
|
|
|
GetBalance(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Big, error)
|
|
|
|
GetTransactionCount(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Uint64, error)
|
|
|
|
GetStorageAt(ctx context.Context, address common.Address, index string, blockNrOrHash rpc.BlockNumberOrHash) (string, error)
|
|
|
|
GetCode(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error)
|
|
|
|
|
2020-10-14 15:59:42 +00:00
|
|
|
// System related (see ./eth_system.go)
|
2020-10-12 08:39:33 +00:00
|
|
|
BlockNumber(ctx context.Context) (hexutil.Uint64, error)
|
|
|
|
Syncing(ctx context.Context) (interface{}, error)
|
2020-10-24 17:03:52 +00:00
|
|
|
ChainId(ctx context.Context) (hexutil.Uint64, error) /* called eth_protocolVersion elsewhere */
|
|
|
|
ProtocolVersion(_ context.Context) (hexutil.Uint, error)
|
2020-10-20 21:16:28 +00:00
|
|
|
GasPrice(_ context.Context) (*hexutil.Big, error)
|
2020-10-12 08:39:33 +00:00
|
|
|
|
2020-10-14 15:59:42 +00:00
|
|
|
// Sending related (see ./eth_call.go)
|
2020-10-12 08:39:33 +00:00
|
|
|
Call(ctx context.Context, args ethapi.CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *map[common.Address]ethapi.Account) (hexutil.Bytes, error)
|
|
|
|
EstimateGas(ctx context.Context, args ethapi.CallArgs) (hexutil.Uint64, error)
|
|
|
|
SendRawTransaction(ctx context.Context, encodedTx hexutil.Bytes) (common.Hash, error)
|
|
|
|
// SendTransaction(ctx context.Context) (string, error)
|
2020-10-24 17:03:52 +00:00
|
|
|
Sign(ctx context.Context, _ common.Address, _ hexutil.Bytes) (hexutil.Bytes, error)
|
2020-10-12 08:39:33 +00:00
|
|
|
|
2020-10-14 15:59:42 +00:00
|
|
|
// Mining related (see ./eth_mining.go)
|
|
|
|
Coinbase(_ context.Context) (common.Address, error)
|
|
|
|
Hashrate(_ context.Context) (uint64, error)
|
|
|
|
Mining(_ context.Context) (bool, error)
|
|
|
|
GetWork(_ context.Context) ([]interface{}, error)
|
|
|
|
SubmitWork(_ context.Context, nonce rpc.BlockNumber, powHash, digest common.Hash) (bool, error)
|
|
|
|
SubmitHashrate(_ context.Context, hashRate common.Hash, id string) (bool, error)
|
2020-10-12 08:39:33 +00:00
|
|
|
|
|
|
|
// Deprecated commands in eth_ (proposed file: ./eth_deprecated.go)
|
2020-10-24 17:03:52 +00:00
|
|
|
GetCompilers(_ context.Context) ([]string, error)
|
|
|
|
CompileLLL(_ context.Context, _ string) (hexutil.Bytes, error)
|
|
|
|
CompileSolidity(ctx context.Context, _ string) (hexutil.Bytes, error)
|
|
|
|
CompileSerpent(ctx context.Context, _ string) (hexutil.Bytes, error)
|
2020-08-01 07:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// APIImpl is implementation of the EthAPI interface based on remote Db access
|
|
|
|
type APIImpl struct {
|
|
|
|
db ethdb.KV
|
2020-08-12 13:47:59 +00:00
|
|
|
ethBackend ethdb.Backend
|
2020-09-28 17:18:36 +00:00
|
|
|
dbReader ethdb.Database
|
2020-08-01 07:39:04 +00:00
|
|
|
chainContext core.ChainContext
|
2020-08-19 11:46:20 +00:00
|
|
|
GasCap uint64
|
2020-08-01 07:39:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 08:39:33 +00:00
|
|
|
// NewEthAPI returns APIImpl instance
|
|
|
|
func NewEthAPI(db ethdb.KV, dbReader ethdb.Database, eth ethdb.Backend, gascap uint64) *APIImpl {
|
2020-08-01 07:39:04 +00:00
|
|
|
return &APIImpl{
|
2020-10-03 08:07:49 +00:00
|
|
|
db: db,
|
|
|
|
dbReader: dbReader,
|
|
|
|
ethBackend: eth,
|
|
|
|
GasCap: gascap,
|
2020-08-01 07:39:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 19:39:31 +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"`
|
2020-09-15 16:44:28 +00:00
|
|
|
R *hexutil.Big `json:"r"`
|
|
|
|
S *hexutil.Big `json:"s"`
|
2020-09-08 19:39:31 +00:00
|
|
|
To *common.Address `json:"to"`
|
|
|
|
TransactionIndex *hexutil.Uint64 `json:"transactionIndex"`
|
|
|
|
V *hexutil.Big `json:"v"`
|
2020-09-15 16:44:28 +00:00
|
|
|
Value *hexutil.Big `json:"value"`
|
2020-09-08 19:39:31 +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.Transaction, blockHash common.Hash, blockNumber uint64, index uint64) *RPCTransaction {
|
|
|
|
var signer types.Signer = types.FrontierSigner{}
|
|
|
|
if tx.Protected() {
|
|
|
|
signer = types.NewEIP155Signer(tx.ChainID().ToBig())
|
|
|
|
}
|
|
|
|
from, _ := types.Sender(signer, tx)
|
|
|
|
v, r, s := tx.RawSignatureValues()
|
2020-09-03 20:41:06 +00:00
|
|
|
|
2020-09-08 19:39:31 +00:00
|
|
|
result := &RPCTransaction{
|
|
|
|
From: from,
|
|
|
|
Gas: hexutil.Uint64(tx.Gas()),
|
|
|
|
GasPrice: (*hexutil.Big)(tx.GasPrice().ToBig()),
|
|
|
|
Hash: tx.Hash(),
|
|
|
|
Input: hexutil.Bytes(tx.Data()),
|
|
|
|
Nonce: hexutil.Uint64(tx.Nonce()),
|
|
|
|
R: (*hexutil.Big)(r.ToBig()),
|
|
|
|
S: (*hexutil.Big)(s.ToBig()),
|
2020-09-15 16:44:28 +00:00
|
|
|
To: tx.To(),
|
|
|
|
V: (*hexutil.Big)(v.ToBig()),
|
|
|
|
Value: (*hexutil.Big)(tx.Value().ToBig()),
|
2020-09-08 19:39:31 +00:00
|
|
|
}
|
|
|
|
if blockHash != (common.Hash{}) {
|
|
|
|
result.BlockHash = &blockHash
|
|
|
|
result.BlockNumber = (*hexutil.Big)(new(big.Int).SetUint64(blockNumber))
|
|
|
|
result.TransactionIndex = (*hexutil.Uint64)(&index)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|