Skip to content
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

fix(py): find more correct gas_consumed #429

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions crates/pathfinder/src/cairo/ext_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ mod tests {
assert_eq!(
at_block_fee,
crate::rpc::types::reply::FeeEstimate {
consumed: H256::from_low_u64_be(0),
consumed: H256::from_low_u64_be(3),
gas_price: H256::from_low_u64_be(1),
fee: H256::from_low_u64_be(4)
}
Expand All @@ -443,7 +443,7 @@ mod tests {
assert_eq!(
current_fee,
crate::rpc::types::reply::FeeEstimate {
consumed: H256::from_low_u64_be(0),
consumed: H256::from_low_u64_be(3),
gas_price: H256::from_low_u64_be(10),
fee: H256::from_low_u64_be(35)
}
Expand Down
25 changes: 20 additions & 5 deletions py/src/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,19 +641,34 @@ async def do_call(


def estimate_fee_after_call(general_config, call_info, carried_state):
from starkware.starknet.business_logic.transaction_fee import calculate_tx_fee
import math
from starkware.starknet.business_logic.utils import get_invoke_tx_total_resources

(l1_gas_used, _cairo_resources_used) = get_invoke_tx_total_resources(
# FIXME: this is now completly inlined calculate_tx_fee, fix with cairo-lang 0.9.1
# it has been adapted to variable names from 167b28bcd940fd25ea3816204fa882a0b0a49603
(l1_gas_usage, cairo_resource_usage) = get_invoke_tx_total_resources(
carried_state, call_info
)

overall_fee = calculate_tx_fee(carried_state, call_info, general_config)
cairo_resource_fee_weights = general_config.cairo_resource_fee_weights
cairo_resource_names = set(cairo_resource_usage.keys())
assert cairo_resource_names.issubset(
cairo_resource_fee_weights.keys()
), "Cairo resource names must be contained in fee weights dict."

# Convert Cairo usage to L1 gas usage.
cairo_l1_gas_usage = max(
cairo_resource_fee_weights[key] * cairo_resource_usage.get(key, 0)
for key in cairo_resource_fee_weights
)

total_l1_gas_usage = cairo_l1_gas_usage + l1_gas_usage
overall_fee = math.ceil(total_l1_gas_usage * carried_state.block_info.gas_price)

return {
"gas_consumed": l1_gas_used,
"gas_consumed": int(total_l1_gas_usage),
"gas_price": carried_state.block_info.gas_price,
"overall_fee": overall_fee,
"overall_fee": int(overall_fee),
}


Expand Down
8 changes: 4 additions & 4 deletions py/src/test_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def test_fee_estimate_on_positive_directly():
(verb, output, _timings) = loop_inner(con, command)

assert output == {
"gas_consumed": 0,
"gas_consumed": 3,
"gas_price": 0,
"overall_fee": 0,
}
Expand All @@ -441,7 +441,7 @@ def test_fee_estimate_on_positive():
assert first == {
"status": "ok",
"output": {
"gas_consumed": "0x" + (0).to_bytes(32, "big").hex(),
"gas_consumed": "0x" + (3).to_bytes(32, "big").hex(),
"gas_price": "0x" + (0).to_bytes(32, "big").hex(),
"overall_fee": "0x" + (0).to_bytes(32, "big").hex(),
},
Expand All @@ -450,7 +450,7 @@ def test_fee_estimate_on_positive():
assert second == {
"status": "ok",
"output": {
"gas_consumed": "0x" + (0).to_bytes(32, "big").hex(),
"gas_consumed": "0x" + (3).to_bytes(32, "big").hex(),
"gas_price": "0x" + (10).to_bytes(32, "big").hex(),
"overall_fee": "0x" + (35).to_bytes(32, "big").hex(),
},
Expand Down Expand Up @@ -509,7 +509,7 @@ def test_failing_mainnet_tx2():

# this is correct
assert output == {
"gas_consumed": 8568,
"gas_consumed": 8732,
"gas_price": 21367239423,
"overall_fee": 186590486623319,
}
Expand Down