From 585c00dddf66f32689dbc83dae3d1c8ba2899cdc Mon Sep 17 00:00:00 2001 From: Jonathan LEI Date: Wed, 10 Apr 2024 10:02:21 +0800 Subject: [PATCH] feat: protobuf codegen --- .cargo/config.toml | 2 + Cargo.lock | 428 ++++++++++++++++++ Cargo.toml | 18 +- README.md | 8 + codegen/Cargo.toml | 7 + codegen/src/main.rs | 6 + proto/zklend.starknet.type.v1.proto | 35 ++ src/main.rs | 3 - substreams-starknet/Cargo.toml | 14 + substreams-starknet/src/lib.rs | 1 + .../src/protobuf/zklend.starknet.r#type.v1.rs | 70 +++ substreams-starknet/src/types.rs | 3 + 12 files changed, 579 insertions(+), 16 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 codegen/Cargo.toml create mode 100644 codegen/src/main.rs create mode 100644 proto/zklend.starknet.type.v1.proto delete mode 100644 src/main.rs create mode 100644 substreams-starknet/Cargo.toml create mode 100644 substreams-starknet/src/lib.rs create mode 100644 substreams-starknet/src/protobuf/zklend.starknet.r#type.v1.rs create mode 100644 substreams-starknet/src/types.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..76a0b90 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[alias] +codegen = "run --package codegen --" diff --git a/Cargo.lock b/Cargo.lock index 26103cd..18a5558 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,434 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247" + +[[package]] +name = "bitflags" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" + +[[package]] +name = "bytes" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "codegen" +version = "0.1.0" +dependencies = [ + "tonic-build", +] + +[[package]] +name = "either" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "errno" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "fastrand" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "658bd65b1cf4c852a3cc96f18a8ce7b5640f6b703f905c7d74532294c2a63984" + +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + +[[package]] +name = "hashbrown" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "indexmap" +version = "2.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + +[[package]] +name = "itertools" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" +dependencies = [ + "either", +] + +[[package]] +name = "libc" +version = "0.2.153" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" + +[[package]] +name = "linux-raw-sys" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" + +[[package]] +name = "log" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" + +[[package]] +name = "memchr" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" + +[[package]] +name = "multimap" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "defc4c55412d89136f966bbb339008b474350e5e6e78d2714439c386b3137a03" + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "petgraph" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9" +dependencies = [ + "fixedbitset", + "indexmap", +] + +[[package]] +name = "prettyplease" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d3928fb5db768cb86f891ff014f0144589297e3c6a1aba6ed7cecfdace270c7" +dependencies = [ + "proc-macro2", + "syn 2.0.58", +] + +[[package]] +name = "proc-macro2" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "prost" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b82eaa1d779e9a4bc1c3217db8ffbeabaae1dca241bf70183242128d48681cd" +dependencies = [ + "bytes", + "prost-derive 0.11.9", +] + +[[package]] +name = "prost" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0f5d036824e4761737860779c906171497f6d55681139d8312388f8fe398922" +dependencies = [ + "bytes", + "prost-derive 0.12.4", +] + +[[package]] +name = "prost-build" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80b776a1b2dc779f5ee0641f8ade0125bc1298dd41a9a0c16d8bd57b42d222b1" +dependencies = [ + "bytes", + "heck", + "itertools 0.12.1", + "log", + "multimap", + "once_cell", + "petgraph", + "prettyplease", + "prost 0.12.4", + "prost-types", + "regex", + "syn 2.0.58", + "tempfile", +] + +[[package]] +name = "prost-derive" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5d2d8d10f3c6ded6da8b05b5fb3b8a5082514344d56c9f871412d29b4e075b4" +dependencies = [ + "anyhow", + "itertools 0.10.5", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "prost-derive" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19de2de2a00075bf566bee3bd4db014b11587e84184d3f7a791bc17f1a8e9e48" +dependencies = [ + "anyhow", + "itertools 0.12.1", + "proc-macro2", + "quote", + "syn 2.0.58", +] + +[[package]] +name = "prost-types" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3235c33eb02c1f1e212abdbe34c78b264b038fb58ca612664343271e36e55ffe" +dependencies = [ + "prost 0.12.4", +] + +[[package]] +name = "quote" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" + +[[package]] +name = "rustix" +version = "0.38.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "substreams-starknet" version = "0.1.0" +dependencies = [ + "prost 0.11.9", +] + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tempfile" +version = "3.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" +dependencies = [ + "cfg-if", + "fastrand", + "rustix", + "windows-sys", +] + +[[package]] +name = "tonic-build" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4ef6dd70a610078cb4e338a0f79d06bc759ff1b22d2120c2ff02ae264ba9c2" +dependencies = [ + "prettyplease", + "proc-macro2", + "prost-build", + "quote", + "syn 2.0.58", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" diff --git a/Cargo.toml b/Cargo.toml index d133d4e..d7d418b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,13 +1,5 @@ -[package] -name = "substreams-starknet" -version = "0.1.0" -authors = ["Jonathan LEI "] -license = "MIT OR Apache-2.0" -edition = "2021" -readme = "README.md" -documentation = "https://docs.rs/substreams-starknet" -repository = "https://github.com/starknet-graph/substreams-starknet" -description = "Rust library for Starknet substreams development" -exclude = [".github/**"] - -[dependencies] +[workspace] +members = [ + "substreams-starknet", + "codegen" +] diff --git a/README.md b/README.md index 44093fb..ea97b01 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,14 @@ Rust library for Starknet [substreams](https://substreams.streamingfast.io/) development. +## Regenerate Protobuf Rust code + +Whenever the [Protobuf types definition](./proto/zklend.starknet.type.v1.proto) changes, run the following command to update the generated Rust code: + +```console +cargo codegen +``` + ## License Licensed under either of diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml new file mode 100644 index 0000000..0b26638 --- /dev/null +++ b/codegen/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "codegen" +version = "0.1.0" +edition = "2021" + +[dependencies] +tonic-build = "0.11.0" diff --git a/codegen/src/main.rs b/codegen/src/main.rs new file mode 100644 index 0000000..0384424 --- /dev/null +++ b/codegen/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + tonic_build::configure() + .out_dir("substreams-starknet/src/protobuf") + .compile(&["proto/zklend.starknet.type.v1.proto"], &["proto"]) + .expect("Failed to compile Firehose Starknet proto"); +} diff --git a/proto/zklend.starknet.type.v1.proto b/proto/zklend.starknet.type.v1.proto new file mode 100644 index 0000000..44e81de --- /dev/null +++ b/proto/zklend.starknet.type.v1.proto @@ -0,0 +1,35 @@ +syntax = "proto3"; + +package zklend.starknet.type.v1; + +// This file only contains the bare minimum types for the POC. It's far from a complete +// representation of a StarkNet network's history as required by the Firehose protocol. As a result, +// any future changes to this schema would require a full re-sync of the StarkNet node. + +message Block { + uint64 height = 1; + bytes hash = 2; + bytes prevHash = 3; + uint64 timestamp = 4; + repeated Transaction transactions = 5; +} + +message Transaction { + TransactionType type = 1; + bytes hash = 2; + repeated Event events = 3; +} + +enum TransactionType { + DEPLOY = 0; + INVOKE_FUNCTION = 1; + DECLARE = 2; + L1_HANDLER = 3; + DEPLOY_ACCOUNT = 4; +} + +message Event { + bytes fromAddr = 1; + repeated bytes keys = 2; + repeated bytes data = 3; +} diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index e7a11a9..0000000 --- a/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -} diff --git a/substreams-starknet/Cargo.toml b/substreams-starknet/Cargo.toml new file mode 100644 index 0000000..82d7f4e --- /dev/null +++ b/substreams-starknet/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "substreams-starknet" +version = "0.1.0" +authors = ["Jonathan LEI "] +license = "MIT OR Apache-2.0" +edition = "2021" +readme = "README.md" +documentation = "https://docs.rs/substreams-starknet" +repository = "https://github.com/starknet-graph/substreams-starknet" +description = "Rust library for Starknet substreams development" +exclude = [".github/**"] + +[dependencies] +prost = "0.11.9" diff --git a/substreams-starknet/src/lib.rs b/substreams-starknet/src/lib.rs new file mode 100644 index 0000000..cd40856 --- /dev/null +++ b/substreams-starknet/src/lib.rs @@ -0,0 +1 @@ +pub mod types; diff --git a/substreams-starknet/src/protobuf/zklend.starknet.r#type.v1.rs b/substreams-starknet/src/protobuf/zklend.starknet.r#type.v1.rs new file mode 100644 index 0000000..35e4dc1 --- /dev/null +++ b/substreams-starknet/src/protobuf/zklend.starknet.r#type.v1.rs @@ -0,0 +1,70 @@ +// This file is @generated by prost-build. +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct Block { + #[prost(uint64, tag = "1")] + pub height: u64, + #[prost(bytes = "vec", tag = "2")] + pub hash: ::prost::alloc::vec::Vec, + #[prost(bytes = "vec", tag = "3")] + pub prev_hash: ::prost::alloc::vec::Vec, + #[prost(uint64, tag = "4")] + pub timestamp: u64, + #[prost(message, repeated, tag = "5")] + pub transactions: ::prost::alloc::vec::Vec, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct Transaction { + #[prost(enumeration = "TransactionType", tag = "1")] + pub r#type: i32, + #[prost(bytes = "vec", tag = "2")] + pub hash: ::prost::alloc::vec::Vec, + #[prost(message, repeated, tag = "3")] + pub events: ::prost::alloc::vec::Vec, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct Event { + #[prost(bytes = "vec", tag = "1")] + pub from_addr: ::prost::alloc::vec::Vec, + #[prost(bytes = "vec", repeated, tag = "2")] + pub keys: ::prost::alloc::vec::Vec<::prost::alloc::vec::Vec>, + #[prost(bytes = "vec", repeated, tag = "3")] + pub data: ::prost::alloc::vec::Vec<::prost::alloc::vec::Vec>, +} +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)] +#[repr(i32)] +pub enum TransactionType { + Deploy = 0, + InvokeFunction = 1, + Declare = 2, + L1Handler = 3, + DeployAccount = 4, +} +impl TransactionType { + /// String value of the enum field names used in the ProtoBuf definition. + /// + /// The values are not transformed in any way and thus are considered stable + /// (if the ProtoBuf definition does not change) and safe for programmatic use. + pub fn as_str_name(&self) -> &'static str { + match self { + TransactionType::Deploy => "DEPLOY", + TransactionType::InvokeFunction => "INVOKE_FUNCTION", + TransactionType::Declare => "DECLARE", + TransactionType::L1Handler => "L1_HANDLER", + TransactionType::DeployAccount => "DEPLOY_ACCOUNT", + } + } + /// Creates an enum from field names used in the ProtoBuf definition. + pub fn from_str_name(value: &str) -> ::core::option::Option { + match value { + "DEPLOY" => Some(Self::Deploy), + "INVOKE_FUNCTION" => Some(Self::InvokeFunction), + "DECLARE" => Some(Self::Declare), + "L1_HANDLER" => Some(Self::L1Handler), + "DEPLOY_ACCOUNT" => Some(Self::DeployAccount), + _ => None, + } + } +} diff --git a/substreams-starknet/src/types.rs b/substreams-starknet/src/types.rs new file mode 100644 index 0000000..09b14e3 --- /dev/null +++ b/substreams-starknet/src/types.rs @@ -0,0 +1,3 @@ +#[path = "protobuf/zklend.starknet.r#type.v1.rs"] +mod pbtype; +pub use pbtype::*;