Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 9154239

Browse files
azteca1998Oppenfmolettajuanbonofguthmann
authored
Make contract caches shared. (#940)
* Unify deprecated and casm contract caches. * Fix formatting and clippy. * Remove unused code. * Unify contract classes in the state traits too. * Fix typos. Make cache shared. * Minor fixes. * Fix testing state generation to make it work with shared caches. * Fix formatting. * Add cache diff. * Add suggestion. * Fix stuff. * Implement cache trait. Add a null and a permanent cache. Fix everything that breaks using the permanent cache (should mimic the previous behaviour). * Add documentation. * Convert the cache trait into immutable (aka. move the lock into them). * Remove external cache lock (no longer needed). * Fix stuff. * Add LRU cache example. * Run `cargo fmt`. * Fix LRU example. * Fix the other example. * Fix after merge. * Add private cache drain method. * Temporarily disable `max_fee` checks. * Add comment on `RefCell::get_mut()`. * Remove `extend` from trait. Remove unused code. * Fix duplicated `CachedState` issue. * Add missing comments. * Update `README.md`. * Fix after merge. * Remove obsolete comment. * Fix test after merge. * Fix borrows. * Fix after merge. * Fix erc20 test. * Remove unused feature. * Update `coverage-helper` to support `#[coverage(off)]`. * Add `coverage` attribute feature on testing. * Update `README.md` and example. * Fix `README.md`. * Improve `README.md`. * Remove references to `StarknetState` in `README.md`. * Remove debug print. * Remove commented block of code. * Fix after merging. * Fix formatting. * Update Pr: Make contract caches shared (#1071) * Remove `serde_json_pythonic`. (#1047) * Remove `serde_json_pythonic`. * Fix JSON formatter on `deprecated_contract_class.rs`. * Fix hash JSON formatter (non-ascii support). * Add unwrap reasoning comment. * Add debug logging. (#1018) * Add `tracing` and update dependencies. * Configure the example to use tracing logging (and make it work again). * Add tracing logging. * Add error logging. * Fix error logging. * Reduce the amount of spam logged. * Update `README.md`. * Fix `Makefile` dependencies. * Remove `Debug` trait dependency. * Update `Cargo.lock` after merge. * Fix warnings. * Fix formatting. --------- Co-authored-by: Esteve Soler Arderiu <esteve.soler@lambdaclass.com> * fmt and improvements * Fix skip validate (#1053) * update version * fix skip validation for invoke txs * run fmt * fix clippy suggestion * simplify a bit the execute_tx function variants * Add documentation to transaction/fee module (#889) * added comments to src/transaction/fee.rs * added return and error comments --------- Co-authored-by: fannyguthmann <fanny.guthmann@post.idc.ac.il> Co-authored-by: Santiago Pittella <87827390+SantiagoPittella@users.noreply.github.com> Co-authored-by: Juan Bono <juanbono94@gmail.com> * Add comments to origin/Document-transactionl1_handler module (#888) * added comments to origin/Document-transactionl1_handler-module * modified comments * Test multi syscall (#687) * create multy syscall * remove the replace syscall, it failed because the contract adress didn't match * added library call_syscall * wip * wip * wip * wip * work in progress * remove .sjon files from starknet_programs * finished implemented all the syscalls * reorder code, create one call to syscall * fix pull bug * Update tests/multi_syscall_test.rs Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> * Update starknet_programs/cairo1/multi_syscall_test.cairo Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> * Update starknet_programs/cairo1/contract_a.cairo Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> * Update tests/multi_syscall_test.rs Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> * added test syscall for deploy * make format changes * corrected make clippy error * get_caller_address and get_contract_address return a adress * failed of get_contract_address * failed of get_contract_address * wip * modify the selector entrypoint_selector to be function specific * wip * wip * wip * add input to cairo functions * coorect format problem * wip * wip * wip * remove format problem * Fix sierra class hash calculation (#886) * reproduce bug * use pythonic formatter * rename test * fix test * cargo fmt * Fail with an Err transactions whose calculated fee exceed `max_fee` (#892) * Make tx fail when actual_fee exceeds max_fee * Changed test * Formatting * Fix logic * Leave fail only without charging * Change test * Fix test broken by better fee calc * Fixed test fee * Update fee on test_deploy_account * Remove comment --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * Fix test_get_nonce_at (#910) * Fix test_get_nonce_at * Rely on another contract * fix get_sorted_events bug (#912) * fix get_sorted_events bug * fmt * fix clippy --------- Co-authored-by: Estéfano Bargas <estefano.bargas@fing.edu.uy> * Added documentations to syscalls/deprecated_syscall_handler module (#883) * added comments to file syscalls/deprecated_syscall_handler-module' * Update src/syscalls/deprecated_syscall_handler.rs Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> * Update src/syscalls/deprecated_syscall_handler.rs Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> --------- Co-authored-by: fannyguthmann <fanny.guthmann@post.idc.ac.il> Co-authored-by: Juan Bono <juanbono94@gmail.com> Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> * wip * Modify the tests * fixed clippy errors --------- Co-authored-by: fannyguthmann <fanny.guthmann@post.idc.ac.il> Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> Co-authored-by: SantiagoPittella <pittellasantiago@gmail.com> Co-authored-by: Juan Bono <juanbono94@gmail.com> Co-authored-by: Estéfano Bargas <estefano.bargas@fing.edu.uy> * Parse internal calls (#915) * Added comments to core/contract_address module (#900) Co-authored-by: fannyguthmann <fanny.guthmann@post.idc.ac.il> * Add more transaction tests and fee investigation (#914) * add function for getting tx and refactor tests * improve imports * separe tests into 2 groups * fix test * add comments * format * cargo clippy * add details to every test * add fee discrepancy to test doc * cargo fmt * improve imports * added safety element --------- Co-authored-by: fannyguthmann <fanny.guthmann@post.idc.ac.il> Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> Co-authored-by: SantiagoPittella <pittellasantiago@gmail.com> Co-authored-by: Juan Bono <juanbono94@gmail.com> Co-authored-by: Estéfano Bargas <estefano.bargas@fing.edu.uy> Co-authored-by: Santiago Pittella <87827390+SantiagoPittella@users.noreply.github.com> * remove transactionalstatereader as it is not needed as-is (#1054) * Fix test (or try to) * Revert "Fix test (or try to)" This reverts commit 423052f. * Implement Clone for CachedState * Fix conflict in Makefile + run clippy-fix * Remove empty line * fmt * Remove unwraps * Add clone_for_testing * Remove clones * Add cairo native (#943) * Added cairo native flag * Added cairo_native as dependency * Partial progress * Progress * Partial progress * Point to newly created branch on cairo native * Use updated version of cairo native and make test pass * Run test with storage_write and storage_read with cairo native * Tidy up code * Start unhardcoding stuff * Handle return values * Tidy up code a little * Added sierra programs cache * Add impl for emit_event and send_l1_message * Fix tests * Install LLVM on CI * Test * Test CI * Fix lint step * Save work in progress * Unhardcode calldata and entry point * Unhardcode more stuff * Fix test * Add basic implementation for call_contract * Add call to get_name to integration test * Make call_contract impl compile * Fix test * Pass the correct amount of builtins for every function * Improve test legibility * Write call_contract test skeleton * Finish writing test, still bugs to fix * implement get_execution_info, make increase_allowance work * More debugging * Basic test for call_contract working * More progress on testing ERC20 * More progress on test * Add caller and callee contracts * Fix call_contract test after merge * Fix callee address in tests * Polish some details * Remove use_cairo_native from TransactionExecutionContext * Write test skeleton * Add test contracts * Fix tests * Readd deleted contracts * Echo contract test passing * Update cairo compiler to version 2.2.0 * Calling another contract with events test is passing * Fix failing tests * Remove internal_calls field TODO in CallInfo returned by native_execute * Add event_emitter contract * Add cairo vm execution to the erc20 test for comparison * Add simple implementation for panics in native execution * Add some documentation in code * Assert equality between events, accessed_storage_keys and l1_l2 messages between native and vm runs * Add event_emitter contract * Remove print * Remove comments * Move native syscall handler to its own file * Add felt decode to string when program panics * Make cairo native an optional dependency behind a feature * Move execution result to cairo native * Add a README section explaining how to setup cairo native * Fix some clippy issues * Fix test compilation * CI test * Revert "CI test" This reverts commit 4631e5e. * CI test * Test * Test * Address comment about multiple cfgs * Remove unnecessary clone * Test * Test * Switch to special workflow for native integration tests * Fix workflow * Fix stuff after merge. * Fix clippy warnings. * Fix after merge. * Fix comments. * Fix `Makefile`. * Remove unused import. * Use transactional state. * update cairo native to llvm 17 and remove nightly requirement * update ci * upd ci * try to fix ci * use ubuntu on native * try to fix ci * not needed? * fix ci * update cairo native * fix nightly usage * try to fix ci * dont need a transactional state reader, simply clone the state reader * try to fix ci * format * fix again * fix if * values * force rebuild * make cache track cairo 2 version to trigger rebuilds * try no restore key * update readme * fix tests on ci * update cairo native commit * fix more tests on ci * fix cairo native interface * try ci without cache * setup rustup home * make param passing to cairo native not obscure * try * try again * remove large packages * remove large dirs * remove android too * polly is needed * needs sudo * fix cov * fix test for now * format * cleanup ci file * use pyenv if available * nightly not needed in readme * add .sierra as generated to gitattributes * fix gitattributes * add casm too --------- Co-authored-by: Javier Chatruc <jrchatruc@gmail.com> Co-authored-by: Mariano Nicolini <mariano.nicolini.91@gmail.com> Co-authored-by: Esteve Soler Arderiu <esteve.soler@fratilia.com> Co-authored-by: Edgar Luque <git@edgarluque.com> * add test to check cairo 2 account contract deploy panic failing properly (#1045) * add test for account contract execution with panic * clippy * update cairo native to latest revision, u128 gas, mut self (#1082) Co-authored-by: Juan Bono <juanbono94@gmail.com> * Fix `get_execution_info` syscall (#1081) * Mark read-only segments * Move call * Remove debug prints * Remove fn * Add test case * Add comment * Restore newlines * Add function comment * Fix test values * Undo changes to makefile * Undo changes to makefile * clippy * cairo-native: implement testing syscalls (#1084) * cleanup erc20 test (#1087) * Fix `get_onchain_data_segment_length` (#1085) * Fix get_onchain_data_segment_length * Update test values * Update test values * fmt --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * Check that running a declare v1 yields a higher fee than simulating it without validation (#1076) * Reorder DeployAccount::apply * Revert "Reorder DeployAccount::apply" This reverts commit 11b0c39. * Add test * Add test * clippy + fmt --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * implement display and debug trait for Address (#1080) * implement display and debug trait for Address * hexa fmt * Add recursive calls tests using `library_call` & `call_contract` syscalls (#1072) * Add recursive library call test * Add test programs * Change base changes * Add recursive test for call_contract * fmt + clippy * Add test for 100 contract calls * clippy + fmt * Update test values --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * Fix/Refactor State::count actual storage changes + Support `DeployAccount` in the RpcStateReader (#1096) * Fix get_onchain_data_segment_length * Update test values * Update test values * fmt * Fix/Refactor `State::count actual storage changes` (#1086) * Add StorageChangesCount struct * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add the ability to execute `DeployAccount` transactions using the `RpcState` (#1089) * Add test case * Fix get_onchain_data_segment_length * Debug setup * Add StorageChangesCount struct * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add note comment * Remove hardcoded contract address * Remove txt files * Remove dbg prints * Remove dbg prints * Format * Restore blockifier version * Restore tests * Restore newlines * Restore newlines * Remove txt file * fmt * Fix bug in `From<StarknetRsContractClass> for CompiledClass` implementation (#1090) * Fix bug in CompiledClass * Add tests --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * fmt * Add test cases for `DeployAccount` with popular account contracts using RpcState (#1104) * Execute `Declare` transactions using the `RpcState` + Various fixes related to `Declare` txs (#1094) * Add test case * Fix get_onchain_data_segment_length * Debug setup * Fix get_onchain_data_segment_length * Add StorageChangesCount struct * Update test values * Update test values * fmt * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add note comment * Remove hardcoded contract address * Remove txt files * Remove dbg prints * Remove dbg prints * Format * Restore blockifier version * Restore tests * Restore newlines * Restore newlines * Remove txt file * fmt * Add test case with declare * Deserialize Declare transactions * Create blockifier Declare transaction * Fix/Refactor `State::count actual storage changes` (#1086) * Add StorageChangesCount struct * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add the ability to execute `DeployAccount` transactions using the `RpcState` (#1089) * Add test case * Fix get_onchain_data_segment_length * Debug setup * Add StorageChangesCount struct * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add note comment * Remove hardcoded contract address * Remove txt files * Remove dbg prints * Remove dbg prints * Format * Restore blockifier version * Restore tests * Restore newlines * Restore newlines * Remove txt file * fmt * Fix bug in `From<StarknetRsContractClass> for CompiledClass` implementation (#1090) * Fix bug in CompiledClass * Add tests * fetch class hash from the next block in declare tx * Return an error if a class_hash is not declared + add tests for declare tx * Fix error msg * Add support for DeclareV0-1 in sir_tests * Make Sierra class optional in declare v2 + other changes * Add support for DeclareV2 * Uncomment test * fix * Use new_with_sierra_class_hash_and_tx_hash * use CompiledClassHash instead of CompiledClass where applicatble * Handle nonce in declare v2 + run fmt * Set casm class before counting state changes in declare v2 * Changes * Make sierra class hash non-optional * fix + clippy * Use state_reader instead of creating a state to fetch the next block s contract classes * Add removed test * Update test values --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * Execute `L1Handler` transactions using the `RpcState` (#1103) * Add test case * Fix get_onchain_data_segment_length * Debug setup * Fix get_onchain_data_segment_length * Add StorageChangesCount struct * Update test values * Update test values * fmt * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add note comment * Remove hardcoded contract address * Remove txt files * Remove dbg prints * Remove dbg prints * Format * Restore blockifier version * Restore tests * Restore newlines * Restore newlines * Remove txt file * fmt * Add test case with declare * Deserialize Declare transactions * Create blockifier Declare transaction * Fix/Refactor `State::count actual storage changes` (#1086) * Add StorageChangesCount struct * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add the ability to execute `DeployAccount` transactions using the `RpcState` (#1089) * Add test case * Fix get_onchain_data_segment_length * Debug setup * Add StorageChangesCount struct * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add note comment * Remove hardcoded contract address * Remove txt files * Remove dbg prints * Remove dbg prints * Format * Restore blockifier version * Restore tests * Restore newlines * Restore newlines * Remove txt file * fmt * Fix bug in `From<StarknetRsContractClass> for CompiledClass` implementation (#1090) * Fix bug in CompiledClass * Add tests * fetch class hash from the next block in declare tx * Return an error if a class_hash is not declared + add tests for declare tx * Fix error msg * Add support for DeclareV0-1 in sir_tests * Make Sierra class optional in declare v2 + other changes * Add support for DeclareV2 * Uncomment test * fix * Use new_with_sierra_class_hash_and_tx_hash * use CompiledClassHash instead of CompiledClass where applicatble * Handle nonce in declare v2 + run fmt * Set casm class before counting state changes in declare v2 * Changes * Make sierra class hash non-optional * fix + clippy * Use state_reader instead of creating a state to fetch the next block s contract classes * Add removed test * Update test values * Make validate_invocation and fee_transfer_info fields optional + add L1_HANDLER transaction RpcState * Add L1Handler to blockifier_tests::execute_tx * Add blockifier test case * Add L1Handler to sir_tests::execute_tx * Add one more test case * fmt --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * Added a usage target to makefile (#1069) * Added a usage target to makefile * Fixed typo * Implement `NativeSyscallHandler::deploy` (#1106) * wip * Minor improvements + add test * Improve make clippy * Clippy + fmt * Update error messages * Add failure flag test * Fix typo * Apply suggestions + run formatter * Add llvm setup so we can run clippy with `cairo-native` feature * Fix * Remove todo * Use a proper class_hash * Fix test assertion --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * update cairo native to use gas consumed (#1102) * update cairo native to use gas consumed * gas consumed * update native rev * fix gas consumed * remove comments * fixes --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * Fix cairo-native feature-gated code * clippy --------- Co-authored-by: MrAzteca <azteca1998@users.noreply.github.com> Co-authored-by: Esteve Soler Arderiu <esteve.soler@lambdaclass.com> Co-authored-by: juanbono <juanbono94@gmail.com> Co-authored-by: Fanny Guthmann <57538139+fguthmann@users.noreply.github.com> Co-authored-by: fannyguthmann <fanny.guthmann@post.idc.ac.il> Co-authored-by: Santiago Pittella <87827390+SantiagoPittella@users.noreply.github.com> Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> Co-authored-by: SantiagoPittella <pittellasantiago@gmail.com> Co-authored-by: Estéfano Bargas <estefano.bargas@fing.edu.uy> Co-authored-by: Edgar <git@edgarluque.com> Co-authored-by: ElFantasma <esteban.dimitroff@lambdaclass.com> Co-authored-by: Javier Chatruc <jrchatruc@gmail.com> Co-authored-by: Mariano Nicolini <mariano.nicolini.91@gmail.com> Co-authored-by: Esteve Soler Arderiu <esteve.soler@fratilia.com> Co-authored-by: Antonio Calvín García <antonio.calvin@lambdaclass.com> Co-authored-by: Iñaki Garay <igarai@gmail.com> * Revert "Update Pr: Make contract caches shared (#1071)" (#1116) This reverts commit 8a11259. * Update contract caches (#1117) * Remove `serde_json_pythonic`. (#1047) * Remove `serde_json_pythonic`. * Fix JSON formatter on `deprecated_contract_class.rs`. * Fix hash JSON formatter (non-ascii support). * Add unwrap reasoning comment. * Add debug logging. (#1018) * Add `tracing` and update dependencies. * Configure the example to use tracing logging (and make it work again). * Add tracing logging. * Add error logging. * Fix error logging. * Reduce the amount of spam logged. * Update `README.md`. * Fix `Makefile` dependencies. * Remove `Debug` trait dependency. * Update `Cargo.lock` after merge. * Fix warnings. * Fix formatting. --------- Co-authored-by: Esteve Soler Arderiu <esteve.soler@lambdaclass.com> * fmt and improvements * Fix skip validate (#1053) * update version * fix skip validation for invoke txs * run fmt * fix clippy suggestion * simplify a bit the execute_tx function variants * Add documentation to transaction/fee module (#889) * added comments to src/transaction/fee.rs * added return and error comments --------- Co-authored-by: fannyguthmann <fanny.guthmann@post.idc.ac.il> Co-authored-by: Santiago Pittella <87827390+SantiagoPittella@users.noreply.github.com> Co-authored-by: Juan Bono <juanbono94@gmail.com> * Add comments to origin/Document-transactionl1_handler module (#888) * added comments to origin/Document-transactionl1_handler-module * modified comments * Test multi syscall (#687) * create multy syscall * remove the replace syscall, it failed because the contract adress didn't match * added library call_syscall * wip * wip * wip * wip * work in progress * remove .sjon files from starknet_programs * finished implemented all the syscalls * reorder code, create one call to syscall * fix pull bug * Update tests/multi_syscall_test.rs Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> * Update starknet_programs/cairo1/multi_syscall_test.cairo Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> * Update starknet_programs/cairo1/contract_a.cairo Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> * Update tests/multi_syscall_test.rs Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> * added test syscall for deploy * make format changes * corrected make clippy error * get_caller_address and get_contract_address return a adress * failed of get_contract_address * failed of get_contract_address * wip * modify the selector entrypoint_selector to be function specific * wip * wip * wip * add input to cairo functions * coorect format problem * wip * wip * wip * remove format problem * Fix sierra class hash calculation (#886) * reproduce bug * use pythonic formatter * rename test * fix test * cargo fmt * Fail with an Err transactions whose calculated fee exceed `max_fee` (#892) * Make tx fail when actual_fee exceeds max_fee * Changed test * Formatting * Fix logic * Leave fail only without charging * Change test * Fix test broken by better fee calc * Fixed test fee * Update fee on test_deploy_account * Remove comment --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * Fix test_get_nonce_at (#910) * Fix test_get_nonce_at * Rely on another contract * fix get_sorted_events bug (#912) * fix get_sorted_events bug * fmt * fix clippy --------- Co-authored-by: Estéfano Bargas <estefano.bargas@fing.edu.uy> * Added documentations to syscalls/deprecated_syscall_handler module (#883) * added comments to file syscalls/deprecated_syscall_handler-module' * Update src/syscalls/deprecated_syscall_handler.rs Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> * Update src/syscalls/deprecated_syscall_handler.rs Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> --------- Co-authored-by: fannyguthmann <fanny.guthmann@post.idc.ac.il> Co-authored-by: Juan Bono <juanbono94@gmail.com> Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> * wip * Modify the tests * fixed clippy errors --------- Co-authored-by: fannyguthmann <fanny.guthmann@post.idc.ac.il> Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> Co-authored-by: SantiagoPittella <pittellasantiago@gmail.com> Co-authored-by: Juan Bono <juanbono94@gmail.com> Co-authored-by: Estéfano Bargas <estefano.bargas@fing.edu.uy> * Parse internal calls (#915) * Added comments to core/contract_address module (#900) Co-authored-by: fannyguthmann <fanny.guthmann@post.idc.ac.il> * Add more transaction tests and fee investigation (#914) * add function for getting tx and refactor tests * improve imports * separe tests into 2 groups * fix test * add comments * format * cargo clippy * add details to every test * add fee discrepancy to test doc * cargo fmt * improve imports * added safety element --------- Co-authored-by: fannyguthmann <fanny.guthmann@post.idc.ac.il> Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> Co-authored-by: SantiagoPittella <pittellasantiago@gmail.com> Co-authored-by: Juan Bono <juanbono94@gmail.com> Co-authored-by: Estéfano Bargas <estefano.bargas@fing.edu.uy> Co-authored-by: Santiago Pittella <87827390+SantiagoPittella@users.noreply.github.com> * remove transactionalstatereader as it is not needed as-is (#1054) * Fix test (or try to) * Revert "Fix test (or try to)" This reverts commit 423052f. * Implement Clone for CachedState * Fix conflict in Makefile + run clippy-fix * Remove empty line * fmt * Remove unwraps * Add clone_for_testing * Remove clones * Add cairo native (#943) * Added cairo native flag * Added cairo_native as dependency * Partial progress * Progress * Partial progress * Point to newly created branch on cairo native * Use updated version of cairo native and make test pass * Run test with storage_write and storage_read with cairo native * Tidy up code * Start unhardcoding stuff * Handle return values * Tidy up code a little * Added sierra programs cache * Add impl for emit_event and send_l1_message * Fix tests * Install LLVM on CI * Test * Test CI * Fix lint step * Save work in progress * Unhardcode calldata and entry point * Unhardcode more stuff * Fix test * Add basic implementation for call_contract * Add call to get_name to integration test * Make call_contract impl compile * Fix test * Pass the correct amount of builtins for every function * Improve test legibility * Write call_contract test skeleton * Finish writing test, still bugs to fix * implement get_execution_info, make increase_allowance work * More debugging * Basic test for call_contract working * More progress on testing ERC20 * More progress on test * Add caller and callee contracts * Fix call_contract test after merge * Fix callee address in tests * Polish some details * Remove use_cairo_native from TransactionExecutionContext * Write test skeleton * Add test contracts * Fix tests * Readd deleted contracts * Echo contract test passing * Update cairo compiler to version 2.2.0 * Calling another contract with events test is passing * Fix failing tests * Remove internal_calls field TODO in CallInfo returned by native_execute * Add event_emitter contract * Add cairo vm execution to the erc20 test for comparison * Add simple implementation for panics in native execution * Add some documentation in code * Assert equality between events, accessed_storage_keys and l1_l2 messages between native and vm runs * Add event_emitter contract * Remove print * Remove comments * Move native syscall handler to its own file * Add felt decode to string when program panics * Make cairo native an optional dependency behind a feature * Move execution result to cairo native * Add a README section explaining how to setup cairo native * Fix some clippy issues * Fix test compilation * CI test * Revert "CI test" This reverts commit 4631e5e. * CI test * Test * Test * Address comment about multiple cfgs * Remove unnecessary clone * Test * Test * Switch to special workflow for native integration tests * Fix workflow * Fix stuff after merge. * Fix clippy warnings. * Fix after merge. * Fix comments. * Fix `Makefile`. * Remove unused import. * Use transactional state. * update cairo native to llvm 17 and remove nightly requirement * update ci * upd ci * try to fix ci * use ubuntu on native * try to fix ci * not needed? * fix ci * update cairo native * fix nightly usage * try to fix ci * dont need a transactional state reader, simply clone the state reader * try to fix ci * format * fix again * fix if * values * force rebuild * make cache track cairo 2 version to trigger rebuilds * try no restore key * update readme * fix tests on ci * update cairo native commit * fix more tests on ci * fix cairo native interface * try ci without cache * setup rustup home * make param passing to cairo native not obscure * try * try again * remove large packages * remove large dirs * remove android too * polly is needed * needs sudo * fix cov * fix test for now * format * cleanup ci file * use pyenv if available * nightly not needed in readme * add .sierra as generated to gitattributes * fix gitattributes * add casm too --------- Co-authored-by: Javier Chatruc <jrchatruc@gmail.com> Co-authored-by: Mariano Nicolini <mariano.nicolini.91@gmail.com> Co-authored-by: Esteve Soler Arderiu <esteve.soler@fratilia.com> Co-authored-by: Edgar Luque <git@edgarluque.com> * add test to check cairo 2 account contract deploy panic failing properly (#1045) * add test for account contract execution with panic * clippy * update cairo native to latest revision, u128 gas, mut self (#1082) Co-authored-by: Juan Bono <juanbono94@gmail.com> * Fix `get_execution_info` syscall (#1081) * Mark read-only segments * Move call * Remove debug prints * Remove fn * Add test case * Add comment * Restore newlines * Add function comment * Fix test values * Undo changes to makefile * Undo changes to makefile * clippy * cairo-native: implement testing syscalls (#1084) * cleanup erc20 test (#1087) * Fix `get_onchain_data_segment_length` (#1085) * Fix get_onchain_data_segment_length * Update test values * Update test values * fmt --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * Check that running a declare v1 yields a higher fee than simulating it without validation (#1076) * Reorder DeployAccount::apply * Revert "Reorder DeployAccount::apply" This reverts commit 11b0c39. * Add test * Add test * clippy + fmt --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * implement display and debug trait for Address (#1080) * implement display and debug trait for Address * hexa fmt * Add recursive calls tests using `library_call` & `call_contract` syscalls (#1072) * Add recursive library call test * Add test programs * Change base changes * Add recursive test for call_contract * fmt + clippy * Add test for 100 contract calls * clippy + fmt * Update test values --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * Fix/Refactor State::count actual storage changes + Support `DeployAccount` in the RpcStateReader (#1096) * Fix get_onchain_data_segment_length * Update test values * Update test values * fmt * Fix/Refactor `State::count actual storage changes` (#1086) * Add StorageChangesCount struct * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add the ability to execute `DeployAccount` transactions using the `RpcState` (#1089) * Add test case * Fix get_onchain_data_segment_length * Debug setup * Add StorageChangesCount struct * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add note comment * Remove hardcoded contract address * Remove txt files * Remove dbg prints * Remove dbg prints * Format * Restore blockifier version * Restore tests * Restore newlines * Restore newlines * Remove txt file * fmt * Fix bug in `From<StarknetRsContractClass> for CompiledClass` implementation (#1090) * Fix bug in CompiledClass * Add tests --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * fmt * Add test cases for `DeployAccount` with popular account contracts using RpcState (#1104) * Execute `Declare` transactions using the `RpcState` + Various fixes related to `Declare` txs (#1094) * Add test case * Fix get_onchain_data_segment_length * Debug setup * Fix get_onchain_data_segment_length * Add StorageChangesCount struct * Update test values * Update test values * fmt * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add note comment * Remove hardcoded contract address * Remove txt files * Remove dbg prints * Remove dbg prints * Format * Restore blockifier version * Restore tests * Restore newlines * Restore newlines * Remove txt file * fmt * Add test case with declare * Deserialize Declare transactions * Create blockifier Declare transaction * Fix/Refactor `State::count actual storage changes` (#1086) * Add StorageChangesCount struct * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add the ability to execute `DeployAccount` transactions using the `RpcState` (#1089) * Add test case * Fix get_onchain_data_segment_length * Debug setup * Add StorageChangesCount struct * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add note comment * Remove hardcoded contract address * Remove txt files * Remove dbg prints * Remove dbg prints * Format * Restore blockifier version * Restore tests * Restore newlines * Restore newlines * Remove txt file * fmt * Fix bug in `From<StarknetRsContractClass> for CompiledClass` implementation (#1090) * Fix bug in CompiledClass * Add tests * fetch class hash from the next block in declare tx * Return an error if a class_hash is not declared + add tests for declare tx * Fix error msg * Add support for DeclareV0-1 in sir_tests * Make Sierra class optional in declare v2 + other changes * Add support for DeclareV2 * Uncomment test * fix * Use new_with_sierra_class_hash_and_tx_hash * use CompiledClassHash instead of CompiledClass where applicatble * Handle nonce in declare v2 + run fmt * Set casm class before counting state changes in declare v2 * Changes * Make sierra class hash non-optional * fix + clippy * Use state_reader instead of creating a state to fetch the next block s contract classes * Add removed test * Update test values --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * Execute `L1Handler` transactions using the `RpcState` (#1103) * Add test case * Fix get_onchain_data_segment_length * Debug setup * Fix get_onchain_data_segment_length * Add StorageChangesCount struct * Update test values * Update test values * fmt * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add note comment * Remove hardcoded contract address * Remove txt files * Remove dbg prints * Remove dbg prints * Format * Restore blockifier version * Restore tests * Restore newlines * Restore newlines * Remove txt file * fmt * Add test case with declare * Deserialize Declare transactions * Create blockifier Declare transaction * Fix/Refactor `State::count actual storage changes` (#1086) * Add StorageChangesCount struct * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add the ability to execute `DeployAccount` transactions using the `RpcState` (#1089) * Add test case * Fix get_onchain_data_segment_length * Debug setup * Add StorageChangesCount struct * Use StorageChangesCount struct in state method * Fix implicated code * Update doc * Update test values * Rename method for consistency * Add note comment * Remove hardcoded contract address * Remove txt files * Remove dbg prints * Remove dbg prints * Format * Restore blockifier version * Restore tests * Restore newlines * Restore newlines * Remove txt file * fmt * Fix bug in `From<StarknetRsContractClass> for CompiledClass` implementation (#1090) * Fix bug in CompiledClass * Add tests * fetch class hash from the next block in declare tx * Return an error if a class_hash is not declared + add tests for declare tx * Fix error msg * Add support for DeclareV0-1 in sir_tests * Make Sierra class optional in declare v2 + other changes * Add support for DeclareV2 * Uncomment test * fix * Use new_with_sierra_class_hash_and_tx_hash * use CompiledClassHash instead of CompiledClass where applicatble * Handle nonce in declare v2 + run fmt * Set casm class before counting state changes in declare v2 * Changes * Make sierra class hash non-optional * fix + clippy * Use state_reader instead of creating a state to fetch the next block s contract classes * Add removed test * Update test values * Make validate_invocation and fee_transfer_info fields optional + add L1_HANDLER transaction RpcState * Add L1Handler to blockifier_tests::execute_tx * Add blockifier test case * Add L1Handler to sir_tests::execute_tx * Add one more test case * fmt --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * Added a usage target to makefile (#1069) * Added a usage target to makefile * Fixed typo * Implement `NativeSyscallHandler::deploy` (#1106) * wip * Minor improvements + add test * Improve make clippy * Clippy + fmt * Update error messages * Add failure flag test * Fix typo * Apply suggestions + run formatter * Add llvm setup so we can run clippy with `cairo-native` feature * Fix * Remove todo * Use a proper class_hash * Fix test assertion --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * update cairo native to use gas consumed (#1102) * update cairo native to use gas consumed * gas consumed * update native rev * fix gas consumed * remove comments * fixes --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> * Fix cairo-native feature-gated code * clippy --------- Co-authored-by: MrAzteca <azteca1998@users.noreply.github.com> Co-authored-by: Esteve Soler Arderiu <esteve.soler@lambdaclass.com> Co-authored-by: juanbono <juanbono94@gmail.com> Co-authored-by: Fanny Guthmann <57538139+fguthmann@users.noreply.github.com> Co-authored-by: fannyguthmann <fanny.guthmann@post.idc.ac.il> Co-authored-by: Santiago Pittella <87827390+SantiagoPittella@users.noreply.github.com> Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> Co-authored-by: SantiagoPittella <pittellasantiago@gmail.com> Co-authored-by: Estéfano Bargas <estefano.bargas@fing.edu.uy> Co-authored-by: Edgar <git@edgarluque.com> Co-authored-by: Federica <fedemoletta@hotmail.com> Co-authored-by: ElFantasma <esteban.dimitroff@lambdaclass.com> Co-authored-by: Javier Chatruc <jrchatruc@gmail.com> Co-authored-by: Mariano Nicolini <mariano.nicolini.91@gmail.com> Co-authored-by: Esteve Soler Arderiu <esteve.soler@fratilia.com> Co-authored-by: fmoletta <99273364+fmoletta@users.noreply.github.com> Co-authored-by: Iñaki Garay <igarai@gmail.com> * Merge * Fix code * clippy * Remove old file * Remove old file * Avoid cloning private cache * Fix locks * Uncomment metrics --------- Co-authored-by: Mario Rugiero <mrugiero@gmail.com> Co-authored-by: fmoletta <99273364+fmoletta@users.noreply.github.com> Co-authored-by: juanbono <juanbono94@gmail.com> Co-authored-by: Fanny Guthmann <57538139+fguthmann@users.noreply.github.com> Co-authored-by: fannyguthmann <fanny.guthmann@post.idc.ac.il> Co-authored-by: Santiago Pittella <87827390+SantiagoPittella@users.noreply.github.com> Co-authored-by: Matías Ignacio González <maigonzalez@fi.uba.ar> Co-authored-by: SantiagoPittella <pittellasantiago@gmail.com> Co-authored-by: Estéfano Bargas <estefano.bargas@fing.edu.uy> Co-authored-by: Edgar <git@edgarluque.com> Co-authored-by: ElFantasma <esteban.dimitroff@lambdaclass.com> Co-authored-by: Javier Chatruc <jrchatruc@gmail.com> Co-authored-by: Mariano Nicolini <mariano.nicolini.91@gmail.com> Co-authored-by: Antonio Calvín García <antonio.calvin@lambdaclass.com> Co-authored-by: Iñaki Garay <igarai@gmail.com> Co-authored-by: Federica <fedemoletta@hotmail.com>
1 parent f4dfa02 commit 9154239

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1915
-1110
lines changed

Cargo.lock

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ tracing = "0.1.37"
6363
[dev-dependencies]
6464
assert_matches = "1.5.0"
6565
coverage-helper = "0.2.0"
66+
lru = "0.11.0"
6667
pretty_assertions_sorted = "1.2.3"
6768
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
6869

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,54 @@ You can find an example on how to use the CLI [here](/docs/CLI_USAGE_EXAMPLE.md)
117117

118118
### Customization
119119

120+
#### Contract class cache behavior
121+
122+
`starknet_in_rust` supports caching contracts in memory. Caching the contracts is useful for
123+
avoiding excessive RPC API usage and keeping the contract class deserialization overhead to the
124+
minimum. The project provides two builtin cache policies: null and permanent. The null cache behaves
125+
as if there was no cache at all. The permanent cache caches everything in memory forever.
126+
127+
In addition to those two, an example is provided that implements and uses an LRU cache policy.
128+
Long-running applications should ideally implement a cache algorithm suited to their needs or
129+
alternatively use our example's implementation to avoid spamming the API when using the null cache
130+
or blowing the memory usage when running with the permanent cache.
131+
132+
Customized cache policies may be used by implementing the `ContractClassCache` trait. Check out our
133+
[LRU cache example](examples/lru_cache/main.rs) for more details. Updating the cache requires
134+
manually merging the local state cache into the shared cache manually. This can be done by calling
135+
the `drain_private_contract_class_cache` on the `CachedState` instance.
136+
137+
```rs
138+
// To use the null cache (aka. no cache at all), create the state as follows:
139+
let cache = Arc::new(NullContractClassCache::default());
140+
let state1 = CachedState::new(state_reader.clone(), cache.clone());
141+
let state2 = CachedState::new(state_reader.clone(), cache.clone()); // Cache is reused.
142+
143+
// Insert state usage here.
144+
145+
// The null cache doesn't have any method to extend it since it has no data.
146+
```
147+
148+
```rs
149+
// If the permanent cache is preferred, then use `PermanentContractClassCache` instead:
150+
let cache = Arc::new(PermanentContractClassCache::default());
151+
let state1 = CachedState::new(state_reader.clone(), cache.clone());
152+
let state2 = CachedState::new(state_reader.clone(), cache.clone()); // Cache is reused.
153+
154+
// Insert state usage here.
155+
156+
// Extend the shared cache with the states' contracts after using them.
157+
cache.extend(state1.state.drain_private_contract_class_cache());
158+
cache.extend(state2.state.drain_private_contract_class_cache());
159+
```
160+
161+
#### Logging configuration
162+
163+
This project uses the [`tracing`](https://crates.io/crates/tracing) crate as a library. Check out
164+
its documentation for more information.
165+
166+
### Testing
167+
120168
#### Logging configuration
121169

122170
This project uses the [`tracing`](https://crates.io/crates/tracing) crate as a library. Check out

bench/internals.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ use starknet_in_rust::{
1515
services::api::contract_classes::{
1616
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
1717
},
18-
state::in_memory_state_reader::InMemoryStateReader,
1918
state::{cached_state::CachedState, state_api::State},
19+
state::{
20+
contract_class_cache::PermanentContractClassCache,
21+
in_memory_state_reader::InMemoryStateReader,
22+
},
2023
transaction::{declare::Declare, Deploy, DeployAccount, InvokeFunction},
2124
utils::{Address, ClassHash},
2225
};
23-
use std::{collections::HashMap, hint::black_box, sync::Arc};
26+
use std::{hint::black_box, sync::Arc};
2427

2528
#[cfg(feature = "cairo-native")]
2629
use std::{cell::RefCell, rc::Rc};
@@ -74,7 +77,10 @@ fn deploy_account(
7477
const RUNS: usize = 500;
7578

7679
let state_reader = Arc::new(InMemoryStateReader::default());
77-
let mut state = CachedState::new(state_reader, HashMap::new());
80+
let mut state = CachedState::new(
81+
state_reader,
82+
Arc::new(PermanentContractClassCache::default()),
83+
);
7884

7985
state
8086
.set_contract_class(
@@ -86,7 +92,7 @@ fn deploy_account(
8692
let block_context = &Default::default();
8793

8894
for _ in 0..RUNS {
89-
let mut state_copy = state.clone();
95+
let mut state_copy = state.clone_for_testing();
9096
let class_hash = *CLASS_HASH;
9197
let signature = SIGNATURE.clone();
9298
scope(|| {
@@ -118,12 +124,15 @@ fn declare(#[cfg(feature = "cairo-native")] program_cache: Rc<RefCell<ProgramCac
118124
const RUNS: usize = 5;
119125

120126
let state_reader = Arc::new(InMemoryStateReader::default());
121-
let state = CachedState::new(state_reader, HashMap::new());
127+
let state = CachedState::new(
128+
state_reader,
129+
Arc::new(PermanentContractClassCache::default()),
130+
);
122131

123132
let block_context = &Default::default();
124133

125134
for _ in 0..RUNS {
126-
let mut cloned_state = state.clone();
135+
let mut cloned_state = state.clone_for_testing();
127136
let class = CONTRACT_CLASS.clone();
128137
let address = CONTRACT_ADDRESS.clone();
129138
scope(|| {
@@ -155,7 +164,10 @@ fn deploy(#[cfg(feature = "cairo-native")] program_cache: Rc<RefCell<ProgramCach
155164
const RUNS: usize = 8;
156165

157166
let state_reader = Arc::new(InMemoryStateReader::default());
158-
let mut state = CachedState::new(state_reader, HashMap::new());
167+
let mut state = CachedState::new(
168+
state_reader,
169+
Arc::new(PermanentContractClassCache::default()),
170+
);
159171

160172
state
161173
.set_contract_class(
@@ -167,7 +179,7 @@ fn deploy(#[cfg(feature = "cairo-native")] program_cache: Rc<RefCell<ProgramCach
167179
let block_context = &Default::default();
168180

169181
for _ in 0..RUNS {
170-
let mut state_copy = state.clone();
182+
let mut state_copy = state.clone_for_testing();
171183
let salt = felt_str!(
172184
"2669425616857739096022668060305620640217901643963991674344872184515580705509"
173185
);
@@ -198,7 +210,10 @@ fn invoke(#[cfg(feature = "cairo-native")] program_cache: Rc<RefCell<ProgramCach
198210
const RUNS: usize = 100;
199211

200212
let state_reader = Arc::new(InMemoryStateReader::default());
201-
let mut state = CachedState::new(state_reader, HashMap::new());
213+
let mut state = CachedState::new(
214+
state_reader,
215+
Arc::new(PermanentContractClassCache::default()),
216+
);
202217

203218
state
204219
.set_contract_class(
@@ -231,7 +246,7 @@ fn invoke(#[cfg(feature = "cairo-native")] program_cache: Rc<RefCell<ProgramCach
231246
.unwrap();
232247

233248
for _ in 0..RUNS {
234-
let mut state_copy = state.clone();
249+
let mut state_copy = state.clone_for_testing();
235250
let address = CONTRACT_ADDRESS.clone();
236251
let selector = VALIDATE_ENTRY_POINT_SELECTOR.clone();
237252
let signature = SIGNATURE.clone();

bench/native_bench.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use num_traits::Zero;
1616
use starknet_in_rust::definitions::block_context::BlockContext;
1717
use starknet_in_rust::definitions::block_context::StarknetChainId;
1818
use starknet_in_rust::services::api::contract_classes::compiled_class::CompiledClass;
19+
use starknet_in_rust::state::contract_class_cache::ContractClassCache;
20+
use starknet_in_rust::state::contract_class_cache::PermanentContractClassCache;
1921
use starknet_in_rust::state::state_api::State;
2022
use starknet_in_rust::transaction::DeployAccount;
2123
use starknet_in_rust::utils::calculate_sn_keccak;
@@ -32,7 +34,6 @@ use starknet_in_rust::{
3234
utils::{Address, ClassHash},
3335
};
3436
use std::cell::RefCell;
35-
use std::collections::HashMap;
3637
use std::rc::Rc;
3738
use std::sync::Arc;
3839

@@ -62,7 +63,7 @@ pub fn main() {
6263

6364
fn bench_fibo(executions: usize, native: bool) {
6465
// Create state reader with class hash data
65-
let mut contract_class_cache = HashMap::new();
66+
let contract_class_cache = PermanentContractClassCache::default();
6667
static CASM_CLASS_HASH: ClassHash = ClassHash([2; 32]);
6768

6869
let (contract_class, constructor_selector) = match native {
@@ -96,7 +97,7 @@ fn bench_fibo(executions: usize, native: bool) {
9697

9798
let caller_address = Address(123456789.into());
9899

99-
contract_class_cache.insert(CASM_CLASS_HASH, contract_class);
100+
contract_class_cache.set_contract_class(CASM_CLASS_HASH, contract_class);
100101
let mut state_reader = InMemoryStateReader::default();
101102
let nonce = Felt252::zero();
102103

@@ -109,7 +110,7 @@ fn bench_fibo(executions: usize, native: bool) {
109110

110111
// Create state from the state_reader and contract cache.
111112
let state_reader = Arc::new(state_reader);
112-
let state = CachedState::new(state_reader, contract_class_cache);
113+
let state = CachedState::new(state_reader, Arc::new(contract_class_cache));
113114

114115
/* f0, f1, N */
115116
let mut calldata = [1.into(), 1.into(), 2000000.into()];
@@ -120,7 +121,7 @@ fn bench_fibo(executions: usize, native: bool) {
120121
for _ in 0..executions {
121122
calldata[2] = &calldata[2] + 1usize;
122123
let result = execute(
123-
&mut state.clone(),
124+
&mut state.clone_for_testing(),
124125
&caller_address,
125126
&caller_address,
126127
&Felt252::new(constructor_selector.clone()),
@@ -136,7 +137,7 @@ fn bench_fibo(executions: usize, native: bool) {
136137

137138
fn bench_fact(executions: usize, native: bool) {
138139
// Create state reader with class hash data
139-
let mut contract_class_cache = HashMap::new();
140+
let contract_class_cache = PermanentContractClassCache::default();
140141
static CASM_CLASS_HASH: ClassHash = ClassHash([2; 32]);
141142

142143
let (contract_class, constructor_selector) = match native {
@@ -172,7 +173,7 @@ fn bench_fact(executions: usize, native: bool) {
172173
// FACT 1M
173174
// FIBO 2M
174175

175-
contract_class_cache.insert(CASM_CLASS_HASH, contract_class);
176+
contract_class_cache.set_contract_class(CASM_CLASS_HASH, contract_class);
176177
let mut state_reader = InMemoryStateReader::default();
177178
let nonce = Felt252::zero();
178179

@@ -185,7 +186,7 @@ fn bench_fact(executions: usize, native: bool) {
185186

186187
// Create state from the state_reader and contract cache.
187188
let state_reader = Arc::new(state_reader);
188-
let state = CachedState::new(state_reader, contract_class_cache);
189+
let state = CachedState::new(state_reader, Arc::new(contract_class_cache));
189190

190191
/* N */
191192
let mut calldata = [2000000.into()];
@@ -196,7 +197,7 @@ fn bench_fact(executions: usize, native: bool) {
196197
for _ in 0..executions {
197198
calldata[0] = &calldata[0] + 1usize;
198199
let result = execute(
199-
&mut state.clone(),
200+
&mut state.clone_for_testing(),
200201
&caller_address,
201202
&caller_address,
202203
&Felt252::new(constructor_selector.clone()),
@@ -213,7 +214,7 @@ fn bench_fact(executions: usize, native: bool) {
213214
fn bench_erc20(executions: usize, native: bool) {
214215
// 1. setup ERC20 contract and state.
215216
// Create state reader and preload the contract classes.
216-
let mut contract_class_cache = HashMap::new();
217+
let contract_class_cache = PermanentContractClassCache::default();
217218

218219
lazy_static! {
219220
static ref ERC20_CLASS_HASH: ClassHash = ClassHash::from(felt_str!("2"));
@@ -239,7 +240,10 @@ fn bench_erc20(executions: usize, native: bool) {
239240
}
240241

241242
let program_cache = Rc::new(RefCell::new(ProgramCache::new(get_native_context())));
242-
let (erc20_address, mut state): (Address, CachedState<InMemoryStateReader>) = match native {
243+
let (erc20_address, mut state): (
244+
Address,
245+
CachedState<InMemoryStateReader, PermanentContractClassCache>,
246+
) = match native {
243247
true => {
244248
let erc20_sierra_class = include_bytes!("../starknet_programs/cairo2/erc20.sierra");
245249
let sierra_contract_class: cairo_lang_starknet::contract_class::ContractClass =
@@ -259,11 +263,11 @@ fn bench_erc20(executions: usize, native: bool) {
259263
let deploy_entrypoint_selector = &entrypoints.external.get(0).unwrap().selector;
260264

261265
// insert deployer and erc20 classes into the cache.
262-
contract_class_cache.insert(
266+
contract_class_cache.set_contract_class(
263267
*DEPLOYER_CLASS_HASH,
264268
CompiledClass::Casm(Arc::new(erc20_deployer_class)),
265269
);
266-
contract_class_cache.insert(*ERC20_CLASS_HASH, erc20_contract_class);
270+
contract_class_cache.set_contract_class(*ERC20_CLASS_HASH, erc20_contract_class);
267271

268272
let mut state_reader = InMemoryStateReader::default();
269273
// setup deployer nonce and address into the state reader
@@ -275,7 +279,8 @@ fn bench_erc20(executions: usize, native: bool) {
275279
.insert(DEPLOYER_ADDRESS.clone(), Felt252::zero());
276280

277281
// Create state from the state_reader and contract cache.
278-
let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache);
282+
let mut state =
283+
CachedState::new(Arc::new(state_reader), Arc::new(contract_class_cache));
279284

280285
// deploy the erc20 contract by calling the deployer contract.
281286

@@ -338,11 +343,11 @@ fn bench_erc20(executions: usize, native: bool) {
338343
let deploy_entrypoint_selector = &entrypoints.external.get(0).unwrap().selector;
339344

340345
// insert deployer and erc20 classes into the cache.
341-
contract_class_cache.insert(
346+
contract_class_cache.set_contract_class(
342347
*DEPLOYER_CLASS_HASH,
343348
CompiledClass::Casm(Arc::new(erc20_deployer_class)),
344349
);
345-
contract_class_cache.insert(*ERC20_CLASS_HASH, erc20_contract_class);
350+
contract_class_cache.set_contract_class(*ERC20_CLASS_HASH, erc20_contract_class);
346351

347352
let mut state_reader = InMemoryStateReader::default();
348353
// setup deployer nonce and address into the state reader
@@ -354,7 +359,8 @@ fn bench_erc20(executions: usize, native: bool) {
354359
.insert(DEPLOYER_ADDRESS.clone(), Felt252::zero());
355360

356361
// Create state from the state_reader and contract cache.
357-
let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache);
362+
let mut state =
363+
CachedState::new(Arc::new(state_reader), Arc::new(contract_class_cache));
358364

359365
// deploy the erc20 contract by calling the deployer contract.
360366

@@ -493,7 +499,7 @@ fn bench_erc20(executions: usize, native: bool) {
493499

494500
for _ in 0..executions {
495501
let result = execute(
496-
&mut state.clone(),
502+
&mut state.clone_for_testing(),
497503
&account1_address,
498504
&erc20_address,
499505
&transfer_entrypoint_selector.clone(),
@@ -510,7 +516,7 @@ fn bench_erc20(executions: usize, native: bool) {
510516
#[inline(never)]
511517
#[allow(clippy::too_many_arguments)]
512518
fn execute(
513-
state: &mut CachedState<InMemoryStateReader>,
519+
state: &mut CachedState<InMemoryStateReader, PermanentContractClassCache>,
514520
caller_address: &Address,
515521
callee_address: &Address,
516522
selector: &Felt252,

0 commit comments

Comments
 (0)