Skip to content
Open
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
2 changes: 1 addition & 1 deletion magicblock-committor-service/src/tasks/args_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl BaseTask for ArgsTask {
}
}

fn optimize(
fn try_optimize_tx_size(
self: Box<Self>,
) -> Result<Box<dyn BaseTask>, Box<dyn BaseTask>> {
match self.task_type {
Expand Down
2 changes: 1 addition & 1 deletion magicblock-committor-service/src/tasks/buffer_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl BaseTask for BufferTask {
}

/// No further optimizations
fn optimize(
fn try_optimize_tx_size(
self: Box<Self>,
) -> Result<Box<dyn BaseTask>, Box<dyn BaseTask>> {
// Since the buffer in BufferTask doesn't contribute to the size of
Expand Down
5 changes: 3 additions & 2 deletions magicblock-committor-service/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ pub trait BaseTask: Send + Sync + DynClone {
/// Gets instruction for task execution
fn instruction(&self, validator: &Pubkey) -> Instruction;

/// Optimizes Task strategy if possible, otherwise returns itself
fn optimize(
/// Optimize for transaction size so that more instructions can be buddled together in a single
/// transaction. Return Ok(new_tx_optimized_task), else Err(self) if task cannot be optimized.
fn try_optimize_tx_size(
self: Box<Self>,
) -> Result<Box<dyn BaseTask>, Box<dyn BaseTask>>;

Expand Down
15 changes: 9 additions & 6 deletions magicblock-committor-service/src/tasks/task_strategist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ impl TaskStrategist {
persistor: &Option<P>,
) -> TaskStrategistResult<TransactionStrategy> {
// Attempt optimizing tasks themselves(using buffers)
if Self::optimize_strategy(&mut tasks)? <= MAX_ENCODED_TRANSACTION_SIZE
if Self::try_optimize_tx_size_if_needed(&mut tasks)?
<= MAX_ENCODED_TRANSACTION_SIZE
{
// Persist tasks strategy
if let Some(persistor) = persistor {
Expand Down Expand Up @@ -150,9 +151,11 @@ impl TaskStrategist {
)
}

/// Optimizes set of [`TaskDeliveryStrategy`] to fit [`MAX_ENCODED_TRANSACTION_SIZE`]
/// Returns size of tx after optimizations
fn optimize_strategy(
/// Optimizes tasks so as to bring the transaction size within the limit [`MAX_ENCODED_TRANSACTION_SIZE`]
/// Returns Ok(size of tx after optimizations) else Err(SignerError).
/// Note that the returned size, though possibly optimized one, may still not be under
/// the limit MAX_ENCODED_TRANSACTION_SIZE. The caller needs to check and make decision accordingly.
fn try_optimize_tx_size_if_needed(
tasks: &mut [Box<dyn BaseTask>],
) -> Result<usize, SignerError> {
// Get initial transaction size
Expand Down Expand Up @@ -207,7 +210,7 @@ impl TaskStrategist {
let tmp_task = Box::new(tmp_task) as Box<dyn BaseTask>;
std::mem::replace(&mut tasks[index], tmp_task)
};
match task.optimize() {
match task.try_optimize_tx_size() {
// If we can decrease:
// 1. Calculate new tx size & ix size
// 2. Insert item's data back in the heap
Expand Down Expand Up @@ -456,7 +459,7 @@ mod tests {
Box::new(create_test_commit_task(3, 1000)) as Box<dyn BaseTask>, // Larger task
];

let _ = TaskStrategist::optimize_strategy(&mut tasks);
let _ = TaskStrategist::try_optimize_tx_size_if_needed(&mut tasks);
// The larger task should have been optimized first
assert!(matches!(tasks[0].strategy(), TaskStrategy::Args));
assert!(matches!(tasks[1].strategy(), TaskStrategy::Buffer));
Expand Down
Loading