From fdd6c978d5ad17980b930633852c5b08e6f8c5ec Mon Sep 17 00:00:00 2001 From: greged93 <82421016+greged93@users.noreply.github.com> Date: Thu, 21 Sep 2023 14:28:39 +0700 Subject: [PATCH] feat: implement blockifier `State` and `StateReader` traits (#107) * implement blockifier * move blockifier from workspace to maintain two versions * view toml * update manifests * lock * update from comments * add rust doc, update should panic * remove unnecessary comments * rename constants + TODO --- Cargo.lock | 180 +++++++++------------- Cargo.toml | 3 + crates/ef-testing/Cargo.toml | 4 +- crates/sequencer/Cargo.toml | 7 +- crates/sequencer/src/config.rs | 1 - crates/sequencer/src/constants.rs | 29 ++++ crates/sequencer/src/lib.rs | 2 +- crates/sequencer/src/sequencer.rs | 22 ++- crates/sequencer/src/state.rs | 242 +++++++++++++++++++++++++++++- 9 files changed, 365 insertions(+), 125 deletions(-) delete mode 100644 crates/sequencer/src/config.rs create mode 100644 crates/sequencer/src/constants.rs diff --git a/Cargo.lock b/Cargo.lock index b94b1a5d..ba8e40ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -63,9 +63,9 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "1.0.5" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c378d78423fdad8089616f827526ee33c19f2fddbd5de1629152c9593ba4783" +checksum = "ea5d730647d4fadd988536d06fecce94b7b4f2a7efdae548f1cf4b63205518ab" dependencies = [ "memchr", ] @@ -388,15 +388,6 @@ dependencies = [ "rand", ] -[[package]] -name = "array-init" -version = "0.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23589ecb866b460d3a0f1278834750268c607e8e28a1b982c907219f3178cd72" -dependencies = [ - "nodrop", -] - [[package]] name = "arrayvec" version = "0.7.4" @@ -749,7 +740,7 @@ dependencies = [ [[package]] name = "boa_ast" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9da687496858551c0ded0deaf66076970dde9389" +source = "git+https://github.com/boa-dev/boa#986b048560f6f1a6f0fbe2724d3b7c59207e75b6" dependencies = [ "bitflags 2.4.0", "boa_interner", @@ -762,7 +753,7 @@ dependencies = [ [[package]] name = "boa_engine" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9da687496858551c0ded0deaf66076970dde9389" +source = "git+https://github.com/boa-dev/boa#986b048560f6f1a6f0fbe2724d3b7c59207e75b6" dependencies = [ "bitflags 2.4.0", "boa_ast", @@ -800,7 +791,7 @@ dependencies = [ [[package]] name = "boa_gc" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9da687496858551c0ded0deaf66076970dde9389" +source = "git+https://github.com/boa-dev/boa#986b048560f6f1a6f0fbe2724d3b7c59207e75b6" dependencies = [ "boa_macros", "boa_profiler", @@ -811,7 +802,7 @@ dependencies = [ [[package]] name = "boa_icu_provider" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9da687496858551c0ded0deaf66076970dde9389" +source = "git+https://github.com/boa-dev/boa#986b048560f6f1a6f0fbe2724d3b7c59207e75b6" dependencies = [ "icu_collections", "icu_normalizer", @@ -824,7 +815,7 @@ dependencies = [ [[package]] name = "boa_interner" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9da687496858551c0ded0deaf66076970dde9389" +source = "git+https://github.com/boa-dev/boa#986b048560f6f1a6f0fbe2724d3b7c59207e75b6" dependencies = [ "boa_gc", "boa_macros", @@ -839,7 +830,7 @@ dependencies = [ [[package]] name = "boa_macros" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9da687496858551c0ded0deaf66076970dde9389" +source = "git+https://github.com/boa-dev/boa#986b048560f6f1a6f0fbe2724d3b7c59207e75b6" dependencies = [ "proc-macro2 1.0.67", "quote 1.0.33", @@ -850,7 +841,7 @@ dependencies = [ [[package]] name = "boa_parser" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9da687496858551c0ded0deaf66076970dde9389" +source = "git+https://github.com/boa-dev/boa#986b048560f6f1a6f0fbe2724d3b7c59207e75b6" dependencies = [ "bitflags 2.4.0", "boa_ast", @@ -870,7 +861,7 @@ dependencies = [ [[package]] name = "boa_profiler" version = "0.17.0" -source = "git+https://github.com/boa-dev/boa#9da687496858551c0ded0deaf66076970dde9389" +source = "git+https://github.com/boa-dev/boa#986b048560f6f1a6f0fbe2724d3b7c59207e75b6" [[package]] name = "brotli" @@ -1613,9 +1604,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.4.3" +version = "4.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84ed82781cea27b43c9b106a979fe450a13a31aab0500595fb3fc06616de08e6" +checksum = "b1d7b8d5ec32af0fadc644bf1fd509a688c2103b185644bb1e29d164e0703136" dependencies = [ "clap_builder", "clap_derive", @@ -1633,9 +1624,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.4.2" +version = "4.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bb9faaa7c2ef94b2743a21f5a29e6f0010dff4caa69ac8e9d6cf8b6fa74da08" +checksum = "5179bb514e4d7c2051749d8fcefa2ed6d06a9f4e6d69faf3805f5d80b8cf8d56" dependencies = [ "anstream", "anstyle", @@ -2418,9 +2409,9 @@ checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b" [[package]] name = "dyn-clone" -version = "1.0.13" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbfc4744c1b8f2a09adc0e55242f60b1af195d88596bd8700be74418c056c555" +checksum = "23d2f3407d9a573d666de4b5bdf10569d73ca9478087346697dcbae6244bfbcd" [[package]] name = "ecdsa" @@ -2551,9 +2542,9 @@ dependencies = [ [[package]] name = "enr" -version = "0.9.0" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0be7b2ac146c1f99fe245c02d16af0696450d8e06c135db75e10eeb9e642c20d" +checksum = "fe81b5c06ecfdbc71dd845216f225f53b62a10cb8a16c946836a3467f701d05b" dependencies = [ "base64 0.21.4", "bytes", @@ -2563,7 +2554,6 @@ dependencies = [ "rand", "rlp", "serde", - "serde-hex", "sha3", "zeroize", ] @@ -2822,7 +2812,7 @@ dependencies = [ [[package]] name = "ethers-core" version = "2.0.10" -source = "git+https://github.com/gakonst/ethers-rs#4ac0058f50571954596cabbe13a3d57351d9adac" +source = "git+https://github.com/gakonst/ethers-rs#08bcb67c36d9a2869950e51ecf76124c9febe035" dependencies = [ "arrayvec", "bytes", @@ -2863,7 +2853,7 @@ dependencies = [ [[package]] name = "ethers-etherscan" version = "2.0.10" -source = "git+https://github.com/gakonst/ethers-rs#4ac0058f50571954596cabbe13a3d57351d9adac" +source = "git+https://github.com/gakonst/ethers-rs#08bcb67c36d9a2869950e51ecf76124c9febe035" dependencies = [ "ethers-core 2.0.10 (git+https://github.com/gakonst/ethers-rs)", "reqwest", @@ -2992,7 +2982,7 @@ dependencies = [ [[package]] name = "ethers-solc" version = "2.0.10" -source = "git+https://github.com/gakonst/ethers-rs#4ac0058f50571954596cabbe13a3d57351d9adac" +source = "git+https://github.com/gakonst/ethers-rs#08bcb67c36d9a2869950e51ecf76124c9febe035" dependencies = [ "cfg-if", "const-hex", @@ -3168,7 +3158,7 @@ dependencies = [ [[package]] name = "foundry-config" version = "0.2.0" -source = "git+https://github.com/foundry-rs/foundry?branch=master#ecf9a10bab059130ef9b90d1b160b6a725a6e21a" +source = "git+https://github.com/foundry-rs/foundry?branch=master#83b9176a6d579e8cfd03741fe322a4420b3c9903" dependencies = [ "Inflector", "dirs-next", @@ -3348,7 +3338,7 @@ checksum = "6973ce8518068a71d404f428f6a5b563088545546a6bd8f9c0a7f2608149bc8a" dependencies = [ "genco-macros", "relative-path", - "smallvec 1.11.0", + "smallvec", ] [[package]] @@ -3438,7 +3428,7 @@ dependencies = [ "log", "once_cell", "signal-hook", - "smallvec 1.11.0", + "smallvec", "thiserror", "unicode-normalization", ] @@ -3482,7 +3472,7 @@ dependencies = [ "gix-quote", "kstring", "log", - "smallvec 1.11.0", + "smallvec", "thiserror", "unicode-bom", ] @@ -3544,7 +3534,7 @@ dependencies = [ "log", "memchr", "once_cell", - "smallvec 1.11.0", + "smallvec", "thiserror", "unicode-bom", "winnow", @@ -3657,7 +3647,7 @@ dependencies = [ "gix-path", "gix-quote", "gix-trace", - "smallvec 1.11.0", + "smallvec", "thiserror", ] @@ -3734,7 +3724,7 @@ dependencies = [ "gix-traverse", "itoa", "memmap2", - "smallvec 1.11.0", + "smallvec", "thiserror", ] @@ -3773,7 +3763,7 @@ dependencies = [ "gix-hash", "gix-object", "gix-revwalk", - "smallvec 1.11.0", + "smallvec", "thiserror", ] @@ -3793,7 +3783,7 @@ dependencies = [ "hex", "itoa", "nom", - "smallvec 1.11.0", + "smallvec", "thiserror", ] @@ -3834,7 +3824,7 @@ dependencies = [ "gix-traverse", "memmap2", "parking_lot 0.12.1", - "smallvec 1.11.0", + "smallvec", "thiserror", "uluru", ] @@ -3918,7 +3908,7 @@ dependencies = [ "gix-hash", "gix-revision", "gix-validate", - "smallvec 1.11.0", + "smallvec", "thiserror", ] @@ -3948,7 +3938,7 @@ dependencies = [ "gix-hash", "gix-hashtable", "gix-object", - "smallvec 1.11.0", + "smallvec", "thiserror", ] @@ -3997,7 +3987,7 @@ dependencies = [ "gix-hashtable", "gix-object", "gix-revwalk", - "smallvec 1.11.0", + "smallvec", "thiserror", ] @@ -4284,9 +4274,9 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "hermit-abi" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b" +checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" [[package]] name = "hex" @@ -4489,7 +4479,7 @@ dependencies = [ "icu_collections", "icu_properties", "icu_provider", - "smallvec 1.11.0", + "smallvec", "utf16_iter", "utf8_iter", "write16", @@ -5507,12 +5497,6 @@ dependencies = [ "rawpointer", ] -[[package]] -name = "maybe-uninit" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" - [[package]] name = "md-5" version = "0.10.5" @@ -5693,12 +5677,6 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" -[[package]] -name = "nodrop" -version = "0.1.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" - [[package]] name = "nom" version = "7.1.3" @@ -5986,9 +5964,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-src" -version = "300.1.3+3.1.2" +version = "300.1.5+3.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd2c101a165fff9935e34def4669595ab1c7847943c42be86e21503e482be107" +checksum = "559068e4c12950d7dcaa1857a61725c0d38d4fc03ff8e070ab31a75d6e316491" dependencies = [ "cc", ] @@ -6105,7 +6083,7 @@ dependencies = [ "instant", "libc", "redox_syscall 0.2.16", - "smallvec 1.11.0", + "smallvec", "winapi", ] @@ -6118,7 +6096,7 @@ dependencies = [ "cfg-if", "libc", "redox_syscall 0.3.5", - "smallvec 1.11.0", + "smallvec", "windows-targets 0.48.5", ] @@ -6319,14 +6297,14 @@ dependencies = [ [[package]] name = "phonenumber" -version = "0.3.2+8.13.9" +version = "0.3.3+8.13.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34749f64ea9d76f10cdc8a859588b57775f59177c7dd91f744d620bd62982d6f" +checksum = "635f3e6288e4f01c049d89332a031bd74f25d64b6fb94703ca966e819488cd06" dependencies = [ "bincode 1.3.3", "either", "fnv", - "itertools 0.10.5", + "itertools 0.11.0", "lazy_static", "nom", "quick-xml", @@ -6334,6 +6312,7 @@ dependencies = [ "regex-cache", "serde", "serde_derive", + "strum 0.24.1", "thiserror", ] @@ -6437,13 +6416,13 @@ checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" [[package]] name = "predicates" -version = "3.0.3" +version = "3.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09963355b9f467184c04017ced4a2ba2d75cbcb4e7462690d388233253d4b1a9" +checksum = "6dfc28575c2e3f19cb3c73b93af36460ae898d426eba6fc15b9bd2a5220758a0" dependencies = [ "anstyle", "difflib", - "itertools 0.10.5", + "itertools 0.11.0", "predicates-core", ] @@ -6679,9 +6658,9 @@ checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" [[package]] name = "rayon" -version = "1.7.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b" +checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1" dependencies = [ "either", "rayon-core", @@ -6689,14 +6668,12 @@ dependencies = [ [[package]] name = "rayon-core" -version = "1.11.0" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d" +checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed" dependencies = [ - "crossbeam-channel", "crossbeam-deque", "crossbeam-utils", - "num_cpus", ] [[package]] @@ -7471,9 +7448,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.13" +version = "0.38.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7db8590df6dfcd144d22afd1b83b36c21a18d7cbc1dc4bb5295a8712e9eb662" +checksum = "747c788e9ce8e92b12cd485c49ddf90723550b654b32508f979b71a7b1ecda4f" dependencies = [ "bitflags 2.4.0", "errno", @@ -7579,7 +7556,7 @@ dependencies = [ "parking_lot 0.11.2", "rustc-hash", "salsa-macros", - "smallvec 1.11.0", + "smallvec", ] [[package]] @@ -7918,7 +7895,9 @@ name = "sequencer" version = "0.1.0" dependencies = [ "blockifier", + "lazy_static", "rustc-hash", + "starknet", "starknet_api", ] @@ -7931,17 +7910,6 @@ dependencies = [ "serde_derive", ] -[[package]] -name = "serde-hex" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca37e3e4d1b39afd7ff11ee4e947efae85adfddf4841787bfa47c470e96dc26d" -dependencies = [ - "array-init", - "serde", - "smallvec 0.6.14", -] - [[package]] name = "serde-value" version = "0.7.0" @@ -8180,18 +8148,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "0.6.14" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b97fcaeba89edba30f044a10c6a3cc39df9c3f17d7cd829dd1446cab35f890e0" -dependencies = [ - "maybe-uninit", -] - -[[package]] -name = "smallvec" -version = "1.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9" +checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" [[package]] name = "smol_str" @@ -8546,6 +8505,9 @@ name = "strum" version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" +dependencies = [ + "strum_macros 0.24.3", +] [[package]] name = "strum" @@ -8732,9 +8694,9 @@ dependencies = [ [[package]] name = "termcolor" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +checksum = "6093bad37da69aab9d123a8091e4be0aa4a03e4d601ec641c327398315f62b64" dependencies = [ "winapi-util", ] @@ -8944,9 +8906,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.8" +version = "0.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "806fe8c2c87eccc8b3267cbae29ed3ab2d0bd37fca70ab622e46aaa9375ddb7d" +checksum = "1d68074620f57a0b21594d9735eb2e98ab38b17f80d3fcb189fca266771ca60d" dependencies = [ "bytes", "futures-core", @@ -9121,7 +9083,7 @@ dependencies = [ "once_cell", "regex", "sharded-slab", - "smallvec 1.11.0", + "smallvec", "thread_local", "tracing", "tracing-core", @@ -9285,9 +9247,9 @@ checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" [[package]] name = "unicode-width" -version = "0.1.10" +version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" +checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" [[package]] name = "unicode-xid" @@ -9546,9 +9508,9 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" dependencies = [ "winapi", ] diff --git a/Cargo.toml b/Cargo.toml index 1a6b1725..16791673 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,11 +47,14 @@ chrono = { version = "0.4.26", features = ["serde"] } ctor = "0.2.4" dotenv = "0.15.0" eyre = "0.6.8" +lazy_static = "1.4.0" regex = "1.9.3" reqwest = { version = "0.11.20", features = ["gzip"] } rstest = "0.18.1" thiserror = "1.0.47" tokio = { version = "1.21.2", features = ["macros"] } +tracing = "0.1.37" +tracing-subscriber = { version = "0.3.17", features = ["env-filter"] } walkdir = "2.3.3" zip = "0.6.6" diff --git a/crates/ef-testing/Cargo.toml b/crates/ef-testing/Cargo.toml index 21c02c1c..5bd2be00 100644 --- a/crates/ef-testing/Cargo.toml +++ b/crates/ef-testing/Cargo.toml @@ -40,12 +40,12 @@ regex = { workspace = true } reqwest = { workspace = true, optional = true } rstest = { workspace = true } tokio = { workspace = true } -tracing = "0.1.37" +tracing = { workspace = true } walkdir = { workspace = true } zip = { workspace = true, optional = true } [dev-dependencies] -tracing-subscriber = "0.3.17" +tracing-subscriber = { workspace = true } [features] ef-tests = [] diff --git a/crates/sequencer/Cargo.toml b/crates/sequencer/Cargo.toml index 9abd6584..c24dd664 100644 --- a/crates/sequencer/Cargo.toml +++ b/crates/sequencer/Cargo.toml @@ -13,8 +13,13 @@ license.workspace = true [dependencies] # Starknet -blockifier = { git = "https://github.com/starkware-libs/blockifier.git", tag = "v0.3.0-rc0" } +# TODO: remove the blockifier patch on the workspace once we can remove Katana. +blockifier = { package = "blockifier", git = "https://github.com/starkware-libs/blockifier.git", tag = "v0.3.0-rc0" } starknet_api = { workspace = true } +starknet = { workspace = true } # Other rustc-hash = "1.1.0" + +[dev-dependencies] +lazy_static = { workspace = true } diff --git a/crates/sequencer/src/config.rs b/crates/sequencer/src/config.rs deleted file mode 100644 index 989c9ba7..00000000 --- a/crates/sequencer/src/config.rs +++ /dev/null @@ -1 +0,0 @@ -pub struct SequencerConfig {} diff --git a/crates/sequencer/src/constants.rs b/crates/sequencer/src/constants.rs new file mode 100644 index 00000000..bc7320e1 --- /dev/null +++ b/crates/sequencer/src/constants.rs @@ -0,0 +1,29 @@ +#[cfg(test)] +pub mod test_constants { + use starknet::core::types::FieldElement; + use starknet_api::{ + block::{BlockNumber, BlockTimestamp}, + core::{ClassHash, CompiledClassHash, ContractAddress, PatriciaKey}, + hash::StarkFelt, + }; + + lazy_static::lazy_static! { + pub static ref TEST_CONTRACT_ADDRESS: ContractAddress = ContractAddress(*ONE_PATRICIA); + pub static ref TEST_CONTRACT_ACCOUNT: ContractAddress = ContractAddress(*TWO_PATRICIA); + pub static ref TEST_ADDRESS: StarkFelt = *ONE_FELT; + pub static ref TEST_CONTRACT: StarkFelt = *TWO_FELT; + pub static ref SENDER_ADDRESS: FieldElement = FieldElement::from(2u8); + pub static ref SEQUENCER_ADDRESS: ContractAddress = ContractAddress(TryInto::::try_into(StarkFelt::from(1234u16)).unwrap()); + pub static ref FEE_TOKEN_ADDRESS: ContractAddress = ContractAddress(TryInto::::try_into(StarkFelt::from(12345u16)).unwrap()); + + pub static ref ONE_FELT: StarkFelt = StarkFelt::from(1u8); + pub static ref TWO_FELT: StarkFelt = StarkFelt::from(2u8); + pub static ref ONE_PATRICIA: PatriciaKey = TryInto::::try_into(*ONE_FELT).unwrap(); + pub static ref TWO_PATRICIA: PatriciaKey = TryInto::::try_into(*TWO_FELT).unwrap(); + pub static ref ONE_CLASS_HASH: ClassHash = ClassHash(*ONE_FELT); + pub static ref TWO_CLASS_HASH: ClassHash = ClassHash(*TWO_FELT); + pub static ref ONE_COMPILED_CLASS_HASH: CompiledClassHash = CompiledClassHash(*ONE_FELT); + pub static ref ONE_BLOCK_NUMBER: BlockNumber = BlockNumber(1); + pub static ref ONE_BLOCK_TIMESTAMP: BlockTimestamp = BlockTimestamp(1); + } +} diff --git a/crates/sequencer/src/lib.rs b/crates/sequencer/src/lib.rs index 4f4593df..d43a0535 100644 --- a/crates/sequencer/src/lib.rs +++ b/crates/sequencer/src/lib.rs @@ -1,3 +1,3 @@ -pub mod config; +pub mod constants; pub mod sequencer; pub mod state; diff --git a/crates/sequencer/src/sequencer.rs b/crates/sequencer/src/sequencer.rs index e4d845b8..1f336a15 100644 --- a/crates/sequencer/src/sequencer.rs +++ b/crates/sequencer/src/sequencer.rs @@ -1,17 +1,27 @@ -use crate::config::SequencerConfig; +use blockifier::block_context::BlockContext; use blockifier::state::state_api::{State, StateReader}; /// Sequencer is the main struct of the sequencer crate. /// Using a trait bound for the state allows for better /// speed, as the type of the state is known at compile time. -pub struct Sequencer { - pub config: SequencerConfig, +/// We bound S such that a mutable reference to S (&'a mut S) +/// must implement State and StateReader. The `for` keyword +/// indicates that the bound must hold for any lifetime 'a. +/// For more details, check out https://doc.rust-lang.org/nomicon/hrtb.html +pub struct Sequencer +where + for<'a> &'a mut S: State + StateReader, +{ + pub context: BlockContext, pub state: S, } -impl Sequencer { +impl Sequencer +where + for<'a> &'a mut S: State + StateReader, +{ /// Creates a new Sequencer instance. - pub fn new(config: SequencerConfig, state: S) -> Self { - Self { config, state } + pub fn new(context: BlockContext, state: S) -> Self { + Self { context, state } } } diff --git a/crates/sequencer/src/state.rs b/crates/sequencer/src/state.rs index 1a375d97..46966626 100644 --- a/crates/sequencer/src/state.rs +++ b/crates/sequencer/src/state.rs @@ -1,8 +1,14 @@ +use blockifier::state::cached_state::CommitmentStateDiff; +use blockifier::state::errors::StateError; +use blockifier::state::state_api::{ + State as BlockifierState, StateReader as BlockifierStateReader, StateResult, +}; use blockifier::{ execution::contract_class::ContractClass, state::cached_state::ContractStorageKey, }; use rustc_hash::FxHashMap; use starknet_api::core::CompiledClassHash; +use starknet_api::state::StorageKey; use starknet_api::{ core::{ClassHash, ContractAddress, Nonce}, hash::StarkFelt, @@ -16,9 +22,235 @@ use starknet_api::{ /// See [rustc-hash](https://crates.io/crates/rustc-hash) for more information. #[derive(Default)] pub struct State { - pub classes: FxHashMap, - pub compiled_classes: FxHashMap, - pub contracts: FxHashMap, - pub storage: FxHashMap, - pub nonces: FxHashMap, + classes: FxHashMap, + compiled_class_hashes: FxHashMap, + contracts: FxHashMap, + storage: FxHashMap, + nonces: FxHashMap, +} + +/// State implementation for the sequencer. We use a mutable reference to the state +/// because this is what will be available during the implementation of the execution. +impl BlockifierState for &mut State { + fn set_storage_at( + &mut self, + contract_address: ContractAddress, + key: StorageKey, + value: StarkFelt, + ) { + self.storage.insert((contract_address, key), value); + } + + fn increment_nonce(&mut self, contract_address: ContractAddress) -> StateResult<()> { + let current_nonce = self + .nonces + .get(&contract_address) + .cloned() + .unwrap_or_default(); + + let mut current_nonce: u64 = current_nonce.0.try_into()?; + current_nonce += 1; + + self.nonces + .insert(contract_address, Nonce(StarkFelt::from(current_nonce))); + + Ok(()) + } + + fn set_class_hash_at( + &mut self, + contract_address: ContractAddress, + class_hash: ClassHash, + ) -> StateResult<()> { + self.contracts.insert(contract_address, class_hash); + Ok(()) + } + + fn set_contract_class( + &mut self, + class_hash: &ClassHash, + contract_class: ContractClass, + ) -> StateResult<()> { + self.classes.insert(class_hash.to_owned(), contract_class); + Ok(()) + } + + fn set_compiled_class_hash( + &mut self, + class_hash: ClassHash, + compiled_class_hash: CompiledClassHash, + ) -> StateResult<()> { + self.compiled_class_hashes + .insert(class_hash, compiled_class_hash); + Ok(()) + } + + fn to_state_diff(&self) -> CommitmentStateDiff { + unreachable!("to_state_diff should not be called in the sequencer") + } +} + +impl BlockifierStateReader for &mut State { + fn get_storage_at( + &mut self, + contract_address: ContractAddress, + key: StorageKey, + ) -> StateResult { + Ok(self + .storage + .get(&(contract_address, key)) + .cloned() + .unwrap_or_default()) + } + + fn get_nonce_at(&mut self, contract_address: ContractAddress) -> StateResult { + Ok(self + .nonces + .get(&contract_address) + .cloned() + .unwrap_or_default()) + } + + fn get_class_hash_at(&mut self, contract_address: ContractAddress) -> StateResult { + Ok(self + .contracts + .get(&contract_address) + .cloned() + .unwrap_or_default()) + } + + /// # Errors + /// + /// If the compiled class is not declared. + fn get_compiled_contract_class( + &mut self, + class_hash: &ClassHash, + ) -> StateResult { + self.classes + .get(class_hash) + .cloned() + .ok_or_else(|| StateError::UndeclaredClassHash(class_hash.to_owned())) + } + + /// # Errors + /// + /// If the compiled class hash is not declared. + fn get_compiled_class_hash(&mut self, class_hash: ClassHash) -> StateResult { + self.compiled_class_hashes + .get(&class_hash) + .cloned() + .ok_or_else(|| StateError::UndeclaredClassHash(class_hash)) + } +} + +#[cfg(test)] +mod tests { + use blockifier::execution::contract_class::ContractClassV0; + + use crate::constants::test_constants::{ + ONE_CLASS_HASH, ONE_COMPILED_CLASS_HASH, ONE_FELT, ONE_PATRICIA, TEST_CONTRACT_ADDRESS, + }; + + use super::*; + + #[test] + fn test_storage() { + // Given + let mut state = &mut State::default(); + + // When + state.set_storage_at(*TEST_CONTRACT_ADDRESS, StorageKey(*ONE_PATRICIA), *ONE_FELT); + + // Then + let expected = *ONE_FELT; + let actual = state + .get_storage_at(*TEST_CONTRACT_ADDRESS, StorageKey(*ONE_PATRICIA)) + .unwrap(); + assert_eq!(expected, actual); + } + + #[test] + fn test_nonce() { + // Given + let mut state = &mut State::default(); + + // When + state.increment_nonce(*TEST_CONTRACT_ADDRESS).unwrap(); + + // Then + let expected = Nonce(*ONE_FELT); + let actual = state.get_nonce_at(*TEST_CONTRACT_ADDRESS).unwrap(); + assert_eq!(expected, actual); + } + + #[test] + fn test_class_hash() { + // Given + let mut state = &mut State::default(); + + // When + state + .set_class_hash_at(*TEST_CONTRACT_ADDRESS, *ONE_CLASS_HASH) + .unwrap(); + + // Then + let expected = *ONE_CLASS_HASH; + let actual = state.get_class_hash_at(*TEST_CONTRACT_ADDRESS).unwrap(); + assert_eq!(expected, actual); + } + + #[test] + fn test_contract_class() { + // Given + let mut state = &mut State::default(); + + // When + state + .set_contract_class( + &ONE_CLASS_HASH, + ContractClass::V0(ContractClassV0::default()), + ) + .unwrap(); + + // Then + let expected = ContractClass::V0(ContractClassV0::default()); + let actual = state.get_compiled_contract_class(&ONE_CLASS_HASH).unwrap(); + assert_eq!(expected, actual); + } + + #[test] + #[should_panic(expected = "UndeclaredClassHash")] + fn test_uninitialized_contract_class() { + // Given + let mut state = &mut State::default(); + + // When + state.get_compiled_contract_class(&ONE_CLASS_HASH).unwrap(); + } + + #[test] + fn test_compiled_class_hash() { + // Given + let mut state = &mut State::default(); + + // When + state + .set_compiled_class_hash(*ONE_CLASS_HASH, *ONE_COMPILED_CLASS_HASH) + .unwrap(); + + // Then + let expected = *ONE_COMPILED_CLASS_HASH; + let actual = state.get_compiled_class_hash(*ONE_CLASS_HASH).unwrap(); + assert_eq!(expected, actual); + } + + #[test] + #[should_panic(expected = "UndeclaredClassHash")] + fn test_uninitialized_compiled_class_hash() { + // Given + let mut state = &mut State::default(); + + // When + state.get_compiled_class_hash(*ONE_CLASS_HASH).unwrap(); + } }