Skip to content

Commit

Permalink
Migrate security/pausable (#593)
Browse files Browse the repository at this point in the history
* fix path

* add security mod

* add pausable

* add mock

* add tests

* add security mod

* update cargo

* update cairo

* Apply suggestions from code review

Co-authored-by: Martín Triay <martriay@gmail.com>

* remove underscore from _paused

* remove unused imports

* add test assertion for is_paused

* move mocks inside tests crate

* remove underscore

* add blank line

---------

Co-authored-by: Martín Triay <martriay@gmail.com>
  • Loading branch information
andrew-fleming and martriay authored Mar 31, 2023
1 parent f48da2c commit d671d4c
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/openzeppelin/security.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod pausable;
mod initializable;
39 changes: 39 additions & 0 deletions src/openzeppelin/security/pausable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#[contract]
mod Pausable {
use starknet::ContractAddress;
use starknet::get_caller_address;

struct Storage {
paused: bool,
}

#[event]
fn Paused(account: ContractAddress) {}

#[event]
fn Unpaused(account: ContractAddress) {}

fn is_paused() -> bool {
paused::read()
}

fn assert_not_paused() {
assert(!is_paused(), 'Pausable: paused');
}

fn assert_paused() {
assert(is_paused(), 'Pausable: not paused');
}

fn pause() {
assert_not_paused();
paused::write(true);
Paused(get_caller_address());
}

fn unpause() {
assert_paused();
paused::write(false);
Unpaused(get_caller_address());
}
}
2 changes: 2 additions & 0 deletions src/openzeppelin/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
mod test_erc20;
mod test_pausable;
mod test_initializable;
mod mocks;
1 change: 1 addition & 0 deletions src/openzeppelin/tests/mocks.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod mock_pausable;
34 changes: 34 additions & 0 deletions src/openzeppelin/tests/mocks/mock_pausable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#[contract]
mod MockPausable {
use openzeppelin::security::pausable::Pausable;

struct Storage {
counter: felt252
}

#[view]
fn is_paused() -> bool {
Pausable::is_paused()
}

#[view]
fn get_count() -> felt252 {
counter::read()
}

#[external]
fn assert_unpaused_and_increment() {
Pausable::assert_not_paused();
counter::write(counter::read() + 1);
}

#[external]
fn pause() {
Pausable::pause();
}

#[external]
fn unpause() {
Pausable::unpause();
}
}
47 changes: 47 additions & 0 deletions src/openzeppelin/tests/test_pausable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use openzeppelin::tests::mocks::mock_pausable::MockPausable;

#[test]
#[available_gas(2000000)]
fn test_pause_when_unpaused() {
assert(! MockPausable::is_paused(), 'Should not be paused');
assert(MockPausable::get_count() == 0, 'Should be 0');
MockPausable::assert_unpaused_and_increment();
assert(MockPausable::get_count() == 1, 'Should increment');
MockPausable::pause();
assert(MockPausable::is_paused(), 'Should be paused');
}

#[test]
#[available_gas(2000000)]
#[should_panic(expected = ('Pausable: paused', ))]
fn test_pause_when_paused() {
MockPausable::pause();
MockPausable::pause();
}

#[test]
#[available_gas(2000000)]
#[should_panic(expected = ('Pausable: paused', ))]
fn test_pause_increment() {
MockPausable::pause();
MockPausable::assert_unpaused_and_increment();
}

#[test]
#[available_gas(2000000)]
fn test_unpause_when_paused() {
MockPausable::pause();
assert(MockPausable::is_paused(), 'Should be paused');
MockPausable::unpause();
assert(! MockPausable::is_paused(), 'Should not be paused');
MockPausable::assert_unpaused_and_increment();
assert(MockPausable::get_count() == 1, 'Should increment');
}

#[test]
#[available_gas(2000000)]
#[should_panic(expected = ('Pausable: not paused', ))]
fn test_unpause_when_unpaused() {
assert(! MockPausable::is_paused(), 'Should be unpaused');
MockPausable::unpause();
}

0 comments on commit d671d4c

Please sign in to comment.