From 48cc744e216a01a11d4c6f5893b3164d669cb576 Mon Sep 17 00:00:00 2001 From: Jonathan LEI Date: Sun, 8 Dec 2024 09:22:03 +0800 Subject: [PATCH] feat: switch to jsonrpc v0.7.x --- Cargo.lock | 319 ++++++++++++++++++---------------------------------- Cargo.toml | 4 +- src/main.rs | 127 +++++---------------- 3 files changed, 144 insertions(+), 306 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f5d78ed..c0a9503 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -131,70 +131,6 @@ version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b13c32d80ecc7ab747b80c3784bce54ee8a7a0cc4fbda9bf4cda2cf6fe90854" -[[package]] -name = "ark-ff" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec847af850f44ad29048935519032c33da8aa03340876d351dfab5660d2966ba" -dependencies = [ - "ark-ff-asm", - "ark-ff-macros", - "ark-serialize", - "ark-std", - "derivative", - "digest", - "itertools", - "num-bigint", - "num-traits", - "paste", - "rustc_version", - "zeroize", -] - -[[package]] -name = "ark-ff-asm" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ed4aa4fe255d0bc6d79373f7e31d2ea147bcf486cba1be5ba7ea85abdb92348" -dependencies = [ - "quote", - "syn 1.0.109", -] - -[[package]] -name = "ark-ff-macros" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7abe79b0e4288889c4574159ab790824d0033b9fdcb2a112a3182fac2e514565" -dependencies = [ - "num-bigint", - "num-traits", - "proc-macro2", - "quote", - "syn 1.0.109", -] - -[[package]] -name = "ark-serialize" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adb7b85a02b83d2f22f89bd5cac66c9c89474240cb6207cb1efc16d098e822a5" -dependencies = [ - "ark-std", - "digest", - "num-bigint", -] - -[[package]] -name = "ark-std" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94893f1e0c6eeab764ade8dc4c0db24caf4fe7cbbaafc0eba0a9030f447b5185" -dependencies = [ - "num-traits", - "rand", -] - [[package]] name = "arrayvec" version = "0.7.4" @@ -245,12 +181,6 @@ dependencies = [ "rustc-demangle", ] -[[package]] -name = "base64" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" - [[package]] name = "base64" version = "0.21.2" @@ -258,16 +188,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "604178f6c5c21f02dc555784810edfb88d34ac2c73b2eae109655649ee73ce3d" [[package]] -name = "bigdecimal" -version = "0.3.1" +name = "base64" +version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6773ddc0eafc0e509fb60e48dff7f450f8e674a0686ae8605e8d9901bd5eefa" -dependencies = [ - "num-bigint", - "num-integer", - "num-traits", - "serde", -] +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" [[package]] name = "bitflags" @@ -508,24 +432,14 @@ dependencies = [ [[package]] name = "deranged" -version = "0.3.7" +version = "0.3.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7684a49fb1af197853ef7b2ee694bc1f5b4179556f1e5710e1760c5db6f5e929" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" dependencies = [ + "powerfmt", "serde", ] -[[package]] -name = "derivative" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "digest" version = "0.10.7" @@ -537,12 +451,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "either" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" - [[package]] name = "encoding_rs" version = "0.8.32" @@ -1007,6 +915,7 @@ checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d" dependencies = [ "equivalent", "hashbrown 0.14.0", + "serde", ] [[package]] @@ -1035,15 +944,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "itertools" -version = "0.10.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" -dependencies = [ - "either", -] - [[package]] name = "itoa" version = "1.0.9" @@ -1086,6 +986,28 @@ dependencies = [ "cpufeatures", ] +[[package]] +name = "lambdaworks-crypto" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbc2a4da0d9e52ccfe6306801a112e81a8fc0c76aa3e4449fefeda7fef72bb34" +dependencies = [ + "lambdaworks-math", + "serde", + "sha2", + "sha3", +] + +[[package]] +name = "lambdaworks-math" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1bd2632acbd9957afc5aeec07ad39f078ae38656654043bf16e046fa2730e23" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "libc" version = "0.2.147" @@ -1157,6 +1079,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + [[package]] name = "num-integer" version = "0.1.45" @@ -1169,9 +1097,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.16" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", ] @@ -1250,12 +1178,6 @@ dependencies = [ "windows-targets 0.48.1", ] -[[package]] -name = "paste" -version = "1.0.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" - [[package]] name = "pbkdf2" version = "0.11.0" @@ -1283,6 +1205,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -1514,15 +1442,6 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" -[[package]] -name = "rustc_version" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" -dependencies = [ - "semver", -] - [[package]] name = "rustix" version = "0.38.6" @@ -1610,26 +1529,20 @@ dependencies = [ "untrusted", ] -[[package]] -name = "semver" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" - [[package]] name = "serde" -version = "1.0.180" +version = "1.0.193" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ea67f183f058fe88a4e3ec6e2788e003840893b91bac4559cabedd00863b3ed" +checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.180" +version = "1.0.193" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24e744d7782b686ab3b73267ef05697159cc0e5abbed3f47f9933165e5219036" +checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" dependencies = [ "proc-macro2", "quote", @@ -1672,15 +1585,17 @@ dependencies = [ [[package]] name = "serde_with" -version = "2.3.3" +version = "3.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07ff71d2c147a7b57362cead5e22f772cd52f6ab31cfcd9edcd7f6aeb2a0afbe" +checksum = "8e28bdad6db2b8340e449f7108f020b3b092e8583a9e3fb82713e1d4e71fe817" dependencies = [ - "base64 0.13.1", + "base64 0.22.1", "chrono", "hex", "indexmap 1.9.3", + "indexmap 2.0.0", "serde", + "serde_derive", "serde_json", "serde_with_macros", "time", @@ -1688,9 +1603,9 @@ dependencies = [ [[package]] name = "serde_with_macros" -version = "2.3.3" +version = "3.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "881b6f881b17d13214e5d494c939ebab463d01264ce1811e9d4ac3a882e7695f" +checksum = "9d846214a9854ef724f3da161b426242d8de7c1fc7de2f89bb1efcb154dca79d" dependencies = [ "darling", "proc-macro2", @@ -1772,14 +1687,14 @@ checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" [[package]] name = "starknet" -version = "0.8.0" -source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=6cadb198644cf5e942187896c2e6bb3ecc9edc6e#6cadb198644cf5e942187896c2e6bb3ecc9edc6e" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f0c9ac3809cc7630784e8c8565fa3013af819d83c97aa2720d566016d439011" dependencies = [ "starknet-accounts", "starknet-contract", "starknet-core", "starknet-crypto", - "starknet-ff", "starknet-macros", "starknet-providers", "starknet-signers", @@ -1787,12 +1702,14 @@ dependencies = [ [[package]] name = "starknet-accounts" -version = "0.7.0" -source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=6cadb198644cf5e942187896c2e6bb3ecc9edc6e#6cadb198644cf5e942187896c2e6bb3ecc9edc6e" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ee27ded58ade61da410fccafd57ed5429b0e79a9d62a4ae8b65818cb9d6f400" dependencies = [ "async-trait", "auto_impl", "starknet-core", + "starknet-crypto", "starknet-providers", "starknet-signers", "thiserror", @@ -1800,8 +1717,9 @@ dependencies = [ [[package]] name = "starknet-contract" -version = "0.7.0" -source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=6cadb198644cf5e942187896c2e6bb3ecc9edc6e#6cadb198644cf5e942187896c2e6bb3ecc9edc6e" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd6ee5762d24c4f06ab7e9406550925df406712e73719bd2de905c879c674a87" dependencies = [ "serde", "serde_json", @@ -1814,10 +1732,12 @@ dependencies = [ [[package]] name = "starknet-core" -version = "0.8.0" -source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=6cadb198644cf5e942187896c2e6bb3ecc9edc6e#6cadb198644cf5e942187896c2e6bb3ecc9edc6e" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2538240cbe6663c673fe77465f294da707080f39678dd7066761554899e46100" dependencies = [ "base64 0.21.2", + "crypto-bigint", "flate2", "hex", "serde", @@ -1826,13 +1746,14 @@ dependencies = [ "serde_with", "sha3", "starknet-crypto", - "starknet-ff", + "starknet-types-core", ] [[package]] name = "starknet-crypto" -version = "0.6.1" -source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=6cadb198644cf5e942187896c2e6bb3ecc9edc6e#6cadb198644cf5e942187896c2e6bb3ecc9edc6e" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ded22ccf4cb9e572ce3f77de6066af53560cd2520d508876c83bb1e6b29d5cbc" dependencies = [ "crypto-bigint", "hex", @@ -1842,48 +1763,25 @@ dependencies = [ "num-traits", "rfc6979", "sha2", - "starknet-crypto-codegen", "starknet-curve", - "starknet-ff", + "starknet-types-core", "zeroize", ] -[[package]] -name = "starknet-crypto-codegen" -version = "0.3.2" -source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=6cadb198644cf5e942187896c2e6bb3ecc9edc6e#6cadb198644cf5e942187896c2e6bb3ecc9edc6e" -dependencies = [ - "starknet-curve", - "starknet-ff", - "syn 2.0.28", -] - [[package]] name = "starknet-curve" -version = "0.4.0" -source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=6cadb198644cf5e942187896c2e6bb3ecc9edc6e#6cadb198644cf5e942187896c2e6bb3ecc9edc6e" -dependencies = [ - "starknet-ff", -] - -[[package]] -name = "starknet-ff" -version = "0.3.6" -source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=6cadb198644cf5e942187896c2e6bb3ecc9edc6e#6cadb198644cf5e942187896c2e6bb3ecc9edc6e" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcde6bd74269b8161948190ace6cf069ef20ac6e79cd2ba09b320efa7500b6de" dependencies = [ - "ark-ff", - "bigdecimal", - "crypto-bigint", - "getrandom", - "hex", - "num-bigint", - "serde", + "starknet-types-core", ] [[package]] name = "starknet-macros" -version = "0.1.5" -source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=6cadb198644cf5e942187896c2e6bb3ecc9edc6e#6cadb198644cf5e942187896c2e6bb3ecc9edc6e" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8986a940af916fc0a034f4e42c6ba76d94f1e97216d75447693dfd7aefaf3ef2" dependencies = [ "starknet-core", "syn 2.0.28", @@ -1891,13 +1789,15 @@ dependencies = [ [[package]] name = "starknet-providers" -version = "0.8.0" -source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=6cadb198644cf5e942187896c2e6bb3ecc9edc6e#6cadb198644cf5e942187896c2e6bb3ecc9edc6e" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60e8e69ba7a36dea2d28333be82b4011f8784333d3ae5618482b6587c1ffb66c" dependencies = [ "async-trait", "auto_impl", "ethereum-types", "flate2", + "getrandom", "log", "reqwest", "serde", @@ -1910,19 +1810,35 @@ dependencies = [ [[package]] name = "starknet-signers" -version = "0.6.0" -source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=6cadb198644cf5e942187896c2e6bb3ecc9edc6e#6cadb198644cf5e942187896c2e6bb3ecc9edc6e" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70b9e01b61ae51d722e2b100d6ef913c5a2e70d1ea672733d385f7296d6055ef" dependencies = [ "async-trait", "auto_impl", "crypto-bigint", "eth-keystore", + "getrandom", "rand", "starknet-core", "starknet-crypto", "thiserror", ] +[[package]] +name = "starknet-types-core" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa1b9e01ccb217ab6d475c5cda05dbb22c30029f7bb52b192a010a00d77a3d74" +dependencies = [ + "lambdaworks-crypto", + "lambdaworks-math", + "num-bigint", + "num-integer", + "num-traits", + "serde", +] + [[package]] name = "static_assertions" version = "1.1.0" @@ -1991,12 +1907,14 @@ dependencies = [ [[package]] name = "time" -version = "0.3.25" +version = "0.3.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0fdd63d58b18d663fbdf70e049f00a22c8e42be082203be7f26589213cd75ea" +checksum = "35e7868883861bd0e56d9ac6efcaaca0d6d5d82a2a7ec8209ff492c07cf37b21" dependencies = [ "deranged", "itoa", + "num-conv", + "powerfmt", "serde", "time-core", "time-macros", @@ -2004,16 +1922,17 @@ dependencies = [ [[package]] name = "time-core" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7300fbefb4dadc1af235a9cef3737cea692a9d97e1b9cbcd4ebdae6f8868e6fb" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.11" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb71511c991639bb078fd5bf97757e03914361c48100d52878b8e52b46fb92cd" +checksum = "2834e6017e3e5e4b9834939793b282bc03b37a3336245fa820e35e233e2a85de" dependencies = [ + "num-conv", "time-core", ] @@ -2535,17 +2454,3 @@ name = "zeroize" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9" -dependencies = [ - "zeroize_derive", -] - -[[package]] -name = "zeroize_derive" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.28", -] diff --git a/Cargo.toml b/Cargo.toml index a0de3fe..9230df5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,8 @@ log = "0.4.21" reqwest = { version = "0.11.18", default-features = false, features = ["rustls-tls"] } serde = "1.0.180" serde_json = "1.0.104" -serde_with = "2.3.2" +serde_with = "3.11.0" sha1 = "0.10.6" -starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "6cadb198644cf5e942187896c2e6bb3ecc9edc6e" } +starknet = "0.12.0" tokio = { version = "1.29.1", features = ["full"] } url = "2.4.0" diff --git a/src/main.rs b/src/main.rs index 80a0c71..cef9ce7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::{collections::HashSet, fs::File, io::Write, path::PathBuf, time::Duration}; +use std::{fs::File, io::Write, path::PathBuf, time::Duration}; use anyhow::Result; use clap::Parser; @@ -10,8 +10,9 @@ use starknet::{ core::{ serde::unsigned_field_element::UfeHex, types::{ - BlockId, BlockTag, BlockWithTxs, EmittedEvent, EventFilter, FieldElement, - MaybePendingBlockWithTxs, PendingBlockWithTxs, StarknetError, Transaction, + BlockId, BlockTag, BlockWithReceipts, Felt, MaybePendingBlockWithReceipts, + PendingBlockWithReceipts, StarknetError, Transaction, TransactionReceipt, + TransactionWithReceipt, }, }, providers::{ @@ -46,14 +47,14 @@ struct Checkpoint { struct BlockInfo { number: u64, #[serde_as(as = "UfeHex")] - hash: FieldElement, + hash: Felt, } enum MaybePendingBlock { - Confirmed(BlockWithTxs), + Confirmed(BlockWithReceipts), Pending { pseudo_height: u64, - block: PendingBlockWithTxs, + block: PendingBlockWithReceipts, }, } @@ -62,7 +63,7 @@ struct PendingBlockInfo { } impl MaybePendingBlock { - pub fn parent_hash(&self) -> FieldElement { + pub fn parent_hash(&self) -> Felt { match self { Self::Confirmed(block) => block.parent_hash, Self::Pending { block, .. } => block.parent_hash, @@ -83,14 +84,14 @@ impl MaybePendingBlock { } } - pub fn transactions(&self) -> &[Transaction] { + pub fn transactions(&self) -> &[TransactionWithReceipt] { match self { Self::Confirmed(block) => &block.transactions, Self::Pending { block, .. } => &block.transactions, } } - pub fn block_hash(&self) -> FieldElement { + pub fn block_hash(&self) -> Felt { match self { Self::Confirmed(block) => block.block_hash, Self::Pending { @@ -129,7 +130,7 @@ impl MaybePendingBlock { buffer[(8 + 8 + 4 + 8)..].copy_from_slice(&hasher.finalize()[0..4]); // This cannot fail as the buffer is always in range - FieldElement::from_bytes_be(&buffer).unwrap() + Felt::from_bytes_be(&buffer) } } } @@ -175,20 +176,20 @@ async fn run( loop { let (expected_parent_hash, current_block_number) = match checkpoint.latest_blocks.last() { Some(last_block) => (last_block.hash, last_block.number + 1), - None => (FieldElement::ZERO, 0), + None => (Felt::ZERO, 0), }; let current_block = match jsonrpc_client - .get_block_with_txs(BlockId::Number(current_block_number)) + .get_block_with_receipts(BlockId::Number(current_block_number)) .await { - Ok(MaybePendingBlockWithTxs::Block(block)) => { + Ok(MaybePendingBlockWithReceipts::Block(block)) => { // Got a confirmed block. Clear the pending info now last_pending_block = None; MaybePendingBlock::Confirmed(block) } - Ok(MaybePendingBlockWithTxs::PendingBlock(_)) => { + Ok(MaybePendingBlockWithReceipts::PendingBlock(_)) => { error!("Unexpected pending block"); tokio::time::sleep(FAILURE_BACKOFF).await; continue; @@ -198,15 +199,15 @@ async fn run( )) => { // No new block. Try the pending block now match jsonrpc_client - .get_block_with_txs(BlockId::Tag(BlockTag::Pending)) + .get_block_with_receipts(BlockId::Tag(BlockTag::Pending)) .await { - Ok(MaybePendingBlockWithTxs::Block(_)) => { + Ok(MaybePendingBlockWithReceipts::Block(_)) => { error!("Unexpected confirmed block"); tokio::time::sleep(FAILURE_BACKOFF).await; continue; } - Ok(MaybePendingBlockWithTxs::PendingBlock(block)) => { + Ok(MaybePendingBlockWithReceipts::PendingBlock(block)) => { // Unlike with confirmed blocks, we simply discard non-linkable pending // blocks if block.parent_hash != expected_parent_hash { @@ -253,7 +254,7 @@ async fn run( } } - if let Err(err) = handle_block(jsonrpc_client, ¤t_block).await { + if let Err(err) = handle_block(¤t_block).await { error!("Error handling block: {err}"); tokio::time::sleep(FAILURE_BACKOFF).await; continue; @@ -307,42 +308,13 @@ async fn run( } } -async fn handle_block( - jsonrpc_client: &JsonRpcClient, - block: &MaybePendingBlock, -) -> Result<()> { +async fn handle_block(block: &MaybePendingBlock) -> Result<()> { let mut buffer = Vec::new(); writeln!(&mut buffer, "FIRE BLOCK_BEGIN {}", block.height())?; - let block_events = get_block_events(jsonrpc_client, block).await?; - - // When using pending blocks, there's a race condition where a new block is confirmed before - // the `starknet_getEvents` request. We discard the block when: - // - // 1. Event list is empty when tx list is not; or - // 2. Any event refers to a tx that's not in the block. - if let MaybePendingBlock::Pending { block, .. } = block { - if block_events.is_empty() && !block.transactions.is_empty() { - anyhow::bail!("inconsistent pending block events: empty event list"); - } - - let block_tx_set = block - .transactions - .iter() - .map(|tx| *tx.transaction_hash()) - .collect::>(); - - if block_events - .iter() - .any(|event| !block_tx_set.contains(&event.transaction_hash)) - { - anyhow::bail!("inconsistent pending block events: invalid transaction reference"); - } - } - for tx in block.transactions().iter() { - let (tx_hash, type_str) = match tx { + let (tx_hash, type_str) = match &tx.transaction { Transaction::Declare(tx) => (*tx.transaction_hash(), "DECLARE"), Transaction::Deploy(tx) => (tx.transaction_hash, "DEPLOY"), Transaction::DeployAccount(tx) => (*tx.transaction_hash(), "DEPLOY_ACCOUNT"), @@ -352,11 +324,15 @@ async fn handle_block( writeln!(&mut buffer, "FIRE BEGIN_TRX {tx_hash:#064x} {type_str}")?; - for (ind_event, event) in block_events - .iter() - .filter(|event| event.transaction_hash == tx_hash) - .enumerate() - { + let tx_events = match &tx.receipt { + TransactionReceipt::Invoke(receipt) => &receipt.events, + TransactionReceipt::L1Handler(receipt) => &receipt.events, + TransactionReceipt::Declare(receipt) => &receipt.events, + TransactionReceipt::Deploy(receipt) => &receipt.events, + TransactionReceipt::DeployAccount(receipt) => &receipt.events, + }; + + for (ind_event, event) in tx_events.iter().enumerate() { writeln!( &mut buffer, "FIRE TRX_BEGIN_EVENT {:#064x} {:#064x}", @@ -396,49 +372,6 @@ async fn handle_block( Ok(()) } -// Use `get_block_with_receipts` once we move to RPC v0.7.x -async fn get_block_events( - jsonrpc_client: &JsonRpcClient, - block: &MaybePendingBlock, -) -> Result> { - const CHUNK_SIZE: u64 = 1000; - - let mut token = None; - let mut events = vec![]; - - let event_filter = match block { - MaybePendingBlock::Confirmed(block) => EventFilter { - from_block: Some(BlockId::Hash(block.block_hash)), - to_block: Some(BlockId::Hash(block.block_hash)), - address: None, - keys: None, - }, - MaybePendingBlock::Pending { .. } => EventFilter { - from_block: Some(BlockId::Tag(BlockTag::Pending)), - to_block: Some(BlockId::Tag(BlockTag::Pending)), - address: None, - keys: None, - }, - }; - - loop { - let mut result = jsonrpc_client - .get_events(event_filter.clone(), token, CHUNK_SIZE) - .await?; - - events.append(&mut result.events); - - match result.continuation_token { - Some(new_token) => { - token = Some(new_token); - } - None => break, - } - } - - Ok(events) -} - fn try_persist_checkpoint(path: &PathBuf, checkpoint: &Checkpoint) -> Result<()> { let temp_path = PathBuf::from(&format!("{}.tmp", path.to_string_lossy()));