From f897cb7f1429a26f7678e46ad820bf110335881e Mon Sep 17 00:00:00 2001 From: Edgar Date: Fri, 26 Jan 2024 14:57:13 -0300 Subject: [PATCH 01/15] progress --- Cargo.lock | 411 +++++------ Cargo.toml | 13 +- bench/internals.rs | 10 + bench/yas.rs | 26 + cli/src/main.rs | 4 + examples/contract_execution/src/main.rs | 8 + examples/lru_cache/main.rs | 6 + fuzzer/src/main.rs | 2 + rpc_state_reader/src/sir_state_reader.rs | 2 +- src/bin/deploy.rs | 2 + src/bin/deploy_invoke.rs | 6 + src/bin/fibonacci.rs | 2 + src/bin/invoke.rs | 4 + src/bin/invoke_with_cachedstate.rs | 4 + src/bin/native_bench.rs | 19 +- src/bin/native_executor.rs | 682 ++++++++++++++++++ src/execution/execution_entry_point.rs | 5 + src/lib.rs | 54 ++ src/sandboxing/mod.rs | 425 +++++++++++ .../business_logic_syscall_handler.rs | 28 + ...precated_business_logic_syscall_handler.rs | 32 + src/syscalls/deprecated_syscall_handler.rs | 14 + src/syscalls/native_syscall_handler.rs | 12 + src/syscalls/syscall_handler.rs | 2 + src/transaction/declare.rs | 33 +- src/transaction/declare_v2.rs | 12 +- src/transaction/deploy.rs | 17 +- src/transaction/deploy_account.rs | 29 +- src/transaction/fee.rs | 11 + src/transaction/invoke_function.rs | 42 +- src/transaction/l1_handler.rs | 7 +- src/transaction/mod.rs | 13 + tests/account_panic.rs | 2 + tests/cairo_1_syscalls.rs | 90 +++ tests/cairo_native.rs | 4 + tests/complex_contracts/erc20.rs | 12 + tests/complex_contracts/utils.rs | 4 + tests/delegate_call.rs | 2 + tests/delegate_l1_handler.rs | 2 + tests/deploy_account.rs | 4 + tests/fibonacci.rs | 4 + tests/increase_balance.rs | 2 + tests/internal_calls.rs | 2 + tests/internals.rs | 34 + tests/multi_syscall_test.rs | 2 + tests/storage.rs | 2 + tests/syscalls.rs | 12 + tests/syscalls_errors.rs | 2 + tests/yas_bench_test.rs | 17 +- 49 files changed, 1914 insertions(+), 220 deletions(-) create mode 100644 src/bin/native_executor.rs create mode 100644 src/sandboxing/mod.rs diff --git a/Cargo.lock b/Cargo.lock index e44656455..09f85fcf0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,7 +65,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb" dependencies = [ "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -198,7 +198,7 @@ dependencies = [ "actix-router", "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -513,7 +513,7 @@ checksum = "a66537f1bb974b254c98ed142ff995236e81b9d0fe4db0575f46612cb15eb0f9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -643,7 +643,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 2.0.39", + "syn 2.0.48", "which", ] @@ -666,7 +666,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 2.0.39", + "syn 2.0.48", "which", ] @@ -860,9 +860,9 @@ dependencies = [ [[package]] name = "cairo-lang-casm" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24abd6752c41f3d2276fba63fa0434875ccf6e7cbcdebfebc790db1201eb0892" +checksum = "40058b275150a66c15f39aa09f2a79be7acd089e526beca79ded7a29aafd0ae9" dependencies = [ "cairo-lang-utils", "indoc", @@ -877,9 +877,9 @@ dependencies = [ [[package]] name = "cairo-lang-compiler" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5699f44b183ddc2982976efdd72bbdfafc13a64f9593b44eb86f8a335f3b90da" +checksum = "dd38cbddb149d5417b3b9a655489de356f29b15be2637e4270c95ea5fcc51e87" dependencies = [ "anyhow", "cairo-lang-defs", @@ -900,18 +900,18 @@ dependencies = [ [[package]] name = "cairo-lang-debug" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a52b381fab22b723818692fa93aafc2e1490a79a4c8bd856e8996f1253f16a8" +checksum = "22ea67db5af077ca6817d1db5020ab450f28aff2d3ffbc55b6d3a5132e9bc0e1" dependencies = [ "cairo-lang-utils", ] [[package]] name = "cairo-lang-defs" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d436dcaa5a3ea69f60b990036d23dc3e54adc623c14cff824cb1230f974ce44" +checksum = "ed0f8ddae7bebbec634f6b0a7040a1c58003ec13be541e00e5e7ded04788c6d2" dependencies = [ "cairo-lang-debug", "cairo-lang-diagnostics", @@ -926,9 +926,9 @@ dependencies = [ [[package]] name = "cairo-lang-diagnostics" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07771c7268044b6a7be828a4f4938bb823944efe6668a974bf5e52a6801ef366" +checksum = "64269b3fd33c17920cb6b300257439a885b1251bddab78cfcfea02031ad10eb6" dependencies = [ "cairo-lang-debug", "cairo-lang-filesystem", @@ -938,9 +938,9 @@ dependencies = [ [[package]] name = "cairo-lang-eq-solver" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53fc8c224c0aaadc01c961fac234e8d8d3563a8fbb8544186a69969765a0c2a5" +checksum = "15f57f448a8ccd1ff77f663941bd8310e3ce4e30fdcfd6ea4ee7d897df0fab04" dependencies = [ "cairo-lang-utils", "good_lp", @@ -948,9 +948,9 @@ dependencies = [ [[package]] name = "cairo-lang-filesystem" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d62a519bd68f158cd1b5ee2f1f1dc2aa916a8dcb14914444846d39ff8fb33789" +checksum = "439a6506fb2f749964aac614f5295aa4fa1e18123aea0d1e09a03ea0311b3847" dependencies = [ "cairo-lang-debug", "cairo-lang-utils", @@ -962,9 +962,9 @@ dependencies = [ [[package]] name = "cairo-lang-lowering" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "989744e09f351ff24a0bc076dba8e3420ab957b99edc46acde58a484ebb09ed9" +checksum = "5c38232c57885ef108395e784aa0fc8d9e23a5a2b0f985484f10d6d9ef85c83e" dependencies = [ "cairo-lang-debug", "cairo-lang-defs", @@ -988,9 +988,9 @@ dependencies = [ [[package]] name = "cairo-lang-parser" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7be5b262ae2d11b4d5783a8b66d8e31cbeab45bb4e14d6ddb13ba280e9c41824" +checksum = "1bcaec01565917823193ea2db9f239c5d496987a006f7be894fd757216d64d94" dependencies = [ "cairo-lang-diagnostics", "cairo-lang-filesystem", @@ -1008,9 +1008,9 @@ dependencies = [ [[package]] name = "cairo-lang-plugins" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebcfe1e269ab2027bad187d9c61c7e1e324374c722b75f3a7d7fba77c977f8ca" +checksum = "428c7ce26429eaf08afe7831aab3e61c5858e88250227cdabfec8307cd25bea2" dependencies = [ "cairo-lang-defs", "cairo-lang-diagnostics", @@ -1027,20 +1027,20 @@ dependencies = [ [[package]] name = "cairo-lang-proc-macros" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9bc4f93f28bda29d879acb9b9e26b32108591225a507ffb5aff9bbf58524c4f" +checksum = "97efc9847766adc92e0c6f21dfaab26960c66579978f5767c5995950b40b73c6" dependencies = [ "cairo-lang-debug", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] name = "cairo-lang-project" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3d490a3d00dd45228c1691c390f177449b45a907a2cb4b3ad8f305faea82a0c" +checksum = "93b3ff44aee12e08955d2c107eb087cf3b238db8f3d306bdd2e3be95bb149880" dependencies = [ "cairo-lang-filesystem", "cairo-lang-utils", @@ -1079,9 +1079,9 @@ dependencies = [ [[package]] name = "cairo-lang-semantic" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bcc25adf813972129dcac6c7d87026d47644f3d4e975c042d27e1166a2a0489" +checksum = "a6cd57e7e6d437419643e075297532d98789f75d12824ee02ec4144278870bfc" dependencies = [ "cairo-lang-debug", "cairo-lang-defs", @@ -1104,9 +1104,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cc9571dbb143e33979fc02d38bbb4d7b608d6f53cde585b08551bbedfb0c217" +checksum = "68a035e4b4b961df135fc3ef81c4b1d5e1efa19a6a3245862860a11856af46bc" dependencies = [ "anyhow", "cairo-lang-utils", @@ -1129,9 +1129,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-ap-change" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16894bfe0072f82b549e4b2be42c7dc8f0611ac5049dfb069b95b10c51ec7cb5" +checksum = "f68b38763f995b27b2fb824a90e43b5671af17e015af8dfeb13eabad8149eba2" dependencies = [ "cairo-lang-eq-solver", "cairo-lang-sierra", @@ -1143,9 +1143,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-gas" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71fbc8baef18b3c315d3fb7a0cf59f0ee14df5cefc7dd147598248cdb7b2252d" +checksum = "19187cf2ef439fa1abcc65dac87e408482ae8a124748558e2d760ab7ce5b7333" dependencies = [ "cairo-lang-eq-solver", "cairo-lang-sierra", @@ -1157,9 +1157,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-generator" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75700bf5fd1c1cdd8ca29af338c65de8b03c905472f9bdce22b3034f3f3755d9" +checksum = "210fcedd39d513d4fdcd727156de179dc640ee27dca8e5bba076aa514cb70fe4" dependencies = [ "cairo-lang-debug", "cairo-lang-defs", @@ -1181,9 +1181,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-to-casm" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0bc8edaf95bdb9577c6b801c36b24538ec08042e1d74b98fc08d50621427061" +checksum = "fdea72b0699e040c1a56ed89878904d44184fc66208ce3da77dc78d4e34c33f9" dependencies = [ "assert_matches", "cairo-felt", @@ -1202,9 +1202,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-type-size" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50b028f3e06e150005e82b07500d4ea509ee8350f841d5645fbcf00bb49c4537" +checksum = "a70bd365647251e18669bb7e702a3e48455ba4d5712959ca11d16ddead65580b" dependencies = [ "cairo-lang-sierra", "cairo-lang-utils", @@ -1212,9 +1212,9 @@ dependencies = [ [[package]] name = "cairo-lang-starknet" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f55ffe2cbd277b6f3040e23bde028ae0b68e8e4910a9dd1c2f78ed668974d2a" +checksum = "b95bc3c2dd33256fe168706189f58d34214ab2b970b038dd27de38b2b2670a05" dependencies = [ "anyhow", "cairo-felt", @@ -1249,9 +1249,9 @@ dependencies = [ [[package]] name = "cairo-lang-syntax" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6d745c72ac1cef893239d36bc749742d2622136ffc1434a2c70b07096d02de0" +checksum = "23fe738fde316bc47a663adb0b6aabe4b8b43a2bcaee4824f220ac32c496672d" dependencies = [ "cairo-lang-debug", "cairo-lang-filesystem", @@ -1265,9 +1265,9 @@ dependencies = [ [[package]] name = "cairo-lang-syntax-codegen" -version = "2.4.3" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7c9f119185b736b5318325e7a0fc1378b706116c1c558e1ced1bdf55367813b" +checksum = "caa5e57952da2c330df7e63181d57b80e639096e061dff2c30730b7f7a124dda" dependencies = [ "genco", "xshell", @@ -1275,9 +1275,9 @@ dependencies = [ [[package]] name = "cairo-lang-utils" -version = "2.4.3" +version = "2.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "343ac08a5b9d34c9f4bbac7249453a31edad4cf4ff54487197cc344f8293dc4f" +checksum = "f12291945d9230ce82f6e658537fbc5cc34920b19283dfff8e814154edd378eb" dependencies = [ "indexmap 2.1.0", "itertools 0.11.0", @@ -1291,7 +1291,7 @@ dependencies = [ [[package]] name = "cairo-native" version = "0.1.0" -source = "git+https://github.com/lambdaclass/cairo_native?rev=5e003699e53003c03b848e3001f1d4ec0d7007ed#5e003699e53003c03b848e3001f1d4ec0d7007ed" +source = "git+https://github.com/lambdaclass/cairo_native?rev=a2e6d118af9a54295ef1ca4d693b98e54ffc3b0f#a2e6d118af9a54295ef1ca4d693b98e54ffc3b0f" dependencies = [ "bumpalo", "cairo-lang-compiler", @@ -1319,6 +1319,7 @@ dependencies = [ "mlir-sys", "num-bigint", "num-traits 0.2.17", + "serde", "starknet-types-core", "tempfile", "thiserror", @@ -1329,7 +1330,7 @@ dependencies = [ [[package]] name = "cairo-native-runtime" version = "0.1.0" -source = "git+https://github.com/lambdaclass/cairo_native?rev=5e003699e53003c03b848e3001f1d4ec0d7007ed#5e003699e53003c03b848e3001f1d4ec0d7007ed" +source = "git+https://github.com/lambdaclass/cairo_native?rev=a2e6d118af9a54295ef1ca4d693b98e54ffc3b0f#a2e6d118af9a54295ef1ca4d693b98e54ffc3b0f" dependencies = [ "cairo-lang-runner", "lazy_static", @@ -1350,7 +1351,7 @@ dependencies = [ "bitvec", "cairo-felt", "generic-array", - "hashbrown 0.14.3", + "hashbrown 0.14.2", "hex", "keccak", "lazy_static", @@ -1382,7 +1383,7 @@ dependencies = [ "cairo-lang-casm", "cairo-lang-starknet", "generic-array", - "hashbrown 0.14.3", + "hashbrown 0.14.2", "hex", "keccak", "lazy_static", @@ -1463,9 +1464,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.4.10" +version = "4.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41fffed7514f420abec6d183b1d3acfd9099c79c3a10a06ade4f8203f1411272" +checksum = "2275f18819641850fa26c89acc84d465c1bf91ce57bc2748b28c420473352f64" dependencies = [ "clap_builder", "clap_derive", @@ -1473,9 +1474,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.4.9" +version = "4.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63361bae7eef3771745f02d8d892bec2fee5f6e34af316ba556e7f97a7069ff1" +checksum = "07cdf1b148b25c1e1f7a42225e30a0d99a615cd4637eae7365548dd4529b95bc" dependencies = [ "anstream", "anstyle", @@ -1493,7 +1494,7 @@ dependencies = [ "heck 0.4.1", "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -1654,14 +1655,20 @@ dependencies = [ ] [[package]] -name = "crossbeam-utils" -version = "0.8.16" +name = "crossbeam-channel" +version = "0.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" +checksum = "176dc175b78f56c0f321911d9c8eb2b77a78a4860b9c19db83835fea1a46649b" dependencies = [ - "cfg-if", + "crossbeam-utils", ] +[[package]] +name = "crossbeam-utils" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" + [[package]] name = "crunchy" version = "0.2.2" @@ -1696,7 +1703,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37e366bff8cd32dd8754b0991fb66b279dc48f598c3a18914852a6673deef583" dependencies = [ "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -1753,7 +1760,7 @@ dependencies = [ "proc-macro2", "quote", "strsim", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -1775,7 +1782,7 @@ checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core 0.20.3", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -1797,7 +1804,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" dependencies = [ "cfg-if", - "hashbrown 0.14.3", + "hashbrown 0.14.2", "lock_api", "once_cell", "parking_lot_core 0.9.9", @@ -1933,7 +1940,7 @@ dependencies = [ "enum-ordinalize", "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -1989,7 +1996,7 @@ checksum = "05aa0010e0e391b9a82dc87bfc4dbee3cb8260eec12ad9d83c12788299fa9093" dependencies = [ "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -2000,12 +2007,12 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "errno" -version = "0.3.8" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +checksum = "f258a7194e7f7c2a7837a8913aeab7fd8c383457034fa20ce4dd3dcb813e8eb8" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.48.0", ] [[package]] @@ -2027,7 +2034,7 @@ dependencies = [ "sha2", "sha3", "thiserror", - "uuid", + "uuid 0.8.2", ] [[package]] @@ -2109,9 +2116,9 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "form_urlencoded" -version = "1.2.1" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652" dependencies = [ "percent-encoding", ] @@ -2215,7 +2222,7 @@ checksum = "d4cf186fea4af17825116f72932fe52cce9a13bae39ff63b4dc0cfdb3fb4bde1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -2255,9 +2262,9 @@ dependencies = [ [[package]] name = "gimli" -version = "0.28.1" +version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" [[package]] name = "glob" @@ -2314,9 +2321,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.14.3" +version = "0.14.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +checksum = "f93e7192158dbcda357bdec5fb5788eebf8bbac027f3f33e719d29135ae84156" dependencies = [ "ahash 0.8.6", "allocator-api2", @@ -2498,9 +2505,9 @@ checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" [[package]] name = "idna" -version = "0.5.0" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c" dependencies = [ "unicode-bidi", "unicode-normalization", @@ -2574,7 +2581,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" dependencies = [ "equivalent", - "hashbrown 0.14.3", + "hashbrown 0.14.2", "serde", ] @@ -2615,6 +2622,26 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "ipc-channel" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ab3a34c91b7e84a72643bd75d1bac3afd241f78f9859fe0b5e5b2a6a75732c2" +dependencies = [ + "bincode 1.3.3", + "crossbeam-channel", + "fnv", + "lazy_static", + "libc", + "mio", + "rand", + "sc", + "serde", + "tempfile", + "uuid 1.7.0", + "windows", +] + [[package]] name = "ipnet" version = "2.9.0" @@ -2676,9 +2703,9 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.66" +version = "0.3.65" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca" +checksum = "54c0c35952f67de54bb584e9fd912b3023117cbafc0a77d8f3dee1fb5f572fe8" dependencies = [ "wasm-bindgen", ] @@ -2881,7 +2908,7 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4a83fb7698b3643a0e34f9ae6f2e8f0178c0fd42f8b59d493aa271ff3a5bf21" dependencies = [ - "hashbrown 0.14.3", + "hashbrown 0.14.2", ] [[package]] @@ -2926,7 +2953,7 @@ dependencies = [ "proc-macro2", "quote", "regex", - "syn 2.0.39", + "syn 2.0.48", "tblgen", "unindent", ] @@ -3291,9 +3318,9 @@ checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" [[package]] name = "percent-encoding" -version = "2.3.1" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" +checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" [[package]] name = "petgraph" @@ -3335,7 +3362,7 @@ dependencies = [ "phf_shared 0.11.2", "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -3445,7 +3472,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae005bd773ab59b4725093fd7df83fd7892f7d8eafb48dbd7de6e024e4215f9d" dependencies = [ "proc-macro2", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -3496,9 +3523,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.76" +version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" dependencies = [ "unicode-ident", ] @@ -3514,9 +3541,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -3901,6 +3928,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "sc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "010e18bd3bfd1d45a7e666b236c78720df0d9a7698ebaa9c1c559961eb60a38b" + [[package]] name = "schemars" version = "0.8.16" @@ -3962,22 +3995,22 @@ checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" [[package]] name = "serde" -version = "1.0.193" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.193" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -4076,7 +4109,7 @@ dependencies = [ "darling 0.20.3", "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -4088,7 +4121,7 @@ dependencies = [ "darling 0.20.3", "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -4364,7 +4397,7 @@ checksum = "af6527b845423542c8a16e060ea1bc43f67229848e7cd4c4d80be994a84220ce" dependencies = [ "starknet-curve 0.4.0", "starknet-ff", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -4407,7 +4440,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f66fe05edab7ee6752a0aff3e14508001191083f3c6d0b6fa14f7008a96839b0" dependencies = [ "starknet-core 0.7.2", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -4516,6 +4549,7 @@ dependencies = [ "flate2", "getset", "hex", + "ipc-channel", "keccak", "lazy_static", "lru 0.11.1", @@ -4534,7 +4568,9 @@ dependencies = [ "starknet_api", "thiserror", "tracing", + "tracing-appender", "tracing-subscriber", + "uuid 1.7.0", ] [[package]] @@ -4600,9 +4636,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.39" +version = "2.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" dependencies = [ "proc-macro2", "quote", @@ -4722,7 +4758,7 @@ dependencies = [ "cfg-if", "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -4733,7 +4769,7 @@ checksum = "5c89e72a01ed4c579669add59014b9a524d609c0c88c6a585ce37485879f6ffb" dependencies = [ "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", "test-case-core", ] @@ -4754,7 +4790,7 @@ checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -4866,7 +4902,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -4956,6 +4992,18 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-appender" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3566e8ce28cc0a3fe42519fc80e6b4c943cc4c8cef275620eb8dac2d3d4e06cf" +dependencies = [ + "crossbeam-channel", + "thiserror", + "time", + "tracing-subscriber", +] + [[package]] name = "tracing-attributes" version = "0.1.27" @@ -4964,7 +5012,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -5038,9 +5086,9 @@ dependencies = [ [[package]] name = "unescaper" -version = "0.1.3" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8f0f68e58d297ba8b22b8b5a96a87b863ba6bb46aaf51e19a4b02c5a6dd5b7f" +checksum = "a96a44ae11e25afb520af4534fd7b0bd8cd613e35a78def813b8cf41631fa3c8" dependencies = [ "thiserror", ] @@ -5104,9 +5152,9 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] name = "ureq" -version = "2.9.1" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8cdd25c339e200129fe4de81451814e5228c9b771d57378817d6117cc2b3f97" +checksum = "7830e33f6e25723d41a63f77e434159dad02919f18f55a512b5f16f3b1d77138" dependencies = [ "base64 0.21.5", "flate2", @@ -5122,9 +5170,9 @@ dependencies = [ [[package]] name = "url" -version = "2.5.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633" +checksum = "143b538f18257fac9cad154828a57c6bf5157e1aa604d4816b5995bf6de87ae5" dependencies = [ "form_urlencoded", "idna", @@ -5153,6 +5201,16 @@ dependencies = [ "serde", ] +[[package]] +name = "uuid" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a" +dependencies = [ + "getrandom", + "serde", +] + [[package]] name = "valuable" version = "0.1.0" @@ -5192,9 +5250,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.89" +version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" +checksum = "7daec296f25a1bae309c0cd5c29c4b260e510e6d813c286b19eaadf409d40fce" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -5202,24 +5260,24 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.89" +version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" +checksum = "e397f4664c0e4e428e8313a469aaa58310d302159845980fd23b0f22a847f217" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-futures" -version = "0.4.39" +version = "0.4.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac36a15a220124ac510204aec1c3e5db8a22ab06fd6706d881dc6149f8ed9a12" +checksum = "9afec9963e3d0994cac82455b2b3502b81a7f40f9a0d32181f7528d9f4b43e02" dependencies = [ "cfg-if", "js-sys", @@ -5229,9 +5287,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.89" +version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2" +checksum = "5961017b3b08ad5f3fe39f1e79877f8ee7c23c5e5fd5eb80de95abc41f1f16b2" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -5239,28 +5297,28 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.89" +version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" +checksum = "c5353b8dab669f5e10f5bd76df26a9360c748f054f862ff5f3f8aae0c7fb3907" dependencies = [ "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.89" +version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" +checksum = "0d046c5d029ba91a1ed14da14dca44b68bf2f124cfbaf741c54151fdb3e0750b" [[package]] name = "web-sys" -version = "0.3.66" +version = "0.3.65" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50c24a44ec86bb68fbecd1b3efed7e85ea5621b39b35ef2766b66cd984f8010f" +checksum = "5db499c5f66323272151db0e666cd34f78617522fb0c1604d31a27c50c206a85" dependencies = [ "js-sys", "wasm-bindgen", @@ -5268,9 +5326,9 @@ dependencies = [ [[package]] name = "webpki-roots" -version = "0.25.3" +version = "0.25.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1778a42e8b3b90bff8d0f5032bf22250792889a5cdc752aa0020c84abe3aaf10" +checksum = "14247bb57be4f377dfb94c72830b8ce8fc6beac03cf4bf7b9732eadd414123fc" [[package]] name = "which" @@ -5315,6 +5373,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-core" version = "0.51.1" @@ -5342,15 +5409,6 @@ dependencies = [ "windows-targets 0.48.5", ] -[[package]] -name = "windows-sys" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" -dependencies = [ - "windows-targets 0.52.0", -] - [[package]] name = "windows-targets" version = "0.42.2" @@ -5381,21 +5439,6 @@ dependencies = [ "windows_x86_64_msvc 0.48.5", ] -[[package]] -name = "windows-targets" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" -dependencies = [ - "windows_aarch64_gnullvm 0.52.0", - "windows_aarch64_msvc 0.52.0", - "windows_i686_gnu 0.52.0", - "windows_i686_msvc 0.52.0", - "windows_x86_64_gnu 0.52.0", - "windows_x86_64_gnullvm 0.52.0", - "windows_x86_64_msvc 0.52.0", -] - [[package]] name = "windows_aarch64_gnullvm" version = "0.42.2" @@ -5408,12 +5451,6 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" - [[package]] name = "windows_aarch64_msvc" version = "0.42.2" @@ -5426,12 +5463,6 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" - [[package]] name = "windows_i686_gnu" version = "0.42.2" @@ -5444,12 +5475,6 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" -[[package]] -name = "windows_i686_gnu" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" - [[package]] name = "windows_i686_msvc" version = "0.42.2" @@ -5462,12 +5487,6 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" -[[package]] -name = "windows_i686_msvc" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" - [[package]] name = "windows_x86_64_gnu" version = "0.42.2" @@ -5480,12 +5499,6 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" - [[package]] name = "windows_x86_64_gnullvm" version = "0.42.2" @@ -5498,12 +5511,6 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" - [[package]] name = "windows_x86_64_msvc" version = "0.42.2" @@ -5516,12 +5523,6 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" -[[package]] -name = "windows_x86_64_msvc" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" - [[package]] name = "winnow" version = "0.5.19" @@ -5603,7 +5604,7 @@ checksum = "dd7e48ccf166952882ca8bd778a43502c64f33bf94c12ebe2a7f08e5a0f6689f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] @@ -5623,7 +5624,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.39", + "syn 2.0.48", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index edeec0b6d..51a0244c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ metrics = [] # Disclaimer: This feature enables state modifications being applied on reverted and failings txs, and also disables address availability check when deploying contracts. # Only use for benchmarking using the replay binary replay_benchmark = [] +cairo-native = ["dep:ipc-channel", "dep:tracing-appender", "dep:cairo-native", "dep:uuid"] [workspace] members = [ @@ -43,8 +44,10 @@ cairo-lang-runner = { workspace = true } cairo-lang-sierra = { workspace = true } cairo-lang-starknet = { workspace = true } cairo-lang-utils = { workspace = true } -cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "5e003699e53003c03b848e3001f1d4ec0d7007ed", optional = true } - +cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "a2e6d118af9a54295ef1ca4d693b98e54ffc3b0f", features = ["with-serde"], optional = true } +ipc-channel = { version = "0.18.0", features = ["memfd"], optional = true } +tracing-appender = { version = "0.2.3", optional = true} +uuid = { version = "1.6.1", features = ["serde", "v4"], optional = true } cairo-vm = { workspace = true } flate2 = "1.0.25" @@ -70,6 +73,7 @@ starknet_api = { workspace = true } starknet-crypto = "0.6.1" thiserror = { workspace = true } tracing = "0.1.37" +tracing-subscriber = "0.3.17" [dev-dependencies] assert_matches = "1.5.0" @@ -88,6 +92,11 @@ path = "src/bin/native_bench.rs" name = "cairo_native" required-features = ["cairo-native"] +[[bin]] +path = "src/bin/native_executor.rs" +name = "cairo_native_executor" +required-features = ["cairo-native"] + [[bench]] path = "bench/yas.rs" name = "yas" diff --git a/bench/internals.rs b/bench/internals.rs index 7d139fd91..c8abfbc53 100644 --- a/bench/internals.rs +++ b/bench/internals.rs @@ -116,6 +116,8 @@ pub fn deploy_account( block_context, #[cfg(feature = "cairo-native")] Some(program_cache.clone()), + #[cfg(feature = "cairo-native")] + None, ) }) .unwrap(); @@ -158,6 +160,8 @@ pub fn declare( block_context, #[cfg(feature = "cairo-native")] Some(program_cache.clone()), + #[cfg(feature = "cairo-native")] + None, ) }) .unwrap(); @@ -207,6 +211,8 @@ pub fn deploy( block_context, #[cfg(feature = "cairo-native")] Some(program_cache.clone()), + #[cfg(feature = "cairo-native")] + None, ) }) .unwrap(); @@ -254,6 +260,8 @@ pub fn invoke( block_context, #[cfg(feature = "cairo-native")] Some(program_cache.clone()), + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -282,6 +290,8 @@ pub fn invoke( 2_000_000, #[cfg(feature = "cairo-native")] Some(program_cache.clone()), + #[cfg(feature = "cairo-native")] + None, ) }) .unwrap(); diff --git a/bench/yas.rs b/bench/yas.rs index c6676c016..b16a878ba 100644 --- a/bench/yas.rs +++ b/bench/yas.rs @@ -385,6 +385,8 @@ where &BlockContext::default(), #[cfg(feature = "cairo-native")] Some(program_cache), + #[cfg(feature = "cairo-native")] + None, )?; // Ensure the execution was successful. @@ -426,6 +428,8 @@ where &BlockContext::default(), #[cfg(feature = "cairo-native")] Some(program_cache), + #[cfg(feature = "cairo-native")] + None, )?; // Ensure the execution was successful. @@ -467,6 +471,8 @@ where &BlockContext::default(), #[cfg(feature = "cairo-native")] Some(program_cache), + #[cfg(feature = "cairo-native")] + None, )?; // Ensure the execution was successful. @@ -508,6 +514,8 @@ where &BlockContext::default(), #[cfg(feature = "cairo-native")] Some(program_cache), + #[cfg(feature = "cairo-native")] + None, )?; // Ensure the execution was successful. @@ -560,6 +568,8 @@ where u64::MAX.into(), #[cfg(feature = "cairo-native")] Some(program_cache), + #[cfg(feature = "cairo-native")] + None, )?; // Ensure the execution was successful. @@ -607,6 +617,8 @@ where u64::MAX.into(), #[cfg(feature = "cairo-native")] Some(program_cache), + #[cfg(feature = "cairo-native")] + None, )?; // Ensure the execution was successful. @@ -646,6 +658,8 @@ where u64::MAX.into(), #[cfg(feature = "cairo-native")] Some(program_cache), + #[cfg(feature = "cairo-native")] + None, )?; // Ensure the execution was successful. @@ -701,6 +715,8 @@ where u64::MAX.into(), #[cfg(feature = "cairo-native")] Some(program_cache), + #[cfg(feature = "cairo-native")] + None, )?; // Ensure the execution was successful. @@ -752,6 +768,8 @@ where u64::MAX.into(), #[cfg(feature = "cairo-native")] Some(program_cache), + #[cfg(feature = "cairo-native")] + None, )?; // Ensure the execution was successful. @@ -801,6 +819,8 @@ where u64::MAX.into(), #[cfg(feature = "cairo-native")] Some(program_cache), + #[cfg(feature = "cairo-native")] + None, )?; // Ensure the execution was successful. @@ -859,6 +879,8 @@ where u64::MAX.into(), #[cfg(feature = "cairo-native")] Some(program_cache), + #[cfg(feature = "cairo-native")] + None, )?; // Ensure the execution was successful. @@ -919,6 +941,8 @@ where u64::MAX.into(), #[cfg(feature = "cairo-native")] Some(program_cache), + #[cfg(feature = "cairo-native")] + None, )?; // Ensure the execution was successful. @@ -967,6 +991,8 @@ where u64::MAX.into(), #[cfg(feature = "cairo-native")] Some(program_cache), + #[cfg(feature = "cairo-native")] + None, )?; // Ensure the execution was successful. diff --git a/cli/src/main.rs b/cli/src/main.rs index f37aca481..39f7f9ebe 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -207,6 +207,8 @@ fn invoke_parser( 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, )?; cached_state.apply_state_update(&StateDiff::from_cached_state(transactional_state.cache())?)?; @@ -274,6 +276,8 @@ fn call_parser( block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, )?; let call_info = call_info.ok_or(TransactionError::CallInfoIsNone)?; diff --git a/examples/contract_execution/src/main.rs b/examples/contract_execution/src/main.rs index ee7c2b1f1..cfc0ef8a4 100644 --- a/examples/contract_execution/src/main.rs +++ b/examples/contract_execution/src/main.rs @@ -138,6 +138,8 @@ fn test_contract( &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .expect("Account Deploy Failed") .call_info @@ -179,6 +181,8 @@ fn test_contract( &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .expect("could not declare the contract class"); @@ -205,6 +209,8 @@ fn test_contract( 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .expect("could not deploy contract") .call_info @@ -254,6 +260,8 @@ fn test_contract( 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); diff --git a/examples/lru_cache/main.rs b/examples/lru_cache/main.rs index 5e0912285..7bf9bd650 100644 --- a/examples/lru_cache/main.rs +++ b/examples/lru_cache/main.rs @@ -70,6 +70,8 @@ fn run_contract( &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -88,6 +90,8 @@ fn run_contract( &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -113,6 +117,8 @@ fn run_contract( 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); diff --git a/fuzzer/src/main.rs b/fuzzer/src/main.rs index 1b81ec7a0..38e35d5c1 100644 --- a/fuzzer/src/main.rs +++ b/fuzzer/src/main.rs @@ -195,6 +195,8 @@ fn main() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!(call_info.unwrap(), expected_call_info); diff --git a/rpc_state_reader/src/sir_state_reader.rs b/rpc_state_reader/src/sir_state_reader.rs index 67f1f9715..e13550a75 100644 --- a/rpc_state_reader/src/sir_state_reader.rs +++ b/rpc_state_reader/src/sir_state_reader.rs @@ -318,7 +318,7 @@ pub fn execute_tx_configurable_with_state( #[cfg(not(feature = "cairo-native"))] let sir_execution = tx.execute(state, &block_context, u128::MAX)?; #[cfg(feature = "cairo-native")] - let sir_execution = tx.execute(state, &block_context, u128::MAX, None)?; + let sir_execution = tx.execute(state, &block_context, u128::MAX, None, None)?; Ok(sir_execution) } diff --git a/src/bin/deploy.rs b/src/bin/deploy.rs index 0a9d62428..04b04355b 100644 --- a/src/bin/deploy.rs +++ b/src/bin/deploy.rs @@ -61,6 +61,8 @@ fn main() { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); } diff --git a/src/bin/deploy_invoke.rs b/src/bin/deploy_invoke.rs index ffe5eb683..11ffbaefe 100644 --- a/src/bin/deploy_invoke.rs +++ b/src/bin/deploy_invoke.rs @@ -78,6 +78,8 @@ fn main() { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -112,6 +114,8 @@ fn main() { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -135,6 +139,8 @@ fn main() { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); diff --git a/src/bin/fibonacci.rs b/src/bin/fibonacci.rs index 9b10d6cd6..776ae6cb8 100644 --- a/src/bin/fibonacci.rs +++ b/src/bin/fibonacci.rs @@ -132,6 +132,8 @@ fn main() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); diff --git a/src/bin/invoke.rs b/src/bin/invoke.rs index 209ae532e..c0ad0d8fb 100644 --- a/src/bin/invoke.rs +++ b/src/bin/invoke.rs @@ -94,6 +94,8 @@ fn main() { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -117,6 +119,8 @@ fn main() { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); diff --git a/src/bin/invoke_with_cachedstate.rs b/src/bin/invoke_with_cachedstate.rs index da04c900f..21eeadc51 100644 --- a/src/bin/invoke_with_cachedstate.rs +++ b/src/bin/invoke_with_cachedstate.rs @@ -71,6 +71,8 @@ fn main() { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -91,6 +93,8 @@ fn main() { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); diff --git a/src/bin/native_bench.rs b/src/bin/native_bench.rs index 23917dd2d..19750423d 100644 --- a/src/bin/native_bench.rs +++ b/src/bin/native_bench.rs @@ -380,6 +380,8 @@ fn bench_erc20(executions: usize, bench_type: BenchType) { false, block_context.invoke_tx_max_n_steps(), Some(program_cache.clone()), + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -461,6 +463,8 @@ fn bench_erc20(executions: usize, bench_type: BenchType) { false, block_context.invoke_tx_max_n_steps(), Some(program_cache.clone()), + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -528,7 +532,12 @@ fn bench_erc20(executions: usize, bench_type: BenchType) { // this will create the account and after that, // we can extract its address. let account1_address = account1_deploy_tx - .execute(&mut state, &Default::default(), Some(program_cache.clone())) + .execute( + &mut state, + &Default::default(), + Some(program_cache.clone()), + None, + ) .expect("failed to execute the deployment of account 1") .validate_info .expect("validate_info missing") @@ -558,7 +567,12 @@ fn bench_erc20(executions: usize, bench_type: BenchType) { // execute the deploy_account transaction and retrieve the deployed account address. let _account2_address = account2_deploy_tx - .execute(&mut state, &Default::default(), Some(program_cache.clone())) + .execute( + &mut state, + &Default::default(), + Some(program_cache.clone()), + None, + ) .expect("failed to execute the deployment of account 2") .validate_info .expect("validate_info missing") @@ -631,6 +645,7 @@ fn execute( false, block_context.invoke_tx_max_n_steps(), Some(program_cache), + None, ) .unwrap() .call_info diff --git a/src/bin/native_executor.rs b/src/bin/native_executor.rs new file mode 100644 index 000000000..c34d0f466 --- /dev/null +++ b/src/bin/native_executor.rs @@ -0,0 +1,682 @@ +use std::{cell::RefCell, path::PathBuf, rc::Rc}; + +use cairo_lang_utils::ResultHelper; +use cairo_native::{ + context::NativeContext, + executor::JitNativeExecutor, + metadata::syscall_handler::SyscallHandlerMeta, + starknet::{StarkNetSyscallHandler, SyscallResult}, + utils::find_entry_point_by_idx, +}; +use cairo_vm::Felt252 as Felt; +use ipc_channel::ipc::{IpcOneShotServer, IpcReceiver, IpcSender}; +use starknet_in_rust::sandboxing::{Message, SyscallAnswer, SyscallRequest, WrappedMessage}; +use tracing::instrument; + +#[derive(Debug)] +struct SyscallHandler { + sender: IpcSender, + receiver: Rc>>, +} + +impl StarkNetSyscallHandler for SyscallHandler { + #[instrument(skip(self))] + fn get_block_hash(&mut self, block_number: u64, gas: &mut u128) -> SyscallResult { + self.sender + .send( + Message::SyscallRequest(SyscallRequest::GetBlockHash { + block_number, + gas: *gas, + }) + .wrap() + .unwrap(), + ) + .expect("failed to send"); + let result = self + .receiver + .borrow() + .recv() + .on_err(|e| tracing::error!("error receiving: {:?}", e)) + .unwrap() + .to_msg() + .unwrap(); + + if let Message::SyscallAnswer(SyscallAnswer::GetBlockHash { + result, + remaining_gas, + }) = result + { + *gas = remaining_gas; + result + } else { + tracing::error!("wrong message received: {:#?}", result); + panic!(); + } + } + + #[instrument(skip(self))] + fn get_execution_info( + &mut self, + gas: &mut u128, + ) -> SyscallResult { + self.sender + .send( + Message::SyscallRequest(SyscallRequest::GetExecutionInfo { gas: *gas }) + .wrap() + .unwrap(), + ) + .expect("failed to send"); + let result = self + .receiver + .borrow() + .recv() + .on_err(|e| tracing::error!("error receiving: {:?}", e)) + .unwrap() + .to_msg() + .unwrap(); + + if let Message::SyscallAnswer(SyscallAnswer::GetExecutionInfo { + result, + remaining_gas, + }) = result + { + *gas = remaining_gas; + result + } else { + tracing::error!("wrong message received: {:#?}", result); + panic!(); + } + } + + fn deploy( + &mut self, + class_hash: Felt, + contract_address_salt: Felt, + calldata: &[Felt], + deploy_from_zero: bool, + gas: &mut u128, + ) -> SyscallResult<(Felt, Vec)> { + self.sender + .send( + Message::SyscallRequest(SyscallRequest::Deploy { + class_hash, + contract_address_salt, + calldata: calldata.to_vec(), + deploy_from_zero, + gas: *gas, + }) + .wrap() + .unwrap(), + ) + .expect("failed to send"); + let result = self + .receiver + .borrow() + .recv() + .on_err(|e| tracing::error!("error receiving: {:?}", e)) + .unwrap() + .to_msg() + .unwrap(); + + if let Message::SyscallAnswer(SyscallAnswer::Deploy { + result, + remaining_gas, + }) = result + { + *gas = remaining_gas; + result + } else { + tracing::error!("wrong message received: {:#?}", result); + panic!(); + } + } + + fn replace_class(&mut self, class_hash: Felt, gas: &mut u128) -> SyscallResult<()> { + self.sender + .send( + Message::SyscallRequest(SyscallRequest::ReplaceClass { + class_hash, + gas: *gas, + }) + .wrap() + .unwrap(), + ) + .expect("failed to send"); + let result = self + .receiver + .borrow() + .recv() + .on_err(|e| tracing::error!("error receiving: {:?}", e)) + .unwrap() + .to_msg() + .unwrap(); + + if let Message::SyscallAnswer(SyscallAnswer::ReplaceClass { + result, + remaining_gas, + }) = result + { + *gas = remaining_gas; + result + } else { + tracing::error!("wrong message received: {:#?}", result); + panic!(); + } + } + + fn library_call( + &mut self, + class_hash: Felt, + function_selector: Felt, + calldata: &[Felt], + gas: &mut u128, + ) -> SyscallResult> { + self.sender + .send( + Message::SyscallRequest(SyscallRequest::LibraryCall { + class_hash, + function_selector, + calldata: calldata.to_vec(), + gas: *gas, + }) + .wrap() + .unwrap(), + ) + .expect("failed to send"); + let result = self + .receiver + .borrow() + .recv() + .on_err(|e| tracing::error!("error receiving: {:?}", e)) + .unwrap() + .to_msg() + .unwrap(); + + if let Message::SyscallAnswer(SyscallAnswer::LibraryCall { + result, + remaining_gas, + }) = result + { + *gas = remaining_gas; + result + } else { + tracing::error!("wrong message received: {:#?}", result); + panic!(); + } + } + + fn call_contract( + &mut self, + address: Felt, + entry_point_selector: Felt, + calldata: &[Felt], + gas: &mut u128, + ) -> SyscallResult> { + self.sender + .send( + Message::SyscallRequest(SyscallRequest::CallContract { + address, + entry_point_selector, + calldata: calldata.to_vec(), + gas: *gas, + }) + .wrap() + .unwrap(), + ) + .expect("failed to send"); + let result = self + .receiver + .borrow() + .recv() + .on_err(|e| tracing::error!("error receiving: {:?}", e)) + .unwrap() + .to_msg() + .unwrap(); + + if let Message::SyscallAnswer(SyscallAnswer::CallContract { + result, + remaining_gas, + }) = result + { + *gas = remaining_gas; + result + } else { + tracing::error!("wrong message received: {:#?}", result); + panic!(); + } + } + + fn storage_read( + &mut self, + address_domain: u32, + address: Felt, + gas: &mut u128, + ) -> SyscallResult { + self.sender + .send( + Message::SyscallRequest(SyscallRequest::StorageRead { + address_domain, + address, + gas: *gas, + }) + .wrap() + .unwrap(), + ) + .expect("failed to send"); + let result = self + .receiver + .borrow() + .recv() + .on_err(|e| tracing::error!("error receiving: {:?}", e)) + .unwrap() + .to_msg() + .unwrap(); + + if let Message::SyscallAnswer(SyscallAnswer::StorageRead { + result, + remaining_gas, + }) = result + { + *gas = remaining_gas; + result + } else { + tracing::error!("wrong message received: {:#?}", result); + panic!(); + } + } + + fn storage_write( + &mut self, + address_domain: u32, + address: Felt, + value: Felt, + gas: &mut u128, + ) -> SyscallResult<()> { + self.sender + .send( + Message::SyscallRequest(SyscallRequest::StorageWrite { + address_domain, + address, + value, + gas: *gas, + }) + .wrap() + .unwrap(), + ) + .expect("failed to send"); + let result = self + .receiver + .borrow() + .recv() + .on_err(|e| tracing::error!("error receiving: {:?}", e)) + .unwrap() + .to_msg() + .unwrap(); + + if let Message::SyscallAnswer(SyscallAnswer::StorageWrite { + result, + remaining_gas, + }) = result + { + *gas = remaining_gas; + result + } else { + tracing::error!("wrong message received: {:#?}", result); + panic!(); + } + } + + fn emit_event(&mut self, keys: &[Felt], data: &[Felt], gas: &mut u128) -> SyscallResult<()> { + self.sender + .send( + Message::SyscallRequest(SyscallRequest::EmitEvent { + keys: keys.to_vec(), + data: data.to_vec(), + gas: *gas, + }) + .wrap() + .unwrap(), + ) + .expect("failed to send"); + let result = self + .receiver + .borrow() + .recv() + .on_err(|e| tracing::error!("error receiving: {:?}", e)) + .unwrap() + .to_msg() + .unwrap(); + + if let Message::SyscallAnswer(SyscallAnswer::EmitEvent { + result, + remaining_gas, + }) = result + { + *gas = remaining_gas; + result + } else { + tracing::error!("wrong message received: {:#?}", result); + panic!(); + } + } + + fn send_message_to_l1( + &mut self, + to_address: Felt, + payload: &[Felt], + gas: &mut u128, + ) -> SyscallResult<()> { + self.sender + .send( + Message::SyscallRequest(SyscallRequest::SendMessageToL1 { + to_address, + payload: payload.to_vec(), + gas: *gas, + }) + .wrap() + .unwrap(), + ) + .expect("failed to send"); + let result = self + .receiver + .borrow() + .recv() + .on_err(|e| tracing::error!("error receiving: {:?}", e)) + .unwrap() + .to_msg() + .unwrap(); + + if let Message::SyscallAnswer(SyscallAnswer::SendMessageToL1 { + result, + remaining_gas, + }) = result + { + *gas = remaining_gas; + result + } else { + tracing::error!("wrong message received: {:#?}", result); + panic!(); + } + } + + fn keccak( + &mut self, + input: &[u64], + gas: &mut u128, + ) -> SyscallResult { + self.sender + .send( + Message::SyscallRequest(SyscallRequest::Keccak { + input: input.to_vec(), + gas: *gas, + }) + .wrap() + .unwrap(), + ) + .expect("failed to send"); + let result = self + .receiver + .borrow() + .recv() + .on_err(|e| tracing::error!("error receiving: {:?}", e)) + .unwrap() + .to_msg() + .unwrap(); + + if let Message::SyscallAnswer(SyscallAnswer::Keccak { + result, + remaining_gas, + }) = result + { + *gas = remaining_gas; + result + } else { + tracing::error!("wrong message received: {:#?}", result); + panic!(); + } + } + + fn secp256k1_add( + &mut self, + _p0: cairo_native::starknet::Secp256k1Point, + _p1: cairo_native::starknet::Secp256k1Point, + _gas: &mut u128, + ) -> SyscallResult> { + todo!() + } + + fn secp256k1_get_point_from_x( + &self, + _x: cairo_native::starknet::U256, + _y_parity: bool, + _gas: &mut u128, + ) -> SyscallResult> { + todo!() + } + + fn secp256k1_get_xy( + &self, + _p: cairo_native::starknet::Secp256k1Point, + _gas: &mut u128, + ) -> SyscallResult<(cairo_native::starknet::U256, cairo_native::starknet::U256)> { + todo!() + } + + fn secp256k1_mul( + &self, + _p: cairo_native::starknet::Secp256k1Point, + _m: cairo_native::starknet::U256, + _gas: &mut u128, + ) -> SyscallResult> { + todo!() + } + + fn secp256k1_new( + &self, + _x: cairo_native::starknet::U256, + _y: cairo_native::starknet::U256, + _gas: &mut u128, + ) -> SyscallResult> { + todo!() + } + + fn secp256r1_add( + &self, + _p0: cairo_native::starknet::Secp256k1Point, + _p1: cairo_native::starknet::Secp256k1Point, + _gas: &mut u128, + ) -> SyscallResult> { + todo!() + } + + fn secp256r1_get_point_from_x( + &self, + _x: cairo_native::starknet::U256, + _y_parity: bool, + _gas: &mut u128, + ) -> SyscallResult> { + todo!() + } + + fn secp256r1_get_xy( + &self, + _p: cairo_native::starknet::Secp256k1Point, + _gas: &mut u128, + ) -> SyscallResult<(cairo_native::starknet::U256, cairo_native::starknet::U256)> { + todo!() + } + + fn secp256r1_mul( + &self, + _p: cairo_native::starknet::Secp256k1Point, + _m: cairo_native::starknet::U256, + _gas: &mut u128, + ) -> SyscallResult> { + todo!() + } + + fn secp256r1_new( + &mut self, + _x: cairo_native::starknet::U256, + _y: cairo_native::starknet::U256, + _gas: &mut u128, + ) -> SyscallResult> { + todo!() + } + + fn pop_log(&mut self) { + todo!() + } + + fn set_account_contract_address(&mut self, _contract_address: Felt) { + todo!() + } + + fn set_block_number(&mut self, _block_number: u64) { + todo!() + } + + fn set_block_timestamp(&mut self, _block_timestamp: u64) { + todo!() + } + + fn set_caller_address(&mut self, _address: Felt) { + todo!() + } + + fn set_chain_id(&mut self, _chain_id: Felt) { + todo!() + } + + fn set_contract_address(&mut self, _address: Felt) { + todo!() + } + + fn set_max_fee(&mut self, _max_fee: u128) { + todo!() + } + + fn set_nonce(&mut self, _nonce: Felt) { + todo!() + } + + fn set_sequencer_address(&mut self, _address: Felt) { + todo!() + } + + fn set_signature(&mut self, _signature: &[Felt]) { + todo!() + } + + fn set_transaction_hash(&mut self, _transaction_hash: Felt) { + todo!() + } + + fn set_version(&mut self, _version: Felt) { + todo!() + } + + fn get_execution_info_v2( + &mut self, + _remaining_gas: &mut u128, + ) -> SyscallResult { + todo!() + } +} + +pub fn main() -> Result<(), Box> { + let mut args = std::env::args(); + + let pid = std::process::id(); + + let log_dir = PathBuf::from( + std::env::var("CAIRO_EXECUTOR_LOGDIR").unwrap_or("executor_logs/".to_string()), + ); + let file_appender = + tracing_appender::rolling::daily(log_dir, format!("cairo-executor.{pid}.log")); + + tracing_subscriber::fmt() + .with_writer(file_appender) + .with_ansi(false) + .init(); + + if args.len() < 2 { + tracing::error!("missing server ipc name"); + std::process::exit(1); + } + + let server = args.nth(1).unwrap(); + let (sv, name) = IpcOneShotServer::::new()?; + println!("{name}"); // print to let know + let sender = IpcSender::connect(server.clone())?; + sender.send(Message::Ping.wrap()?)?; + tracing::info!("connected to {server:?}"); + let (receiver, msg) = sv.accept()?; + let receiver = Rc::new(RefCell::new(receiver)); + tracing::info!("accepted {receiver:?}"); + assert_eq!(msg, Message::Ping.wrap()?); + + let native_context = NativeContext::new(); + tracing::info!("initialized native context"); + + let mut syscall_handler = SyscallHandler { + sender: sender.clone(), + receiver: receiver.clone(), + }; + + loop { + tracing::info!("waiting for message"); + + let message: Message = receiver.borrow().recv()?.to_msg()?; + + match message { + Message::ExecuteJIT { + id, + program, + inputs, + function_idx, + gas, + } => { + tracing::info!("Message: ExecuteJIT"); + sender.send(Message::Ack(id).wrap()?)?; + tracing::info!("sent ack: {:?}", id); + let program = program.into_v1()?.program; + let native_program = native_context.compile(&program)?; + + let entry_point_fn = find_entry_point_by_idx(&program, function_idx).unwrap(); + + let fn_id = &entry_point_fn.id; + + let native_executor = + JitNativeExecutor::from_native_module(native_program, Default::default()); + + let result = native_executor.invoke_contract_dynamic( + fn_id, + &inputs, + gas, + Some(&SyscallHandlerMeta::new(&mut syscall_handler)), + )?; + + tracing::info!("invoked with result: {:?}", result); + + sender.send(Message::ExecutionResult { id, result }.wrap()?)?; + + tracing::info!("sent result msg"); + } + Message::ExecutionResult { .. } => {} + Message::Ack(_) => {} + Message::Ping => { + tracing::info!("Message: Ping"); + sender.send(Message::Ping.wrap()?)?; + } + Message::Kill => { + tracing::info!("Message: KILL"); + break; + } + Message::SyscallRequest(_) => todo!(), + Message::SyscallAnswer(_) => todo!(), + } + } + + Ok(()) +} diff --git a/src/execution/execution_entry_point.rs b/src/execution/execution_entry_point.rs index 1d5edafdb..6ef5f630a 100644 --- a/src/execution/execution_entry_point.rs +++ b/src/execution/execution_entry_point.rs @@ -46,6 +46,7 @@ use cairo_vm::{ use std::sync::Arc; #[cfg(feature = "cairo-native")] use { + crate::sandboxing::IsolatedExecutor, crate::state::StateDiff, cairo_native::cache::{JitProgramCache, ProgramCache}, cairo_native::OptLevel, @@ -116,6 +117,7 @@ impl ExecutionEntryPoint { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&IsolatedExecutor>, ) -> Result where T: StateReader, @@ -174,6 +176,7 @@ impl ExecutionEntryPoint { block_context, &class_hash, program_cache, + sandbox, ) { Ok(call_info) => { state.apply_state_update(&StateDiff::from_cached_state( @@ -684,6 +687,7 @@ impl ExecutionEntryPoint { block_context: &BlockContext, class_hash: &ClassHash, program_cache: Rc>>, + sandbox: Option<&IsolatedExecutor>, ) -> Result { use cairo_native::{ executor::NativeExecutor, metadata::syscall_handler::SyscallHandlerMeta, @@ -753,6 +757,7 @@ impl ExecutionEntryPoint { block_context: block_context.clone(), program_cache: program_cache.clone(), resources_manager: Default::default(), + sandbox, }; let syscall_meta = SyscallHandlerMeta::new(&mut syscall_handler); diff --git a/src/lib.rs b/src/lib.rs index eb95726d2..f321d9d4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,6 +48,8 @@ pub mod execution; pub mod hash_utils; pub mod parser_errors; pub mod runner; +#[cfg(feature = "cairo-native")] +pub mod sandboxing; pub mod serde_structs; pub mod services; pub mod state; @@ -70,6 +72,7 @@ pub fn simulate_transaction( #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result, TransactionError> { let mut cache_state = CachedState::new(Arc::new(state), contract_class_cache); let mut result = Vec::with_capacity(transactions.len()); @@ -87,6 +90,8 @@ pub fn simulate_transaction( remaining_gas, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox, )?; result.push(tx_result); } @@ -102,6 +107,7 @@ pub fn estimate_fee( #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result, TransactionError> where T: StateReader, @@ -122,6 +128,8 @@ where 100_000_000, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox, )?; if let Some(gas_usage) = transaction_result.actual_resources.get("l1_gas_usage") { result.push((transaction_result.actual_fee, *gas_usage)); @@ -135,6 +143,7 @@ where Ok(result) } +#[allow(clippy::too_many_arguments)] pub fn call_contract( contract_address: Felt252, entrypoint_selector: Felt252, @@ -145,6 +154,7 @@ pub fn call_contract( #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result, TransactionError> { let contract_address = Address(contract_address); let class_hash = state.get_class_hash_at(&contract_address)?; @@ -187,6 +197,8 @@ pub fn call_contract( block_context.invoke_tx_max_n_steps, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?; let call_info = call_info.ok_or(TransactionError::CallInfoIsNone)?; @@ -201,6 +213,7 @@ pub fn estimate_message_fee( #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result<(u128, usize), TransactionError> where T: StateReader, @@ -216,6 +229,8 @@ where 1_000_000, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?; let tx_fee = calculate_tx_fee( &transaction_result.actual_resources, @@ -237,6 +252,7 @@ pub fn execute_transaction( #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { tx.execute( state, @@ -244,6 +260,8 @@ pub fn execute_transaction( remaining_gas, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) } @@ -340,6 +358,8 @@ mod test { &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!(estimated_fee[0], (2483, 2448)); @@ -390,6 +410,8 @@ mod test { Address(0.into()), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -449,6 +471,8 @@ mod test { &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!(estimated_fee, (18485, 18471)); @@ -517,6 +541,8 @@ mod test { 1000000, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -645,6 +671,8 @@ mod test { false, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -754,6 +782,8 @@ mod test { false, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -807,6 +837,8 @@ mod test { false, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); } @@ -850,6 +882,8 @@ mod test { false, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); } @@ -891,6 +925,8 @@ mod test { &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -924,6 +960,8 @@ mod test { false, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); } @@ -973,6 +1011,8 @@ mod test { false, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); } @@ -1021,6 +1061,8 @@ mod test { false, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); } @@ -1090,6 +1132,8 @@ mod test { false, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); } @@ -1150,6 +1194,8 @@ mod test { false, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -1160,6 +1206,8 @@ mod test { block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(), [(0, 1836), (0, 2448)] @@ -1182,6 +1230,8 @@ mod test { INITIAL_GAS_COST, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap_err(); @@ -1292,6 +1342,8 @@ mod test { false, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap()[0] .actual_fee; @@ -1309,6 +1361,8 @@ mod test { false, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap()[0] .actual_fee; diff --git a/src/sandboxing/mod.rs b/src/sandboxing/mod.rs new file mode 100644 index 000000000..721184d89 --- /dev/null +++ b/src/sandboxing/mod.rs @@ -0,0 +1,425 @@ +use std::{ + io::{BufRead, BufReader}, + path::Path, + process::{Child, Stdio}, +}; + +use cairo_lang_sierra::program::{Program, VersionedProgram}; +use cairo_vm::Felt252 as Felt; +use ipc_channel::ipc::{IpcOneShotServer, IpcReceiver, IpcSender}; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +use cairo_native::{ + execution_result::ContractExecutionResult, + starknet::{ExecutionInfo, StarkNetSyscallHandler, SyscallResult}, +}; + +#[allow(clippy::large_enum_variant)] +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum Message { + ExecuteJIT { + id: Uuid, + program: VersionedProgram, + inputs: Vec, + function_idx: usize, + gas: Option, + }, + ExecutionResult { + id: Uuid, + result: ContractExecutionResult, + }, + Ack(Uuid), + Ping, + Kill, + SyscallRequest(SyscallRequest), + SyscallAnswer(SyscallAnswer), +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum SyscallRequest { + GetBlockHash { + block_number: u64, + gas: u128, + }, + GetExecutionInfo { + gas: u128, + }, + Deploy { + class_hash: Felt, + contract_address_salt: Felt, + calldata: Vec, + deploy_from_zero: bool, + gas: u128, + }, + ReplaceClass { + class_hash: Felt, + gas: u128, + }, + LibraryCall { + class_hash: Felt, + function_selector: Felt, + calldata: Vec, + gas: u128, + }, + CallContract { + address: Felt, + entry_point_selector: Felt, + calldata: Vec, + gas: u128, + }, + EmitEvent { + keys: Vec, + data: Vec, + gas: u128, + }, + SendMessageToL1 { + to_address: Felt, + payload: Vec, + gas: u128, + }, + Keccak { + input: Vec, + gas: u128, + }, + StorageRead { + address_domain: u32, + address: Felt, + gas: u128, + }, + StorageWrite { + address_domain: u32, + address: Felt, + value: Felt, + gas: u128, + }, +} + +#[allow(clippy::large_enum_variant)] +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum SyscallAnswer { + GetBlockHash { + result: SyscallResult, + remaining_gas: u128, + }, + GetExecutionInfo { + result: SyscallResult, + remaining_gas: u128, + }, + Deploy { + result: SyscallResult<(Felt, Vec)>, + remaining_gas: u128, + }, + ReplaceClass { + result: SyscallResult<()>, + remaining_gas: u128, + }, + LibraryCall { + result: SyscallResult>, + remaining_gas: u128, + }, + CallContract { + result: SyscallResult>, + remaining_gas: u128, + }, + StorageRead { + result: SyscallResult, + remaining_gas: u128, + }, + StorageWrite { + result: SyscallResult<()>, + remaining_gas: u128, + }, + EmitEvent { + result: SyscallResult<()>, + remaining_gas: u128, + }, + SendMessageToL1 { + result: SyscallResult<()>, + remaining_gas: u128, + }, + Keccak { + result: SyscallResult, + remaining_gas: u128, + }, +} + +impl Message { + pub fn serialize(&self) -> Result { + serde_json::to_string(self) + } + + pub fn deserialize(value: &str) -> Result { + serde_json::from_str(value) + } + + pub fn wrap(&self) -> Result { + Ok(WrappedMessage::Message(self.serialize()?)) + } +} + +#[derive(Debug, Serialize, Deserialize, PartialEq)] +pub enum WrappedMessage { + Message(String), // ipc-channel uses bincode and doesnt support serializing Vecs +} + +impl WrappedMessage { + pub fn to_msg(self) -> Result { + match self { + WrappedMessage::Message(msg) => Message::deserialize(&msg), + } + } +} + +#[derive(Debug)] +pub struct IsolatedExecutor { + proc: Child, + sender: IpcSender, + receiver: IpcReceiver, +} + +impl IsolatedExecutor { + // "target/debug/cairo-executor" + pub fn new(executor_path: &Path) -> Result { + let (server, server_name) = IpcOneShotServer::new().unwrap(); + tracing::debug!("creating executor with: {:?}", executor_path); + + let mut cmd = std::process::Command::new(executor_path); + cmd.stdout(Stdio::piped()); + let mut proc = cmd.arg(server_name).spawn()?; + let stdout = proc.stdout.take().unwrap(); + let mut stdout = BufReader::new(stdout); + let mut client_name = String::new(); + stdout.read_line(&mut client_name)?; + + // first we accept the connection + let (receiver, msg) = server.accept().expect("failed to accept receiver"); + tracing::debug!("accepted receiver {receiver:?} with msg {msg:?}"); + // then we connect + tracing::debug!("connecting to {client_name}"); + let sender = IpcSender::connect(client_name.trim().to_string()).expect("failed to connect"); + sender.send(Message::Ping.wrap()?).unwrap(); + + Ok(Self { + proc, + sender, + receiver, + }) + } + + pub fn run_program( + &self, + program: Program, + inputs: Vec, + gas: Option, + function_idx: usize, + handler: &mut impl StarkNetSyscallHandler, + ) -> Result> { + tracing::debug!("running program"); + let id = Uuid::new_v4(); + + let msg = Message::ExecuteJIT { + id, + program: program.into_artifact(), + inputs, + gas, + function_idx, + }; + self.sender.send(msg.wrap()?)?; + + loop { + let msg = self.receiver.recv()?.to_msg()?; + match msg { + Message::ExecuteJIT { .. } => unreachable!(), + Message::ExecutionResult { + id: recv_id, + result, + } => { + assert_eq!(recv_id, id, "id mismatch"); + return Ok(result); + } + Message::Ack(recv_id) => { + assert_eq!(recv_id, id, "id mismatch"); + } + Message::Ping => unreachable!(), + Message::Kill => todo!(), + Message::SyscallRequest(request) => match request { + SyscallRequest::GetBlockHash { + block_number, + mut gas, + } => { + let result = handler.get_block_hash(block_number, &mut gas); + self.sender.send( + Message::SyscallAnswer(SyscallAnswer::GetBlockHash { + result, + remaining_gas: gas, + }) + .wrap()?, + )?; + } + SyscallRequest::GetExecutionInfo { mut gas } => { + let result = handler.get_execution_info(&mut gas); + self.sender.send( + Message::SyscallAnswer(SyscallAnswer::GetExecutionInfo { + result, + remaining_gas: gas, + }) + .wrap()?, + )?; + } + SyscallRequest::StorageRead { + address_domain, + address, + mut gas, + } => { + let result = handler.storage_read(address_domain, address, &mut gas); + self.sender.send( + Message::SyscallAnswer(SyscallAnswer::StorageRead { + result, + remaining_gas: gas, + }) + .wrap()?, + )?; + } + SyscallRequest::StorageWrite { + address_domain, + address, + value, + mut gas, + } => { + let result = + handler.storage_write(address_domain, address, value, &mut gas); + self.sender.send( + Message::SyscallAnswer(SyscallAnswer::StorageWrite { + result, + remaining_gas: gas, + }) + .wrap()?, + )?; + } + SyscallRequest::Deploy { + class_hash, + contract_address_salt, + calldata, + deploy_from_zero, + mut gas, + } => { + let result = handler.deploy( + class_hash, + contract_address_salt, + &calldata, + deploy_from_zero, + &mut gas, + ); + self.sender.send( + Message::SyscallAnswer(SyscallAnswer::Deploy { + result, + remaining_gas: gas, + }) + .wrap()?, + )?; + } + SyscallRequest::ReplaceClass { + class_hash, + mut gas, + } => { + let result = handler.replace_class(class_hash, &mut gas); + self.sender.send( + Message::SyscallAnswer(SyscallAnswer::ReplaceClass { + result, + remaining_gas: gas, + }) + .wrap()?, + )?; + } + SyscallRequest::LibraryCall { + class_hash, + function_selector, + calldata, + mut gas, + } => { + let result = handler.library_call( + class_hash, + function_selector, + &calldata, + &mut gas, + ); + self.sender.send( + Message::SyscallAnswer(SyscallAnswer::LibraryCall { + result, + remaining_gas: gas, + }) + .wrap()?, + )?; + } + SyscallRequest::CallContract { + address, + entry_point_selector, + calldata, + mut gas, + } => { + let result = handler.call_contract( + address, + entry_point_selector, + &calldata, + &mut gas, + ); + self.sender.send( + Message::SyscallAnswer(SyscallAnswer::CallContract { + result, + remaining_gas: gas, + }) + .wrap()?, + )?; + } + SyscallRequest::EmitEvent { + keys, + data, + mut gas, + } => { + let result = handler.emit_event(&keys, &data, &mut gas); + self.sender.send( + Message::SyscallAnswer(SyscallAnswer::EmitEvent { + result, + remaining_gas: gas, + }) + .wrap()?, + )?; + } + SyscallRequest::SendMessageToL1 { + to_address, + payload, + mut gas, + } => { + let result = handler.send_message_to_l1(to_address, &payload, &mut gas); + self.sender.send( + Message::SyscallAnswer(SyscallAnswer::SendMessageToL1 { + result, + remaining_gas: gas, + }) + .wrap()?, + )?; + } + SyscallRequest::Keccak { input, mut gas } => { + let result = handler.keccak(&input, &mut gas); + self.sender.send( + Message::SyscallAnswer(SyscallAnswer::Keccak { + result, + remaining_gas: gas, + }) + .wrap()?, + )?; + } + }, + Message::SyscallAnswer(_) => unreachable!(), + } + } + } +} + +impl Drop for IsolatedExecutor { + fn drop(&mut self) { + let _ = self.sender.send(Message::Kill.wrap().unwrap()); + let _ = self.proc.kill(); + } +} diff --git a/src/syscalls/business_logic_syscall_handler.rs b/src/syscalls/business_logic_syscall_handler.rs index c81e3e47b..8a3d3348b 100644 --- a/src/syscalls/business_logic_syscall_handler.rs +++ b/src/syscalls/business_logic_syscall_handler.rs @@ -281,6 +281,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { let ExecutionResult { call_info, @@ -296,6 +297,8 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, self.block_context.invoke_tx_max_n_steps, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) .map_err(|err| SyscallHandlerError::ExecutionError(err.to_string()))?; @@ -368,6 +371,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { let compiled_class = if let Ok(compiled_class) = self .starknet_storage_state @@ -423,6 +427,8 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, self.block_context.invoke_tx_max_n_steps, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) .map_err(|_| StateError::ExecutionEntryPoint)?; @@ -448,6 +454,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result<(), SyscallHandlerError> { let selector = get_big_int(vm, syscall_ptr)?; let syscall_name = self.selector_to_syscall.get(&selector).ok_or( @@ -495,6 +502,8 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, vm, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )? }; @@ -516,6 +525,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { match request { SyscallRequest::LibraryCall(req) => self.library_call( @@ -524,6 +534,8 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, remaining_gas, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ), SyscallRequest::CallContract(req) => self.call_contract( vm, @@ -531,6 +543,8 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, remaining_gas, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ), SyscallRequest::Deploy(req) => self.deploy( vm, @@ -538,6 +552,8 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, remaining_gas, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ), SyscallRequest::StorageRead(req) => self.storage_read(vm, req, remaining_gas), SyscallRequest::StorageWrite(req) => self.storage_write(vm, req, remaining_gas), @@ -796,6 +812,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { let calldata = get_felt_range(vm, request.calldata_start, request.calldata_end)?; let execution_entry_point = ExecutionEntryPoint::new( @@ -815,6 +832,8 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, execution_entry_point, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) } @@ -858,6 +877,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result<(Address, CallResult), SyscallHandlerError> { if !(request.deploy_from_zero.is_zero() || request.deploy_from_zero.is_one()) { return Err(SyscallHandlerError::DeployFromZero( @@ -910,6 +930,8 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, remaining_gas, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?; Ok((contract_address, result)) @@ -924,6 +946,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { let (contract_address, result) = self.syscall_deploy( vm, @@ -931,6 +954,8 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, remaining_gas, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?; remaining_gas -= result.gas_consumed; @@ -1049,6 +1074,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { let calldata = get_felt_range(vm, request.calldata_start, request.calldata_end)?; let class_hash = ClassHash::from(request.class_hash); @@ -1069,6 +1095,8 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, execution_entry_point, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) } diff --git a/src/syscalls/deprecated_business_logic_syscall_handler.rs b/src/syscalls/deprecated_business_logic_syscall_handler.rs index 1582d974b..31693777a 100644 --- a/src/syscalls/deprecated_business_logic_syscall_handler.rs +++ b/src/syscalls/deprecated_business_logic_syscall_handler.rs @@ -238,6 +238,7 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result<(), StateError> { let contract_class = self .starknet_storage_state @@ -282,6 +283,8 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S self.block_context.invoke_tx_max_n_steps, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) .map_err(|_| StateError::ExecutionEntryPoint)?; @@ -341,6 +344,7 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { let request = match self.read_and_validate_syscall_request("deploy", vm, syscall_ptr)? { DeprecatedSyscallRequest::Deploy(request) => request, @@ -395,6 +399,8 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S constructor_calldata, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?; Ok(deploy_contract_address) } @@ -408,6 +414,7 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result, SyscallHandlerError> { let request = self.read_and_validate_syscall_request(syscall_name, vm, syscall_ptr)?; @@ -510,6 +517,8 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S self.block_context.invoke_tx_max_n_steps, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) .map_err(|e| SyscallHandlerError::ExecutionError(e.to_string()))?; @@ -555,6 +564,7 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result<(), SyscallHandlerError> { self.call_contract_and_write_response( "delegate_l1_handler", @@ -562,6 +572,8 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S syscall_ptr, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) } @@ -694,12 +706,15 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result<(), SyscallHandlerError> { let contract_address = self.syscall_deploy( vm, syscall_ptr, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?; let response = DeprecatedDeployResponse::new( @@ -724,6 +739,7 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result<(), SyscallHandlerError> { let retdata = self.syscall_call_contract( syscall_name, @@ -731,6 +747,8 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S syscall_ptr, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?; let retdata_maybe_reloc = retdata @@ -885,6 +903,7 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result<(), SyscallHandlerError> { self.call_contract_and_write_response( "library_call", @@ -892,6 +911,8 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S syscall_ptr, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) } @@ -903,6 +924,7 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result<(), SyscallHandlerError> { self.call_contract_and_write_response( "library_call_l1_handler", @@ -910,6 +932,8 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S syscall_ptr, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) } @@ -921,6 +945,7 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result<(), SyscallHandlerError> { self.call_contract_and_write_response( "call_contract", @@ -928,6 +953,8 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S syscall_ptr, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) } @@ -939,6 +966,7 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result<(), SyscallHandlerError> { self.call_contract_and_write_response( "delegate_call", @@ -946,6 +974,8 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedBLSyscallHandler<'a, S syscall_ptr, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) } @@ -1138,6 +1168,8 @@ mod tests { relocatable!(1, 0), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None ), Err(SyscallHandlerError::DeployFromZero(4)) ) diff --git a/src/syscalls/deprecated_syscall_handler.rs b/src/syscalls/deprecated_syscall_handler.rs index cc172df4c..68baff273 100644 --- a/src/syscalls/deprecated_syscall_handler.rs +++ b/src/syscalls/deprecated_syscall_handler.rs @@ -88,6 +88,8 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedSyscallHintProcessor<' // TODO: Get the program_cache somehow. #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) } EMIT_EVENT_CODE => { @@ -118,6 +120,8 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedSyscallHintProcessor<' // TODO: Get the program_cache somehow. #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) } LIBRARY_CALL_L1_HANDLER => { @@ -128,6 +132,8 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedSyscallHintProcessor<' // TODO: Get the program_cache somehow. #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) } CALL_CONTRACT => { @@ -138,6 +144,8 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedSyscallHintProcessor<' // TODO: Get the program_cache somehow. #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) } STORAGE_READ => { @@ -172,6 +180,8 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedSyscallHintProcessor<' // TODO: Get the program_cache somehow. #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) } DELEGATE_L1_HANDLER => { @@ -182,6 +192,8 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedSyscallHintProcessor<' // TODO: Get the program_cache somehow. #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) } REPLACE_CLASS => { @@ -1252,6 +1264,8 @@ mod tests { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); diff --git a/src/syscalls/native_syscall_handler.rs b/src/syscalls/native_syscall_handler.rs index 0db0fb5e7..97696806c 100644 --- a/src/syscalls/native_syscall_handler.rs +++ b/src/syscalls/native_syscall_handler.rs @@ -8,6 +8,7 @@ use crate::{ TransactionExecutionContext, }, hash_utils::calculate_contract_address, + sandboxing::IsolatedExecutor, services::api::{ contract_class_errors::ContractClassError, contract_classes::compiled_class::CompiledClass, }, @@ -49,6 +50,7 @@ where pub(crate) block_context: BlockContext, pub(crate) internal_calls: Vec, pub(crate) program_cache: Rc>>, + pub(crate) sandbox: Option<&'a IsolatedExecutor>, } impl<'a, 'cache, S: StateReader, C: ContractClassCache> NativeSyscallHandler<'a, 'cache, S, C> { @@ -134,6 +136,13 @@ impl<'a, 'cache, S: StateReader, C: ContractClassCache> StarkNetSyscallHandler }) } + fn get_execution_info_v2( + &mut self, + _remaining_gas: &mut u128, + ) -> SyscallResult { + todo!() + } + fn deploy( &mut self, class_hash: Felt252, @@ -254,6 +263,7 @@ impl<'a, 'cache, S: StateReader, C: ContractClassCache> StarkNetSyscallHandler false, self.block_context.invoke_tx_max_n_steps, Some(self.program_cache.clone()), + self.sandbox, )?; let call_info = call_info.ok_or(SyscallHandlerError::ExecutionError( @@ -318,6 +328,7 @@ impl<'a, 'cache, S: StateReader, C: ContractClassCache> StarkNetSyscallHandler false, self.block_context.invoke_tx_max_n_steps, Some(self.program_cache.clone()), + self.sandbox, ) .unwrap(); @@ -669,6 +680,7 @@ where false, u64::MAX, Some(self.program_cache.clone()), + self.sandbox, ) .map_err(|_| StateError::ExecutionEntryPoint)?; diff --git a/src/syscalls/syscall_handler.rs b/src/syscalls/syscall_handler.rs index bb769bdfa..9818b9933 100644 --- a/src/syscalls/syscall_handler.rs +++ b/src/syscalls/syscall_handler.rs @@ -78,6 +78,8 @@ impl<'a, S: StateReader, C: ContractClassCache> HintProcessorLogic // TODO: Get the program_cache somehow. #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .map_err(|err| { HintError::CustomHint( diff --git a/src/transaction/declare.rs b/src/transaction/declare.rs index ca6671bb4..87b20ab61 100644 --- a/src/transaction/declare.rs +++ b/src/transaction/declare.rs @@ -194,6 +194,7 @@ impl Declare { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { // validate transaction let mut resources_manager = ExecutionResourcesManager::default(); @@ -206,6 +207,8 @@ impl Declare { &mut resources_manager, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )? }; let changes = state.count_actual_state_changes(Some(( @@ -260,6 +263,7 @@ impl Declare { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result, TransactionError> { if self.version.is_zero() { return Ok(None); @@ -287,6 +291,8 @@ impl Declare { block_context.validate_max_n_steps, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?; let call_info = call_info.ok_or(TransactionError::CallInfoIsNone)?; @@ -365,7 +371,7 @@ impl Declare { /// Calculates actual fee used by the transaction using the execution /// info returned by apply(), then updates the transaction execution info with the data of the fee. - #[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context, program_cache), fields( + #[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context, program_cache, sandbox), fields( tx_type = ?TransactionType::Declare, self.version = ?self.version, self.class_hash = ?self.class_hash, @@ -380,6 +386,7 @@ impl Declare { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { if self.version != Felt252::ONE && self.version != Felt252::ZERO { return Err(TransactionError::UnsupportedTxVersion( @@ -400,6 +407,8 @@ impl Declare { block_context, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox, )?; let mut tx_execution_context = @@ -413,6 +422,8 @@ impl Declare { self.skip_fee_transfer, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?; state.set_contract_class( @@ -591,6 +602,8 @@ mod tests { &BlockContext::default(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(), transaction_exec_info @@ -670,6 +683,8 @@ mod tests { &BlockContext::default(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -681,6 +696,8 @@ mod tests { &BlockContext::default(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -749,6 +766,8 @@ mod tests { &BlockContext::default(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -757,6 +776,8 @@ mod tests { &BlockContext::default(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); // --------------------- @@ -802,6 +823,8 @@ mod tests { &BlockContext::default(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert!(internal_declare_error.is_err()); @@ -874,6 +897,8 @@ mod tests { &BlockContext::default(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ), Err(TransactionError::MaxFeeExceedsBalance(_, _, _)) ); @@ -977,6 +1002,8 @@ mod tests { &bock_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap() .actual_fee @@ -987,6 +1014,8 @@ mod tests { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap() .actual_fee, @@ -1016,6 +1045,8 @@ mod tests { &BlockContext::default(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert_matches!( diff --git a/src/transaction/declare_v2.rs b/src/transaction/declare_v2.rs index ae7e0e7c5..1a1a26e71 100644 --- a/src/transaction/declare_v2.rs +++ b/src/transaction/declare_v2.rs @@ -354,7 +354,7 @@ impl DeclareV2 { /// ## Parameter: /// - state: An state that implements the State and StateReader traits. /// - block_context: The block that contains the execution context - #[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context, program_cache), fields( + #[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context, program_cache, sandbox), fields( tx_type = ?TransactionType::Declare, self.version = ?self.version, self.sierra_class_hash = ?self.sierra_class_hash, @@ -370,6 +370,7 @@ impl DeclareV2 { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { if self.version != 2.into() { return Err(TransactionError::UnsupportedTxVersion( @@ -397,6 +398,8 @@ impl DeclareV2 { INITIAL_GAS_COST, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox, )? }; self.compile_and_store_casm_class(state)?; @@ -429,6 +432,8 @@ impl DeclareV2 { self.skip_fee_transfer, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?; let mut tx_exec_info = TransactionExecutionInfo::new_without_fee_info( @@ -502,6 +507,7 @@ impl DeclareV2 { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { let calldata = [self.compiled_class_hash].to_vec(); @@ -532,6 +538,8 @@ impl DeclareV2 { block_context.validate_max_n_steps, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )? }; @@ -1005,6 +1013,8 @@ mod tests { &BlockContext::default(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert_matches!( diff --git a/src/transaction/deploy.rs b/src/transaction/deploy.rs index 37ef553aa..4bcf542be 100644 --- a/src/transaction/deploy.rs +++ b/src/transaction/deploy.rs @@ -162,6 +162,7 @@ impl Deploy { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { state.set_contract_class(&self.contract_hash, &self.contract_class)?; state.deploy_contract(self.contract_address.clone(), self.contract_hash)?; @@ -175,6 +176,8 @@ impl Deploy { block_context, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) } } @@ -229,6 +232,7 @@ impl Deploy { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { let call = ExecutionEntryPoint::new( self.contract_address.clone(), @@ -265,6 +269,8 @@ impl Deploy { block_context.validate_max_n_steps, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?; let changes = state.count_actual_state_changes(None)?; @@ -291,7 +297,7 @@ impl Deploy { /// ## Parameters /// - state: A state that implements the [`State`] and [`StateReader`] traits. /// - block_context: The block's execution context. - #[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context, program_cache), fields( + #[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context, program_cache, sandbox), fields( tx_type = ?TransactionType::Deploy, self.version = ?self.version, self.contract_hash = ?self.contract_hash, @@ -306,12 +312,15 @@ impl Deploy { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { let mut tx_exec_info = self.apply( state, block_context, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?; let (fee_transfer_info, actual_fee) = (None, 0); tx_exec_info.set_fee_info(actual_fee, fee_transfer_info); @@ -385,6 +394,8 @@ mod tests { &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -442,6 +453,8 @@ mod tests { &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert_matches!(result.unwrap_err(), TransactionError::CairoRunner(..)) } @@ -484,6 +497,8 @@ mod tests { &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert_matches!( result.unwrap_err(), diff --git a/src/transaction/deploy_account.rs b/src/transaction/deploy_account.rs index 4dc39198e..dfacd47e4 100644 --- a/src/transaction/deploy_account.rs +++ b/src/transaction/deploy_account.rs @@ -180,7 +180,7 @@ impl DeployAccount { } } - #[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context, program_cache), fields( + #[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context, program_cache, sandbox), fields( tx_type = ?TransactionType::DeployAccount, self.version = ?self.version, self.class_hash = ?self.class_hash, @@ -196,6 +196,7 @@ impl DeployAccount { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { if self.version != Felt252::ONE { return Err(TransactionError::UnsupportedTxVersion( @@ -217,6 +218,8 @@ impl DeployAccount { block_context, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox, ); #[cfg(feature = "replay_benchmark")] // Add initial values to cache despite tx outcome @@ -270,6 +273,8 @@ impl DeployAccount { self.skip_fee_transfer, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?; tx_exec_info.set_fee_info(actual_fee, fee_transfer_info); @@ -302,6 +307,7 @@ impl DeployAccount { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { let contract_class = state.get_contract_class(&self.class_hash)?; @@ -315,6 +321,8 @@ impl DeployAccount { &mut resources_manager, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox, )?; let validate_info = if self.skip_validate { @@ -326,6 +334,8 @@ impl DeployAccount { &mut resources_manager, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )? }; @@ -364,6 +374,7 @@ impl DeployAccount { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { if self.constructor_entry_points_empty(contract_class)? { if !self.constructor_calldata.is_empty() { @@ -382,6 +393,8 @@ impl DeployAccount { resources_manager, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) } } @@ -461,6 +474,7 @@ impl DeployAccount { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { let entry_point = ExecutionEntryPoint::new( self.contract_address.clone(), @@ -485,6 +499,8 @@ impl DeployAccount { block_context.validate_max_n_steps, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )? }; @@ -513,6 +529,7 @@ impl DeployAccount { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result, TransactionError> { let call = ExecutionEntryPoint::new( self.contract_address.clone(), @@ -543,6 +560,8 @@ impl DeployAccount { block_context.validate_max_n_steps, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )? }; @@ -748,6 +767,8 @@ mod tests { &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_matches!( @@ -757,6 +778,8 @@ mod tests { &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap_err(), TransactionError::State(StateError::ContractAddressUnavailable(..)) @@ -801,6 +824,8 @@ mod tests { &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); } @@ -826,6 +851,8 @@ mod tests { &BlockContext::default(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert_matches!( diff --git a/src/transaction/fee.rs b/src/transaction/fee.rs index 30dac5642..cf5163d6c 100644 --- a/src/transaction/fee.rs +++ b/src/transaction/fee.rs @@ -38,6 +38,7 @@ pub(crate) fn execute_fee_transfer( #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { if actual_fee > tx_execution_context.account_tx_fields.max_fee() { return Err(TransactionError::ActualFeeExceedsMaxFee( @@ -77,6 +78,8 @@ pub(crate) fn execute_fee_transfer( block_context.invoke_tx_max_n_steps, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) .map_err(|e| TransactionError::FeeTransferError(Box::new(e)))?; @@ -152,6 +155,7 @@ fn max_of_keys(cairo_rsc: &HashMap, weights: &HashMap( state: &mut CachedState, resources: &HashMap, @@ -162,6 +166,7 @@ pub fn charge_fee( #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { if max_fee.is_zero() { return Ok((None, 0)); @@ -192,6 +197,8 @@ pub fn charge_fee( actual_fee, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?) }; @@ -238,6 +245,8 @@ mod tests { skip_fee_transfer, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -274,6 +283,8 @@ mod tests { skip_fee_transfer, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); diff --git a/src/transaction/invoke_function.rs b/src/transaction/invoke_function.rs index 5beb4f0cd..c38768de4 100644 --- a/src/transaction/invoke_function.rs +++ b/src/transaction/invoke_function.rs @@ -188,6 +188,7 @@ impl InvokeFunction { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result, TransactionError> { if self.version.is_zero() || self.skip_validation { return Ok(None); @@ -213,6 +214,8 @@ impl InvokeFunction { block_context.validate_max_n_steps, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?; // Validate the return data @@ -255,6 +258,7 @@ impl InvokeFunction { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { let call = ExecutionEntryPoint::new( self.contract_address.clone(), @@ -275,6 +279,8 @@ impl InvokeFunction { block_context.invoke_tx_max_n_steps, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ) } @@ -292,6 +298,7 @@ impl InvokeFunction { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { let mut resources_manager = ExecutionResourcesManager::default(); let validate_info = if self.skip_validation { @@ -304,6 +311,8 @@ impl InvokeFunction { remaining_gas, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox, )? }; @@ -326,6 +335,8 @@ impl InvokeFunction { remaining_gas, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )? }; let changes = state.count_actual_state_changes(Some(( @@ -359,7 +370,7 @@ impl InvokeFunction { /// - state: A state that implements the [`State`] and [`StateReader`] traits. /// - block_context: The block's execution context. /// - remaining_gas: The amount of gas that the transaction disposes. - #[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context, program_cache), fields( + #[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context, program_cache, sandbox), fields( tx_type = ?TransactionType::InvokeFunction, self.version = ?self.version, self.hash_value = ?self.hash_value, @@ -375,6 +386,7 @@ impl InvokeFunction { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { if self.version != Felt252::ONE && self.version != Felt252::ZERO { return Err(TransactionError::UnsupportedTxVersion( @@ -398,6 +410,8 @@ impl InvokeFunction { remaining_gas, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox, ); #[cfg(feature = "replay_benchmark")] // Add initial values to cache despite tx outcome @@ -467,6 +481,8 @@ impl InvokeFunction { self.skip_fee_transfer, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )?; tx_exec_info.set_fee_info(actual_fee, fee_transfer_info); @@ -859,6 +875,8 @@ mod tests { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); state @@ -939,6 +957,8 @@ mod tests { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -1013,6 +1033,8 @@ mod tests { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert!(expected_error.is_err()); @@ -1082,6 +1104,8 @@ mod tests { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); state @@ -1160,6 +1184,8 @@ mod tests { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert!(expected_error.is_err()); @@ -1228,6 +1254,8 @@ mod tests { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert!(result.is_err()); assert_matches!( @@ -1296,6 +1324,8 @@ mod tests { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); let expected_actual_fee = 1258; @@ -1366,6 +1396,8 @@ mod tests { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -1375,6 +1407,8 @@ mod tests { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert!(expected_error.is_err()); @@ -1441,6 +1475,8 @@ mod tests { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert!(expected_error.is_err()); @@ -1584,6 +1620,8 @@ mod tests { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -1636,6 +1674,8 @@ mod tests { u128::MAX, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert_matches!( diff --git a/src/transaction/l1_handler.rs b/src/transaction/l1_handler.rs index 232ac7ba9..a1ff34248 100644 --- a/src/transaction/l1_handler.rs +++ b/src/transaction/l1_handler.rs @@ -104,7 +104,7 @@ impl L1Handler { } /// Applies self to 'state' by executing the L1-handler entry point. - #[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context, program_cache), fields( + #[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context, program_cache, sandbox), fields( tx_type = ?TransactionType::L1Handler, self.hash_value = ?self.hash_value, self.contract_address = ?self.contract_address, @@ -119,6 +119,7 @@ impl L1Handler { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { let mut resources_manager = ExecutionResourcesManager::default(); let entrypoint = ExecutionEntryPoint::new( @@ -148,6 +149,8 @@ impl L1Handler { block_context.invoke_tx_max_n_steps, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, )? }; @@ -327,6 +330,8 @@ mod test { 100000, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); diff --git a/src/transaction/mod.rs b/src/transaction/mod.rs index dcd95633f..8ebba9a9a 100644 --- a/src/transaction/mod.rs +++ b/src/transaction/mod.rs @@ -83,6 +83,7 @@ impl Transaction { #[cfg(feature = "cairo-native")] program_cache: Option< Rc>>, >, + #[cfg(feature = "cairo-native")] sandbox: Option<&crate::sandboxing::IsolatedExecutor>, ) -> Result { match self { Transaction::Declare(tx) => tx.execute( @@ -90,24 +91,32 @@ impl Transaction { block_context, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ), Transaction::DeclareV2(tx) => tx.execute( state, block_context, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ), Transaction::Deploy(tx) => tx.execute( state, block_context, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ), Transaction::DeployAccount(tx) => tx.execute( state, block_context, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ), Transaction::InvokeFunction(tx) => tx.execute( state, @@ -115,6 +124,8 @@ impl Transaction { remaining_gas, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ), Transaction::L1Handler(tx) => tx.execute( state, @@ -122,6 +133,8 @@ impl Transaction { remaining_gas, #[cfg(feature = "cairo-native")] program_cache, + #[cfg(feature = "cairo-native")] + sandbox, ), } } diff --git a/tests/account_panic.rs b/tests/account_panic.rs index 0c19adbbb..966873cbb 100644 --- a/tests/account_panic.rs +++ b/tests/account_panic.rs @@ -119,6 +119,8 @@ fn account_panic() { u128::MAX, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .expect("failed to invoke"); let call_info = exec_info.call_info.as_ref().unwrap(); diff --git a/tests/cairo_1_syscalls.rs b/tests/cairo_1_syscalls.rs index 34f05ee67..0115a871b 100644 --- a/tests/cairo_1_syscalls.rs +++ b/tests/cairo_1_syscalls.rs @@ -125,6 +125,8 @@ fn storage_write_read() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -150,6 +152,8 @@ fn storage_write_read() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!(call_info.call_info.unwrap().retdata, [25.into()]); @@ -176,6 +180,8 @@ fn storage_write_read() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -201,6 +207,8 @@ fn storage_write_read() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!(call_info.call_info.unwrap().retdata, [125.into()]) @@ -367,6 +375,8 @@ fn library_call() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap() .call_info @@ -503,6 +513,8 @@ fn call_contract_storage_write_read() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -528,6 +540,8 @@ fn call_contract_storage_write_read() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!(call_info.call_info.unwrap().retdata, [25.into()]); @@ -554,6 +568,8 @@ fn call_contract_storage_write_read() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -579,6 +595,8 @@ fn call_contract_storage_write_read() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!(call_info.call_info.unwrap().retdata, [125.into()]) @@ -658,6 +676,8 @@ fn emit_event() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!( @@ -787,6 +807,8 @@ fn deploy_cairo1_from_cairo1() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert!(call_info.is_ok()); @@ -898,6 +920,8 @@ fn deploy_cairo0_from_cairo1_without_constructor() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert!(call_info.is_ok()); @@ -1008,6 +1032,8 @@ fn deploy_cairo0_from_cairo1_with_constructor() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert!(call_info.is_ok()); @@ -1120,6 +1146,8 @@ fn deploy_cairo0_and_invoke() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert!(call_info.is_ok()); @@ -1168,6 +1196,8 @@ fn deploy_cairo0_and_invoke() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -1248,6 +1278,8 @@ fn test_send_message_to_l1_syscall() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -1359,6 +1391,8 @@ fn test_get_execution_info() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -1495,6 +1529,8 @@ fn replace_class_internal() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); // Check that the class was indeed replaced in storage @@ -1640,6 +1676,8 @@ fn replace_class_contract_call() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!(result.call_info.unwrap().retdata, vec![25.into()]); @@ -1669,6 +1707,8 @@ fn replace_class_contract_call() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -1697,6 +1737,8 @@ fn replace_class_contract_call() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!(result.call_info.clone().unwrap().retdata, vec![17.into()]); @@ -1831,6 +1873,8 @@ fn replace_class_contract_call_same_transaction() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!( @@ -1962,6 +2006,8 @@ fn call_contract_upgrade_cairo_0_to_cairo_1_same_transaction() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!( @@ -2091,6 +2137,8 @@ fn call_contract_downgrade_cairo_1_to_cairo_0_same_transaction() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!( @@ -2213,6 +2261,8 @@ fn call_contract_replace_class_cairo_0() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!( @@ -2296,6 +2346,8 @@ fn test_out_of_gas_failure() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); let call_info = call_info.call_info.unwrap(); @@ -2380,6 +2432,8 @@ fn deploy_syscall_failure_uninitialized_class_hash() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!( @@ -2479,6 +2533,8 @@ fn deploy_syscall_failure_in_constructor() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); // Check that we get the error from the constructor @@ -2562,6 +2618,8 @@ fn storage_read_no_value() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); // As the value doesn't exist in storage, it's value will be 0 @@ -2641,6 +2699,8 @@ fn storage_read_unavailable_address_domain() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -2723,6 +2783,8 @@ fn storage_write_unavailable_address_domain() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -2838,6 +2900,8 @@ fn library_call_failure() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -2956,6 +3020,8 @@ fn send_messages_to_l1_different_contract_calls() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); let l1_to_l2_messages = call_info @@ -3084,6 +3150,8 @@ fn send_messages_to_l1_different_contract_calls_cairo1_to_cairo0() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); let l1_to_l2_messages = call_info @@ -3210,6 +3278,8 @@ fn send_messages_to_l1_different_contract_calls_cairo0_to_cairo1() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); let l1_to_l2_messages = call_info @@ -3308,6 +3378,8 @@ fn keccak_syscall() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -3433,6 +3505,8 @@ fn library_call_recursive_50_calls() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap() .call_info @@ -3593,6 +3667,8 @@ fn call_contract_storage_write_read_recursive_50_calls() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -3618,6 +3694,8 @@ fn call_contract_storage_write_read_recursive_50_calls() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!(call_info.call_info.unwrap().retdata, [25.into()]); @@ -3644,6 +3722,8 @@ fn call_contract_storage_write_read_recursive_50_calls() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap() .call_info @@ -3674,6 +3754,8 @@ fn call_contract_storage_write_read_recursive_50_calls() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!(call_info.call_info.unwrap().retdata, [75.into()]) @@ -3808,6 +3890,8 @@ fn call_contract_storage_write_read_recursive_100_calls() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -3833,6 +3917,8 @@ fn call_contract_storage_write_read_recursive_100_calls() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!(call_info.call_info.unwrap().retdata, [25.into()]); @@ -3859,6 +3945,8 @@ fn call_contract_storage_write_read_recursive_100_calls() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap() .call_info @@ -3889,6 +3977,8 @@ fn call_contract_storage_write_read_recursive_100_calls() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); assert_eq!(call_info.call_info.unwrap().retdata, [125.into()]) diff --git a/tests/cairo_native.rs b/tests/cairo_native.rs index 8a53e204b..9dd94e8bf 100644 --- a/tests/cairo_native.rs +++ b/tests/cairo_native.rs @@ -979,6 +979,8 @@ impl TestState { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, )?; // Overwrite the execution result's execution_resources as native doesn't output it if let Some(callinfo) = execution_result_vm.call_info.as_mut() { @@ -1015,6 +1017,8 @@ impl TestState { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, )?; Ok((execution_result_vm, execution_result_native)) diff --git a/tests/complex_contracts/erc20.rs b/tests/complex_contracts/erc20.rs index 4177dd238..e5b9ae7d2 100644 --- a/tests/complex_contracts/erc20.rs +++ b/tests/complex_contracts/erc20.rs @@ -122,6 +122,8 @@ fn test_erc20_cairo2() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); let erc20_address = *call_info.call_info.unwrap().retdata.get(0).unwrap(); @@ -180,6 +182,8 @@ fn test_erc20_cairo2() { &Default::default(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .expect("failed to execute internal_deploy_account") .validate_info @@ -237,6 +241,8 @@ fn test_erc20_cairo2() { &Default::default(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap() .validate_info @@ -256,6 +262,8 @@ fn test_erc20_cairo2() { account_address_1.clone(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -272,6 +280,8 @@ fn test_erc20_cairo2() { account_address_1.clone(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -288,6 +298,8 @@ fn test_erc20_cairo2() { account_address_1, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); diff --git a/tests/complex_contracts/utils.rs b/tests/complex_contracts/utils.rs index 84f2369cd..f6390ba5f 100644 --- a/tests/complex_contracts/utils.rs +++ b/tests/complex_contracts/utils.rs @@ -137,6 +137,8 @@ pub fn execute_entry_point( call_config.block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, )?; Ok(call_info.unwrap()) @@ -178,6 +180,8 @@ pub fn deploy( block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, )?; let call_info = tx_execution_info.call_info.unwrap(); diff --git a/tests/delegate_call.rs b/tests/delegate_call.rs index 578c835b6..e300d6262 100644 --- a/tests/delegate_call.rs +++ b/tests/delegate_call.rs @@ -129,6 +129,8 @@ fn delegate_call() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .is_ok()); } diff --git a/tests/delegate_l1_handler.rs b/tests/delegate_l1_handler.rs index 80156bb4b..3372ef604 100644 --- a/tests/delegate_l1_handler.rs +++ b/tests/delegate_l1_handler.rs @@ -124,6 +124,8 @@ fn delegate_l1_handler() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .is_ok()); } diff --git a/tests/deploy_account.rs b/tests/deploy_account.rs index 90e870a54..7f809d972 100644 --- a/tests/deploy_account.rs +++ b/tests/deploy_account.rs @@ -81,6 +81,8 @@ fn internal_deploy_account() { &Default::default(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -196,6 +198,8 @@ fn internal_deploy_account_cairo1() { &Default::default(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); diff --git a/tests/fibonacci.rs b/tests/fibonacci.rs index aa7eb506b..8bbd20051 100644 --- a/tests/fibonacci.rs +++ b/tests/fibonacci.rs @@ -135,6 +135,8 @@ fn integration_test() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap() .call_info @@ -239,6 +241,8 @@ fn integration_test_cairo1() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap() .call_info diff --git a/tests/increase_balance.rs b/tests/increase_balance.rs index a3a3cae8b..35838bbf9 100644 --- a/tests/increase_balance.rs +++ b/tests/increase_balance.rs @@ -142,6 +142,8 @@ fn hello_starknet_increase_balance() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap() .call_info diff --git a/tests/internal_calls.rs b/tests/internal_calls.rs index 846058cf3..02ae67ee4 100644 --- a/tests/internal_calls.rs +++ b/tests/internal_calls.rs @@ -85,6 +85,8 @@ fn test_internal_calls() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .expect("Could not execute contract"); diff --git a/tests/internals.rs b/tests/internals.rs index 0375dac72..067ca6814 100644 --- a/tests/internals.rs +++ b/tests/internals.rs @@ -1057,6 +1057,8 @@ fn test_declare_tx() { &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); // Check ContractClass is set after the declare_tx @@ -1154,6 +1156,8 @@ fn test_declarev2_tx() { &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); // Check ContractClass is set after the declare_tx @@ -1442,6 +1446,8 @@ fn test_invoke_tx() { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); let expected_execution_info = expected_transaction_execution_info(block_context); @@ -1472,6 +1478,8 @@ fn test_invoke_tx_exceeded_max_fee() { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); let mut expected_result = expected_transaction_execution_info(block_context).to_revert_error( @@ -1567,6 +1575,8 @@ fn test_invoke_tx_state() { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -1642,6 +1652,8 @@ fn test_invoke_with_declarev2_tx() { block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -1653,6 +1665,8 @@ fn test_invoke_with_declarev2_tx() { block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -1675,6 +1689,8 @@ fn test_invoke_with_declarev2_tx() { expected_gas_consumed, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -1732,6 +1748,8 @@ fn test_deploy_account() { &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -1859,6 +1877,8 @@ fn test_deploy_account_revert() { &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -2159,6 +2179,8 @@ fn test_state_for_declare_tx() { &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .is_ok()); assert_eq!( @@ -2347,6 +2369,8 @@ fn test_invoke_tx_wrong_call_data() { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); // Assert error @@ -2392,6 +2416,8 @@ fn test_invoke_tx_wrong_entrypoint() { 0, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); // Assert error @@ -2425,6 +2451,8 @@ fn test_deploy_undeclared_account() { &block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); // Execute transaction @@ -2446,6 +2474,8 @@ fn test_library_call_with_declare_v2() { block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -2457,6 +2487,8 @@ fn test_library_call_with_declare_v2() { block_context, #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -2558,6 +2590,8 @@ fn test_library_call_with_declare_v2() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); diff --git a/tests/multi_syscall_test.rs b/tests/multi_syscall_test.rs index 18574b267..d1245dbc0 100644 --- a/tests/multi_syscall_test.rs +++ b/tests/multi_syscall_test.rs @@ -260,6 +260,8 @@ fn test_syscall( block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap() .call_info diff --git a/tests/storage.rs b/tests/storage.rs index 656e281eb..103b9df09 100644 --- a/tests/storage.rs +++ b/tests/storage.rs @@ -140,6 +140,8 @@ fn integration_storage_test() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap() .call_info diff --git a/tests/syscalls.rs b/tests/syscalls.rs index 7b537b0e8..4a5afb356 100644 --- a/tests/syscalls.rs +++ b/tests/syscalls.rs @@ -155,6 +155,8 @@ fn test_contract<'a>( block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .expect("Could not execute contract") .call_info @@ -1212,6 +1214,8 @@ fn deploy_cairo1_from_cairo0_with_constructor() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert!(call_info.is_ok()); @@ -1323,6 +1327,8 @@ fn deploy_cairo1_from_cairo0_without_constructor() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap(); @@ -1434,6 +1440,8 @@ fn deploy_cairo1_and_invoke() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert!(call_info.is_ok()); @@ -1478,6 +1486,8 @@ fn deploy_cairo1_and_invoke() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap() .call_info @@ -1579,6 +1589,8 @@ fn send_messages_to_l1_different_contract_calls() { block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ) .unwrap() .call_info diff --git a/tests/syscalls_errors.rs b/tests/syscalls_errors.rs index ba91ccb14..02654bd2a 100644 --- a/tests/syscalls_errors.rs +++ b/tests/syscalls_errors.rs @@ -132,6 +132,8 @@ fn test_contract<'a>( block_context.invoke_tx_max_n_steps(), #[cfg(feature = "cairo-native")] None, + #[cfg(feature = "cairo-native")] + None, ); assert_matches!(result, Err(e) if e.to_string().contains(error_msg)); diff --git a/tests/yas_bench_test.rs b/tests/yas_bench_test.rs index 1ebe97998..45855c1bc 100644 --- a/tests/yas_bench_test.rs +++ b/tests/yas_bench_test.rs @@ -328,7 +328,7 @@ where vec![], nonce, )? - .execute(state, &BlockContext::default(), program_cache)?; + .execute(state, &BlockContext::default(), program_cache, None)?; // Ensure the execution was successful. let validate_info = tx_execution_info.validate_info.unwrap(); @@ -364,7 +364,7 @@ where vec![], nonce, )? - .execute(state, &BlockContext::default(), program_cache)?; + .execute(state, &BlockContext::default(), program_cache, None)?; // Ensure the execution was successful. let validate_info = tx_execution_info.validate_info.unwrap(); @@ -400,7 +400,7 @@ where vec![], nonce, )? - .execute(state, &BlockContext::default(), program_cache)?; + .execute(state, &BlockContext::default(), program_cache, None)?; // Ensure the execution was successful. let validate_info = tx_execution_info.validate_info.unwrap(); @@ -436,7 +436,7 @@ where vec![], nonce, )? - .execute(state, &BlockContext::default(), program_cache)?; + .execute(state, &BlockContext::default(), program_cache, None)?; // Ensure the execution was successful. let validate_info = tx_execution_info.validate_info.unwrap(); @@ -487,6 +487,7 @@ where &BlockContext::default(), u64::MAX.into(), program_cache, + None, )?; // Ensure the execution was successful. @@ -533,6 +534,7 @@ where &BlockContext::default(), u64::MAX.into(), program_cache, + None, )?; // Ensure the execution was successful. @@ -571,6 +573,7 @@ where &BlockContext::default(), u64::MAX.into(), program_cache, + None, )?; // Ensure the execution was successful. @@ -625,6 +628,7 @@ where &BlockContext::default(), u64::MAX.into(), program_cache, + None, )?; // Ensure the execution was successful. @@ -675,6 +679,7 @@ where &BlockContext::default(), u64::MAX.into(), program_cache, + None, )?; // Ensure the execution was successful. @@ -723,6 +728,7 @@ where &BlockContext::default(), u64::MAX.into(), program_cache, + None, )?; // Ensure the execution was successful. @@ -780,6 +786,7 @@ where &BlockContext::default(), u64::MAX.into(), program_cache, + None, )?; // Ensure the execution was successful. @@ -839,6 +846,7 @@ where &BlockContext::default(), u64::MAX.into(), program_cache, + None, )?; // Ensure the execution was successful. @@ -886,6 +894,7 @@ where &BlockContext::default(), u64::MAX.into(), program_cache, + None, )?; // Ensure the execution was successful. From a8ab7aa34926600c1d52421043042bd88f858df6 Mon Sep 17 00:00:00 2001 From: Edgar Date: Mon, 29 Jan 2024 11:41:57 -0300 Subject: [PATCH 02/15] works --- .gitignore | 4 +- Cargo.toml | 4 + examples/sandbox/main.rs | 121 +++++++++++++++++++++++++ src/bin/native_executor.rs | 14 +-- src/execution/execution_entry_point.rs | 103 ++++++++++++--------- 5 files changed, 194 insertions(+), 52 deletions(-) create mode 100644 examples/sandbox/main.rs diff --git a/.gitignore b/.gitignore index abb8b256d..19d254e3a 100644 --- a/.gitignore +++ b/.gitignore @@ -59,4 +59,6 @@ lcov.info .rusty-hook.toml !/starknet_programs/raw_contract_classes/*.json cairo-*.tar -starknet-pypy-env/ \ No newline at end of file +starknet-pypy-env/ + +executor_logs/ diff --git a/Cargo.toml b/Cargo.toml index 51a0244c2..d90a3f5f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -102,6 +102,10 @@ path = "bench/yas.rs" name = "yas" harness = false +[[example]] +name = "sandbox" +required-features = ["cairo-native"] + [profile.release] codegen-units = 1 lto = "fat" diff --git a/examples/sandbox/main.rs b/examples/sandbox/main.rs new file mode 100644 index 000000000..0de46b3af --- /dev/null +++ b/examples/sandbox/main.rs @@ -0,0 +1,121 @@ +use std::{ + fs, + path::{Path, PathBuf}, + sync::Arc, +}; + +use cairo_vm::Felt252; +use starknet_in_rust::{ + definitions::{block_context::BlockContext, constants::TRANSACTION_VERSION}, + execution::{ + execution_entry_point::ExecutionEntryPoint, CallType, TransactionExecutionContext, + }, + sandboxing::IsolatedExecutor, + services::api::contract_classes::compiled_class::CompiledClass, + state::{ + cached_state::CachedState, + contract_class_cache::{ContractClassCache, PermanentContractClassCache}, + in_memory_state_reader::InMemoryStateReader, + ExecutionResourcesManager, + }, + utils::{felt_to_hash, Address, ClassHash}, + CasmContractClass, +}; + +fn main() -> Result<(), Box> { + let executor_path = std::env::var("CAIRO_NATIVE_EXECUTOR_PATH") + .map(PathBuf::from) + .unwrap_or_else(|_| { + std::env::current_dir() + .unwrap() + .join("target/debug/cairo_native_executor") + }); + let sandbox = IsolatedExecutor::new(executor_path.as_path())?; + + let mut state_reader = InMemoryStateReader::default(); + let cache = PermanentContractClassCache::default(); + + let class_hash = ClassHash([1; 32]); + let caller_address = Address(1.into()); + let callee_address = Address(1.into()); + + let path = Path::new("starknet_programs/cairo2/get_block_hash_basic.cairo"); + + let casm_contract_class_data = fs::read_to_string(path.with_extension("casm"))?; + let sierra_contract_class_data = fs::read_to_string(path.with_extension("sierra"))?; + + let casm_contract_class: CasmContractClass = serde_json::from_str(&casm_contract_class_data)?; + let sierra_contract_class: cairo_lang_starknet::contract_class::ContractClass = + serde_json::from_str(&sierra_contract_class_data)?; + + let casm_contract_class = Arc::new(casm_contract_class); + let sierra_contract_class = Arc::new(( + sierra_contract_class.extract_sierra_program().unwrap(), + sierra_contract_class.entry_points_by_type, + )); + + cache.set_contract_class( + class_hash, + CompiledClass::Casm { + casm: casm_contract_class, + sierra: Some(sierra_contract_class), + }, + ); + + state_reader + .address_to_class_hash_mut() + .insert(caller_address.clone(), class_hash); + state_reader + .address_to_nonce_mut() + .insert(callee_address.clone(), Felt252::default()); + + let mut state = CachedState::new(Arc::new(state_reader), Arc::new(cache)); + + state.cache_mut().storage_initial_values_mut().insert( + (Address(Felt252::ONE), felt_to_hash(&10.into()).0), + Felt252::from_bytes_be(&[5; 32]), + ); + + let class_hash = *state + .state_reader + .address_to_class_hash + .get(&caller_address) + .unwrap(); + + let mut block_context = BlockContext::default(); + block_context.block_info_mut().block_number = 30; + + let execution_result_native = ExecutionEntryPoint::new( + callee_address.clone(), + vec![10.into()], + Felt252::from_hex("377ae94b690204c74c8d21938c5b72e80fdaee3d21c780fd7557a7f84a8b379") + .unwrap(), + caller_address.clone(), + starknet_in_rust::EntryPointType::External, + Some(CallType::Delegate), + Some(class_hash), + u128::MAX, + ) + .execute( + &mut state, + &block_context, + &mut ExecutionResourcesManager::default(), + &mut TransactionExecutionContext::new( + Address(Felt252::default()), + Felt252::default(), + Vec::default(), + Default::default(), + 10.into(), + block_context.invoke_tx_max_n_steps(), + *TRANSACTION_VERSION, + ), + false, + block_context.invoke_tx_max_n_steps(), + None, + Some(&sandbox), + )?; + + dbg!(execution_result_native); + + Ok(()) +} diff --git a/src/bin/native_executor.rs b/src/bin/native_executor.rs index c34d0f466..91a709a6c 100644 --- a/src/bin/native_executor.rs +++ b/src/bin/native_executor.rs @@ -88,6 +88,13 @@ impl StarkNetSyscallHandler for SyscallHandler { } } + fn get_execution_info_v2( + &mut self, + _remaining_gas: &mut u128, + ) -> SyscallResult { + todo!() + } + fn deploy( &mut self, class_hash: Felt, @@ -575,13 +582,6 @@ impl StarkNetSyscallHandler for SyscallHandler { fn set_version(&mut self, _version: Felt) { todo!() } - - fn get_execution_info_v2( - &mut self, - _remaining_gas: &mut u128, - ) -> SyscallResult { - todo!() - } } pub fn main() -> Result<(), Box> { diff --git a/src/execution/execution_entry_point.rs b/src/execution/execution_entry_point.rs index 6ef5f630a..6671465bc 100644 --- a/src/execution/execution_entry_point.rs +++ b/src/execution/execution_entry_point.rs @@ -721,26 +721,6 @@ impl ExecutionEntryPoint { .iter() .find(|entry_point| entry_point.selector == self.entry_point_selector.to_biguint()) .unwrap(); - let native_executor: NativeExecutor = { - let mut cache = program_cache.borrow_mut(); - let cache = &mut *cache; - match cache { - ProgramCache::Aot(cache) => { - NativeExecutor::Aot(if let Some(executor) = cache.get(class_hash) { - executor - } else { - cache.compile_and_insert(*class_hash, sierra_program, OptLevel::Default) - }) - } - ProgramCache::Jit(cache) => { - NativeExecutor::Jit(if let Some(executor) = cache.get(class_hash) { - executor - } else { - cache.compile_and_insert(*class_hash, sierra_program, OptLevel::Default) - }) - } - } - }; let contract_storage_state = ContractStorageState::new(state, self.contract_address.clone()); @@ -760,31 +740,66 @@ impl ExecutionEntryPoint { sandbox, }; - let syscall_meta = SyscallHandlerMeta::new(&mut syscall_handler); + let value = if let Some(sandbox) = sandbox { + sandbox + .run_program( + sierra_program.clone(), + self.calldata.clone(), + Some(self.initial_gas), + entry_point.function_idx, + &mut syscall_handler, + ) + .map_err(|e| { + TransactionError::CustomError(format!("cairo-native error: {:?}", e)) + })? + } else { + let native_executor: NativeExecutor = { + let mut cache = program_cache.borrow_mut(); + let cache = &mut *cache; + match cache { + ProgramCache::Aot(cache) => { + NativeExecutor::Aot(if let Some(executor) = cache.get(class_hash) { + executor + } else { + cache.compile_and_insert(*class_hash, sierra_program, OptLevel::Default) + }) + } + ProgramCache::Jit(cache) => { + NativeExecutor::Jit(if let Some(executor) = cache.get(class_hash) { + executor + } else { + cache.compile_and_insert(*class_hash, sierra_program, OptLevel::Default) + }) + } + } + }; - let entry_point_fn = &sierra_program - .funcs - .iter() - .find(|x| x.id.id == (entry_point.function_idx as u64)) - .unwrap(); - - let entry_point_id = &entry_point_fn.id; - - let value = match native_executor { - NativeExecutor::Aot(executor) => executor.invoke_contract_dynamic( - entry_point_id, - &self.calldata, - Some(self.initial_gas), - Some(&syscall_meta), - ), - NativeExecutor::Jit(executor) => executor.invoke_contract_dynamic( - entry_point_id, - &self.calldata, - Some(self.initial_gas), - Some(&syscall_meta), - ), - } - .map_err(|e| TransactionError::CustomError(format!("cairo-native error: {:?}", e)))?; + let syscall_meta = SyscallHandlerMeta::new(&mut syscall_handler); + + let entry_point_fn = &sierra_program + .funcs + .iter() + .find(|x| x.id.id == (entry_point.function_idx as u64)) + .unwrap(); + + let entry_point_id = &entry_point_fn.id; + + match native_executor { + NativeExecutor::Aot(executor) => executor.invoke_contract_dynamic( + entry_point_id, + &self.calldata, + Some(self.initial_gas), + Some(&syscall_meta), + ), + NativeExecutor::Jit(executor) => executor.invoke_contract_dynamic( + entry_point_id, + &self.calldata, + Some(self.initial_gas), + Some(&syscall_meta), + ), + } + .map_err(|e| TransactionError::CustomError(format!("cairo-native error: {:?}", e)))? + }; Ok(CallInfo { caller_address: self.caller_address.clone(), From bedd1cc757550cfa7d3c30deeca85389ecb25814 Mon Sep 17 00:00:00 2001 From: Edgar Date: Mon, 29 Jan 2024 11:58:03 -0300 Subject: [PATCH 03/15] use cache in executor --- src/bin/native_executor.rs | 17 +++++++++-------- src/execution/execution_entry_point.rs | 1 + src/sandboxing/mod.rs | 9 ++++++++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/bin/native_executor.rs b/src/bin/native_executor.rs index 91a709a6c..18193f416 100644 --- a/src/bin/native_executor.rs +++ b/src/bin/native_executor.rs @@ -2,11 +2,12 @@ use std::{cell::RefCell, path::PathBuf, rc::Rc}; use cairo_lang_utils::ResultHelper; use cairo_native::{ + cache::JitProgramCache, context::NativeContext, - executor::JitNativeExecutor, metadata::syscall_handler::SyscallHandlerMeta, starknet::{StarkNetSyscallHandler, SyscallResult}, utils::find_entry_point_by_idx, + OptLevel, }; use cairo_vm::Felt252 as Felt; use ipc_channel::ipc::{IpcOneShotServer, IpcReceiver, IpcSender}; @@ -624,6 +625,8 @@ pub fn main() -> Result<(), Box> { receiver: receiver.clone(), }; + let mut cache = JitProgramCache::new(&native_context); + loop { tracing::info!("waiting for message"); @@ -632,6 +635,7 @@ pub fn main() -> Result<(), Box> { match message { Message::ExecuteJIT { id, + class_hash, program, inputs, function_idx, @@ -640,16 +644,15 @@ pub fn main() -> Result<(), Box> { tracing::info!("Message: ExecuteJIT"); sender.send(Message::Ack(id).wrap()?)?; tracing::info!("sent ack: {:?}", id); + let program = program.into_v1()?.program; - let native_program = native_context.compile(&program)?; + let native_executor = + cache.compile_and_insert(class_hash, &program, OptLevel::Default); let entry_point_fn = find_entry_point_by_idx(&program, function_idx).unwrap(); let fn_id = &entry_point_fn.id; - let native_executor = - JitNativeExecutor::from_native_module(native_program, Default::default()); - let result = native_executor.invoke_contract_dynamic( fn_id, &inputs, @@ -663,7 +666,6 @@ pub fn main() -> Result<(), Box> { tracing::info!("sent result msg"); } - Message::ExecutionResult { .. } => {} Message::Ack(_) => {} Message::Ping => { tracing::info!("Message: Ping"); @@ -673,8 +675,7 @@ pub fn main() -> Result<(), Box> { tracing::info!("Message: KILL"); break; } - Message::SyscallRequest(_) => todo!(), - Message::SyscallAnswer(_) => todo!(), + _ => unreachable!(), } } diff --git a/src/execution/execution_entry_point.rs b/src/execution/execution_entry_point.rs index 6671465bc..d06043b29 100644 --- a/src/execution/execution_entry_point.rs +++ b/src/execution/execution_entry_point.rs @@ -743,6 +743,7 @@ impl ExecutionEntryPoint { let value = if let Some(sandbox) = sandbox { sandbox .run_program( + *class_hash, sierra_program.clone(), self.calldata.clone(), Some(self.initial_gas), diff --git a/src/sandboxing/mod.rs b/src/sandboxing/mod.rs index 721184d89..321553d59 100644 --- a/src/sandboxing/mod.rs +++ b/src/sandboxing/mod.rs @@ -15,11 +15,14 @@ use cairo_native::{ starknet::{ExecutionInfo, StarkNetSyscallHandler, SyscallResult}, }; +use crate::utils::ClassHash; + #[allow(clippy::large_enum_variant)] #[derive(Debug, Clone, Serialize, Deserialize)] pub enum Message { ExecuteJIT { id: Uuid, + class_hash: ClassHash, program: VersionedProgram, inputs: Vec, function_idx: usize, @@ -160,7 +163,9 @@ impl Message { #[derive(Debug, Serialize, Deserialize, PartialEq)] pub enum WrappedMessage { - Message(String), // ipc-channel uses bincode and doesnt support serializing Vecs + // ipc-channel uses bincode and doesnt support serializing Vecs + // so we serialize it to a json string first and pass it as a string. + Message(String), } impl WrappedMessage { @@ -209,6 +214,7 @@ impl IsolatedExecutor { pub fn run_program( &self, + class_hash: ClassHash, program: Program, inputs: Vec, gas: Option, @@ -220,6 +226,7 @@ impl IsolatedExecutor { let msg = Message::ExecuteJIT { id, + class_hash, program: program.into_artifact(), inputs, gas, From 8f9e8a8f488e8d78f407058fbb8e6dc24ee2b131 Mon Sep 17 00:00:00 2001 From: Edgar Date: Mon, 29 Jan 2024 12:04:07 -0300 Subject: [PATCH 04/15] clean --- src/bin/native_executor.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bin/native_executor.rs b/src/bin/native_executor.rs index 18193f416..31bd639f3 100644 --- a/src/bin/native_executor.rs +++ b/src/bin/native_executor.rs @@ -626,6 +626,7 @@ pub fn main() -> Result<(), Box> { }; let mut cache = JitProgramCache::new(&native_context); + tracing::info!("initialized program cache"); loop { tracing::info!("waiting for message"); @@ -641,9 +642,9 @@ pub fn main() -> Result<(), Box> { function_idx, gas, } => { - tracing::info!("Message: ExecuteJIT"); + tracing::info!("Message: ExecuteJIT with id {}", id); sender.send(Message::Ack(id).wrap()?)?; - tracing::info!("sent ack: {:?}", id); + tracing::info!("sent ack: {}", id); let program = program.into_v1()?.program; let native_executor = From 17ea3310433703f64ace4216e0e0818f4a4bab83 Mon Sep 17 00:00:00 2001 From: Edgar Date: Mon, 29 Jan 2024 17:22:12 -0300 Subject: [PATCH 05/15] sandbox error --- src/sandboxing/mod.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/sandboxing/mod.rs b/src/sandboxing/mod.rs index 321553d59..85f314c67 100644 --- a/src/sandboxing/mod.rs +++ b/src/sandboxing/mod.rs @@ -14,9 +14,22 @@ use cairo_native::{ execution_result::ContractExecutionResult, starknet::{ExecutionInfo, StarkNetSyscallHandler, SyscallResult}, }; +use thiserror::Error; use crate::utils::ClassHash; +#[derive(Debug, Error)] +pub enum SandboxError { + #[error("process error")] + ProcessError(u32), + #[error("ipc error: {0}")] + IpcBincodeError(#[from] ipc_channel::Error), + #[error("ipc bincode error: {0}")] + IpcError(#[from] ipc_channel::ipc::IpcError), + #[error("serializer error {0:?}")] + SerializeError(#[from] serde_json::Error), +} + #[allow(clippy::large_enum_variant)] #[derive(Debug, Clone, Serialize, Deserialize)] pub enum Message { @@ -220,7 +233,7 @@ impl IsolatedExecutor { gas: Option, function_idx: usize, handler: &mut impl StarkNetSyscallHandler, - ) -> Result> { + ) -> Result { tracing::debug!("running program"); let id = Uuid::new_v4(); From 9095ec0fa22b10c7973cd15195a2d1e3a960790f Mon Sep 17 00:00:00 2001 From: Edgar Date: Mon, 29 Jan 2024 17:23:20 -0300 Subject: [PATCH 06/15] make child pub --- src/sandboxing/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sandboxing/mod.rs b/src/sandboxing/mod.rs index 85f314c67..408b4a17d 100644 --- a/src/sandboxing/mod.rs +++ b/src/sandboxing/mod.rs @@ -191,7 +191,7 @@ impl WrappedMessage { #[derive(Debug)] pub struct IsolatedExecutor { - proc: Child, + pub proc: Child, sender: IpcSender, receiver: IpcReceiver, } From e128161dac435dc23e1300660e75f47508d26ee6 Mon Sep 17 00:00:00 2001 From: Edgar Date: Mon, 29 Jan 2024 18:16:04 -0300 Subject: [PATCH 07/15] add error --- src/execution/execution_entry_point.rs | 2 +- src/transaction/error.rs | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/execution/execution_entry_point.rs b/src/execution/execution_entry_point.rs index d06043b29..ec3ebec7a 100644 --- a/src/execution/execution_entry_point.rs +++ b/src/execution/execution_entry_point.rs @@ -188,7 +188,7 @@ impl ExecutionEntryPoint { revert_error: None, n_reverted_steps: 0, }) - } + }, Err(e) => { if !support_reverted { state.apply_state_update(&StateDiff::from_cached_state( diff --git a/src/transaction/error.rs b/src/transaction/error.rs index b277783a3..8b3e9569c 100644 --- a/src/transaction/error.rs +++ b/src/transaction/error.rs @@ -2,12 +2,10 @@ use crate::{ core::errors::{ contract_address_errors::ContractAddressError, hash_errors::HashError, state_errors::StateError, - }, - definitions::transaction_type::TransactionType, - execution::os_usage::OsResources, - syscalls::syscall_handler_errors::SyscallHandlerError, - utils::ClassHash, + }, definitions::transaction_type::TransactionType, execution::os_usage::OsResources, syscalls::syscall_handler_errors::SyscallHandlerError, utils::ClassHash }; + + use cairo_vm::{ types::{ errors::{math_errors::MathError, program_errors::ProgramError}, @@ -164,4 +162,7 @@ pub enum TransactionError { DeprecatedAccountTxFieldsVInV3TX, #[error("Non V3 Transactions can't be created with non deprecated account tx fields")] CurrentAccountTxFieldsInNonV3TX, + #[cfg(feature = "cairo-native")] + #[error("sandbox error {0}")] + SandboxError(#[from] crate::sandboxing::SandboxError) } From c3ee89e67f3b1f9b6a41b28d86697f09b573eefb Mon Sep 17 00:00:00 2001 From: Edgar Date: Mon, 29 Jan 2024 18:16:45 -0300 Subject: [PATCH 08/15] error --- src/execution/execution_entry_point.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/execution/execution_entry_point.rs b/src/execution/execution_entry_point.rs index ec3ebec7a..50e501f00 100644 --- a/src/execution/execution_entry_point.rs +++ b/src/execution/execution_entry_point.rs @@ -749,10 +749,7 @@ impl ExecutionEntryPoint { Some(self.initial_gas), entry_point.function_idx, &mut syscall_handler, - ) - .map_err(|e| { - TransactionError::CustomError(format!("cairo-native error: {:?}", e)) - })? + )? } else { let native_executor: NativeExecutor = { let mut cache = program_cache.borrow_mut(); From 817222e2b3108246d1697b94483a5cacebf68b1d Mon Sep 17 00:00:00 2001 From: Edgar Date: Mon, 29 Jan 2024 18:22:55 -0300 Subject: [PATCH 09/15] remove unused error --- src/sandboxing/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/sandboxing/mod.rs b/src/sandboxing/mod.rs index 408b4a17d..7730fedd3 100644 --- a/src/sandboxing/mod.rs +++ b/src/sandboxing/mod.rs @@ -20,11 +20,9 @@ use crate::utils::ClassHash; #[derive(Debug, Error)] pub enum SandboxError { - #[error("process error")] - ProcessError(u32), - #[error("ipc error: {0}")] - IpcBincodeError(#[from] ipc_channel::Error), #[error("ipc bincode error: {0}")] + IpcBincodeError(#[from] ipc_channel::Error), + #[error("ipc error: {0}")] IpcError(#[from] ipc_channel::ipc::IpcError), #[error("serializer error {0:?}")] SerializeError(#[from] serde_json::Error), From 42bd83373d7e5ca3a7c6af3f47462e8d139529f3 Mon Sep 17 00:00:00 2001 From: Edgar Date: Thu, 1 Feb 2024 12:42:01 +0100 Subject: [PATCH 10/15] fix --- src/bin/native_executor.rs | 48 +++++++------- src/execution/execution_entry_point.rs | 19 +++--- src/syscalls/native_syscall_handler.rs | 90 +++++++++++--------------- src/transaction/error.rs | 9 ++- 4 files changed, 77 insertions(+), 89 deletions(-) diff --git a/src/bin/native_executor.rs b/src/bin/native_executor.rs index 31bd639f3..473579cb4 100644 --- a/src/bin/native_executor.rs +++ b/src/bin/native_executor.rs @@ -448,13 +448,13 @@ impl StarkNetSyscallHandler for SyscallHandler { &mut self, _p0: cairo_native::starknet::Secp256k1Point, _p1: cairo_native::starknet::Secp256k1Point, - _gas: &mut u128, - ) -> SyscallResult> { + _remaining_gas: &mut u128, + ) -> SyscallResult { todo!() } fn secp256k1_get_point_from_x( - &self, + &mut self, _x: cairo_native::starknet::U256, _y_parity: bool, _gas: &mut u128, @@ -463,7 +463,7 @@ impl StarkNetSyscallHandler for SyscallHandler { } fn secp256k1_get_xy( - &self, + &mut self, _p: cairo_native::starknet::Secp256k1Point, _gas: &mut u128, ) -> SyscallResult<(cairo_native::starknet::U256, cairo_native::starknet::U256)> { @@ -471,16 +471,16 @@ impl StarkNetSyscallHandler for SyscallHandler { } fn secp256k1_mul( - &self, + &mut self, _p: cairo_native::starknet::Secp256k1Point, _m: cairo_native::starknet::U256, - _gas: &mut u128, - ) -> SyscallResult> { + _remaining_gas: &mut u128, + ) -> SyscallResult { todo!() } fn secp256k1_new( - &self, + &mut self, _x: cairo_native::starknet::U256, _y: cairo_native::starknet::U256, _gas: &mut u128, @@ -489,37 +489,37 @@ impl StarkNetSyscallHandler for SyscallHandler { } fn secp256r1_add( - &self, - _p0: cairo_native::starknet::Secp256k1Point, - _p1: cairo_native::starknet::Secp256k1Point, - _gas: &mut u128, - ) -> SyscallResult> { + &mut self, + _p0: cairo_native::starknet::Secp256r1Point, + _p1: cairo_native::starknet::Secp256r1Point, + _remaining_gas: &mut u128, + ) -> SyscallResult { todo!() } fn secp256r1_get_point_from_x( - &self, + &mut self, _x: cairo_native::starknet::U256, _y_parity: bool, - _gas: &mut u128, - ) -> SyscallResult> { + _remaining_gas: &mut u128, + ) -> SyscallResult> { todo!() } fn secp256r1_get_xy( - &self, - _p: cairo_native::starknet::Secp256k1Point, + &mut self, + _p: cairo_native::starknet::Secp256r1Point, _gas: &mut u128, ) -> SyscallResult<(cairo_native::starknet::U256, cairo_native::starknet::U256)> { todo!() } fn secp256r1_mul( - &self, - _p: cairo_native::starknet::Secp256k1Point, + &mut self, + _p: cairo_native::starknet::Secp256r1Point, _m: cairo_native::starknet::U256, - _gas: &mut u128, - ) -> SyscallResult> { + _remaining_gas: &mut u128, + ) -> SyscallResult { todo!() } @@ -527,8 +527,8 @@ impl StarkNetSyscallHandler for SyscallHandler { &mut self, _x: cairo_native::starknet::U256, _y: cairo_native::starknet::U256, - _gas: &mut u128, - ) -> SyscallResult> { + _remaining_gas: &mut u128, + ) -> SyscallResult> { todo!() } diff --git a/src/execution/execution_entry_point.rs b/src/execution/execution_entry_point.rs index 50e501f00..1069f2d9a 100644 --- a/src/execution/execution_entry_point.rs +++ b/src/execution/execution_entry_point.rs @@ -188,7 +188,7 @@ impl ExecutionEntryPoint { revert_error: None, n_reverted_steps: 0, }) - }, + } Err(e) => { if !support_reverted { state.apply_state_update(&StateDiff::from_cached_state( @@ -741,15 +741,14 @@ impl ExecutionEntryPoint { }; let value = if let Some(sandbox) = sandbox { - sandbox - .run_program( - *class_hash, - sierra_program.clone(), - self.calldata.clone(), - Some(self.initial_gas), - entry_point.function_idx, - &mut syscall_handler, - )? + sandbox.run_program( + *class_hash, + sierra_program.clone(), + self.calldata.clone(), + Some(self.initial_gas), + entry_point.function_idx, + &mut syscall_handler, + )? } else { let native_executor: NativeExecutor = { let mut cache = program_cache.borrow_mut(); diff --git a/src/syscalls/native_syscall_handler.rs b/src/syscalls/native_syscall_handler.rs index 0a2fb608d..6d9530b50 100644 --- a/src/syscalls/native_syscall_handler.rs +++ b/src/syscalls/native_syscall_handler.rs @@ -27,10 +27,7 @@ use crate::{ }; use cairo_native::{ cache::ProgramCache, - starknet::{ - BlockInfo, ExecutionInfo, ExecutionInfoV2, StarkNetSyscallHandler, SyscallResult, TxInfo, - U256, - }, + starknet::{BlockInfo, ExecutionInfo, StarkNetSyscallHandler, SyscallResult, TxInfo, U256}, }; use cairo_vm::Felt252; use starknet::core::utils::cairo_short_string_to_felt; @@ -461,37 +458,26 @@ impl<'a, 'cache, S: StateReader, C: ContractClassCache> StarkNetSyscallHandler } keccak::f1600(&mut state) } - // state[0] and state[1] conform the hash_low (u128) - // state[2] and state[3] conform the hash_high (u128) - let hash = [ - state[0].to_le_bytes(), - state[1].to_le_bytes(), - state[2].to_le_bytes(), - state[3].to_le_bytes(), - ] - .concat(); - - SyscallResult::Ok(U256(hash[0..32].try_into().unwrap())) - } + // state[0] and state[1] conform the hash_high (u128) + // state[2] and state[3] conform the hash_low (u128) - fn get_execution_info_v2( - &mut self, - _: &mut u128, - ) -> Result> { - todo!() + SyscallResult::Ok(U256 { + lo: state[2] as u128 | ((state[3] as u128) << 64), + hi: state[0] as u128 | ((state[1] as u128) << 64), + }) } fn secp256k1_add( &mut self, _p0: cairo_native::starknet::Secp256k1Point, _p1: cairo_native::starknet::Secp256k1Point, - _gas: &mut u128, - ) -> SyscallResult> { + _remaining_gas: &mut u128, + ) -> SyscallResult { todo!() } fn secp256k1_get_point_from_x( - &self, + &mut self, _x: cairo_native::starknet::U256, _y_parity: bool, _gas: &mut u128, @@ -500,7 +486,7 @@ impl<'a, 'cache, S: StateReader, C: ContractClassCache> StarkNetSyscallHandler } fn secp256k1_get_xy( - &self, + &mut self, _p: cairo_native::starknet::Secp256k1Point, _gas: &mut u128, ) -> SyscallResult<(cairo_native::starknet::U256, cairo_native::starknet::U256)> { @@ -508,16 +494,16 @@ impl<'a, 'cache, S: StateReader, C: ContractClassCache> StarkNetSyscallHandler } fn secp256k1_mul( - &self, + &mut self, _p: cairo_native::starknet::Secp256k1Point, - _m: cairo_native::starknet::U256, - _gas: &mut u128, - ) -> SyscallResult> { + _m: U256, + _remaining_gas: &mut u128, + ) -> SyscallResult { todo!() } fn secp256k1_new( - &self, + &mut self, _x: cairo_native::starknet::U256, _y: cairo_native::starknet::U256, _gas: &mut u128, @@ -526,46 +512,46 @@ impl<'a, 'cache, S: StateReader, C: ContractClassCache> StarkNetSyscallHandler } fn secp256r1_add( - &self, - _p0: cairo_native::starknet::Secp256k1Point, - _p1: cairo_native::starknet::Secp256k1Point, - _gas: &mut u128, - ) -> SyscallResult> { + &mut self, + _p0: cairo_native::starknet::Secp256r1Point, + _p1: cairo_native::starknet::Secp256r1Point, + _remaining_gas: &mut u128, + ) -> SyscallResult { todo!() } fn secp256r1_get_point_from_x( - &self, - _x: cairo_native::starknet::U256, + &mut self, + _x: U256, _y_parity: bool, - _gas: &mut u128, - ) -> SyscallResult> { + _remaining_gas: &mut u128, + ) -> SyscallResult> { todo!() } fn secp256r1_get_xy( - &self, - _p: cairo_native::starknet::Secp256k1Point, - _gas: &mut u128, - ) -> SyscallResult<(cairo_native::starknet::U256, cairo_native::starknet::U256)> { + &mut self, + _p: cairo_native::starknet::Secp256r1Point, + _remaining_gas: &mut u128, + ) -> SyscallResult<(U256, U256)> { todo!() } fn secp256r1_mul( - &self, - _p: cairo_native::starknet::Secp256k1Point, - _m: cairo_native::starknet::U256, - _gas: &mut u128, - ) -> SyscallResult> { + &mut self, + _p: cairo_native::starknet::Secp256r1Point, + _m: U256, + _remaining_gas: &mut u128, + ) -> SyscallResult { todo!() } fn secp256r1_new( &mut self, - _x: cairo_native::starknet::U256, - _y: cairo_native::starknet::U256, - _gas: &mut u128, - ) -> SyscallResult> { + _x: U256, + _y: U256, + _remaining_gas: &mut u128, + ) -> SyscallResult> { todo!() } diff --git a/src/transaction/error.rs b/src/transaction/error.rs index 4d0bd330b..02b058fd8 100644 --- a/src/transaction/error.rs +++ b/src/transaction/error.rs @@ -2,10 +2,13 @@ use crate::{ core::errors::{ contract_address_errors::ContractAddressError, hash_errors::HashError, state_errors::StateError, - }, definitions::transaction_type::TransactionType, execution::os_usage::OsResources, syscalls::syscall_handler_errors::SyscallHandlerError, utils::ClassHash + }, + definitions::transaction_type::TransactionType, + execution::os_usage::OsResources, + syscalls::syscall_handler_errors::SyscallHandlerError, + utils::ClassHash, }; - use cairo_vm::{ types::{ errors::{math_errors::MathError, program_errors::ProgramError}, @@ -168,5 +171,5 @@ pub enum TransactionError { CurrentAccountTxFieldsInNonV3TX, #[cfg(feature = "cairo-native")] #[error("sandbox error {0}")] - SandboxError(#[from] crate::sandboxing::SandboxError) + SandboxError(#[from] crate::sandboxing::SandboxError), } From 9350b35dd1bb0ecb438eb43662d18bfcd7994a99 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Wed, 7 Feb 2024 13:07:29 +0100 Subject: [PATCH 11/15] doc --- src/bin/native_executor.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/bin/native_executor.rs b/src/bin/native_executor.rs index 473579cb4..4129fb0e2 100644 --- a/src/bin/native_executor.rs +++ b/src/bin/native_executor.rs @@ -1,3 +1,9 @@ +//! This binary is meant to be spawned by the `IsolatedExecutor` API. +//! +//! When it starts, it prints to stdout it's ipc handle, so a client can connect to it. +//! +//! After receiving a initial connection it loops waiting for messages. + use std::{cell::RefCell, path::PathBuf, rc::Rc}; use cairo_lang_utils::ResultHelper; From dcce49ed77da98581777c103676530f00d61c636 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Wed, 7 Feb 2024 13:10:36 +0100 Subject: [PATCH 12/15] docs --- examples/sandbox/main.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/sandbox/main.rs b/examples/sandbox/main.rs index 0de46b3af..db06c7665 100644 --- a/examples/sandbox/main.rs +++ b/examples/sandbox/main.rs @@ -1,3 +1,5 @@ +//! This example shows how to use the `IsolatedExecutor` cairo-native sandbox with starknet in rust. + use std::{ fs, path::{Path, PathBuf}, From bdcf0caa432383bd93c1ed542332223d75a85b3d Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Wed, 7 Feb 2024 14:03:29 +0100 Subject: [PATCH 13/15] fixes --- Makefile | 4 ++ bench/yas.rs | 114 ++++++++++++++++++++++++++++++++----- src/bin/native_executor.rs | 102 ++++++++++++++++++++++++++++----- src/sandboxing/mod.rs | 25 ++++++-- 4 files changed, 213 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index 8536c4071..c95cb7fbe 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,7 @@ STARKNET_SIERRA_COMPILE_CAIRO_2:=cairo2/bin/starknet-sierra-compile usage: @echo 'Usage:' @echo ' build: Builds the Rust code' + @echo ' build-native: Builds the Rust code with the cairo-native feature' @echo ' check: Runs cargo check' @echo ' deps: Installs dependencies' @echo ' deps-macos: Installs depedencies for MacOS' @@ -156,6 +157,9 @@ cairo-%.tar: build: compile-cairo compile-starknet compile-cairo-1-casm compile-cairo-1-sierra compile-cairo-2-casm compile-cairo-2-sierra cargo build --release --workspace +build-native: compile-cairo compile-starknet compile-cairo-1-casm compile-cairo-1-sierra compile-cairo-2-casm compile-cairo-2-sierra + cargo build --release --workspace --features cairo-native + check: compile-cairo compile-starknet compile-cairo-1-casm compile-cairo-1-sierra compile-cairo-2-casm compile-cairo-2-sierra cargo check --workspace --all-targets diff --git a/bench/yas.rs b/bench/yas.rs index b16a878ba..981724fd6 100644 --- a/bench/yas.rs +++ b/bench/yas.rs @@ -20,6 +20,7 @@ use starknet_in_rust::{ block_context::{BlockContext, StarknetChainId}, constants::EXECUTE_ENTRY_POINT_SELECTOR, }, + sandboxing::IsolatedExecutor, state::{ cached_state::CachedState, contract_class_cache::ContractClassCache, state_api::StateReader, }, @@ -50,6 +51,8 @@ fn main() -> Result<(), Box> { #[cfg(feature = "cairo-native")] let mut jit_run: bool = true; #[cfg(feature = "cairo-native")] + let mut sandboxed: bool = true; + #[cfg(feature = "cairo-native")] let args: Vec = std::env::args().collect(); #[cfg(feature = "cairo-native")] if args.len() < 2 { @@ -59,10 +62,19 @@ fn main() -> Result<(), Box> { "jit" => { info!("Running in JIT mode"); } + "jit-sandbox" => { + info!("Running in JIT Sandboxed mode"); + sandboxed = true; + } "aot" => { info!("Running in AOT mode"); jit_run = false; } + "aot-sandbox" => { + info!("Running in AOT Sandboxed mode"); + jit_run = false; + sandboxed = true; + } arg => { info!("Invalid mode {}, running in JIT mode", arg); } @@ -85,6 +97,24 @@ fn main() -> Result<(), Box> { }; #[cfg(feature = "cairo-native")] let program_cache = Rc::new(RefCell::new(cache)); + #[cfg(feature = "cairo-native")] + let sandbox = if sandboxed { + Some( + starknet_in_rust::sandboxing::IsolatedExecutor::new( + std::env::var("CAIRO_NATIVE_EXECUTOR_PATH") + .map(std::path::PathBuf::from) + .unwrap_or_else(|_| { + std::env::current_dir() + .unwrap() + .join("target/release/cairo_native_executor") + }) + .as_path(), + ) + .unwrap(), + ) + } else { + None + }; // Declare ERC20, YASFactory, YASPool and YASRouter contracts. info!("Declaring the ERC20 contract."); @@ -92,24 +122,32 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), )?; info!("Declaring the YASFactory contract."); let yas_factory_class_hash = declare_yas_factory( &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), )?; info!("Declaring the YASRouter contract."); let yas_router_class_hash = declare_yas_router( &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), )?; info!("Declaring the YASPool contract."); let yas_pool_class_hash = declare_yas_pool( &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), )?; // Deploy two ERC20 contracts. @@ -118,6 +156,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &erc20_class_hash, "TYAS0", "$YAS0", @@ -129,6 +169,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &erc20_class_hash, "TYAS1", "$YAS1", @@ -142,6 +184,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &yas_factory_class_hash, *OWNER_ADDRESS, yas_pool_class_hash, @@ -153,6 +197,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &yas_router_class_hash, )?; @@ -162,6 +208,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &yas_pool_class_hash, yas_factory_address, yas0_token_address, @@ -176,6 +224,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &yas_pool_address, (79_228_162_514_264_337_593_543_950_336, 0), false, @@ -187,6 +237,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &yas0_token_address, *OWNER_ADDRESS )? @@ -197,6 +249,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &yas1_token_address, *OWNER_ADDRESS )? @@ -208,6 +262,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &ACCOUNT_ADDRESS, yas0_token_address, yas_router_address, @@ -216,6 +272,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &ACCOUNT_ADDRESS, yas1_token_address, yas_router_address, @@ -227,6 +285,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &yas0_token_address, *OWNER_ADDRESS )? @@ -237,6 +297,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &yas1_token_address, *OWNER_ADDRESS )? @@ -248,6 +310,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &ACCOUNT_ADDRESS, yas_router_address, yas_pool_address, @@ -263,6 +327,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &yas0_token_address, *OWNER_ADDRESS )? @@ -273,6 +339,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &yas1_token_address, *OWNER_ADDRESS )? @@ -293,6 +361,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &ACCOUNT_ADDRESS, yas_router_address, yas_pool_address, @@ -337,6 +407,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &yas0_token_address, *OWNER_ADDRESS )? @@ -347,6 +419,8 @@ fn main() -> Result<(), Box> { &mut state, #[cfg(feature = "cairo-native")] program_cache.clone(), + #[cfg(feature = "cairo-native")] + sandbox.as_ref(), &yas1_token_address, *OWNER_ADDRESS )? @@ -358,6 +432,7 @@ fn main() -> Result<(), Box> { fn declare_erc20( state: &mut CachedState, #[cfg(feature = "cairo-native")] program_cache: Rc>>, + #[cfg(feature = "cairo-native")] sandbox: Option<&IsolatedExecutor>, ) -> Result> where S: StateReader, @@ -386,7 +461,7 @@ where #[cfg(feature = "cairo-native")] Some(program_cache), #[cfg(feature = "cairo-native")] - None, + sandbox, )?; // Ensure the execution was successful. @@ -401,6 +476,7 @@ where fn declare_yas_factory( state: &mut CachedState, #[cfg(feature = "cairo-native")] program_cache: Rc>>, + #[cfg(feature = "cairo-native")] sandbox: Option<&IsolatedExecutor>, ) -> Result> where S: StateReader, @@ -429,7 +505,7 @@ where #[cfg(feature = "cairo-native")] Some(program_cache), #[cfg(feature = "cairo-native")] - None, + sandbox, )?; // Ensure the execution was successful. @@ -444,6 +520,7 @@ where fn declare_yas_router( state: &mut CachedState, #[cfg(feature = "cairo-native")] program_cache: Rc>>, + #[cfg(feature = "cairo-native")] sandbox: Option<&IsolatedExecutor>, ) -> Result> where S: StateReader, @@ -472,7 +549,7 @@ where #[cfg(feature = "cairo-native")] Some(program_cache), #[cfg(feature = "cairo-native")] - None, + sandbox, )?; // Ensure the execution was successful. @@ -487,6 +564,7 @@ where fn declare_yas_pool( state: &mut CachedState, #[cfg(feature = "cairo-native")] program_cache: Rc>>, + #[cfg(feature = "cairo-native")] sandbox: Option<&IsolatedExecutor>, ) -> Result> where S: StateReader, @@ -515,7 +593,7 @@ where #[cfg(feature = "cairo-native")] Some(program_cache), #[cfg(feature = "cairo-native")] - None, + sandbox, )?; // Ensure the execution was successful. @@ -527,9 +605,11 @@ where Ok(casm_class_hash) } +#[allow(clippy::too_many_arguments)] fn deploy_erc20( state: &mut CachedState, #[cfg(feature = "cairo-native")] program_cache: Rc>>, + #[cfg(feature = "cairo-native")] sandbox: Option<&IsolatedExecutor>, erc20_class_hash: &Felt252, name: &str, symbol: &str, @@ -569,7 +649,7 @@ where #[cfg(feature = "cairo-native")] Some(program_cache), #[cfg(feature = "cairo-native")] - None, + sandbox, )?; // Ensure the execution was successful. @@ -584,6 +664,7 @@ where fn deploy_yas_factory( state: &mut CachedState, #[cfg(feature = "cairo-native")] program_cache: Rc>>, + #[cfg(feature = "cairo-native")] sandbox: Option<&IsolatedExecutor>, yas_factory_class_hash: &Felt252, owner_address: Felt252, pool_class_hash: Felt252, @@ -618,7 +699,7 @@ where #[cfg(feature = "cairo-native")] Some(program_cache), #[cfg(feature = "cairo-native")] - None, + sandbox, )?; // Ensure the execution was successful. @@ -633,6 +714,7 @@ where fn deploy_yas_router( state: &mut CachedState, #[cfg(feature = "cairo-native")] program_cache: Rc>>, + #[cfg(feature = "cairo-native")] sandbox: Option<&IsolatedExecutor>, yas_router_class_hash: &Felt252, ) -> Result> where @@ -659,7 +741,7 @@ where #[cfg(feature = "cairo-native")] Some(program_cache), #[cfg(feature = "cairo-native")] - None, + sandbox, )?; // Ensure the execution was successful. @@ -675,6 +757,7 @@ where fn deploy_yas_pool( state: &mut CachedState, #[cfg(feature = "cairo-native")] program_cache: Rc>>, + #[cfg(feature = "cairo-native")] sandbox: Option<&IsolatedExecutor>, yas_pool_class_hash: &Felt252, yas_factory_address: Felt252, yas0_token_address: Felt252, @@ -716,7 +799,7 @@ where #[cfg(feature = "cairo-native")] Some(program_cache), #[cfg(feature = "cairo-native")] - None, + sandbox, )?; // Ensure the execution was successful. @@ -731,6 +814,7 @@ where fn initialize_pool( state: &mut CachedState, #[cfg(feature = "cairo-native")] program_cache: Rc>>, + #[cfg(feature = "cairo-native")] sandbox: Option<&IsolatedExecutor>, yas_pool_address: &Felt252, price_sqrt: (u128, u128), sign: bool, @@ -769,7 +853,7 @@ where #[cfg(feature = "cairo-native")] Some(program_cache), #[cfg(feature = "cairo-native")] - None, + sandbox, )?; // Ensure the execution was successful. @@ -784,6 +868,7 @@ where fn approve_max( state: &mut CachedState, #[cfg(feature = "cairo-native")] program_cache: Rc>>, + #[cfg(feature = "cairo-native")] sandbox: Option<&IsolatedExecutor>, account_address: &Felt252, token_address: Felt252, wallet_address: Felt252, @@ -820,7 +905,7 @@ where #[cfg(feature = "cairo-native")] Some(program_cache), #[cfg(feature = "cairo-native")] - None, + sandbox, )?; // Ensure the execution was successful. @@ -836,6 +921,7 @@ where fn mint( state: &mut CachedState, #[cfg(feature = "cairo-native")] program_cache: Rc>>, + #[cfg(feature = "cairo-native")] sandbox: Option<&IsolatedExecutor>, account_address: &Felt252, yas_router_address: Felt252, yas_pool_address: Felt252, @@ -880,7 +966,7 @@ where #[cfg(feature = "cairo-native")] Some(program_cache), #[cfg(feature = "cairo-native")] - None, + sandbox, )?; // Ensure the execution was successful. @@ -896,6 +982,7 @@ where fn swap( state: &mut CachedState, #[cfg(feature = "cairo-native")] program_cache: Rc>>, + #[cfg(feature = "cairo-native")] sandbox: Option<&IsolatedExecutor>, account_address: &Felt252, yas_router_address: Felt252, yas_pool_address: Felt252, @@ -942,7 +1029,7 @@ where #[cfg(feature = "cairo-native")] Some(program_cache), #[cfg(feature = "cairo-native")] - None, + sandbox, )?; // Ensure the execution was successful. @@ -957,6 +1044,7 @@ where fn balance_of( state: &mut CachedState, #[cfg(feature = "cairo-native")] program_cache: Rc>>, + #[cfg(feature = "cairo-native")] sandbox: Option<&IsolatedExecutor>, token_address: &Felt252, wallet_address: Felt252, ) -> Result> @@ -992,7 +1080,7 @@ where #[cfg(feature = "cairo-native")] Some(program_cache), #[cfg(feature = "cairo-native")] - None, + sandbox, )?; // Ensure the execution was successful. diff --git a/src/bin/native_executor.rs b/src/bin/native_executor.rs index 4129fb0e2..82245e597 100644 --- a/src/bin/native_executor.rs +++ b/src/bin/native_executor.rs @@ -56,7 +56,10 @@ impl StarkNetSyscallHandler for SyscallHandler { *gas = remaining_gas; result } else { - tracing::error!("wrong message received: {:#?}", result); + tracing::error!( + "wrong message received (expected SyscallAnswer::GetBlockHash): {:?}", + result + ); panic!(); } } @@ -90,16 +93,48 @@ impl StarkNetSyscallHandler for SyscallHandler { *gas = remaining_gas; result } else { - tracing::error!("wrong message received: {:#?}", result); + tracing::error!( + "wrong message received (expected SyscallAnswer::GetExecutionInfo): {:?}", + result + ); panic!(); } } fn get_execution_info_v2( &mut self, - _remaining_gas: &mut u128, + gas: &mut u128, ) -> SyscallResult { - todo!() + self.sender + .send( + Message::SyscallRequest(SyscallRequest::GetExecutionInfo { gas: *gas }) + .wrap() + .unwrap(), + ) + .expect("failed to send"); + let result = self + .receiver + .borrow() + .recv() + .on_err(|e| tracing::error!("error receiving: {:?}", e)) + .unwrap() + .to_msg() + .unwrap(); + + if let Message::SyscallAnswer(SyscallAnswer::GetExecutionInfoV2 { + result, + remaining_gas, + }) = result + { + *gas = remaining_gas; + result + } else { + tracing::error!( + "wrong message received (expected SyscallAnswer::GetExecutionInfoV2): {:?}", + result + ); + panic!(); + } } fn deploy( @@ -140,7 +175,10 @@ impl StarkNetSyscallHandler for SyscallHandler { *gas = remaining_gas; result } else { - tracing::error!("wrong message received: {:#?}", result); + tracing::error!( + "wrong message received (expected SyscallAnswer::Deploy): {:?}", + result + ); panic!(); } } @@ -173,7 +211,10 @@ impl StarkNetSyscallHandler for SyscallHandler { *gas = remaining_gas; result } else { - tracing::error!("wrong message received: {:#?}", result); + tracing::error!( + "wrong message received (expected SyscallAnswer::ReplaceClass): {:?}", + result + ); panic!(); } } @@ -214,7 +255,10 @@ impl StarkNetSyscallHandler for SyscallHandler { *gas = remaining_gas; result } else { - tracing::error!("wrong message received: {:#?}", result); + tracing::error!( + "wrong message received (expected SyscallAnswer::LibraryCall): {:?}", + result + ); panic!(); } } @@ -255,7 +299,10 @@ impl StarkNetSyscallHandler for SyscallHandler { *gas = remaining_gas; result } else { - tracing::error!("wrong message received: {:#?}", result); + tracing::error!( + "wrong message received (expected SyscallAnswer::CallContract): {:?}", + result + ); panic!(); } } @@ -294,7 +341,10 @@ impl StarkNetSyscallHandler for SyscallHandler { *gas = remaining_gas; result } else { - tracing::error!("wrong message received: {:#?}", result); + tracing::error!( + "wrong message received (expected SyscallAnswer::StorageRead): {:?}", + result + ); panic!(); } } @@ -335,7 +385,10 @@ impl StarkNetSyscallHandler for SyscallHandler { *gas = remaining_gas; result } else { - tracing::error!("wrong message received: {:#?}", result); + tracing::error!( + "wrong message received (expected SyscallAnswer::StorageWrite ): {:#?}", + result + ); panic!(); } } @@ -369,7 +422,10 @@ impl StarkNetSyscallHandler for SyscallHandler { *gas = remaining_gas; result } else { - tracing::error!("wrong message received: {:#?}", result); + tracing::error!( + "wrong message received (expected SyscallAnswer::EmitEvent): {:#?}", + result + ); panic!(); } } @@ -408,7 +464,10 @@ impl StarkNetSyscallHandler for SyscallHandler { *gas = remaining_gas; result } else { - tracing::error!("wrong message received: {:#?}", result); + tracing::error!( + "wrong message received (expected SyscallRequest::SendMessageToL1): {:#?}", + result + ); panic!(); } } @@ -445,7 +504,10 @@ impl StarkNetSyscallHandler for SyscallHandler { *gas = remaining_gas; result } else { - tracing::error!("wrong message received: {:#?}", result); + tracing::error!( + "wrong message received (expected SyscallAnswer::Keccak): {:#?}", + result + ); panic!(); } } @@ -456,6 +518,7 @@ impl StarkNetSyscallHandler for SyscallHandler { _p1: cairo_native::starknet::Secp256k1Point, _remaining_gas: &mut u128, ) -> SyscallResult { + tracing::error!("todo: secp256k1_add"); todo!() } @@ -465,6 +528,7 @@ impl StarkNetSyscallHandler for SyscallHandler { _y_parity: bool, _gas: &mut u128, ) -> SyscallResult> { + tracing::error!("todo: secp256k1_get_point_from_x"); todo!() } @@ -473,6 +537,7 @@ impl StarkNetSyscallHandler for SyscallHandler { _p: cairo_native::starknet::Secp256k1Point, _gas: &mut u128, ) -> SyscallResult<(cairo_native::starknet::U256, cairo_native::starknet::U256)> { + tracing::error!("todo: secp256k1_get_xy"); todo!() } @@ -482,6 +547,7 @@ impl StarkNetSyscallHandler for SyscallHandler { _m: cairo_native::starknet::U256, _remaining_gas: &mut u128, ) -> SyscallResult { + tracing::error!("todo: secp256k1_mul"); todo!() } @@ -491,6 +557,7 @@ impl StarkNetSyscallHandler for SyscallHandler { _y: cairo_native::starknet::U256, _gas: &mut u128, ) -> SyscallResult> { + tracing::error!("todo: secp256k1_new"); todo!() } @@ -500,6 +567,7 @@ impl StarkNetSyscallHandler for SyscallHandler { _p1: cairo_native::starknet::Secp256r1Point, _remaining_gas: &mut u128, ) -> SyscallResult { + tracing::error!("todo: secp256r1_add"); todo!() } @@ -509,6 +577,7 @@ impl StarkNetSyscallHandler for SyscallHandler { _y_parity: bool, _remaining_gas: &mut u128, ) -> SyscallResult> { + tracing::error!("todo: secp256r1_get_point_from_x"); todo!() } @@ -517,6 +586,7 @@ impl StarkNetSyscallHandler for SyscallHandler { _p: cairo_native::starknet::Secp256r1Point, _gas: &mut u128, ) -> SyscallResult<(cairo_native::starknet::U256, cairo_native::starknet::U256)> { + tracing::error!("todo: secp256r1_get_xy"); todo!() } @@ -526,6 +596,7 @@ impl StarkNetSyscallHandler for SyscallHandler { _m: cairo_native::starknet::U256, _remaining_gas: &mut u128, ) -> SyscallResult { + tracing::error!("todo: secp256r1_mul"); todo!() } @@ -535,6 +606,7 @@ impl StarkNetSyscallHandler for SyscallHandler { _y: cairo_native::starknet::U256, _remaining_gas: &mut u128, ) -> SyscallResult> { + tracing::error!("todo: secp256r1_new"); todo!() } @@ -640,7 +712,7 @@ pub fn main() -> Result<(), Box> { let message: Message = receiver.borrow().recv()?.to_msg()?; match message { - Message::ExecuteJIT { + Message::ExecuteProgram { id, class_hash, program, @@ -648,7 +720,7 @@ pub fn main() -> Result<(), Box> { function_idx, gas, } => { - tracing::info!("Message: ExecuteJIT with id {}", id); + tracing::info!("Message: ExecuteProgram with id {}", id); sender.send(Message::Ack(id).wrap()?)?; tracing::info!("sent ack: {}", id); diff --git a/src/sandboxing/mod.rs b/src/sandboxing/mod.rs index 7730fedd3..2743fa343 100644 --- a/src/sandboxing/mod.rs +++ b/src/sandboxing/mod.rs @@ -12,7 +12,7 @@ use uuid::Uuid; use cairo_native::{ execution_result::ContractExecutionResult, - starknet::{ExecutionInfo, StarkNetSyscallHandler, SyscallResult}, + starknet::{ExecutionInfo, ExecutionInfoV2, StarkNetSyscallHandler, SyscallResult}, }; use thiserror::Error; @@ -31,7 +31,7 @@ pub enum SandboxError { #[allow(clippy::large_enum_variant)] #[derive(Debug, Clone, Serialize, Deserialize)] pub enum Message { - ExecuteJIT { + ExecuteProgram { id: Uuid, class_hash: ClassHash, program: VersionedProgram, @@ -59,6 +59,9 @@ pub enum SyscallRequest { GetExecutionInfo { gas: u128, }, + GetExecutionInfoV2 { + gas: u128, + }, Deploy { class_hash: Felt, contract_address_salt: Felt, @@ -120,6 +123,10 @@ pub enum SyscallAnswer { result: SyscallResult, remaining_gas: u128, }, + GetExecutionInfoV2 { + result: SyscallResult, + remaining_gas: u128, + }, Deploy { result: SyscallResult<(Felt, Vec)>, remaining_gas: u128, @@ -235,7 +242,7 @@ impl IsolatedExecutor { tracing::debug!("running program"); let id = Uuid::new_v4(); - let msg = Message::ExecuteJIT { + let msg = Message::ExecuteProgram { id, class_hash, program: program.into_artifact(), @@ -248,7 +255,7 @@ impl IsolatedExecutor { loop { let msg = self.receiver.recv()?.to_msg()?; match msg { - Message::ExecuteJIT { .. } => unreachable!(), + Message::ExecuteProgram { .. } => unreachable!(), Message::ExecutionResult { id: recv_id, result, @@ -285,6 +292,16 @@ impl IsolatedExecutor { .wrap()?, )?; } + SyscallRequest::GetExecutionInfoV2 { mut gas } => { + let result = handler.get_execution_info_v2(&mut gas); + self.sender.send( + Message::SyscallAnswer(SyscallAnswer::GetExecutionInfoV2 { + result, + remaining_gas: gas, + }) + .wrap()?, + )?; + } SyscallRequest::StorageRead { address_domain, address, From d7be0e841003d096b39b811ae34fa1232331b017 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Wed, 7 Feb 2024 14:35:34 +0100 Subject: [PATCH 14/15] fixes --- src/bin/native_executor.rs | 199 ++++++++++++++++++++++++++++--------- 1 file changed, 150 insertions(+), 49 deletions(-) diff --git a/src/bin/native_executor.rs b/src/bin/native_executor.rs index 82245e597..a9303ba8d 100644 --- a/src/bin/native_executor.rs +++ b/src/bin/native_executor.rs @@ -17,16 +17,20 @@ use cairo_native::{ }; use cairo_vm::Felt252 as Felt; use ipc_channel::ipc::{IpcOneShotServer, IpcReceiver, IpcSender}; -use starknet_in_rust::sandboxing::{Message, SyscallAnswer, SyscallRequest, WrappedMessage}; +use starknet_in_rust::{ + sandboxing::{Message, SyscallAnswer, SyscallRequest, WrappedMessage}, + utils::ClassHash, +}; use tracing::instrument; #[derive(Debug)] -struct SyscallHandler { +struct SyscallHandler<'a> { sender: IpcSender, receiver: Rc>>, + cache: Rc>>, } -impl StarkNetSyscallHandler for SyscallHandler { +impl<'a> StarkNetSyscallHandler for SyscallHandler<'a> { #[instrument(skip(self))] fn get_block_hash(&mut self, block_number: u64, gas: &mut u128) -> SyscallResult { self.sender @@ -158,28 +162,73 @@ impl StarkNetSyscallHandler for SyscallHandler { .unwrap(), ) .expect("failed to send"); - let result = self - .receiver - .borrow() - .recv() - .on_err(|e| tracing::error!("error receiving: {:?}", e)) - .unwrap() - .to_msg() - .unwrap(); - if let Message::SyscallAnswer(SyscallAnswer::Deploy { - result, - remaining_gas, - }) = result - { - *gas = remaining_gas; - result - } else { - tracing::error!( - "wrong message received (expected SyscallAnswer::Deploy): {:?}", - result - ); - panic!(); + loop { + let result = self + .receiver + .borrow() + .recv() + .on_err(|e| tracing::error!("error receiving: {:?}", e)) + .unwrap() + .to_msg() + .unwrap(); + + match result { + Message::SyscallAnswer(SyscallAnswer::Deploy { + result, + remaining_gas, + }) => { + *gas = remaining_gas; + return result; + } + Message::ExecuteProgram { + id, + class_hash: exec_class_hash, + program, + inputs, + function_idx, + gas: exec_gas, + } => { + tracing::info!("Message: ExecuteProgram (inside deploy) with id {}", id); + self.sender.send(Message::Ack(id).wrap().unwrap()).unwrap(); + tracing::info!("sent ack: {}", id); + + let program = program.into_v1().unwrap().program; + let native_executor = self.cache.borrow_mut().compile_and_insert( + exec_class_hash, + &program, + OptLevel::Default, + ); + + let entry_point_fn = find_entry_point_by_idx(&program, function_idx).unwrap(); + + let fn_id = &entry_point_fn.id; + + let result = native_executor + .invoke_contract_dynamic( + fn_id, + &inputs, + exec_gas, + Some(&SyscallHandlerMeta::new(self)), + ) + .unwrap(); + + tracing::info!("invoked with result: {:?}", result); + + self.sender + .send(Message::ExecutionResult { id, result }.wrap().unwrap()) + .unwrap(); + + tracing::info!("sent result msg"); + } + msg => { + tracing::error!( + "wrong message received (expected Message::ExecuteProgram or SyscallAnswer::Deploy): {:?}", + msg + ); + panic!() + } + } } } @@ -282,28 +331,76 @@ impl StarkNetSyscallHandler for SyscallHandler { .unwrap(), ) .expect("failed to send"); - let result = self - .receiver - .borrow() - .recv() - .on_err(|e| tracing::error!("error receiving: {:?}", e)) - .unwrap() - .to_msg() - .unwrap(); - if let Message::SyscallAnswer(SyscallAnswer::CallContract { - result, - remaining_gas, - }) = result - { - *gas = remaining_gas; - result - } else { - tracing::error!( - "wrong message received (expected SyscallAnswer::CallContract): {:?}", - result - ); - panic!(); + loop { + let result = self + .receiver + .borrow() + .recv() + .on_err(|e| tracing::error!("error receiving: {:?}", e)) + .unwrap() + .to_msg() + .unwrap(); + + match result { + Message::SyscallAnswer(SyscallAnswer::CallContract { + result, + remaining_gas, + }) => { + *gas = remaining_gas; + return result; + } + Message::ExecuteProgram { + id, + class_hash: exec_class_hash, + program, + inputs, + function_idx, + gas: exec_gas, + } => { + tracing::info!( + "Message: ExecuteProgram (inside call_contract) with id {}", + id + ); + self.sender.send(Message::Ack(id).wrap().unwrap()).unwrap(); + tracing::info!("sent ack: {}", id); + + let program = program.into_v1().unwrap().program; + let native_executor = self.cache.borrow_mut().compile_and_insert( + exec_class_hash, + &program, + OptLevel::Default, + ); + + let entry_point_fn = find_entry_point_by_idx(&program, function_idx).unwrap(); + + let fn_id = &entry_point_fn.id; + + let result = native_executor + .invoke_contract_dynamic( + fn_id, + &inputs, + exec_gas, + Some(&SyscallHandlerMeta::new(self)), + ) + .unwrap(); + + tracing::info!("invoked with result: {:?}", result); + + self.sender + .send(Message::ExecutionResult { id, result }.wrap().unwrap()) + .unwrap(); + + tracing::info!("sent result msg"); + } + msg => { + tracing::error!( + "wrong message received (expected Message::ExecuteProgram or SyscallAnswer::CallContract): {:?}", + msg + ); + panic!() + } + } } } @@ -698,13 +795,15 @@ pub fn main() -> Result<(), Box> { let native_context = NativeContext::new(); tracing::info!("initialized native context"); + let cache = Rc::new(RefCell::new(JitProgramCache::new(&native_context))); + tracing::info!("initialized program cache"); + let mut syscall_handler = SyscallHandler { sender: sender.clone(), receiver: receiver.clone(), + cache: cache.clone(), }; - - let mut cache = JitProgramCache::new(&native_context); - tracing::info!("initialized program cache"); + tracing::info!("initialized syscall handler"); loop { tracing::info!("waiting for message"); @@ -726,7 +825,9 @@ pub fn main() -> Result<(), Box> { let program = program.into_v1()?.program; let native_executor = - cache.compile_and_insert(class_hash, &program, OptLevel::Default); + cache + .borrow_mut() + .compile_and_insert(class_hash, &program, OptLevel::Default); let entry_point_fn = find_entry_point_by_idx(&program, function_idx).unwrap(); From 37cddc82cb4919b84ecbd548776d4bb81692b4e8 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Wed, 7 Feb 2024 17:23:40 +0100 Subject: [PATCH 15/15] fix --- src/bin/native_executor.rs | 41 +++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/bin/native_executor.rs b/src/bin/native_executor.rs index a9303ba8d..deec75232 100644 --- a/src/bin/native_executor.rs +++ b/src/bin/native_executor.rs @@ -194,11 +194,15 @@ impl<'a> StarkNetSyscallHandler for SyscallHandler<'a> { tracing::info!("sent ack: {}", id); let program = program.into_v1().unwrap().program; - let native_executor = self.cache.borrow_mut().compile_and_insert( - exec_class_hash, - &program, - OptLevel::Default, - ); + let native_executor = { + let mut cache = self.cache.borrow_mut(); + let cache = &mut *cache; + if let Some(executor) = cache.get(&exec_class_hash) { + executor + } else { + cache.compile_and_insert(exec_class_hash, &program, OptLevel::Default) + } + }; let entry_point_fn = find_entry_point_by_idx(&program, function_idx).unwrap(); @@ -366,11 +370,15 @@ impl<'a> StarkNetSyscallHandler for SyscallHandler<'a> { tracing::info!("sent ack: {}", id); let program = program.into_v1().unwrap().program; - let native_executor = self.cache.borrow_mut().compile_and_insert( - exec_class_hash, - &program, - OptLevel::Default, - ); + let native_executor = { + let mut cache = self.cache.borrow_mut(); + let cache = &mut *cache; + if let Some(executor) = cache.get(&exec_class_hash) { + executor + } else { + cache.compile_and_insert(exec_class_hash, &program, OptLevel::Default) + } + }; let entry_point_fn = find_entry_point_by_idx(&program, function_idx).unwrap(); @@ -824,10 +832,15 @@ pub fn main() -> Result<(), Box> { tracing::info!("sent ack: {}", id); let program = program.into_v1()?.program; - let native_executor = - cache - .borrow_mut() - .compile_and_insert(class_hash, &program, OptLevel::Default); + let native_executor = { + let mut cache = cache.borrow_mut(); + let cache = &mut *cache; + if let Some(executor) = cache.get(&class_hash) { + executor + } else { + cache.compile_and_insert(class_hash, &program, OptLevel::Default) + } + }; let entry_point_fn = find_entry_point_by_idx(&program, function_idx).unwrap();