shardeum/shardeum · error

Failed to retrieve eth_getBlockByHash

Error message

Failed to retrieve eth_getBlockByHash

What it means

The eth_getBlockByHash external endpoint returns this 500 when looking up a block by its hash throws. It maps blockHash -> blockNumber via the blocksByHash table, then indexes readableBlocks with that number; if the hash key is absent the lookup yields undefined (usually not a throw), so an actual 500 means something else in the try block failed.

Source

Thrown at src/index.ts:1544

    }
  })

  shardus.registerExternalGet('eth_getBlockByHash', externalApiMiddleware, async (req, res) => {
    try {
      /* eslint-disable security/detect-object-injection */
      let blockHash = req.query.blockHash as string
      if (blockHash === 'latest') blockHash = readableBlocks[latestBlock].hash
      else if (blockHash.length !== 66 || !isHexString(blockHash)) {
        res.json({ error: 'Invalid block hash' })
        return
      }
      if (ShardeumFlags.VerboseLogs) console.log('Req: eth_getBlockByHash', blockHash)
      const blockNumber = blocksByHash[blockHash]
      res.json({ block: readableBlocks[blockNumber] })
      /* eslint-enable security/detect-object-injection */
    } catch (err) {
      if (ShardeumFlags.VerboseLogs) console.log('Failed to retrieve eth_getBlockByHash: ', err)
      res.status(500).json({ error: 'Failed to retrieve eth_getBlockByHash' })
    }
  })

  shardus.registerExternalGet('stake', async (req, res) => {
    try {
      const stakeRequiredUsd = AccountsStorage.cachedNetworkAccount.current.stakeRequiredUsd
      const stakeRequired = scaleByStabilityFactor(stakeRequiredUsd, AccountsStorage.cachedNetworkAccount)
      if (ShardeumFlags.VerboseLogs) console.log('Req: stake requirement', _readableSHM(stakeRequired))

      const response = {
        stakeRequired: {
          dataType: 'bi',
          value: stakeRequired.toString(16).padStart(16, '0'),
        },
        stakeRequiredUsd: {
          dataType: 'bi',
          value: stakeRequiredUsd.toString(16).padStart(16, '0'),
        },

View on GitHub (pinned to 0c454caf06)

Solutions

  1. Verify the hash is a valid 66-char hex block hash and exists on this network
  2. Enable ShardeumFlags.VerboseLogs to capture the underlying error logged as 'Failed to retrieve eth_getBlockByHash'
  3. Check that readableBlocks/blocksByHash are populated (node synced); restart the node if internal maps are empty
Defensive patterns

Strategy: try-catch

Validate before calling

const BLOCK_HASH_RE = /^0x[0-9a-fA-F]{64}$/
if (!BLOCK_HASH_RE.test(blockHash)) throw new Error('malformed block hash')

Type guard

function isBlockHash(v: unknown): v is `0x${string}` {
  return typeof v === 'string' && /^0x[0-9a-fA-F]{64}$/.test(v)
}

Try / catch

try {
  const r = await fetch(`/eth_getBlockByHash?hash=${blockHash}`)
  if (r.status === 500) return null // treat as not-found on this node
  return r.json()
} catch { return null }

Prevention

When it happens

Trigger: Calling GET /eth_getBlockByHash with a malformed hash string, a hash containing characters that break object-key injection guards or internal lookups, or an exception while serializing the found block for the response.

Common situations: Explorers or indexers querying by transaction/block hashes the node does not have (post-clean networks), passing 0x-prefixed vs non-prefixed hashes inconsistently, or hitting the endpoint mid-sync.

Related errors


AI-assisted analysis of shardeum/shardeum@0c454caf06 (2026-08-28). Data as JSON: /api/errors/db0310df023f74ff. Report an issue: GitHub.