From 85e846a1ba88d8268d4f9970bd377cd2a4ff75de Mon Sep 17 00:00:00 2001 From: hulane1 Date: Fri, 27 Nov 2026 11:58:10 +0100 Subject: [PATCH] fix: enforce strict CEI ordering and atomicity across multicall batch operations Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- contracts/escrow/src/instructions.rs | 8 ++- contracts/escrow/src/test_multicall.rs | 86 ++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/contracts/escrow/src/instructions.rs b/contracts/escrow/src/instructions.rs index 05c49643..cdb806b3 100644 --- a/contracts/escrow/src/instructions.rs +++ b/contracts/escrow/src/instructions.rs @@ -2355,7 +2355,9 @@ impl Escrow { /// paused, and with `MulticallBatchTooLarge` when the batch exceeds /// `MAX_MULTICALL_BATCH_SIZE` — an unbounded batch could otherwise be used /// to exhaust the transaction's instruction or read/write limits and abort - /// midway. + /// midway. Calls run sequentially in the same Soroban invocation, so each + /// operation observes prior storage writes and any error reverts the + /// complete batch. Nested multicalls are not supported. // `s_get_*` / `s_set_*` pairs mirror the public function names they match. #[allow(clippy::similar_names)] pub fn multicall(env: Env, calls: Vec) -> Result, ContractError> { @@ -2382,8 +2384,12 @@ impl Escrow { let s_get_arbitration_fee = Symbol::new(&env, "get_arbitration_fee"); let s_rotate_resolver = Symbol::new(&env, "rotate_resolver"); let s_cancel_escrow = Symbol::new(&env, "cancel_escrow"); + let s_multicall = Symbol::new(&env, "multicall"); for call in calls { + if call.function == s_multicall { + return Err(ContractError::NotAuthorized); + } let res_val: Val = if call.function == s_fund_escrow { dispatch_fund_escrow(&env, &call.args)? } else if call.function == s_get_escrow { diff --git a/contracts/escrow/src/test_multicall.rs b/contracts/escrow/src/test_multicall.rs index 67762e23..c13f4f87 100644 --- a/contracts/escrow/src/test_multicall.rs +++ b/contracts/escrow/src/test_multicall.rs @@ -361,3 +361,89 @@ fn test_multicall_duplicate_fund_escrow_reverts() { let token_client = token::StellarAssetClient::new(&env, &token); assert_eq!(token_client.balance(&buyer), 2_000); } + +/// A failure in the third operation reverts mutations from the first two +/// operations, including the token transfer performed by funding. +#[test] +fn test_multicall_third_failure_reverts_entire_batch() { + let (env, admin, seller, buyer, resolver, token, fee_collector) = setup_env(); + let contract_id = env.register(Escrow, ()); + let client = EscrowClient::new(&env, &contract_id); + client.initialize(&admin, &fee_collector, &0_u32); + + let payees = single_payee(&env, &seller); + let id = client.create_escrow_8( + &payees.into_val(&env), + &Some(buyer.clone()), + &resolver, + &token, + &1_000_i128, + &0_u32, + &3600_u64, + ); + mint(&env, &token, &buyer, 1_000); + + let mut fund_args = Vec::new(&env); + fund_args.push_back(id.into_val(&env)); + fund_args.push_back(buyer.clone().into_val(&env)); + + let tracking = soroban_sdk::String::from_str(&env, "BATCH-TRK-ATOMIC"); + let mut ship_args = Vec::new(&env); + ship_args.push_back(seller.clone().into_val(&env)); + ship_args.push_back(id.into_val(&env)); + ship_args.push_back(tracking.into_val(&env)); + + let mut get_args = Vec::new(&env); + get_args.push_back(id.into_val(&env)); + + let mut calls = Vec::new(&env); + calls.push_back(ContractCall { + function: Symbol::new(&env, "fund_escrow"), + args: fund_args, + }); + calls.push_back(ContractCall { + function: Symbol::new(&env, "mark_shipped"), + args: ship_args.clone(), + }); + calls.push_back(ContractCall { + function: Symbol::new(&env, "mark_shipped"), + args: ship_args, + }); + calls.push_back(ContractCall { + function: Symbol::new(&env, "get_escrow"), + args: get_args.clone(), + }); + calls.push_back(ContractCall { + function: Symbol::new(&env, "get_escrow"), + args: get_args, + }); + + assert_eq!( + client.try_multicall(&calls), + Err(Ok(ContractError::InvalidState)) + ); + assert_eq!(client.get_escrow(&id).state, EscrowState::Pending); + let token_client = token::Client::new(&env, &token); + assert_eq!(token_client.balance(&buyer), 1_000); + assert_eq!(token_client.balance(&contract_id), 0); +} + +#[test] +fn test_multicall_rejects_nested_multicall() { + let (env, admin, _seller, _buyer, _resolver, _token, fee_collector) = setup_env(); + let contract_id = env.register(Escrow, ()); + let client = EscrowClient::new(&env, &contract_id); + client.initialize(&admin, &fee_collector, &0_u32); + + let calls = soroban_sdk::vec![ + &env, + ContractCall { + function: Symbol::new(&env, "multicall"), + args: Vec::new(&env), + } + ]; + assert_eq!( + client.try_multicall(&calls), + Err(Ok(ContractError::NotAuthorized)) + ); +}