LiteSVM 使用构建器模式进行配置。所有方法都会返回 LiteSVM,
以便链式调用。
典型的测试设置
import { createClient, generateKeyPairSigner } from "@solana/kit";
import { litesvm } from "@solana/kit-plugin-litesvm";
import { signer } from "@solana/kit-plugin-signer";
const mySigner = await generateKeyPairSigner();
const client = createClient().use(signer(mySigner)).use(litesvm());
client.svm
.withSigverify(false) // Skip signature verification (faster)
.withBlockhashCheck(false) // Skip blockhash validation (simpler)
.withTransactionHistory(100n); // Store last 100 transactions
litesvm() 客户端已经加载了 sysvar、内置程序、
预编译程序和默认程序,因此典型的设置只需调整
所需的检查项。
签名与 Blockhash
withSigverify
withSigverify(sigverify: boolean): LiteSVM
启用或禁用签名验证。
// Disable for faster tests (default is enabled)
client.svm.withSigverify(false);
// Enable for production-like testing
client.svm.withSigverify(true);
禁用签名验证会让测试运行得更快,但也会降低真实性。
withBlockhashCheck
withBlockhashCheck(check: boolean): LiteSVM
启用或禁用 blockhash 过期检查。
// Disable for simpler tests
client.svm.withBlockhashCheck(false);
程序与 Sysvar
这些方法会将程序和 sysvar 加载到虚拟机中。litesvm() 客户端
已经包含了所有这些内容,因此只有在重置客户端,或需要基于 litesvm 包设置
一个空白的 LiteSVM.default() 实例时,才需要用到它们。
withSysvars
将 Clock、Rent、EpochSchedule 和其他 sysvar 重置为默认值。
新建的客户端已经自带 sysvar,因此只有在需要撤销更改时才需要使用此方法。
在 setClock 或 warpToSlot 之后调用它,会丢弃你设置的时钟。
client.svm.setClock(myClock);
client.svm.withSysvars(); // the clock is back to its default
withBuiltins
添加运行时的内置程序,包括 System Program、BPF 加载器、Vote
和 Compute Budget。
client.svm.withBuiltins();
withPrecompiles
withPrecompiles(): LiteSVM
添加签名验证预编译程序,包括 ed25519、secp256k1 和 secp256r1。
client.svm.withPrecompiles();
withDefaultPrograms
withDefaultPrograms(): LiteSVM
添加默认程序,包括 Token、Token-2022、Associated Token Account、Memo、
Stake 和 Address Lookup Table。
client.svm.withDefaultPrograms();
资源限制
withLamports
withLamports(lamports: bigint): LiteSVM
设置内部水龙头账户的余额,airdrop() 会从该账户中划拨资金。
超过此余额的空投将会失败。
client.svm.withLamports(1_000_000_000n); // the faucet holds 1 SOL
withLogBytesLimit
withLogBytesLimit(limit?: bigint): LiteSVM
设置日志输出的字节数上限。
client.svm.withLogBytesLimit(10000n);
withTransactionHistory
withTransactionHistory(capacity: bigint): LiteSVM
启用交易历史记录存储。
// Store last 100 transactions
client.svm.withTransactionHistory(100n);
功能集
withFeatureSet
withFeatureSet(featureSet: FeatureSet): LiteSVM
配置功能集,以便针对特定的 Solana 版本进行测试。