Skip to content

Commit 171b9dc

Browse files
committed
feat: Execute CommitDiff as BufferTask
1 parent f4d2a03 commit 171b9dc

File tree

11 files changed

+190
-96
lines changed

11 files changed

+190
-96
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ magicblock-committor-program = { path = "./magicblock-committor-program", featur
9595
magicblock-committor-service = { path = "./magicblock-committor-service" }
9696
magicblock-config = { path = "./magicblock-config" }
9797
magicblock-core = { path = "./magicblock-core" }
98-
magicblock-delegation-program = { git = "https://github.com/magicblock-labs/delegation-program.git", rev = "e8d03936", features = [
98+
magicblock-delegation-program = { git = "https://github.com/magicblock-labs/delegation-program.git", rev = "ea1f2f916268132248fe8d5de5f07d76765dd937", features = [
9999
"no-entrypoint",
100100
] }
101101
magicblock-geyser-plugin = { path = "./magicblock-geyser-plugin" }

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,8 @@ impl BaseTask for ArgsTask {
132132
)))
133133
}
134134
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.
139135
Ok(Box::new(BufferTask::new_preparation_required(
140-
BufferTaskType::Commit(CommitTask {
141-
commit_id: value.commit_id,
142-
allow_undelegation: value.allow_undelegation,
143-
committed_account: value.committed_account,
144-
}),
136+
BufferTaskType::CommitDiff(value),
145137
)))
146138
}
147139
ArgsTaskType::BaseAction(_)

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

Lines changed: 99 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use dlp::args::CommitStateFromBufferArgs;
1+
use dlp::{args::CommitStateFromBufferArgs, compute_diff};
22
use magicblock_committor_program::Chunks;
33
use magicblock_metrics::metrics::LabelValue;
44
use solana_instruction::Instruction;
@@ -9,15 +9,17 @@ use crate::tasks::TaskStrategy;
99
use crate::{
1010
consts::MAX_WRITE_CHUNK_SIZE,
1111
tasks::{
12-
visitor::Visitor, BaseTask, BaseTaskError, BaseTaskResult, CommitTask,
13-
PreparationState, PreparationTask, TaskType,
12+
visitor::Visitor, BaseTask, BaseTaskError, BaseTaskResult,
13+
CommitDiffTask, CommitTask, PreparationState, PreparationTask,
14+
TaskType,
1415
},
1516
};
1617

1718
/// Tasks that could be executed using buffers
1819
#[derive(Clone)]
1920
pub enum BufferTaskType {
2021
Commit(CommitTask),
22+
CommitDiff(CommitDiffTask),
2123
// Action in the future
2224
}
2325

@@ -46,50 +48,94 @@ impl BufferTask {
4648
}
4749

4850
fn preparation_required(task_type: &BufferTaskType) -> PreparationState {
49-
let BufferTaskType::Commit(ref commit_task) = task_type;
50-
let committed_data = commit_task.committed_account.account.data.clone();
51-
let chunks = Chunks::from_data_length(
52-
committed_data.len(),
53-
MAX_WRITE_CHUNK_SIZE,
54-
);
55-
56-
PreparationState::Required(PreparationTask {
57-
commit_id: commit_task.commit_id,
58-
pubkey: commit_task.committed_account.pubkey,
59-
committed_data,
60-
chunks,
61-
})
51+
match task_type {
52+
BufferTaskType::Commit(task) => {
53+
let data = task.committed_account.account.data.clone();
54+
let chunks =
55+
Chunks::from_data_length(data.len(), MAX_WRITE_CHUNK_SIZE);
56+
57+
PreparationState::Required(PreparationTask {
58+
commit_id: task.commit_id,
59+
pubkey: task.committed_account.pubkey,
60+
committed_data: data,
61+
chunks,
62+
})
63+
}
64+
65+
BufferTaskType::CommitDiff(task) => {
66+
let diff = compute_diff(
67+
&task.committed_account.account.data,
68+
&task.base_account.data,
69+
)
70+
.to_vec();
71+
let chunks =
72+
Chunks::from_data_length(diff.len(), MAX_WRITE_CHUNK_SIZE);
73+
74+
PreparationState::Required(PreparationTask {
75+
commit_id: task.commit_id,
76+
pubkey: task.committed_account.pubkey,
77+
committed_data: diff,
78+
chunks,
79+
})
80+
}
81+
}
6282
}
6383
}
6484

6585
impl BaseTask for BufferTask {
6686
fn instruction(&self, validator: &Pubkey) -> Instruction {
67-
let BufferTaskType::Commit(ref value) = self.task_type;
68-
let commit_id_slice = value.commit_id.to_le_bytes();
69-
let (commit_buffer_pubkey, _) =
70-
magicblock_committor_program::pdas::buffer_pda(
71-
validator,
72-
&value.committed_account.pubkey,
73-
&commit_id_slice,
74-
);
75-
76-
dlp::instruction_builder::commit_state_from_buffer(
77-
*validator,
78-
value.committed_account.pubkey,
79-
value.committed_account.account.owner,
80-
commit_buffer_pubkey,
81-
CommitStateFromBufferArgs {
82-
nonce: value.commit_id,
83-
lamports: value.committed_account.account.lamports,
84-
allow_undelegation: value.allow_undelegation,
85-
},
86-
)
87+
match &self.task_type {
88+
BufferTaskType::Commit(task) => {
89+
let commit_id_slice = task.commit_id.to_le_bytes();
90+
let (commit_buffer_pubkey, _) =
91+
magicblock_committor_program::pdas::buffer_pda(
92+
validator,
93+
&task.committed_account.pubkey,
94+
&commit_id_slice,
95+
);
96+
97+
dlp::instruction_builder::commit_state_from_buffer(
98+
*validator,
99+
task.committed_account.pubkey,
100+
task.committed_account.account.owner,
101+
commit_buffer_pubkey,
102+
CommitStateFromBufferArgs {
103+
nonce: task.commit_id,
104+
lamports: task.committed_account.account.lamports,
105+
allow_undelegation: task.allow_undelegation,
106+
},
107+
)
108+
}
109+
BufferTaskType::CommitDiff(task) => {
110+
let commit_id_slice = task.commit_id.to_le_bytes();
111+
let (commit_buffer_pubkey, _) =
112+
magicblock_committor_program::pdas::buffer_pda(
113+
validator,
114+
&task.committed_account.pubkey,
115+
&commit_id_slice,
116+
);
117+
118+
dlp::instruction_builder::commit_diff_from_buffer(
119+
*validator,
120+
task.committed_account.pubkey,
121+
task.committed_account.account.owner,
122+
commit_buffer_pubkey,
123+
CommitStateFromBufferArgs {
124+
nonce: task.commit_id,
125+
lamports: task.committed_account.account.lamports,
126+
allow_undelegation: task.allow_undelegation,
127+
},
128+
)
129+
}
130+
}
87131
}
88132

89133
/// No further optimizations
90134
fn optimize(
91135
self: Box<Self>,
92136
) -> Result<Box<dyn BaseTask>, Box<dyn BaseTask>> {
137+
// Since the buffer in BufferTask doesn't contribute to the size of
138+
// transaction, there is nothing we can do here to optimize/reduce the size.
93139
Err(self)
94140
}
95141

@@ -112,6 +158,7 @@ impl BaseTask for BufferTask {
112158
fn compute_units(&self) -> u32 {
113159
match self.task_type {
114160
BufferTaskType::Commit(_) => 70_000,
161+
BufferTaskType::CommitDiff(_) => 70_000,
115162
}
116163
}
117164

@@ -123,6 +170,7 @@ impl BaseTask for BufferTask {
123170
fn task_type(&self) -> TaskType {
124171
match self.task_type {
125172
BufferTaskType::Commit(_) => TaskType::Commit,
173+
BufferTaskType::CommitDiff(_) => TaskType::Commit,
126174
}
127175
}
128176

@@ -132,12 +180,21 @@ impl BaseTask for BufferTask {
132180
}
133181

134182
fn reset_commit_id(&mut self, commit_id: u64) {
135-
let BufferTaskType::Commit(commit_task) = &mut self.task_type;
136-
if commit_id == commit_task.commit_id {
137-
return;
138-
}
183+
match &mut self.task_type {
184+
BufferTaskType::Commit(commit_task) => {
185+
if commit_id == commit_task.commit_id {
186+
return;
187+
}
188+
commit_task.commit_id = commit_id;
189+
}
190+
BufferTaskType::CommitDiff(task) => {
191+
if commit_id == task.commit_id {
192+
return;
193+
}
194+
task.commit_id = commit_id;
195+
}
196+
};
139197

140-
commit_task.commit_id = commit_id;
141198
self.preparation_state = Self::preparation_required(&self.task_type)
142199
}
143200
}
@@ -146,6 +203,7 @@ impl LabelValue for BufferTask {
146203
fn value(&self) -> &str {
147204
match self.task_type {
148205
BufferTaskType::Commit(_) => "buffer_commit",
206+
BufferTaskType::CommitDiff(_) => "buffer_commit_diff",
149207
}
150208
}
151209
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ pub enum PreparationState {
4444
Cleanup(CleanupTask),
4545
}
4646

47-
#[cfg(test)]
4847
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
4948
pub enum TaskStrategy {
5049
Args,

magicblock-committor-service/src/tasks/task_visitors/persistor_visitor.rs

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,40 @@ where
2626
fn visit_args_task(&mut self, task: &ArgsTask) {
2727
match self.context {
2828
PersistorContext::PersistStrategy { uses_lookup_tables } => {
29-
let ArgsTaskType::Commit(ref commit_task) = task.task_type
30-
else {
31-
return;
32-
};
33-
3429
let commit_strategy = if uses_lookup_tables {
3530
CommitStrategy::ArgsWithLookupTable
3631
} else {
3732
CommitStrategy::Args
3833
};
3934

40-
if let Err(err) = self.persistor.set_commit_strategy(
41-
commit_task.commit_id,
42-
&commit_task.committed_account.pubkey,
43-
commit_strategy,
44-
) {
45-
error!(
46-
"Failed to persist commit strategy {}: {}",
47-
commit_strategy.as_str(),
48-
err
49-
);
35+
match &task.task_type {
36+
ArgsTaskType::Commit(task) => {
37+
if let Err(err) = self.persistor.set_commit_strategy(
38+
task.commit_id,
39+
&task.committed_account.pubkey,
40+
commit_strategy,
41+
) {
42+
error!(
43+
"Failed to persist commit strategy {}: {}",
44+
commit_strategy.as_str(),
45+
err
46+
);
47+
}
48+
}
49+
ArgsTaskType::CommitDiff(task) => {
50+
if let Err(err) = self.persistor.set_commit_strategy(
51+
task.commit_id,
52+
&task.committed_account.pubkey,
53+
commit_strategy,
54+
) {
55+
error!(
56+
"Failed to persist commit strategy {}: {}",
57+
commit_strategy.as_str(),
58+
err
59+
);
60+
}
61+
}
62+
_ => {}
5063
}
5164
}
5265
}
@@ -55,23 +68,39 @@ where
5568
fn visit_buffer_task(&mut self, task: &BufferTask) {
5669
match self.context {
5770
PersistorContext::PersistStrategy { uses_lookup_tables } => {
58-
let BufferTaskType::Commit(ref commit_task) = task.task_type;
5971
let commit_strategy = if uses_lookup_tables {
6072
CommitStrategy::FromBufferWithLookupTable
6173
} else {
6274
CommitStrategy::FromBuffer
6375
};
6476

65-
if let Err(err) = self.persistor.set_commit_strategy(
66-
commit_task.commit_id,
67-
&commit_task.committed_account.pubkey,
68-
commit_strategy,
69-
) {
70-
error!(
71-
"Failed to persist commit strategy {}: {}",
72-
commit_strategy.as_str(),
73-
err
74-
);
77+
match &task.task_type {
78+
BufferTaskType::Commit(task) => {
79+
if let Err(err) = self.persistor.set_commit_strategy(
80+
task.commit_id,
81+
&task.committed_account.pubkey,
82+
commit_strategy,
83+
) {
84+
error!(
85+
"Failed to persist commit strategy {}: {}",
86+
commit_strategy.as_str(),
87+
err
88+
);
89+
}
90+
}
91+
BufferTaskType::CommitDiff(task) => {
92+
if let Err(err) = self.persistor.set_commit_strategy(
93+
task.commit_id,
94+
&task.committed_account.pubkey,
95+
commit_strategy,
96+
) {
97+
error!(
98+
"Failed to persist commit strategy {}: {}",
99+
commit_strategy.as_str(),
100+
err
101+
);
102+
}
103+
}
75104
}
76105
}
77106
}

0 commit comments

Comments
 (0)