Explorar el Código

cleaned byte reading checks, fixed authorize_governance_transfer version handling

Ayush Suresh hace 4 meses
padre
commit
2e17cc9bd1

+ 5 - 11
target_chains/stylus/contracts/pyth-receiver/src/governance_structs.rs

@@ -238,9 +238,6 @@ pub fn parse_instruction(payload: Vec<u8>) -> Result<GovernanceInstruction, Pyth
             GovernancePayload::SetFee(SetFee { value, expo })
         }
         GovernanceAction::SetValidPeriod => {
-            if payload.len() < cursor + 8 {
-                return Err(PythReceiverError::InvalidGovernanceMessage);
-            }
             let valid_period_bytes = payload
                 .get(cursor..cursor + 8)
                 .ok_or(PythReceiverError::InvalidGovernanceMessage)?;
@@ -255,20 +252,17 @@ pub fn parse_instruction(payload: Vec<u8>) -> Result<GovernanceInstruction, Pyth
             })
         }
         GovernanceAction::SetWormholeAddress => {
-            if payload.len() < cursor + 20 {
-                return Err(PythReceiverError::InvalidGovernanceMessage);
-            }
-            let mut address_bytes = [0u8; 20];
-            address_bytes.copy_from_slice(&payload[cursor..cursor + 20]);
+            let address_bytes: &[u8; 20] = payload
+                .get(cursor..cursor + 20)
+                .ok_or(PythReceiverError::InvalidGovernanceMessage)?
+                .try_into()
+                .map_err(|_| PythReceiverError::InvalidGovernanceMessage)?;
             cursor += 20;
             GovernancePayload::SetWormholeAddress(SetWormholeAddress {
                 address: Address::from(address_bytes),
             })
         }
         GovernanceAction::SetTransactionFee => {
-            if payload.len() < cursor + 16 {
-                return Err(PythReceiverError::InvalidGovernanceMessage);
-            }
             let fee_value_bytes = payload
                 .get(cursor..cursor + 8)
                 .ok_or(PythReceiverError::InvalidGovernanceMessage)?;

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

@@ -713,7 +713,7 @@ impl PythReceiver {
         let current_index = self.governance_data_source_index.get().to::<u32>();
         let new_index = request_payload.governance_data_source_index;
 
-        if current_index > new_index {
+        if current_index >= new_index {
             return Err(PythReceiverError::OldGovernanceMessage);
         }
 

+ 21 - 10
target_chains/stylus/contracts/pyth-receiver/src/pyth_governance_test.rs

@@ -1,7 +1,7 @@
 #[cfg(test)]
 mod test {
-    use crate::{PythReceiver, FeeSet, TransactionFeeSet, DataSourcesSet, GovernanceDataSourceSet};
-    use alloy_primitives::{address, Address, U256, FixedBytes};
+    use crate::{DataSourcesSet, FeeSet, GovernanceDataSourceSet, PythReceiver, TransactionFeeSet};
+    use alloy_primitives::{address, Address, FixedBytes, U256};
     use hex::FromHex;
     use motsu::prelude::*;
     use wormhole_contract::WormholeContract;
@@ -100,16 +100,18 @@ mod test {
             .execute_governance_instruction(bytes.clone());
         assert!(result.is_ok());
 
-
         let expected_event = DataSourcesSet {
             old_data_sources: vec![FixedBytes::from(PYTHNET_EMITTER_ADDRESS)],
             new_data_sources: vec![FixedBytes::from([
-                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, 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, 0x11, 0x11,
             ])],
         };
-        assert!(pyth_contract.emitted(&expected_event), "DataSourcesSet event should be emitted");
+        assert!(
+            pyth_contract.emitted(&expected_event),
+            "DataSourcesSet event should be emitted"
+        );
 
         let result2 = pyth_contract
             .sender(alice)
@@ -161,7 +163,10 @@ mod test {
             old_fee: SINGLE_UPDATE_FEE_IN_WEI,
             new_fee: expected_new_fee,
         };
-        assert!(pyth_contract.emitted(&expected_event), "FeeSet event should be emitted");
+        assert!(
+            pyth_contract.emitted(&expected_event),
+            "FeeSet event should be emitted"
+        );
 
         let result2 = pyth_contract
             .sender(alice)
@@ -271,7 +276,10 @@ mod test {
             new_emitter_address: FixedBytes::from(GOVERNANCE_EMITTER), // emitter_bytes from the VAA
             initial_sequence: 100, // claim_vm.body.sequence from the VAA (0x64 = 100)
         };
-        assert!(pyth_contract.emitted(&expected_event), "GovernanceDataSourceSet event should be emitted");
+        assert!(
+            pyth_contract.emitted(&expected_event),
+            "GovernanceDataSourceSet event should be emitted"
+        );
     }
 
     #[motsu::test]
@@ -301,7 +309,10 @@ mod test {
             old_fee: U256::ZERO,
             new_fee: expected_new_fee,
         };
-        assert!(pyth_contract.emitted(&expected_event), "TransactionFeeSet event should be emitted");
+        assert!(
+            pyth_contract.emitted(&expected_event),
+            "TransactionFeeSet event should be emitted"
+        );
 
         let result2 = pyth_contract
             .sender(alice)