Solana 中的 Multicall:批处理交易和交易大小的限制

  • 0xE
  • 发布于 2025-04-02 10:17
  • 阅读 796

Solana 原生支持多指令批处理交易并具备原子性,但受限于 1232 字节的大小限制,需精简设计或分片部署以应对复杂程序。

Solana 的内置 Multicall

在以太坊中,Multicall 是一个常见的模式,通过智能合约将多个调用打包,确保原子性:要么全成功,要么全回滚。Solana 则无需额外实现这一模式,其运行时原生支持在一笔交易中执行多个指令,天然具备原子性。以下示例展示如何在单笔交易内初始化账户并写入数据,而不依赖 Anchor 的 init_if_needed。

Typescript 实现

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
  });
});

Rust 程序

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,
}

代码解析

  • 数值传递:Rust 的 u32 在 JavaScript 中无需 BN(大数)处理,简化交互。
  • 交易构造:从 .rpc() 切换到 .transaction(),允许手动组装指令。这种方式让我想起 Solidity 中通过 call 构建复杂调用时的灵活性,但 Solana 的实现更轻量。
  • 原子性:initialize 和 set 在同一交易中执行,若任一失败,账户状态保持不变。

Solana 的交易大小限制:1232 字节的边界

Solana 的交易大小上限为 1232 字节,这是其高吞吐量设计中的权衡。与以太坊通过增加 Gas 扩展交易不同,Solana 要求开发者精简指令。这限制了批处理的数量,但也推动了更高效的程序设计。


验证原子性:失败即回滚

为了直观展示批处理的原子性,我们修改 set 函数使其始终失败,观察 initialize 是否被回滚。

Rust 程序:模拟失败

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,
}

Typescript 测试:触发回滚


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...

剩余50%的内容订阅专栏后可查看

点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
0xE
0xE
0x59f6...a17e
17年进入币圈,Web3 开发者。刨根问底探链上真相,品味坎坷悟 Web3 人生。