Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Remove hardcoded values in estimate_fee test #1526

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next release

- dev: better estimate fee test
- chore(rpc): clean trace api
- feat(rpc): added state diff real value in trace api
- chore: update cairo-vm commit and update gas per op
Expand Down
43 changes: 31 additions & 12 deletions starknet-rpc-test/estimate_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,20 @@ async fn fail_if_one_txn_cannot_be_executed(madara: &ThreadSafeMadaraClient) ->
#[tokio::test]
async fn works_ok(madara: &ThreadSafeMadaraClient) -> Result<(), anyhow::Error> {
let rpc = madara.get_starknet_client().await;
let sender_address = FieldElement::from_hex_be(ACCOUNT_CONTRACT).unwrap();
let calldata = vec![
FieldElement::from_hex_be(TEST_CONTRACT_ADDRESS).unwrap(),
get_selector_from_name("sqrt").unwrap(),
FieldElement::from_hex_be("1").unwrap(),
FieldElement::from(81u8),
];

let tx = BroadcastedInvokeTransaction {
max_fee: FieldElement::ZERO,
signature: vec![],
nonce: FieldElement::ZERO,
sender_address: FieldElement::from_hex_be(ACCOUNT_CONTRACT).unwrap(),
calldata: vec![
FieldElement::from_hex_be(TEST_CONTRACT_ADDRESS).unwrap(),
get_selector_from_name("sqrt").unwrap(),
FieldElement::from_hex_be("1").unwrap(),
FieldElement::from(81u8),
],
sender_address,
calldata: calldata.clone(),
is_query: true,
};

Expand All @@ -103,13 +105,30 @@ async fn works_ok(madara: &ThreadSafeMadaraClient) -> Result<(), anyhow::Error>
let estimates =
rpc.estimate_fee(&vec![invoke_transaction, invoke_transaction_2], BlockId::Tag(BlockTag::Latest)).await?;

// TODO: instead execute the tx and check that the actual fee are the same as the estimated ones
let tx = BroadcastedInvokeTransaction {
max_fee: FieldElement::from(210u16),
signature: vec![],
nonce: FieldElement::ZERO,
sender_address,
calldata,
is_query: false,
};

let invoke_transaction = BroadcastedTransaction::Invoke(tx.clone());

let invoke_transaction_2 =
BroadcastedTransaction::Invoke(BroadcastedInvokeTransaction { nonce: FieldElement::ONE, ..tx });

let simulations = rpc
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we execute it instead of doing the simulation? i think that would be a better test, wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apoorvsadana Thank you for your comment. Could you please clarify how to check the actual fee after execution? Additionally, does "executing it" refer to calling JsonRpcClient.add_invoke_transaction? Thanks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can fetch the receipt to get the actual_fee. Yes, you need to call add_invoke_transaction

Copy link
Contributor Author

@fishseabowl fishseabowl Apr 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apoorvsadana, Thank you for your help. do you know how long we should wait until MaybePendingTransactionReceipt gets the Receipt value?

In my local machine, waiting for 20 seconds works fine, estimate_fee equals actual_fee(0xf0).

However, in the rpc-tests, it fails. After I changed wait time to 200 seconds, it still failed.

In another case, after adding PendingReceipt to match arm, it fails in rpc-tests because PendingReceipt.actual_fee(0xdc) is slightly smaller than Receipt.actual_fee(0xf0).

It looks like there is no validate, the fee should be 0xd2(210), if validate is skipped, the fee should be 0xdc(220), with signature, the fee should be 0xf0(240).

.simulate_transactions(BlockId::Tag(BlockTag::Latest), &[invoke_transaction, invoke_transaction_2], [])
.await?;

assert_eq!(estimates.len(), 2);
assert_eq!(estimates[0].overall_fee, 210);
assert_eq!(estimates[1].overall_fee, 210);
assert_eq!(estimates[0].overall_fee, simulations[0].fee_estimation.overall_fee);
assert_eq!(estimates[1].overall_fee, simulations[1].fee_estimation.overall_fee);
// https://starkscan.co/block/5
assert_eq!(estimates[0].gas_consumed, 0);
assert_eq!(estimates[1].gas_consumed, 0);
assert_eq!(estimates[0].gas_consumed, simulations[0].fee_estimation.gas_consumed);
assert_eq!(estimates[1].gas_consumed, simulations[1].fee_estimation.gas_consumed);

Ok(())
}
Loading