SolanaドキュメントLiteSVMRustプログラムのテスト

instructions の実行

概要

LiteSVMにプログラムをデプロイした後、デプロイされたプログラムと対話するためにinstructionsを実行する必要があります。

LiteSVMは、トランザクションの作成、送信、シミュレーションのためのシンプルなAPIを提供しており、Solanaのトランザクションモデルと完全に互換性があります。

基本的なトランザクションフロー

instructionsを実行する際の一般的なフローは以下の通りです:

  1. instructionsの作成 - 呼び出すプログラムとデータを定義する
  2. メッセージの構築 - 1つ以上のinstructionsを組み合わせる
  3. トランザクションの作成 - 必要な署名者でメッセージに署名する
  4. 送信またはシミュレーション - トランザクションを実行して結果を処理する

instructionsの作成

基本的なinstructionsの構造

use solana_instruction::{Instruction, AccountMeta};
use solana_pubkey::Pubkey;
let instruction = Instruction {
program_id: Pubkey::new_unique(), // The program to call
accounts: vec![ // Accounts the program needs
AccountMeta::new(account_pubkey, false), // Writable, not signer
AccountMeta::new_readonly(readonly_pubkey, false), // Read-only, not signer
AccountMeta::new(signer_pubkey, true), // Writable, signer
],
data: vec![0, 1, 2, 3], // Instruction data (program-specific)
};

トランザクションの構築と送信

方法1: 基本的なトランザクション

use litesvm::LiteSVM;
use solana_keypair::Keypair;
use solana_message::Message;
use solana_transaction::Transaction;
use solana_signer::Signer;
let mut svm = LiteSVM::new();
let payer = Keypair::new();
// Airdrop SOL for fees
svm.airdrop(&payer.pubkey(), 1_000_000_000).unwrap();
// Create instruction
let instruction = /* your instruction */;
// Build message with payer
let message = Message::new(&[instruction], Some(&payer.pubkey()));
// Create and sign transaction
let tx = Transaction::new_signed_with_payer(
&[instruction], // Your instructions
Some(&payer.pubkey()), // Who pays transaction fees
&[&payer], // All required signers
svm.latest_blockhash(), // Recent blockhash
);
// Send transaction
let result = svm.send_transaction(tx);

new_signed_with_payer の動作:

  • instructionsから自動的にメッセージを構築する
  • 提供されたすべてのkeypairでトランザクションに自動的に署名する
  • 完全に署名された、送信可能なトランザクションを返す

方法2: カスタムメッセージトランザクション

let payer = Keypair::new();
// Manually construct message
let message = Message::new(&[instruction], Some(&payer.pubkey()));
// Create transaction with signers, message, and blockhash
let tx = Transaction::new(
&[&payer],
message,
svm.latest_blockhash(),
);
let result = svm.send_transaction(tx);

次のような場合に Message を制御する必要があるときにこの方法を使用します:

  • バージョン管理されたトランザクション(ルックアップテーブルを使用したv0)

  • 部分署名/マルチシグワークフロー
  • Durable nonce
  • 署名前のシミュレーションまたは検査
  • カスタム手数料支払者ロジック
  • トランザクションサイズの最適化

バージョン管理されたトランザクション

LiteSVMはレガシーとバージョン管理の両方のトランザクションをサポートしています:

use solana_transaction::versioned::VersionedTransaction;
use solana_message::VersionedMessage;
// Legacy transaction (most common)
let legacy_msg = Message::new(&[instruction], Some(&payer.pubkey()));
let versioned_tx = VersionedTransaction::try_new(
VersionedMessage::Legacy(legacy_msg),
&[&payer]
).unwrap();
// Send versioned transaction
let result = svm.send_transaction(versioned_tx);

トランザクションの結果

成功したトランザクション

match svm.send_transaction(tx) {
Ok(meta) => {
println!("Signature: {}", meta.signature);
println!("Compute units: {}", meta.compute_units_consumed);
println!("Logs:");
for log in &meta.logs {
println!(" {}", log);
}
}
Err(err) => {
println!("Transaction failed: {:?}", err.err);
// Logs are still available on failure
println!("Failure logs: {:?}", err.meta.logs);
}
}

トランザクションメタデータフィールド

pub struct TransactionMetadata {
pub signature: Signature,
pub logs: Vec<String>,
pub inner_instructions: InnerInstructionsList,
pub compute_units_consumed: u64,
pub return_data: TransactionReturnData,
pub fee: u64,
}

トランザクションのシミュレーション

トランザクションのシミュレーションを使用すると、状態を変更せずにトランザクションをテストできます:

// Simulate instead of sending
match svm.simulate_transaction(tx) {
Ok(sim_result) => {
println!("Simulation successful!");
println!("Logs: {:?}", sim_result.meta.logs);
println!("Compute units: {}", sim_result.meta.compute_units_consumed);
}
Err(err) => {
println!("Simulation failed: {:?}", err.err);
}
}

エラー処理

よくあるトランザクションエラー

use solana_transaction_error::TransactionError;
use solana_instruction::error::InstructionError;
match svm.send_transaction(tx) {
Err(failed_tx) => {
match failed_tx.err {
TransactionError::InsufficientFundsForFee => {
println!("Not enough SOL for fees");
}
TransactionError::InvalidProgramForExecution => {
println!("Program doesn't exist or isn't executable");
}
TransactionError::InstructionError(index, err) => {
println!("Instruction {} failed: {:?}", index, err);
match err {
InstructionError::Custom(code) => {
println!("Custom error code: {}", code);
}
InstructionError::AccountNotFound => {
println!("An account doesn't exist");
}
_ => {}
}
}
TransactionError::BlockhashNotFound => {
println!("Blockhash expired or invalid");
}
_ => println!("Other error: {:?}", failed_tx.err),
}
}
Ok(_) => {}
}

プログラムログの操作

ログへのアクセス

let result = svm.send_transaction(tx).unwrap();
// All logs (including system logs)
for log in &result.logs {
println!("{}", log);
}
// Pretty-printed logs (formatted)
println!("{}", result.pretty_logs());

ログ出力の例

Program 11111111111111111111111111111111 invoke [1]
Program log: Processing instruction
Program 11111111111111111111111111111111 consumed 2000 compute units
Program 11111111111111111111111111111111 success

コンピュートバジェットの設定

グローバルコンピュートバジェットの設定

use solana_compute_budget::compute_budget::ComputeBudget;
let mut svm = LiteSVM::new()
.with_compute_budget(ComputeBudget {
compute_unit_limit: 200_000,
..Default::default()
});

トランザクションごとのコンピュートバジェット

use solana_compute_budget_interface::ComputeBudgetInstruction;
let instructions = vec![
// Set compute budget for this transaction
ComputeBudgetInstruction::set_compute_unit_limit(400_000),
ComputeBudgetInstruction::set_compute_unit_price(1),
// Your actual instruction
your_instruction,
];

System Program Instructions

LiteSVMにはsystem programのサポートが含まれています:

use solana_keypair::Keypair;
use solana_signer::Signer;
use solana_system_interface::instruction as system_instruction;
use solana_transaction::Transaction;
// Transfer SOL
let transfer_ix = system_instruction::transfer(
&alice.pubkey(),
&bob.pubkey(),
1_000_000_000, // 1 SOL
);
// Create new account
let create_ix = system_instruction::create_account(
&payer_pubkey,
&new_account_pubkey,
lamports,
space as u64,
&owner_program_id,
);

まとめ

LiteSVMでのinstructionsの実行は、標準的なSolanaトランザクションモデルに従います:

  1. プログラムID、アカウント、データを含むInstructionオブジェクトを作成する
  2. instructionsと必要な署名者を含むTransactionを構築する
  3. send_transaction()で実行するか、simulate_transaction()でテストする
  4. TransactionMetadataまたはエラーの詳細を確認して結果を処理する

LiteSVMは詳細なログとデバッグ情報を備えた即時実行を提供し、Solanaプログラムを効率的にテストするのに最適です。

Is this page helpful?

© 2026 Solana Foundation. 無断転載を禁じます。