环境配置

所有配置方法均使用构建者模式,配合 LiteSVM::default()

with_compute_budget

pub fn with_compute_budget(self, compute_budget: ComputeBudget) -> Self

设置计算单元限制和堆大小。

use solana_compute_budget::compute_budget::ComputeBudget;
let mut svm = LiteSVM::default()
.with_compute_budget(ComputeBudget {
compute_unit_limit: 1_400_000,
heap_size: 256 * 1024,
..ComputeBudget::default()
});

with_sigverify

pub fn with_sigverify(self, sigverify: bool) -> Self

启用或禁用签名验证。默认值:true

// Disable for faster tests
let mut svm = LiteSVM::default()
.with_sigverify(false);

with_blockhash_check

pub fn with_blockhash_check(self, check: bool) -> Self

启用或禁用区块哈希验证。默认值:true

// Disable to use any blockhash
let mut svm = LiteSVM::default()
.with_blockhash_check(false);

with_transaction_history

pub fn with_transaction_history(self, capacity: usize) -> Self

设置历史记录中保留的交易数量。默认值:0

// Keep last 100 transactions
let mut svm = LiteSVM::default()
.with_transaction_history(100);

with_log_bytes_limit

pub fn with_log_bytes_limit(self, limit: Option<usize>) -> Self

设置交易日志的最大字节数。默认值:Some(10_000)

// Unlimited logs
let mut svm = LiteSVM::default()
.with_log_bytes_limit(None);
// Custom limit (50KB)
let mut svm = LiteSVM::default()
.with_log_bytes_limit(Some(50_000));

with_lamports

pub fn with_lamports(self, lamports: u64) -> Self

设置用于费用收集的初始 lamport 数量。默认值:1_000_000_000

let mut svm = LiteSVM::default()
.with_lamports(10_000_000_000);

with_default_programs

pub fn with_default_programs(self) -> Self

包含默认的 SPL 程序(Token、Associated Token 等)。

let mut svm = LiteSVM::default()
.with_default_programs();

with_sysvars

pub fn with_sysvars(self) -> Self

包含默认的系统变量(Clock、Rent、EpochSchedule、SlotHashes、StakeHistory 等)。

let mut svm = LiteSVM::default()
.with_sysvars();

with_feature_set

pub fn with_feature_set(self, feature_set: FeatureSet) -> Self

配置 Solana 功能集,以测试特定的运行时特性。

use agave_feature_set::FeatureSet;
let mut svm = LiteSVM::default()
.with_feature_set(FeatureSet::all_enabled());

with_precompiles

#[cfg(feature = "precompiles")]
pub fn with_precompiles(self) -> Self

包含标准预编译程序(ed25519、secp256k1、secp256r1)。

let mut svm = LiteSVM::default()
.with_precompiles();

需要 precompiles 功能标志:

[dev-dependencies]
litesvm = { version = "0.11", features = ["precompiles"] }

with_feature_accounts

pub fn with_feature_accounts(self) -> Self

在测试环境中包含功能门控账户。这可确保测试中准确反映受功能门控的 Solana 运行时行为。

let mut svm = LiteSVM::default()
.with_feature_accounts();

register-tracing 功能

register-tracing 功能可在交易执行期间启用详细的 SBF 寄存器追踪。它隐含了 invocation-inspect-callback 功能。

[dev-dependencies]
litesvm = { version = "0.11", features = ["register-tracing"] }

使用 LiteSVM::new_debuggable(true) 创建一个启用寄存器追踪的实例:

#[cfg(feature = "register-tracing")]
let mut svm = LiteSVM::new_debuggable(true);

启用后,设置 SBF_TRACE_DISASSEMBLE 环境变量,以在寄存器追踪的同时输出反汇编结果:

SBF_TRACE_DISASSEMBLE=1 cargo test

set_invocation_inspect_callback

pub fn set_invocation_inspect_callback<C: InvocationInspectCallback + 'static>(&mut self, callback: C)

注册一个在每次程序调用前后触发的回调函数。需要 invocation-inspect-callback 功能。

[dev-dependencies]
litesvm = { version = "0.11", features = ["invocation-inspect-callback"] }
use litesvm::{LiteSVM, types::InvocationInspectCallback};
use solana_svm::invoke_context::InvokeContext;
use solana_svm_transaction::svm_message::SVMMessage;
struct MyInspector;
impl InvocationInspectCallback for MyInspector {
fn before_invocation(
&self,
_svm: &LiteSVM,
_tx: &impl SVMMessage,
_program_indices: &[u16],
_invoke_context: &InvokeContext,
) {
println!("Before program invocation");
}
fn after_invocation(
&self,
_svm: &LiteSVM,
_invoke_context: &InvokeContext,
_enable_register_tracing: bool,
) {
println!("After program invocation");
}
}
let mut svm = LiteSVM::new();
svm.set_invocation_inspect_callback(MyInspector);

Is this page helpful?

Table of Contents

Edit Page
©️ 2026 Solana 基金会版权所有