所有配置方法都是在 LiteSVM::new() 上链式调用的构建器方法。
不要从 LiteSVM::default() 开始。它创建的是一个空白实例,不包含
sysvar、程序或空投账户,因此 airdrop 会 panic,交易也会
失败。LiteSVM::new() 会返回一个已完整设置好的实例,之后您可以使用下面的方法
对其进行自定义。
with_compute_budget
pub fn with_compute_budget(self, compute_budget: ComputeBudget) -> Self
设置计算单元上限和堆大小。
use solana_compute_budget::compute_budget::ComputeBudget;
let mut svm = LiteSVM::new()
.with_compute_budget(ComputeBudget {
compute_unit_limit: 1_400_000,
heap_size: 256 * 1024,
..ComputeBudget::new_with_defaults(true)
});
with_sigverify
pub fn with_sigverify(self, sigverify: bool) -> Self
启用或禁用签名验证。默认值:true。
// Disable for faster tests
let mut svm = LiteSVM::new()
.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::new()
.with_blockhash_check(false);
with_transaction_history
pub fn with_transaction_history(self, capacity: usize) -> Self
设置历史记录中保留的交易数量。默认值:32。传入 0
会禁用交易历史记录,同时也允许重复(完全相同)的
交易被再次发送。
// Keep last 100 transactions
let mut svm = LiteSVM::new()
.with_transaction_history(100);
// Disable history and allow duplicate transactions
let mut svm = LiteSVM::new()
.with_transaction_history(0);
在 litesvm 0.18 中,任何小于或等于 32 的值都会静默禁用历史记录,效果与
0 相同:get_transaction 返回 None,并且重复的交易会被
接受。若要保留历史记录,请使用大于 32 的值,例如 100。大于
32 的值会扩大缓冲区,但实际保留的交易数量可能与您传入的值
略有差异。
with_log_bytes_limit
pub fn with_log_bytes_limit(self, limit: Option<usize>) -> Self
设置交易日志的最大字节数。默认值:Some(10_000)。
// Unlimited logs
let mut svm = LiteSVM::new()
.with_log_bytes_limit(None);
// Custom limit (50KB)
let mut svm = LiteSVM::new()
.with_log_bytes_limit(Some(50_000));
with_lamports
pub fn with_lamports(self, lamports: u64) -> Self
设置 LiteSVM 空投(水龙头)账户的初始余额,该账户为
airdrop 调用提供资金。默认值:1_000_000_000_000_000(1e15 lamports)。
let mut svm = LiteSVM::new()
.with_lamports(10_000_000_000);
with_default_programs
pub fn with_default_programs(self) -> Self
包含默认的 SPL 程序(Token、Associated Token 等)。
let mut svm = LiteSVM::new()
.with_default_programs();
with_sysvars
pub fn with_sysvars(self) -> Self
包含默认的 sysvar(Clock、Rent、EpochSchedule、SlotHashes、StakeHistory
等)。
let mut svm = LiteSVM::new()
.with_sysvars();
with_feature_set
pub fn with_feature_set(self, feature_set: FeatureSet) -> Self
配置 Solana 功能集,用于测试特定的运行时功能。
use agave_feature_set::FeatureSet;
use solana_sdk::native_token::LAMPORTS_PER_SOL;
let mut svm = LiteSVM::default()
.with_feature_set(FeatureSet::all_enabled())
.with_builtins()
.with_lamports(1_000_000 * LAMPORTS_PER_SOL)
.with_sysvars()
.with_feature_accounts()
.with_default_programs()
.with_sigverify(true)
.with_blockhash_check(true);
请先在 LiteSVM::default() 上设置功能集:在其之后添加的内置程序、sysvar 和
功能账户都会遵循该功能集。将 with_feature_set 链式调用到
LiteSVM::new() 上,则会保留 new() 为主网功能所设置的 Rent sysvar 和功能账户。
with_precompiles
#[cfg(feature = "precompiles")]
pub fn with_precompiles(self) -> Self
包含标准预编译程序(ed25519、secp256k1、secp256r1)。除非启用了 precompiles 功能,
否则它们不属于 LiteSVM::new()。
let mut svm = LiteSVM::new()
.with_precompiles();
需要启用 precompiles 功能标志:[dev-dependencies]
litesvm = { version = "0.18", features = ["precompiles"] }
with_feature_accounts
pub fn with_feature_accounts(self) -> Self
在测试环境中包含功能开关账户。这可确保
由功能开关控制的 Solana 运行时行为能在您的测试中得到准确体现。
let mut svm = LiteSVM::new()
.with_feature_accounts();
register-tracing 功能
register-tracing 功能可在交易执行期间启用详细的 SBF 寄存器追踪。它隐含启用 invocation-inspect-callback 功能。
[dev-dependencies]
litesvm = { version = "0.18", 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.18", features = ["invocation-inspect-callback"] }
solana-program-runtime = "4"
solana-transaction = "4"
use litesvm::{InvocationInspectCallback, LiteSVM};
use solana_program_runtime::invoke_context::InvokeContext;
use solana_transaction::sanitized::SanitizedTransaction;
struct MyInspector;
impl InvocationInspectCallback for MyInspector {
fn before_invocation(
&self,
_svm: &LiteSVM,
_tx: &SanitizedTransaction,
_program_indices: &[u16],
_invoke_context: &mut InvokeContext,
_enable_register_tracing: bool,
) {
println!("Before program invocation");
}
fn after_invocation(
&self,
_svm: &LiteSVM,
_tx: &SanitizedTransaction,
_program_indices: &[u16],
_invoke_context: &InvokeContext,
_enable_register_tracing: bool,
) {
println!("After program invocation");
}
}
let mut svm = LiteSVM::new();
svm.set_invocation_inspect_callback(MyInspector);