diff --git a/.changeset/forty-hotels-brake.md b/.changeset/forty-hotels-brake.md new file mode 100644 index 000000000000..da742376623f --- /dev/null +++ b/.changeset/forty-hotels-brake.md @@ -0,0 +1,5 @@ +--- +'@eth-optimism/l2geth': patch +--- + +Protect a possible `nil` reference in `eth_call` when the blockchain is empty diff --git a/l2geth/internal/ethapi/api.go b/l2geth/internal/ethapi/api.go index a3f0688f2727..2f3f9940e082 100644 --- a/l2geth/internal/ethapi/api.go +++ b/l2geth/internal/ethapi/api.go @@ -937,14 +937,16 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.Blo if err != nil { return nil, 0, false, err } - txs := block.Transactions() - if header.Number.Uint64() != 0 { - if len(txs) != 1 { - return nil, 0, false, fmt.Errorf("block %d has more than 1 transaction", header.Number.Uint64()) + if block != nil { + txs := block.Transactions() + if header.Number.Uint64() != 0 { + if len(txs) != 1 { + return nil, 0, false, fmt.Errorf("block %d has more than 1 transaction", header.Number.Uint64()) + } + tx := txs[0] + blockNumber = tx.L1BlockNumber() + timestamp = new(big.Int).SetUint64(tx.L1Timestamp()) } - tx := txs[0] - blockNumber = tx.L1BlockNumber() - timestamp = new(big.Int).SetUint64(tx.L1Timestamp()) } msg, err = core.EncodeSimulatedMessage(msg, timestamp, blockNumber, executionManager, stateManager) if err != nil {