시간 기반 테스트

개요

이 예제는 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 워핑: 시간을 앞으로 이동하려면 warp_to_slot()를 사용하세요
  3. 타임스탬프 계산: Solana에서 slot은 대략 ~0.4초에 해당합니다
  4. 잠금 해제 테스트: 잠금 해제 시간을 워핑하여 시간 잠금 기능을 테스트하세요
  5. 되돌리기 불가: 시간은 앞으로만 워핑할 수 있으며, 뒤로는 되돌릴 수 없습니다

일반적인 사용 사례

  • 베스팅 일정
  • 시간 잠금 출금
  • 스테이크/언스테이크 쿨다운 기간
  • 경매 종료 시간
  • 임대 기간

Is this page helpful?

목차

페이지 편집