Skip to content

Commit 7782990

Browse files
authored
Merge pull request #3525 from dessaya/fix-nil-pointer
jsonrpc: fix nil pointer when block by hash not found
2 parents da17d43 + 54611c9 commit 7782990

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

packages/evm/jsonrpc/evmchain.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ func (e *EVMChain) iscStateFromEVMBlockNumberOrHash(blockNumberOrHash *rpc.Block
258258
}
259259
blockHash, _ := blockNumberOrHash.Hash()
260260
block := e.BlockByHash(blockHash)
261+
if block == nil {
262+
return nil, fmt.Errorf("block with hash %s not found", blockHash)
263+
}
261264
return e.iscStateFromEVMBlockNumber(block.Number())
262265
}
263266

@@ -808,7 +811,11 @@ func (e *EVMChain) TraceBlockByNumber(blockNumber uint64, config *tracers.TraceC
808811

809812
func (e *EVMChain) getBlockByNumberOrHash(blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
810813
if h, ok := blockNrOrHash.Hash(); ok {
811-
return e.BlockByHash(h), nil
814+
block := e.BlockByHash(h)
815+
if block == nil {
816+
return nil, fmt.Errorf("block not found: %v", blockNrOrHash.String())
817+
}
818+
return block, nil
812819
} else if n, ok := blockNrOrHash.Number(); ok {
813820
switch n {
814821
case rpc.LatestBlockNumber:

packages/evm/jsonrpc/jsonrpctest/jsonrpc_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ func newSoloTestEnv(t testing.TB) *soloTestEnv {
9393
func TestRPCGetBalance(t *testing.T) {
9494
env := newSoloTestEnv(t)
9595
_, emptyAddress := solo.NewEthereumAccount()
96+
97+
{
98+
_, err := env.Client.BalanceAtHash(context.Background(), emptyAddress, common.Hash{})
99+
require.ErrorContains(env.T, err, "not found")
100+
}
101+
96102
require.Zero(t, env.Balance(emptyAddress).Uint64())
97103
wallet, nonEmptyAddress := env.soloChain.NewEthereumAccountWithL2Funds()
98104
require.Equal(

packages/evm/jsonrpc/service.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,9 @@ func (e *EthService) NewHeads(ctx context.Context) (*rpc.Subscription, error) {
424424
}
425425

426426
func (e *EthService) Logs(ctx context.Context, q *RPCFilterQuery) (*rpc.Subscription, error) {
427+
if q == nil {
428+
q = &RPCFilterQuery{}
429+
}
427430
notifier, supported := rpc.NotifierFromContext(ctx)
428431
if !supported {
429432
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported

0 commit comments

Comments
 (0)