-
Notifications
You must be signed in to change notification settings - Fork 76
Custom assertion methods return Result #848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 10 commits
edf6f5d
909b38a
c77afb1
1f12195
e37d93e
d104800
e1d3bd5
dc4fa67
a9bd618
f000fe6
0102175
e16a2b8
ae93217
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,7 +17,7 @@ use starknet_rs_core::types::contract::{CompiledClass, SierraClass}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use starknet_rs_core::types::{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
BlockId, BlockTag, ContractClass, ContractExecutionError, DeployAccountTransactionResult, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ExecutionResult, FeeEstimate, Felt, FlattenedSierraClass, FunctionCall, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
InnerContractExecutionError, ResourceBounds, ResourceBoundsMapping, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
InnerContractExecutionError, ResourceBounds, ResourceBoundsMapping, TransactionReceipt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use starknet_rs_core::utils::{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
UdcUniqueSettings, get_selector_from_name, get_udc_deployed_address, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -102,16 +102,23 @@ pub fn get_timestamp_asserter_contract_artifacts() -> SierraWithCasmHash { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub async fn assert_tx_succeeded_accepted<T: Provider>(tx_hash: &Felt, client: &T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let receipt = client.get_transaction_receipt(tx_hash).await.unwrap().receipt; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub async fn assert_tx_succeeded_accepted<T: Provider>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tx_hash: &Felt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
client: &T, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) -> Result<(), anyhow::Error> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let receipt = match client.get_transaction_receipt(tx_hash).await { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(receipt) => receipt.receipt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Err(err) => return Err(err.into()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match receipt.execution_result() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ExecutionResult::Succeeded => (), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
other => panic!("Should have succeeded; got: {other:?}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
other => anyhow::bail!("Should have succeeded; got: {other:?}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match receipt.finality_status() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
starknet_rs_core::types::TransactionFinalityStatus::AcceptedOnL2 => (), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
other => panic!("Should have been accepted on L2; got: {other:?}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
starknet_rs_core::types::TransactionFinalityStatus::AcceptedOnL2 => Ok(()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
other => anyhow::bail!("Should have been accepted on L2; got: {other:?}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -129,15 +136,18 @@ pub async fn assert_tx_succeeded_pre_confirmed<T: Provider>(_tx_hash: &Felt, _cl | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub async fn get_contract_balance(devnet: &BackgroundDevnet, contract_address: Felt) -> Felt { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub async fn get_contract_balance( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
devnet: &BackgroundDevnet, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
contract_address: Felt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) -> Result<Felt, anyhow::Error> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
get_contract_balance_by_block_id(devnet, contract_address, BlockId::Tag(BlockTag::Latest)).await | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub async fn get_contract_balance_by_block_id( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
devnet: &BackgroundDevnet, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
contract_address: Felt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
block_id: BlockId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) -> Felt { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) -> Result<Felt, anyhow::Error> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let contract_call = FunctionCall { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
contract_address, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
entry_point_selector: get_selector_from_name("get_balance").unwrap(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -146,25 +156,33 @@ pub async fn get_contract_balance_by_block_id( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match devnet.json_rpc_client.call(contract_call, block_id).await { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(res) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert_eq!(res.len(), 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(res[0]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Err(e) => panic!("Call failed: {e}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Err(e) => anyhow::bail!("Call failed: {e}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keeping assertions in these helper functions does not completely address the issue targeted by this PR. Perhaps it can be restructured as: Ok(res) if res.len() == 1 => Ok(res[0]),
other => anyhow::bail!("Expected a single-felt result; got: {other:?}"), |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub async fn assert_tx_reverted<T: Provider>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tx_hash: &Felt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
client: &T, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expected_failure_reasons: &[&str], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let receipt = client.get_transaction_receipt(tx_hash).await.unwrap().receipt; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) -> Result<(), anyhow::Error> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let receipt: TransactionReceipt = match client.get_transaction_receipt(tx_hash).await { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(receipt) => receipt.receipt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Err(e) => return Err(e.into()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can it be replaced with this?
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match receipt.execution_result() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ExecutionResult::Reverted { reason } => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for expected_reason in expected_failure_reasons { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert_contains(reason, expected_reason); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match assert_contains(reason, expected_reason) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(_) => (), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Err(e) => return Err(e), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can it be replaced with this?
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
other => panic!("Should have reverted; got: {other:?}; receipt: {receipt:?}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
other => anyhow::bail!("Should have reverted; got: {other:?}; receipt: {receipt:?}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -436,14 +454,22 @@ pub async fn deploy_argent_account( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// Assert that the set of elements of `iterable1` is a subset of the elements of `iterable2` and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// vice versa. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub fn assert_equal_elements<T>(iterable1: &[T], iterable2: &[T]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub fn assert_equal_elements<T>(iterable1: &[T], iterable2: &[T]) -> Result<(), anyhow::Error> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
where | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
T: PartialEq, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert_eq!(iterable1.len(), iterable2.len()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match iterable1.len() == iterable2.len() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
true => (), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
false => return Err(anyhow::anyhow!("Length mismatch")), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for e in iterable1 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert!(iterable2.contains(e)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match iterable2.contains(e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
true => (), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
false => return Err(anyhow::anyhow!("Element mismatch")), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO matching with cases
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub fn felt_to_u256(f: Felt) -> U256 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -479,18 +505,20 @@ pub fn assert_json_rpc_errors_equal(e1: JsonRpcError, e2: JsonRpcError) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// Extract the message that is encapsulated inside the provided error. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub fn extract_message_error(error: &ContractExecutionError) -> &String { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub fn extract_message_error(error: &ContractExecutionError) -> Result<&String, anyhow::Error> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ContractExecutionError::Message(msg) => msg, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
other => panic!("Unexpected error: {other:?}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ContractExecutionError::Message(msg) => Ok(msg), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
other => anyhow::bail!("Unexpected error: {other:?}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// Extract the error that is nested inside the provided `error`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub fn extract_nested_error(error: &ContractExecutionError) -> &InnerContractExecutionError { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub fn extract_nested_error( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error: &ContractExecutionError, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) -> Result<&InnerContractExecutionError, anyhow::Error> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ContractExecutionError::Nested(nested) => nested, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
other => panic!("Unexpected error: {other:?}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ContractExecutionError::Nested(nested) => Ok(nested), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
other => anyhow::bail!("Unexpected error: {other:?}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -581,11 +609,16 @@ pub async fn receive_notification( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(notification["params"]["result"].take()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub async fn assert_no_notifications(ws: &mut WebSocketStream<MaybeTlsStream<TcpStream>>) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub async fn assert_no_notifications( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ws: &mut WebSocketStream<MaybeTlsStream<TcpStream>>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) -> Result<(), anyhow::Error> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match receive_rpc_via_ws(ws).await { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(resp) => panic!("Expected no notifications; found: {resp}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Err(e) if e.to_string().contains("deadline has elapsed") => { /* expected */ } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Err(e) => panic!("Expected to error out due to empty channel; found: {e}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(resp) => anyhow::bail!("Expected no notifications; found: {resp}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Err(e) if e.to_string().contains("deadline has elapsed") => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/* expected */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Err(e) => anyhow::bail!("Expected to error out due to empty channel; found: {e}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -672,15 +705,16 @@ impl From<FeeEstimate> for LocalFee { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// Panics if `text` does not contain `pattern` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub fn assert_contains(text: &str, pattern: &str) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub fn assert_contains(text: &str, pattern: &str) -> Result<(), anyhow::Error> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if !text.contains(pattern) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
panic!( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
anyhow::bail!( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"Failed content assertion! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Pattern: '{pattern}' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
not present in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Text: '{text}'" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// Set time and generate a new block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can it be replaced with this?