Skip to content

Commit a7e24df

Browse files
authored
cli: allow drift in simulated CUs (#7447)
1 parent c372da2 commit a7e24df

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

clap-utils/src/compute_budget.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ pub enum ComputeUnitLimit {
4343
Static(u32),
4444
/// Simulate the transaction to find out the compute unit usage
4545
Simulated,
46+
/// Simulate the transaction and add a small percentage to account for potential drift
47+
SimulatedWithExtraPercentage(u8),
4648
}

cli/src/compute_budget.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,19 @@ pub(crate) fn simulate_and_update_compute_unit_limit(
9898
};
9999

100100
match compute_unit_limit {
101-
ComputeUnitLimit::Simulated => {
102-
let compute_unit_limit =
101+
ComputeUnitLimit::Simulated | ComputeUnitLimit::SimulatedWithExtraPercentage(_) => {
102+
let base_compute_unit_limit =
103103
simulate_for_compute_unit_limit_unchecked(rpc_client, message)?;
104104

105+
let compute_unit_limit =
106+
if let ComputeUnitLimit::SimulatedWithExtraPercentage(n) = compute_unit_limit {
107+
(base_compute_unit_limit as u64)
108+
.saturating_mul(100_u64.saturating_add(*n as u64))
109+
.saturating_div(100) as u32
110+
} else {
111+
base_compute_unit_limit
112+
};
113+
105114
// Overwrite the compute unit limit instruction with the actual units consumed
106115
message.instructions[compute_unit_limit_ix_index].data =
107116
ComputeBudgetInstruction::set_compute_unit_limit(compute_unit_limit).data;
@@ -138,7 +147,7 @@ impl WithComputeUnitConfig for Vec<Instruction> {
138147
compute_unit_limit,
139148
));
140149
}
141-
ComputeUnitLimit::Simulated => {
150+
ComputeUnitLimit::Simulated | ComputeUnitLimit::SimulatedWithExtraPercentage(_) => {
142151
// Default to the max compute unit limit because later transactions will be
143152
// simulated to get the exact compute units consumed.
144153
self.push(ComputeBudgetInstruction::set_compute_unit_limit(

cli/src/stake.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1709,8 +1709,12 @@ pub fn process_deactivate_stake_account(
17091709
*stake_account_pubkey
17101710
};
17111711

1712+
// DeactivateDelinquent parses a VoteState, which may change between simulation and execution
17121713
let compute_unit_limit = match blockhash_query {
17131714
BlockhashQuery::None(_) | BlockhashQuery::FeeCalculator(_, _) => ComputeUnitLimit::Default,
1715+
BlockhashQuery::All(_) if deactivate_delinquent => {
1716+
ComputeUnitLimit::SimulatedWithExtraPercentage(5)
1717+
}
17141718
BlockhashQuery::All(_) => ComputeUnitLimit::Simulated,
17151719
};
17161720
let ixs = vec![if deactivate_delinquent {
@@ -2805,9 +2809,10 @@ pub fn process_delegate_stake(
28052809

28062810
let recent_blockhash = blockhash_query.get_blockhash(rpc_client, config.commitment)?;
28072811

2812+
// DelegateStake parses a VoteState, which may change between simulation and execution
28082813
let compute_unit_limit = match blockhash_query {
28092814
BlockhashQuery::None(_) | BlockhashQuery::FeeCalculator(_, _) => ComputeUnitLimit::Default,
2810-
BlockhashQuery::All(_) => ComputeUnitLimit::Simulated,
2815+
BlockhashQuery::All(_) => ComputeUnitLimit::SimulatedWithExtraPercentage(5),
28112816
};
28122817
let ixs = vec![stake_instruction::delegate_stake(
28132818
stake_account_pubkey,

0 commit comments

Comments
 (0)