From f8b9d9475e46a897c362f758aeeb5c780547be27 Mon Sep 17 00:00:00 2001 From: ramil Date: Tue, 20 Apr 2021 00:52:58 +0300 Subject: [PATCH] add eth_chainId JSON-RPC endpoint --- pkg/eth/api.go | 13 +++++++++++++ pkg/eth/backend.go | 6 +++--- pkg/graphql/graphql.go | 6 +++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/pkg/eth/api.go b/pkg/eth/api.go index 0b9f8e63..536673f3 100644 --- a/pkg/eth/api.go +++ b/pkg/eth/api.go @@ -167,6 +167,19 @@ func (pea *PublicEthAPI) GetBlockByHash(ctx context.Context, hash common.Hash, f return nil, err } +// ChainId is the EIP-155 replay-protection chain id for the current ethereum chain config. +func (api *PublicEthAPI) ChainId() (hexutil.Uint64, error) { + chainID := new(big.Int) + block, err := api.B.CurrentBlock() + if err != nil { + return 0, err + } + if config := api.B.Config.ChainConfig; config.IsEIP155(block.Number()) { + chainID = config.ChainID + } + return (hexutil.Uint64)(chainID.Uint64()), nil +} + /* Uncles diff --git a/pkg/eth/backend.go b/pkg/eth/backend.go index 86474f7f..6d378fdd 100644 --- a/pkg/eth/backend.go +++ b/pkg/eth/backend.go @@ -202,9 +202,9 @@ func (b *Backend) GetTd(blockHash common.Hash) (*big.Int, error) { } // CurrentBlock returns the current block -func (b *Backend) CurrentBlock() *types.Block { - block, _ := b.BlockByNumber(context.Background(), rpc.LatestBlockNumber) - return block +func (b *Backend) CurrentBlock() (*types.Block, error) { + block, err := b.BlockByNumber(context.Background(), rpc.LatestBlockNumber) + return block, err } // BlockByNumberOrHash returns block by number or hash diff --git a/pkg/graphql/graphql.go b/pkg/graphql/graphql.go index a4f4a923..12533016 100644 --- a/pkg/graphql/graphql.go +++ b/pkg/graphql/graphql.go @@ -867,7 +867,11 @@ func (r *Resolver) Blocks(ctx context.Context, args struct { if args.To != nil { to = rpc.BlockNumber(*args.To) } else { - to = rpc.BlockNumber(r.backend.CurrentBlock().Number().Int64()) + block, err := r.backend.CurrentBlock() + if err != nil { + return []*Block{}, nil + } + to = rpc.BlockNumber(block.Number().Int64()) } if to < from { return []*Block{}, nil