erigon-pulse/cmd/rpcdaemon/commands/daemon.go
Alex Sharov 1dcc2b141a
Rpcdaemon as lib (#940)
* share config object

* create default config and logger

* move db connection to common func

* move server start to cli package

* clear

* clear

* rename cli to rpc

* use unified SetupLogger func

* make all root flag persistent

* use common flags in different packages

* use common flags in different packages

* move TraceTx method to eth package

* use native slice flags

* create package "turbo"

* disable geth api

* disable geth api

* move more data types to turbo/adapter package

* add support for customApiList

* run more

* run more

* run more

* dog-food

* move DoCall

* move DoCall

* fix tests

* fix test
2020-08-19 12:46:20 +01:00

76 lines
2.1 KiB
Go

package commands
import (
"context"
"fmt"
"github.com/ledgerwatch/turbo-geth/cmd/rpcdaemon/cli"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
"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) {
additionalFields := make(map[string]interface{})
block := rawdb.ReadBlockByNumber(api.dbReader, uint64(number.Int64()))
if block == nil {
return nil, fmt.Errorf("block not found: %d", number.Int64())
}
additionalFields["totalDifficulty"] = rawdb.ReadTd(api.dbReader, block.Hash(), uint64(number.Int64()))
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
}
func APIList(db ethdb.KV, eth ethdb.Backend, cfg cli.Flags, customApiList []rpc.API) []rpc.API {
var rpcAPI []rpc.API
dbReader := ethdb.NewObjectDatabase(db)
apiImpl := NewAPI(db, dbReader, eth, cfg.Gascap)
netImpl := NewNetAPIImpl(eth)
dbgAPIImpl := NewPrivateDebugAPI(db, dbReader)
for _, enabledAPI := range cfg.API {
switch enabledAPI {
case "eth":
rpcAPI = append(rpcAPI, rpc.API{
Namespace: "eth",
Public: true,
Service: EthAPI(apiImpl),
Version: "1.0",
})
case "debug":
rpcAPI = append(rpcAPI, rpc.API{
Namespace: "debug",
Public: true,
Service: PrivateDebugAPI(dbgAPIImpl),
Version: "1.0",
})
case "net":
rpcAPI = append(rpcAPI, rpc.API{
Namespace: "net",
Public: true,
Service: NetAPI(netImpl),
Version: "1.0",
})
default:
// TODO: enable validation after checking customApiList
//log.Error("Unrecognised", "api", enabledAPI)
}
}
return append(rpcAPI, customApiList...)
}