Skip to content

Commit

Permalink
feat(rpc): add scroll_getBlockByNumber (ethereum#413)
Browse files Browse the repository at this point in the history
* add scroll_getBlockByNumber rpc api

* bump version

* fix

* bump version

* Update version.go

* Revert "Update version.go"

This reverts commit 1be1d9c.

---------

Co-authored-by: Péter Garamvölgyi <peter@scroll.io>
Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
Co-authored-by: HAOYUatHZ <haoyu@protonmail.com>
  • Loading branch information
4 people authored Aug 1, 2023
1 parent 22fec57 commit 7b75f16
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
10 changes: 10 additions & 0 deletions eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 9 additions & 4 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
[
Expand Down
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down

0 comments on commit 7b75f16

Please sign in to comment.