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

Fix pending owner late overwrite issue #1122

Merged
merged 12 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `DualCaseAccountABI` renamed to `DualCaseAccountTrait`
- `DualCaseEthAccountABI` renamed to `DualCaseEthAccountTrait`

### Fixed

- `OwnableTwoStep` allowing a pending owner to accept ownership after the original owner has renounced ownership (#1119)

## 0.15.1 (2024-08-13)

### Changed
Expand Down
2 changes: 2 additions & 0 deletions packages/access/src/ownable/ownable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ pub mod OwnableComponent {
fn _transfer_ownership(
ref self: ComponentState<TContractState>, new_owner: ContractAddress
) {
self.Ownable_pending_owner.write(Zero::zero());

andrew-fleming marked this conversation as resolved.
Show resolved Hide resolved
let previous_owner: ContractAddress = self.Ownable_owner.read();
self.Ownable_owner.write(new_owner);
self
Expand Down
16 changes: 15 additions & 1 deletion packages/access/src/tests/test_ownable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use openzeppelin_access::ownable::interface::{IOwnable, IOwnableCamelOnly};
use openzeppelin_access::tests::mocks::ownable_mocks::DualCaseOwnableMock;

use openzeppelin_test_common::ownable::OwnableSpyHelpers;
use openzeppelin_testing::constants::{ZERO, OTHER, OWNER};
use openzeppelin_testing::constants::{ZERO, OTHER, OWNER, RECIPIENT};
use snforge_std::{spy_events, test_address, start_cheat_caller_address};

//
Expand Down Expand Up @@ -86,6 +86,20 @@ fn test__transfer_ownership() {
assert_eq!(current_owner, OTHER());
}

#[test]
fn test__transfer_ownership_resets_pending_owner() {
let mut state = setup();

state.Ownable_pending_owner.write(OTHER());
let current_pending_owner = state.Ownable_pending_owner.read();
assert_eq!(current_pending_owner, OTHER());

state._transfer_ownership(RECIPIENT());

let current_owner = state.Ownable_pending_owner.read();
assert!(current_owner.is_zero());
ericnordelo marked this conversation as resolved.
Show resolved Hide resolved
}

//
// transfer_ownership & transferOwnership
//
Expand Down
41 changes: 16 additions & 25 deletions packages/access/src/tests/test_ownable_twostep.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,22 @@ fn test_renounce_ownership() {
assert!(state.owner().is_zero());
}

#[test]
fn test_renounce_ownership_resets_pending_owner() {
let mut state = setup();
let contract_address = test_address();
start_cheat_caller_address(contract_address, OWNER());

state.Ownable_pending_owner.write(OTHER());
let current_pending_owner = state.Ownable_pending_owner.read();
assert_eq!(current_pending_owner, OTHER());

state.renounce_ownership();

let current_owner = state.Ownable_pending_owner.read();
assert!(current_owner.is_zero());
ericnordelo marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
#[should_panic(expected: ('Caller is the zero address',))]
fn test_renounce_ownership_from_zero_address() {
Expand Down Expand Up @@ -307,31 +323,6 @@ fn test_full_two_step_transfer() {
assert!(state.pending_owner().is_zero());
}

#[test]
fn test_pending_accept_after_owner_renounce() {
let mut state = setup();
let mut spy = spy_events();
let contract_address = test_address();
start_cheat_caller_address(contract_address, OWNER());
state.transfer_ownership(OTHER());

spy.assert_event_ownership_transfer_started(contract_address, OWNER(), OTHER());
assert_eq!(state.owner(), OWNER());
assert_eq!(state.pending_owner(), OTHER());

state.renounce_ownership();

spy.assert_only_event_ownership_transferred(contract_address, OWNER(), ZERO());
assert!(state.owner().is_zero());

start_cheat_caller_address(contract_address, OTHER());
state.accept_ownership();

spy.assert_only_event_ownership_transferred(contract_address, ZERO(), OTHER());
assert_eq!(state.owner(), OTHER());
assert!(state.pending_owner().is_zero());
}

ericnordelo marked this conversation as resolved.
Show resolved Hide resolved
//
// Helpers
//
Expand Down