Skip to content

Commit 2a10bab

Browse files
committed
feat: Add ScheduleCommitFinalize along with CommitFinalizeTask
1 parent 34585f0 commit 2a10bab

28 files changed

Lines changed: 1360 additions & 334 deletions

File tree

Cargo.lock

Lines changed: 91 additions & 68 deletions
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
@@ -102,7 +102,7 @@ magicblock-committor-program = { path = "./magicblock-committor-program", featur
102102
magicblock-committor-service = { path = "./magicblock-committor-service" }
103103
magicblock-config = { path = "./magicblock-config" }
104104
magicblock-core = { path = "./magicblock-core" }
105-
magicblock-delegation-program = { git = "https://github.com/magicblock-labs/delegation-program.git", rev = "2cb491032f372", features = [
105+
magicblock-delegation-program = { git = "https://github.com/magicblock-labs/delegation-program.git", branch = "snawaz/commit-finalize-from-buffer", features = [
106106
"no-entrypoint",
107107
] }
108108
magicblock-ledger = { path = "./magicblock-ledger" }

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl metrics::LabelValue for ExecutionOutput {
8383
}
8484
}
8585

86+
#[derive(Debug)]
8687
pub struct IntentExecutionResult {
8788
/// Final result of Intent Execution
8889
pub inner: IntentExecutorResult<ExecutionOutput>,
@@ -161,6 +162,8 @@ where
161162

162163
if all_committed_pubkeys.is_empty() {
163164
// Build tasks for commit stage
165+
// TODO (snawaz): it's actually MagicBaseIntent::BaseActionse scenario, not Commit
166+
// scenario. The related code needs little bit of refactoring.
164167
let commit_tasks = TaskBuilderImpl::commit_tasks(
165168
&self.task_info_fetcher,
166169
&intent_bundle,

magicblock-committor-service/src/persist/commit_persister.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ impl IntentPersisterImpl {
136136
[
137137
(false, intent_bundle.get_commit_intent_accounts()),
138138
(true, intent_bundle.get_undelegate_intent_accounts()),
139+
(false, intent_bundle.get_commit_finalize_intent_accounts()),
140+
(
141+
true,
142+
intent_bundle
143+
.get_commit_finalize_and_undelegate_intent_accounts(),
144+
),
139145
]
140146
.into_iter()
141147
.filter_map(|(undelegate, accounts)| {
@@ -494,6 +500,8 @@ mod tests {
494500
MagicIntentBundle {
495501
commit: Some(CommitType::Standalone(test_accounts())),
496502
commit_and_undelegate: None,
503+
commit_finalize: None,
504+
commit_finalize_and_undelegate: None,
497505
standalone_actions: vec![],
498506
}
499507
}
@@ -505,6 +513,8 @@ mod tests {
505513
commit_action: CommitType::Standalone(test_accounts()),
506514
undelegate_action: UndelegateType::Standalone,
507515
}),
516+
commit_finalize: None,
517+
commit_finalize_and_undelegate: None,
508518
standalone_actions: vec![],
509519
}
510520
}
@@ -516,6 +526,8 @@ mod tests {
516526
commit_action: CommitType::Standalone(test_accounts()),
517527
undelegate_action: UndelegateType::Standalone,
518528
}),
529+
commit_finalize: None,
530+
commit_finalize_and_undelegate: None,
519531
standalone_actions: vec![],
520532
}
521533
}
@@ -524,6 +536,8 @@ mod tests {
524536
MagicIntentBundle {
525537
commit: None,
526538
commit_and_undelegate: None,
539+
commit_finalize: None,
540+
commit_finalize_and_undelegate: None,
527541
standalone_actions: vec![],
528542
}
529543
}

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

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use dlp::{
2-
args::{CommitDiffArgs, CommitStateArgs},
2+
args::{CommitDiffArgs, CommitFinalizeArgs, CommitStateArgs},
33
compute_diff,
44
instruction_builder::{
55
call_handler_size_budget, call_handler_v2_size_budget,
6-
commit_diff_size_budget, commit_size_budget, finalize_size_budget,
7-
undelegate_size_budget,
6+
commit_diff_size_budget, commit_finalize_size_budget,
7+
commit_size_budget, finalize_size_budget, undelegate_size_budget,
88
},
99
AccountSizeClass,
1010
};
@@ -19,15 +19,16 @@ use crate::tasks::{
1919
buffer_task::{BufferTask, BufferTaskType},
2020
visitor::Visitor,
2121
BaseActionTask, BaseActionV2Task, BaseTask, BaseTaskError, BaseTaskResult,
22-
CommitDiffTask, CommitTask, FinalizeTask, PreparationState, TaskType,
23-
UndelegateTask,
22+
CommitDiffTask, CommitFinalizeTask, CommitTask, FinalizeTask,
23+
PreparationState, TaskType, UndelegateTask,
2424
};
2525

2626
/// Task that will be executed on Base layer via arguments
2727
#[derive(Clone)]
2828
pub enum ArgsTaskType {
2929
Commit(CommitTask),
3030
CommitDiff(CommitDiffTask),
31+
CommitFinalize(CommitFinalizeTask),
3132
Finalize(FinalizeTask),
3233
Undelegate(UndelegateTask), // Special action really
3334
BaseAction(BaseActionTask),
@@ -95,6 +96,38 @@ impl BaseTask for ArgsTask {
9596
args,
9697
)
9798
}
99+
ArgsTaskType::CommitFinalize(task) => {
100+
let (data, data_is_diff) =
101+
if let Some(base_account) = &task.base_account {
102+
(
103+
compute_diff(
104+
base_account.data(),
105+
task.committed_account.account.data(),
106+
)
107+
.to_vec(),
108+
true,
109+
)
110+
} else {
111+
(task.committed_account.account.data.clone(), false)
112+
};
113+
114+
let mut args = CommitFinalizeArgs {
115+
commit_id: task.commit_id,
116+
lamports: task.committed_account.account.lamports,
117+
data_is_diff: data_is_diff.into(),
118+
allow_undelegation: task.allow_undelegation.into(),
119+
bumps: Default::default(),
120+
reserved_padding: Default::default(),
121+
};
122+
123+
dlp::instruction_builder::commit_finalize(
124+
*validator,
125+
task.committed_account.pubkey,
126+
&mut args,
127+
&data,
128+
)
129+
.0
130+
}
98131
ArgsTaskType::Finalize(value) => {
99132
dlp::instruction_builder::finalize(
100133
*validator,
@@ -111,6 +144,8 @@ impl BaseTask for ArgsTask {
111144
}
112145
ArgsTaskType::BaseAction(value) => {
113146
let action = &value.action;
147+
148+
#[allow(deprecated)]
114149
dlp::instruction_builder::call_handler(
115150
*validator,
116151
action.destination_program,
@@ -147,6 +182,11 @@ impl BaseTask for ArgsTask {
147182
BufferTaskType::CommitDiff(value),
148183
)))
149184
}
185+
ArgsTaskType::CommitFinalize(value) => {
186+
Ok(Box::new(BufferTask::new_preparation_required(
187+
BufferTaskType::CommitFinalize(value),
188+
)))
189+
}
150190
ArgsTaskType::BaseAction(_)
151191
| ArgsTaskType::BaseActionV2(_)
152192
| ArgsTaskType::Finalize(_)
@@ -175,6 +215,7 @@ impl BaseTask for ArgsTask {
175215
match &self.task_type {
176216
ArgsTaskType::Commit(_) => 70_000,
177217
ArgsTaskType::CommitDiff(_) => 70_000,
218+
ArgsTaskType::CommitFinalize(_) => 70_000,
178219
ArgsTaskType::BaseAction(task) => task.action.compute_units,
179220
ArgsTaskType::BaseActionV2(task) => task.action.compute_units,
180221
ArgsTaskType::Undelegate(_) => 70_000,
@@ -194,6 +235,11 @@ impl BaseTask for ArgsTask {
194235
task.committed_account.account.data.len() as u32,
195236
))
196237
}
238+
ArgsTaskType::CommitFinalize(task) => {
239+
commit_finalize_size_budget(AccountSizeClass::Dynamic(
240+
task.committed_account.account.data.len() as u32,
241+
))
242+
}
197243
ArgsTaskType::BaseAction(task) => {
198244
// assume all other accounts are Small accounts.
199245
let other_accounts_budget =
@@ -235,6 +281,7 @@ impl BaseTask for ArgsTask {
235281
match &self.task_type {
236282
ArgsTaskType::Commit(_) => TaskType::Commit,
237283
ArgsTaskType::CommitDiff(_) => TaskType::Commit,
284+
ArgsTaskType::CommitFinalize(_) => TaskType::CommitFinalize,
238285
ArgsTaskType::BaseAction(_) | ArgsTaskType::BaseActionV2(_) => {
239286
TaskType::Action
240287
}
@@ -256,6 +303,9 @@ impl BaseTask for ArgsTask {
256303
ArgsTaskType::CommitDiff(task) => {
257304
task.commit_id = commit_id;
258305
}
306+
ArgsTaskType::CommitFinalize(task) => {
307+
task.commit_id = commit_id;
308+
}
259309
ArgsTaskType::BaseAction(_)
260310
| ArgsTaskType::BaseActionV2(_)
261311
| ArgsTaskType::Finalize(_)
@@ -269,6 +319,7 @@ impl LabelValue for ArgsTask {
269319
match self.task_type {
270320
ArgsTaskType::Commit(_) => "args_commit",
271321
ArgsTaskType::CommitDiff(_) => "args_commit_diff",
322+
ArgsTaskType::CommitFinalize(_) => "args_commit_finalize",
272323
ArgsTaskType::BaseAction(_) => "args_action",
273324
ArgsTaskType::BaseActionV2(_) => "args_action_v2",
274325
ArgsTaskType::Finalize(_) => "args_finalize",

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

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
use dlp::{
2-
args::CommitStateFromBufferArgs,
2+
args::{CommitFinalizeArgs, CommitStateFromBufferArgs},
33
compute_diff,
4-
instruction_builder::{commit_diff_size_budget, commit_size_budget},
4+
instruction_builder::{
5+
commit_diff_size_budget, commit_finalize_from_buffer_size_budget,
6+
commit_size_budget,
7+
},
58
AccountSizeClass,
69
};
710
use magicblock_committor_program::Chunks;
811
use magicblock_metrics::metrics::LabelValue;
12+
use solana_account::ReadableAccount;
913
use solana_instruction::Instruction;
1014
use solana_pubkey::Pubkey;
1115

@@ -17,8 +21,8 @@ use crate::{
1721
consts::MAX_WRITE_CHUNK_SIZE,
1822
tasks::{
1923
visitor::Visitor, BaseTask, BaseTaskError, BaseTaskResult,
20-
CommitDiffTask, CommitTask, PreparationState, PreparationTask,
21-
TaskType,
24+
CommitDiffTask, CommitFinalizeTask, CommitTask, PreparationState,
25+
PreparationTask, TaskType,
2226
},
2327
};
2428

@@ -27,6 +31,7 @@ use crate::{
2731
pub enum BufferTaskType {
2832
Commit(CommitTask),
2933
CommitDiff(CommitDiffTask),
34+
CommitFinalize(CommitFinalizeTask),
3035
// Action in the future
3136
}
3237

@@ -85,6 +90,28 @@ impl BufferTask {
8590
chunks,
8691
})
8792
}
93+
94+
BufferTaskType::CommitFinalize(task) => {
95+
let data = if let Some(base_account) = &task.base_account {
96+
compute_diff(
97+
base_account.data(),
98+
task.committed_account.account.data(),
99+
)
100+
.to_vec()
101+
} else {
102+
task.committed_account.account.data.clone()
103+
};
104+
105+
let chunks =
106+
Chunks::from_data_length(data.len(), MAX_WRITE_CHUNK_SIZE);
107+
108+
PreparationState::Required(PreparationTask {
109+
commit_id: task.commit_id,
110+
pubkey: task.committed_account.pubkey,
111+
committed_data: data,
112+
chunks,
113+
})
114+
}
88115
}
89116
}
90117
}
@@ -151,6 +178,32 @@ impl BaseTask for BufferTask {
151178
},
152179
)
153180
}
181+
182+
BufferTaskType::CommitFinalize(task) => {
183+
let (data_buffer_pubkey, _) =
184+
magicblock_committor_program::pdas::buffer_pda(
185+
validator,
186+
&task.committed_account.pubkey,
187+
&task.commit_id.to_le_bytes(),
188+
);
189+
190+
let mut args = CommitFinalizeArgs {
191+
commit_id: task.commit_id,
192+
lamports: task.committed_account.account.lamports,
193+
data_is_diff: task.base_account.is_some().into(),
194+
allow_undelegation: task.allow_undelegation.into(),
195+
bumps: Default::default(),
196+
reserved_padding: Default::default(),
197+
};
198+
199+
dlp::instruction_builder::commit_finalize_from_buffer(
200+
*validator,
201+
task.committed_account.pubkey,
202+
data_buffer_pubkey,
203+
&mut args,
204+
)
205+
.0
206+
}
154207
}
155208
}
156209

@@ -181,6 +234,7 @@ impl BaseTask for BufferTask {
181234
match self.task_type {
182235
BufferTaskType::Commit(_) => 70_000,
183236
BufferTaskType::CommitDiff(_) => 70_000,
237+
BufferTaskType::CommitFinalize(_) => 70_000,
184238
}
185239
}
186240

@@ -192,6 +246,9 @@ impl BaseTask for BufferTask {
192246
BufferTaskType::CommitDiff(_) => {
193247
commit_diff_size_budget(AccountSizeClass::Huge)
194248
}
249+
BufferTaskType::CommitFinalize(_) => {
250+
commit_finalize_from_buffer_size_budget(AccountSizeClass::Huge)
251+
}
195252
}
196253
}
197254

@@ -204,6 +261,7 @@ impl BaseTask for BufferTask {
204261
match self.task_type {
205262
BufferTaskType::Commit(_) => TaskType::Commit,
206263
BufferTaskType::CommitDiff(_) => TaskType::Commit,
264+
BufferTaskType::CommitFinalize(_) => TaskType::CommitFinalize,
207265
}
208266
}
209267

@@ -220,6 +278,9 @@ impl BaseTask for BufferTask {
220278
BufferTaskType::CommitDiff(task) => {
221279
task.commit_id = commit_id;
222280
}
281+
BufferTaskType::CommitFinalize(task) => {
282+
task.commit_id = commit_id;
283+
}
223284
};
224285

225286
self.preparation_state = Self::preparation_required(&self.task_type)
@@ -231,6 +292,7 @@ impl LabelValue for BufferTask {
231292
match self.task_type {
232293
BufferTaskType::Commit(_) => "buffer_commit",
233294
BufferTaskType::CommitDiff(_) => "buffer_commit_diff",
295+
BufferTaskType::CommitFinalize(_) => "buffer_commit_finalize",
234296
}
235297
}
236298
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub use task_builder::TaskBuilderImpl;
3535
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
3636
pub enum TaskType {
3737
Commit,
38+
CommitFinalize,
3839
Finalize,
3940
Undelegate,
4041
Action,
@@ -123,6 +124,14 @@ pub struct CommitDiffTask {
123124
pub base_account: Account,
124125
}
125126

127+
#[derive(Clone, Debug)]
128+
pub struct CommitFinalizeTask {
129+
pub commit_id: u64,
130+
pub allow_undelegation: bool,
131+
pub base_account: Option<Account>, // None implies commit-full-bytes else commit-diff
132+
pub committed_account: CommittedAccount,
133+
}
134+
126135
#[derive(Clone, Debug)]
127136
pub struct UndelegateTask {
128137
pub delegated_account: Pubkey,

0 commit comments

Comments
 (0)