Skip to content

Commit

Permalink
Spend down the block budget limit by 25% every block
Browse files Browse the repository at this point in the history
Signed-off-by: Jacinta Ferrant <[email protected]>
  • Loading branch information
jferrant committed Nov 11, 2024
1 parent 7224b9b commit 1443e8c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 14 additions & 0 deletions clarity/src/vm/costs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,7 @@ pub trait CostOverflowingMath<T> {
fn cost_overflow_mul(self, other: T) -> Result<T>;
fn cost_overflow_add(self, other: T) -> Result<T>;
fn cost_overflow_sub(self, other: T) -> Result<T>;
fn cost_overflow_div(self, other: T) -> Result<T>;
}

impl CostOverflowingMath<u64> for u64 {
Expand All @@ -1185,6 +1186,10 @@ impl CostOverflowingMath<u64> for u64 {
self.checked_sub(other)
.ok_or_else(|| CostErrors::CostOverflow)
}
fn cost_overflow_div(self, other: u64) -> Result<u64> {
self.checked_div(other)
.ok_or_else(|| CostErrors::CostOverflow)
}
}

impl ExecutionCost {
Expand Down Expand Up @@ -1293,6 +1298,15 @@ impl ExecutionCost {
Ok(())
}

pub fn divide(&mut self, divisor: u64) -> Result<()> {
self.runtime = self.runtime.cost_overflow_div(divisor)?;
self.read_count = self.read_count.cost_overflow_div(divisor)?;
self.read_length = self.read_length.cost_overflow_div(divisor)?;
self.write_length = self.write_length.cost_overflow_div(divisor)?;
self.write_count = self.write_count.cost_overflow_div(divisor)?;
Ok(())
}

/// Returns whether or not this cost exceeds any dimension of the
/// other cost.
pub fn exceeds(&self, other: &ExecutionCost) -> bool {
Expand Down
14 changes: 13 additions & 1 deletion stackslib/src/chainstate/stacks/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2222,10 +2222,22 @@ impl StacksBlockBuilder {
let mempool_settings = settings.mempool_settings.clone();
let ts_start = get_epoch_time_ms();
let stacks_epoch_id = epoch_tx.get_epoch();
let block_limit = epoch_tx
let remaining_limit = epoch_tx
.block_limit()
.expect("Failed to obtain block limit from miner's block connection");

// Attempt to spread the cost limit across the tenure by always using only 25% of the remaining budget
// until the remaining budget is less than 100
let mut target_limit = remaining_limit.clone();
let block_limit = if target_limit.divide(100).is_ok() {
target_limit.multiply(25).expect(
"BUG: Successfully divided by 100, but failed to multiply block limit by 25",
);
target_limit
} else {
remaining_limit
};

let mut tx_events = Vec::new();

for initial_tx in initial_txs.iter() {
Expand Down

0 comments on commit 1443e8c

Please sign in to comment.