Skip to content

Commit 18adf1e

Browse files
committed
feat: Execute CommitDiff as BufferTask
1 parent 9da0edd commit 18adf1e

File tree

8 files changed

+108
-66
lines changed

8 files changed

+108
-66
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ magicblock-config-helpers = { path = "./magicblock-config-helpers" }
109109
magicblock-config-macro = { path = "./magicblock-config-macro" }
110110
magicblock-core = { path = "./magicblock-core" }
111111
magicblock-aperture = { path = "./magicblock-aperture" }
112-
magicblock-delegation-program = { git = "https://github.com/magicblock-labs/delegation-program.git", rev = "e8d03936", features = [
113-
"no-entrypoint",
112+
magicblock-delegation-program = { git = "https://github.com/magicblock-labs/delegation-program.git", rev = "ea1f2f916268132248fe8d5de5f07d76765dd937", features = [
113+
"no-entrypoint",
114114
] }
115115
magicblock-geyser-plugin = { path = "./magicblock-geyser-plugin" }
116116
magicblock-ledger = { path = "./magicblock-ledger" }

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,9 @@ impl BaseTask for ArgsTask {
8888
self: Box<Self>,
8989
) -> Result<Box<dyn BaseTask>, Box<dyn BaseTask>> {
9090
match self.task_type {
91-
ArgsTaskType::Commit(mut value) if value.is_commit_diff() => {
92-
// TODO (snawaz): Currently, we do not support executing CommitDiff
93-
// as BufferTask, which is why we're forcing CommitTask to use CommitState
94-
// before converting this task into BufferTask Once CommitDiff is supported
95-
// by BufferTask, we do not have to force_commit_state and we can remove
96-
// force_commit_state stuff, as it's essentially a downgrade.
97-
98-
value.force_commit_state();
99-
Ok(Box::new(BufferTask::new_preparation_required(
100-
BufferTaskType::Commit(value),
101-
)))
102-
}
10391
ArgsTaskType::Commit(value) => {
10492
Ok(Box::new(BufferTask::new_preparation_required(
105-
BufferTaskType::Commit(value),
93+
BufferTaskType::Commit(value.switch_to_buffer_strategy()),
10694
)))
10795
}
10896
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 solana_pubkey::Pubkey;
43
use solana_sdk::instruction::Instruction;
@@ -46,16 +45,18 @@ impl BufferTask {
4645

4746
fn preparation_required(task_type: &BufferTaskType) -> PreparationState {
4847
let BufferTaskType::Commit(ref commit_task) = task_type;
49-
let committed_data = commit_task.committed_account.account.data.clone();
50-
let chunks = Chunks::from_data_length(
51-
committed_data.len(),
52-
MAX_WRITE_CHUNK_SIZE,
53-
);
48+
let state_or_diff = if let Some(diff) = commit_task.compute_diff() {
49+
diff.to_vec()
50+
} else {
51+
commit_task.committed_account.account.data.clone()
52+
};
53+
let chunks =
54+
Chunks::from_data_length(state_or_diff.len(), MAX_WRITE_CHUNK_SIZE);
5455

5556
PreparationState::Required(PreparationTask {
5657
commit_id: commit_task.commit_id,
5758
pubkey: commit_task.committed_account.pubkey,
58-
committed_data,
59+
committed_data: state_or_diff,
5960
chunks,
6061
})
6162
}
@@ -64,31 +65,15 @@ impl BufferTask {
6465
impl BaseTask for BufferTask {
6566
fn instruction(&self, validator: &Pubkey) -> Instruction {
6667
let BufferTaskType::Commit(ref value) = self.task_type;
67-
let commit_id_slice = value.commit_id.to_le_bytes();
68-
let (commit_buffer_pubkey, _) =
69-
magicblock_committor_program::pdas::buffer_pda(
70-
validator,
71-
&value.committed_account.pubkey,
72-
&commit_id_slice,
73-
);
74-
75-
dlp::instruction_builder::commit_state_from_buffer(
76-
*validator,
77-
value.committed_account.pubkey,
78-
value.committed_account.account.owner,
79-
commit_buffer_pubkey,
80-
CommitStateFromBufferArgs {
81-
nonce: value.commit_id,
82-
lamports: value.committed_account.account.lamports,
83-
allow_undelegation: value.allow_undelegation,
84-
},
85-
)
68+
value.create_commit_ix(validator)
8669
}
8770

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

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

Lines changed: 88 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use account_fetcher::*;
22
use dlp::{
3-
args::{CommitDiffArgs, CommitStateArgs},
3+
args::{CommitDiffArgs, CommitStateArgs, CommitStateFromBufferArgs},
44
compute_diff,
55
};
66
use dyn_clone::DynClone;
@@ -50,7 +50,6 @@ pub enum PreparationState {
5050
Cleanup(CleanupTask),
5151
}
5252

53-
#[cfg(test)]
5453
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
5554
pub enum TaskStrategy {
5655
Args,
@@ -113,7 +112,7 @@ pub struct CommitTask {
113112
pub allow_undelegation: bool,
114113
pub committed_account: CommittedAccount,
115114
base_account: Option<Account>,
116-
force_commit_state: bool,
115+
strategy: TaskStrategy,
117116
}
118117

119118
impl CommitTask {
@@ -157,29 +156,52 @@ impl CommitTask {
157156
allow_undelegation,
158157
committed_account,
159158
base_account,
160-
force_commit_state: false,
159+
strategy: TaskStrategy::Args,
161160
}
162161
}
163162

164163
pub fn is_commit_diff(&self) -> bool {
165-
!self.force_commit_state
166-
&& self.committed_account.account.data.len()
167-
> CommitTask::COMMIT_STATE_SIZE_THRESHOLD
164+
self.committed_account.account.data.len()
165+
> CommitTask::COMMIT_STATE_SIZE_THRESHOLD
168166
&& self.base_account.is_some()
169167
}
170168

171-
pub fn force_commit_state(&mut self) {
172-
self.force_commit_state = true;
169+
pub fn switch_to_buffer_strategy(mut self) -> Self {
170+
self.strategy = TaskStrategy::Buffer;
171+
self
173172
}
174173

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

196+
pub fn compute_diff(&self) -> Option<dlp::rkyv::AlignedVec> {
197+
self.base_account.as_ref().map(|base_account| {
198+
compute_diff(
199+
base_account.data(),
200+
self.committed_account.account.data(),
201+
)
202+
})
203+
}
204+
183205
fn create_commit_state_ix(&self, validator: &Pubkey) -> Instruction {
184206
let args = CommitStateArgs {
185207
nonce: self.commit_id,
@@ -198,17 +220,13 @@ impl CommitTask {
198220
fn create_commit_diff_ix(
199221
&self,
200222
validator: &Pubkey,
201-
fetched_account: &Account,
223+
base_account: &Account,
202224
) -> Instruction {
203-
if self.force_commit_state {
204-
return self.create_commit_state_ix(validator);
205-
}
206-
207225
let args = CommitDiffArgs {
208226
nonce: self.commit_id,
209227
lamports: self.committed_account.account.lamports,
210228
diff: compute_diff(
211-
fetched_account.data(),
229+
base_account.data(),
212230
self.committed_account.account.data(),
213231
)
214232
.to_vec(),
@@ -222,6 +240,57 @@ impl CommitTask {
222240
args,
223241
)
224242
}
243+
244+
fn create_commit_state_from_buffer_ix(
245+
&self,
246+
validator: &Pubkey,
247+
) -> Instruction {
248+
let commit_id_slice = self.commit_id.to_le_bytes();
249+
let (commit_buffer_pubkey, _) =
250+
magicblock_committor_program::pdas::buffer_pda(
251+
validator,
252+
&self.committed_account.pubkey,
253+
&commit_id_slice,
254+
);
255+
256+
dlp::instruction_builder::commit_state_from_buffer(
257+
*validator,
258+
self.committed_account.pubkey,
259+
self.committed_account.account.owner,
260+
commit_buffer_pubkey,
261+
CommitStateFromBufferArgs {
262+
nonce: self.commit_id,
263+
lamports: self.committed_account.account.lamports,
264+
allow_undelegation: self.allow_undelegation,
265+
},
266+
)
267+
}
268+
269+
fn create_commit_diff_from_buffer_ix(
270+
&self,
271+
validator: &Pubkey,
272+
_fetched_account: &Account,
273+
) -> Instruction {
274+
let commit_id_slice = self.commit_id.to_le_bytes();
275+
let (commit_buffer_pubkey, _) =
276+
magicblock_committor_program::pdas::buffer_pda(
277+
validator,
278+
&self.committed_account.pubkey,
279+
&commit_id_slice,
280+
);
281+
282+
dlp::instruction_builder::commit_diff_from_buffer(
283+
*validator,
284+
self.committed_account.pubkey,
285+
self.committed_account.account.owner,
286+
commit_buffer_pubkey,
287+
CommitStateFromBufferArgs {
288+
nonce: self.commit_id,
289+
lamports: self.committed_account.account.lamports,
290+
allow_undelegation: self.allow_undelegation,
291+
},
292+
)
293+
}
225294
}
226295

227296
#[derive(Clone)]

test-integration/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.

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" }

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
@@ -253,13 +253,13 @@ fn test_committing_and_undelegating_huge_order_book_account() {
253253
println!("Important: use {rng_seed} as seed to regenerate the random inputs in case of test failure");
254254
let mut random = StdRng::seed_from_u64(rng_seed);
255255
let mut update = BookUpdate::default();
256-
update.bids.extend((0..random.gen_range(5..10)).map(|_| {
256+
update.bids.extend((0..random.gen_range(5..100)).map(|_| {
257257
OrderLevel {
258258
price: random.gen_range(75000..90000),
259259
size: random.gen_range(1..10),
260260
}
261261
}));
262-
update.asks.extend((0..random.gen_range(5..10)).map(|_| {
262+
update.asks.extend((0..random.gen_range(5..100)).map(|_| {
263263
OrderLevel {
264264
price: random.gen_range(125000..150000),
265265
size: random.gen_range(1..10),

0 commit comments

Comments
 (0)