Your impression of setComputeUnitLimit() is indeed correct. This function is used to set the maximum number of compute units (CUs) that the transaction is allowed to consume. If the actual compute units required for executing the transaction are less than the limit set, only the required amount will be consumed, not the full amount specified by the limit.
This is useful for ensuring your transaction does not exceed a certain budget and ensures your transactions are processed within the resource limits you specify
For instance, if you set the compute unit limit to 500,000 but the transaction only needs 300,000 compute units, it will only consume 300,000 CUs.
If the transaction tries to exceed this cap during execution, it will fail.
How to Test and Verify:
To verify how setComputeUnitLimit() behaves, you can create a test where you measure the actual compute units consumed by a transaction. Here’s a basic outline for such a test using Solana's development tools:
Execute the transaction with a compute unit limit just above the expected consumption (e.g., if expected is 300,000, set it at 310,000).
Execute the same transaction with a higher limit (e.g., 500,000 and 700,000).
Measure and Compare:
Use simulateTransaction RPC calls to simulate the transactions and retrieve the number of compute units actually used.
Compare the compute units consumed in each transaction to verify if the set limit affects the actual usage.
https://solana.com/docs/core/fees
https://solana.com/developers/guides/advanced/how-to-request-optimal-compute