Skip to content

Commit 267ae58

Browse files
committed
Make unit-tests work again
1 parent e44fc81 commit 267ae58

File tree

6 files changed

+85
-56
lines changed

6 files changed

+85
-56
lines changed

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,7 @@ impl ArgsTask {
4444
impl BaseTask for ArgsTask {
4545
fn instruction(&self, validator: &Pubkey) -> Instruction {
4646
match &self.task_type {
47-
ArgsTaskType::Commit(value) => {
48-
if value.is_commit_diff() {
49-
value.create_commit_diff_ix(validator)
50-
} else {
51-
value.create_commit_state_ix(validator)
52-
}
53-
}
47+
ArgsTaskType::Commit(value) => value.create_commit_ix(validator),
5448
ArgsTaskType::Finalize(value) => {
5549
dlp::instruction_builder::finalize(
5650
*validator,

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

Lines changed: 70 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use magicblock_committor_program::{
1717
use magicblock_program::magic_scheduled_base_intent::{
1818
BaseAction, CommittedAccount,
1919
};
20-
use solana_account::ReadableAccount;
20+
use solana_account::{Account, ReadableAccount};
2121
use solana_pubkey::Pubkey;
2222
use solana_rpc_client::rpc_client::RpcClient;
2323
use solana_sdk::{
@@ -114,61 +114,93 @@ pub struct CommitTask {
114114
pub commit_id: u64,
115115
pub allow_undelegation: bool,
116116
pub committed_account: CommittedAccount,
117+
fetched_account: Option<Account>,
117118
}
118119

119120
impl CommitTask {
120121
const COMMIT_STATE_SIZE_THRESHOLD: usize = 200;
121122

123+
pub fn new(
124+
commit_id: u64,
125+
allow_undelegation: bool,
126+
committed_account: CommittedAccount,
127+
) -> Self {
128+
let chain_config =
129+
ChainConfig::local(ComputeBudgetConfig::new(1_000_000));
130+
131+
let rpc_client = RpcClient::new_with_commitment(
132+
chain_config.rpc_uri.to_string(),
133+
CommitmentConfig {
134+
commitment: chain_config.commitment,
135+
},
136+
);
137+
138+
let fetched_account = if committed_account.account.data.len()
139+
> CommitTask::COMMIT_STATE_SIZE_THRESHOLD
140+
{
141+
rpc_client.get_account(&committed_account.pubkey).ok()
142+
} else {
143+
None
144+
};
145+
146+
Self {
147+
commit_id,
148+
allow_undelegation,
149+
committed_account,
150+
fetched_account,
151+
}
152+
}
153+
154+
// TODO (snawaz): it is infinitely bad implementation
155+
// as it's making a network call, but we'll fix it soon once
156+
// we start using caching and fetched accounts.
122157
pub fn is_commit_diff(&self) -> bool {
123158
self.committed_account.account.data.len()
124159
> CommitTask::COMMIT_STATE_SIZE_THRESHOLD
160+
&& self.fetched_account.is_some()
125161
}
126162

127-
pub fn create_commit_state_ix(&self, validator: &Pubkey) -> Instruction {
163+
pub fn create_commit_ix(&self, validator: &Pubkey) -> Instruction {
164+
if let Some(fetched_account) = self.fetched_account.as_ref() {
165+
self.create_commit_diff_ix(validator, fetched_account)
166+
} else {
167+
self.create_commit_state_ix(validator)
168+
}
169+
}
170+
171+
fn create_commit_state_ix(&self, validator: &Pubkey) -> Instruction {
128172
let args = CommitStateArgs {
129173
nonce: self.commit_id,
130174
lamports: self.committed_account.account.lamports,
131175
data: self.committed_account.account.data.clone(),
132176
allow_undelegation: self.allow_undelegation,
133177
};
178+
println!("create_commit_state_ix, data: {}", args.data.len());
134179
dlp::instruction_builder::commit_state(
135180
*validator,
136181
self.committed_account.pubkey,
137182
self.committed_account.account.owner,
138183
args,
139184
)
140185
}
141-
pub fn create_commit_diff_ix(&self, validator: &Pubkey) -> Instruction {
142-
let chain_config =
143-
ChainConfig::local(ComputeBudgetConfig::new(1_000_000));
144-
145-
let rpc_client = RpcClient::new_with_commitment(
146-
chain_config.rpc_uri.to_string(),
147-
CommitmentConfig {
148-
commitment: chain_config.commitment,
149-
},
150-
);
151-
152-
let account = match rpc_client
153-
.get_account(&self.committed_account.pubkey)
154-
{
155-
Ok(account) => account,
156-
Err(e) => {
157-
log::warn!("Fallback to commit_state and send full-bytes, as rpc failed to fetch the delegated-account from base chain, commmit_id: {} , error: {}", self.commit_id, e);
158-
return self.create_commit_state_ix(validator);
159-
}
160-
};
161186

187+
fn create_commit_diff_ix(
188+
&self,
189+
validator: &Pubkey,
190+
fetched_account: &Account,
191+
) -> Instruction {
162192
let args = CommitDiffArgs {
163193
nonce: self.commit_id,
164194
lamports: self.committed_account.account.lamports,
165195
diff: compute_diff(
166-
account.data(),
196+
fetched_account.data(),
167197
self.committed_account.account.data(),
168198
)
169199
.to_vec(),
170200
allow_undelegation: self.allow_undelegation,
171201
};
202+
println!("create_commit_diff_ix, diff: {}", args.diff.len());
203+
172204
dlp::instruction_builder::commit_diff(
173205
*validator,
174206
self.committed_account.pubkey,
@@ -382,10 +414,10 @@ mod serialization_safety_test {
382414
let validator = Pubkey::new_unique();
383415

384416
// Test Commit variant
385-
let commit_task: ArgsTask = ArgsTaskType::Commit(CommitTask {
386-
commit_id: 123,
387-
allow_undelegation: true,
388-
committed_account: CommittedAccount {
417+
let commit_task: ArgsTask = ArgsTaskType::Commit(CommitTask::new(
418+
123,
419+
true,
420+
CommittedAccount {
389421
pubkey: Pubkey::new_unique(),
390422
account: Account {
391423
lamports: 1000,
@@ -395,7 +427,7 @@ mod serialization_safety_test {
395427
rent_epoch: 0,
396428
},
397429
},
398-
})
430+
))
399431
.into();
400432
assert_serializable(&commit_task.instruction(&validator));
401433

@@ -442,10 +474,10 @@ mod serialization_safety_test {
442474
let validator = Pubkey::new_unique();
443475

444476
let buffer_task = BufferTask::new_preparation_required(
445-
BufferTaskType::Commit(CommitTask {
446-
commit_id: 456,
447-
allow_undelegation: false,
448-
committed_account: CommittedAccount {
477+
BufferTaskType::Commit(CommitTask::new(
478+
456,
479+
false,
480+
CommittedAccount {
449481
pubkey: Pubkey::new_unique(),
450482
account: Account {
451483
lamports: 2000,
@@ -455,7 +487,7 @@ mod serialization_safety_test {
455487
rent_epoch: 0,
456488
},
457489
},
458-
}),
490+
)),
459491
);
460492
assert_serializable(&buffer_task.instruction(&validator));
461493
}
@@ -467,10 +499,10 @@ mod serialization_safety_test {
467499

468500
// Test BufferTask preparation
469501
let buffer_task = BufferTask::new_preparation_required(
470-
BufferTaskType::Commit(CommitTask {
471-
commit_id: 789,
472-
allow_undelegation: true,
473-
committed_account: CommittedAccount {
502+
BufferTaskType::Commit(CommitTask::new(
503+
789,
504+
true,
505+
CommittedAccount {
474506
pubkey: Pubkey::new_unique(),
475507
account: Account {
476508
lamports: 3000,
@@ -480,7 +512,7 @@ mod serialization_safety_test {
480512
rent_epoch: 0,
481513
},
482514
},
483-
}),
515+
)),
484516
);
485517

486518
let PreparationState::Required(preparation_task) =

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ impl TasksBuilder for TaskBuilderImpl {
8989
.iter()
9090
.map(|account| {
9191
let commit_id = *commit_ids.get(&account.pubkey).expect("CommitIdFetcher provide commit ids for all listed pubkeys, or errors!");
92-
let task = ArgsTaskType::Commit(CommitTask {
92+
let task = ArgsTaskType::Commit(CommitTask::new(
9393
commit_id,
9494
allow_undelegation,
95-
committed_account: account.clone(),
96-
});
95+
account.clone(),
96+
));
9797

9898
Box::new(ArgsTask::new(task)) as Box<dyn BaseTask>
9999
})

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl TaskStrategist {
9595
lookup_tables_keys,
9696
})
9797
} else {
98+
println!("snawaz: inside build_strategy");
9899
Err(TaskStrategistError::FailedToFitError)
99100
}
100101
}
@@ -165,7 +166,10 @@ impl TaskStrategist {
165166
&[],
166167
) {
167168
Ok(tx) => Ok(serialize_and_encode_base64(&tx).len()),
168-
Err(TaskStrategistError::FailedToFitError) => Ok(usize::MAX),
169+
Err(TaskStrategistError::FailedToFitError) => {
170+
println!("snawaz: inside optimize_strategy");
171+
Ok(usize::MAX)
172+
}
169173
Err(TaskStrategistError::SignerError(err)) => Err(err),
170174
}
171175
};
@@ -264,10 +268,10 @@ mod tests {
264268

265269
// Helper to create a simple commit task
266270
fn create_test_commit_task(commit_id: u64, data_size: usize) -> ArgsTask {
267-
ArgsTask::new(ArgsTaskType::Commit(CommitTask {
271+
ArgsTask::new(ArgsTaskType::Commit(CommitTask::new(
268272
commit_id,
269-
allow_undelegation: false,
270-
committed_account: CommittedAccount {
273+
false,
274+
CommittedAccount {
271275
pubkey: Pubkey::new_unique(),
272276
account: Account {
273277
lamports: 1000,
@@ -277,7 +281,7 @@ mod tests {
277281
rent_epoch: 0,
278282
},
279283
},
280-
}))
284+
)))
281285
}
282286

283287
// Helper to create a Base action task

test-integration/programs/schedulecommit-security/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use borsh::{BorshDeserialize, BorshSerialize};
2-
use ephemeral_rollups_sdk::ephem::{create_schedule_commit_ix, CommitPolicy};
2+
use ephemeral_rollups_sdk::ephem::create_schedule_commit_ix;
33
use program_schedulecommit::{
44
api::schedule_commit_cpi_instruction, process_schedulecommit_cpi,
55
ProcessSchedulecommitCpiArgs,
@@ -146,7 +146,6 @@ fn process_sibling_schedule_cpis(
146146
magic_context,
147147
magic_program,
148148
false,
149-
CommitPolicy::UseFullBytes,
150149
);
151150
invoke(
152151
&direct_ix,

test-integration/programs/schedulecommit/src/order_book.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<'a> OrderBook<'a> {
204204
) -> &'a mut [OrderLevel] {
205205
slice::from_raw_parts_mut(
206206
self.levels.add(self.capacity - asks_len),
207-
asks_len as usize,
207+
asks_len,
208208
)
209209
}
210210

0 commit comments

Comments
 (0)