4

I am trying to get a bit better performance for a game and would like to have the block hash already there when I send the transaction so I don't have to wait for the roundtrip to get it just before I send the transaction.

So best would be if I could socket subscribe to the latest block hash but seems this function is not available. Does anyone have experience with this?

I could poll the blockhash, but then I get rate limited quite quickly.

2 Answers 2

3

Blockhashes are valid for 150 slots which is roughly 60 seconds. You don't need to continually poll it to send transactions. You can fetch it roughly every minute and use it until it expires.

2
  • Will my transaction always end up in the next block no matter which recent block hash i put in? What is the blockhash used for? Do you know?
    – Jonas H.
    Commented Jan 8, 2023 at 10:41
  • 1
    It won't always end up in the next block if you put in a blockhash that's nonexistent or expired. As I mentioned in the post, a new blockhash is only valid for 150 slots. It's used to confirm that the transaction was signed and sent in a specific time period.
    – acheron
    Commented Jan 8, 2023 at 14:37
2

I ended up polling the latestBockhash every half a second and using the cached one for my transactions since the block hashes are valid for longer and apparently it does not need to be the latest even when the name implies that.

But there are some other options.

  1. subscribeBlock pushed the whole block data to the client and could be used to get a hash but it contains lots of data and the rpc may not support it. Maybe it could be filtered somehow, but I did not try that.

The more complicated approach from Solana break works like this as Jordash explained me is to send the transaction directly to the current leader. Sadly with the Introduction of QUIC it will probably not be possible anymore to push transactions directly to the TPU UDP port of the leader anymore.

https://github.com/solana-labs/break/blob/c97989858498f056954649c0f8ae03862a805a4d/server/src/available_nodes.ts#L33

Polling getClusterNodes is used to obtain https://solana-labs.github.io/solana-web3.js/types/ContactInfo.html which has the TPU address for nodes.

onSlotUpdate is used to track recent slots

https://github.com/solana-labs/break/blob/c97989858498f056954649c0f8ae03862a805a4d/server/src/leader_tracker.ts#L20

This is combined with the leader schedule to determine the current leader

Sockets are opened to nodes

https://github.com/solana-labs/break/blob/c97989858498f056954649c0f8ae03862a805a4d/server/src/tpu_proxy.ts#L161

and raw signed transactions are sent over these sockets

A websocket from the server forwards to the TPU proxy

https://github.com/solana-labs/break/blob/c97989858498f056954649c0f8ae03862a805a4d/server/src/api.ts#L13

The client has a socket connection to the server to send transactions over

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.