Skip to content

Commit 6ec9a77

Browse files
committed
Fix gas.
1 parent 73706c1 commit 6ec9a77

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ edition = "2021"
66
[dependencies]
77
cairo-lang-compiler = "2.9.0-dev.0"
88
cairo-lang-filesystem = "2.9.0-dev.0"
9+
cairo-lang-runner = "2.9.0-dev.0"
910
cairo-lang-sierra = "2.9.0-dev.0"
1011
cairo-lang-sierra-ap-change = "2.9.0-dev.0"
1112
cairo-lang-sierra-gas = "2.9.0-dev.0"
13+
cairo-lang-starknet-classes = "2.9.0-dev.0"
1214
cairo-lang-utils = "2.9.0-dev.0"
1315
clap = { version = "4.5.20", features = ["derive"] }
1416
k256 = "0.13.4"

src/gas.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use cairo_lang_runner::token_gas_cost;
12
use cairo_lang_sierra::{
23
extensions::gas::CostTokenType,
34
ids::FunctionId,
@@ -195,21 +196,3 @@ fn calc_metadata(
195196
gas_info: pre_gas_info.combine(post_gas_info),
196197
})
197198
}
198-
199-
pub fn token_gas_cost(token_type: CostTokenType) -> usize {
200-
match token_type {
201-
CostTokenType::Const => 1,
202-
CostTokenType::Step
203-
| CostTokenType::Hole
204-
| CostTokenType::RangeCheck
205-
| CostTokenType::RangeCheck96 => {
206-
panic!("Token type {:?} has no gas cost.", token_type)
207-
}
208-
CostTokenType::Pedersen => 4130,
209-
CostTokenType::Poseidon => 500,
210-
CostTokenType::Bitwise => 594,
211-
CostTokenType::EcOp => 4166,
212-
CostTokenType::AddMod => 234,
213-
CostTokenType::MulMod => 616,
214-
}
215-
}

src/vm.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ use cairo_lang_sierra::{
99
extensions::{
1010
circuit::CircuitTypeConcrete,
1111
core::{CoreConcreteLibfunc, CoreLibfunc, CoreType, CoreTypeConcrete},
12+
gas::CostTokenType,
1213
starknet::StarkNetTypeConcrete,
1314
ConcreteLibfunc, ConcreteType,
1415
},
1516
ids::{ConcreteLibfuncId, FunctionId, VarId},
1617
program::{GenFunction, GenStatement, Invocation, Program, StatementIdx},
1718
program_registry::ProgramRegistry,
1819
};
20+
use cairo_lang_starknet_classes::{
21+
casm_contract_class::ENTRY_POINT_COST, contract_class::ContractEntryPoints,
22+
};
1923
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
2024
use smallvec::{smallvec, SmallVec};
2125
use starknet_types_core::felt::Felt;
@@ -79,10 +83,33 @@ impl VirtualMachine {
7983
}
8084

8185
impl<S: StarknetSyscallHandler> VirtualMachine<S> {
82-
pub fn new_starknet(program: Arc<Program>, syscall_handler: S) -> Self {
86+
pub fn new_starknet(
87+
program: Arc<Program>,
88+
entry_points: &ContractEntryPoints,
89+
syscall_handler: S,
90+
) -> Self {
8391
let registry = ProgramRegistry::new(&program).unwrap();
8492
Self {
85-
gas: GasMetadata::new(&program, Some(MetadataComputationConfig::default())).unwrap(),
93+
gas: GasMetadata::new(
94+
&program,
95+
Some(MetadataComputationConfig {
96+
function_set_costs: entry_points
97+
.constructor
98+
.iter()
99+
.chain(entry_points.external.iter())
100+
.chain(entry_points.l1_handler.iter())
101+
.map(|id| {
102+
(
103+
program.funcs[id.function_idx].id.clone(),
104+
[(CostTokenType::Const, ENTRY_POINT_COST)].into(),
105+
)
106+
})
107+
.collect(),
108+
linear_gas_solver: true,
109+
linear_ap_change_solver: true,
110+
}),
111+
)
112+
.unwrap(),
86113
program,
87114
registry,
88115
syscall_handler,

0 commit comments

Comments
 (0)