Skip to content

Commit

Permalink
feat: extract internal
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Nov 3, 2023
1 parent b32fa50 commit f20895c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 95 deletions.
1 change: 1 addition & 0 deletions src/identity.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod main;
mod internal;
92 changes: 92 additions & 0 deletions src/identity/internal.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use identity::identity::main::Identity;
use starknet::{SyscallResultTrait, storage_base_address_from_felt252};

#[generate_trait]
impl InternalImpl of InternalTrait {
// todo: move these functions into a storage contract when components are available
fn get_extended(
self: @Identity::ContractState,
fn_name: felt252,
params: Span<felt252>,
length: felt252,
domain: u32
) -> Span<felt252> {
let base = self.compute_base_address(fn_name, params);
let mut data = ArrayTrait::new();
let mut offset = 0;
loop {
if length == offset.into() {
break ();
}
let value = self._get(domain, storage_base_address_from_felt252(base + offset));
data.append(value);
offset += 1;
};
data.span()
}

fn get_unbounded(
self: @Identity::ContractState, fn_name: felt252, params: Span<felt252>, domain: u32
) -> Span<felt252> {
let base = self.compute_base_address(fn_name, params);
let mut data = ArrayTrait::new();
let mut offset = 0;
loop {
let value = self._get(domain, storage_base_address_from_felt252(base + offset));
if value == 0 {
break ();
}
data.append(value);
offset += 1;
};
data.span()
}

fn _get(
self: @Identity::ContractState, domain: u32, base: starknet::StorageBaseAddress
) -> felt252 {
starknet::storage_read_syscall(
domain, starknet::storage_address_from_base_and_offset(base, 0)
)
.unwrap_syscall()
}

fn set(
ref self: Identity::ContractState,
fn_name: felt252,
params: Span<felt252>,
value: Span<felt252>,
domain: u32,
) {
let base = self.compute_base_address(fn_name, params);
self._set(domain, base, value);
}

fn _set(
ref self: Identity::ContractState, domain: u32, base: felt252, mut values: Span<felt252>,
) {
match values.pop_back() {
Option::Some(value) => {
let addr = storage_base_address_from_felt252(base + values.len().into());
starknet::storage_write_syscall(
domain, starknet::storage_address_from_base_and_offset(addr, 0), *value
);
self._set(domain, base, values);
},
Option::None(_) => {},
}
}

fn compute_base_address(
self: @Identity::ContractState, fn_name: felt252, mut params: Span<felt252>
) -> felt252 {
let mut hashed = fn_name;
loop {
match params.pop_front() {
Option::Some(param) => { hashed = hash::LegacyHash::hash(hashed, *param); },
Option::None => { break; }
};
};
hashed
}
}
91 changes: 1 addition & 90 deletions src/identity/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod Identity {
},
introspection::{src5::SRC5 as src5_component, dual_src5::{DualCaseSRC5, DualCaseSRC5Trait}}
};
use identity::identity::internal::InternalTrait;

const USER_DATA_ADDR: felt252 =
1043580099640415304067929596039389735845630832049981224284932480360577081706;
Expand Down Expand Up @@ -262,94 +263,4 @@ mod Identity {
);
}
}

//
// Internals
//

#[generate_trait]
impl InternalImpl of InternalTrait {
// todo: move these functions into a storage contract when components are available
fn get_extended(
self: @ContractState,
fn_name: felt252,
params: Span<felt252>,
length: felt252,
domain: u32
) -> Span<felt252> {
let base = self.compute_base_address(fn_name, params);
let mut data = ArrayTrait::new();
let mut offset = 0;
loop {
if length == offset.into() {
break ();
}
let value = self._get(domain, storage_base_address_from_felt252(base + offset));
data.append(value);
offset += 1;
};
data.span()
}

fn get_unbounded(
self: @ContractState, fn_name: felt252, params: Span<felt252>, domain: u32
) -> Span<felt252> {
let base = self.compute_base_address(fn_name, params);
let mut data = ArrayTrait::new();
let mut offset = 0;
loop {
let value = self._get(domain, storage_base_address_from_felt252(base + offset));
if value == 0 {
break ();
}
data.append(value);
offset += 1;
};
data.span()
}

fn _get(self: @ContractState, domain: u32, base: starknet::StorageBaseAddress) -> felt252 {
starknet::storage_read_syscall(
domain, starknet::storage_address_from_base_and_offset(base, 0)
)
.unwrap_syscall()
}

fn set(
ref self: ContractState,
fn_name: felt252,
params: Span<felt252>,
value: Span<felt252>,
domain: u32,
) {
let base = self.compute_base_address(fn_name, params);
self._set(domain, base, value);
}

fn _set(ref self: ContractState, domain: u32, base: felt252, mut values: Span<felt252>,) {
match values.pop_back() {
Option::Some(value) => {
let addr = storage_base_address_from_felt252(base + values.len().into());
starknet::storage_write_syscall(
domain, starknet::storage_address_from_base_and_offset(addr, 0), *value
);
self._set(domain, base, values);
},
Option::None(_) => {},
}
}

fn compute_base_address(
self: @ContractState, fn_name: felt252, mut params: Span<felt252>
) -> felt252 {
let mut hashed = fn_name;
loop {
match params.pop_front() {
Option::Some(param) => { hashed = hash::LegacyHash::hash(hashed, *param); },
Option::None => { break; }
};
};
hashed
}
}
}
11 changes: 6 additions & 5 deletions src/tests/test_addresses_comp.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use starknet::testing;
use identity::identity::main::Identity;
use identity::identity::internal::InternalImpl;
use identity::interface::identity::{IIdentity, IIdentityDispatcher, IIdentityDispatcherTrait};
use identity::identity::main::Identity;
use debug::PrintTrait;
use starknet::{SyscallResultTrait, StorageBaseAddress};
use traits::{Into, TryInto};
Expand All @@ -15,14 +16,14 @@ fn test_compute_address_single_param() {

// It should return the same value as utils::get_storage_var_address() from starknet-rs
let expected_0 = 0x04496ba66d9685813220a5ba3d7b2be924385ad47abfafeec804b0e2f3f0ec56;
let computed_addr_0 = Identity::InternalImpl::compute_base_address(
let computed_addr_0 = InternalImpl::compute_base_address(
@unsafe_state, VERIFIER_DATA_ADDR, array![0].span()
);
assert(computed_addr_0 == expected_0, 'Invalid storage address');

// It should return the same value as utils::get_storage_var_address() from starknet-rs
let expected: felt252 = 0x01f65ea3e42f099a1c085eecf45ce0d476a1ab440e3ed539604cac5ba6944258;
let computed_addr = Identity::InternalImpl::compute_base_address(
let computed_addr = InternalImpl::compute_base_address(
@unsafe_state, VERIFIER_DATA_ADDR, array![123].span()
);
assert(computed_addr == expected, 'Invalid storage address');
Expand All @@ -36,7 +37,7 @@ fn test_compute_address_multiple_params() {

// It should return the same value as utils::get_storage_var_address() from starknet-rs
let expected = 0x023289a31298cac4a750e1fbc154c96b5398aa7e94018d9d5c115690aa124767;
let computed_addr = Identity::InternalImpl::compute_base_address(
let computed_addr = InternalImpl::compute_base_address(
@unsafe_state, VERIFIER_DATA_ADDR, array![0, 1, 3].span()
);
assert(computed_addr == expected, 'Invalid storage address');
Expand All @@ -50,7 +51,7 @@ fn test_compute_address_empty_param() {

// It should return the same value as utils::get_storage_var_address() from starknet-rs
let expected = 0x00ac8e2e1fdb949863544c38e1ed04b4c447121f2b60005f7c7f798c6a35ab40;
let computed_addr = Identity::InternalImpl::compute_base_address(
let computed_addr = InternalImpl::compute_base_address(
@unsafe_state, VERIFIER_DATA_ADDR, array![].span()
);
assert(computed_addr == expected, 'Invalid storage address');
Expand Down

0 comments on commit f20895c

Please sign in to comment.