diff --git a/eth/api_backend.go b/eth/api_backend.go index da3cb1a3d8..612571845c 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -157,7 +157,11 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe if number == rpc.SafeBlockNumber { header := b.eth.blockchain.CurrentSafeBlock() - return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil + if header == nil { + return nil, errors.New("safe block not found") + } else { + return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil + } } return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil diff --git a/internal/ethapi/api_test.go b/internal/ethapi/api_test.go index ac825fa402..07e5761951 100644 --- a/internal/ethapi/api_test.go +++ b/internal/ethapi/api_test.go @@ -1818,6 +1818,11 @@ func TestRPCGetBlockOrHeader(t *testing.T) { fullTx: true, file: "hash-pending-fullTx", }, + // 26. safe block + { + blockNumber: rpc.SafeBlockNumber, + file: "tag-safe", + }, } for i, tt := range testSuite { diff --git a/internal/ethapi/testdata/eth_getBlockByNumber-tag-safe.json b/internal/ethapi/testdata/eth_getBlockByNumber-tag-safe.json new file mode 100644 index 0000000000..ec747fa47d --- /dev/null +++ b/internal/ethapi/testdata/eth_getBlockByNumber-tag-safe.json @@ -0,0 +1 @@ +null \ No newline at end of file