-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ByteArray
based errors handling (#1679)
<!-- Reference any GitHub issues resolved by this PR --> Closes #1627 ## Introduced changes <!-- A brief description of the changes --> - Adds a mechanism for parsing the errors from the trace and passing it on to test runner - Adds an extension for ease of further parsing the array ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [x] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [x] Added changes to `CHANGELOG.md` --------- Co-authored-by: Piotr Magiera <[email protected]> Co-authored-by: Kamil Jankowski <[email protected]>
- Loading branch information
1 parent
9505d79
commit 7f2ff86
Showing
14 changed files
with
350 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
crates/cheatnet/tests/contracts/src/bytearray_string_panic_call.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use starknet::ContractAddress; | ||
|
||
#[starknet::interface] | ||
trait IByteArrayPanickingContract<TContractState> { | ||
fn do_panic(self: @TContractState); | ||
} | ||
|
||
#[starknet::contract] | ||
mod ByteArrayPanickingContract { | ||
#[storage] | ||
struct Storage {} | ||
|
||
#[abi(embed_v0)] | ||
impl Impl of super::IByteArrayPanickingContract<ContractState> { | ||
fn do_panic(self: @ContractState) { | ||
assert!( | ||
false, | ||
"This is a very long\n and multiline string, that will for sure saturate the pending_word" | ||
); | ||
} | ||
} | ||
} | ||
|
||
#[starknet::interface] | ||
trait IByteArrayPanickingContractProxy<TContractState> { | ||
fn call_bytearray_panicking_contract(self: @TContractState, contract_address: ContractAddress); | ||
} | ||
|
||
#[starknet::contract] | ||
mod ByteArrayPanickingContractProxy { | ||
use starknet::ContractAddress; | ||
use super::{IByteArrayPanickingContractDispatcherTrait, IByteArrayPanickingContractDispatcher}; | ||
|
||
#[storage] | ||
struct Storage {} | ||
|
||
#[abi(embed_v0)] | ||
impl Impl of super::IByteArrayPanickingContractProxy<ContractState> { | ||
fn call_bytearray_panicking_contract( | ||
self: @ContractState, contract_address: ContractAddress | ||
) { | ||
IByteArrayPanickingContractDispatcher { contract_address }.do_panic(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ mod warp; | |
mod segment_arena_user; | ||
mod panic_call; | ||
mod store_load; | ||
mod bytearray_string_panic_call; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use cairo_felt::Felt252; | ||
use cairo_lang_utils::byte_array::{BYTES_IN_WORD, BYTE_ARRAY_MAGIC}; | ||
use itertools::chain; | ||
use num_traits::Num; | ||
|
||
pub struct ByteArray { | ||
words: Vec<Felt252>, | ||
pending_word_len: usize, | ||
pending_word: Felt252, | ||
} | ||
|
||
impl From<String> for ByteArray { | ||
fn from(value: String) -> Self { | ||
let chunks = value.as_bytes().chunks_exact(BYTES_IN_WORD); | ||
let remainder = chunks.remainder(); | ||
let pending_word_len = remainder.len(); | ||
|
||
let words = chunks.map(Felt252::from_bytes_be).collect(); | ||
let pending_word = Felt252::from_bytes_be(remainder); | ||
|
||
Self { | ||
words, | ||
pending_word_len, | ||
pending_word, | ||
} | ||
} | ||
} | ||
|
||
impl ByteArray { | ||
#[must_use] | ||
pub fn serialize(self) -> Vec<Felt252> { | ||
chain!( | ||
[ | ||
Felt252::from_str_radix(BYTE_ARRAY_MAGIC, 16).unwrap(), | ||
self.words.len().into() | ||
], | ||
self.words.into_iter(), | ||
[self.pending_word, self.pending_word_len.into()] | ||
) | ||
.collect() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.