4

I'm using getLatestBlockhash() to pass into a transaction.

const { blockhash } = await connection.getLatestBlockhash("finalized");

However, I want to invalidate the transaction on my backend if a block has passed and the user has not approved the transaction. How I would check if a block has expired using @solana/web3.js?

2
  • can you elaborate what you mean by "if a block has passed", as well as your motivation for invalidation?
    – trent.sol
    Commented Jul 23, 2022 at 18:13
  • i'm creating a partially signed transaction on my backend, saving the transaction referrence in a database. sending the transaction to the front end and asking the user to sign it. there's a cron job on my backend that uses findReferrence() to check if a transaction is successful, but i also want to check if a block has passed before a user is able to sign/the user has left the site and the transaction will never be completed so i want to invalidate the transaction Commented Jul 23, 2022 at 19:55

2 Answers 2

2

There's no way to "invalidate" a transaction that has been fully signed and broadcast, it can only be allowed to expire.

The reply of getLatestBlockhash also has a lastValidBlockheight field. getEpochInfo can be polled until the value in its blockHeight field exceeds the blockhash's last valid height. The cookbook has a pretty good explainer on the lifetime of transactions

0

You can use the web3 method simulateTransaction. If the blockhash has expired then this method returns an error indicating that the blockhash has expired.

The blockhash is valid if it is within the last 150 blocks.

Do note though that sendTransaction will also check that the blockhash is valid for you, so long as the you do not skip the preflight checks, skipPreflight: false, which is one of the sendOptions in the sendTransaction method. More on that from the cookbook here

The solana cookbook also goes into more detail here on keeping the transaction fresh. It references an RPC method that isn't yet available on the web3 library specifically for the purpose of checking a blockhash is still valid, called isBlockhashValid.

2
  • use of isBlockhashValid should be discouraged as it can return false in situations other than when the blockhash has expired
    – trent.sol
    Commented Jul 24, 2022 at 4:39
  • Probably should update the solana cookbook then
    – Henry E
    Commented Jul 24, 2022 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.