Skip to content

Commit 5e9c354

Browse files
committed
Use existing style of tasks
1 parent 22ab70b commit 5e9c354

File tree

5 files changed

+113
-148
lines changed

5 files changed

+113
-148
lines changed

magicblock-committor-service/src/intent_execution_manager/intent_execution_engine.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,6 @@ mod tests {
371371

372372
use async_trait::async_trait;
373373
use magicblock_program::magic_scheduled_base_intent::ScheduledBaseIntent;
374-
use magicblock_rpc_client::MagicBlockRpcClientResult;
375-
use solana_account::Account;
376374
use solana_pubkey::{pubkey, Pubkey};
377375
use solana_signature::Signature;
378376
use solana_signer::SignerError;

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

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
use dlp::args::CallHandlerArgs;
1+
use dlp::{
2+
args::{CallHandlerArgs, CommitDiffArgs, CommitStateArgs},
3+
compute_diff,
4+
};
25
use magicblock_metrics::metrics::LabelValue;
6+
use solana_account::ReadableAccount;
37
use solana_instruction::{AccountMeta, Instruction};
48
use solana_pubkey::Pubkey;
59

@@ -8,14 +12,15 @@ use crate::tasks::TaskStrategy;
812
use crate::tasks::{
913
buffer_task::{BufferTask, BufferTaskType},
1014
visitor::Visitor,
11-
BaseActionTask, BaseTask, BaseTaskError, BaseTaskResult, CommitTask,
12-
FinalizeTask, PreparationState, TaskType, UndelegateTask,
15+
BaseActionTask, BaseTask, BaseTaskError, BaseTaskResult, CommitDiffTask,
16+
CommitTask, FinalizeTask, PreparationState, TaskType, UndelegateTask,
1317
};
1418

1519
/// Task that will be executed on Base layer via arguments
1620
#[derive(Clone)]
1721
pub enum ArgsTaskType {
1822
Commit(CommitTask),
23+
CommitDiff(CommitDiffTask),
1924
Finalize(FinalizeTask),
2025
Undelegate(UndelegateTask), // Special action really
2126
BaseAction(BaseActionTask),
@@ -45,7 +50,39 @@ impl ArgsTask {
4550
impl BaseTask for ArgsTask {
4651
fn instruction(&self, validator: &Pubkey) -> Instruction {
4752
match &self.task_type {
48-
ArgsTaskType::Commit(value) => value.create_commit_ix(validator),
53+
ArgsTaskType::Commit(value) => {
54+
let args = CommitStateArgs {
55+
nonce: value.commit_id,
56+
lamports: value.committed_account.account.lamports,
57+
data: value.committed_account.account.data.clone(),
58+
allow_undelegation: value.allow_undelegation,
59+
};
60+
dlp::instruction_builder::commit_state(
61+
*validator,
62+
value.committed_account.pubkey,
63+
value.committed_account.account.owner,
64+
args,
65+
)
66+
}
67+
ArgsTaskType::CommitDiff(value) => {
68+
let args = CommitDiffArgs {
69+
nonce: value.commit_id,
70+
lamports: value.committed_account.account.lamports,
71+
diff: compute_diff(
72+
value.base_account.data(),
73+
value.committed_account.account.data(),
74+
)
75+
.to_vec(),
76+
allow_undelegation: value.allow_undelegation,
77+
};
78+
79+
dlp::instruction_builder::commit_diff(
80+
*validator,
81+
value.committed_account.pubkey,
82+
value.committed_account.account.owner,
83+
args,
84+
)
85+
}
4986
ArgsTaskType::Finalize(value) => {
5087
dlp::instruction_builder::finalize(
5188
*validator,
@@ -89,21 +126,22 @@ impl BaseTask for ArgsTask {
89126
self: Box<Self>,
90127
) -> Result<Box<dyn BaseTask>, Box<dyn BaseTask>> {
91128
match self.task_type {
92-
ArgsTaskType::Commit(mut value) if value.is_commit_diff() => {
93-
// TODO (snawaz): Currently, we do not support executing CommitDiff
94-
// as BufferTask, which is why we're forcing CommitTask to use CommitState
95-
// before converting this task into BufferTask. Once CommitDiff is supported
96-
// by BufferTask, we do not have to force_commit_state and we can remove
97-
// force_commit_state stuff, as it's essentially a downgrade.
98-
99-
value.force_commit_state();
129+
ArgsTaskType::Commit(value) => {
100130
Ok(Box::new(BufferTask::new_preparation_required(
101131
BufferTaskType::Commit(value),
102132
)))
103133
}
104-
ArgsTaskType::Commit(value) => {
134+
ArgsTaskType::CommitDiff(value) => {
135+
// TODO (snawaz): Currently, we do not support executing CommitDiff
136+
// as BufferTask, which is why we're forcing CommitDiffTask to become CommitTask
137+
// before converting this task into BufferTask. Once CommitDiff is supported
138+
// by BufferTask, we do not have to do this, as it's essentially a downgrade.
105139
Ok(Box::new(BufferTask::new_preparation_required(
106-
BufferTaskType::Commit(value),
140+
BufferTaskType::Commit(CommitTask {
141+
commit_id: value.commit_id,
142+
allow_undelegation: value.allow_undelegation,
143+
committed_account: value.committed_account,
144+
}),
107145
)))
108146
}
109147
ArgsTaskType::BaseAction(_)
@@ -132,6 +170,7 @@ impl BaseTask for ArgsTask {
132170
fn compute_units(&self) -> u32 {
133171
match &self.task_type {
134172
ArgsTaskType::Commit(_) => 70_000,
173+
ArgsTaskType::CommitDiff(_) => 70_000,
135174
ArgsTaskType::BaseAction(task) => task.action.compute_units,
136175
ArgsTaskType::Undelegate(_) => 70_000,
137176
ArgsTaskType::Finalize(_) => 70_000,
@@ -146,6 +185,7 @@ impl BaseTask for ArgsTask {
146185
fn task_type(&self) -> TaskType {
147186
match &self.task_type {
148187
ArgsTaskType::Commit(_) => TaskType::Commit,
188+
ArgsTaskType::CommitDiff(_) => TaskType::Commit,
149189
ArgsTaskType::BaseAction(_) => TaskType::Action,
150190
ArgsTaskType::Undelegate(_) => TaskType::Undelegate,
151191
ArgsTaskType::Finalize(_) => TaskType::Finalize,
@@ -170,6 +210,7 @@ impl LabelValue for ArgsTask {
170210
fn value(&self) -> &str {
171211
match self.task_type {
172212
ArgsTaskType::Commit(_) => "args_commit",
213+
ArgsTaskType::CommitDiff(_) => "args_commit_diff",
173214
ArgsTaskType::BaseAction(_) => "args_action",
174215
ArgsTaskType::Finalize(_) => "args_finalize",
175216
ArgsTaskType::Undelegate(_) => "args_undelegate",

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

Lines changed: 22 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
use std::sync::Arc;
2-
3-
use dlp::{
4-
args::{CommitDiffArgs, CommitStateArgs},
5-
compute_diff,
6-
};
71
use dyn_clone::DynClone;
82
use magicblock_committor_program::{
93
instruction_builder::{
@@ -20,15 +14,12 @@ use magicblock_metrics::metrics::LabelValue;
2014
use magicblock_program::magic_scheduled_base_intent::{
2115
BaseAction, CommittedAccount,
2216
};
23-
use solana_account::{Account, ReadableAccount};
17+
use solana_account::Account;
2418
use solana_instruction::Instruction;
2519
use solana_pubkey::Pubkey;
2620
use thiserror::Error;
2721

28-
use crate::{
29-
intent_executor::task_info_fetcher::TaskInfoFetcher,
30-
tasks::visitor::Visitor,
31-
};
22+
use crate::tasks::visitor::Visitor;
3223

3324
pub mod args_task;
3425
pub mod buffer_task;
@@ -64,8 +55,6 @@ pub enum TaskStrategy {
6455
pub trait BaseTask: Send + Sync + DynClone + LabelValue {
6556
/// Gets all pubkeys that involved in Task's instruction
6657
fn involved_accounts(&self, validator: &Pubkey) -> Vec<Pubkey> {
67-
// TODO (snawaz): rewrite it.
68-
// currently it is slow as it discards heavy computations and memory allocations.
6958
self.instruction(validator)
7059
.accounts
7160
.iter()
@@ -115,78 +104,14 @@ pub struct CommitTask {
115104
pub commit_id: u64,
116105
pub allow_undelegation: bool,
117106
pub committed_account: CommittedAccount,
118-
base_account: Option<Account>,
119-
force_commit_state: bool,
120107
}
121108

122-
impl CommitTask {
123-
// Accounts larger than COMMIT_STATE_SIZE_THRESHOLD, use CommitDiff to
124-
// reduce instruction size. Below this, commit is sent as CommitState.
125-
// Chose 256 as thresold seems good enough as it could hold 8 u32 fields
126-
// or 4 u64 fields.
127-
const COMMIT_STATE_SIZE_THRESHOLD: usize = 256;
128-
129-
pub fn is_commit_diff(&self) -> bool {
130-
!self.force_commit_state
131-
&& self.committed_account.account.data.len()
132-
> Self::COMMIT_STATE_SIZE_THRESHOLD
133-
&& self.base_account.is_some()
134-
}
135-
136-
pub fn force_commit_state(&mut self) {
137-
self.force_commit_state = true;
138-
}
139-
140-
pub fn create_commit_ix(&self, validator: &Pubkey) -> Instruction {
141-
if let Some(fetched_account) = self.base_account.as_ref() {
142-
self.create_commit_diff_ix(validator, fetched_account)
143-
} else {
144-
self.create_commit_state_ix(validator)
145-
}
146-
}
147-
148-
fn create_commit_state_ix(&self, validator: &Pubkey) -> Instruction {
149-
let args = CommitStateArgs {
150-
nonce: self.commit_id,
151-
lamports: self.committed_account.account.lamports,
152-
data: self.committed_account.account.data.clone(),
153-
allow_undelegation: self.allow_undelegation,
154-
};
155-
dlp::instruction_builder::commit_state(
156-
*validator,
157-
self.committed_account.pubkey,
158-
self.committed_account.account.owner,
159-
args,
160-
)
161-
}
162-
163-
fn create_commit_diff_ix(
164-
&self,
165-
validator: &Pubkey,
166-
fetched_account: &Account,
167-
) -> Instruction {
168-
if self.force_commit_state {
169-
return self.create_commit_state_ix(validator);
170-
}
171-
172-
let args = CommitDiffArgs {
173-
nonce: self.commit_id,
174-
lamports: self.committed_account.account.lamports,
175-
diff: compute_diff(
176-
fetched_account.data(),
177-
self.committed_account.account.data(),
178-
)
179-
.to_vec(),
180-
allow_undelegation: self.allow_undelegation,
181-
};
182-
183-
dlp::instruction_builder::commit_diff(
184-
*validator,
185-
self.committed_account.pubkey,
186-
self.committed_account.account.owner,
187-
args,
188-
)
189-
}
109+
#[derive(Clone)]
110+
pub struct CommitDiffTask {
111+
pub commit_id: u64,
112+
pub allow_undelegation: bool,
113+
pub committed_account: CommittedAccount,
114+
pub base_account: Account,
190115
}
191116

192117
#[derive(Clone)]
@@ -397,25 +322,22 @@ mod serialization_safety_test {
397322
let validator = Pubkey::new_unique();
398323

399324
// Test Commit variant
400-
let commit_task: ArgsTask = ArgsTaskType::Commit(
401-
TaskBuilderImpl::create_commit_task(
402-
123,
403-
true,
404-
CommittedAccount {
405-
pubkey: Pubkey::new_unique(),
406-
account: Account {
407-
lamports: 1000,
408-
data: vec![1, 2, 3],
409-
owner: Pubkey::new_unique(),
410-
executable: false,
411-
rent_epoch: 0,
412-
},
325+
let commit_task: ArgsTask = TaskBuilderImpl::create_commit_task(
326+
123,
327+
true,
328+
CommittedAccount {
329+
pubkey: Pubkey::new_unique(),
330+
account: Account {
331+
lamports: 1000,
332+
data: vec![1, 2, 3],
333+
owner: Pubkey::new_unique(),
334+
executable: false,
335+
rent_epoch: 0,
413336
},
414-
&Arc::new(NullTaskInfoFetcher),
415-
)
416-
.await,
337+
},
338+
&Arc::new(NullTaskInfoFetcher),
417339
)
418-
.into();
340+
.await;
419341
assert_serializable(&commit_task.instruction(&validator));
420342

421343
// Test Finalize variant

0 commit comments

Comments
 (0)