Skip to content

feat(relayer): adaptive batch size#136

Open
frolvanya wants to merge 4 commits intomainfrom
feat/adaptive-batch-size
Open

feat(relayer): adaptive batch size#136
frolvanya wants to merge 4 commits intomainfrom
feat/adaptive-batch-size

Conversation

@frolvanya
Copy link
Contributor

No description provided.

@frolvanya frolvanya requested a review from a team March 6, 2026 23:47
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces adaptive batch sizing for the relayer's block submission process, implementing an AIMD (Additive Increase, Multiplicative Decrease) strategy combined with proactive gas-based sizing. Instead of using fixed batch sizes from configuration, the relayer now dynamically adjusts the number of blocks per transaction based on observed gas consumption and error feedback from the NEAR network.

Changes:

  • Added a new AdaptiveBatchSizer module that dynamically adjusts submit batch size and fetch size based on gas telemetry and gas-exceeded errors, using AIMD with configurable cooldown periods.
  • Modified NearClient::submit_blocks to return a SubmitResult with gas telemetry and detect "gas exceeded" errors as a new CustomError::GasExceeded variant.
  • Updated configuration to support new min_batch_size and batch_size_cooldown_sec parameters, and changed submit_batch_size type from usize to u64.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
relayer/src/adaptive_batch.rs New module implementing AdaptiveBatchSizer with AIMD algorithm, gas-based sizing, and comprehensive unit tests.
relayer/src/near_client.rs Added GasExceeded error variant, SubmitResult struct with gas telemetry, and updated response parsing to extract gas metrics.
relayer/src/main.rs Integrated AdaptiveBatchSizer into Synchronizer, using dynamic batch/fetch sizes and handling GasExceeded errors.
relayer/src/config.rs Added min_batch_size and batch_size_cooldown_sec config fields, changed submit_batch_size to u64.
relayer/src/lib.rs Exported the new adaptive_batch module.
relayer/config.toml.example Added default values for new config parameters.
relayer/configs/btc_testnet.toml Added new adaptive batch config settings.
relayer/configs/btc_mainnet.toml Added new adaptive batch config settings.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

@frolvanya frolvanya force-pushed the feat/adaptive-batch-size branch from c3895c3 to 884bc49 Compare March 6, 2026 23:55
impl AdaptiveBatchSizer {
/// Create a new `AdaptiveBatchSizer` from config.
///
/// `num_parallel_txs` (N) is computed as `config.fetch_batch_size / config.submit_batch_size`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the opposite approach is clearer? submit_batch_size and num_parallel_txs are chosen parameters and fetch size is derived

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see it's the current behavior so it's okay not to change

}
}

/// Current submit batch size.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such comments don't really say anything

'main_loop: loop {
let (current_fetch_size, current_batch_size) = {
let mut sizer = self.batch_sizer.lock().await;
sizer.try_recover();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get why try_recover is needed if we straight up calculate the optimal batch size on successful submission?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants