Cross Program Invocation
クロスプログラム呼び出し(CPI)は、あるSolanaプログラムが別のプログラムのinstructionsを直接呼び出す時に発生します。これによりプログラムの組み合わせが可能になります。Solanaのinstructionをネットワークに公開されるAPIエンドポイントと考えると、CPIは一つのエンドポイントが内部で別のエンドポイントを呼び出すようなものです。
CPIを行う際、プログラムはそのプログラムIDから派生したPDAの代わりに署名することができます。これらの署名権限は呼び出し元プログラムから呼び出し先プログラムへと拡張されます。
クロスプログラム呼び出しの例
CPIを行う際、アカウント権限は一つのプログラムから別のプログラムへと拡張されます。例えば、プログラムAが署名者アカウントと書き込み可能アカウントを持つinstructionを受け取ったとします。プログラムAがプログラムBにCPIを行うと、プログラムBはプログラムAと同じアカウントを元の権限で使用できます(つまり、プログラムBは署名者アカウントで署名でき、書き込み可能アカウントに書き込むことができます)。プログラムBが独自のCPIを行う場合、同じ権限を最大深さ4まで転送できます。
プログラムinstruction呼び出しの最大高さは
max_instruction_stack_depth
と呼ばれ、MAX_INSTRUCTION_STACK_DEPTH
定数の5に設定されています。
スタックの高さは最初のトランザクションで1から始まり、プログラムが別のinstructionを呼び出すたびに1ずつ増加し、CPIの呼び出し深さを4に制限します。
PDA署名者を使用したCPI
CPIでPDA署名者が必要な場合、invoke_signed関数が使用されます。この関数は署名者PDAを導出するための署名者シードを受け取ります。Solanaランタイムは内部的に呼び出し元プログラムのsigners_seedsとprogram_idを使用してcreate_program_addressを呼び出します。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を使用してPDAから受信者アカウントにSOLを転送する単一のinstructionが含まれています。
Anchor
以下の例は、AnchorプログラムでCPIを実装する3つのアプローチを示しています。これらの例は機能的に同等ですが、それぞれ異なるレベルの抽象化を示しています。
- 例1:Anchorの
CpiContextとヘルパー関数を使用します。 - 例2:
solana_programクレートからsystem_instruction::transfer関数を使用します。(例1はこの実装の抽象化です。) - 例3:CPIのinstructionを手動で構築します。この方法は、呼び出したいinstructionの構築を支援するクレートが利用できない場合に役立ちます。
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アカウントから別のアカウントにSOLを転送する単一のinstructionが含まれています。CPIは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関数は空のsigners_seeds配列を使用してinvoke_signed関数を呼び出します。空の署名者配列は、署名のためのPDAが不要であることを示します。
pub fn invoke(instruction: &Instruction, account_infos: &[AccountInfo]) -> ProgramResult {invoke_signed(instruction, account_infos, &[])}
以下の例では、AnchorとネイティブRustを使用してCPIを行います。一つのアカウントから別のアカウントにSOLを転送する単一のinstructionが含まれています。
Anchor
以下の例では、AnchorプログラムでCPIを実装する3つのアプローチを示しています。これらの例は機能的に同等ですが、それぞれ異なるレベルの抽象化を示しています。
- 例1:Anchorの
CpiContextとヘルパー関数を使用します。 - 例2:
solana_programクレートからsystem_instruction::transfer関数を使用します。 - 例3:CPIインストラクションを手動で構築します。このアプローチは、呼び出したいインストラクションを構築するためのクレートが存在しない場合に役立ちます。
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を転送する単一のinstructionが含まれています。(テストファイルは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?