Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinI committed Nov 11, 2024
1 parent 4a09ecb commit f361ed2
Show file tree
Hide file tree
Showing 9 changed files with 6,085 additions and 6,019 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/legacy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async-std = { workspace = true, features = ["unstable", "attributes"] }
async-trait = { workspace = true }
bincode = { workspace = true }
committable = { workspace = true }
derive_more = { workspace = true, features = ["deref", "deref_mut", "debug"] }
futures = { workspace = true }
hotshot = { workspace = true }
hotshot-builder-api = { workspace = true }
Expand Down
113 changes: 113 additions & 0 deletions crates/legacy/src/block_size_limits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use std::time::{Duration, Instant};

/// Adjustable limits for block size ceiled by
/// maximum block size allowed by the protocol
#[derive(Debug, Clone)]
pub struct BlockSizeLimits {
// maximum block size allowed by the protocol
pub protocol_max_block_size: u64,
// estimated maximum block size we can build in time
pub max_block_size: u64,
pub increment_period: Duration,
pub last_block_size_increment: Instant,
}

impl BlockSizeLimits {
/// Never go lower than 10 kilobytes
pub const MAX_BLOCK_SIZE_FLOOR: u64 = 10_000;
/// When adjusting max block size, it will be decremented or incremented
/// by current value / `MAX_BLOCK_SIZE_CHANGE_DIVISOR`
pub const MAX_BLOCK_SIZE_CHANGE_DIVISOR: u64 = 10;

pub fn new(protocol_max_block_size: u64, increment_period: Duration) -> Self {
Self {
protocol_max_block_size,
max_block_size: protocol_max_block_size,
increment_period,
last_block_size_increment: Instant::now(),
}
}

/// If increment period has elapsed or `force` flag is set,
/// increment [`Self::max_block_size`] by current value * [`Self::MAX_BLOCK_SIZE_CHANGE_DIVISOR`]
/// with [`Self::protocol_max_block_size`] as a ceiling
pub fn try_increment_block_size(&mut self, force: bool) {
if force || self.last_block_size_increment.elapsed() >= self.increment_period {
self.max_block_size = std::cmp::min(
self.max_block_size
+ self
.max_block_size
.div_ceil(Self::MAX_BLOCK_SIZE_CHANGE_DIVISOR),
self.protocol_max_block_size,
);
self.last_block_size_increment = Instant::now();
}
}

/// Decrement [`Self::max_block_size`] by current value * [`Self::MAX_BLOCK_SIZE_CHANGE_DIVISOR`]
/// with [`Self::MAX_BLOCK_SIZE_FLOOR`] as a floor
pub fn decrement_block_size(&mut self) {
self.max_block_size = std::cmp::max(
self.max_block_size
- self
.max_block_size
.div_ceil(Self::MAX_BLOCK_SIZE_CHANGE_DIVISOR),
Self::MAX_BLOCK_SIZE_FLOOR,
);
}
}

#[cfg(test)]
mod tests {
use marketplace_builder_shared::testing::constants::{
TEST_MAX_BLOCK_SIZE_INCREMENT_PERIOD, TEST_PROTOCOL_MAX_BLOCK_SIZE,
};

use super::*;

#[test]
fn test_increment_block_size() {
let mut block_size_limits =
BlockSizeLimits::new(TEST_PROTOCOL_MAX_BLOCK_SIZE, Duration::from_millis(25));
// Simulate decreased limits
block_size_limits.max_block_size = TEST_PROTOCOL_MAX_BLOCK_SIZE / 2;

// Shouldn't increment, increment period hasn't passed yet
block_size_limits.try_increment_block_size(false);
assert!(block_size_limits.max_block_size == TEST_PROTOCOL_MAX_BLOCK_SIZE / 2);

// Should increment, increment period hasn't passed yet, but force flag is set
block_size_limits.try_increment_block_size(true);
assert!(block_size_limits.max_block_size > TEST_PROTOCOL_MAX_BLOCK_SIZE / 2);
let new_size = block_size_limits.max_block_size;

std::thread::sleep(Duration::from_millis(30));

// Should increment, increment period has passed
block_size_limits.try_increment_block_size(false);
assert!(block_size_limits.max_block_size > new_size);
}

#[test]
fn test_decrement_block_size() {
let mut block_size_limits = BlockSizeLimits::new(
TEST_PROTOCOL_MAX_BLOCK_SIZE,
TEST_MAX_BLOCK_SIZE_INCREMENT_PERIOD,
);
block_size_limits.decrement_block_size();
assert!(block_size_limits.max_block_size < TEST_PROTOCOL_MAX_BLOCK_SIZE);
}

#[test]
fn test_max_block_size_floor() {
let mut block_size_limits = BlockSizeLimits::new(
BlockSizeLimits::MAX_BLOCK_SIZE_FLOOR + 1,
TEST_MAX_BLOCK_SIZE_INCREMENT_PERIOD,
);
block_size_limits.decrement_block_size();
assert_eq!(
block_size_limits.max_block_size,
BlockSizeLimits::MAX_BLOCK_SIZE_FLOOR
);
}
}
Loading

0 comments on commit f361ed2

Please sign in to comment.