Skip to content

Commit

Permalink
feat(target_chains/starknet): add GuardianSetAdded event to wormhole (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Riateche authored May 22, 2024
1 parent 638ac84 commit 79e009a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
15 changes: 15 additions & 0 deletions target_chains/starknet/contracts/src/wormhole.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub use errors::{GovernanceError, SubmitNewGuardianSetError, ParseAndVerifyVmErr
pub use interface::{
VerifiedVM, IWormhole, IWormholeDispatcher, IWormholeDispatcherTrait, GuardianSignature, quorum
};
pub use wormhole::{Event, GuardianSetAdded};

#[starknet::contract]
mod wormhole {
Expand All @@ -26,6 +27,17 @@ mod wormhole {
use core::panic_with_felt252;
use pyth::util::{UNEXPECTED_OVERFLOW};

#[event]
#[derive(Drop, PartialEq, starknet::Event)]
pub enum Event {
GuardianSetAdded: GuardianSetAdded,
}

#[derive(Drop, PartialEq, starknet::Event)]
pub struct GuardianSetAdded {
pub index: u32,
}

#[derive(Drop, Debug, Clone, Serde, starknet::Store)]
struct GuardianSet {
num_guardians: usize,
Expand Down Expand Up @@ -134,6 +146,9 @@ mod wormhole {
self.expire_guardian_set(current_set_index, get_block_timestamp());

self.consumed_governance_actions.write(vm.hash, true);

let event = GuardianSetAdded { index: new_set.set_index };
self.emit(event);
}
}

Expand Down
57 changes: 55 additions & 2 deletions target_chains/starknet/contracts/tests/wormhole.cairo
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
use snforge_std::{declare, ContractClass, ContractClassTrait, start_prank, stop_prank, CheatTarget};
use pyth::wormhole::{IWormholeDispatcher, IWormholeDispatcherTrait, ParseAndVerifyVmError};
use snforge_std::{
declare, ContractClass, ContractClassTrait, start_prank, stop_prank, CheatTarget, Event,
event_name_hash, spy_events, SpyOn, EventFetcher,
};
use pyth::wormhole::{
IWormholeDispatcher, IWormholeDispatcherTrait, ParseAndVerifyVmError, Event as WormholeEvent,
GuardianSetAdded
};
use pyth::reader::ReaderImpl;
use pyth::byte_array::{ByteArray, ByteArrayImpl};
use pyth::util::{UnwrapWithFelt252, array_try_into};
use core::starknet::{ContractAddress, EthAddress};
use core::panic_with_felt252;
use super::data;

#[generate_trait]
impl DecodeEventHelpers of DecodeEventHelpersTrait {
fn pop<T, +TryInto<felt252, T>>(ref self: Array<felt252>) -> T {
self.pop_front().unwrap().try_into().unwrap()
}
}

fn decode_event(mut event: Event) -> WormholeEvent {
let key0: felt252 = event.keys.pop();
let output = if key0 == event_name_hash('GuardianSetAdded') {
let event = GuardianSetAdded { index: event.data.pop(), };
WormholeEvent::GuardianSetAdded(event)
} else {
panic!("unrecognized event")
};
assert!(event.keys.len() == 0);
assert!(event.data.len() == 0);
output
}

#[test]
fn test_parse_and_verify_vm_works() {
let dispatcher = deploy_with_mainnet_guardians();
Expand Down Expand Up @@ -63,6 +89,33 @@ fn test_submit_guardian_set_rejects_wrong_index_in_signer() {
dispatcher.submit_new_guardian_set(data::mainnet_guardian_set_upgrade3());
}

#[test]
fn test_submit_guardian_set_emits_events() {
let dispatcher = deploy(guardian_set0(), CHAIN_ID, GOVERNANCE_CHAIN_ID, GOVERNANCE_CONTRACT);

let mut spy = spy_events(SpyOn::One(dispatcher.contract_address));

dispatcher.submit_new_guardian_set(data::mainnet_guardian_set_upgrade1());

spy.fetch_events();
assert!(spy.events.len() == 1);
let (from, event) = spy.events.pop_front().unwrap();
assert!(from == dispatcher.contract_address);
let event = decode_event(event);
let expected = GuardianSetAdded { index: 1 };
assert!(event == WormholeEvent::GuardianSetAdded(expected));

dispatcher.submit_new_guardian_set(data::mainnet_guardian_set_upgrade2());

spy.fetch_events();
assert!(spy.events.len() == 1);
let (from, event) = spy.events.pop_front().unwrap();
assert!(from == dispatcher.contract_address);
let event = decode_event(event);
let expected = GuardianSetAdded { index: 2 };
assert!(event == WormholeEvent::GuardianSetAdded(expected));
}

#[test]
#[should_panic(expected: ('invalid guardian set sequence',))]
fn test_submit_guardian_set_rejects_wrong_index_in_payload() {
Expand Down

0 comments on commit 79e009a

Please sign in to comment.