Skip to content

Commit 83ce8a8

Browse files
committed
refactor(committor): unify transaction execution
Combined commits no longer need separate executor implementations for single-stage execution, the first transaction of a split execution, and the subsequent actions or undelegations. Replace their duplicated retry loops and timeout adapters with one transaction executor. - Track the current strategy, an optional pending strategy, the confirmed commit signature, and the current attempt count in one concrete runner. Remove SingleStageExecutor, TwoStageExecutor, Initialized, Committed, the sealed trait, StageExecutor, and its three adapters. - Route selected strategies through execute_strategy. Borrow the existing authority, clients, preparator, callback scheduler, and committed-account list instead of cloning services or rebuilding the account list. - Preserve nonce, action, and undelegation recovery according to whether the commit has already landed. Keep execution-limit fallback splitting after the last combined commit and propagate recovered uniqueness nonces to the pending transaction. - Keep the ten-attempt recovery limit per transaction. Preserve attempts consumed before a timeout, resetting the count only when advancing to another transaction or switching to split execution. - Apply the existing intent-wide callback deadline to the shared loop, including callbacks in the pending transaction. Report each successful transaction's callbacks before preparing the next transaction and retain the existing callback behavior for preparation and recovery failures. - Reuse the confirmed commit signature when removing failed follow-up actions leaves no work. Preserve the failure when undelegation recovery empties the transaction, and prevent whole-intent retries after a known successful commit. - Surrender current, pending, and replaced strategies for cleanup on terminal paths. Record recovery cleanup before awaiting blockhash-cache invalidation so cancellation cannot lose that cleanup information. - Move integration tests to the shared entry point and check callback timing at the next transaction's preparation boundary. Add six focused unit tests covering timeout, retry counts, preparation and nonce-fetch failures, cleanup, and recovery after a confirmed commit. Keep transaction packing thresholds, result and error types, metric labels, persisted status/signature mappings, public intents, and buffer-close policy unchanged. Larger naming and persistence cleanup remains separate work. Validation: - All six transaction-executor unit tests passed. - Clippy with warnings denied passed for all committor-service targets and the test_intent_executor integration target. - Integration test targets compiled successfully. - Nightly formatting checks passed in both workspaces; git diff --check passed. - The devnet test_commit_id_actions_cpi_limit_errors_recovery test compiled but failed during fixture funding, before exercising the executor. Local RPC connectivity and the initial airdrop were unavailable in this session. - No performance benchmark was run. The refactor adds no production RPC calls or transactions; broader runtime coverage remains for CI.
1 parent 48f4f1d commit 83ce8a8

7 files changed

Lines changed: 965 additions & 1165 deletions

File tree

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

Lines changed: 30 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
pub mod error;
22
pub mod intent_execution_client;
33
pub(crate) mod intent_executor_factory;
4-
pub mod single_stage_executor;
54
pub mod task_info_fetcher;
6-
pub mod two_stage_executor;
5+
mod transaction_executor;
76
pub mod utils;
87

98
use std::{
@@ -26,7 +25,6 @@ use solana_keypair::Keypair;
2625
use solana_pubkey::Pubkey;
2726
use solana_signature::Signature;
2827
use solana_signer::Signer;
29-
use tracing::trace;
3028

3129
use crate::{
3230
intent_executor::{
@@ -35,13 +33,8 @@ use crate::{
3533
TransactionStrategyExecutionError,
3634
},
3735
intent_execution_client::IntentExecutionClient,
38-
single_stage_executor::SingleStageExecutor,
3936
task_info_fetcher::{CacheTaskInfoFetcher, ResetType, TaskInfoFetcher},
40-
two_stage_executor::TwoStageExecutor,
41-
utils::{
42-
execute_with_timeout, handle_cpi_limit_error, CommitStage,
43-
FinalizeStage, SingleStage,
44-
},
37+
transaction_executor::TransactionExecutor,
4538
},
4639
persist::{CommitStatus, CommitStatusSignatures, IntentPersister},
4740
tasks::{
@@ -60,14 +53,12 @@ use crate::{
6053

6154
#[derive(Clone, Copy, Debug)]
6255
pub enum ExecutionOutput {
63-
// TODO: with arrival of challenge window remove SingleStage
64-
// Protocol requires 2 stage: Commit, Finalize
65-
// SingleStage - optimization for timebeing
56+
/// All tasks completed in one transaction.
6657
SingleStage(Signature),
6758
TwoStage {
68-
/// Commit stage signature
59+
/// Signature of the transaction containing the combined commits.
6960
commit_signature: Signature,
70-
/// Finalize stage signature
61+
/// Signature of the subsequent actions or undelegations.
7162
finalize_signature: Signature,
7263
},
7364
}
@@ -238,9 +229,7 @@ where
238229
}
239230

240231
if all_committed_pubkeys.is_empty() {
241-
// Build tasks for commit stage
242-
// TODO (snawaz): it's actually MagicBaseIntent::BaseActions scenario, not Commit
243-
// scenario, so the related code needs little bit of refactoring and proper renaming.
232+
// Action-only intents use one transaction.
244233
let commit_tasks = TaskBuilderImpl::commit_tasks(
245234
&self.task_info_fetcher,
246235
&intent_bundle,
@@ -256,9 +245,10 @@ where
256245
Some(intent_bundle.id),
257246
)?;
258247
return self
259-
.single_stage_execution_flow(
260-
intent_bundle,
261-
strategy,
248+
.execute_strategy(
249+
intent_bundle.id,
250+
&all_committed_pubkeys,
251+
StrategyExecutionMode::SingleStage(strategy),
262252
execution_report,
263253
persister,
264254
)
@@ -285,168 +275,45 @@ where
285275
let uniqueness_nonce = requires_uniqueness_nonce(&commit_tasks)
286276
.then_some(intent_bundle.id);
287277

288-
// Build execution strategy
289-
match TaskStrategist::build_execution_strategy(
278+
let strategy = TaskStrategist::build_execution_strategy(
290279
commit_tasks,
291280
finalize_tasks,
292281
&self.authority.pubkey(),
293282
persister,
294283
uniqueness_nonce,
295-
)? {
296-
StrategyExecutionMode::SingleStage(strategy) => {
297-
trace!("Single stage execution");
298-
self.single_stage_execution_flow(
299-
intent_bundle,
300-
strategy,
301-
execution_report,
302-
persister,
303-
)
304-
.await
305-
}
306-
StrategyExecutionMode::TwoStage {
307-
commit_stage,
308-
finalize_stage,
309-
} => {
310-
trace!("Two stage execution");
311-
self.two_stage_execution_flow(
312-
&all_committed_pubkeys,
313-
commit_stage,
314-
finalize_stage,
315-
execution_report,
316-
persister,
317-
intent_bundle.id,
318-
)
319-
.await
320-
}
321-
}
322-
}
323-
324-
fn time_left(&self) -> Option<Duration> {
325-
self.actions_timeout.checked_sub(self.started_at.elapsed())
326-
}
327-
328-
/// Starting execution from single stage
329-
pub async fn single_stage_execution_flow<P: IntentPersister>(
330-
&mut self,
331-
base_intent: ScheduledIntentBundle,
332-
transaction_strategy: TransactionStrategy,
333-
execution_report: &mut IntentExecutionReport,
334-
persister: &Option<P>,
335-
) -> IntentExecutorResult<ExecutionOutput> {
336-
let committed_pubkeys = base_intent.get_all_committed_pubkeys();
337-
338-
let mut single_stage_executor = SingleStageExecutor::new(
339-
self.authority.insecure_clone(),
340-
self.intent_client.clone(),
341-
self.task_info_fetcher.clone(),
342-
transaction_strategy,
343-
self.actions_callback_executor.clone(),
284+
)?;
285+
self.execute_strategy(
286+
intent_bundle.id,
287+
&all_committed_pubkeys,
288+
strategy,
344289
execution_report,
345-
base_intent.id,
346-
);
347-
let res = execute_with_timeout(
348-
self.time_left(),
349-
SingleStage {
350-
inner: &mut single_stage_executor,
351-
transaction_preparator: &self.transaction_preparator,
352-
committed_pubkeys: &committed_pubkeys,
353-
},
354290
persister,
355291
)
356-
.await;
357-
358-
// Here we continue only IF the error is a limit-type execution error
359-
// We can recover that Error by splitting execution
360-
// in 2 stages - commit & finalize
361-
// Otherwise we return error
362-
let execution_err = match res {
363-
Err(IntentExecutorError::FailedToFinalizeError {
364-
err,
365-
commit_signature: _,
366-
finalize_signature: _,
367-
}) if !committed_pubkeys.is_empty()
368-
&& single_stage_executor.has_tasks_after_commit()
369-
&& err.is_recoverable_by_two_stage() =>
370-
{
371-
err
372-
}
373-
res => {
374-
let signature = res.as_ref().ok().copied();
375-
single_stage_executor
376-
.execute_callbacks(signature, res.as_ref().map(|_| ()));
377-
let transaction_strategy =
378-
single_stage_executor.consume_strategy();
379-
execution_report.dispose(transaction_strategy);
380-
return res.map(ExecutionOutput::SingleStage);
381-
}
382-
};
383-
384-
// With actions, we can't predict num of CPIs
385-
// If we get here we will try to switch from Single stage to Two Stage commit
386-
// Note that this not necessarily will pass at the end due to the same reason
387-
let strategy = single_stage_executor.consume_strategy();
388-
let (commit_strategy, finalize_strategy, cleanup) =
389-
handle_cpi_limit_error(&self.authority.pubkey(), strategy);
390-
execution_report.dispose(cleanup);
391-
execution_report.add_patched_error(execution_err);
392-
393-
self.two_stage_execution_flow(
394-
&committed_pubkeys,
395-
commit_strategy,
396-
finalize_strategy,
397-
execution_report,
398-
persister,
399-
base_intent.id,
400-
)
401292
.await
402293
}
403294

404-
pub async fn two_stage_execution_flow<P: IntentPersister>(
295+
fn time_left(&self) -> Option<Duration> {
296+
self.actions_timeout.checked_sub(self.started_at.elapsed())
297+
}
298+
299+
/// Executes the selected transaction plan, including recovery and callbacks.
300+
pub async fn execute_strategy<P: IntentPersister>(
405301
&mut self,
302+
intent_id: u64,
406303
committed_pubkeys: &[Pubkey],
407-
commit_strategy: TransactionStrategy,
408-
finalize_strategy: TransactionStrategy,
304+
strategy: StrategyExecutionMode,
409305
execution_report: &mut IntentExecutionReport,
410306
persister: &Option<P>,
411-
intent_id: u64,
412307
) -> IntentExecutorResult<ExecutionOutput> {
413-
let mut executor = TwoStageExecutor::new(
414-
self.authority.insecure_clone(),
415-
commit_strategy,
416-
finalize_strategy,
417-
self.intent_client.clone(),
418-
self.actions_callback_executor.clone(),
308+
TransactionExecutor::new(
309+
self,
419310
execution_report,
420311
intent_id,
421-
);
422-
423-
let commit_signature = execute_with_timeout(
424-
self.time_left(),
425-
CommitStage {
426-
inner: &mut executor,
427-
transaction_preparator: &self.transaction_preparator,
428-
task_info_fetcher: &self.task_info_fetcher,
429-
committed_pubkeys,
430-
},
431-
persister,
432-
)
433-
.await?;
434-
435-
let mut finalize_executor = executor.done(commit_signature);
436-
let finalize_signature = execute_with_timeout(
437-
self.time_left(),
438-
FinalizeStage {
439-
inner: &mut finalize_executor,
440-
transaction_preparator: &self.transaction_preparator,
441-
},
442-
persister,
312+
committed_pubkeys,
313+
strategy,
443314
)
444-
.await?;
445-
446-
Ok(ExecutionOutput::TwoStage {
447-
commit_signature,
448-
finalize_signature,
449-
})
315+
.execute(persister)
316+
.await
450317
}
451318

452319
/// Flushes result into presistor

0 commit comments

Comments
 (0)