2019-12-02 13:47:00 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-10-02 12:52:02 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/params"
|
2020-08-22 20:13:38 +00:00
|
|
|
|
2020-08-19 11:46:20 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/cmd/rpcdaemon/cli"
|
2020-08-22 20:13:38 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
2020-08-30 03:55:31 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common/hexutil"
|
2019-12-10 05:37:18 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/core/rawdb"
|
2020-09-03 07:51:19 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types"
|
2019-12-10 05:37:18 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
2019-12-02 13:47:00 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/rpc"
|
2020-08-19 11:46:20 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/turbo/adapter/ethapi"
|
2019-12-02 13:47:00 +00:00
|
|
|
)
|
|
|
|
|
2019-12-01 09:52:11 +00:00
|
|
|
// 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) {
|
2020-09-03 07:51:19 +00:00
|
|
|
blockNum, err := getBlockNumber(number, api.dbReader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-08-29 21:28:09 +00:00
|
|
|
}
|
2019-12-02 03:19:02 +00:00
|
|
|
additionalFields := make(map[string]interface{})
|
2019-12-01 09:52:11 +00:00
|
|
|
|
2020-08-29 21:28:09 +00:00
|
|
|
block := rawdb.ReadBlockByNumber(api.dbReader, blockNum)
|
2020-07-21 08:19:04 +00:00
|
|
|
if block == nil {
|
2020-08-29 21:28:09 +00:00
|
|
|
return nil, fmt.Errorf("block not found: %d", blockNum)
|
2019-12-01 09:52:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-30 03:55:31 +00:00
|
|
|
additionalFields["totalDifficulty"] = (*hexutil.Big)(rawdb.ReadTd(api.dbReader, block.Hash(), blockNum))
|
2020-08-19 11:46:20 +00:00
|
|
|
response, err := ethapi.RPCMarshalBlock(block, true, fullTx, additionalFields)
|
2019-12-02 03:19:02 +00:00
|
|
|
|
2020-07-21 08:19:04 +00:00
|
|
|
if err == nil && number == rpc.PendingBlockNumber {
|
|
|
|
// Pending blocks need to nil out a few fields
|
|
|
|
for _, field := range []string{"hash", "nonce", "miner"} {
|
2020-08-22 20:13:38 +00:00
|
|
|
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()
|
|
|
|
|
2020-08-30 03:55:31 +00:00
|
|
|
additionalFields["totalDifficulty"] = (*hexutil.Big)(rawdb.ReadTd(api.dbReader, hash, number))
|
2020-08-22 20:13:38 +00:00
|
|
|
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"} {
|
2020-07-21 08:19:04 +00:00
|
|
|
response[field] = nil
|
2019-12-01 09:52:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-21 08:19:04 +00:00
|
|
|
return response, err
|
2019-12-01 09:52:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 06:59:07 +00:00
|
|
|
// GetHeaderByNumber returns a block's header by number
|
2020-09-03 07:51:19 +00:00
|
|
|
func (api *APIImpl) GetHeaderByNumber(_ context.Context, number rpc.BlockNumber) (*types.Header, error) {
|
|
|
|
header := rawdb.ReadHeaderByNumber(api.dbReader, uint64(number.Int64()))
|
|
|
|
if header == nil {
|
|
|
|
return nil, fmt.Errorf("block header not found: %d", number.Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
return header, nil
|
|
|
|
}
|
|
|
|
|
2020-09-14 06:59:07 +00:00
|
|
|
// GetHeaderByHash returns a block's header by hash
|
2020-09-03 07:51:19 +00:00
|
|
|
func (api *APIImpl) GetHeaderByHash(_ context.Context, hash common.Hash) (*types.Header, error) {
|
|
|
|
header := rawdb.ReadHeaderByHash(api.dbReader, hash)
|
|
|
|
if header == nil {
|
|
|
|
return nil, fmt.Errorf("block header not found: %s", hash.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return header, nil
|
|
|
|
}
|
|
|
|
|
2020-10-02 12:52:02 +00:00
|
|
|
func getChainConfig(db rawdb.DatabaseReader) *params.ChainConfig {
|
|
|
|
genesisHash := rawdb.ReadBlockByNumber(db, 0).Hash()
|
|
|
|
return rawdb.ReadChainConfig(db, genesisHash)
|
|
|
|
}
|
|
|
|
|
2020-08-19 11:46:20 +00:00
|
|
|
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
|
2020-04-04 07:18:10 +00:00
|
|
|
|
2020-07-22 01:31:47 +00:00
|
|
|
dbReader := ethdb.NewObjectDatabase(db)
|
2020-10-02 12:52:02 +00:00
|
|
|
|
|
|
|
chainConfig := getChainConfig(dbReader)
|
|
|
|
|
|
|
|
apiImpl := NewAPI(db, dbReader, eth, cfg.Gascap, chainConfig)
|
2020-08-17 15:27:29 +00:00
|
|
|
netImpl := NewNetAPIImpl(eth)
|
2020-10-02 12:52:02 +00:00
|
|
|
dbgAPIImpl := NewPrivateDebugAPI(db, dbReader, chainConfig)
|
|
|
|
traceAPIImpl := NewTraceAPI(db, dbReader, &cfg, chainConfig)
|
2020-09-11 13:12:38 +00:00
|
|
|
web3Impl := NewWeb3APIImpl()
|
2019-12-16 14:54:30 +00:00
|
|
|
|
2020-08-19 11:46:20 +00:00
|
|
|
for _, enabledAPI := range cfg.API {
|
2019-12-02 13:47:00 +00:00
|
|
|
switch enabledAPI {
|
|
|
|
case "eth":
|
2020-08-20 03:52:27 +00:00
|
|
|
defaultAPIList = append(defaultAPIList, rpc.API{
|
2019-12-02 13:47:00 +00:00
|
|
|
Namespace: "eth",
|
|
|
|
Public: true,
|
2019-12-10 05:37:18 +00:00
|
|
|
Service: EthAPI(apiImpl),
|
2019-12-02 13:47:00 +00:00
|
|
|
Version: "1.0",
|
|
|
|
})
|
2019-12-10 05:37:18 +00:00
|
|
|
case "debug":
|
2020-08-20 03:52:27 +00:00
|
|
|
defaultAPIList = append(defaultAPIList, rpc.API{
|
2019-12-10 05:37:18 +00:00
|
|
|
Namespace: "debug",
|
|
|
|
Public: true,
|
2019-12-16 14:27:49 +00:00
|
|
|
Service: PrivateDebugAPI(dbgAPIImpl),
|
2019-12-10 05:37:18 +00:00
|
|
|
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-09-11 13:12:38 +00:00
|
|
|
case "web3":
|
|
|
|
defaultAPIList = append(defaultAPIList, rpc.API{
|
|
|
|
Namespace: "web3",
|
|
|
|
Public: true,
|
|
|
|
Service: Web3API(web3Impl),
|
|
|
|
Version: "1.0",
|
|
|
|
})
|
2020-08-29 15:50:24 +00:00
|
|
|
case "trace":
|
|
|
|
defaultAPIList = append(defaultAPIList, rpc.API{
|
|
|
|
Namespace: "trace",
|
|
|
|
Public: true,
|
|
|
|
Service: TraceAPI(traceAPIImpl),
|
|
|
|
Version: "1.0",
|
|
|
|
})
|
2019-12-02 13:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 03:52:27 +00:00
|
|
|
return append(defaultAPIList, customApiList...)
|
2019-12-02 13:47:00 +00:00
|
|
|
}
|