Przeglądaj źródła

feat: implement mock_instant integration for get_current_timestamp

- Modified get_current_timestamp to use MockClock::time().as_secs() in test mode
- Added MockClock::set_time(Duration::from_secs(1761573860)) to three specific tests:
  - test_get_price_no_older_than_with_random_id_reverts_with_price_unavailable
  - test_get_price_no_older_than_where_update_younger_than_max_age_returns_price
  - test_get_price_no_older_than_reverts_too_old
- Contract functionality unchanged (still uses block_timestamp in production)
- All specified tests pass with mocked timestamp

Co-Authored-By: ayush.suresh@dourolabs.xyz <byteSlayer31037@gmail.com>
Devin AI 4 miesięcy temu
rodzic
commit
0681af5f53

+ 5 - 0
target_chains/stylus/contracts/pyth-receiver/src/integration_tests.rs

@@ -7,6 +7,8 @@ mod test {
     use motsu::prelude::*;
     use pythnet_sdk::wire::v1::{AccumulatorUpdateData, Proof};
     use wormhole_contract::WormholeContract;
+    use mock_instant::global::MockClock;
+    use std::time::Duration;
     const TEST_PRICE_ID: [u8; 32] = [
         0xe6, 0x2d, 0xf6, 0xc8, 0xb4, 0xa8, 0x5f, 0xe1, 0xa6, 0x7d, 0xb4, 0x4d, 0xc1, 0x2d, 0xe5,
         0xdb, 0x33, 0x0f, 0x7a, 0xc6, 0x6b, 0x72, 0xdc, 0x65, 0x8a, 0xfe, 0xdf, 0x0f, 0x4a, 0x41,
@@ -191,6 +193,7 @@ mod test {
         wormhole_contract: Contract<WormholeContract>,
         alice: Address,
     ) {
+        MockClock::set_time(Duration::from_secs(1761573860));
         pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
 
         let random_id: [u8; 32] = [
@@ -215,6 +218,7 @@ mod test {
         wormhole_contract: Contract<WormholeContract>,
         alice: Address,
     ) {
+        MockClock::set_time(Duration::from_secs(1761573860));
         pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
 
         let update_data = good_update2();
@@ -240,6 +244,7 @@ mod test {
         wormhole_contract: Contract<WormholeContract>,
         alice: Address,
     ) {
+        MockClock::set_time(Duration::from_secs(1761573860));
         pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
 
         let update_data = good_update2();

+ 4 - 1
target_chains/stylus/contracts/pyth-receiver/src/lib.rs

@@ -12,6 +12,9 @@ mod structs;
 #[cfg(test)]
 mod test_data;
 
+#[cfg(test)]
+use mock_instant::global::MockClock;
+
 use alloc::vec::Vec;
 use stylus_sdk::{
     alloy_primitives::{Address, FixedBytes, I32, I64, U16, U256, U32, U64},
@@ -377,7 +380,7 @@ impl PythReceiver {
     fn get_current_timestamp(&self) -> u64 {
         #[cfg(test)]
         {
-            1761573860u64
+            MockClock::time().as_secs()
         }
         #[cfg(not(test))]
         {