Skip to content

Commit

Permalink
Add compute unit buffer to transactions (#149)
Browse files Browse the repository at this point in the history
* Add compute unit buffer to transactions

* Fix build error
  • Loading branch information
nickgarfield committed Jan 29, 2023
1 parent 44f4741 commit ee5494d
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion plugin/src/builders/thread_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ static TRANSACTION_MESSAGE_SIZE_LIMIT: usize = 1_232;
/// Max compute units that may be used by transaction.
static TRANSACTION_COMPUTE_UNIT_LIMIT: u32 = 1_400_000;

/// The buffer amount to add to transactions' compute units in case on-chain PDA derivations take more CUs than used in simulation.
static TRANSACTION_COMPUTE_UNIT_BUFFER: u32 = 100;

pub fn build_thread_exec_tx(
client: Arc<ClockworkClient>,
thread: Thread,
Expand Down Expand Up @@ -137,9 +140,13 @@ pub fn build_thread_exec_tx(

// Set the transaction's compute unit limit to be exactly the amount that was used in simulation.
if let Some(units_consumed) = units_consumed {
let units_committed = std::cmp::min(
(units_consumed as u32) + TRANSACTION_COMPUTE_UNIT_BUFFER,
TRANSACTION_COMPUTE_UNIT_LIMIT,
);
_ = std::mem::replace(
&mut successful_ixs[0],
ComputeBudgetInstruction::set_compute_unit_limit(units_consumed as u32),
ComputeBudgetInstruction::set_compute_unit_limit(units_committed),
);
}

Expand Down

0 comments on commit ee5494d

Please sign in to comment.