Skip to content

Commit

Permalink
audit: registration verification (#71)
Browse files Browse the repository at this point in the history
* audit: registration verification

* fix: fmt

* feat: break loop if condition
  • Loading branch information
JordyRo1 authored Nov 13, 2023
1 parent 82cca31 commit df592e9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/publisher_registry/publisher_registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ mod PublisherRegistry {
let existing_publisher_address = PublisherRegistryImpl::get_publisher_address(
@self, publisher
);

assert(!is_address_registered(@self, publisher_address), 'Address already registered');
assert(existing_publisher_address.is_zero(), 'Name already registered');
assert(!publisher_address.is_zero(), 'Cannot set address to zero');
let publishers_len = self.publishers_storage_len.read();
Expand All @@ -132,7 +132,9 @@ mod PublisherRegistry {
@self, publisher
);
let caller = get_caller_address();

assert(
!is_address_registered(@self, new_publisher_address), 'Address already registered'
);
assert(!existing_publisher_address.is_zero(), 'Name not registered');

assert(caller == existing_publisher_address, 'Caller is not the publisher');
Expand Down Expand Up @@ -441,5 +443,27 @@ mod PublisherRegistry {
// gas::withdraw_gas_all(get_builtin_costs()).expect('Out of gas');
_iter_publisher_sources(self, cur_idx + 1_usize, max_idx, publisher, ref sources_arr)
}

// @notice check if a given contract address is already associated to a publisher
// @param address: address to check
// @returns a boolean
fn is_address_registered(self: @ContractState, address: ContractAddress) -> bool {
let mut cur_idx = 0;
let arr_len = self.publishers_storage_len.read();
let mut boolean = false;
loop {
if (cur_idx == arr_len) {
break ();
}
let publisher = self.publishers_storage.read(cur_idx);
let publisher_address = self.publisher_address_storage.read(publisher);
if (publisher_address == address) {
boolean = true;
break ();
}
cur_idx += 1;
};
boolean
}
}

28 changes: 28 additions & 0 deletions src/tests/test_publisher_registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,31 @@ fn test_transfer_ownership() {
let admin_address = publisher_registry.get_admin_address();
assert(admin_address == test_address, 'wrong admin address');
}


#[test]
#[should_panic(expected: ('Address already registered', 'ENTRYPOINT_FAILED'))]
#[available_gas(20000000000)]
fn test_add_pubisher_should_fail_if_already_registered() {
let admin = contract_address_const::<0x12345>();
set_contract_address(admin);
let publisher_registry = deploy_publisher_registry();
let publisher_address = contract_address_const::<0x54321>();
publisher_registry.add_publisher(1, publisher_address);
publisher_registry.add_publisher(2, publisher_address);
}


#[test]
#[should_panic(expected: ('Address already registered', 'ENTRYPOINT_FAILED'))]
#[available_gas(20000000000)]
fn test_update_pubisher_should_fail_if_already_registered() {
let admin = contract_address_const::<0x12345>();
set_contract_address(admin);
let publisher_registry = deploy_publisher_registry();
let publisher_address = contract_address_const::<0x54321>();
publisher_registry.add_publisher(1, publisher_address);
let new_publisher_address = contract_address_const::<0x54322>();
publisher_registry.add_publisher(2, new_publisher_address);
publisher_registry.update_publisher_address(2, publisher_address);
}

0 comments on commit df592e9

Please sign in to comment.