NEW: Solana Privacy Rings are now live on mainnet
How to Deal with Blockhash Errors on Solana
Blog/Development

How to Deal with Blockhash Errors on Solana

Developer Experience EngineerOwen Venter on XOwen Venter on LinkedIn
7 min read

What is a Blockhash?

To understand what a blockhash is, you must understand what slots and blocks are.

  • A slot is a period of time in which a validator can produce a block 
  • A block is a collection of transactions + metadata that a validator processes. The metadata in each block links it to the previous block, creating a chain.

Solana has a target slot duration of 200ms, and in each slot, a validator can propose a new block. Actual timing can vary. If a block is not created, the slot is incremented, and the leader scheduled for the next slot can try to produce a block. This means that not all slots will have associated blocks, but all blocks have associated slots in which they were proposed.

A blockhash is a value that identifies a block. Each transaction includes a recent blockhash to show that it was prepared recently and limit how long it can be accepted. Fetch one with getLatestBlockhash. The response also includes lastValidBlockHeight, the last block height at which the transaction can be accepted. Save both values so your application can check whether the transaction has expired.

What are Solana's commitment levels?

Another important concept to understand is commitment levels. When an application requests data from an RPC node, the commitment level specifies how settled the block containing that data must be. The three options are processed, confirmed, and finalized.

With processed, the responding node has executed the block’s transactions, but the network may still discard that block. With finalized, validators have agreed to keep the block as part of the permanent ledger through Alpenglow, Solana’s consensus protocol. The confirmed level currently reports the same finalized state.

Why do blockhash errors occur?

Transactions using a recent blockhash expire when that hash is no longer considered recent enough. Validators check the BlockhashQueue, which stores the last 300 blockhashes, and accept hashes with an age of at most 150 produced blocks (151 stored hashes, since age is zero-indexed). Assuming one block per 200ms slot, this gives an approximate validity window of 30 seconds from blockhash creation. Skipped slots and slower block production can extend the elapsed time, so track expiry using the lastValidBlockHeight returned by getLatestBlockhash rather than a fixed timer.

Blockhash not found (Transaction simulation failed: Blockhash not found)

“Blockhash not found" errors occur when the blockhash included in a transaction is not deemed to be valid when a validator processes a transaction. This can either be because it is too old or, in some cases, too new. 

The most common cause of this error is when the blockhash included in a transaction is not present in the queue of the last 300 blockhashes that the validator will compare against. This will cause the blockhash not found error. Expired transactions can happen when a transaction is created and processed within the required time period. This can be because a user takes a long time to sign a transaction. There can also be situations in which a valid transaction is submitted but not included in the current block, and when it can be included in a later block, the transaction’s block hash is too old.

In situations like this, where the transaction has effectively expired, it is common to see Block height exceeded errors (TransactionExpiredBlock heightExceededError). To better understand this error, one should understand what block height means. Block height refers to the number of blocks beneath the current block. If the current block is block 1000 then the block height is 1000. When a transaction is created, the maximum block height for that transaction will be valid is noted. If it is observed while this transaction is being processed that the current block height is higher than the transaction's maximum valid block height, then the error will be thrown.

Another situation that could cause a “blockhash not found error” is when a transaction’s blockhash is newer than the blockhash used to check the expiration for that transaction. This is a bit confusing so here is an example: You create a transaction and include a blockhash for a specific block, let's say block 1000, and immediately send it to an RPC, the RPC may get the blockhash for the previous block, which in this case could be block 999 (due to the RPC using a higher commitment level or if the RPC node is lagging behind the network). As a result, the blockhash in the transaction will not be found. This is an odd situation, but it can happen in two scenarios:

1. Mismatched commitment levels

A blockhash fetched at processed may come from a block that has not yet become final. If preflight, the checks performed before sending a transaction to a leader, uses finalized, it checks against an earlier state of the ledger and may not recognize that blockhash. The block could also be discarded before it becomes final, making its blockhash unusable for subsequent transactions.

Use the same commitment level when fetching a blockhash and checking the transaction. This helps ensure both operations use a consistent view of the ledger.

2. Mismatched RPCs

If two different RPCs are used for getting the blockhash and sending the transaction, any lag experienced by the RPC sending the transaction could cause the block it uses to check validity to be older than the block used when creating the transaction.

How to Fix Blockhash Errors

There are a few steps to take to deal with each of the above-mentioned situations.

1. Ensure a transaction is submitted with a blockhash that is not too old:

Fetch a fresh blockhash shortly before signing the transaction. Use finalized to request one from a block the network has agreed to keep, and save its lastValidBlockHeight to track expiry.

A processed blockhash may be available earlier, but its block could be discarded. A transaction referencing that blockhash would then be rejected by validators following the network’s agreed history.

2. Ensure a transaction’s blockhash is not newer than the blockhash used to check the validity of the transaction:

Always set the preflightCommitment (even when using skipPreflight) to the same commitment level used to fetch the transactions blockhash. This will help you avoid the transaction’s blockhash being newer than the blockhash used to check the transaction's validity.

To deal with lagging RPC nodes when sending transactions, one should keep resending transitions to the RPC. This can be done on a set interval so that if an RPC is lagging, it will eventually catch up and detect the transaction's expiry.

If the simulateTransaction request is used, then the replaceRecentBlockhash parameter should be set. This flag instructs the RPC to replace the simulated transactions blockhash with a blockhash that will always be valid for simulation.

3. Continue to retry the transaction while the blockhash is valid:

When a transaction is created and the latest blockhash is fetched, you should note the lastValidBlockHeight for that blockhash. You can then continue to retry the transaction with that blockhash until the current block height exceeds the transaction’s valid block height. The getBlockHeight RPC call can be used to continuously check if the transaction is still valid. Once the current block height is higher than the transaction’s lastValidBlock height, a new blockhash should be fetched and used.

I hope this blog post helps you gain a better understanding of blockhash errors and how to reduce the number of times these errors occur. If you have any further questions, please don't hesitate to join our Discord community or send us a direct message on X.

Additional Resources:

‍

Subscribe to Helius

Stay up-to-date with the latest in Solana development and receive updates when we post