Skip to content

Commit 2ae750b

Browse files
committed
Add MagicBlockInstruction::ScheduleCommitDiffAndUndelegate to efficiently commit changes in delegated accounts
1 parent 2bafa8e commit 2ae750b

File tree

38 files changed

+1151
-150
lines changed

38 files changed

+1151
-150
lines changed

Cargo.lock

Lines changed: 34 additions & 13 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
@@ -124,9 +124,7 @@ magicblock-config = { path = "./magicblock-config" }
124124
magicblock-config-helpers = { path = "./magicblock-config-helpers" }
125125
magicblock-config-macro = { path = "./magicblock-config-macro" }
126126
magicblock-core = { path = "./magicblock-core" }
127-
magicblock-delegation-program = { git = "https://github.com/magicblock-labs/delegation-program.git", rev = "aa1de56d90c", features = [
128-
"no-entrypoint",
129-
] }
127+
magicblock-delegation-program = { path="../delegation-program", features = ["no-entrypoint"] }
130128
magicblock-geyser-plugin = { path = "./magicblock-geyser-plugin" }
131129
magicblock-ledger = { path = "./magicblock-ledger" }
132130
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
@@ -357,6 +357,7 @@ impl<C: BaseIntentCommittor> ScheduledCommitsProcessorImpl<C> {
357357
excluded_pubkeys: intent_meta.excluded_pubkeys,
358358
feepayers: intent_meta.feepayers,
359359
requested_undelegation: intent_meta.requested_undelegation,
360+
commit_diff: intent_meta.commit_diff,
360361
}
361362
}
362363
}
@@ -424,6 +425,7 @@ struct ScheduledBaseIntentMeta {
424425
feepayers: HashSet<FeePayerAccount>,
425426
intent_sent_transaction: Transaction,
426427
requested_undelegation: bool,
428+
commit_diff: bool,
427429
}
428430

429431
impl ScheduledBaseIntentMeta {
@@ -443,6 +445,7 @@ impl ScheduledBaseIntentMeta {
443445
feepayers,
444446
intent_sent_transaction: intent.action_sent_transaction.clone(),
445447
requested_undelegation: intent.is_undelegate(),
448+
commit_diff: intent.is_commit_diff(),
446449
}
447450
}
448451
}

magicblock-api/src/tickers.rs

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

2121
use crate::slot::advance_slot_and_update_ledger;
2222

23+
// ER chain
2324
pub fn init_slot_ticker<C: ScheduledCommitsProcessor>(
2425
bank: &Arc<Bank>,
2526
committor_processor: &Option<Arc<C>>,
@@ -71,6 +72,7 @@ pub fn init_slot_ticker<C: ScheduledCommitsProcessor>(
7172
})
7273
}
7374

75+
// ER chain
7476
async fn handle_scheduled_commits<C: ScheduledCommitsProcessor>(
7577
bank: &Arc<Bank>,
7678
committor_processor: &Arc<C>,

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(_) => 65_000,
200+
ArgsTaskType::CommitDiff(_) => 65_000,
135201
ArgsTaskType::BaseAction(task) => task.action.compute_units,
136202
ArgsTaskType::Undelegate(_) => 70_000,
137203
ArgsTaskType::Finalize(_) => 40_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()

0 commit comments

Comments
 (0)