時間ベースのテスト

概要

この例では、LiteSVMでslotとタイムスタンプを操作することにより、時間依存の機能をテストする方法を示します。

完全なテスト

tests/time_test.rs
use litesvm::LiteSVM;
use solana_sdk::clock::Clock;
#[test]
fn test_time_locked_feature() {
let mut svm = LiteSVM::new();
// Get current time
let clock: Clock = svm.get_sysvar();
println!("Starting slot: {}", clock.slot);
println!("Starting timestamp: {}", clock.unix_timestamp);
// Test something at current time
// ... your test logic ...
// Jump forward 100 slots
svm.warp_to_slot(clock.slot + 100);
// Verify time changed
let new_clock: Clock = svm.get_sysvar();
assert_eq!(new_clock.slot, clock.slot + 100);
// Test time-locked feature is now available
// ... your test logic ...
println!("Time travel successful!");
}

slotベースの機能のテスト

#[test]
fn test_slot_based_unlock() {
let mut svm = LiteSVM::new();
// Get starting slot
let start_slot = svm.get_sysvar::<Clock>().slot;
// Deploy program with slot-based lock
let program_id = deploy_locked_program(&mut svm);
// Try to call before unlock slot (should fail)
let result = call_program(&mut svm, program_id);
assert!(result.is_err());
// Warp to unlock slot
svm.warp_to_slot(start_slot + 1000);
// Now it should succeed
let result = call_program(&mut svm, program_id);
assert!(result.is_ok());
}

タイムスタンプベースの機能のテスト

#[test]
fn test_timestamp_based_feature() {
let mut svm = LiteSVM::new();
// Get current timestamp
let clock: Clock = svm.get_sysvar();
let current_time = clock.unix_timestamp;
// Warp forward 1 hour (3600 seconds)
let target_slot = clock.slot + (3600.0 / 0.4) as u64; // ~0.4s per slot
svm.warp_to_slot(target_slot);
// Verify timestamp increased
let new_clock: Clock = svm.get_sysvar();
assert!(new_clock.unix_timestamp >= current_time + 3600);
}

重要なポイント

  1. Clock Sysvar: svm.get_sysvar::<Clock>() を使用して現在の時刻を取得する
  2. Slot Warping: warp_to_slot() を使用して時間を先へジャンプする
  3. タイムスタンプの計算: Solanaではslotはおよそ〜0.4秒に相当する
  4. アンロックのテスト: アンロック時刻を超えてワープすることで、時間ロックされた機能をテストする
  5. 後戻り不可: 時間は前方にのみワープでき、過去に戻ることはできない

一般的なユースケース

  • ベスティングスケジュール
  • 時間ロックされた引き出し
  • ステーク/アンステークのクールダウン期間
  • オークションの終了時刻
  • レンタル期間

Is this page helpful?

目次

ページを編集
© 2026 Solana Foundation. 無断転載を禁じます。