From 7b75f163b0c3999abe319635d622f8ba69aa6368 Mon Sep 17 00:00:00 2001 From: Nazarii Denha Date: Tue, 1 Aug 2023 15:32:12 +0200 Subject: [PATCH] feat(rpc): add `scroll_getBlockByNumber` (#413) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add scroll_getBlockByNumber rpc api * bump version * fix * bump version * Update version.go * Revert "Update version.go" This reverts commit 1be1d9c4fdbb64254659ac193cffd23dbbacfb5e. --------- Co-authored-by: Péter Garamvölgyi Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> Co-authored-by: HAOYUatHZ --- eth/api.go | 10 ++++++++++ ethclient/ethclient.go | 13 +++++++++---- internal/web3ext/web3ext.go | 6 ++++++ params/version.go | 2 +- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/eth/api.go b/eth/api.go index d1c9fee90a..49e5bfcf67 100644 --- a/eth/api.go +++ b/eth/api.go @@ -698,3 +698,13 @@ func (api *ScrollAPI) GetBlockByHash(ctx context.Context, hash common.Hash, full } return nil, err } + +// GetBlockByNumber returns the requested block. When fullTx is true all transactions in the block are returned in full +// detail, otherwise only the transaction hash is returned. +func (api *ScrollAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) { + block, err := api.eth.APIBackend.BlockByNumber(ctx, number) + if block != nil { + return api.rpcMarshalBlock(ctx, block, fullTx) + } + return nil, err +} diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 5f1f54794f..f46aed9a73 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -357,11 +357,16 @@ func (r *rpcRowConsumption) UnmarshalJSON(input []byte) error { return nil } -// GetBlockByHash returns the requested block. When fullTx is true all transactions in the block are returned in full -// detail, otherwise only the transaction hash is returned. -func (ec *Client) GetBlockByHash(ctx context.Context, blockHash common.Hash) (*types.BlockWithRowConsumption, error) { +// GetBlockByNumberOrHash returns the requested block +func (ec *Client) GetBlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.BlockWithRowConsumption, error) { var raw json.RawMessage - err := ec.c.CallContext(ctx, &raw, "scroll_getBlockByHash", blockHash, true) + var err error + if number, ok := blockNrOrHash.Number(); ok { + err = ec.c.CallContext(ctx, &raw, "scroll_getBlockByNumber", number, true) + } + if hash, ok := blockNrOrHash.Hash(); ok { + err = ec.c.CallContext(ctx, &raw, "scroll_getBlockByHash", hash, true) + } if err != nil { return nil, err } else if len(raw) == 0 { diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 917204409b..7c65cb0f53 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -881,6 +881,12 @@ web3._extend({ params: 2, inputFormatter: [null, function (val) { return !!val; }] }), + new web3._extend.Method({ + name: 'getBlockByNumber', + call: 'scroll_getBlockByNumber', + params: 2, + inputFormatter: [null, function (val) { return !!val; }] + }), ], properties: [ diff --git a/params/version.go b/params/version.go index 0d21d16a2f..e5cc4f104d 100644 --- a/params/version.go +++ b/params/version.go @@ -24,7 +24,7 @@ import ( const ( VersionMajor = 4 // Major version component of the current release VersionMinor = 3 // Minor version component of the current release - VersionPatch = 2 // Patch version component of the current release + VersionPatch = 3 // Patch version component of the current release VersionMeta = "sepolia" // Version metadata to append to the version string )