Skip to content

Commit

Permalink
use storage prefix to avoid clashes
Browse files Browse the repository at this point in the history
  • Loading branch information
pscott committed Oct 16, 2024
1 parent f2ad4a5 commit 8d000ed
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
10 changes: 5 additions & 5 deletions starknet/src/utils/reinitializable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod ReinitializableComponent {

#[storage]
struct Storage {
_initialized: bool
Reinitializable_initialized: bool
}

// Event is needed to derive PartialEq on it so we can test it in other modules.
Expand All @@ -20,22 +20,22 @@ mod ReinitializableComponent {
/// Initialize the contract. Must not have been initialized before.
fn initialize(ref self: ComponentState<TContractState>) {
self.not_initialized();
self._initialized.write(true);
self.Reinitializable_initialized.write(true);
}

/// Reset the initialization state of the contract, allowing it to be initialized again in the future.
fn reset(ref self: ComponentState<TContractState>) {
self._initialized.write(false);
self.Reinitializable_initialized.write(false);
}

/// Asserts that the contract has been initialized.
fn initialized(self: @ComponentState<TContractState>) {
assert(self._initialized.read() == true, 'Not Initialized');
assert(self.Reinitializable_initialized.read() == true, 'Not Initialized');
}

/// Asserts that the contract has not been initialized.
fn not_initialized(self: @ComponentState<TContractState>) {
assert(self._initialized.read() == false, 'Already Initialized');
assert(self.Reinitializable_initialized.read() == false, 'Already Initialized');
}
}
}
8 changes: 4 additions & 4 deletions starknet/src/utils/simple_quorum.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ mod SimpleQuorumComponent {

#[storage]
struct Storage {
_quorum: u256
Simplequorum_quorum: u256
}

#[generate_trait]
impl InternalImpl<
TContractState, +HasComponent<TContractState>
> of InternalTrait<TContractState> {
fn initializer(ref self: ComponentState<TContractState>, quorum: u256) {
self._quorum.write(quorum);
self.Simplequorum_quorum.write(quorum);
}

/// Returns the status of a proposal.
Expand All @@ -32,7 +32,7 @@ mod SimpleQuorumComponent {
votes_against: u256,
votes_abstain: u256,
) -> ProposalStatus {
let quorum_reached = votes_for + votes_abstain >= self._quorum.read();
let quorum_reached = votes_for + votes_abstain >= self.Simplequorum_quorum.read();
let supported = votes_for > votes_against;
let accepted = quorum_reached && supported;

Expand Down Expand Up @@ -64,7 +64,7 @@ mod SimpleQuorumComponent {
TContractState, +HasComponent<TContractState>
> of IQuorum<ComponentState<TContractState>> {
fn quorum(self: @ComponentState<TContractState>) -> u256 {
self._quorum.read()
self.Simplequorum_quorum.read()
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions starknet/src/utils/single_slot_proof.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ mod SingleSlotProofComponent {

#[storage]
struct Storage {
_timestamp_remappers: ContractAddress,
_facts_registry: ContractAddress,
_cached_remapped_timestamps: LegacyMap::<u32, u256>
Singleslotproof_timestamp_remappers: ContractAddress,
Singleslotproof_facts_registry: ContractAddress,
Singleslotproof_cached_remapped_timestamps: LegacyMap::<u32, u256>
}

#[generate_trait]
Expand All @@ -23,8 +23,8 @@ mod SingleSlotProofComponent {
timestamp_remappers: ContractAddress,
facts_registry: ContractAddress
) {
self._timestamp_remappers.write(timestamp_remappers);
self._facts_registry.write(facts_registry);
self.Singleslotproof_timestamp_remappers.write(timestamp_remappers);
self.Singleslotproof_facts_registry.write(facts_registry);
}

fn get_storage_slot(
Expand All @@ -35,12 +35,12 @@ mod SingleSlotProofComponent {
mpt_proof: Span<Words64>
) -> u256 {
// Checks if the timestamp is already cached.
let l1_block_number = self._cached_remapped_timestamps.read(timestamp);
let l1_block_number = self.Singleslotproof_cached_remapped_timestamps.read(timestamp);
assert(l1_block_number.is_non_zero(), 'Timestamp not cached');

// Returns the value of the storage slot of account: `l1_contract_address` at key: `slot_key` and block number: `l1_block_number`.
let slot_value = IEVMFactsRegistryDispatcher {
contract_address: self._facts_registry.read()
contract_address: self.Singleslotproof_facts_registry.read()
}
.get_storage(l1_block_number, l1_contract_address.into(), slot_key, mpt_proof);

Expand All @@ -54,17 +54,17 @@ mod SingleSlotProofComponent {
// timestamp is less than the earliest timestamp or larger than the latest timestamp in the mapper
// then the call will return Option::None and the transaction will revert.
let l1_block_number = ITimestampRemappersDispatcher {
contract_address: self._timestamp_remappers.read()
contract_address: self.Singleslotproof_timestamp_remappers.read()
}
.get_closest_l1_block_number(tree, timestamp.into())
.expect('TimestampRemappers call failed')
.expect('Timestamp out of range');

self._cached_remapped_timestamps.write(timestamp, l1_block_number);
self.Singleslotproof_cached_remapped_timestamps.write(timestamp, l1_block_number);
}

fn cached_timestamps(self: @ComponentState<TContractState>, timestamp: u32) -> u256 {
let l1_block_number = self._cached_remapped_timestamps.read(timestamp);
let l1_block_number = self.Singleslotproof_cached_remapped_timestamps.read(timestamp);
assert(l1_block_number.is_non_zero(), 'Timestamp not cached');
l1_block_number
}
Expand Down
19 changes: 10 additions & 9 deletions starknet/src/utils/space_manager.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod SpaceManagerComponent {

#[storage]
struct Storage {
_spaces: LegacyMap::<ContractAddress, bool>
Spacemanager_spaces: LegacyMap::<ContractAddress, bool>
}

#[event]
Expand Down Expand Up @@ -35,33 +35,34 @@ mod SpaceManagerComponent {
match spaces.pop_front() {
Option::Some(space) => {
assert(
(*space).is_non_zero() && !self._spaces.read(*space), 'Invalid Space'
(*space).is_non_zero() && !self.Spacemanager_spaces.read(*space),
'Invalid Space'
);
self._spaces.write(*space, true);
self.Spacemanager_spaces.write(*space, true);
},
Option::None(()) => { break; }
};
}
}

fn enable_space(ref self: ComponentState<TContractState>, space: ContractAddress) {
assert(space.is_non_zero() && !self._spaces.read(space), 'Invalid Space');
self._spaces.write(space, true);
assert(space.is_non_zero() && !self.Spacemanager_spaces.read(space), 'Invalid Space');
self.Spacemanager_spaces.write(space, true);
self.emit(Event::SpaceEnabled(SpaceEnabled { space: space }));
}

fn disable_space(ref self: ComponentState<TContractState>, space: ContractAddress) {
assert(self._spaces.read(space), 'Invalid Space');
self._spaces.write(space, false);
assert(self.Spacemanager_spaces.read(space), 'Invalid Space');
self.Spacemanager_spaces.write(space, false);
self.emit(Event::SpaceDisabled(SpaceDisabled { space: space }));
}

fn is_space_enabled(self: @ComponentState<TContractState>, space: ContractAddress) -> bool {
return self._spaces.read(space);
return self.Spacemanager_spaces.read(space);
}

fn assert_only_spaces(self: @ComponentState<TContractState>) {
assert(self._spaces.read(info::get_caller_address()), 'Unauthorized Space');
assert(self.Spacemanager_spaces.read(info::get_caller_address()), 'Unauthorized Space');
}
}
}
Expand Down

0 comments on commit 8d000ed

Please sign in to comment.