Skip to content

Commit

Permalink
Use ByteArrays instead of felt252s in our own library assertions (#…
Browse files Browse the repository at this point in the history
…1999)

<!-- Reference any GitHub issues resolved by this PR -->

Closes #1892

## Introduced changes

<!-- A brief description of the changes -->

- Use `ByteArray`s instead of `felt252`s in our own library assertions

## 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`
  • Loading branch information
Draggu authored Apr 10, 2024
1 parent ba96c4d commit 2a26982
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
36 changes: 20 additions & 16 deletions sncast_std/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ pub fn call(

let mut buf = handle_cheatcode(cheatcode::<'call'>(inputs.span()));

let mut result_data: Result<CallResult, ScriptCommandError> = Serde::<
Result<CallResult>
>::deserialize(ref buf)
.expect('call deserialize failed');
let mut result_data: Result<CallResult, ScriptCommandError> =
match Serde::<Result<CallResult>>::deserialize(ref buf) {
Option::Some(result_data) => result_data,
Option::None => panic!("call deserialize failed")
};

result_data
}
Expand Down Expand Up @@ -165,10 +166,11 @@ pub fn declare(

let mut buf = handle_cheatcode(cheatcode::<'declare'>(inputs.span()));

let mut result_data: Result<DeclareResult, ScriptCommandError> = Serde::<
Result<DeclareResult>
>::deserialize(ref buf)
.expect('declare deserialize failed');
let mut result_data: Result<DeclareResult, ScriptCommandError> =
match Serde::<Result<DeclareResult>>::deserialize(ref buf) {
Option::Some(result_data) => result_data,
Option::None => panic!("declare deserialize failed"),
};

result_data
}
Expand Down Expand Up @@ -221,10 +223,11 @@ pub fn deploy(

let mut buf = handle_cheatcode(cheatcode::<'deploy'>(inputs.span()));

let mut result_data: Result<DeployResult, ScriptCommandError> = Serde::<
Result<DeployResult>
>::deserialize(ref buf)
.expect('deploy deserialize failed');
let mut result_data: Result<DeployResult, ScriptCommandError> =
match Serde::<Result<DeployResult>>::deserialize(ref buf) {
Option::Some(result_data) => result_data,
Option::None => panic!("deploy deserialize failed")
};

result_data
}
Expand Down Expand Up @@ -265,10 +268,11 @@ pub fn invoke(

let mut buf = handle_cheatcode(cheatcode::<'invoke'>(inputs.span()));

let mut result_data: Result<InvokeResult, ScriptCommandError> = Serde::<
Result<InvokeResult>
>::deserialize(ref buf)
.expect('invoke deserialize failed');
let mut result_data: Result<InvokeResult, ScriptCommandError> =
match Serde::<Result<InvokeResult>>::deserialize(ref buf) {
Option::Some(result_data) => result_data,
Option::None => panic!("invoke deserialize failed")
};

result_data
}
Expand Down
5 changes: 4 additions & 1 deletion snforge_std/src/cheatcodes/contract_class.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ fn get_class_hash(contract_address: ContractAddress) -> ClassHash {

// Expecting a buffer with one felt252, being the class hash.
let buf = handle_cheatcode(cheatcode::<'get_class_hash'>(array![contract_address_felt].span()));
(*buf[0]).try_into().expect('Invalid class hash value')
match (*buf[0]).try_into() {
Option::Some(hash) => hash,
Option::None => panic!("Invalid class hash value")
}
}

fn _prepare_calldata(
Expand Down
13 changes: 5 additions & 8 deletions snforge_std/src/cheatcodes/events.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::option::OptionTrait;
use starknet::testing::cheatcode;
use starknet::ContractAddress;
use super::super::_cheatcode::handle_cheatcode;
Expand Down Expand Up @@ -76,11 +77,8 @@ impl EventAssertionsImpl<
let emitted = is_emitted(ref self, from, event);

if !emitted {
panic(
array![
'Event with matching data and', 'keys was not emitted from', (*from).into()
]
);
let from: felt252 = (*from).into();
panic!("Event with matching data and keys was not emitted from {}", from);
}

i += 1;
Expand All @@ -100,9 +98,8 @@ impl EventAssertionsImpl<
let emitted = is_emitted(ref self, from, event);

if emitted {
panic(
array!['Event with matching data and', 'keys was emitted from', (*from).into()]
);
let from: felt252 = (*from).into();
panic!("Event with matching data and keys was emitted from {}", from);
}

i += 1;
Expand Down
2 changes: 1 addition & 1 deletion snforge_std/src/cheatcodes/storage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn validate_storage_address_felt(storage_address_felt: felt252) {
match storage_address_try_from_felt252(storage_address_felt) {
Option::Some(_) => {},
// Panics in order not to leave inconsistencies in the state
Option::None(()) => panic(array!['storage_address out of range', storage_address_felt]),
Option::None(()) => panic!("storage_address out of range {}", storage_address_felt),
}
}

Expand Down
6 changes: 4 additions & 2 deletions snforge_std/src/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ impl SyscallResultStringErrorTraitImpl<T> of SyscallResultStringErrorTrait<T> {
Result::Err(panic_data) => {
if panic_data.len() > 0 && *panic_data.at(0) == BYTE_ARRAY_MAGIC {
let mut panic_data_span = panic_data.span().slice(1, panic_data.len() - 1);
let deserialized = Serde::<ByteArray>::deserialize(ref panic_data_span)
.expect('panic string not deserializable');
let deserialized = match Serde::<ByteArray>::deserialize(ref panic_data_span) {
Option::Some(str) => str,
Option::None => panic!("panic string not deserializable")
};
return Result::Err(PanicDataOrString::String(deserialized));
}
Result::Err(PanicDataOrString::PanicData(panic_data))
Expand Down

0 comments on commit 2a26982

Please sign in to comment.