-
Notifications
You must be signed in to change notification settings - Fork 48
GasReserve libfuncs #1439
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
Open
DiegoCivi
wants to merge
19
commits into
main
Choose a base branch
from
gas_reserve-libfuncs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
GasReserve libfuncs #1439
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
75ebc06
Begin implementation of GasReserve libfuncs
DiegoCivi ef485c7
Implement gas_reserve_create libfunc
DiegoCivi 2dceb04
Implement from_ptr for GasReserve
DiegoCivi 5cddeb6
Implement gas_reserve_utilize libfunc
DiegoCivi 2d18455
Refactor create libfunc tests
DiegoCivi 9452c2e
Refactor of utilize libfunc tests
DiegoCivi 72a3c0c
Update corelib patch
DiegoCivi 926e73d
Implement sierra-emu side
DiegoCivi c557c9e
Add docu and remove todos
DiegoCivi 1835660
Merge branch 'main' into gas_reserve-libfuncs
gabrielbosio ab389e6
Merge branch 'main' into gas_reserve-libfuncs
DiegoCivi c09a95c
Apply suggestions from code review
DiegoCivi 62229c4
Use proptest
DiegoCivi b460de7
Fix clippy
DiegoCivi ecf794f
Merge branch 'main' into gas_reserve-libfuncs
DiegoCivi 43b084f
Revert "Fix clippy"
DiegoCivi c8acf98
Revert "Use proptest"
DiegoCivi 2056560
Fix clippy
DiegoCivi c32e627
Merge branch 'gas_reserve-libfuncs' of github.com:lambdaclass/cairo_n…
DiegoCivi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| use cairo_lang_sierra::extensions::gas_reserve::GasReserveConcreteLibfunc; | ||
| use smallvec::smallvec; | ||
|
|
||
| use crate::{vm::EvalAction, Value}; | ||
|
|
||
| pub fn eval(selector: &GasReserveConcreteLibfunc, args: Vec<Value>) -> EvalAction { | ||
| match selector { | ||
| GasReserveConcreteLibfunc::Create(_) => eval_gas_reserve_create(args), | ||
| GasReserveConcreteLibfunc::Utilize(_) => eval_gas_reserve_utilize(args), | ||
| } | ||
| } | ||
|
|
||
| fn eval_gas_reserve_create(args: Vec<Value>) -> EvalAction { | ||
| let [range_check @ Value::Unit, Value::U64(gas), Value::U128(amount)]: [Value; 3] = | ||
| args.try_into().unwrap() | ||
| else { | ||
| panic!() | ||
| }; | ||
|
|
||
| if amount <= gas.into() { | ||
| let spare_gas = gas - amount as u64; | ||
| EvalAction::NormalBranch( | ||
| 0, | ||
| smallvec![range_check, Value::U64(spare_gas), Value::U128(amount)], | ||
| ) | ||
| } else { | ||
| EvalAction::NormalBranch(1, smallvec![range_check, Value::U64(gas)]) | ||
| } | ||
| } | ||
|
|
||
| fn eval_gas_reserve_utilize(args: Vec<Value>) -> EvalAction { | ||
| let [Value::U64(gas), Value::U128(amount)]: [Value; 2] = args.try_into().unwrap() else { | ||
| panic!() | ||
| }; | ||
|
|
||
| let updated_gas = gas + amount as u64; | ||
|
|
||
| EvalAction::NormalBranch(0, smallvec![Value::U64(updated_gas)]) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| use crate::libfuncs::LibfuncHelper; | ||
| use crate::{error::Result, metadata::MetadataStorage}; | ||
| use cairo_lang_sierra::extensions::lib_func::SignatureOnlyConcreteLibfunc; | ||
| use cairo_lang_sierra::{ | ||
| extensions::{ | ||
| core::{CoreLibfunc, CoreType}, | ||
| gas_reserve::GasReserveConcreteLibfunc, | ||
| }, | ||
| program_registry::ProgramRegistry, | ||
| }; | ||
| use melior::dialect::arith::{self, CmpiPredicate}; | ||
| use melior::helpers::{ArithBlockExt, BuiltinBlockExt}; | ||
| use melior::ir::r#type::IntegerType; | ||
| use melior::{ | ||
| ir::{Block, Location}, | ||
| Context, | ||
| }; | ||
|
|
||
| pub fn build<'ctx, 'this>( | ||
| context: &'ctx Context, | ||
| registry: &ProgramRegistry<CoreType, CoreLibfunc>, | ||
| entry: &'this Block<'ctx>, | ||
| location: Location<'ctx>, | ||
| helper: &LibfuncHelper<'ctx, 'this>, | ||
| metadata: &mut MetadataStorage, | ||
| selector: &GasReserveConcreteLibfunc, | ||
| ) -> Result<()> { | ||
| match selector { | ||
| GasReserveConcreteLibfunc::Create(info) => { | ||
| build_gas_reserve_create(context, registry, entry, location, helper, metadata, info) | ||
| } | ||
| GasReserveConcreteLibfunc::Utilize(info) => { | ||
| build_gas_reserve_utilize(context, registry, entry, location, helper, metadata, info) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Generate MLIR operations for the `gas_reserve_create` libfunc. | ||
| /// | ||
| /// # Cairo Signature | ||
| /// | ||
| /// ```cairo | ||
| /// pub extern fn gas_reserve_create( | ||
| /// amount: u128, | ||
| /// ) -> Option<GasReserve> implicits(RangeCheck, GasBuiltin) nopanic; | ||
| /// ``` | ||
| fn build_gas_reserve_create<'ctx, 'this>( | ||
| context: &'ctx Context, | ||
| _registry: &ProgramRegistry<CoreType, CoreLibfunc>, | ||
| entry: &'this Block<'ctx>, | ||
| location: Location<'ctx>, | ||
| helper: &LibfuncHelper<'ctx, 'this>, | ||
| _metadata: &mut MetadataStorage, | ||
| _info: &SignatureOnlyConcreteLibfunc, | ||
| ) -> Result<()> { | ||
| let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; | ||
| let current_gas = entry.arg(1)?; // u64 | ||
| let amount = entry.arg(2)?; // u128 | ||
|
|
||
| let amount_ty = IntegerType::new(context, 128).into(); | ||
| let current_gas_128 = entry.append_op_result(arith::extui(current_gas, amount_ty, location))?; | ||
| let enough_gas = entry.cmpi( | ||
| context, | ||
| CmpiPredicate::Uge, | ||
| current_gas_128, | ||
| amount, | ||
| location, | ||
| )?; | ||
|
|
||
| let gas_builtin_ty = IntegerType::new(context, 64).into(); | ||
| let spare_gas = entry.append_op_result(arith::subi(current_gas_128, amount, location))?; | ||
| let spare_gas = entry.append_op_result(arith::trunci(spare_gas, gas_builtin_ty, location))?; | ||
|
|
||
| helper.cond_br( | ||
| context, | ||
| entry, | ||
| enough_gas, | ||
| [0, 1], | ||
| [ | ||
| &[range_check, spare_gas, amount], | ||
| &[range_check, current_gas], | ||
| ], | ||
| location, | ||
| ) | ||
| } | ||
|
|
||
| /// Generate MLIR operations for the `gas_reserve_utilize` libfunc. | ||
| /// | ||
| /// # Cairo Signature | ||
| /// | ||
| /// ```cairo | ||
| /// pub extern fn gas_reserve_utilize(reserve: GasReserve) implicits(GasBuiltin) nopanic; | ||
| /// ``` | ||
| fn build_gas_reserve_utilize<'ctx, 'this>( | ||
| context: &'ctx Context, | ||
| _registry: &ProgramRegistry<CoreType, CoreLibfunc>, | ||
| entry: &'this Block<'ctx>, | ||
| location: Location<'ctx>, | ||
| helper: &LibfuncHelper<'ctx, 'this>, | ||
| _metadata: &mut MetadataStorage, | ||
| _info: &SignatureOnlyConcreteLibfunc, | ||
| ) -> Result<()> { | ||
| let current_gas = entry.arg(0)?; // u64 | ||
| let gas_reserve = entry.arg(1)?; // u128 | ||
|
|
||
| let trunc_reserve = entry.append_op_result(arith::trunci( | ||
| gas_reserve, | ||
| IntegerType::new(context, 64).into(), | ||
| location, | ||
| ))?; | ||
| let updated_gas = entry.append_op_result(arith::addi(current_gas, trunc_reserve, location))?; | ||
DiegoCivi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| helper.br(entry, 0, &[updated_gas], location) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use crate::{load_cairo, utils::testing::run_program, Value}; | ||
|
|
||
| #[test] | ||
| fn run_create() { | ||
| let program = load_cairo!( | ||
| use core::gas::{GasReserve, gas_reserve_create, gas_reserve_utilize}; | ||
|
|
||
| fn run_test_1() -> Option<GasReserve> { | ||
| gas_reserve_create(100) | ||
| } | ||
|
|
||
| fn run_test_2(amount: u128) -> u128 { | ||
| let initial_gas = core::testing::get_available_gas(); | ||
| let reserve = gas_reserve_create(amount).unwrap(); | ||
| let final_gas = core::testing::get_available_gas(); | ||
| gas_reserve_utilize(reserve); | ||
|
|
||
| initial_gas - final_gas | ||
| } | ||
| ); | ||
|
|
||
| let result = run_program(&program, "run_test_1", &[]).return_value; | ||
| if let Value::Enum { tag, value, .. } = result { | ||
| assert_eq!(tag, 0); | ||
| assert_eq!(value, Box::new(Value::Uint128(100))) | ||
| } | ||
|
|
||
| let amount = 100; | ||
| let result = run_program(&program, "run_test_2", &[Value::Uint128(amount)]).return_value; | ||
| if let Value::Enum { tag, value, .. } = result { | ||
| if let Value::Struct { fields, .. } = *value { | ||
| assert_eq!(tag, 0); | ||
| assert_eq!(fields[0], Value::Uint128(amount)); | ||
| } | ||
| } | ||
|
|
||
| let amount = 700; | ||
| let result = run_program(&program, "run_test_2", &[Value::Uint128(amount)]).return_value; | ||
| if let Value::Enum { tag, value, .. } = result { | ||
| if let Value::Struct { fields, .. } = *value { | ||
| assert_eq!(tag, 0); | ||
| assert_eq!(fields[0], Value::Uint128(amount)); | ||
| } | ||
| } | ||
gabrielbosio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| #[test] | ||
| fn run_utilize() { | ||
| let program = load_cairo!( | ||
| use core::gas::{GasReserve, gas_reserve_create, gas_reserve_utilize}; | ||
|
|
||
| fn run_test(amount: u128) -> u128 { | ||
| let initial_gas = core::testing::get_available_gas(); | ||
| let reserve = gas_reserve_create(amount).unwrap(); | ||
| gas_reserve_utilize(reserve); | ||
| let final_gas = core::testing::get_available_gas(); | ||
|
|
||
| initial_gas - final_gas | ||
| } | ||
| ); | ||
|
|
||
| let gas_quant = 10; | ||
| let result = run_program(&program, "run_test", &[Value::Uint128(gas_quant)]).return_value; | ||
| if let Value::Enum { tag, value, .. } = result { | ||
| if let Value::Struct { fields, .. } = *value { | ||
| assert_eq!(tag, 0); | ||
| assert_eq!(fields[0], Value::Uint128(0)); | ||
| } | ||
| } | ||
|
|
||
| let gas_quant = 1000; | ||
| let result = run_program(&program, "run_test", &[Value::Uint128(gas_quant)]).return_value; | ||
| if let Value::Enum { tag, value, .. } = result { | ||
| if let Value::Struct { fields, .. } = *value { | ||
| assert_eq!(tag, 0); | ||
| assert_eq!(fields[0], Value::Uint128(0)); | ||
| } | ||
| } | ||
gabrielbosio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.