Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions contracts/predictify-hybrid/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@ impl GasTracker {
/// - Create the guard once at the start of the operation
/// - Call `check()` at strategic checkpoints (every 10-50 iterations)
/// - Use a threshold of 50,000-100,000 instructions for safe abort
/// Conservative per-transaction CPU instruction ceiling used by [`BudgetGuard`]
/// to compute the remaining budget.
pub const TX_CPU_INSTRUCTION_LIMIT: u64 = 100_000_000;

#[derive(Clone)]
pub struct BudgetGuard {
env: Env,
Expand Down Expand Up @@ -470,10 +474,15 @@ impl BudgetGuard {
/// This is a lightweight call that reads a single value from the host.
/// It should be called at regular intervals, not on every iteration.
pub fn check(&self) -> Result<(), Error> {
// `threshold_remaining` is the minimum headroom that must still be
// available under the per-transaction CPU instruction ceiling. The
// previous implementation compared the *consumed* instructions against
// the threshold, which aborted any non-trivial payout loop after just
// 100k instructions.
let current = cpu_instruction_cost(&self.env);
let consumed = current.saturating_sub(self.start_instructions);
let remaining = TX_CPU_INSTRUCTION_LIMIT.saturating_sub(current);

if consumed >= self.threshold_remaining {
if remaining < self.threshold_remaining {
return Err(Error::OperationWouldExceedBudget);
}
Ok(())
Expand Down
2 changes: 0 additions & 2 deletions contracts/predictify-hybrid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ mod recovery;
mod statistics;
mod tokens;
mod rate_limiter;
#[cfg(test)]
mod rate_limit_enforcement_tests;
mod dispute_multisig;
mod event_topic_catalog;
pub mod event_topic_compat;
Expand Down
Loading
Loading