From 3f52268d2ece23a9ab02b1d0361765c586441646 Mon Sep 17 00:00:00 2001 From: Babur Makhmudov Date: Fri, 23 Oct 2026 09:33:18 +0800 Subject: [PATCH] fix: distinguish repeated private account mutations --- Cargo.lock | 1 + engine/Cargo.toml | 1 + engine/src/accessor.rs | 13 ++++++---- engine/src/transaction.rs | 27 +++++++++++--------- programs/magic-root-interface/src/lib.rs | 4 ++- programs/magic-root-program/src/processor.rs | 1 + 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4f8b8fea..e218a805 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2339,6 +2339,7 @@ dependencies = [ "magicblock-processor", "num_cpus", "oneshot", + "rand 0.9.5", "solana-account", "solana-compute-budget-program", "solana-instruction", diff --git a/engine/Cargo.toml b/engine/Cargo.toml index 4040a626..0d01e299 100644 --- a/engine/Cargo.toml +++ b/engine/Cargo.toml @@ -26,6 +26,7 @@ processor = { workspace = true } derive_more = { workspace = true } num_cpus = { workspace = true } oneshot = { workspace = true } +rand = { workspace = true } thiserror = { workspace = true } tokio = { workspace = true, features = ["sync"] } tracing = { workspace = true } diff --git a/engine/src/accessor.rs b/engine/src/accessor.rs index f9f71d2d..bbd6a2a6 100644 --- a/engine/src/accessor.rs +++ b/engine/src/accessor.rs @@ -79,15 +79,14 @@ impl AccountAccessor<'_> { let ix = MagicRootInstruction::PostFinalize(actions); instructions.push(ix.compose(pubkey)?); } - self.execute(&instructions, Some(mode)).await + self.execute(instructions, Some(mode)).await } /// Closes the account, releasing ownership after definitive completion. /// Cancellation has the same ownership contract as [`Self::materialize`]. pub async fn delete(self) -> Result<()> { - let pubkey = self.pubkey(); - let instruction = MagicRootInstruction::Delete.compose(pubkey)?; - self.execute(&[instruction], None).await + let instruction = MagicRootInstruction::Delete.compose(self.pubkey())?; + self.execute(vec![instruction], None).await } /// Keeps this accessor only for a present account that does not require persistence. @@ -102,7 +101,11 @@ impl AccountAccessor<'_> { /// Before submission, cancellation releases the lease without submitting work. /// After submission, the task owns completion and success bookkeeping. - async fn execute(self, instructions: &[Instruction], mode: Option) -> Result<()> { + async fn execute( + self, + instructions: Vec, + mode: Option, + ) -> Result<()> { let txn = transaction::magicblock(instructions, self.engine)?; let rx = self.engine.transaction(txn)?.submit().await?; // No await may separate successful submission from this lease handoff. diff --git a/engine/src/transaction.rs b/engine/src/transaction.rs index 8b74849b..9766c575 100644 --- a/engine/src/transaction.rs +++ b/engine/src/transaction.rs @@ -5,6 +5,7 @@ use agave_transaction_view::{ transaction_version::{MAGICBLOCK_PREFIX, TransactionVersion}, }; use keeper::TransactionView; +use magic_root_interface::MagicRootInstruction; use solana_instruction::Instruction; use solana_message::{ VersionedMessage, @@ -141,27 +142,29 @@ pub(super) fn sigverify(view: &TransactionView) -> Result<()> { /// Composes an Engine-private transaction and signs its final /// Magicblock wire representation with the Engine authority. -pub(crate) fn magicblock(instructions: &[Instruction], engine: &Engine) -> Result> { - let message = v1::Message::try_compile(&engine.authority(), instructions, engine.blockhash())?; - let message = VersionedMessage::V1(message); +pub(crate) fn magicblock(mut instructions: Vec, engine: &Engine) -> Result> { + // Randomize the signed payload: repeated operations can share a blockhash. + instructions.push(MagicRootInstruction::Nonce(rand::random()).compose(engine.authority())?); + let message = v1::Message::try_compile(&engine.authority(), &instructions, engine.blockhash())?; // Private transactions have larger payloads but still obey runtime // instruction-trace and account-index limits. - if message.instructions().len() > MAGICBLOCK_INSTRUCTION_TRACE_LENGTH { - Err(TransactionError::SanitizeFailure)?; - } else if message.static_account_keys().len() > MAX_MAGICBLOCK_ACCOUNT_LOCKS { - Err(TransactionError::TooManyAccountLocks)?; + if message.instructions.len() > MAGICBLOCK_INSTRUCTION_TRACE_LENGTH { + return Err(TransactionError::SanitizeFailure.into()); } - for ix in message.instructions() { - if ix.accounts.len() > MAX_MAGICBLOCK_ACCOUNT_LOCKS { - Err(TransactionError::TooManyAccountLocks)?; - } + if message.account_keys.len() > MAX_MAGICBLOCK_ACCOUNT_LOCKS + || message + .instructions + .iter() + .any(|ix| ix.accounts.len() > MAX_MAGICBLOCK_ACCOUNT_LOCKS) + { + return Err(TransactionError::TooManyAccountLocks.into()); } // Reserve the trailing signature slot without signing the V1 prefix, which // is replaced below before the only signing operation. let transaction = VersionedTransaction { signatures: vec![Default::default()], - message, + message: VersionedMessage::V1(message), }; let mut data = wincode::serialize(&transaction).map_err(wincode::Error::from)?; // Select the private wire format before signing; its payload limit exceeds V1's. diff --git a/programs/magic-root-interface/src/lib.rs b/programs/magic-root-interface/src/lib.rs index c7c49825..bcaae7dc 100644 --- a/programs/magic-root-interface/src/lib.rs +++ b/programs/magic-root-interface/src/lib.rs @@ -38,6 +38,8 @@ pub enum MagicRootInstruction { /// (e.g. initializing a freshly created account); each is invoked via CPI /// against the accounts it declares. PostFinalize(PostFinalize), + /// Distinguishes repeated private operations without changing account state. + Nonce(u64), } impl MagicRootInstruction { @@ -79,7 +81,7 @@ impl MagicRootInstruction { }; for ix in &post_finalize.actions { accounts.push(AccountMeta::new_readonly(ix.program_id, false)); - let metas = ix.accounts.clone().into_iter().map(|mut meta| { + let metas = ix.accounts.iter().cloned().map(|mut meta| { meta.is_signer = false; meta }); diff --git a/programs/magic-root-program/src/processor.rs b/programs/magic-root-program/src/processor.rs index d4830cc2..c51a3fd4 100644 --- a/programs/magic-root-program/src/processor.rs +++ b/programs/magic-root-program/src/processor.rs @@ -75,6 +75,7 @@ fn dispatch( MagicRootInstruction::Finalize(flags) => account::finalize(ctx, target, flags), MagicRootInstruction::Delete => account::delete(ctx, target), MagicRootInstruction::PostFinalize(actions) => post_finalize::process(ctx, actions), + MagicRootInstruction::Nonce(_) => Ok(()), } }