erigon-pulse/cmd/rpcdaemon/commands/daemon.go

97 lines
3.0 KiB
Go
Raw Normal View History

package commands
import (
"context"
"fmt"
"github.com/ledgerwatch/turbo-geth/cmd/rpcdaemon/cli"
"github.com/ledgerwatch/turbo-geth/common"
"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
}
// 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) {
additionalFields := make(map[string]interface{})
block := rawdb.ReadBlockByHash(api.dbReader, hash)
if block == nil {
return nil, fmt.Errorf("block not found: %x", hash)
}
number := block.NumberU64()
additionalFields["totalDifficulty"] = rawdb.ReadTd(api.dbReader, 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
}
func APIList(db ethdb.KV, eth ethdb.Backend, cfg cli.Flags, customApiList []rpc.API) []rpc.API {
2020-08-20 03:52:27 +00:00
var defaultAPIList []rpc.API
dbReader := ethdb.NewObjectDatabase(db)
apiImpl := NewAPI(db, dbReader, eth, cfg.Gascap)
2020-08-17 15:27:29 +00:00
netImpl := NewNetAPIImpl(eth)
dbgAPIImpl := NewPrivateDebugAPI(db, dbReader)
2019-12-16 14:54:30 +00:00
for _, enabledAPI := range cfg.API {
switch enabledAPI {
case "eth":
2020-08-20 03:52:27 +00:00
defaultAPIList = append(defaultAPIList, rpc.API{
Namespace: "eth",
Public: true,
Service: EthAPI(apiImpl),
Version: "1.0",
})
case "debug":
2020-08-20 03:52:27 +00:00
defaultAPIList = append(defaultAPIList, rpc.API{
Namespace: "debug",
Public: true,
2019-12-16 14:27:49 +00:00
Service: PrivateDebugAPI(dbgAPIImpl),
Version: "1.0",
})
2020-08-17 15:27:29 +00:00
case "net":
2020-08-20 03:52:27 +00:00
defaultAPIList = append(defaultAPIList, rpc.API{
2020-08-17 15:27:29 +00:00
Namespace: "net",
Public: true,
Service: NetAPI(netImpl),
Version: "1.0",
})
}
}
2020-08-20 03:52:27 +00:00
return append(defaultAPIList, customApiList...)
}