erigon-pulse/cmd/rpcdaemon/commands/daemon.go
Alex Sharov fc3cd4d5c3
Rpcdaemon: private api V2 (#1214)
* save progress

* GracefulShutdown grpc server, send to server close signal before canceling context

* GracefulShutdown json server

* GracefulShutdown json server

* fix lint

* clean

* hack hugeFreelist

* up streams limit

* up streams limit

* up streams limit

* up streams limit

* up streams limit

* up streams limit

* up streams limit

* up streams limit

* up streams limit

* save progress

* fix_race_condition_on_zstd_build

* fix_race_condition_on_zstd_build

* better close cursor

* save progress

* open read tx in all api methods

* clean

* clean
2020-10-10 13:24:56 +01:00

162 lines
4.6 KiB
Go

package commands
import (
"context"
"fmt"
"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"
"github.com/ledgerwatch/turbo-geth/turbo/adapter/ethapi"
)
// GetBlockByNumber see https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getblockbynumber
// see internal/ethapi.PublicBlockChainAPI.GetBlockByNumber
func (api *APIImpl) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) {
tx, err := api.dbReader.Begin(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()
blockNum, err := getBlockNumber(number, tx)
if err != nil {
return nil, err
}
additionalFields := make(map[string]interface{})
block := rawdb.ReadBlockByNumber(tx, blockNum)
if block == nil {
return nil, fmt.Errorf("block not found: %d", blockNum)
}
additionalFields["totalDifficulty"] = (*hexutil.Big)(rawdb.ReadTd(tx, block.Hash(), blockNum))
response, err := ethapi.RPCMarshalBlock(block, true, fullTx, additionalFields)
if err == nil && number == rpc.PendingBlockNumber {
// Pending blocks need to nil out a few fields
for _, field := range []string{"hash", "nonce", "miner"} {
response[field] = nil
}
}
return response, err
}
// GetBlockByHash see https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getblockbyhash
// see internal/ethapi.PublicBlockChainAPI.GetBlockByHash
func (api *APIImpl) GetBlockByHash(ctx context.Context, hash common.Hash, fullTx bool) (map[string]interface{}, error) {
tx, err := api.dbReader.Begin(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()
additionalFields := make(map[string]interface{})
block := rawdb.ReadBlockByHash(tx, hash)
if block == nil {
return nil, fmt.Errorf("block not found: %x", hash)
}
number := block.NumberU64()
additionalFields["totalDifficulty"] = (*hexutil.Big)(rawdb.ReadTd(tx, hash, number))
response, err := ethapi.RPCMarshalBlock(block, true, fullTx, additionalFields)
if err == nil && int64(number) == rpc.PendingBlockNumber.Int64() {
// Pending blocks need to nil out a few fields
for _, field := range []string{"hash", "nonce", "miner"} {
response[field] = nil
}
}
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
}
func APIList(db ethdb.KV, eth ethdb.Backend, cfg cli.Flags, customApiList []rpc.API) []rpc.API {
var defaultAPIList []rpc.API
dbReader := ethdb.NewObjectDatabase(db)
apiImpl := NewAPI(db, dbReader, eth, cfg.Gascap)
netImpl := NewNetAPIImpl(eth)
dbgAPIImpl := NewPrivateDebugAPI(db, dbReader)
traceAPIImpl := NewTraceAPI(db, dbReader, &cfg)
web3Impl := NewWeb3APIImpl()
for _, enabledAPI := range cfg.API {
switch enabledAPI {
case "eth":
defaultAPIList = append(defaultAPIList, rpc.API{
Namespace: "eth",
Public: true,
Service: EthAPI(apiImpl),
Version: "1.0",
})
case "debug":
defaultAPIList = append(defaultAPIList, rpc.API{
Namespace: "debug",
Public: true,
Service: PrivateDebugAPI(dbgAPIImpl),
Version: "1.0",
})
case "net":
defaultAPIList = append(defaultAPIList, rpc.API{
Namespace: "net",
Public: true,
Service: NetAPI(netImpl),
Version: "1.0",
})
case "web3":
defaultAPIList = append(defaultAPIList, rpc.API{
Namespace: "web3",
Public: true,
Service: Web3API(web3Impl),
Version: "1.0",
})
case "trace":
defaultAPIList = append(defaultAPIList, rpc.API{
Namespace: "trace",
Public: true,
Service: TraceAPI(traceAPIImpl),
Version: "1.0",
})
}
}
return append(defaultAPIList, customApiList...)
}