Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify channel alias #6

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- main

jobs:
lin:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
Expand All @@ -24,7 +24,7 @@ jobs:
- name: Cairo lint
run: scarb fmt --check

test:
build:
name: Build
runs-on: ubuntu-latest
steps:
Expand All @@ -38,4 +38,20 @@ jobs:
scarb-version: ${{ env.SCARB_VERSION }}
- uses: foundry-rs/setup-snfoundry@v3
- name: Cairo Build
run: scarb build
run: scarb build

test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Extract scarb version
run: |
SCARB_VERSION=$(grep 'scarb-version = ' Scarb.toml | sed 's/scarb-version = "\(.*\)"/\1/')
echo "SCARB_VERSION=$SCARB_VERSION" >> $GITHUB_ENV
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: ${{ env.SCARB_VERSION }}
- uses: foundry-rs/setup-snfoundry@v3
- name: Cairo Test
run: scarb test
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');
}
Loading