Skip to content

Commit

Permalink
Fix gas.
Browse files Browse the repository at this point in the history
  • Loading branch information
azteca1998 committed Nov 26, 2024
1 parent 73706c1 commit 6ec9a77
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ edition = "2021"
[dependencies]
cairo-lang-compiler = "2.9.0-dev.0"
cairo-lang-filesystem = "2.9.0-dev.0"
cairo-lang-runner = "2.9.0-dev.0"
cairo-lang-sierra = "2.9.0-dev.0"
cairo-lang-sierra-ap-change = "2.9.0-dev.0"
cairo-lang-sierra-gas = "2.9.0-dev.0"
cairo-lang-starknet-classes = "2.9.0-dev.0"
cairo-lang-utils = "2.9.0-dev.0"
clap = { version = "4.5.20", features = ["derive"] }
k256 = "0.13.4"
Expand Down
19 changes: 1 addition & 18 deletions src/gas.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cairo_lang_runner::token_gas_cost;
use cairo_lang_sierra::{
extensions::gas::CostTokenType,
ids::FunctionId,
Expand Down Expand Up @@ -195,21 +196,3 @@ fn calc_metadata(
gas_info: pre_gas_info.combine(post_gas_info),
})
}

pub fn token_gas_cost(token_type: CostTokenType) -> usize {
match token_type {
CostTokenType::Const => 1,
CostTokenType::Step
| CostTokenType::Hole
| CostTokenType::RangeCheck
| CostTokenType::RangeCheck96 => {
panic!("Token type {:?} has no gas cost.", token_type)
}
CostTokenType::Pedersen => 4130,
CostTokenType::Poseidon => 500,
CostTokenType::Bitwise => 594,
CostTokenType::EcOp => 4166,
CostTokenType::AddMod => 234,
CostTokenType::MulMod => 616,
}
}
31 changes: 29 additions & 2 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ use cairo_lang_sierra::{
extensions::{
circuit::CircuitTypeConcrete,
core::{CoreConcreteLibfunc, CoreLibfunc, CoreType, CoreTypeConcrete},
gas::CostTokenType,
starknet::StarkNetTypeConcrete,
ConcreteLibfunc, ConcreteType,
},
ids::{ConcreteLibfuncId, FunctionId, VarId},
program::{GenFunction, GenStatement, Invocation, Program, StatementIdx},
program_registry::ProgramRegistry,
};
use cairo_lang_starknet_classes::{
casm_contract_class::ENTRY_POINT_COST, contract_class::ContractEntryPoints,
};
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use smallvec::{smallvec, SmallVec};
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -79,10 +83,33 @@ impl VirtualMachine {
}

impl<S: StarknetSyscallHandler> VirtualMachine<S> {
pub fn new_starknet(program: Arc<Program>, syscall_handler: S) -> Self {
pub fn new_starknet(
program: Arc<Program>,
entry_points: &ContractEntryPoints,
syscall_handler: S,
) -> Self {
let registry = ProgramRegistry::new(&program).unwrap();
Self {
gas: GasMetadata::new(&program, Some(MetadataComputationConfig::default())).unwrap(),
gas: GasMetadata::new(
&program,
Some(MetadataComputationConfig {
function_set_costs: entry_points
.constructor
.iter()
.chain(entry_points.external.iter())
.chain(entry_points.l1_handler.iter())
.map(|id| {
(
program.funcs[id.function_idx].id.clone(),
[(CostTokenType::Const, ENTRY_POINT_COST)].into(),
)
})
.collect(),
linear_gas_solver: true,
linear_ap_change_solver: true,
}),
)
.unwrap(),
program,
registry,
syscall_handler,
Expand Down

0 comments on commit 6ec9a77

Please sign in to comment.