2020-10-12 14:17:34 +00:00
|
|
|
package commands
|
|
|
|
|
2020-10-14 15:59:42 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/hexutil"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/eth"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/eth/stagedsync/stages"
|
2020-10-25 08:38:55 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
2020-10-14 15:59:42 +00:00
|
|
|
)
|
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// BlockNumber implements eth_blockNumber. Returns the block number of most recent block.
|
2020-10-16 12:15:10 +00:00
|
|
|
func (api *APIImpl) BlockNumber(_ context.Context) (hexutil.Uint64, error) {
|
2020-10-14 15:59:42 +00:00
|
|
|
execution, _, err := stages.GetStageProgress(api.dbReader, stages.Finish)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return hexutil.Uint64(execution), nil
|
|
|
|
}
|
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// Syncing implements eth_syncing. Returns a data object detaling the status of the sync process or false if not syncing.
|
2020-10-16 12:15:10 +00:00
|
|
|
func (api *APIImpl) Syncing(_ context.Context) (interface{}, error) {
|
2020-10-14 15:59:42 +00:00
|
|
|
highestBlock, _, err := stages.GetStageProgress(api.dbReader, stages.Headers)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
currentBlock, _, err := stages.GetStageProgress(api.dbReader, stages.Finish)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return not syncing if the synchronisation already completed
|
|
|
|
if currentBlock >= highestBlock {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
// Otherwise gather the block sync stats
|
|
|
|
return map[string]hexutil.Uint64{
|
|
|
|
"currentBlock": hexutil.Uint64(currentBlock),
|
|
|
|
"highestBlock": hexutil.Uint64(highestBlock),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// ChainId implements eth_chainId. Returns the current ethereum chainId.
|
2020-10-14 15:59:42 +00:00
|
|
|
func (api *APIImpl) ChainId(ctx context.Context) (hexutil.Uint64, error) {
|
2020-10-25 08:38:55 +00:00
|
|
|
tx, err := api.dbReader.Begin(ctx, ethdb.RO)
|
2020-10-14 15:59:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2020-10-24 06:57:09 +00:00
|
|
|
chainConfig, err := getChainConfig(tx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-10-14 15:59:42 +00:00
|
|
|
return hexutil.Uint64(chainConfig.ChainID.Uint64()), nil
|
|
|
|
}
|
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// ProtocolVersion implements eth_protocolVersion. Returns the current ethereum protocol version.
|
2020-10-14 15:59:42 +00:00
|
|
|
func (api *APIImpl) ProtocolVersion(_ context.Context) (hexutil.Uint, error) {
|
|
|
|
return hexutil.Uint(eth.ProtocolVersions[0]), nil
|
|
|
|
}
|
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// GasPrice implements eth_gasPrice. Returns the current price per gas in wei.
|
2020-10-16 12:15:10 +00:00
|
|
|
func (api *APIImpl) GasPrice(_ context.Context) (*hexutil.Big, error) {
|
2020-10-14 15:59:42 +00:00
|
|
|
return nil, fmt.Errorf(NotImplemented, "eth_getPrice")
|
|
|
|
// price, err := eth.SuggestPrice(ctx)
|
|
|
|
// return (*hexutil.Big)(price), err
|
|
|
|
}
|