Ver Fonte

test(target_chains/fuel): add invalid merkle proof update data test (#1639)

* add invalid merkle proof test

* fix precommit
Daniel Chew há 1 ano atrás
pai
commit
f899a34206

+ 1 - 0
.gitignore

@@ -19,3 +19,4 @@ tsconfig.tsbuildinfo
 .envrc
 */*.sui.log*
 __pycache__
+.DS_Store

Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
target_chains/fuel/contracts/src/constants.rs


+ 8 - 9
target_chains/fuel/contracts/src/pyth_utils.rs

@@ -1,13 +1,10 @@
 use crate::constants::{
     BTC_USD_PRICE_FEED_ID, DEFAULT_SINGLE_UPDATE_FEE, ETH_USD_PRICE_FEED_ID,
     GUARDIAN_SET_UPGRADE_3_VAA, GUARDIAN_SET_UPGRADE_4_VAA, PYTH_CONTRACT_BINARY_PATH,
-    TEST_ACCUMULATOR_UPDATE_DATA, TEST_BATCH_UPDATE_DATA, UNI_USD_PRICE_FEED_ID,
-    USDC_USD_PRICE_FEED_ID,
-};
-use base64::{
-    engine::general_purpose,
-    prelude::{Engine, BASE64_STANDARD},
+    TEST_ACCUMULATOR_UPDATE_DATA, TEST_BATCH_UPDATE_DATA,
+    TEST_CORRUPTED_PROOF_ACCUMULATOR_UPDATE_DATA, UNI_USD_PRICE_FEED_ID, USDC_USD_PRICE_FEED_ID,
 };
+use base64::{engine::general_purpose, prelude::Engine};
 use fuels::{
     prelude::{abigen, CallParameters, Contract, LoadConfiguration, TxPolicies, WalletUnlocked},
     programs::call_response::FuelCallResponse,
@@ -82,10 +79,12 @@ pub fn test_batch_update_data_bytes() -> Vec<Bytes> {
 }
 
 pub fn test_accumulator_update_data_bytes() -> Vec<Bytes> {
+    vec![Bytes(hex::decode(TEST_ACCUMULATOR_UPDATE_DATA).unwrap())]
+}
+
+pub fn test_corrupted_proof_accumulator_update_data_bytes() -> Vec<Bytes> {
     vec![Bytes(
-        BASE64_STANDARD
-            .decode(TEST_ACCUMULATOR_UPDATE_DATA)
-            .unwrap(),
+        hex::decode(TEST_CORRUPTED_PROOF_ACCUMULATOR_UPDATE_DATA).unwrap(),
     )]
 }
 

+ 5 - 2
target_chains/fuel/contracts/tests/functions/pyth_core/ema_price.rs

@@ -41,7 +41,9 @@ mod success {
             .await
             .value;
 
-        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()).await;
+        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes())
+            .await
+            .unwrap();
 
         let eth_usd_ema_price = ema_price(&deployer.instance, default_price_feed_ids()[0])
             .await
@@ -88,7 +90,8 @@ mod success {
             fee,
             test_accumulator_update_data_bytes(),
         )
-        .await;
+        .await
+        .unwrap();
 
         let eth_usd_ema_price = ema_price(&deployer.instance, default_price_feed_ids()[0])
             .await

+ 5 - 2
target_chains/fuel/contracts/tests/functions/pyth_core/ema_price_no_older_than.rs

@@ -41,7 +41,9 @@ mod success {
             .await
             .value;
 
-        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()).await;
+        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes())
+            .await
+            .unwrap();
 
         let eth_usd_ema_price = ema_price_no_older_than(
             &deployer.instance,
@@ -96,7 +98,8 @@ mod success {
             fee,
             test_accumulator_update_data_bytes(),
         )
-        .await;
+        .await
+        .unwrap();
 
         let eth_usd_ema_price = ema_price_no_older_than(
             &deployer.instance,

+ 9 - 6
target_chains/fuel/contracts/tests/functions/pyth_core/ema_price_unsafe.rs

@@ -5,10 +5,10 @@ use crate::utils::interface::{
 use crate::utils::setup::setup_environment;
 use pyth_sdk::{
     constants::{
-        DEFAULT_SINGLE_UPDATE_FEE, DEFAULT_VALID_TIME_PERIOD, GOVERNANCE_DATA_SOURCE,
-        TEST_ACCUMULATOR_ETH_USD_PRICE_FEED, TEST_ACCUMULATOR_USDC_USD_PRICE_FEED,
-        TEST_BATCH_ETH_USD_PRICE_FEED, TEST_BATCH_USDC_USD_PRICE_FEED,
-        WORMHOLE_GOVERNANCE_DATA_SOURCE, DUMMY_CHAIN_ID
+        DEFAULT_SINGLE_UPDATE_FEE, DEFAULT_VALID_TIME_PERIOD, DUMMY_CHAIN_ID,
+        GOVERNANCE_DATA_SOURCE, TEST_ACCUMULATOR_ETH_USD_PRICE_FEED,
+        TEST_ACCUMULATOR_USDC_USD_PRICE_FEED, TEST_BATCH_ETH_USD_PRICE_FEED,
+        TEST_BATCH_USDC_USD_PRICE_FEED, WORMHOLE_GOVERNANCE_DATA_SOURCE,
     },
     pyth_utils::{
         default_data_sources, default_price_feed_ids, guardian_set_upgrade_3_addresses,
@@ -41,7 +41,9 @@ mod success {
             .await
             .value;
 
-        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()).await;
+        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes())
+            .await
+            .unwrap();
 
         let eth_usd_ema_price = ema_price_unsafe(&deployer.instance, default_price_feed_ids()[0])
             .await
@@ -88,7 +90,8 @@ mod success {
             fee,
             test_accumulator_update_data_bytes(),
         )
-        .await;
+        .await
+        .unwrap();
 
         let eth_usd_ema_price = ema_price_unsafe(&deployer.instance, default_price_feed_ids()[0])
             .await

+ 2 - 2
target_chains/fuel/contracts/tests/functions/pyth_core/price.rs

@@ -40,7 +40,7 @@ mod success {
             .await
             .value;
 
-        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()).await;
+        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()).await.unwrap();
 
         let eth_usd_price = price(&deployer.instance, default_price_feed_ids()[0])
             .await
@@ -87,7 +87,7 @@ mod success {
             fee,
             test_accumulator_update_data_bytes(),
         )
-        .await;
+        .await.unwrap();
 
         let eth_usd_price = price(&deployer.instance, default_price_feed_ids()[0])
             .await

+ 5 - 2
target_chains/fuel/contracts/tests/functions/pyth_core/price_no_older_than.rs

@@ -42,7 +42,9 @@ mod success {
             .await
             .value;
 
-        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()).await;
+        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes())
+            .await
+            .unwrap();
 
         let eth_usd_price = price_no_older_than(
             &deployer.instance,
@@ -97,7 +99,8 @@ mod success {
             fee,
             test_accumulator_update_data_bytes(),
         )
-        .await;
+        .await
+        .unwrap();
 
         let eth_usd_price = price_no_older_than(
             &deployer.instance,

+ 2 - 2
target_chains/fuel/contracts/tests/functions/pyth_core/price_unsafe.rs

@@ -40,7 +40,7 @@ mod success {
             .await
             .value;
 
-        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()).await;
+        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()).await.unwrap();
 
         let eth_usd_price = price_unsafe(&deployer.instance, default_price_feed_ids()[0])
             .await
@@ -87,7 +87,7 @@ mod success {
             fee,
             test_accumulator_update_data_bytes(),
         )
-        .await;
+        .await.unwrap();
 
         let eth_usd_price = price_unsafe(&deployer.instance, default_price_feed_ids()[0])
             .await

+ 77 - 2
target_chains/fuel/contracts/tests/functions/pyth_core/update_price_feeds.rs

@@ -4,6 +4,8 @@ use crate::utils::interface::{
     pyth_init::constructor,
 };
 use crate::utils::setup::setup_environment;
+
+use fuels::types::errors::{transaction::Reason, Error};
 use pyth_sdk::{
     constants::{
         DEFAULT_SINGLE_UPDATE_FEE, DEFAULT_VALID_TIME_PERIOD, DUMMY_CHAIN_ID,
@@ -12,8 +14,10 @@ use pyth_sdk::{
     pyth_utils::{
         default_data_sources, default_price_feed_ids, guardian_set_upgrade_3_addresses,
         test_accumulator_update_data_bytes, test_batch_update_data_bytes,
+        test_corrupted_proof_accumulator_update_data_bytes,
     },
 };
+
 mod success {
 
     use super::*;
@@ -52,7 +56,9 @@ mod success {
             (false, false)
         );
 
-        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()).await;
+        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes())
+            .await
+            .unwrap();
 
         // Final values
         assert_eq!(
@@ -107,7 +113,8 @@ mod success {
             fee,
             test_accumulator_update_data_bytes(),
         )
-        .await;
+        .await
+        .unwrap();
 
         // Final values
         assert_eq!(
@@ -123,3 +130,71 @@ mod success {
         );
     }
 }
+
+mod failure {
+
+    use super::*;
+
+    #[tokio::test]
+    async fn updates_price_feeds_for_accumulator_update_fail_merkle_proof_verification() {
+        let (_oracle_contract_id, deployer) = setup_environment().await.unwrap();
+
+        constructor(
+            &deployer.instance,
+            default_data_sources(),
+            GOVERNANCE_DATA_SOURCE,
+            WORMHOLE_GOVERNANCE_DATA_SOURCE,
+            DEFAULT_SINGLE_UPDATE_FEE,
+            DEFAULT_VALID_TIME_PERIOD,
+            guardian_set_upgrade_3_addresses(),
+            3,
+            DUMMY_CHAIN_ID,
+        )
+        .await;
+
+        let fee = update_fee(&deployer.instance, test_accumulator_update_data_bytes())
+            .await
+            .value;
+
+        // Initial values
+        assert_eq!(
+            (
+                price_feed_exists(&deployer.instance, default_price_feed_ids()[0])
+                    .await
+                    .value,
+                price_feed_exists(&deployer.instance, default_price_feed_ids()[1])
+                    .await
+                    .value
+            ),
+            (false, false)
+        );
+
+        let response = update_price_feeds(
+            &deployer.instance,
+            fee,
+            test_corrupted_proof_accumulator_update_data_bytes(),
+        )
+        .await;
+
+        let is_error = response.is_err();
+
+        assert!(is_error);
+
+        let error = response.unwrap_err();
+
+        if let Error::Transaction(Reason::Reverted {
+            reason,
+            revert_id: _,
+            receipts: _,
+        }) = error
+        {
+            assert_eq!(
+                reason, "InvalidProof",
+                "Expected InvalidProof error, got: {}",
+                reason
+            );
+        } else {
+            panic!("Expected RevertTransactionError, got another error type.");
+        }
+    }
+}

+ 2 - 2
target_chains/fuel/contracts/tests/functions/pyth_info/price_feed_unsafe.rs

@@ -41,7 +41,7 @@ mod success {
             .await
             .value;
 
-        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()).await;
+        update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()).await.unwrap();
 
         let eth_usd_price_feed = price_feed_unsafe(&deployer.instance, default_price_feed_ids()[0])
             .await
@@ -81,7 +81,7 @@ mod success {
             fee,
             test_accumulator_update_data_bytes(),
         )
-        .await;
+        .await.unwrap();
 
         let eth_usd_price_feed = price_feed_unsafe(&deployer.instance, default_price_feed_ids()[0])
             .await

+ 2 - 2
target_chains/fuel/contracts/tests/utils/interface/pyth_core.rs

@@ -2,6 +2,7 @@ use fuels::{
     accounts::wallet::WalletUnlocked,
     prelude::{Bytes, CallParameters, TxPolicies},
     programs::call_response::FuelCallResponse,
+    types::errors::Error,
     types::Bits256,
 };
 
@@ -121,7 +122,7 @@ pub(crate) async fn update_price_feeds(
     contract: &PythOracleContract<WalletUnlocked>,
     fee: u64,
     update_data: Vec<Bytes>,
-) -> FuelCallResponse<()> {
+) -> Result<FuelCallResponse<()>, Error> {
     contract
         .methods()
         .update_price_feeds(update_data)
@@ -129,7 +130,6 @@ pub(crate) async fn update_price_feeds(
         .unwrap()
         .call()
         .await
-        .unwrap()
 }
 
 pub(crate) async fn update_price_feeds_if_necessary(

Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff