Browse Source

figured out test_set_data_source errors

Ayush Suresh 4 months ago
parent
commit
2aae27707e

+ 33 - 11
target_chains/stylus/contracts/pyth-receiver/src/error.rs

@@ -43,20 +43,30 @@ impl core::fmt::Debug for PythReceiverError {
             PythReceiverError::InvalidMerklePath => write!(f, "InvalidMerklePath"),
             PythReceiverError::InvalidUnknownSource => write!(f, "InvalidUnknownSource"),
             PythReceiverError::NewPriceUnavailable => write!(f, "NewPriceUnavailable"),
-            PythReceiverError::InvalidAccumulatorMessageType => write!(f, "InvalidAccumulatorMessageType"),
+            PythReceiverError::InvalidAccumulatorMessageType => {
+                write!(f, "InvalidAccumulatorMessageType")
+            }
             PythReceiverError::InsufficientFee => write!(f, "InsufficientFee"),
             PythReceiverError::InvalidEmitterAddress => write!(f, "InvalidEmitterAddress"),
             PythReceiverError::TooManyUpdates => write!(f, "TooManyUpdates"),
-            PythReceiverError::PriceFeedNotFoundWithinRange => write!(f, "PriceFeedNotFoundWithinRange"),
+            PythReceiverError::PriceFeedNotFoundWithinRange => {
+                write!(f, "PriceFeedNotFoundWithinRange")
+            }
             PythReceiverError::NoFreshUpdate => write!(f, "NoFreshUpdate"),
             PythReceiverError::PriceFeedNotFound => write!(f, "PriceFeedNotFound"),
             PythReceiverError::InvalidGovernanceMessage => write!(f, "InvalidGovernanceMessage"),
             PythReceiverError::InvalidGovernanceTarget => write!(f, "InvalidGovernanceTarget"),
             PythReceiverError::InvalidGovernanceAction => write!(f, "InvalidGovernanceAction"),
-            PythReceiverError::InvalidGovernanceDataSource => write!(f, "InvalidGovernanceDataSource"),
+            PythReceiverError::InvalidGovernanceDataSource => {
+                write!(f, "InvalidGovernanceDataSource")
+            }
             PythReceiverError::OldGovernanceMessage => write!(f, "OldGovernanceMessage"),
-            PythReceiverError::GovernanceMessageAlreadyExecuted => write!(f, "GovernanceMessageAlreadyExecuted"),
-            PythReceiverError::InvalidWormholeAddressToSet => write!(f, "InvalidWormholeAddressToSet"),
+            PythReceiverError::GovernanceMessageAlreadyExecuted => {
+                write!(f, "GovernanceMessageAlreadyExecuted")
+            }
+            PythReceiverError::InvalidWormholeAddressToSet => {
+                write!(f, "InvalidWormholeAddressToSet")
+            }
         }
     }
 }
@@ -70,25 +80,37 @@ impl core::fmt::Display for PythReceiverError {
             PythReceiverError::InvalidVaa => write!(f, "Invalid VAA"),
             PythReceiverError::InvalidWormholeMessage => write!(f, "Invalid Wormhole message"),
             PythReceiverError::InvalidMerkleProof => write!(f, "Invalid Merkle proof"),
-            PythReceiverError::InvalidAccumulatorMessage => write!(f, "Invalid accumulator message"),
+            PythReceiverError::InvalidAccumulatorMessage => {
+                write!(f, "Invalid accumulator message")
+            }
             PythReceiverError::InvalidMerkleRoot => write!(f, "Invalid Merkle root"),
             PythReceiverError::InvalidMerklePath => write!(f, "Invalid Merkle path"),
             PythReceiverError::InvalidUnknownSource => write!(f, "Invalid unknown source"),
             PythReceiverError::NewPriceUnavailable => write!(f, "New price unavailable"),
-            PythReceiverError::InvalidAccumulatorMessageType => write!(f, "Invalid accumulator message type"),
+            PythReceiverError::InvalidAccumulatorMessageType => {
+                write!(f, "Invalid accumulator message type")
+            }
             PythReceiverError::InsufficientFee => write!(f, "Insufficient fee"),
             PythReceiverError::InvalidEmitterAddress => write!(f, "Invalid emitter address"),
             PythReceiverError::TooManyUpdates => write!(f, "Too many updates"),
-            PythReceiverError::PriceFeedNotFoundWithinRange => write!(f, "Price feed not found within range"),
+            PythReceiverError::PriceFeedNotFoundWithinRange => {
+                write!(f, "Price feed not found within range")
+            }
             PythReceiverError::NoFreshUpdate => write!(f, "No fresh update"),
             PythReceiverError::PriceFeedNotFound => write!(f, "Price feed not found"),
             PythReceiverError::InvalidGovernanceMessage => write!(f, "Invalid governance message"),
             PythReceiverError::InvalidGovernanceTarget => write!(f, "Invalid governance target"),
             PythReceiverError::InvalidGovernanceAction => write!(f, "Invalid governance action"),
-            PythReceiverError::InvalidGovernanceDataSource => write!(f, "Invalid governance data source"),
+            PythReceiverError::InvalidGovernanceDataSource => {
+                write!(f, "Invalid governance data source")
+            }
             PythReceiverError::OldGovernanceMessage => write!(f, "Old governance message"),
-            PythReceiverError::GovernanceMessageAlreadyExecuted => write!(f, "Governance message already executed"),
-            PythReceiverError::InvalidWormholeAddressToSet => write!(f, "Invalid Wormhole address to set"),
+            PythReceiverError::GovernanceMessageAlreadyExecuted => {
+                write!(f, "Governance message already executed")
+            }
+            PythReceiverError::InvalidWormholeAddressToSet => {
+                write!(f, "Invalid Wormhole address to set")
+            }
         }
     }
 }

+ 7 - 3
target_chains/stylus/contracts/pyth-receiver/src/lib.rs

@@ -543,9 +543,13 @@ impl PythReceiver {
         let instruction = governance_structs::parse_instruction(vm.body.payload.to_vec())
             .map_err(|_| PythReceiverError::InvalidGovernanceMessage)?;
 
-        if instruction.target_chain_id != 0
-            && instruction.target_chain_id != self.vm().chain_id() as u16
-        {
+        let chain_id_config = Call::new();
+
+        let wormhole_id = wormhole
+            .chain_id(chain_id_config)
+            .map_err(|_| PythReceiverError::InvalidWormholeMessage)?;
+
+        if instruction.target_chain_id != 0 && instruction.target_chain_id != wormhole_id {
             return Err(PythReceiverError::InvalidGovernanceTarget);
         }
 

+ 22 - 24
target_chains/stylus/contracts/pyth-receiver/src/pyth_governance_test.rs

@@ -4,13 +4,13 @@ mod test {
     use crate::test_data::*;
     use crate::PythReceiver;
     use alloy_primitives::{address, Address, U256};
+    use hex::FromHex;
     use mock_instant::global::MockClock;
     use motsu::prelude::*;
     use pythnet_sdk::wire::v1::{AccumulatorUpdateData, Proof};
     use std::time::Duration;
-    use hex::FromHex;
     use wormhole_contract::WormholeContract;
-    use wormhole_vaas::{Vaa, Readable, Writeable};
+    use wormhole_vaas::{Readable, Vaa, Writeable};
 
     const PYTHNET_CHAIN_ID: u16 = 26;
     const PYTHNET_EMITTER_ADDRESS: [u8; 32] = [
@@ -19,27 +19,31 @@ mod test {
         0xaa, 0x71,
     ];
 
-    const CHAIN_ID: u16 = 60051;
+    const CHAIN_ID: u16 = 2;
     const GOVERNANCE_CONTRACT: U256 = U256::from_limbs([4, 0, 0, 0]);
 
     const SINGLE_UPDATE_FEE_IN_WEI: U256 = U256::from_limbs([100, 0, 0, 0]);
     const TRANSACTION_FEE_IN_WEI: U256 = U256::from_limbs([32, 0, 0, 0]);
-    
+
     const TEST_SIGNER1: Address = Address::new([
-        0xbe, 0xFA, 0x42, 0x9d, 0x57, 0xcD, 0x18, 0xb7, 0xF8, 0xA4, 0xd9, 0x1A, 0x2d, 0xa9, 0xAB, 0x4A, 0xF0, 0x5d, 0x0F, 0xBe
+        0xbe, 0xFA, 0x42, 0x9d, 0x57, 0xcD, 0x18, 0xb7, 0xF8, 0xA4, 0xd9, 0x1A, 0x2d, 0xa9, 0xAB,
+        0x4A, 0xF0, 0x5d, 0x0F, 0xBe,
     ]);
     const TEST_SIGNER2: Address = Address::new([
-        0x4b, 0xa0, 0xC2, 0xdb, 0x9A, 0x26, 0x20, 0x8b, 0x3b, 0xB1, 0xa5, 0x0B, 0x01, 0xb1, 0x69, 0x41, 0xc1, 0x0D, 0x76, 0xdb
+        0x4b, 0xa0, 0xC2, 0xdb, 0x9A, 0x26, 0x20, 0x8b, 0x3b, 0xB1, 0xa5, 0x0B, 0x01, 0xb1, 0x69,
+        0x41, 0xc1, 0x0D, 0x76, 0xdb,
     ]);
     const GOVERNANCE_CHAIN_ID: u16 = 1;
     const GOVERNANCE_EMITTER: [u8; 32] = [
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x11,
     ];
     const TEST_PYTH2_WORMHOLE_CHAIN_ID: u16 = 1;
     const TEST_PYTH2_WORMHOLE_EMITTER: [u8; 32] = [
-        0x71, 0xf8, 0xdc, 0xb8, 0x63, 0xd1, 0x76, 0xe2, 0xc4, 0x20, 0xad, 0x66, 0x10, 0xcf, 0x68, 0x73,
-        0x59, 0x61, 0x2b, 0x6f, 0xb3, 0x92, 0xe0, 0x64, 0x2b, 0x0c, 0xa6, 0xb1, 0xf1, 0x86, 0xaa, 0x3b
+        0x71, 0xf8, 0xdc, 0xb8, 0x63, 0xd1, 0x76, 0xe2, 0xc4, 0x20, 0xad, 0x66, 0x10, 0xcf, 0x68,
+        0x73, 0x59, 0x61, 0x2b, 0x6f, 0xb3, 0x92, 0xe0, 0x64, 0x2b, 0x0c, 0xa6, 0xb1, 0xf1, 0x86,
+        0xaa, 0x3b,
     ];
     const TARGET_CHAIN_ID: u16 = 2;
 
@@ -48,18 +52,17 @@ mod test {
         pyth_contract: &Contract<PythReceiver>,
         wormhole_contract: &Contract<WormholeContract>,
         alice: &Address,
+        guardian_set_index: u32,
     ) {
-        let guardians = vec![
-            address!("0x6579c588be2026d866231cccc364881cc1219c56")
-        ];
-        
+        let guardians = vec![address!("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf")];
+
         let governance_contract =
             Address::from_slice(&GOVERNANCE_CONTRACT.to_be_bytes::<32>()[12..32]);
         wormhole_contract
             .sender(*alice)
             .initialize(
                 guardians,
-                4,
+                guardian_set_index,
                 CHAIN_ID,
                 GOVERNANCE_CHAIN_ID,
                 governance_contract,
@@ -93,19 +96,14 @@ mod test {
         wormhole_contract: Contract<WormholeContract>,
         alice: Address,
     ) {
-        pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
+        pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice, 0);
 
         let hex_str = "0100000000010069825ef00344cf745b6e72a41d4f869d4e90de517849360c72bf94efc97681671d826e484747b21a80c8f1e7816021df9f55e458a6e7a717cb2bd2a1e85fd57100499602d200000000000100000000000000000000000000000000000000000000000000000000000000110000000000000001005054474d010200020100010000000000000000000000000000000000000000000000000000000000001111";
         let bytes = Vec::from_hex(hex_str).expect("Invalid hex string");
 
-        println!("Executing set_data_sources with bytes: {:?}", bytes);
-
-        let result = pyth_contract.sender(alice).execute_governance_instruction(bytes.clone());
-        if let Err(e) = &result {
-            println!("Governance instruction failed with error: {:?}", e);
-        }
-
+        let result = pyth_contract
+            .sender(alice)
+            .execute_governance_instruction(bytes.clone());
         assert!(result.is_ok());
     }
-
 }

+ 0 - 1
target_chains/stylus/contracts/wormhole/src/lib.rs

@@ -547,7 +547,6 @@ mod tests {
     use motsu::prelude::Contract;
     use core::str::FromStr;
     use k256::ecdsa::SigningKey;
-    use motsu::prelude::DefaultStorage;
     use stylus_sdk::alloy_primitives::keccak256;
 
     #[cfg(test)]

+ 1 - 1
target_chains/stylus/contracts/wormhole/tests/integration_test.rs

@@ -419,4 +419,4 @@ fn test_duplicate_verification(wormhole_contract: Contract<WormholeContract>, al
     let result = wormhole_contract.sender(alice).parse_and_verify_vm(test_vaa);
     println!("result: {:?}", result);
     assert!(result.is_err());
-}
+}