From e72e488a928ad7f55d1fe5ae4be844d549614577 Mon Sep 17 00:00:00 2001 From: eddnewgate Date: Tue, 6 Aug 2024 22:53:18 +0545 Subject: [PATCH] Verify channel alias (#6) verifyChannelAlias --- src/lib.cairo | 27 +++++++++++++++++++-------- tests/common.cairo | 15 ++++++++++++++- tests/test_channel_alias.cairo | 29 +++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 tests/test_channel_alias.cairo diff --git a/src/lib.cairo b/src/lib.cairo index 2e848ab..09b8c89 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -19,7 +19,7 @@ pub mod PushComm { use openzeppelin::access::ownable::interface::OwnableABI; use core::starknet::storage::StoragePointerWriteAccess; use starknet::storage::{Map, StorageMapReadAccess, StorageMapWriteAccess}; - use starknet::ContractAddress; + use starknet::{ContractAddress, get_caller_address}; use openzeppelin::access::ownable::OwnableComponent; component!(path: OwnableComponent, storage: ownable, event: OwnableEvent); @@ -46,7 +46,10 @@ pub mod PushComm { governance: felt252, is_migration_complete: bool, push_core_address: felt252, - push_token_address: felt252 + push_token_address: felt252, + // Chain Info + chain_name: felt252, + chain_id: felt252, } #[starknet::storage_node] @@ -62,23 +65,29 @@ pub mod PushComm { #[event] #[derive(Drop, starknet::Event)] - enum Event { + pub enum Event { #[flat] OwnableEvent: OwnableComponent::Event, ChannelAlias: ChannelAlias } #[derive(Drop, starknet::Event)] - struct ChannelAlias { + pub struct ChannelAlias { #[key] - channel_owner_address: ContractAddress, - ethereum_channel_address: felt252, + pub chain_name: felt252, + pub chain_id: felt252, + pub channel_owner_address: ContractAddress, + pub ethereum_channel_address: felt252, } #[constructor] - fn constructor(ref self: ContractState, owner: ContractAddress) { + fn constructor( + ref self: ContractState, owner: ContractAddress, chain_id: felt252, chain_name: felt252 + ) { // Set the initial owner of the contract self.ownable.initializer(owner); + self.chain_id.write(chain_id); + self.chain_name.write(chain_name); } @@ -106,7 +115,9 @@ pub mod PushComm { self .emit( ChannelAlias { - channel_owner_address: self.owner(), + chain_name: self.chain_name.read(), + chain_id: self.chain_id.read(), + channel_owner_address: get_caller_address(), ethereum_channel_address: channel_address } ); diff --git a/tests/common.cairo b/tests/common.cairo index 6028cea..d47fe70 100644 --- a/tests/common.cairo +++ b/tests/common.cairo @@ -6,9 +6,22 @@ pub fn PUSH_ADMIN() -> ContractAddress { 'push_admin'.try_into().unwrap() } +pub fn USER_1() -> ContractAddress { + 'user_1'.try_into().unwrap() +} + +pub fn CHAIN_ID() -> felt252 { + 'Some ID'.try_into().unwrap() +} + +pub fn CHAIN_NAME() -> felt252 { + 'Starknet'.try_into().unwrap() +} + pub fn deploy_contract() -> ContractAddress { let contract = declare("PushComm").unwrap(); - let calldata = array![PUSH_ADMIN().into()]; + + let calldata = array![PUSH_ADMIN().into(), CHAIN_NAME(), CHAIN_ID()]; let (contract_address, _) = contract.deploy(@calldata).unwrap(); contract_address } diff --git a/tests/test_channel_alias.cairo b/tests/test_channel_alias.cairo new file mode 100644 index 0000000..4805568 --- /dev/null +++ b/tests/test_channel_alias.cairo @@ -0,0 +1,29 @@ +use starknet::ContractAddress; + +use snforge_std::{ + declare, ContractClassTrait, cheat_caller_address, CheatSpan, spy_events, + EventSpyAssertionsTrait, Event, EventSpyTrait +}; +use push_comm::{PushComm, IPushCommDispatcher, IPushCommDispatcherTrait}; +use super::common::{USER_1, deploy_contract, CHAIN_NAME, CHAIN_ID}; + +#[test] +fn test_verify_channel_alias() { + let contract_address = deploy_contract(); + let push_comm = IPushCommDispatcher { contract_address }; + let channel_address: felt252 = 'some address'; + + let mut spy = spy_events(); + + // user sets the alias + cheat_caller_address(contract_address, USER_1(), CheatSpan::TargetCalls(1)); + push_comm.verify_channel_alias(channel_address); + + // assert on the vent + let events = spy.get_events(); + assert(events.events.len() == 1, 'There should be one event'); + + let (from_addrs, event) = events.events.at(0); + assert(from_addrs == @contract_address, 'Emitted from wrong address'); + assert(event.keys.at(0) == @selector!("ChannelAlias"), 'Wrong event was emitted'); +}