Skip to content

Commit 4c75d78

Browse files
committed
Add MagicBlockInstruction::ScheduleCommitDiffAndUndelegate to efficiently commit changes in delegated accounts
1 parent a1dfd59 commit 4c75d78

File tree

37 files changed

+1294
-166
lines changed

37 files changed

+1294
-166
lines changed

Cargo.lock

Lines changed: 29 additions & 8 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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,8 @@ magicblock-config = { path = "./magicblock-config" }
108108
magicblock-config-helpers = { path = "./magicblock-config-helpers" }
109109
magicblock-config-macro = { path = "./magicblock-config-macro" }
110110
magicblock-core = { path = "./magicblock-core" }
111-
magicblock-delegation-program = { git = "https://github.com/magicblock-labs/delegation-program.git", rev = "aa1de56d90c", features = [
112-
"no-entrypoint",
113-
] }
114111
magicblock-aperture = { path = "./magicblock-aperture" }
112+
magicblock-delegation-program = { path="../delegation-program", features = ["no-entrypoint"] }
115113
magicblock-geyser-plugin = { path = "./magicblock-geyser-plugin" }
116114
magicblock-ledger = { path = "./magicblock-ledger" }
117115
magicblock-metrics = { path = "./magicblock-metrics" }

magicblock-accounts/src/scheduled_commits_processor.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ impl ScheduledCommitsProcessorImpl {
343343
included_pubkeys: intent_meta.included_pubkeys,
344344
excluded_pubkeys: intent_meta.excluded_pubkeys,
345345
requested_undelegation: intent_meta.requested_undelegation,
346+
commit_diff: intent_meta.commit_diff,
346347
}
347348
}
348349
}
@@ -412,6 +413,7 @@ struct ScheduledBaseIntentMeta {
412413
excluded_pubkeys: Vec<Pubkey>,
413414
intent_sent_transaction: Transaction,
414415
requested_undelegation: bool,
416+
commit_diff: bool,
415417
}
416418

417419
impl ScheduledBaseIntentMeta {
@@ -429,6 +431,7 @@ impl ScheduledBaseIntentMeta {
429431
excluded_pubkeys,
430432
intent_sent_transaction: intent.action_sent_transaction.clone(),
431433
requested_undelegation: intent.is_undelegate(),
434+
commit_diff: intent.is_commit_diff(),
432435
}
433436
}
434437
}

magicblock-api/src/tickers.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use tokio_util::sync::CancellationToken;
2222

2323
use crate::slot::advance_slot_and_update_ledger;
2424

25+
// ER chain
2526
pub fn init_slot_ticker<C: ScheduledCommitsProcessor>(
2627
accountsdb: Arc<AccountsDb>,
2728
committor_processor: &Option<Arc<C>>,
@@ -79,6 +80,7 @@ pub fn init_slot_ticker<C: ScheduledCommitsProcessor>(
7980
})
8081
}
8182

83+
// ER chain
8284
async fn handle_scheduled_commits<C: ScheduledCommitsProcessor>(
8385
committor_processor: &Arc<C>,
8486
transaction_scheduler: &TransactionSchedulerHandle,

magicblock-committor-program/src/instruction.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use borsh::{BorshDeserialize, BorshSerialize};
22
use solana_pubkey::Pubkey;
33

4+
// this is deployed on Base chain as user-program
5+
46
#[derive(BorshSerialize, BorshDeserialize, Debug, Clone)]
57
pub enum CommittorInstruction {
68
/// Initializes the buffer and [Chunks] accounts which will be used to

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ where
6262
Err(err) => err,
6363
};
6464

65+
error!("execution_err: {}", execution_err);
66+
6567
// Attempt patching
6668
let flow = self
6769
.patch_strategy(
@@ -79,6 +81,7 @@ where
7981
cleanup
8082
}
8183
ControlFlow::Break(()) => {
84+
info!("base_intent: {:?}", base_intent);
8285
error!("Could not patch failed intent: {}", base_intent.id);
8386
break (execution_err, transaction_strategy);
8487
}

magicblock-committor-service/src/service.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub struct LookupTables {
3030
pub released: Vec<Pubkey>,
3131
}
3232

33+
// this is a part of ER.. and communicates with Base chain
34+
3335
#[derive(Debug)]
3436
pub enum CommittorMessage {
3537
ReservePubkeysForCommittee {

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

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
1-
use dlp::args::{CallHandlerArgs, CommitStateArgs};
1+
use dlp::{
2+
args::{CallHandlerArgs, CommitDiffArgs, CommitStateArgs},
3+
compute_diff,
4+
};
5+
use solana_account::ReadableAccount;
26
use solana_pubkey::Pubkey;
3-
use solana_sdk::instruction::{AccountMeta, Instruction};
7+
use solana_rpc_client::rpc_client::RpcClient;
8+
use solana_sdk::{
9+
commitment_config::CommitmentConfig,
10+
instruction::{AccountMeta, Instruction},
11+
};
412

513
#[cfg(test)]
614
use crate::tasks::TaskStrategy;
7-
use crate::tasks::{
8-
buffer_task::{BufferTask, BufferTaskType},
9-
visitor::Visitor,
10-
BaseActionTask, BaseTask, BaseTaskError, BaseTaskResult, CommitTask,
11-
FinalizeTask, PreparationState, TaskType, UndelegateTask,
15+
use crate::{
16+
config::ChainConfig,
17+
tasks::{
18+
buffer_task::{BufferTask, BufferTaskType},
19+
visitor::Visitor,
20+
BaseActionTask, BaseTask, BaseTaskError, BaseTaskResult, CommitTask,
21+
FinalizeTask, PreparationState, TaskType, UndelegateTask,
22+
},
23+
ComputeBudgetConfig,
1224
};
1325

1426
/// Task that will be executed on Base layer via arguments
1527
#[derive(Clone)]
1628
pub enum ArgsTaskType {
1729
Commit(CommitTask),
30+
CommitDiff(CommitTask),
1831
Finalize(FinalizeTask),
1932
Undelegate(UndelegateTask), // Special action really
2033
BaseAction(BaseActionTask),
@@ -58,6 +71,55 @@ impl BaseTask for ArgsTask {
5871
args,
5972
)
6073
}
74+
ArgsTaskType::CommitDiff(value) => {
75+
let chain_config =
76+
ChainConfig::local(ComputeBudgetConfig::new(1_000_000));
77+
78+
let rpc_client = RpcClient::new_with_commitment(
79+
chain_config.rpc_uri.to_string(),
80+
CommitmentConfig {
81+
commitment: chain_config.commitment,
82+
},
83+
);
84+
85+
let account = match rpc_client
86+
.get_account(&value.committed_account.pubkey)
87+
{
88+
Ok(account) => account,
89+
Err(e) => {
90+
log::warn!("Fallback to commit_state and send full-bytes, as rpc failed to fetch the delegated-account from base chain: {}", e);
91+
let args = CommitStateArgs {
92+
nonce: value.commit_id,
93+
lamports: value.committed_account.account.lamports,
94+
data: value.committed_account.account.data.clone(),
95+
allow_undelegation: value.allow_undelegation,
96+
};
97+
return dlp::instruction_builder::commit_state(
98+
*validator,
99+
value.committed_account.pubkey,
100+
value.committed_account.account.owner,
101+
args,
102+
);
103+
}
104+
};
105+
106+
let args = CommitDiffArgs {
107+
nonce: value.commit_id,
108+
lamports: value.committed_account.account.lamports,
109+
diff: compute_diff(
110+
account.data(),
111+
value.committed_account.account.data(),
112+
),
113+
allow_undelegation: value.allow_undelegation,
114+
};
115+
log::warn!("DIFF computed: {:?}", args.diff);
116+
dlp::instruction_builder::commit_diff(
117+
*validator,
118+
value.committed_account.pubkey,
119+
value.committed_account.account.owner,
120+
args,
121+
)
122+
}
61123
ArgsTaskType::Finalize(value) => {
62124
dlp::instruction_builder::finalize(
63125
*validator,
@@ -106,6 +168,9 @@ impl BaseTask for ArgsTask {
106168
BufferTaskType::Commit(value),
107169
)))
108170
}
171+
ArgsTaskType::CommitDiff(_) => {
172+
panic!("ArgsTaskType::CommitDiff not handled")
173+
}
109174
ArgsTaskType::BaseAction(_)
110175
| ArgsTaskType::Finalize(_)
111176
| ArgsTaskType::Undelegate(_) => Err(self),
@@ -132,6 +197,7 @@ impl BaseTask for ArgsTask {
132197
fn compute_units(&self) -> u32 {
133198
match &self.task_type {
134199
ArgsTaskType::Commit(_) => 70_000,
200+
ArgsTaskType::CommitDiff(_) => 65_000,
135201
ArgsTaskType::BaseAction(task) => task.action.compute_units,
136202
ArgsTaskType::Undelegate(_) => 70_000,
137203
ArgsTaskType::Finalize(_) => 70_000,
@@ -146,6 +212,9 @@ impl BaseTask for ArgsTask {
146212
fn task_type(&self) -> TaskType {
147213
match &self.task_type {
148214
ArgsTaskType::Commit(_) => TaskType::Commit,
215+
// TODO (snawaz): What should we use here? Commit (in the sense of "category of task"), or add a
216+
// new variant "CommitDiff" to indicate a specific instruction?
217+
ArgsTaskType::CommitDiff(_) => TaskType::Commit,
149218
ArgsTaskType::BaseAction(_) => TaskType::Action,
150219
ArgsTaskType::Undelegate(_) => TaskType::Undelegate,
151220
ArgsTaskType::Finalize(_) => TaskType::Finalize,
@@ -158,7 +227,9 @@ impl BaseTask for ArgsTask {
158227
}
159228

160229
fn reset_commit_id(&mut self, commit_id: u64) {
230+
// TODO (snawaz): handle CommitDiff as well?
161231
let ArgsTaskType::Commit(commit_task) = &mut self.task_type else {
232+
log::error!("reset_commit_id");
162233
return;
163234
};
164235

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ pub enum TaskStrategy {
5353
pub trait BaseTask: Send + Sync + DynClone {
5454
/// Gets all pubkeys that involved in Task's instruction
5555
fn involved_accounts(&self, validator: &Pubkey) -> Vec<Pubkey> {
56+
// TODO (snawaz): rewrite it.
57+
// currently it is slow as it discards heavy computations and memory allocations.
5658
self.instruction(validator)
5759
.accounts
5860
.iter()

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

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,29 @@ impl TasksBuilder for TaskBuilderImpl {
4747
base_intent: &ScheduledBaseIntent,
4848
persister: &Option<P>,
4949
) -> TaskBuilderResult<Vec<Box<dyn BaseTask>>> {
50-
let (accounts, allow_undelegation) = match &base_intent.base_intent {
51-
MagicBaseIntent::BaseActions(actions) => {
52-
let tasks = actions
53-
.iter()
54-
.map(|el| {
55-
let task = BaseActionTask { action: el.clone() };
56-
let task =
57-
ArgsTask::new(ArgsTaskType::BaseAction(task));
58-
Box::new(task) as Box<dyn BaseTask>
59-
})
60-
.collect();
61-
62-
return Ok(tasks);
63-
}
64-
MagicBaseIntent::Commit(t) => (t.get_committed_accounts(), false),
65-
MagicBaseIntent::CommitAndUndelegate(t) => {
66-
(t.commit_action.get_committed_accounts(), true)
67-
}
68-
};
50+
let (accounts, allow_undelegation, commit_diff) =
51+
match &base_intent.base_intent {
52+
MagicBaseIntent::BaseActions(actions) => {
53+
let tasks = actions
54+
.iter()
55+
.map(|el| {
56+
let task = BaseActionTask { action: el.clone() };
57+
let task =
58+
ArgsTask::new(ArgsTaskType::BaseAction(task));
59+
Box::new(task) as Box<dyn BaseTask>
60+
})
61+
.collect();
62+
return Ok(tasks);
63+
}
64+
MagicBaseIntent::Commit(t) => {
65+
(t.get_committed_accounts(), false, t.is_commit_diff())
66+
}
67+
MagicBaseIntent::CommitAndUndelegate(t) => (
68+
t.commit_action.get_committed_accounts(),
69+
true,
70+
t.commit_action.is_commit_diff(),
71+
),
72+
};
6973

7074
let committed_pubkeys = accounts
7175
.iter()
@@ -89,11 +93,16 @@ impl TasksBuilder for TaskBuilderImpl {
8993
.iter()
9094
.map(|account| {
9195
let commit_id = *commit_ids.get(&account.pubkey).expect("CommitIdFetcher provide commit ids for all listed pubkeys, or errors!");
92-
let task = ArgsTaskType::Commit(CommitTask {
96+
let task = CommitTask {
9397
commit_id,
9498
allow_undelegation,
9599
committed_account: account.clone(),
96-
});
100+
};
101+
let task = if commit_diff {
102+
ArgsTaskType::CommitDiff(task)
103+
} else {
104+
ArgsTaskType::Commit(task)
105+
};
97106

98107
Box::new(ArgsTask::new(task)) as Box<dyn BaseTask>
99108
})
@@ -134,6 +143,9 @@ impl TasksBuilder for TaskBuilderImpl {
134143
CommitType::Standalone(accounts) => {
135144
accounts.iter().map(finalize_task).collect()
136145
}
146+
CommitType::StandaloneDiff(accounts) => {
147+
accounts.iter().map(finalize_task).collect()
148+
}
137149
CommitType::WithBaseActions {
138150
committed_accounts,
139151
base_actions,

0 commit comments

Comments
 (0)