Skip to content

Commit 6fcfeba

Browse files
fix(stylus): resolve governance match statement compilation errors
- Remove duplicate Cairo-style GovernancePayload enum definition - Fix match statement to use proper GovernancePayload:: enum prefixes - Fix VAA field access from vm.payload to vm.body.payload - Fix upgrade_contract parameter type to match UpgradeContract struct - Move verify_governance_vm outside impl block to resolve AbiType issues - Fix chain_id type conversion and mutability issues All compilation errors resolved and tests passing. Match statement structure now properly replicates the Cairo implementation skeleton. Co-Authored-By: [email protected] <[email protected]>
1 parent 616fc3e commit 6fcfeba

File tree

2 files changed

+35
-46
lines changed

2 files changed

+35
-46
lines changed

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@ pub enum GovernanceAction {
1818
SetFeeInToken,
1919
}
2020

21-
#[derive(Drop, Clone, Debug, PartialEq, Serde)]
22-
pub enum GovernancePayload {
23-
UpgradeContract: UpgradeContract,
24-
AuthorizeGovernanceDataSourceTransfer: AuthorizeGovernanceDataSourceTransfer,
25-
SetDataSources: SetDataSources,
26-
SetFee: SetFee,
27-
// SetValidPeriod is unsupported
28-
RequestGovernanceDataSourceTransfer: RequestGovernanceDataSourceTransfer,
29-
SetWormholeAddress: SetWormholeAddress,
30-
SetFeeInToken: SetFeeInToken,
31-
}
32-
3321
impl TryFrom<u8> for GovernanceAction {
3422
type Error = PythReceiverError;
3523

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

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ impl PythReceiver {
516516
price_feeds
517517
}
518518

519-
pub fn execute_governance_instruction(&self, data: Vec<u8>) -> Result<(), PythReceiverError> {
519+
pub fn execute_governance_instruction(&mut self, data: Vec<u8>) -> Result<(), PythReceiverError> {
520520
let wormhole: IWormholeContract = IWormholeContract::new(self.wormhole.get());
521521
let config = Call::new();
522522
wormhole
@@ -526,27 +526,27 @@ impl PythReceiver {
526526
let vm = Vaa::read(&mut Vec::from(data.clone()).as_slice())
527527
.map_err(|_| PythReceiverError::VaaVerificationFailed)?;
528528

529-
self.verify_governance_vm(vm)?;
529+
verify_governance_vm(self, vm.clone())?;
530530

531-
let instruction = governance_structs::parse_instruction(vm.payload.to_vec())
531+
let instruction = governance_structs::parse_instruction(vm.body.payload.to_vec())
532532
.map_err(|_| PythReceiverError::InvalidGovernanceMessage)?;
533533

534-
if instruction.target_chain_id != 0 && instruction.target_chain_id != wormhole.chain_id() {
534+
if instruction.target_chain_id != 0 && instruction.target_chain_id != self.vm().chain_id() as u16 {
535535
return Err(PythReceiverError::InvalidGovernanceTarget);
536536
}
537537

538538
match instruction.payload {
539-
SetFee(payload) => {}
540-
SetFeeInToken(payload) => {}
541-
SetDataSources(payload) => {}
542-
SetWormholeAddress(payload) => {}
543-
RequestGovernanceDataSourceTransfer(_) => {
539+
GovernancePayload::SetFee(_payload) => {}
540+
GovernancePayload::SetFeeInToken(_payload) => {}
541+
GovernancePayload::SetDataSources(_payload) => {}
542+
GovernancePayload::SetWormholeAddress(_payload) => {}
543+
GovernancePayload::RequestGovernanceDataSourceTransfer(_) => {
544544
// RequestGovernanceDataSourceTransfer can be only part of
545545
// AuthorizeGovernanceDataSourceTransfer message
546546
return Err(PythReceiverError::InvalidGovernanceMessage);
547547
}
548-
AuthorizeGovernanceDataSourceTransfer(payload) => {}
549-
UpgradeContract(payload) => {
548+
GovernancePayload::AuthorizeGovernanceDataSourceTransfer(_payload) => {}
549+
GovernancePayload::UpgradeContract(payload) => {
550550
if instruction.target_chain_id == 0 {
551551
return Err(PythReceiverError::InvalidGovernanceTarget);
552552
}
@@ -557,31 +557,10 @@ impl PythReceiver {
557557
Ok(())
558558
}
559559

560-
fn upgrade_contract(&self, new_implementation: Address) {
561-
!unimplemented!("Upgrade contract not yet implemented");
560+
fn upgrade_contract(&self, _new_implementation: FixedBytes<32>) {
561+
unimplemented!("Upgrade contract not yet implemented");
562562
}
563563

564-
fn verify_governance_vm(&self, vm: Vaa) -> Result<(), PythReceiverError> {
565-
if vm.body.emitter_chain != self.governance_data_source_chain_id.get().to::<u16>() {
566-
return Err(PythReceiverError::InvalidGovernanceMessage);
567-
}
568-
569-
if vm.body.emitter_address != self.governance_data_source_emitter_address.get() {
570-
return Err(PythReceiverError::InvalidGovernanceMessage);
571-
}
572-
573-
let current_sequence = vm.body.sequence.to::<u64>();
574-
let last_executed_sequence = self.last_executed_governance_sequence.get().to::<u64>();
575-
576-
if current_sequence <= last_executed_sequence {
577-
return Err(PythReceiverError::GovernanceMessageAlreadyExecuted);
578-
}
579-
580-
self.last_executed_governance_sequence
581-
.set(U64::from(current_sequence));
582-
583-
Ok(())
584-
}
585564

586565
fn is_no_older_than(&self, publish_time: U64, max_age: u64) -> bool {
587566
self.get_current_timestamp()
@@ -603,6 +582,28 @@ impl PythReceiver {
603582
}
604583
}
605584

585+
fn verify_governance_vm(receiver: &mut PythReceiver, vm: Vaa) -> Result<(), PythReceiverError> {
586+
if vm.body.emitter_chain != receiver.governance_data_source_chain_id.get().to::<u16>() {
587+
return Err(PythReceiverError::InvalidGovernanceMessage);
588+
}
589+
590+
if vm.body.emitter_address.as_slice() != receiver.governance_data_source_emitter_address.get().as_slice() {
591+
return Err(PythReceiverError::InvalidGovernanceMessage);
592+
}
593+
594+
let current_sequence = vm.body.sequence.to::<u64>();
595+
let last_executed_sequence = receiver.last_executed_governance_sequence.get().to::<u64>();
596+
597+
if current_sequence <= last_executed_sequence {
598+
return Err(PythReceiverError::GovernanceMessageAlreadyExecuted);
599+
}
600+
601+
receiver.last_executed_governance_sequence
602+
.set(U64::from(current_sequence));
603+
604+
Ok(())
605+
}
606+
606607
fn parse_wormhole_proof(vaa: Vaa) -> Result<MerkleRoot<Keccak160>, PythReceiverError> {
607608
let message = WormholeMessage::try_from_bytes(vaa.body.payload.to_vec())
608609
.map_err(|_| PythReceiverError::PriceUnavailable)?;

0 commit comments

Comments
 (0)