Skip to content

Commit ee0518e

Browse files
committed
feat: Add ScheduleCommitFinalize along with CommitFinalizeTask
1 parent b12cb06 commit ee0518e

14 files changed

Lines changed: 526 additions & 53 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 2 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
@@ -101,7 +101,7 @@ magicblock-committor-program = { path = "./magicblock-committor-program", featur
101101
magicblock-committor-service = { path = "./magicblock-committor-service" }
102102
magicblock-config = { path = "./magicblock-config" }
103103
magicblock-core = { path = "./magicblock-core" }
104-
magicblock-delegation-program = { git = "https://github.com/magicblock-labs/delegation-program.git", rev = "1874b4f5f5f55cb9ab54b64de2cc0d41107d1435", features = [
104+
magicblock-delegation-program = { path = "../delegation-program", features = [
105105
"no-entrypoint",
106106
] }
107107
magicblock-ledger = { path = "./magicblock-ledger" }

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ where
160160
let committed_pubkeys = match base_intent.get_committed_pubkeys() {
161161
Some(value) => value,
162162
None => {
163-
// Build tasks for commit stage
163+
// TODO (snawaz): it's actually MagicBaseIntent::BaseActionse scenario.
164+
// do something here so that we do not have to call "commit_tasks".
165+
// or simply rename this to something generic and return Either<commit_tasks,
166+
// base_actions>;
164167
let commit_tasks = TaskBuilderImpl::commit_tasks(
165168
&self.task_info_fetcher,
166169
&base_intent,
@@ -184,6 +187,46 @@ where
184187
}
185188
};
186189

190+
// if base_intent.is_commit_finalize() {
191+
// // Build tasks for commit & finalize stages
192+
// let commit_tasks = TaskBuilderImpl::commit_tasks(
193+
// &self.task_info_fetcher,
194+
// &base_intent,
195+
// persister,
196+
// )
197+
// .await?;
198+
199+
// // Build execution strategy
200+
// match TaskStrategist::build_execution_strategy(
201+
// commit_tasks,
202+
// Vec::new(),
203+
// &self.authority.pubkey(),
204+
// persister,
205+
// )? {
206+
// StrategyExecutionMode::SingleStage(strategy) => {
207+
// trace!("Executing intent in single stage");
208+
// self.single_stage_execution_flow(
209+
// base_intent,
210+
// strategy,
211+
// persister,
212+
// )
213+
// .await
214+
// }
215+
// StrategyExecutionMode::TwoStage {
216+
// commit_stage,
217+
// finalize_stage,
218+
// } => {
219+
// trace!("Executing intent in two stages");
220+
// self.two_stage_execution_flow(
221+
// &committed_pubkeys,
222+
// commit_stage,
223+
// finalize_stage,
224+
// persister,
225+
// )
226+
// .await
227+
// }
228+
// }
229+
// } else {
187230
// Build tasks for commit & finalize stages
188231
let (commit_tasks, finalize_tasks) = {
189232
let commit_tasks_fut = TaskBuilderImpl::commit_tasks(
@@ -231,6 +274,7 @@ where
231274
.await
232275
}
233276
}
277+
//}
234278
}
235279

236280
/// Starting execution from single stage

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

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use dlp::{
2-
args::{CallHandlerArgs, CommitDiffArgs, CommitStateArgs},
2+
args::{
3+
CallHandlerArgs, CommitDiffArgs, CommitFinalizeArgs, CommitStateArgs,
4+
},
35
compute_diff,
46
instruction_builder::{
5-
call_handler_size_budget, commit_diff_size_budget, commit_size_budget,
6-
finalize_size_budget, undelegate_size_budget,
7+
call_handler_size_budget, commit_diff_size_budget,
8+
commit_finalize_size_budget, commit_size_budget, finalize_size_budget,
9+
undelegate_size_budget,
710
},
811
AccountSizeClass,
912
};
@@ -18,14 +21,16 @@ use crate::tasks::{
1821
buffer_task::{BufferTask, BufferTaskType},
1922
visitor::Visitor,
2023
BaseActionTask, BaseTask, BaseTaskError, BaseTaskResult, CommitDiffTask,
21-
CommitTask, FinalizeTask, PreparationState, TaskType, UndelegateTask,
24+
CommitFinalizeTask, CommitTask, FinalizeTask, PreparationState, TaskType,
25+
UndelegateTask,
2226
};
2327

2428
/// Task that will be executed on Base layer via arguments
2529
#[derive(Clone)]
2630
pub enum ArgsTaskType {
2731
Commit(CommitTask),
2832
CommitDiff(CommitDiffTask),
33+
CommitFinalize(CommitFinalizeTask),
2934
Finalize(FinalizeTask),
3035
Undelegate(UndelegateTask), // Special action really
3136
BaseAction(BaseActionTask),
@@ -88,6 +93,41 @@ impl BaseTask for ArgsTask {
8893
args,
8994
)
9095
}
96+
ArgsTaskType::CommitFinalize(value) => {
97+
let args = CommitFinalizeArgs {
98+
nonce: value.commit_id,
99+
lamports: value.committed_account.account.lamports,
100+
101+
data: if let Some(base_account) = &value.base_account {
102+
compute_diff(
103+
base_account.data(),
104+
value.committed_account.account.data(),
105+
)
106+
.to_vec()
107+
} else {
108+
value.committed_account.account.data.clone()
109+
},
110+
111+
data_is_diff: value
112+
.base_account
113+
.as_ref()
114+
.map(|_| 1)
115+
.unwrap_or(0),
116+
117+
allow_undelegation: if value.allow_undelegation {
118+
1
119+
} else {
120+
0
121+
},
122+
};
123+
124+
dlp::instruction_builder::commit_finalize(
125+
*validator,
126+
value.committed_account.pubkey,
127+
value.committed_account.account.owner,
128+
args,
129+
)
130+
}
91131
ArgsTaskType::Finalize(value) => {
92132
dlp::instruction_builder::finalize(
93133
*validator,
@@ -141,7 +181,8 @@ impl BaseTask for ArgsTask {
141181
BufferTaskType::CommitDiff(value),
142182
)))
143183
}
144-
ArgsTaskType::BaseAction(_)
184+
ArgsTaskType::CommitFinalize(_)
185+
| ArgsTaskType::BaseAction(_)
145186
| ArgsTaskType::Finalize(_)
146187
| ArgsTaskType::Undelegate(_) => Err(self),
147188
}
@@ -168,6 +209,7 @@ impl BaseTask for ArgsTask {
168209
match &self.task_type {
169210
ArgsTaskType::Commit(_) => 70_000,
170211
ArgsTaskType::CommitDiff(_) => 70_000,
212+
ArgsTaskType::CommitFinalize(_) => 25000,
171213
ArgsTaskType::BaseAction(task) => task.action.compute_units,
172214
ArgsTaskType::Undelegate(_) => 70_000,
173215
ArgsTaskType::Finalize(_) => 70_000,
@@ -186,6 +228,11 @@ impl BaseTask for ArgsTask {
186228
task.committed_account.account.data.len() as u32,
187229
))
188230
}
231+
ArgsTaskType::CommitFinalize(task) => {
232+
commit_finalize_size_budget(AccountSizeClass::Dynamic(
233+
task.committed_account.account.data.len() as u32,
234+
))
235+
}
189236
ArgsTaskType::BaseAction(task) => {
190237
// assume all other accounts are Small accounts.
191238
let other_accounts_budget =
@@ -215,6 +262,7 @@ impl BaseTask for ArgsTask {
215262
match &self.task_type {
216263
ArgsTaskType::Commit(_) => TaskType::Commit,
217264
ArgsTaskType::CommitDiff(_) => TaskType::Commit,
265+
ArgsTaskType::CommitFinalize(_) => TaskType::CommitFinalize,
218266
ArgsTaskType::BaseAction(_) => TaskType::Action,
219267
ArgsTaskType::Undelegate(_) => TaskType::Undelegate,
220268
ArgsTaskType::Finalize(_) => TaskType::Finalize,
@@ -234,6 +282,9 @@ impl BaseTask for ArgsTask {
234282
ArgsTaskType::CommitDiff(task) => {
235283
task.commit_id = commit_id;
236284
}
285+
ArgsTaskType::CommitFinalize(task) => {
286+
task.commit_id = commit_id;
287+
}
237288
ArgsTaskType::BaseAction(_)
238289
| ArgsTaskType::Finalize(_)
239290
| ArgsTaskType::Undelegate(_) => {}
@@ -246,6 +297,7 @@ impl LabelValue for ArgsTask {
246297
match self.task_type {
247298
ArgsTaskType::Commit(_) => "args_commit",
248299
ArgsTaskType::CommitDiff(_) => "args_commit_diff",
300+
ArgsTaskType::CommitFinalize(_) => "args_commit_finalize",
249301
ArgsTaskType::BaseAction(_) => "args_action",
250302
ArgsTaskType::Finalize(_) => "args_finalize",
251303
ArgsTaskType::Undelegate(_) => "args_undelegate",

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub use task_builder::TaskBuilderImpl;
3434
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
3535
pub enum TaskType {
3636
Commit,
37+
CommitFinalize,
3738
Finalize,
3839
Undelegate,
3940
Action,
@@ -119,6 +120,14 @@ pub struct CommitDiffTask {
119120
pub base_account: Account,
120121
}
121122

123+
#[derive(Clone)]
124+
pub struct CommitFinalizeTask {
125+
pub commit_id: u64,
126+
pub allow_undelegation: bool,
127+
pub base_account: Option<Account>, // None implies commit-full-bytes else commit-diff
128+
pub committed_account: CommittedAccount,
129+
}
130+
122131
#[derive(Clone)]
123132
pub struct UndelegateTask {
124133
pub delegated_account: Pubkey,

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

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::sync::Arc;
33
use async_trait::async_trait;
44
use log::error;
55
use magicblock_program::magic_scheduled_base_intent::{
6-
CommitType, CommittedAccount, MagicBaseIntent, ScheduledBaseIntent,
7-
UndelegateType,
6+
CommitAndUndelegate, CommitType, CommittedAccount, MagicBaseIntent,
7+
ScheduledBaseIntent, UndelegateType,
88
};
99
use solana_account::Account;
1010
use solana_pubkey::Pubkey;
@@ -108,6 +108,12 @@ impl TasksBuilder for TaskBuilderImpl {
108108
MagicBaseIntent::CommitAndUndelegate(t) => {
109109
(t.commit_action.get_committed_accounts(), true)
110110
}
111+
MagicBaseIntent::CommitFinalize(t) => {
112+
(t.get_committed_accounts(), false)
113+
}
114+
MagicBaseIntent::CommitFinalizeAndUndelegate(t) => {
115+
(t.commit_action.get_committed_accounts(), true)
116+
}
111117
};
112118

113119
let (commit_ids, base_accounts) = {
@@ -229,51 +235,62 @@ impl TasksBuilder for TaskBuilderImpl {
229235
}
230236
}
231237

238+
async fn handler<C: TaskInfoFetcher>(
239+
commit_and_undelegate: &CommitAndUndelegate,
240+
info_fetcher: &Arc<C>,
241+
) -> TaskBuilderResult<Vec<Box<dyn BaseTask>>> {
242+
// Get rent reimbursments for undelegated accounts
243+
let accounts = commit_and_undelegate.get_committed_accounts();
244+
let mut min_context_slot = 0;
245+
let pubkeys = accounts
246+
.iter()
247+
.map(|account| {
248+
min_context_slot =
249+
std::cmp::max(min_context_slot, account.remote_slot);
250+
account.pubkey
251+
})
252+
.collect::<Vec<_>>();
253+
let rent_reimbursements = info_fetcher
254+
.fetch_rent_reimbursements(&pubkeys, min_context_slot)
255+
.await
256+
.map_err(TaskBuilderError::FinalizedTasksBuildError)?;
257+
258+
let mut tasks = accounts
259+
.iter()
260+
.zip(rent_reimbursements)
261+
.map(|(account, rent_reimbursement)| {
262+
undelegate_task(account, &rent_reimbursement)
263+
})
264+
.collect::<Vec<_>>();
265+
266+
match &commit_and_undelegate.undelegate_action {
267+
UndelegateType::Standalone => Ok(tasks),
268+
UndelegateType::WithBaseActions(actions) => {
269+
tasks.extend(actions.iter().map(|action| {
270+
let task = BaseActionTask {
271+
action: action.clone(),
272+
};
273+
let task =
274+
ArgsTask::new(ArgsTaskType::BaseAction(task));
275+
Box::new(task) as Box<dyn BaseTask>
276+
}));
277+
278+
Ok(tasks)
279+
}
280+
}
281+
}
282+
232283
match &base_intent.base_intent {
233284
MagicBaseIntent::BaseActions(_) => Ok(vec![]),
234285
MagicBaseIntent::Commit(commit) => Ok(process_commit(commit)),
235286
MagicBaseIntent::CommitAndUndelegate(t) => {
236287
let mut tasks = process_commit(&t.commit_action);
237-
238-
// Get rent reimbursments for undelegated accounts
239-
let accounts = t.get_committed_accounts();
240-
let mut min_context_slot = 0;
241-
let pubkeys = accounts
242-
.iter()
243-
.map(|account| {
244-
min_context_slot = std::cmp::max(
245-
min_context_slot,
246-
account.remote_slot,
247-
);
248-
account.pubkey
249-
})
250-
.collect::<Vec<_>>();
251-
let rent_reimbursements = info_fetcher
252-
.fetch_rent_reimbursements(&pubkeys, min_context_slot)
253-
.await
254-
.map_err(TaskBuilderError::FinalizedTasksBuildError)?;
255-
256-
tasks.extend(accounts.iter().zip(rent_reimbursements).map(
257-
|(account, rent_reimbursement)| {
258-
undelegate_task(account, &rent_reimbursement)
259-
},
260-
));
261-
262-
match &t.undelegate_action {
263-
UndelegateType::Standalone => Ok(tasks),
264-
UndelegateType::WithBaseActions(actions) => {
265-
tasks.extend(actions.iter().map(|action| {
266-
let task = BaseActionTask {
267-
action: action.clone(),
268-
};
269-
let task =
270-
ArgsTask::new(ArgsTaskType::BaseAction(task));
271-
Box::new(task) as Box<dyn BaseTask>
272-
}));
273-
274-
Ok(tasks)
275-
}
276-
}
288+
tasks.extend(handler(t, info_fetcher).await?);
289+
Ok(tasks)
290+
}
291+
MagicBaseIntent::CommitFinalize(_) => Ok(vec![]),
292+
MagicBaseIntent::CommitFinalizeAndUndelegate(t) => {
293+
handler(t, info_fetcher).await
277294
}
278295
}
279296
}

magicblock-committor-service/src/types.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ impl metrics::LabelValue for ScheduledBaseIntentWrapper {
2525
MagicBaseIntent::BaseActions(_) => "actions",
2626
MagicBaseIntent::Commit(_) => "commit",
2727
MagicBaseIntent::CommitAndUndelegate(_) => "commit_and_undelegate",
28+
MagicBaseIntent::CommitFinalize(_) => "commit_finalize",
29+
MagicBaseIntent::CommitFinalizeAndUndelegate(_) => {
30+
"commit_finalize_and_undelegate"
31+
}
2832
}
2933
}
3034
}

magicblock-magic-program-api/src/args.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ pub enum MagicBaseIntentArgs {
8585
BaseActions(Vec<BaseActionArgs>),
8686
Commit(CommitTypeArgs),
8787
CommitAndUndelegate(CommitAndUndelegateArgs),
88+
CommitFinalize(CommitTypeArgs),
89+
CommitFinalizeAndUndelegate(CommitAndUndelegateArgs),
8890
}
8991

9092
/// A compact account meta used for base-layer actions.

0 commit comments

Comments
 (0)