Skip to content

Commit

Permalink
Advanced concepts: typos, wording, grammar, formatting (NethermindEth…
Browse files Browse the repository at this point in the history
…#215)

* formatting for write to slot

* stor. arrs. > wording, grammar

* hashing > wording, grammar

* packing > wording, grammar, format

* list > amount->amounts, wording, typos

* plugins > grammar

* sign. verif. > grammar

* remove 'a' in how_to

* update library calls -> dispatcher

* Revert "update library calls -> dispatcher"

This reverts commit e7d3b0c.

* align library calls header

* fix tests

---------

Co-authored-by: Nenad <[email protected]>
  • Loading branch information
2 people authored and julio4 committed Oct 24, 2024
1 parent 0091a36 commit 777f885
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ pub mod TimeContract {
fn pack(value: Time) -> felt252 {
let msb: felt252 = 256 * value.hour.into();
let lsb: felt252 = value.minute.into();
return msb + lsb;
msb + lsb
}
fn unpack(value: felt252) -> Time {
let value: u16 = value.try_into().unwrap();
let (q, r) = DivRem::div_rem(value, 256_u16.try_into().unwrap());
let hour: u8 = Into::<u16, felt252>::into(q).try_into().unwrap();
let minute: u8 = Into::<u16, felt252>::into(r).try_into().unwrap();
return Time { hour, minute };
Time { hour, minute }
}
}

Expand Down
7 changes: 3 additions & 4 deletions listings/advanced-concepts/storing_arrays/src/contract.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use starknet::SyscallResultTrait;
use starknet::{Store, SyscallResult};
use starknet::storage_access::{StorageBaseAddress, storage_address_from_base_and_offset};
use starknet::syscalls::{storage_read_syscall, storage_write_syscall};
use starknet::storage_access::StorageBaseAddress;

// ANCHOR: StorageAccessImpl
impl StoreFelt252Array of Store<Array<felt252>> {
Expand All @@ -20,7 +19,7 @@ impl StoreFelt252Array of Store<Array<felt252>> {
) -> SyscallResult<Array<felt252>> {
let mut arr: Array<felt252> = array![];

// Read the stored array's length. If the length is superior to 255, the read will fail.
// Read the stored array's length. If the length is greater than 255, the read will fail.
let len: u8 = Store::<u8>::read_at_offset(address_domain, base, offset)
.expect('Storage Span too large');
offset += 1;
Expand All @@ -44,7 +43,7 @@ impl StoreFelt252Array of Store<Array<felt252>> {
fn write_at_offset(
address_domain: u32, base: StorageBaseAddress, mut offset: u8, mut value: Array<felt252>
) -> SyscallResult<()> {
// // Store the length of the array in the first storage slot.
// Store the length of the array in the first storage slot.
let len: u8 = value.len().try_into().expect('Storage - Span too large');
Store::<u8>::write_at_offset(address_domain, base, offset, len).unwrap();
offset += 1;
Expand Down
16 changes: 8 additions & 8 deletions listings/advanced-concepts/using_lists/src/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ pub mod ListExample {

#[storage]
pub struct Storage {
amount: List<u128>,
amounts: List<u128>,
tasks: List<Task>
}

#[abi(embed_v0)]
impl ListExample of super::IListExample<ContractState> {
fn add_in_amount(ref self: ContractState, number: u128) {
let mut current_amount_list = self.amount.read();
let mut current_amount_list = self.amounts.read();
current_amount_list.append(number).unwrap();
}

Expand All @@ -41,31 +41,31 @@ pub mod ListExample {
}

fn is_empty_list(self: @ContractState) -> bool {
let mut current_amount_list = self.amount.read();
let mut current_amount_list = self.amounts.read();
current_amount_list.is_empty()
}

fn list_length(self: @ContractState) -> u32 {
let mut current_amount_list = self.amount.read();
let mut current_amount_list = self.amounts.read();
current_amount_list.len()
}

fn get_from_index(self: @ContractState, index: u32) -> u128 {
self.amount.read()[index]
self.amounts.read()[index]
}

fn set_from_index(ref self: ContractState, index: u32, number: u128) {
let mut current_amount_list = self.amount.read();
let mut current_amount_list = self.amounts.read();
current_amount_list.set(index, number).unwrap();
}

fn pop_front_list(ref self: ContractState) {
let mut current_amount_list = self.amount.read();
let mut current_amount_list = self.amounts.read();
current_amount_list.pop_front().unwrap().unwrap();
}

fn array_conversion(self: @ContractState) -> Array<u128> {
let mut current_amount_list = self.amount.read();
let mut current_amount_list = self.amounts.read();
current_amount_list.array().unwrap()
}
}
Expand Down
6 changes: 3 additions & 3 deletions listings/advanced-concepts/using_lists/src/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use using_lists::contract::IListExample;
use using_lists::contract::{Task, ListExample};
use using_lists::contract::ListExample::{
amountContractMemberStateTrait, tasksContractMemberStateTrait
amountsContractMemberStateTrait, tasksContractMemberStateTrait
};

fn STATE() -> ListExample::ContractState {
Expand All @@ -13,7 +13,7 @@ fn STATE() -> ListExample::ContractState {
fn test_add_in_amount() {
let mut state = STATE();
state.add_in_amount(200);
assert(state.amount.read()[0] == 200, 'should be 200');
assert(state.amounts.read()[0] == 200, 'should be 200');
}

#[test]
Expand Down Expand Up @@ -67,7 +67,7 @@ fn test_set_from_index() {
let mut state = STATE();
state.add_in_amount(200);
state.set_from_index(0, 400);
assert(state.amount.read()[0] == 400, 'should be 400');
assert(state.amounts.read()[0] == 400, 'should be 400');
}

#[test]
Expand Down
30 changes: 15 additions & 15 deletions po/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -10840,7 +10840,7 @@ msgid ""
"\n"
" #[storage]\n"
" struct Storage {\n"
" amount: List<u128>,\n"
" amounts: List<u128>,\n"
" tasks: List<Task>\n"
" }\n"
"\n"
Expand All @@ -10854,7 +10854,7 @@ msgid ""
" #[abi(embed_v0)]\n"
" impl ListExample of super::IListExample<ContractState> {\n"
" fn add_in_amount(ref self: ContractState, number: u128) {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.append(number);\n"
" }\n"
"\n"
Expand All @@ -10867,32 +10867,32 @@ msgid ""
" }\n"
"\n"
" fn is_empty_list(self: @ContractState) -> bool {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.is_empty()\n"
" }\n"
"\n"
" fn list_length(self: @ContractState) -> u32 {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.len()\n"
" }\n"
"\n"
" fn get_from_index(self: @ContractState, index: u32) -> u128 {\n"
" self.amount.read()[index]\n"
" self.amounts.read()[index]\n"
" }\n"
"\n"
" fn set_from_index(ref self: ContractState, index: u32, number: u128) "
"{\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.set(index, number);\n"
" }\n"
"\n"
" fn pop_front_list(ref self: ContractState) {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.pop_front();\n"
" }\n"
"\n"
" fn array_conversion(self: @ContractState) -> Array<u128> {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.array()\n"
" }\n"
" }\n"
Expand Down Expand Up @@ -10933,7 +10933,7 @@ msgstr ""
" #[abi(embed_v0)]\n"
" impl ListExample of super::IListExample<ContractState> {\n"
" fn add_in_amount(ref self: ContractState, number: u128) {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.append(number);\n"
" }\n"
"\n"
Expand All @@ -10946,32 +10946,32 @@ msgstr ""
" }\n"
"\n"
" fn is_empty_list(self: @ContractState) -> bool {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.is_empty()\n"
" }\n"
"\n"
" fn list_length(self: @ContractState) -> u32 {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.len()\n"
" }\n"
"\n"
" fn get_from_index(self: @ContractState, index: u32) -> u128 {\n"
" self.amount.read()[index]\n"
" self.amounts.read()[index]\n"
" }\n"
"\n"
" fn set_from_index(ref self: ContractState, index: u32, number: u128) "
"{\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.set(index, number);\n"
" }\n"
"\n"
" fn pop_front_list(ref self: ContractState) {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.pop_front();\n"
" }\n"
"\n"
" fn array_conversion(self: @ContractState) -> Array<u128> {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.array()\n"
" }\n"
" }\n"
Expand Down
30 changes: 15 additions & 15 deletions po/zh-cn.po
Original file line number Diff line number Diff line change
Expand Up @@ -6945,7 +6945,7 @@ msgid ""
"\n"
" #[storage]\n"
" struct Storage {\n"
" amount: List<u128>,\n"
" amounts: List<u128>,\n"
" tasks: List<Task>\n"
" }\n"
"\n"
Expand All @@ -6959,7 +6959,7 @@ msgid ""
" #[abi(embed_v0)]\n"
" impl ListExample of super::IListExample<ContractState> {\n"
" fn add_in_amount(ref self: ContractState, number: u128) {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.append(number);\n"
" }\n"
"\n"
Expand All @@ -6972,32 +6972,32 @@ msgid ""
" }\n"
"\n"
" fn is_empty_list(self: @ContractState) -> bool {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.is_empty()\n"
" }\n"
"\n"
" fn list_length(self: @ContractState) -> u32 {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.len()\n"
" }\n"
"\n"
" fn get_from_index(self: @ContractState, index: u32) -> u128 {\n"
" self.amount.read()[index]\n"
" self.amounts.read()[index]\n"
" }\n"
"\n"
" fn set_from_index(ref self: ContractState, index: u32, number: u128) "
"{\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.set(index, number);\n"
" }\n"
"\n"
" fn pop_front_list(ref self: ContractState) {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.pop_front();\n"
" }\n"
"\n"
" fn array_conversion(self: @ContractState) -> Array<u128> {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.array()\n"
" }\n"
" }\n"
Expand Down Expand Up @@ -7038,7 +7038,7 @@ msgstr ""
" #[abi(embed_v0)]\n"
" impl ListExample of super::IListExample<ContractState> {\n"
" fn add_in_amount(ref self: ContractState, number: u128) {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.append(number);\n"
" }\n"
"\n"
Expand All @@ -7051,32 +7051,32 @@ msgstr ""
" }\n"
"\n"
" fn is_empty_list(self: @ContractState) -> bool {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.is_empty()\n"
" }\n"
"\n"
" fn list_length(self: @ContractState) -> u32 {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.len()\n"
" }\n"
"\n"
" fn get_from_index(self: @ContractState, index: u32) -> u128 {\n"
" self.amount.read()[index]\n"
" self.amounts.read()[index]\n"
" }\n"
"\n"
" fn set_from_index(ref self: ContractState, index: u32, number: u128) "
"{\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.set(index, number);\n"
" }\n"
"\n"
" fn pop_front_list(ref self: ContractState) {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.pop_front();\n"
" }\n"
"\n"
" fn array_conversion(self: @ContractState) -> Array<u128> {\n"
" let mut current_amount_list = self.amount.read();\n"
" let mut current_amount_list = self.amounts.read();\n"
" current_amount_list.array()\n"
" }\n"
" }\n"
Expand Down
6 changes: 3 additions & 3 deletions src/ch02/hashing.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ The resulting output is called a hash and it's completely different from the inp
Hash functions are deterministic, meaning that the same input will always produce the same output.

The two hash functions provided by the Cairo library are `Poseidon` and `Pedersen`.
Pedersen hashes were used in the past (but still used in some scenario for backward compatibility) while Poseidon hashes are the standard nowadays since they were designed to be very efficient for Zero Knowledge proof systems.
Pedersen hashes were used in the past (but are still used in some scenarios for backward compatibility), while Poseidon hashes are the standard nowadays since they were designed to be very efficient for Zero Knowledge proof systems.

In Cairo it's possible to hash all the types that can be converted to `felt252` since they implement natively the `Hash` trait. It's also possible to hash more complex types like structs by deriving the Hash trait with the attribute `#[derive(Hash)]` but only if all the struct's fields are themselves hashable.
In Cairo, it's possible to hash all types that can be converted to `felt252` since they natively implement the `Hash` trait. It's also possible to hash more complex types, like structs, by deriving the Hash trait with the `#[derive(Hash)]` attribute, but only if all the struct's fields are themselves hashable.

You first need to initialize a hash state with the `new` method of the `HashStateTrait` and then you can update it with the `update` method. You can accumulate multiple updates. Then, the `finalize` method returns the final hash value as a `felt252`.
To hash a value, you first need to initialize a hash state with the `new` method of the `HashStateTrait`. Then, you can update the hash state with the `update` method. You can accumulate multiple updates if necessary. Finally, the `finalize` method returns the final hash value as a `felt252`.

```rust
{{#rustdoc_include ../../listings/advanced-concepts/hash_trait/src/hash_trait.cairo:hash}}
Expand Down
5 changes: 1 addition & 4 deletions src/ch02/library_calls.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Library Dispatcher
# Library Calls

External calls can be made on Starknet by two means: Contract dispatchers or Library dispatchers. Dispatchers are automatically created and exported by the compiler when a contract interface is defined.

Expand All @@ -11,6 +11,3 @@ For further reading: [Cairo book](https://book.cairo-lang.org/ch15-02-contract-d
```rust
{{#rustdoc_include ../../listings/advanced-concepts/library_calls/src/library_call.cairo:library_dispatcher}}
```



Loading

0 comments on commit 777f885

Please sign in to comment.