Skip to content

Commit 646ab8b

Browse files
committed
feat: Execute CommitDiff as BufferTask
1 parent 54a1a4c commit 646ab8b

File tree

6 files changed

+102
-66
lines changed

6 files changed

+102
-66
lines changed

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,9 @@ impl BaseTask for ArgsTask {
8989
self: Box<Self>,
9090
) -> Result<Box<dyn BaseTask>, Box<dyn BaseTask>> {
9191
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();
100-
Ok(Box::new(BufferTask::new_preparation_required(
101-
BufferTaskType::Commit(value),
102-
)))
103-
}
10492
ArgsTaskType::Commit(value) => {
10593
Ok(Box::new(BufferTask::new_preparation_required(
106-
BufferTaskType::Commit(value),
94+
BufferTaskType::Commit(value.switch_to_buffer_strategy()),
10795
)))
10896
}
10997
ArgsTaskType::BaseAction(_)

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

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use dlp::args::CommitStateFromBufferArgs;
21
use magicblock_committor_program::Chunks;
32
use magicblock_metrics::metrics::LabelValue;
43
use solana_instruction::Instruction;
@@ -47,16 +46,18 @@ impl BufferTask {
4746

4847
fn preparation_required(task_type: &BufferTaskType) -> PreparationState {
4948
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-
);
49+
let state_or_diff = if let Some(diff) = commit_task.compute_diff() {
50+
diff.to_vec()
51+
} else {
52+
commit_task.committed_account.account.data.clone()
53+
};
54+
let chunks =
55+
Chunks::from_data_length(state_or_diff.len(), MAX_WRITE_CHUNK_SIZE);
5556

5657
PreparationState::Required(PreparationTask {
5758
commit_id: commit_task.commit_id,
5859
pubkey: commit_task.committed_account.pubkey,
59-
committed_data,
60+
committed_data: state_or_diff,
6061
chunks,
6162
})
6263
}
@@ -65,31 +66,15 @@ impl BufferTask {
6566
impl BaseTask for BufferTask {
6667
fn instruction(&self, validator: &Pubkey) -> Instruction {
6768
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-
)
69+
value.create_commit_ix(validator)
8770
}
8871

8972
/// No further optimizations
9073
fn optimize(
9174
self: Box<Self>,
9275
) -> Result<Box<dyn BaseTask>, Box<dyn BaseTask>> {
76+
// Since the buffer in BufferTask doesn't contribute to the size of
77+
// transaction, there is nothing we can do here to optimize/reduce the size.
9378
Err(self)
9479
}
9580

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

Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::sync::Arc;
22

33
use dlp::{
4-
args::{CommitDiffArgs, CommitStateArgs},
4+
args::{CommitDiffArgs, CommitStateArgs, CommitStateFromBufferArgs},
55
compute_diff,
66
};
77
use dyn_clone::DynClone;
@@ -53,7 +53,6 @@ pub enum PreparationState {
5353
Cleanup(CleanupTask),
5454
}
5555

56-
#[cfg(test)]
5756
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
5857
pub enum TaskStrategy {
5958
Args,
@@ -153,7 +152,7 @@ impl CommitTaskBuilder {
153152
allow_undelegation,
154153
committed_account,
155154
base_account,
156-
force_commit_state: false,
155+
strategy: TaskStrategy::Args,
157156
}
158157
}
159158
}
@@ -164,29 +163,46 @@ pub struct CommitTask {
164163
pub allow_undelegation: bool,
165164
pub committed_account: CommittedAccount,
166165
base_account: Option<Account>,
167-
force_commit_state: bool,
166+
strategy: TaskStrategy,
168167
}
169168

170169
impl CommitTask {
171-
pub fn is_commit_diff(&self) -> bool {
172-
!self.force_commit_state
173-
&& self.committed_account.account.data.len()
174-
> CommitTaskBuilder::COMMIT_STATE_SIZE_THRESHOLD
175-
&& self.base_account.is_some()
176-
}
177-
178-
pub fn force_commit_state(&mut self) {
179-
self.force_commit_state = true;
170+
pub fn switch_to_buffer_strategy(mut self) -> Self {
171+
self.strategy = TaskStrategy::Buffer;
172+
self
180173
}
181174

182175
pub fn create_commit_ix(&self, validator: &Pubkey) -> Instruction {
183-
if let Some(fetched_account) = self.base_account.as_ref() {
184-
self.create_commit_diff_ix(validator, fetched_account)
185-
} else {
186-
self.create_commit_state_ix(validator)
176+
match self.strategy {
177+
TaskStrategy::Args => {
178+
if let Some(base_account) = self.base_account.as_ref() {
179+
self.create_commit_diff_ix(validator, base_account)
180+
} else {
181+
self.create_commit_state_ix(validator)
182+
}
183+
}
184+
TaskStrategy::Buffer => {
185+
if let Some(base_account) = self.base_account.as_ref() {
186+
self.create_commit_diff_from_buffer_ix(
187+
validator,
188+
base_account,
189+
)
190+
} else {
191+
self.create_commit_state_from_buffer_ix(validator)
192+
}
193+
}
187194
}
188195
}
189196

197+
pub fn compute_diff(&self) -> Option<dlp::rkyv::AlignedVec> {
198+
self.base_account.as_ref().map(|base_account| {
199+
compute_diff(
200+
base_account.data(),
201+
self.committed_account.account.data(),
202+
)
203+
})
204+
}
205+
190206
fn create_commit_state_ix(&self, validator: &Pubkey) -> Instruction {
191207
let args = CommitStateArgs {
192208
nonce: self.commit_id,
@@ -205,17 +221,13 @@ impl CommitTask {
205221
fn create_commit_diff_ix(
206222
&self,
207223
validator: &Pubkey,
208-
fetched_account: &Account,
224+
base_account: &Account,
209225
) -> Instruction {
210-
if self.force_commit_state {
211-
return self.create_commit_state_ix(validator);
212-
}
213-
214226
let args = CommitDiffArgs {
215227
nonce: self.commit_id,
216228
lamports: self.committed_account.account.lamports,
217229
diff: compute_diff(
218-
fetched_account.data(),
230+
base_account.data(),
219231
self.committed_account.account.data(),
220232
)
221233
.to_vec(),
@@ -229,6 +241,57 @@ impl CommitTask {
229241
args,
230242
)
231243
}
244+
245+
fn create_commit_state_from_buffer_ix(
246+
&self,
247+
validator: &Pubkey,
248+
) -> Instruction {
249+
let commit_id_slice = self.commit_id.to_le_bytes();
250+
let (commit_buffer_pubkey, _) =
251+
magicblock_committor_program::pdas::buffer_pda(
252+
validator,
253+
&self.committed_account.pubkey,
254+
&commit_id_slice,
255+
);
256+
257+
dlp::instruction_builder::commit_state_from_buffer(
258+
*validator,
259+
self.committed_account.pubkey,
260+
self.committed_account.account.owner,
261+
commit_buffer_pubkey,
262+
CommitStateFromBufferArgs {
263+
nonce: self.commit_id,
264+
lamports: self.committed_account.account.lamports,
265+
allow_undelegation: self.allow_undelegation,
266+
},
267+
)
268+
}
269+
270+
fn create_commit_diff_from_buffer_ix(
271+
&self,
272+
validator: &Pubkey,
273+
_fetched_account: &Account,
274+
) -> Instruction {
275+
let commit_id_slice = self.commit_id.to_le_bytes();
276+
let (commit_buffer_pubkey, _) =
277+
magicblock_committor_program::pdas::buffer_pda(
278+
validator,
279+
&self.committed_account.pubkey,
280+
&commit_id_slice,
281+
);
282+
283+
dlp::instruction_builder::commit_diff_from_buffer(
284+
*validator,
285+
self.committed_account.pubkey,
286+
self.committed_account.account.owner,
287+
commit_buffer_pubkey,
288+
CommitStateFromBufferArgs {
289+
nonce: self.commit_id,
290+
lamports: self.committed_account.account.lamports,
291+
allow_undelegation: self.allow_undelegation,
292+
},
293+
)
294+
}
232295
}
233296

234297
#[derive(Clone)]

test-integration/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ magicblock-config = { path = "../magicblock-config" }
5757
magicblock-core = { path = "../magicblock-core" }
5858
magic-domain-program = { git = "https://github.com/magicblock-labs/magic-domain-program.git", rev = "ea04d46", default-features = false }
5959
magicblock_magic_program_api = { package = "magicblock-magic-program-api", path = "../magicblock-magic-program-api" }
60-
magicblock-delegation-program = { git = "https://github.com/magicblock-labs/delegation-program.git", rev = "e8d03936", features = [
61-
"no-entrypoint",
60+
magicblock-delegation-program = { git = "https://github.com/magicblock-labs/delegation-program.git", rev = "ea1f2f916268132248fe8d5de5f07d76765dd937", features = [
61+
"no-entrypoint",
6262
] }
6363
magicblock-program = { path = "../programs/magicblock" }
6464
magicblock-rpc-client = { path = "../magicblock-rpc-client" }
968 Bytes
Binary file not shown.

test-integration/schedulecommit/test-scenarios/tests/02_commit_and_undelegate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,13 @@ fn test_committing_and_undelegating_huge_order_book_account() {
303303
println!("Important: use {rng_seed} as seed to regenerate the random inputs in case of test failure");
304304
let mut random = StdRng::seed_from_u64(rng_seed);
305305
let mut update = BookUpdate::default();
306-
update.bids.extend((0..random.gen_range(5..10)).map(|_| {
306+
update.bids.extend((0..random.gen_range(5..100)).map(|_| {
307307
OrderLevel {
308308
price: random.gen_range(75000..90000),
309309
size: random.gen_range(1..10),
310310
}
311311
}));
312-
update.asks.extend((0..random.gen_range(5..10)).map(|_| {
312+
update.asks.extend((0..random.gen_range(5..100)).map(|_| {
313313
OrderLevel {
314314
price: random.gen_range(125000..150000),
315315
size: random.gen_range(1..10),

0 commit comments

Comments
 (0)