Cleans up some custom RPC commands (#1270)

* Moving a few files to be more consistent

* Collecting together custom code under tg_ namespace

* Small cleanups

* Updating test cases

* Uncomment eth_gasPrice but still unimplemented
This commit is contained in:
Thomas Jay Rush 2020-10-20 17:16:28 -04:00 committed by GitHub
parent 9e7ed07fc6
commit 2aa6fb50ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 1001 additions and 141 deletions

View File

@ -57,7 +57,7 @@ type EthAPI interface {
ChainId(ctx context.Context) (hexutil.Uint64, error) /* called eth_protocolVersion elsewhere */
BlockNumber(ctx context.Context) (hexutil.Uint64, error)
Syncing(ctx context.Context) (interface{}, error)
// GasPrice(_ context.Context) (*hexutil.Big, error)
GasPrice(_ context.Context) (*hexutil.Big, error)
// Sending related (see ./eth_call.go)
Call(ctx context.Context, args ethapi.CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *map[common.Address]ethapi.Account) (hexutil.Bytes, error)

View File

@ -7,7 +7,6 @@ import (
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/hexutil"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
"github.com/ledgerwatch/turbo-geth/core/types"
"github.com/ledgerwatch/turbo-geth/rpc"
"github.com/ledgerwatch/turbo-geth/turbo/adapter/ethapi"
)
@ -73,38 +72,6 @@ func (api *APIImpl) GetBlockByHash(ctx context.Context, hash common.Hash, fullTx
return response, err
}
// GetHeaderByNumber returns a block's header by number
func (api *APIImpl) GetHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
tx, err := api.dbReader.Begin(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()
header := rawdb.ReadHeaderByNumber(tx, uint64(number.Int64()))
if header == nil {
return nil, fmt.Errorf("block header not found: %d", number.Int64())
}
return header, nil
}
// GetHeaderByHash returns a block's header by hash
func (api *APIImpl) GetHeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
tx, err := api.dbReader.Begin(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()
header := rawdb.ReadHeaderByHash(tx, hash)
if header == nil {
return nil, fmt.Errorf("block header not found: %s", hash.String())
}
return header, nil
}
// GetBlockTransactionCountByNumber returns the number of transactions in the block
func (api *APIImpl) GetBlockTransactionCountByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*hexutil.Uint, error) {
tx, err := api.dbReader.Begin(ctx)

View File

@ -14,11 +14,16 @@ type TgAPI interface {
Forks(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (Forks, error)
// Blocks related (see ./tg_blocks.go)
GetHeaderByNumber(_ context.Context, number rpc.BlockNumber) (*types.Header, error)
GetHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
GetHeaderByHash(_ context.Context, hash common.Hash) (*types.Header, error)
// Receipt related (see ./tg_receipts.go)
GetLogsByHash(ctx context.Context, hash common.Hash) ([][]*types.Log, error)
// Issuance / reward related (see ./tg_issuance.go)
BlockReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error)
UncleReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error)
Issuance(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error)
}
// TgImpl is implementation of the TgAPI interface

View File

@ -11,16 +11,16 @@ import (
)
// GetHeaderByNumber returns a block's header by number
func (api *TgImpl) GetHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
func (api *TgImpl) GetHeaderByNumber(ctx context.Context, blockNumber rpc.BlockNumber) (*types.Header, error) {
tx, err := api.db.Begin(ctx, nil, false)
if err != nil {
return nil, err
}
defer tx.Rollback()
header := rawdb.ReadHeaderByNumber(tx, uint64(number.Int64()))
header := rawdb.ReadHeaderByNumber(tx, uint64(blockNumber.Int64()))
if header == nil {
return nil, fmt.Errorf("block header not found: %d", number.Int64())
return nil, fmt.Errorf("block header not found: %d", blockNumber.Int64())
}
return header, nil

View File

@ -3,14 +3,16 @@ package commands
import (
"context"
"fmt"
"github.com/ledgerwatch/turbo-geth/common/hexutil"
"github.com/ledgerwatch/turbo-geth/consensus/ethash"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
"github.com/ledgerwatch/turbo-geth/core/types"
"github.com/ledgerwatch/turbo-geth/rpc"
)
// BlockReward returns the block reward for this block
func (api *TraceAPIImpl) BlockReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
func (api *TgImpl) BlockReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
tx, err := api.dbReader.Begin(ctx)
if err != nil {
return Issuance{}, err
@ -21,7 +23,7 @@ func (api *TraceAPIImpl) BlockReward(ctx context.Context, blockNr rpc.BlockNumbe
}
// UncleReward returns the uncle reward for this block
func (api *TraceAPIImpl) UncleReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
func (api *TgImpl) UncleReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
tx, err := api.dbReader.Begin(ctx)
if err != nil {
return Issuance{}, err
@ -32,7 +34,7 @@ func (api *TraceAPIImpl) UncleReward(ctx context.Context, blockNr rpc.BlockNumbe
}
// Issuance returns the issuance for this block
func (api *TraceAPIImpl) Issuance(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
func (api *TgImpl) Issuance(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
tx, err := api.dbReader.Begin(ctx)
if err != nil {
return Issuance{}, err
@ -42,7 +44,7 @@ func (api *TraceAPIImpl) Issuance(ctx context.Context, blockNr rpc.BlockNumber)
return api.rewardCalc(tx, blockNr, "issuance")
}
func (api *TraceAPIImpl) rewardCalc(db rawdb.DatabaseReader, blockNr rpc.BlockNumber, which string) (Issuance, error) {
func (api *TgImpl) rewardCalc(db rawdb.DatabaseReader, blockNr rpc.BlockNumber, which string) (Issuance, error) {
genesisHash := rawdb.ReadBlockByNumber(db, 0).Hash()
chainConfig := rawdb.ReadChainConfig(db, genesisHash)
if chainConfig.Ethash == nil {
@ -80,6 +82,14 @@ func (api *TraceAPIImpl) rewardCalc(db rawdb.DatabaseReader, blockNr rpc.BlockNu
return Issuance{}, fmt.Errorf("should not happen in rewardCalc")
}
func (api *TgImpl) getBlockByRPCNumber(db rawdb.DatabaseReader, blockNr rpc.BlockNumber) (*types.Block, error) {
blockNum, err := getBlockNumber(blockNr, db)
if err != nil {
return nil, err
}
return rawdb.ReadBlockByNumber(db, blockNum), nil
}
// Issuance structure to return information about issuance
type Issuance struct {
BlockReward string `json:"blockReward,omitempty"`

View File

@ -15,7 +15,7 @@ type Forks struct {
Next *uint64 `json:"next,omitempty"`
}
// returns forkID hash, sorted list of already passed forks and next fork block
// Forks returns forkID hash, sorted list of already passed forks and next fork block
func (api *TgImpl) Forks(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (Forks, error) {
blockNumber, _, err := rpchelper.GetBlockNumber(blockNrOrHash, api.dbReader)
if err != nil {

View File

@ -6,31 +6,24 @@ import (
"github.com/ledgerwatch/turbo-geth/cmd/rpcdaemon/cli"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/hexutil"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
"github.com/ledgerwatch/turbo-geth/core/types"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/rpc"
)
// TraceAPI RPC interface into tracing API
type TraceAPI interface {
// Ad-hoc
// Ad-hoc (see ./trace_adhoc.go)
ReplayBlockTransactions(ctx context.Context, blockNr rpc.BlockNumber, traceTypes []string) ([]interface{}, error)
ReplayTransaction(ctx context.Context, txHash common.Hash, traceTypes []string) ([]interface{}, error)
Call(ctx context.Context, call CallParam, blockNr rpc.BlockNumber) ([]interface{}, error)
CallMany(ctx context.Context, calls CallParams) ([]interface{}, error)
RawTransaction(ctx context.Context, txHash common.Hash, traceTypes []string) ([]interface{}, error)
// Filtering
// Filtering (see ./trace_filtering.go)
Transaction(ctx context.Context, txHash common.Hash) (ParityTraces, error)
Get(ctx context.Context, txHash common.Hash, txIndicies []hexutil.Uint64) (*ParityTrace, error)
Block(ctx context.Context, blockNr rpc.BlockNumber) (ParityTraces, error)
Filter(ctx context.Context, req TraceFilterRequest) (ParityTraces, error)
// Custom (turbo geth exclusive)
BlockReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error)
UncleReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error)
Issuance(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error)
}
// TraceAPIImpl is implementation of the TraceAPI interface based on remote Db access
@ -50,11 +43,3 @@ func NewTraceAPI(db ethdb.KV, dbReader ethdb.Database, cfg *cli.Flags) *TraceAPI
traceType: cfg.TraceType,
}
}
func (api *TraceAPIImpl) getBlockByRPCNumber(db rawdb.DatabaseReader, blockNr rpc.BlockNumber) (*types.Block, error) {
blockNum, err := getBlockNumber(blockNr, db)
if err != nil {
return nil, err
}
return rawdb.ReadBlockByNumber(db, blockNum), nil
}

File diff suppressed because it is too large Load Diff