Solana 原生支持多指令批处理交易并具备原子性,但受限于 1232 字节的大小限制,需精简设计或分片部署以应对复杂程序。
在以太坊中,Multicall 是一个常见的模式,通过智能合约将多个调用打包,确保原子性:要么全成功,要么全回滚。Solana 则无需额外实现这一模式,其运行时原生支持在一笔交易中执行多个指令,天然具备原子性。以下示例展示如何在单笔交易内初始化账户并写入数据,而不依赖 Anchor 的 init_if_needed。
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { Batch } from "../target/types/batch";
describe("batch", () => {
  anchor.setProvider(anchor.AnchorProvider.env());
  const program = anchor.workspace.Batch as Program<Batch>;
  it("Is initialized!", async () => {
    const wallet = anchor.workspace.Batch.provider.wallet.payer;
    const [pda, _bump] = anchor.web3.PublicKey.findProgramAddressSync([], program.programId);
    const initTx = await program.methods.initialize()
      .accounts({ pda: pda })
      .transaction();
    // for u32, we don't need to use big numbers
    const setTx = await program.methods.set(5)
      .accounts({ pda: pda })
      .transaction();
    let transaction = new anchor.web3.Transaction();
    transaction.add(initTx);
    transaction.add(setTx);
    await anchor.web3.sendAndConfirmTransaction(anchor.getProvider().connection, transaction, [wallet]);
    const pdaAcc = await program.account.pda.fetch(pda);
    console.log(pdaAcc.value); // prints 5
  });
});
use anchor_lang::prelude::*;
use std::mem::size_of;
declare_id!("CKT2SwoGyNibpqwSqy1DESbwVquENS6qHrFziAxnt8UW");
#[program]
pub mod batch {
    use super::*;
    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
        Ok(())
    }
    pub fn set(ctx: Context<Set>, new_val: u32) -> Result<()> {
        ctx.accounts.pda.value = new_val;
        Ok(())
    }
}
#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(init, payer = signer, space = size_of::<PDA>() + 8, seeds = [], bump)]
    pub pda: Account<'info, PDA>,
    #[account(mut)]
    pub signer: Signer<'info>,
    pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct Set<'info> {
    #[account(mut)]
    pub pda: Account<'info, PDA>,
}
#[account]
pub struct PDA {
    pub value: u32,
}
代码解析
Solana 的交易大小上限为 1232 字节,这是其高吞吐量设计中的权衡。与以太坊通过增加 Gas 扩展交易不同,Solana 要求开发者精简指令。这限制了批处理的数量,但也推动了更高效的程序设计。
为了直观展示批处理的原子性,我们修改 set 函数使其始终失败,观察 initialize 是否被回滚。
use anchor_lang::prelude::*;
use std::mem::size_of;
declare_id!("CKT2SwoGyNibpqwSqy1DESbwVquENS6qHrFziAxnt8UW");
#[program]
pub mod batch {
    use super::*;
    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
        Ok(())
    }
    pub fn set(ctx: Context<Set>, new_val: u32) -> Result<()> {
        ctx.accounts.pda.value = new_val;
        return err!(Error::AlwaysFails);
    }
}
#[error_code]
pub enum Error {
    #[msg("always fails")]
    AlwaysFails,
}
#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(init, payer = signer, space = size_of::<PDA>() + 8, seeds = [], bump)]
    pub pda: Account<'info, PDA>,
    #[account(mut)]
    pub signer: Signer<'info>,
    pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct Set<'info> {
    #[account(mut)]
    pub pda: Account<'info, PDA>,
}
#[account]
pub struct PDA {
    pub value: u32,
}
import * as anchor from "@coral-xyz/anchor";
import { Program, SystemProgram } from "@coral-xyz/anchor";
import { Batch } from "../target/types/batch";
describe("batch", () => {
  anchor.setProvider(anchor.AnchorProvider.env());
  const program = anchor.workspace.Batch as Program<Batch>;
  it("Set the number to 5, initializing if necessary", async () => {
    const wallet = anchor.workspace.Batch.p...                                如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!