From d2d6e79467ad09da5dd4ee3ac0190c4a77e3e749 Mon Sep 17 00:00:00 2001 From: eddnewgate Date: Tue, 6 Aug 2024 02:07:02 +0545 Subject: [PATCH 1/4] feat: admin tests added --- src/lib.cairo | 31 ++++++++++++++++++++++++++--- tests/common.cairo | 14 +++++++++++++ tests/test_contract.cairo | 14 ------------- tests/test_push_admin.cairo | 39 +++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 17 deletions(-) create mode 100644 tests/common.cairo delete mode 100644 tests/test_contract.cairo create mode 100644 tests/test_push_admin.cairo diff --git a/src/lib.cairo b/src/lib.cairo index 5ea1e19..ad91324 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,8 +1,15 @@ #[starknet::interface] -pub trait IPushComm {} +pub trait IPushComm { + fn complete_migration(ref self: TContractState); + fn get_migration_status(self: @TContractState) -> bool; + fn set_push_core_address(ref self: TContractState, core_address: felt252); + fn get_push_core_address(self: @TContractState) -> felt252; +} #[starknet::contract] pub mod PushComm { + use openzeppelin::access::ownable::interface::OwnableABI; + use core::starknet::storage::StoragePointerWriteAccess; use starknet::storage::{Map, StorageMapReadAccess, StorageMapWriteAccess}; use starknet::ContractAddress; use openzeppelin::access::ownable::OwnableComponent; @@ -30,7 +37,7 @@ pub mod PushComm { // Contract State governance: ContractAddress, is_migration_complete: bool, - push_core_address: ContractAddress + push_core_address: felt252 } #[starknet::storage_node] @@ -59,5 +66,23 @@ pub mod PushComm { #[abi(embed_v0)] - impl PushComm of super::IPushComm {} + impl PushComm of super::IPushComm { + fn complete_migration(ref self: ContractState) { + self.ownable.assert_only_owner(); + self.is_migration_complete.write(true); + } + + fn get_migration_status(self: @ContractState) -> bool { + self.is_migration_complete.read() + } + + fn set_push_core_address(ref self: ContractState, core_address: felt252) { + self.ownable.assert_only_owner(); + self.push_core_address.write(core_address); + } + + fn get_push_core_address(self: @ContractState) -> felt252 { + self.push_core_address.read() + } + } } diff --git a/tests/common.cairo b/tests/common.cairo new file mode 100644 index 0000000..6028cea --- /dev/null +++ b/tests/common.cairo @@ -0,0 +1,14 @@ +use starknet::ContractAddress; +use snforge_std::{declare, ContractClassTrait, cheat_caller_address, CheatSpan}; + +// Constants +pub fn PUSH_ADMIN() -> ContractAddress { + 'push_admin'.try_into().unwrap() +} + +pub fn deploy_contract() -> ContractAddress { + let contract = declare("PushComm").unwrap(); + let calldata = array![PUSH_ADMIN().into()]; + let (contract_address, _) = contract.deploy(@calldata).unwrap(); + contract_address +} diff --git a/tests/test_contract.cairo b/tests/test_contract.cairo deleted file mode 100644 index 5c2ee09..0000000 --- a/tests/test_contract.cairo +++ /dev/null @@ -1,14 +0,0 @@ -use starknet::ContractAddress; - -use snforge_std::{declare, ContractClassTrait}; - -fn deploy_contract(name: ByteArray) -> ContractAddress { - let contract = declare(name).unwrap(); - let (contract_address, _) = contract.deploy(@array![0]).unwrap(); - contract_address -} - -#[test] -fn test_test() { - let _ = deploy_contract("PushComm"); -} diff --git a/tests/test_push_admin.cairo b/tests/test_push_admin.cairo new file mode 100644 index 0000000..0b56e09 --- /dev/null +++ b/tests/test_push_admin.cairo @@ -0,0 +1,39 @@ +use starknet::ContractAddress; + +use snforge_std::{declare, ContractClassTrait, cheat_caller_address, CheatSpan}; +use push_comm::{IPushCommDispatcher, IPushCommDispatcherTrait}; +use super::common::{PUSH_ADMIN, deploy_contract}; + +#[test] +fn test_migration_status() { + let contract_address = deploy_contract(); + let push_comm = IPushCommDispatcher { contract_address }; + + let migration_status = push_comm.get_migration_status(); + assert(migration_status == false, 'Initial migration set to false'); + + // admin sets the migration status + cheat_caller_address(contract_address, PUSH_ADMIN(), CheatSpan::TargetCalls(1)); + push_comm.complete_migration(); + + let migration_status = push_comm.get_migration_status(); + assert(migration_status == true, 'Migration status not updated'); +} + + +#[test] +fn test_core_channel_address() { + let contract_address = deploy_contract(); + let push_comm = IPushCommDispatcher { contract_address }; + + let CORE_ADDRESS: felt252 = 'some addrs'; + + // admin sets the migration status + cheat_caller_address(contract_address, PUSH_ADMIN(), CheatSpan::TargetCalls(1)); + push_comm.set_push_core_address(CORE_ADDRESS); + + let UPDATED_ADDRESS = push_comm.get_push_core_address(); + assert(CORE_ADDRESS == UPDATED_ADDRESS, 'Core Contract Update Failed'); +} + + From f54ef8bf2e50c2befea2c893eb9718846a9bb7b5 Mon Sep 17 00:00:00 2001 From: eddnewgate Date: Tue, 6 Aug 2024 02:34:13 +0545 Subject: [PATCH 2/4] feat: admin features added --- src/lib.cairo | 42 ++++++++++++++++++++++++++++++++++--- tests/test_push_admin.cairo | 41 +++++++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 6 deletions(-) diff --git a/src/lib.cairo b/src/lib.cairo index ad91324..76cee41 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -4,6 +4,11 @@ pub trait IPushComm { fn get_migration_status(self: @TContractState) -> bool; fn set_push_core_address(ref self: TContractState, core_address: felt252); fn get_push_core_address(self: @TContractState) -> felt252; + fn verify_channel_alias(ref self: TContractState, channel_address: felt252); + fn get_push_governance_address(self: @TContractState) -> felt252; + fn set_push_governance_address(ref self: TContractState, governance_address: felt252); + fn get_push_token_address(self: @TContractState) -> felt252; + fn set_push_token_address(ref self: TContractState, push_token_address: felt252); } #[starknet::contract] @@ -35,9 +40,10 @@ pub mod PushComm { // Channels delegatedNotificationSenders: Map, // Contract State - governance: ContractAddress, + governance: felt252, is_migration_complete: bool, - push_core_address: felt252 + push_core_address: felt252, + push_token_address: felt252 } #[starknet::storage_node] @@ -55,7 +61,15 @@ pub mod PushComm { #[derive(Drop, starknet::Event)] enum Event { #[flat] - OwnableEvent: OwnableComponent::Event + OwnableEvent: OwnableComponent::Event, + ChannelAlias: ChannelAlias + } + + #[derive(Drop, starknet::Event)] + struct ChannelAlias { + #[key] + channel_owner_address: ContractAddress, + ethereum_channel_address: felt252, } #[constructor] @@ -84,5 +98,27 @@ pub mod PushComm { fn get_push_core_address(self: @ContractState) -> felt252 { self.push_core_address.read() } + + fn verify_channel_alias(ref self: ContractState, channel_address: felt252) { + self.emit(ChannelAlias { channel_owner_address: self.owner(), ethereum_channel_address: channel_address }); + } + + fn set_push_governance_address(ref self: ContractState, governance_address: felt252) { + self.ownable.assert_only_owner(); + self.governance.write(governance_address); + } + + fn get_push_governance_address(self: @ContractState) -> felt252 { + self.governance.read() + } + + fn set_push_token_address(ref self: ContractState, push_token_address: felt252) { + self.ownable.assert_only_owner(); + self.push_token_address.write(push_token_address); + } + + fn get_push_token_address(self: @ContractState) -> felt252 { + self.push_token_address.read() + } } } diff --git a/tests/test_push_admin.cairo b/tests/test_push_admin.cairo index 0b56e09..f148ac7 100644 --- a/tests/test_push_admin.cairo +++ b/tests/test_push_admin.cairo @@ -1,6 +1,6 @@ use starknet::ContractAddress; -use snforge_std::{declare, ContractClassTrait, cheat_caller_address, CheatSpan}; +use snforge_std::{declare, ContractClassTrait, cheat_caller_address, CheatSpan, spy_events}; use push_comm::{IPushCommDispatcher, IPushCommDispatcherTrait}; use super::common::{PUSH_ADMIN, deploy_contract}; @@ -22,13 +22,13 @@ fn test_migration_status() { #[test] -fn test_core_channel_address() { +fn test_core_contract_address() { let contract_address = deploy_contract(); let push_comm = IPushCommDispatcher { contract_address }; let CORE_ADDRESS: felt252 = 'some addrs'; - // admin sets the migration status + // admin sets the core channel address cheat_caller_address(contract_address, PUSH_ADMIN(), CheatSpan::TargetCalls(1)); push_comm.set_push_core_address(CORE_ADDRESS); @@ -37,3 +37,38 @@ fn test_core_channel_address() { } +#[test] +fn test_gov_address() { + let contract_address = deploy_contract(); + let push_comm = IPushCommDispatcher { contract_address }; + + let GOV_ADDRESS: felt252 = 'some addrs'; + + // admin sets the migration status + cheat_caller_address(contract_address, PUSH_ADMIN(), CheatSpan::TargetCalls(1)); + push_comm.set_push_governance_address(GOV_ADDRESS); + + let UPDATED_ADDRESS = push_comm.get_push_governance_address(); + assert(GOV_ADDRESS == UPDATED_ADDRESS, 'Core Contract Update Failed'); +} + +#[test] +fn test_push_token_address() { + let contract_address = deploy_contract(); + let push_comm = IPushCommDispatcher { contract_address }; + + let TOKEN_ADDRESS: felt252 = 'some addrs'; + + // admin sets the migration status + cheat_caller_address(contract_address, PUSH_ADMIN(), CheatSpan::TargetCalls(1)); + push_comm.set_push_governance_address(TOKEN_ADDRESS); + + let UPDATED_ADDRESS = push_comm.get_push_governance_address(); + assert(TOKEN_ADDRESS == UPDATED_ADDRESS, 'Core Contract Update Failed'); +} + + + + + + From ddac9442688c9436c0d368d2ef1e1e3667a3c976 Mon Sep 17 00:00:00 2001 From: eddnewgate Date: Tue, 6 Aug 2024 02:45:20 +0545 Subject: [PATCH 3/4] --amend --- src/lib.cairo | 12 +++++++++++- tests/test_push_admin.cairo | 5 ----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/lib.cairo b/src/lib.cairo index 76cee41..51cb3fa 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,5 +1,8 @@ +use starknet::ContractAddress; + #[starknet::interface] pub trait IPushComm { + // Push Admin fn complete_migration(ref self: TContractState); fn get_migration_status(self: @TContractState) -> bool; fn set_push_core_address(ref self: TContractState, core_address: felt252); @@ -100,7 +103,13 @@ pub mod PushComm { } fn verify_channel_alias(ref self: ContractState, channel_address: felt252) { - self.emit(ChannelAlias { channel_owner_address: self.owner(), ethereum_channel_address: channel_address }); + self + .emit( + ChannelAlias { + channel_owner_address: self.owner(), + ethereum_channel_address: channel_address + } + ); } fn set_push_governance_address(ref self: ContractState, governance_address: felt252) { @@ -120,5 +129,6 @@ pub mod PushComm { fn get_push_token_address(self: @ContractState) -> felt252 { self.push_token_address.read() } + } } diff --git a/tests/test_push_admin.cairo b/tests/test_push_admin.cairo index f148ac7..5a8cecb 100644 --- a/tests/test_push_admin.cairo +++ b/tests/test_push_admin.cairo @@ -67,8 +67,3 @@ fn test_push_token_address() { assert(TOKEN_ADDRESS == UPDATED_ADDRESS, 'Core Contract Update Failed'); } - - - - - From 2788e653c004157b55f3d704fd97a0c42d195189 Mon Sep 17 00:00:00 2001 From: eddnewgate Date: Tue, 6 Aug 2024 02:46:52 +0545 Subject: [PATCH 4/4] --amend --- src/lib.cairo | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.cairo b/src/lib.cairo index 51cb3fa..2e848ab 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -129,6 +129,5 @@ pub mod PushComm { fn get_push_token_address(self: @ContractState) -> felt252 { self.push_token_address.read() } - } }