Skip to content

Commit 2c0886c

Browse files
Implement timestamp manipulation for age validation testing
- Add get_current_timestamp() method with test-specific hardcoded timestamp - Modify test_get_price_no_older_than_too_old to properly test age validation - Use simplified approach with hardcoded timestamp (1761573860u64) instead of threading - Test now correctly fails with NewPriceUnavailable when price is too old - All existing tests continue to pass Co-Authored-By: [email protected] <[email protected]>
1 parent e449601 commit 2c0886c

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

target_chains/stylus/contracts/pyth-receiver/src/integration_tests.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ mod test {
55
use crate::error::PythReceiverError;
66
use alloy_primitives::{address, U256, Address, U64, I64, I32};
77
use stylus_sdk::testing::TestVM;
8-
use pythnet_sdk::wire::v1::PYTHNET_ACCUMULATOR_UPDATE_MAGIC;
98
use motsu::prelude::*;
109
use wormhole_contract::WormholeContract;
11-
1210
const TEST_PRICE_ID: [u8; 32] = [
1311
0xe6, 0x2d, 0xf6, 0xc8, 0xb4, 0xa8, 0x5f, 0xe1, 0xa6, 0x7d, 0xb4, 0x4d, 0xc1, 0x2d, 0xe5, 0xdb,
1412
0x33, 0x0f, 0x7a, 0xc6, 0x6b, 0x72, 0xdc, 0x65, 0x8a, 0xfe, 0xdf, 0x0f, 0x4a, 0x41, 0x5b, 0x43
@@ -333,15 +331,8 @@ mod test {
333331
assert!(result.is_ok());
334332

335333
let price_result = pyth_contract.sender(alice).get_price_no_older_than(TEST_PRICE_ID, 1);
336-
assert!(price_result.is_ok());
337-
assert_eq!(price_result.unwrap(), (
338-
U64::from(1751573860u64),
339-
I32::from_le_bytes((-8i32).to_le_bytes()),
340-
I64::from_le_bytes(10985663592646i64.to_le_bytes()),
341-
U64::from(4569386330u64),
342-
I64::from_le_bytes(10977795800000i64.to_le_bytes()),
343-
U64::from(3919318300u64)
344-
));
334+
assert!(price_result.is_err());
335+
assert_eq!(price_result.unwrap_err(), PythReceiverError::NewPriceUnavailable);
345336
}
346337

347338
#[motsu::test]

target_chains/stylus/contracts/pyth-receiver/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,18 @@ impl PythReceiver {
293293

294294
#[inline]
295295
fn is_no_older_than(&self, publish_time: U64, max_age: u64) -> bool {
296-
self.vm().block_timestamp().saturating_sub(publish_time.to::<u64>()) <= max_age
296+
self.get_current_timestamp().saturating_sub(publish_time.to::<u64>()) <= max_age
297+
}
298+
299+
fn get_current_timestamp(&self) -> u64 {
300+
#[cfg(test)]
301+
{
302+
1761573860u64
303+
}
304+
#[cfg(not(test))]
305+
{
306+
self.vm().block_timestamp()
307+
}
297308
}
298309
}
299310

@@ -304,4 +315,4 @@ fn parse_wormhole_proof(vaa: Vaa) -> Result<MerkleRoot<Keccak160>, PythReceiverE
304315
WormholePayload::Merkle(merkle_root) => merkle_root.root,
305316
});
306317
Ok(root)
307-
}
318+
}

0 commit comments

Comments
 (0)