Commit 8f72064d authored by Mark Tyneway's avatar Mark Tyneway

dtl: handle errors in context api

Both `GET /eth/context/latest` and `GET /eth/context/blocknumber/:number`
rely on fetching a remote block to pull the timestamp, blocknumber and
blockhash from. If the fetched block is not found, then properties on
`null` will be attempted to be accessed which will result in a runtime
error. This commit adds handling for this case to prevent that kind of
bug.
parent 849ec8cc
---
'@eth-optimism/data-transport-layer': patch
---
Handle case where the remote block isn't found for `GET /eth/context/latest` and `GET /eth/context/blocknumber/:number`
...@@ -340,6 +340,9 @@ export class L1TransportServer extends BaseService<L1TransportServerOptions> { ...@@ -340,6 +340,9 @@ export class L1TransportServer extends BaseService<L1TransportServerOptions> {
const blockNumber = Math.max(0, tip - this.options.confirmations) const blockNumber = Math.max(0, tip - this.options.confirmations)
const block = await this.state.l1RpcProvider.getBlock(blockNumber) const block = await this.state.l1RpcProvider.getBlock(blockNumber)
if (block === null) {
throw new Error(`Cannot GET /eth/context/latest at ${blockNumber}`)
}
return { return {
blockNumber: block.number, blockNumber: block.number,
...@@ -366,6 +369,10 @@ export class L1TransportServer extends BaseService<L1TransportServerOptions> { ...@@ -366,6 +369,10 @@ export class L1TransportServer extends BaseService<L1TransportServerOptions> {
} }
const block = await this.state.l1RpcProvider.getBlock(number) const block = await this.state.l1RpcProvider.getBlock(number)
if (block === null) {
throw new Error(`Cannot GET /eth/context/blocknumber/${number}`)
}
return { return {
blockNumber: block.number, blockNumber: block.number,
timestamp: block.timestamp, timestamp: block.timestamp,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment