Skip to content

Commit

Permalink
Re enroll (#4)
Browse files Browse the repository at this point in the history
* feat: initiliased testcases

* admin function tests

* fix: max total balance check

* feat: block token test

* disable limit test

* feat: add failing tc for withdraw limit

* fixed testcases after rebase

* chore: added messaging mock

* Update src/bridge/token_bridge.cairo

Co-authored-by: Apoorv Sadana <[email protected]>

* resolve comments

* add reactivate and unblock

* add reactivate and unblock

* unblock tests

* reactivate and unblock tests

* resolved merge conflicts

* make them unit

* improve: not needed to deploy usdc in while mock testing

---------

Co-authored-by: Apoorv Sadana <[email protected]>
  • Loading branch information
byteZorvin and apoorvsadana authored Aug 5, 2024
1 parent 2b044ad commit e6d2ab1
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 57 deletions.
1 change: 0 additions & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v

[[target.starknet-contract]]
casm = true
build-external-contracts = ["piltover::messaging::mock::messaging_mock"]

[scripts]
test = "snforge test"
3 changes: 3 additions & 0 deletions src/bridge/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use starknet_bridge::bridge::types::{TokenStatus, TokenSettings};
pub trait ITokenBridgeAdmin<TContractState> {
fn set_appchain_token_bridge(ref self: TContractState, appchain_bridge: ContractAddress);
fn block_token(ref self: TContractState, token: ContractAddress);
fn unblock_token(ref self: TContractState, token: ContractAddress);
fn deactivate_token(ref self: TContractState, token: ContractAddress);
fn reactivate_token(ref self: TContractState, token: ContractAddress);

fn enable_withdrawal_limit(ref self: TContractState, token: ContractAddress);
fn disable_withdrawal_limit(ref self: TContractState, token: ContractAddress);
fn set_max_total_balance(
Expand Down
60 changes: 52 additions & 8 deletions src/bridge/token_bridge.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ pub mod TokenBridge {
pub const ZERO_DEPOSIT: felt252 = 'Zero amount';
pub const ALREADY_ENROLLED: felt252 = 'Already enrolled';
pub const DEPLOYMENT_MESSAGE_DOES_NOT_EXIST: felt252 = 'Deployment message inexistent';
pub const CANNOT_DEACTIVATE: felt252 = 'Cannot deactivate and block';
pub const CANNOT_BLOCK: felt252 = 'Cannot block';
pub const NOT_ACTIVE: felt252 = 'Token not active';
pub const NOT_DEACTIVATED: felt252 = 'Token not deactivated';
pub const NOT_BLOCKED: felt252 = 'Token not blocked';
pub const NOT_UNKNOWN: felt252 = 'Only unknown can be blocked';
pub const INVALID_RECIPIENT: felt252 = 'Invalid recipient';
pub const MAX_BALANCE_EXCEEDED: felt252 = 'Max Balance Exceeded';
}
Expand All @@ -94,8 +96,11 @@ pub mod TokenBridge {
#[event]
pub enum Event {
TokenEnrollmentInitiated: TokenEnrollmentInitiated,
TokenActivated: TokenActivated,
TokenDeactivated: TokenDeactivated,
TokenBlocked: TokenBlocked,
TokenReactivated: TokenReactivated,
TokenUnblocked: TokenUnblocked,
Deposit: Deposit,
DepositWithMessage: DepositWithMessage,
DepostiCancelRequest: DepositCancelRequest,
Expand All @@ -117,6 +122,11 @@ pub mod TokenBridge {
ReentrancyGuardEvent: ReentrancyGuardComponent::Event,
}

#[derive(Drop, starknet::Event)]
pub struct TokenActivated {
pub token: ContractAddress
}

#[derive(Drop, starknet::Event)]
pub struct TokenDeactivated {
pub token: ContractAddress
Expand All @@ -127,6 +137,18 @@ pub mod TokenBridge {
pub token: ContractAddress
}


#[derive(Drop, starknet::Event)]
pub struct TokenUnblocked {
pub token: ContractAddress
}


#[derive(Drop, starknet::Event)]
pub struct TokenReactivated {
pub token: ContractAddress
}

#[derive(Drop, starknet::Event)]
pub struct TokenEnrollmentInitiated {
pub token: ContractAddress,
Expand Down Expand Up @@ -382,7 +404,7 @@ pub mod TokenBridge {
// Throws an error if the token is not enrolled or if the sender is not the manager.
fn block_token(ref self: ContractState, token: ContractAddress) {
self.ownable.assert_only_owner();
assert(self.get_status(token) == TokenStatus::Unknown, Errors::CANNOT_BLOCK);
assert(self.get_status(token) == TokenStatus::Unknown, Errors::NOT_UNKNOWN);

let new_settings = TokenSettings {
token_status: TokenStatus::Blocked, ..self.token_settings.read(token)
Expand All @@ -391,24 +413,45 @@ pub mod TokenBridge {
self.emit(TokenBlocked { token });
}

fn unblock_token(ref self: ContractState, token: ContractAddress) {
self.ownable.assert_only_owner();
assert(self.get_status(token) == TokenStatus::Blocked, Errors::NOT_BLOCKED);

let new_settings = TokenSettings {
token_status: TokenStatus::Unknown, ..self.token_settings.read(token)
};
self.token_settings.write(token, new_settings);
self.emit(TokenUnblocked { token });
}


fn deactivate_token(ref self: ContractState, token: ContractAddress) {
self.ownable.assert_only_owner();
let status = self.get_status(token);
assert(
status == TokenStatus::Active || status == TokenStatus::Pending,
Errors::CANNOT_DEACTIVATE
);
assert(status == TokenStatus::Active, Errors::NOT_ACTIVE);

let new_settings = TokenSettings {
token_status: TokenStatus::Deactivated, ..self.token_settings.read(token)
};
self.token_settings.write(token, new_settings);

self.emit(TokenDeactivated { token });
self.emit(TokenBlocked { token });
}

fn reactivate_token(ref self: ContractState, token: ContractAddress) {
self.ownable.assert_only_owner();
let status = self.get_status(token);
assert(status == TokenStatus::Deactivated, Errors::NOT_DEACTIVATED);

let new_settings = TokenSettings {
token_status: TokenStatus::Active, ..self.token_settings.read(token)
};
self.token_settings.write(token, new_settings);

self.emit(TokenReactivated { token });
}


fn enable_withdrawal_limit(ref self: ContractState, token: ContractAddress) {
self.ownable.assert_only_owner();
let new_settings = TokenSettings {
Expand Down Expand Up @@ -566,6 +609,7 @@ pub mod TokenBridge {
if (nonce.is_zero()) {
let new_settings = TokenSettings { token_status: TokenStatus::Active, ..settings };
self.token_settings.write(token, new_settings);
self.emit(TokenActivated { token });
} else if (get_block_timestamp() > settings.pending_deployment_expiration) {
let new_settings = TokenSettings { token_status: TokenStatus::Unknown, ..settings };
self.token_settings.write(token, new_settings);
Expand Down
1 change: 1 addition & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ pub mod constants;

pub mod mocks {
pub mod erc20;
pub mod messaging;
}

45 changes: 45 additions & 0 deletions src/mocks/messaging.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use piltover::messaging::output_process::MessageToAppchain;
#[starknet::interface]
pub trait IMockMessaging<TState> {
fn update_state_for_message(ref self: TState, message_hash: felt252);
}

#[starknet::contract]
mod messaging_mock {
use piltover::messaging::{
output_process::MessageToAppchain, messaging_cpt,
messaging_cpt::InternalTrait as MessagingInternal, IMessaging
};
use starknet::ContractAddress;
use super::IMockMessaging;

component!(path: messaging_cpt, storage: messaging, event: MessagingEvent);

#[abi(embed_v0)]
impl MessagingImpl = messaging_cpt::MessagingImpl<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
messaging: messaging_cpt::Storage
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
MessagingEvent: messaging_cpt::Event
}

#[constructor]
fn constructor(ref self: ContractState, cancellation_delay_secs: u64) {
self.messaging.initialize(cancellation_delay_secs);
}

#[abi(embed_v0)]
impl MockMessagingImpl of IMockMessaging<ContractState> {
fn update_state_for_message(ref self: ContractState, message_hash: felt252) {
self.messaging.sn_to_appc_messages.write(message_hash, 0);
}
}
}
3 changes: 3 additions & 0 deletions tests/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub fn L3_BRIDGE_ADDRESS() -> ContractAddress {
contract_address_const::<'l3_bridge_address'>()
}

pub fn USDC_MOCK_ADDRESS() -> ContractAddress {
contract_address_const::<'Usdc address'>()
}

// 5 days as the delay time (5 * 86400 = 432000)
pub const DELAY_TIME: felt252 = 432000;
Expand Down
Loading

0 comments on commit e6d2ab1

Please sign in to comment.