跳到主要内容

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

solana transaction batch and transaction size limits

Solana 内置 Multicall

在以太坊中,如果我们想要原子地批处理多个交易,我们会使用 Multicall 模式。如果一个失败,其余的也会失败。

Solana 已经将此功能内置到运行时中,因此我们不需要实现 Multicall。在下面的示例中,我们在一个交易中初始化一个账户并向其写入 —— 而不使用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
});
});

以下是相应的 Rust 代码:

use anchor_lang::prelude::*;
use std::mem::size_of;

declare_id!("Ao9LdZtHdMAzrFUEfRNbKEb5H4nXvpRZC69kxeAGbTPE");

#[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 大数。
  • 我们不再需要使用await program.methods.initialize().accounts({pda: pda}).rpc(),而是使用await program.methods.initialize().accounts({pda: pda}).transaction()来创建一个交易。

Solana 交易大小限制

Solana 交易的总大小不能超过1232 字节

这意味着你将无法像在以太坊中那样批处理“无限”数量的交易并支付更多的 gas。

演示批处理交易的原子性

让我们修改我们 Rust 中的set函数,使其始终失败。这将帮助我们看到如果其中一个批处理的交易失败,initialize交易会被回滚。

以下 Rust 程序在调用set时始终返回错误:

use anchor_lang::prelude::*;
use std::mem::size_of;

declare_id!("Ao9LdZtHdMAzrFUEfRNbKEb5H4nXvpRZC69kxeAGbTPE");

#[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", () => {
// Configure the client to use the local cluster.
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.provider.wallet.payer;
const [pda, _bump] = anchor.web3.PublicKey.findProgramAddressSync([], program.programId);

// console.log the address of the pda
console.log(pda.toBase58());

let transaction = new anchor.web3.Transaction();
transaction.add(await program.methods.initialize().accounts({pda: pda}).transaction());
transaction.add(await program.methods.set(5).accounts({pda: pda}).transaction());

await anchor.web3.sendAndConfirmTransaction(anchor.getProvider().connection, transaction, [wallet]);
});
});

当我们运行测试,然后查询本地验证器以获取 pda 账户时,我们会发现它不存在。尽管初始化交易首先执行,但随后执行的设置交易失败,导致整个交易被取消,因此没有账户被初始化。

atomic multiple transaction fails

前端中的“如果需要则初始化”

你可以使用前端代码模拟init_if_needed的行为,同时拥有一个单独的initialize函数。然而,从用户的角度来看,当他们第一次使用账户时,所有这些都会被平滑处理,因为他们不必在第一次使用账户时发出多个交易。

要确定是否需要初始化一个账户,我们检查它是否具有零 lamports 或是否由系统程序拥有。以下是我们在 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.provider.wallet.payer;
const [pda, _bump] = anchor.web3.PublicKey.findProgramAddressSync([], program.programId);

let accountInfo = await anchor.getProvider().connection.getAccountInfo(pda);

let transaction = new anchor.web3.Transaction();
if (accountInfo == null || accountInfo.lamports == 0 || accountInfo.owner == anchor.web3.SystemProgram.programId) {
console.log("need to initialize");
const initTx = await program.methods.initialize().accounts({pda: pda}).transaction();
transaction.add(initTx);
}
else {
console.log("no need to initialize");
}

// we're going to set the number anyway
const setTx = await program.methods.set(5).accounts({pda: pda}).transaction();
transaction.add(setTx);

await anchor.web3.sendAndConfirmTransaction(anchor.getProvider().connection, transaction, [wallet]);

const pdaAcc = await program.account.pda.fetch(pda);
console.log(pdaAcc.value);
});
});

我们还需要修改我们的 Rust 代码,set操作上强制失败。

use anchor_lang::prelude::*;
use std::mem::size_of;

declare_id!("Ao9LdZtHdMAzrFUEfRNbKEb5H4nXvpRZC69kxeAGbTPE");

#[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(()) // ERROR HAS BEEN REMOVED
}
}

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

如果我们针对同一个本地验证器实例运行测试两次,我们将得到以下输出:

第一次测试运行:

batched transaction succeeds on first initialized

第二次测试运行:

batched transaction suceeds on second call with account already initialized

Solana 如何部署超过 1232 字节的程序?

如果你创建一个新的 Solana 程序并运行anchor deploy(或anchor test),你将在日志中看到有大量交易到BFPLoaderUpgradeable

anchor deploy logs with many transactions

在这里,Anchor 正在将部署的字节码拆分成多个交易,因为一次性部署整个字节码无法适应单个交易。我们可以通过将日志导向文件并计算发生的交易数量来查看它花费了多少交易:

solana logs > logs.txt
# run `anchor deploy` in another shell
grep "Transaction executed" logs.txt | wc -l

这将大致匹配在执行anchor testanchor deploy命令后暂时出现的内容:

transaction count to deploy program

关于如何将交易批处理的确切过程在 Solana 文档:Solana 程序部署工作原理中有描述。

这些交易列表是单独的交易,而不是批处理的交易。如果是批处理的话,将会超过 1232 字节的限制。

通过 RareSkills 了解更多

查看我们的 Solana 开发课程以获取更多 Solana 教程。