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

151 lines
4.2 KiB
Go
Raw Normal View History

package commands
import (
"context"
"fmt"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/eth/gasprice"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/ethdb"
"github.com/ledgerwatch/erigon/log"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/rpc"
)
// BlockNumber implements eth_blockNumber. Returns the block number of most recent block.
2021-03-30 09:53:54 +00:00
func (api *APIImpl) BlockNumber(ctx context.Context) (hexutil.Uint64, error) {
2021-04-03 06:26:00 +00:00
tx, err := api.db.BeginRo(ctx)
2021-03-30 09:53:54 +00:00
if err != nil {
return 0, err
}
defer tx.Rollback()
2021-04-08 11:03:45 +00:00
execution, err := stages.GetStageProgress(tx, stages.Finish)
if err != nil {
return 0, err
}
return hexutil.Uint64(execution), nil
}
// Syncing implements eth_syncing. Returns a data object detaling the status of the sync process or false if not syncing.
func (api *APIImpl) Syncing(ctx context.Context) (interface{}, error) {
2021-04-03 06:26:00 +00:00
tx, err := api.db.BeginRo(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()
2021-04-08 11:03:45 +00:00
highestBlock, err := stages.GetStageProgress(tx, stages.Headers)
if err != nil {
return false, err
}
2021-04-08 11:03:45 +00:00
currentBlock, err := stages.GetStageProgress(tx, 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
}
// ChainId implements eth_chainId. Returns the current ethereum chainId.
func (api *APIImpl) ChainId(ctx context.Context) (hexutil.Uint64, error) {
2021-04-03 06:26:00 +00:00
tx, err := api.db.BeginRo(ctx)
if err != nil {
return 0, err
}
defer tx.Rollback()
chainConfig, err := api.chainConfig(tx)
if err != nil {
return 0, err
}
return hexutil.Uint64(chainConfig.ChainID.Uint64()), nil
}
// ProtocolVersion implements eth_protocolVersion. Returns the current ethereum protocol version.
Integration tests 1 (#1793) * Initial commit * Add sentry gRPC interface * p2psentry directory * Update README.md * Update README.md * Update README.md * Add go package * Correct syntax * add external downloader interface (#2) * Add txpool (#3) * Add private API (#4) * Invert control.proto, add PeerMinBlock, Separare incoming Tx message into a separate stream (#5) Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local> * Separate upload messages into its own stream (#6) Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local> * Only send changed accounts to listeners (#7) * Txpool interface doc (#9) * Add architecture diagram source and picture (#10) * Typed hashes (#11) * Typed hashes * Fix PeerId * 64-bit tx nonce * Add proper golang packages, max_block into p2p sentry Status (#12) * Add proper golang packages, max_block into p2p sentry Status * Change EtherReply to address Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local> * Add Rust infrastructure (#13) * DB stats methods removed by https://github.com/ledgerwatch/turbo-geth/pull/1665 * more p2p methods (#15) * add mining methods (#16) * First draft of Consensus gRPC interface (#14) * Update Rust build * Fix interfaces in architecture diagram (#17) * Fix KV interface provider * Fix Consensus interface provider * drop java attributes (#18) * tx pool remove unused import (#19) * ethbackend: add protocol version and client version (#20) * Add missing ethbackend I/F (#21) * Add interface versioning mechanism (#23) Add versioning in KV interface Co-authored-by: Artem Vorotnikov <artem@vorotnikov.me> * spec of tx pool method (#24) * spec of tx pool method (#25) * Update version.proto * Refactor interface versioning * Refactor interface versioning * Testing interface * Remove tree * Fix * Build testing protos * Fix * Fix * Update to the newer interfaces * Add ProtocolVersion and ClientVersion stubs * Hook up ProtocolVersion and ClientVersion * Remove service * Add compatibility checks for RPC daemon * Fix typos * Properly update DB schema version * Fix test * Add test for KV compatibility| * Info messages about compability for RPC daemon * DB schema version to be one key * Update release intructions Co-authored-by: Artem Vorotnikov <artem@vorotnikov.me> Co-authored-by: b00ris <b00ris@mail.ru> Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local> Co-authored-by: lightclient <14004106+lightclient@users.noreply.github.com> Co-authored-by: canepat <16927169+canepat@users.noreply.github.com> Co-authored-by: Alex Sharov <AskAlexSharov@gmail.com> Co-authored-by: canepat <tullio.canepa@gmail.com> Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local>
2021-04-24 15:46:29 +00:00
func (api *APIImpl) ProtocolVersion(ctx context.Context) (hexutil.Uint, error) {
ver, err := api.ethBackend.ProtocolVersion(ctx)
if err != nil {
return 0, err
}
return hexutil.Uint(ver), nil
}
// GasPrice implements eth_gasPrice. Returns the current price per gas in wei.
2020-11-06 07:59:50 +00:00
func (api *APIImpl) GasPrice(ctx context.Context) (*hexutil.Big, error) {
2021-03-14 18:52:15 +00:00
oracle := gasprice.NewOracle(api, ethconfig.Defaults.GPO)
2020-11-06 07:59:50 +00:00
price, err := oracle.SuggestPrice(ctx)
return (*hexutil.Big)(price), err
}
// HeaderByNumber is necessary for gasprice.OracleBackend implementation
func (api *APIImpl) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
2021-04-03 06:26:00 +00:00
tx, err := api.db.BeginRo(ctx)
2020-11-06 07:59:50 +00:00
if err != nil {
return nil, err
}
defer tx.Rollback()
blockNum, err := getBlockNumber(number, tx)
if err != nil {
return nil, err
}
2021-03-30 09:53:54 +00:00
header := rawdb.ReadHeaderByNumber(ethdb.NewRoTxDb(tx), blockNum)
2020-11-06 07:59:50 +00:00
if header == nil {
return nil, fmt.Errorf("header not found: %d", blockNum)
}
return header, nil
}
// BlockByNumber is necessary for gasprice.OracleBackend implementation
func (api *APIImpl) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
2021-04-03 06:26:00 +00:00
tx, err := api.db.BeginRo(ctx)
2020-11-06 07:59:50 +00:00
if err != nil {
return nil, err
}
defer tx.Rollback()
blockNum, err := getBlockNumber(number, tx)
if err != nil {
return nil, err
}
block, err := rawdb.ReadBlockByNumber(tx, blockNum)
2020-11-06 07:59:50 +00:00
if err != nil {
return nil, err
}
if block == nil {
return nil, fmt.Errorf("block not found: %d", blockNum)
}
return block, nil
}
// ChainConfig is necessary for gasprice.OracleBackend implementation
func (api *APIImpl) ChainConfig() *params.ChainConfig {
2021-04-03 06:26:00 +00:00
tx, err := api.db.BeginRo(context.TODO())
if err != nil {
log.Warn("Could not read chain config from the db, defaulting to MainnetChainConfig", "err", err)
return params.MainnetChainConfig
}
defer tx.Rollback()
chainConfig, err := api.chainConfig(tx)
if err != nil {
log.Warn("Could not read chain config from the db, defaulting to MainnetChainConfig", "err", err)
return params.MainnetChainConfig
}
return chainConfig
}