Cross Program Invocation(CPI)指的是一个 Solana 程序直接调用另一个程序的指令。这使得程序具备可组合性。如果你把 Solana 的instruction看作程序向网络暴露的 API 端点,那么 CPI 就像是一个端点在内部调用另一个端点。
在进行 CPI 时,程序可以代表由其 program ID 派生的 PDA 进行签名。这些签名权限会从调用方程序传递给被调用方程序。
跨程序调用示例
进行 CPI 时,账户权限会从一个程序扩展到另一个程序。假设程序 A 收到一个包含签名账户和可写账户的指令,然后程序 A 对程序 B 发起 CPI。此时,程序 B 可以像程序 A 一样使用这些账户,并保留原有权限(也就是说,程序 B 可以用签名账户签名,也可以写入可写账户)。如果程序 B 继续发起自己的 CPI,这些权限也可以继续传递下去,最多可传递 4 层。
程序指令调用的最大高度被称为
max_instruction_stack_depth,
并被设置为常量 MAX_INSTRUCTION_STACK_DEPTH,其值为 5。
堆栈高度从初始交易的 1 开始,每当一个程序调用另一个指令时加 1,因此 CPI 的调用深度限制为 4。
带有 PDA 签名者的 CPI
当 CPI 需要 PDA 签名者时,会使用
invoke_signed
函数。它接收用于派生签名者 PDA 的
signer seeds。Solana 运行时会在内部调用
create_program_address,并传入调用方程序的
signers_seeds 和 program_id。当 PDA 验证通过后,
会被添加为有效签名者。
pub fn invoke_signed(instruction: &Instruction,account_infos: &[AccountInfo],signers_seeds: &[&[&[u8]]],) -> ProgramResult {// --snip--invoke_signed_unchecked(instruction, account_infos, signers_seeds)}
以下示例展示了如何使用 Anchor 和原生 Rust,通过 PDA 签名者进行 CPI。每个示例都包含一个通过 PDA 签名的 CPI,将 SOL 从 PDA 转账到收款账户的指令。
Anchor
以下示例展示了在 Anchor 程序中实现 CPI 的三种方法。这些示例在功能上是等效的,但每个示例展示了不同的抽象级别。
- 示例 1:使用 Anchor 的
CpiContext及辅助函数。 - 示例 2:使用
system_instruction::transfer函数,来自solana_programcrate。(示例 1 是该实现的抽象。) - 示例 3:手动构造 CPI 指令。当没有可用 crate 帮助构建你想调用的指令时,这种方式很有用。
use anchor_lang::prelude::*;use anchor_lang::system_program::{transfer, Transfer};declare_id!("BrcdB9sV7z9DvF9rDHG263HUxXgJM3iCQdF36TcxbFEn");#[program]pub mod cpi {use super::*;pub fn sol_transfer(ctx: Context<SolTransfer>, amount: u64) -> Result<()> {let from_pubkey = ctx.accounts.pda_account.to_account_info();let to_pubkey = ctx.accounts.recipient.to_account_info();let program_id = ctx.accounts.system_program.to_account_info();let seed = to_pubkey.key();let bump_seed = ctx.bumps.pda_account;let signer_seeds: &[&[&[u8]]] = &[&[b"pda", seed.as_ref(), &[bump_seed]]];let cpi_context = CpiContext::new(program_id,Transfer {from: from_pubkey,to: to_pubkey,},).with_signer(signer_seeds);transfer(cpi_context, amount)?;Ok(())}}#[derive(Accounts)]pub struct SolTransfer<'info> {#[account(mut,seeds = [b"pda", recipient.key().as_ref()],bump,)]pda_account: SystemAccount<'info>,#[account(mut)]recipient: SystemAccount<'info>,system_program: Program<'info, System>,}
Rust
以下示例展示了如何在原生 Rust 程序中通过 PDA 签名者进行 CPI。它包含一个通过 PDA 账户签名的 CPI,将 SOL 从 PDA 账户转账到另一个账户。(测试文件使用 LiteSVM 测试该程序。)
use borsh::BorshDeserialize;use solana_program::{account_info::AccountInfo,entrypoint,entrypoint::ProgramResult,program::invoke_signed,program_error::ProgramError,pubkey::Pubkey,system_instruction,};// Declare program entrypointentrypoint!(process_instruction);// Define program instructions#[derive(BorshDeserialize)]enum ProgramInstruction {SolTransfer { amount: u64 },}impl ProgramInstruction {fn unpack(input: &[u8]) -> Result<Self, ProgramError> {Self::try_from_slice(input).map_err(|_| ProgramError::InvalidInstructionData)}}pub fn process_instruction(program_id: &Pubkey,accounts: &[AccountInfo],instruction_data: &[u8],) -> ProgramResult {// Deserialize instruction datalet instruction = ProgramInstruction::unpack(instruction_data)?;// Process instructionmatch instruction {ProgramInstruction::SolTransfer { amount } => {// Parse accountslet [pda_account_info, recipient_info, system_program_info] = accounts else {return Err(ProgramError::NotEnoughAccountKeys);};// Derive PDA and verify it matches the account provided by clientlet recipient_pubkey = recipient_info.key;let seeds = &[b"pda", recipient_pubkey.as_ref()];let (expected_pda, bump_seed) = Pubkey::find_program_address(seeds, program_id);if expected_pda != *pda_account_info.key {return Err(ProgramError::InvalidArgument);}// Create the transfer instructionlet transfer_ix = system_instruction::transfer(pda_account_info.key,recipient_info.key,amount,);// Create signer seeds for PDAlet signer_seeds: &[&[&[u8]]] = &[&[b"pda", recipient_pubkey.as_ref(), &[bump_seed]]];// Invoke the transfer instruction with PDA as signerinvoke_signed(&transfer_ix,&[pda_account_info.clone(),recipient_info.clone(),system_program_info.clone(),],signer_seeds,)?;Ok(())}}}
无 PDA 签名者的 CPI
当 CPI 不需要 PDA 签名者时,会使用
invoke
函数。该 invoke 函数会调用 invoke_signed 函数,并传入一个空的
signers_seeds 数组。空的签名者数组表示不需要任何 PDA 进行签名。
pub fn invoke(instruction: &Instruction, account_infos: &[AccountInfo]) -> ProgramResult {invoke_signed(instruction, account_infos, &[])}
下方示例展示了如何使用 Anchor 和原生 Rust 进行 CPI。每个示例都包含一个指令,将 SOL 从一个账户转账到另一个账户。
Anchor
以下示例展示了在 Anchor 程序中实现 CPI 的三种方法。这些示例在功能上是等效的,但每个示例展示了不同的抽象级别。
- 示例 1:使用 Anchor 的
CpiContext及辅助函数。 - 示例 2:使用
system_instruction::transfer函数,来自 crate。 - 示例 3:手动构造 CPI 指令。当没有可用 crate 帮助构建你想调用的指令时,这种方式很有用。
use anchor_lang::prelude::*;use anchor_lang::system_program::{transfer, Transfer};declare_id!("9AvUNHjxscdkiKQ8tUn12QCMXtcnbR9BVGq3ULNzFMRi");#[program]pub mod cpi {use super::*;pub fn sol_transfer(ctx: Context<SolTransfer>, amount: u64) -> Result<()> {let from_pubkey = ctx.accounts.sender.to_account_info();let to_pubkey = ctx.accounts.recipient.to_account_info();let program_id = ctx.accounts.system_program.to_account_info();let cpi_context = CpiContext::new(program_id,Transfer {from: from_pubkey,to: to_pubkey,},);transfer(cpi_context, amount)?;Ok(())}}#[derive(Accounts)]pub struct SolTransfer<'info> {#[account(mut)]sender: Signer<'info>,#[account(mut)]recipient: SystemAccount<'info>,system_program: Program<'info, System>,}
Rust
下方示例展示了如何在原生 Rust 编写的程序中进行 CPI。示例包含一个指令,将 SOL 从一个账户转账到另一个账户。(测试文件使用 LiteSVM 测试程序。)
use borsh::BorshDeserialize;use solana_program::{account_info::AccountInfo,entrypoint,entrypoint::ProgramResult,program::invoke,program_error::ProgramError,pubkey::Pubkey,system_instruction,};// Declare program entrypointentrypoint!(process_instruction);// Define program instructions#[derive(BorshDeserialize)]enum ProgramInstruction {SolTransfer { amount: u64 },}impl ProgramInstruction {fn unpack(input: &[u8]) -> Result<Self, ProgramError> {Self::try_from_slice(input).map_err(|_| ProgramError::InvalidInstructionData)}}pub fn process_instruction(_program_id: &Pubkey,accounts: &[AccountInfo],instruction_data: &[u8],) -> ProgramResult {// Deserialize instruction datalet instruction = ProgramInstruction::unpack(instruction_data)?;// Process instructionmatch instruction {ProgramInstruction::SolTransfer { amount } => {// Parse accountslet [sender_info, recipient_info, system_program_info] = accounts else {return Err(ProgramError::NotEnoughAccountKeys);};// Verify the sender is a signerif !sender_info.is_signer {return Err(ProgramError::MissingRequiredSignature);}// Create and invoke the transfer instructionlet transfer_ix = system_instruction::transfer(sender_info.key,recipient_info.key,amount,);invoke(&transfer_ix,&[sender_info.clone(),recipient_info.clone(),system_program_info.clone(),],)?;Ok(())}}}
Is this page helpful?