Skip to content

Commit

Permalink
Verify channel alias (#6)
Browse files Browse the repository at this point in the history
verifyChannelAlias
  • Loading branch information
eddnewgate authored Aug 6, 2024
1 parent 5e263dd commit e72e488
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
27 changes: 19 additions & 8 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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]
Expand All @@ -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);
}


Expand Down Expand Up @@ -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
}
);
Expand Down
15 changes: 14 additions & 1 deletion tests/common.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
29 changes: 29 additions & 0 deletions tests/test_channel_alias.cairo
Original file line number Diff line number Diff line change
@@ -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');
}

0 comments on commit e72e488

Please sign in to comment.