Skip to content

Commit 9f09261

Browse files
committed
Add one test and cleanup
1 parent ab51f90 commit 9f09261

File tree

5 files changed

+124
-61
lines changed

5 files changed

+124
-61
lines changed

magicblock-committor-service/src/tasks/args_task.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ impl BaseTask for ArgsTask {
7676
allow_undelegation: value.allow_undelegation,
7777
};
7878

79-
log::info!(
80-
"DIFF SIZE: {} / {}",
81-
args.diff.len(),
82-
value.base_account.data.len()
83-
);
84-
8579
dlp::instruction_builder::commit_diff(
8680
*validator,
8781
value.committed_account.pubkey,

magicblock-committor-service/src/tasks/buffer_task.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl From<ArgsTaskType> for BufferTaskType {
6969
fn from(value: ArgsTaskType) -> Self {
7070
match value {
7171
ArgsTaskType::Commit(task) => BufferTaskType::Commit(task),
72-
ArgsTaskType::CommitDiff(_) => panic!("BufferTask doesn't support CommitDiff yet. Disable your tests temporarily till the next PR"),
72+
ArgsTaskType::CommitDiff(_) => panic!("BufferTask doesn't support CommitDiff yet. Disable your tests (if any) temporarily till the next PR"),
7373
_ => unimplemented!("Only commit task can be BufferTask currently. Fix your tests"),
7474
}
7575
}

magicblock-committor-service/src/tasks/task_builder.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub struct TaskBuilderImpl;
4848
// for small accounts, which typically could hold up to 8 u32 fields or
4949
// 4 u64 fields. These integers are expected to be on the hot path
5050
// and updated continuously.
51-
const COMMIT_STATE_SIZE_THRESHOLD: usize = 256;
51+
pub const COMMIT_STATE_SIZE_THRESHOLD: usize = 256;
5252

5353
impl TaskBuilderImpl {
5454
pub async fn create_commit_task<C: TaskInfoFetcher>(
@@ -57,8 +57,6 @@ impl TaskBuilderImpl {
5757
account: CommittedAccount,
5858
task_info_fetcher: &Arc<C>,
5959
) -> ArgsTask {
60-
log::warn!("create_commit_task entered");
61-
6260
let base_account = if account.account.data.len()
6361
> COMMIT_STATE_SIZE_THRESHOLD
6462
{
@@ -80,19 +78,13 @@ impl TaskBuilderImpl {
8078
};
8179

8280
if let Some(base_account) = base_account {
83-
log::warn!(
84-
"create_commit_task: base_account: {:#?}, {:#?}",
85-
base_account,
86-
account.account
87-
);
8881
ArgsTaskType::CommitDiff(CommitDiffTask {
8982
commit_id,
9083
allow_undelegation,
9184
committed_account: account,
9285
base_account,
9386
})
9487
} else {
95-
log::warn!("create_commit_task: {:#?}", account.account);
9688
ArgsTaskType::Commit(CommitTask {
9789
commit_id,
9890
allow_undelegation,

magicblock-committor-service/src/tasks/task_strategist.rs

Lines changed: 121 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,6 @@ impl TaskStrategist {
289289

290290
// Get initial transaction size
291291
let mut current_tx_length = calculate_tx_length(tasks)?;
292-
log::info!(
293-
"optimze initial size: {} / limit {}",
294-
current_tx_length,
295-
MAX_ENCODED_TRANSACTION_SIZE
296-
);
297292

298293
if current_tx_length <= MAX_ENCODED_TRANSACTION_SIZE {
299294
return Ok(current_tx_length);
@@ -346,11 +341,6 @@ impl TaskStrategist {
346341
let new_ix_size =
347342
usize::try_from(new_ix_size).unwrap_or(usize::MAX);
348343
current_tx_length = calculate_tx_length(tasks)?;
349-
log::info!(
350-
"optimze updated size: {} / limit {}",
351-
current_tx_length,
352-
MAX_ENCODED_TRANSACTION_SIZE
353-
);
354344
map.push((new_ix_size, index));
355345
}
356346
// That means el-t can't be optimized further
@@ -401,12 +391,15 @@ mod tests {
401391
},
402392
persist::IntentPersisterImpl,
403393
tasks::{
404-
task_builder::{TaskBuilderImpl, TasksBuilder},
394+
task_builder::{
395+
TaskBuilderImpl, TasksBuilder, COMMIT_STATE_SIZE_THRESHOLD,
396+
},
405397
BaseActionTask, TaskStrategy, UndelegateTask,
406398
},
407399
};
408400

409-
struct MockInfoFetcher;
401+
#[derive(Default)]
402+
struct MockInfoFetcher(HashMap<Pubkey, Account>);
410403

411404
#[async_trait::async_trait]
412405
impl TaskInfoFetcher for MockInfoFetcher {
@@ -432,33 +425,54 @@ mod tests {
432425

433426
async fn get_base_account(
434427
&self,
435-
_pubkey: &Pubkey,
428+
pubkey: &Pubkey,
436429
) -> MagicBlockRpcClientResult<Option<Account>> {
437-
Ok(None) // Account Not Found
430+
Ok(self.0.get(pubkey).cloned())
438431
}
439432
}
440433

441434
// Helper to create a simple commit task
442435
async fn create_test_commit_task(
443436
commit_id: u64,
444437
data_size: usize,
438+
diff_len: usize,
445439
) -> ArgsTask {
446-
TaskBuilderImpl::create_commit_task(
447-
commit_id,
448-
false,
449-
CommittedAccount {
450-
pubkey: Pubkey::new_unique(),
451-
account: Account {
452-
lamports: 1000,
453-
data: vec![1; data_size],
454-
owner: system_program::id(),
455-
executable: false,
456-
rent_epoch: 0,
457-
},
440+
let committed_account = CommittedAccount {
441+
pubkey: Pubkey::new_unique(),
442+
account: Account {
443+
lamports: 1000,
444+
data: vec![1; data_size],
445+
owner: system_program::id(),
446+
executable: false,
447+
rent_epoch: 0,
458448
},
459-
&Arc::new(NullTaskInfoFetcher),
460-
)
461-
.await
449+
};
450+
451+
if diff_len == 0 {
452+
TaskBuilderImpl::create_commit_task(
453+
commit_id,
454+
false,
455+
committed_account,
456+
&Arc::new(NullTaskInfoFetcher),
457+
)
458+
.await
459+
} else {
460+
let originals: HashMap<Pubkey, Account> = {
461+
let mut acc = committed_account.account.clone();
462+
assert!(diff_len <= acc.data.len());
463+
for byte in &mut acc.data[..diff_len] {
464+
*byte = byte.wrapping_add(1);
465+
}
466+
[(committed_account.pubkey, acc)].iter().cloned().collect()
467+
};
468+
TaskBuilderImpl::create_commit_task(
469+
commit_id,
470+
false,
471+
committed_account,
472+
&Arc::new(MockInfoFetcher(originals)),
473+
)
474+
.await
475+
}
462476
}
463477

464478
// Helper to create a Base action task
@@ -496,7 +510,7 @@ mod tests {
496510
#[tokio::test]
497511
async fn test_build_strategy_with_single_small_task() {
498512
let validator = Pubkey::new_unique();
499-
let task = create_test_commit_task(1, 100).await;
513+
let task = create_test_commit_task(1, 100, 0).await;
500514
let tasks = vec![Box::new(task) as Box<dyn BaseTask>];
501515

502516
let strategy = TaskStrategist::build_strategy(
@@ -514,7 +528,7 @@ mod tests {
514528
async fn test_build_strategy_optimizes_to_buffer_when_needed() {
515529
let validator = Pubkey::new_unique();
516530

517-
let task = create_test_commit_task(1, 1000).await; // Large task
531+
let task = create_test_commit_task(1, 1000, 0).await; // Large task
518532
let tasks = vec![Box::new(task) as Box<dyn BaseTask>];
519533

520534
let strategy = TaskStrategist::build_strategy(
@@ -535,7 +549,7 @@ mod tests {
535549
async fn test_build_strategy_optimizes_to_buffer_u16_exceeded() {
536550
let validator = Pubkey::new_unique();
537551

538-
let task = create_test_commit_task(1, 66_000).await; // Large task
552+
let task = create_test_commit_task(1, 66_000, 0).await; // Large task
539553
let tasks = vec![Box::new(task) as Box<dyn BaseTask>];
540554

541555
let strategy = TaskStrategist::build_strategy(
@@ -552,6 +566,71 @@ mod tests {
552566
));
553567
}
554568

569+
#[tokio::test]
570+
async fn test_build_strategy_does_not_optimize_large_account_but_small_diff(
571+
) {
572+
let validator = Pubkey::new_unique();
573+
574+
let task =
575+
create_test_commit_task(1, 66_000, COMMIT_STATE_SIZE_THRESHOLD)
576+
.await; // large account but small diff
577+
let tasks = vec![Box::new(task) as Box<dyn BaseTask>];
578+
579+
let strategy = TaskStrategist::build_strategy(
580+
tasks,
581+
&validator,
582+
&None::<IntentPersisterImpl>,
583+
)
584+
.expect("Should build strategy with buffer optimization");
585+
586+
assert_eq!(strategy.optimized_tasks.len(), 1);
587+
assert_eq!(strategy.optimized_tasks[0].strategy(), TaskStrategy::Args);
588+
}
589+
590+
#[tokio::test]
591+
async fn test_build_strategy_does_not_optimize_large_account_and_above_threshold_diff(
592+
) {
593+
let validator = Pubkey::new_unique();
594+
595+
let task =
596+
create_test_commit_task(1, 66_000, COMMIT_STATE_SIZE_THRESHOLD + 1)
597+
.await; // large account but small diff
598+
let tasks = vec![Box::new(task) as Box<dyn BaseTask>];
599+
600+
let strategy = TaskStrategist::build_strategy(
601+
tasks,
602+
&validator,
603+
&None::<IntentPersisterImpl>,
604+
)
605+
.expect("Should build strategy with buffer optimization");
606+
607+
assert_eq!(strategy.optimized_tasks.len(), 1);
608+
assert_eq!(strategy.optimized_tasks[0].strategy(), TaskStrategy::Args);
609+
}
610+
611+
#[tokio::test]
612+
async fn test_build_strategy_does_optimize_large_account_and_large_diff() {
613+
let validator = Pubkey::new_unique();
614+
615+
let task =
616+
create_test_commit_task(1, 66_000, COMMIT_STATE_SIZE_THRESHOLD * 4)
617+
.await; // large account but small diff
618+
let tasks = vec![Box::new(task) as Box<dyn BaseTask>];
619+
620+
let strategy = TaskStrategist::build_strategy(
621+
tasks,
622+
&validator,
623+
&None::<IntentPersisterImpl>,
624+
)
625+
.expect("Should build strategy with buffer optimization");
626+
627+
assert_eq!(strategy.optimized_tasks.len(), 1);
628+
assert_eq!(
629+
strategy.optimized_tasks[0].strategy(),
630+
TaskStrategy::Buffer
631+
);
632+
}
633+
555634
#[tokio::test]
556635
async fn test_build_strategy_creates_multiple_buffers() {
557636
// TODO: ALSO MAX NUM WITH PURE BUFFER commits, no alts
@@ -560,7 +639,7 @@ mod tests {
560639
let validator = Pubkey::new_unique();
561640

562641
let tasks = join_all((0..NUM_COMMITS).map(|i| async move {
563-
let task = create_test_commit_task(i, 500).await; // Large task
642+
let task = create_test_commit_task(i, 500, 0).await; // Large task
564643
Box::new(task) as Box<dyn BaseTask>
565644
}))
566645
.await;
@@ -587,7 +666,7 @@ mod tests {
587666

588667
let tasks = join_all((0..NUM_COMMITS).map(|i| async move {
589668
// Large task
590-
let task = create_test_commit_task(i, 10000).await;
669+
let task = create_test_commit_task(i, 10000, 0).await;
591670
Box::new(task) as Box<dyn BaseTask>
592671
}))
593672
.await;
@@ -613,7 +692,7 @@ mod tests {
613692

614693
let tasks = join_all((0..NUM_COMMITS).map(|i| async move {
615694
// Large task
616-
let task = create_test_commit_task(i, 1000).await;
695+
let task = create_test_commit_task(i, 1000, 0).await;
617696
Box::new(task) as Box<dyn BaseTask>
618697
}))
619698
.await;
@@ -629,11 +708,11 @@ mod tests {
629708
#[tokio::test]
630709
async fn test_optimize_strategy_prioritizes_largest_tasks() {
631710
let mut tasks = [
632-
Box::new(create_test_commit_task(1, 100).await)
711+
Box::new(create_test_commit_task(1, 100, 0).await)
633712
as Box<dyn BaseTask>,
634-
Box::new(create_test_commit_task(2, 1000).await)
713+
Box::new(create_test_commit_task(2, 1000, 0).await)
635714
as Box<dyn BaseTask>, // Larger task
636-
Box::new(create_test_commit_task(3, 1000).await)
715+
Box::new(create_test_commit_task(3, 1000, 0).await)
637716
as Box<dyn BaseTask>, // Larger task
638717
];
639718

@@ -647,7 +726,7 @@ mod tests {
647726
async fn test_mixed_task_types_with_optimization() {
648727
let validator = Pubkey::new_unique();
649728
let tasks = vec![
650-
Box::new(create_test_commit_task(1, 1000).await)
729+
Box::new(create_test_commit_task(1, 1000, 0).await)
651730
as Box<dyn BaseTask>,
652731
Box::new(create_test_finalize_task()) as Box<dyn BaseTask>,
653732
Box::new(create_test_base_action_task(500)) as Box<dyn BaseTask>,
@@ -689,7 +768,7 @@ mod tests {
689768
let pubkey = [Pubkey::new_unique()];
690769
let intent = create_test_intent(0, &pubkey, false);
691770

692-
let info_fetcher = Arc::new(MockInfoFetcher);
771+
let info_fetcher = Arc::new(MockInfoFetcher::default());
693772
let commit_task = TaskBuilderImpl::commit_tasks(
694773
&info_fetcher,
695774
&intent,
@@ -721,7 +800,7 @@ mod tests {
721800
let pubkeys: [_; 3] = std::array::from_fn(|_| Pubkey::new_unique());
722801
let intent = create_test_intent(0, &pubkeys, true);
723802

724-
let info_fetcher = Arc::new(MockInfoFetcher);
803+
let info_fetcher = Arc::new(MockInfoFetcher::default());
725804
let commit_task = TaskBuilderImpl::commit_tasks(
726805
&info_fetcher,
727806
&intent,
@@ -758,7 +837,7 @@ mod tests {
758837
let pubkeys: [_; 8] = std::array::from_fn(|_| Pubkey::new_unique());
759838
let intent = create_test_intent(0, &pubkeys, false);
760839

761-
let info_fetcher = Arc::new(MockInfoFetcher);
840+
let info_fetcher = Arc::new(MockInfoFetcher::default());
762841
let commit_task = TaskBuilderImpl::commit_tasks(
763842
&info_fetcher,
764843
&intent,

test-integration/test-committor-service/tests/utils/transactions.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub async fn init_and_delegate_account_on_chain(
235235
(pda, pda_acc)
236236
}
237237

238-
/// This needs to be run for each test that required a new counter to be delegated
238+
/// This needs to be run for each test that required a new order_book to be delegated
239239
#[allow(dead_code)]
240240
pub async fn init_and_delegate_order_book_on_chain(
241241
payer: &Keypair,
@@ -246,7 +246,6 @@ pub async fn init_and_delegate_order_book_on_chain(
246246
.request_airdrop(&payer.pubkey(), 777 * LAMPORTS_PER_SOL)
247247
.await
248248
.unwrap();
249-
debug!("Airdropped to counter auth: {} SOL", 777 * LAMPORTS_PER_SOL);
250249

251250
let InitOrderBookAndDelegateIxs {
252251
init,
@@ -292,7 +291,6 @@ pub async fn init_and_delegate_order_book_on_chain(
292291
)
293292
.await
294293
.expect("Failed to delegate");
295-
// debug!("Delegated account: {:?}", pda);
296294

297295
let order_book_acc = get_account!(rpc_client, order_book, "order_book");
298296

0 commit comments

Comments
 (0)