Skip to content

Commit 2e17cc9

Browse files
committed
cleaned byte reading checks, fixed authorize_governance_transfer version handling
1 parent e36fdc1 commit 2e17cc9

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,6 @@ pub fn parse_instruction(payload: Vec<u8>) -> Result<GovernanceInstruction, Pyth
238238
GovernancePayload::SetFee(SetFee { value, expo })
239239
}
240240
GovernanceAction::SetValidPeriod => {
241-
if payload.len() < cursor + 8 {
242-
return Err(PythReceiverError::InvalidGovernanceMessage);
243-
}
244241
let valid_period_bytes = payload
245242
.get(cursor..cursor + 8)
246243
.ok_or(PythReceiverError::InvalidGovernanceMessage)?;
@@ -255,20 +252,17 @@ pub fn parse_instruction(payload: Vec<u8>) -> Result<GovernanceInstruction, Pyth
255252
})
256253
}
257254
GovernanceAction::SetWormholeAddress => {
258-
if payload.len() < cursor + 20 {
259-
return Err(PythReceiverError::InvalidGovernanceMessage);
260-
}
261-
let mut address_bytes = [0u8; 20];
262-
address_bytes.copy_from_slice(&payload[cursor..cursor + 20]);
255+
let address_bytes: &[u8; 20] = payload
256+
.get(cursor..cursor + 20)
257+
.ok_or(PythReceiverError::InvalidGovernanceMessage)?
258+
.try_into()
259+
.map_err(|_| PythReceiverError::InvalidGovernanceMessage)?;
263260
cursor += 20;
264261
GovernancePayload::SetWormholeAddress(SetWormholeAddress {
265262
address: Address::from(address_bytes),
266263
})
267264
}
268265
GovernanceAction::SetTransactionFee => {
269-
if payload.len() < cursor + 16 {
270-
return Err(PythReceiverError::InvalidGovernanceMessage);
271-
}
272266
let fee_value_bytes = payload
273267
.get(cursor..cursor + 8)
274268
.ok_or(PythReceiverError::InvalidGovernanceMessage)?;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ impl PythReceiver {
713713
let current_index = self.governance_data_source_index.get().to::<u32>();
714714
let new_index = request_payload.governance_data_source_index;
715715

716-
if current_index > new_index {
716+
if current_index >= new_index {
717717
return Err(PythReceiverError::OldGovernanceMessage);
718718
}
719719

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(test)]
22
mod test {
3-
use crate::{PythReceiver, FeeSet, TransactionFeeSet, DataSourcesSet, GovernanceDataSourceSet};
4-
use alloy_primitives::{address, Address, U256, FixedBytes};
3+
use crate::{DataSourcesSet, FeeSet, GovernanceDataSourceSet, PythReceiver, TransactionFeeSet};
4+
use alloy_primitives::{address, Address, FixedBytes, U256};
55
use hex::FromHex;
66
use motsu::prelude::*;
77
use wormhole_contract::WormholeContract;
@@ -100,16 +100,18 @@ mod test {
100100
.execute_governance_instruction(bytes.clone());
101101
assert!(result.is_ok());
102102

103-
104103
let expected_event = DataSourcesSet {
105104
old_data_sources: vec![FixedBytes::from(PYTHNET_EMITTER_ADDRESS)],
106105
new_data_sources: vec![FixedBytes::from([
107-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
109-
0x11, 0x11
106+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
107+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108+
0x00, 0x00, 0x11, 0x11,
110109
])],
111110
};
112-
assert!(pyth_contract.emitted(&expected_event), "DataSourcesSet event should be emitted");
111+
assert!(
112+
pyth_contract.emitted(&expected_event),
113+
"DataSourcesSet event should be emitted"
114+
);
113115

114116
let result2 = pyth_contract
115117
.sender(alice)
@@ -161,7 +163,10 @@ mod test {
161163
old_fee: SINGLE_UPDATE_FEE_IN_WEI,
162164
new_fee: expected_new_fee,
163165
};
164-
assert!(pyth_contract.emitted(&expected_event), "FeeSet event should be emitted");
166+
assert!(
167+
pyth_contract.emitted(&expected_event),
168+
"FeeSet event should be emitted"
169+
);
165170

166171
let result2 = pyth_contract
167172
.sender(alice)
@@ -271,7 +276,10 @@ mod test {
271276
new_emitter_address: FixedBytes::from(GOVERNANCE_EMITTER), // emitter_bytes from the VAA
272277
initial_sequence: 100, // claim_vm.body.sequence from the VAA (0x64 = 100)
273278
};
274-
assert!(pyth_contract.emitted(&expected_event), "GovernanceDataSourceSet event should be emitted");
279+
assert!(
280+
pyth_contract.emitted(&expected_event),
281+
"GovernanceDataSourceSet event should be emitted"
282+
);
275283
}
276284

277285
#[motsu::test]
@@ -301,7 +309,10 @@ mod test {
301309
old_fee: U256::ZERO,
302310
new_fee: expected_new_fee,
303311
};
304-
assert!(pyth_contract.emitted(&expected_event), "TransactionFeeSet event should be emitted");
312+
assert!(
313+
pyth_contract.emitted(&expected_event),
314+
"TransactionFeeSet event should be emitted"
315+
);
305316

306317
let result2 = pyth_contract
307318
.sender(alice)

0 commit comments

Comments
 (0)