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

Commit

Permalink
fix: balance print in errors (#1986)
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware authored Jun 17, 2024
1 parent b898b1c commit d924c79
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
13 changes: 9 additions & 4 deletions crates/blockifier/src/fee/fee_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashSet;

use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use num_bigint::BigUint;
use starknet_api::core::ContractAddress;
use starknet_api::hash::StarkFelt;
use starknet_api::state::StorageKey;
Expand Down Expand Up @@ -121,14 +122,12 @@ pub fn verify_can_pay_committed_bounds(
TransactionFeeError::L1GasBoundsExceedBalance {
max_amount: l1_bounds.max_amount,
max_price: l1_bounds.max_price_per_unit,
balance_low,
balance_high,
balance: balance_to_big_uint(&balance_low, &balance_high),
}
}
TransactionInfo::Deprecated(context) => TransactionFeeError::MaxFeeExceedsBalance {
max_fee: context.max_fee,
balance_low,
balance_high,
balance: balance_to_big_uint(&balance_low, &balance_high),
},
})
}
Expand All @@ -146,3 +145,9 @@ pub fn get_address_balance_keys(address: ContractAddress) -> (StorageKey, Storag
});
(balance_key_low, balance_key_high)
}

pub(crate) fn balance_to_big_uint(balance_low: &StarkFelt, balance_high: &StarkFelt) -> BigUint {
let low = BigUint::from_bytes_be(balance_low.bytes());
let high = BigUint::from_bytes_be(balance_high.bytes());
(high << 128) + low
}
15 changes: 5 additions & 10 deletions crates/blockifier/src/transaction/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use num_bigint::BigUint;
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector, Nonce};
use starknet_api::hash::StarkFelt;
use starknet_api::transaction::{Fee, TransactionVersion};
use starknet_api::StarknetApiError;
use thiserror::Error;
Expand All @@ -23,16 +23,11 @@ pub enum TransactionFeeError {
InsufficientL1Fee { paid_fee: Fee, actual_fee: Fee },
#[error(
"L1 gas bounds (max amount: {max_amount}, max price: {max_price}) exceed balance \
(Uint256({balance_low}, {balance_high}))."
({balance})."
)]
L1GasBoundsExceedBalance {
max_amount: u64,
max_price: u128,
balance_low: StarkFelt,
balance_high: StarkFelt,
},
#[error("Max fee ({}) exceeds balance (Uint256({balance_low}, {balance_high})).", max_fee.0)]
MaxFeeExceedsBalance { max_fee: Fee, balance_low: StarkFelt, balance_high: StarkFelt },
L1GasBoundsExceedBalance { max_amount: u64, max_price: u128, balance: BigUint },
#[error("Max fee ({}) exceeds balance ({balance}).", max_fee.0, )]
MaxFeeExceedsBalance { max_fee: Fee, balance: BigUint },
#[error("Max fee ({}) is too low. Minimum fee: {}.", max_fee.0, min_fee.0)]
MaxFeeTooLow { min_fee: Fee, max_fee: Fee },
#[error(
Expand Down
8 changes: 8 additions & 0 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use assert_matches::assert_matches;
use cairo_felt::Felt252;
use cairo_vm::vm::runners::builtin_runner::{HASH_BUILTIN_NAME, RANGE_CHECK_BUILTIN_NAME};
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use num_bigint::BigUint;
use num_traits::Pow;
use once_cell::sync::Lazy;
use pretty_assertions::assert_eq;
Expand Down Expand Up @@ -35,6 +36,7 @@ use crate::execution::execution_utils::{felt_to_stark_felt, stark_felt_to_felt};
use crate::execution::syscalls::hint_processor::EmitEventError;
use crate::execution::syscalls::SyscallSelector;
use crate::fee::actual_cost::TransactionReceipt;
use crate::fee::fee_utils::balance_to_big_uint;
use crate::fee::gas_usage::{
estimate_minimal_gas_vector, get_da_gas_cost, get_onchain_data_segment_length,
};
Expand Down Expand Up @@ -2012,3 +2014,9 @@ fn test_emit_event_exceeds_limit(
}
}
}

#[test]
fn test_balance_print() {
let int = balance_to_big_uint(&StarkFelt::from(16_u64), &StarkFelt::from(1_u64));
assert!(format!("{}", int) == (BigUint::from(u128::MAX) + BigUint::from(17_u128)).to_string());
}

0 comments on commit d924c79

Please sign in to comment.