From 3391f85f428a1dcea1f6d56f5f4b1fb743326b52 Mon Sep 17 00:00:00 2001 From: Joseph Livesey Date: Wed, 11 Sep 2024 17:34:19 -0400 Subject: [PATCH 1/4] feat: support conversion to Lighthouse Beacon block --- Cargo.lock | 395 ++++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 3 +- src/lib.rs | 16 +++ 3 files changed, 407 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 11e31b5..2ae136f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -468,6 +468,24 @@ version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" +[[package]] +name = "arbitrary" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d5a26814d8dcb93b0e5a0ff3c6d80a8843bafb21b39e8e18a6f05471870e110" +dependencies = [ + "derive_arbitrary", +] + +[[package]] +name = "archery" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a8da9bc4c4053ee067669762bcaeea6e241841295a2b6c948312dad6ef4cc02" +dependencies = [ + "static_assertions", +] + [[package]] name = "ark-ff" version = "0.3.0" @@ -849,6 +867,24 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae" +[[package]] +name = "bls" +version = "0.2.0" +source = "git+https://github.com/sigp/lighthouse.git?branch=stable#2e0eb6d1b8705bbda2ba56eb195d9cc7c6575e95" +dependencies = [ + "arbitrary", + "blst", + "ethereum-types", + "ethereum_hashing", + "ethereum_serde_utils", + "ethereum_ssz", + "hex", + "rand", + "serde", + "tree_hash 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "zeroize", +] + [[package]] name = "blst" version = "0.3.13" @@ -994,6 +1030,23 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" +[[package]] +name = "compare_fields" +version = "0.2.0" +source = "git+https://github.com/sigp/lighthouse.git?branch=stable#2e0eb6d1b8705bbda2ba56eb195d9cc7c6575e95" +dependencies = [ + "itertools 0.10.5", +] + +[[package]] +name = "compare_fields_derive" +version = "0.2.0" +source = "git+https://github.com/sigp/lighthouse.git?branch=stable#2e0eb6d1b8705bbda2ba56eb195d9cc7c6575e95" +dependencies = [ + "quote", + "syn 1.0.109", +] + [[package]] name = "const-hex" version = "1.12.0" @@ -1304,6 +1357,17 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "derive_arbitrary" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67e77553c4162a157adbf834ebae5b415acbecbeafc7a74b0e886657506a7611" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.76", +] + [[package]] name = "derive_more" version = "0.99.18" @@ -1491,6 +1555,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "erased-serde" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c138974f9d5e7fe373eb04df7cae98833802ae4b11c24ac7039a21d5af4b26c" +dependencies = [ + "serde", +] + [[package]] name = "erased-serde" version = "0.4.5" @@ -1511,6 +1584,20 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "eth2_interop_keypairs" +version = "0.2.0" +source = "git+https://github.com/sigp/lighthouse.git?branch=stable#2e0eb6d1b8705bbda2ba56eb195d9cc7c6575e95" +dependencies = [ + "bls", + "ethereum_hashing", + "hex", + "lazy_static", + "num-bigint", + "serde", + "serde_yaml", +] + [[package]] name = "eth_trie" version = "0.4.0" @@ -1645,12 +1732,12 @@ dependencies = [ "sha2", "sha3 0.9.1", "snap", - "ssz_types", - "superstruct", + "ssz_types 0.6.0 (git+https://github.com/KolbyML/ssz_types.git?rev=2a5922de75f00746890bf4ea9ad663c9d5d58efe)", + "superstruct 0.7.0", "thiserror", "tokio", - "tree_hash", - "tree_hash_derive", + "tree_hash 0.6.0 (git+https://github.com/KolbyML/tree_hash.git?rev=8aaf8bb4184148768d48e2cfbbdd0b95d1da8730)", + "tree_hash_derive 0.6.0 (git+https://github.com/KolbyML/tree_hash.git?rev=8aaf8bb4184148768d48e2cfbbdd0b95d1da8730)", "trin-utils", "ureq", "url", @@ -1663,6 +1750,18 @@ version = "2.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" +[[package]] +name = "fallible-iterator" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + [[package]] name = "fastrand" version = "2.1.1" @@ -1702,6 +1801,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "835c052cb0c08c1acf6ffd71c022172e18723949c8282f2b9f27efbc51e64534" dependencies = [ + "arbitrary", "byteorder", "rand", "rustc-hex", @@ -2369,6 +2469,14 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "int_to_bytes" +version = "0.2.0" +source = "git+https://github.com/sigp/lighthouse.git?branch=stable#2e0eb6d1b8705bbda2ba56eb195d9cc7c6575e95" +dependencies = [ + "bytes", +] + [[package]] name = "inventory" version = "0.3.15" @@ -2657,6 +2765,23 @@ version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4933f3f57a8e9d9da04db23fb153356ecaf00cbd14aee46279c33dc80925c37" +[[package]] +name = "kzg" +version = "0.1.0" +source = "git+https://github.com/sigp/lighthouse.git?branch=stable#2e0eb6d1b8705bbda2ba56eb195d9cc7c6575e95" +dependencies = [ + "arbitrary", + "c-kzg", + "derivative", + "ethereum_hashing", + "ethereum_serde_utils", + "ethereum_ssz", + "ethereum_ssz_derive", + "hex", + "serde", + "tree_hash 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -2687,6 +2812,17 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" +[[package]] +name = "libsqlite3-sys" +version = "0.25.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29f835d03d717946d28b1d1ed632eb6f0e24a299388ee623d0c23118d3e8a7fa" +dependencies = [ + "cc", + "pkg-config", + "vcpkg", +] + [[package]] name = "libz-sys" version = "1.1.20" @@ -2730,6 +2866,12 @@ dependencies = [ "hashbrown 0.14.5", ] +[[package]] +name = "maplit" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" + [[package]] name = "matchers" version = "0.1.0" @@ -2757,6 +2899,63 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "merkle_proof" +version = "0.2.0" +source = "git+https://github.com/sigp/lighthouse.git?branch=stable#2e0eb6d1b8705bbda2ba56eb195d9cc7c6575e95" +dependencies = [ + "ethereum-types", + "ethereum_hashing", + "lazy_static", + "safe_arith", +] + +[[package]] +name = "metastruct" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f00a5ba4a0f3453c31c397b214e1675d95b697c33763aa58add57ea833424384" +dependencies = [ + "metastruct_macro", +] + +[[package]] +name = "metastruct_macro" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c3a991d4536c933306e52f0e8ab303757185ec13a09d1f3e1cbde5a0d8410bf" +dependencies = [ + "darling 0.13.4", + "itertools 0.10.5", + "proc-macro2", + "quote", + "smallvec", + "syn 1.0.109", +] + +[[package]] +name = "milhouse" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3826d3602a3674b07e080ce1982350e454ec253d73f156bd927ac1b652293f4d" +dependencies = [ + "arbitrary", + "derivative", + "ethereum-types", + "ethereum_hashing", + "ethereum_ssz", + "ethereum_ssz_derive", + "itertools 0.10.5", + "parking_lot 0.12.3", + "rayon", + "serde", + "smallvec", + "tree_hash 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "triomphe", + "typenum", + "vec_map", +] + [[package]] name = "mime" version = "0.3.17" @@ -3710,6 +3909,15 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "afab94fb28594581f62d981211a9a4d53cc8130bbcbbb89a0440d9b8e81a7746" +[[package]] +name = "rpds" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ef5140bcb576bfd6d56cd2de709a7d17851ac1f3805e67fe9d99e42a11821f" +dependencies = [ + "archery", +] + [[package]] name = "rs_merkle" version = "1.4.2" @@ -3750,6 +3958,20 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48fd7bd8a6377e15ad9d42a8ec25371b94ddc67abe7c8b9127bec79bebaaae18" +[[package]] +name = "rusqlite" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01e213bc3ecb39ac32e81e51ebe31fd888a940515173e3a18a35f8c6e896422a" +dependencies = [ + "bitflags 1.3.2", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "smallvec", +] + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -3872,6 +4094,11 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "safe_arith" +version = "0.1.0" +source = "git+https://github.com/sigp/lighthouse.git?branch=stable#2e0eb6d1b8705bbda2ba56eb195d9cc7c6575e95" + [[package]] name = "schannel" version = "0.1.23" @@ -4102,6 +4329,7 @@ dependencies = [ "thiserror", "tonic", "tonic-build", + "types", ] [[package]] @@ -4216,12 +4444,22 @@ dependencies = [ "autocfg", ] +[[package]] +name = "slog" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8347046d4ebd943127157b94d63abb990fcf729dc4e9978927fdf4ac3c998d06" +dependencies = [ + "erased-serde 0.3.31", +] + [[package]] name = "smallvec" version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" dependencies = [ + "arbitrary", "serde", ] @@ -4283,6 +4521,24 @@ dependencies = [ "der", ] +[[package]] +name = "ssz_types" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "625b20de2d4b3891e6972f4ce5061cb11bd52b3479270c4b177c134b571194a9" +dependencies = [ + "arbitrary", + "derivative", + "ethereum_serde_utils", + "ethereum_ssz", + "itertools 0.10.5", + "serde", + "serde_derive", + "smallvec", + "tree_hash 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "typenum", +] + [[package]] name = "ssz_types" version = "0.6.0" @@ -4295,10 +4551,16 @@ dependencies = [ "serde", "serde_derive", "smallvec", - "tree_hash", + "tree_hash 0.6.0 (git+https://github.com/KolbyML/tree_hash.git?rev=8aaf8bb4184148768d48e2cfbbdd0b95d1da8730)", "typenum", ] +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + [[package]] name = "static_assertions" version = "1.1.0" @@ -4359,6 +4621,29 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "superstruct" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf0f31f730ad9e579364950e10d6172b4a9bd04b447edf5988b066a860cc340e" +dependencies = [ + "darling 0.13.4", + "itertools 0.10.5", + "proc-macro2", + "quote", + "smallvec", + "syn 1.0.109", +] + +[[package]] +name = "swap_or_not_shuffle" +version = "0.2.0" +source = "git+https://github.com/sigp/lighthouse.git?branch=stable#2e0eb6d1b8705bbda2ba56eb195d9cc7c6575e95" +dependencies = [ + "ethereum-types", + "ethereum_hashing", +] + [[package]] name = "syn" version = "1.0.109" @@ -4424,6 +4709,15 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "test_random_derive" +version = "0.2.0" +source = "git+https://github.com/sigp/lighthouse.git?branch=stable#2e0eb6d1b8705bbda2ba56eb195d9cc7c6575e95" +dependencies = [ + "quote", + "syn 1.0.109", +] + [[package]] name = "thiserror" version = "1.0.63" @@ -4770,6 +5064,17 @@ dependencies = [ "tracing-log", ] +[[package]] +name = "tree_hash" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134d6b24a5b829f30b5ee7de05ba7384557f5f6b00e29409cdf2392f93201bfa" +dependencies = [ + "ethereum-types", + "ethereum_hashing", + "smallvec", +] + [[package]] name = "tree_hash" version = "0.6.0" @@ -4780,6 +5085,17 @@ dependencies = [ "smallvec", ] +[[package]] +name = "tree_hash_derive" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce7bccc538359a213436af7bc95804bdbf1c2a21d80e22953cbe9e096837ff1" +dependencies = [ + "darling 0.13.4", + "quote", + "syn 1.0.109", +] + [[package]] name = "tree_hash_derive" version = "0.6.0" @@ -4801,6 +5117,16 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "triomphe" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6631e42e10b40c0690bf92f404ebcfe6e1fdb480391d15f17cc8e96eeed5369" +dependencies = [ + "serde", + "stable_deref_trait", +] + [[package]] name = "try-lock" version = "0.2.5" @@ -4819,13 +5145,63 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "types" +version = "0.2.1" +source = "git+https://github.com/sigp/lighthouse.git?branch=stable#2e0eb6d1b8705bbda2ba56eb195d9cc7c6575e95" +dependencies = [ + "alloy-primitives", + "alloy-rlp", + "arbitrary", + "bls", + "compare_fields", + "compare_fields_derive", + "derivative", + "eth2_interop_keypairs", + "ethereum-types", + "ethereum_hashing", + "ethereum_serde_utils", + "ethereum_ssz", + "ethereum_ssz_derive", + "hex", + "int_to_bytes", + "itertools 0.10.5", + "kzg", + "lazy_static", + "log", + "maplit", + "merkle_proof", + "metastruct", + "milhouse", + "parking_lot 0.12.3", + "rand", + "rand_xorshift", + "rayon", + "regex", + "rpds", + "rusqlite", + "safe_arith", + "serde", + "serde_json", + "serde_yaml", + "slog", + "smallvec", + "ssz_types 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "superstruct 0.8.0", + "swap_or_not_shuffle", + "tempfile", + "test_random_derive", + "tree_hash 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tree_hash_derive 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "typetag" version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "52ba3b6e86ffe0054b2c44f2d86407388b933b16cb0a70eea3929420db1d9bbe" dependencies = [ - "erased-serde", + "erased-serde 0.4.5", "inventory", "once_cell", "serde", @@ -4884,6 +5260,7 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76f64bba2c53b04fcab63c01a7d7427eadc821e3bc48c34dc9ba29c501164b52" dependencies = [ + "arbitrary", "byteorder", "crunchy", "hex", @@ -5037,6 +5414,12 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version_check" version = "0.9.5" diff --git a/Cargo.toml b/Cargo.toml index ca8534c..5d007d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,12 +14,13 @@ prost-wkt = "0.6.0" prost-wkt-types = "0.6.0" reth-primitives = { git = "https://github.com/paradigmxyz/reth", version = "1.0.1", tag = "v1.0.1" } serde = "1.0.196" +serde_json = "1.0.127" thiserror = "1.0.62" tonic = "0.12.0" +types = { git = "https://github.com/sigp/lighthouse.git", branch = "stable" } [dev-dependencies] hex = "0.4.3" -serde_json = "1.0.127" [build-dependencies] prost-build = "0.13.1" diff --git a/src/lib.rs b/src/lib.rs index 4786fee..a1e67d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -201,6 +201,7 @@ pub mod beacon { pub mod v1 { use crate::{firehose::v2::SingleBlockResponse, StreamingFastProtosError}; use prost::Message; + use types::{BeaconBlock, EthSpec, FullPayload}; tonic::include_proto!("sf.beacon.r#type.v1"); @@ -213,6 +214,18 @@ pub mod beacon { Ok(block) } } + + // TODO: Not efficient, but it works. + impl TryFrom for BeaconBlock> { + type Error = StreamingFastProtosError; + + fn try_from(block: Block) -> Result { + let json_string = serde_json::to_string(&block)?; + let beacon_block: BeaconBlock = serde_json::from_str(&json_string)?; + + Ok(beacon_block) + } + } } } } @@ -225,6 +238,9 @@ pub enum StreamingFastProtosError { #[error("Error in decoding block: {0}")] DecodeError(#[from] prost::DecodeError), + #[error("JSON error: {0}")] + JsonError(#[from] serde_json::Error), + #[error("Null block field in block response")] NullBlock, } From 17dff5ba37e35d924e44d232daf529ec6cc22660 Mon Sep 17 00:00:00 2001 From: Joseph Livesey Date: Thu, 12 Sep 2024 14:29:26 -0400 Subject: [PATCH 2/4] feat: support conversion to Lighthouse Beacon block --- Cargo.lock | 3 + Cargo.toml | 5 +- src/lib.rs | 518 +++++++++++++++++- ...header.json => block-20562650-header.json} | 0 4 files changed, 514 insertions(+), 12 deletions(-) rename tests/data/{exec-layer-block-20562650-header.json => block-20562650-header.json} (100%) diff --git a/Cargo.lock b/Cargo.lock index 2ae136f..e4f83b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4317,8 +4317,10 @@ name = "sf-protos" version = "0.1.0" dependencies = [ "alloy-primitives", + "bls", "ethportal-api", "hex", + "primitive-types", "prost", "prost-build", "prost-wkt", @@ -4326,6 +4328,7 @@ dependencies = [ "reth-primitives", "serde", "serde_json", + "ssz_types 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "thiserror", "tonic", "tonic-build", diff --git a/Cargo.toml b/Cargo.toml index 5d007d9..90760fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,19 +8,22 @@ doctest = false [dependencies] alloy-primitives = "=0.7.7" +bls = { git = "https://github.com/sigp/lighthouse.git", branch = "stable" } ethportal-api = { git = "https://github.com/ethereum/trin.git", version = "0.2.2", tag = "v0.1.0-alpha.35" } +primitive-types = "0.12.2" prost = "0.13.1" prost-wkt = "0.6.0" prost-wkt-types = "0.6.0" reth-primitives = { git = "https://github.com/paradigmxyz/reth", version = "1.0.1", tag = "v1.0.1" } serde = "1.0.196" -serde_json = "1.0.127" +ssz_types = "0.6" thiserror = "1.0.62" tonic = "0.12.0" types = { git = "https://github.com/sigp/lighthouse.git", branch = "stable" } [dev-dependencies] hex = "0.4.3" +serde_json = "1.0.127" [build-dependencies] prost-build = "0.13.1" diff --git a/src/lib.rs b/src/lib.rs index a1e67d2..ac57bde 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -133,8 +133,7 @@ pub mod ethereum { #[test] fn test_block_to_header() { let reader = - std::fs::File::open("tests/data/exec-layer-block-20562650-header.json") - .unwrap(); + std::fs::File::open("tests/data/block-20562650-header.json").unwrap(); let block: Block = serde_json::from_reader(reader).unwrap(); // Confirm block number and hash. @@ -200,11 +199,349 @@ pub mod beacon { pub mod r#type { pub mod v1 { use crate::{firehose::v2::SingleBlockResponse, StreamingFastProtosError}; + use primitive_types::{H256, U256}; use prost::Message; - use types::{BeaconBlock, EthSpec, FullPayload}; + use ssz_types::{length::Fixed, Bitfield, FixedVector}; + use types::{ + Address, BeaconBlockBodyDeneb, BitList, EthSpec, ExecutionBlockHash, Graffiti, + IndexedAttestationBase, MainnetEthSpec, GRAFFITI_BYTES_LEN, + }; tonic::include_proto!("sf.beacon.r#type.v1"); + impl TryFrom for types::AttestationBase { + type Error = StreamingFastProtosError; + + fn try_from( + Attestation { + aggregation_bits, + data, + signature, + }: Attestation, + ) -> Result { + Ok(Self { + aggregation_bits: BitList::from_bytes(aggregation_bits.as_slice().into()) + .map_err(|e| { + StreamingFastProtosError::SszTypesError(format!("{:?}", e)) + })?, + data: data + .ok_or(StreamingFastProtosError::NullAttestationData)? + .try_into()?, + signature: bls::generics::GenericAggregateSignature::deserialize( + signature.as_slice(), + ) + .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + }) + } + } + + impl TryFrom for types::AttestationData { + type Error = StreamingFastProtosError; + + fn try_from( + AttestationData { + slot, + committee_index, + beacon_block_root, + source, + target, + }: AttestationData, + ) -> Result { + Ok(Self { + slot: slot.into(), + index: committee_index, + beacon_block_root: H256::from_slice(beacon_block_root.as_slice()), + source: source + .ok_or(StreamingFastProtosError::NullCheckpoint)? + .into(), + target: target + .ok_or(StreamingFastProtosError::NullCheckpoint)? + .into(), + }) + } + } + + impl TryFrom for types::AttesterSlashingBase { + type Error = StreamingFastProtosError; + + fn try_from( + AttesterSlashing { + attestation_1, + attestation_2, + }: AttesterSlashing, + ) -> Result { + let attestation_1 = + attestation_1.ok_or(StreamingFastProtosError::NullSigner)?; + let attestation_2 = + attestation_2.ok_or(StreamingFastProtosError::NullSigner)?; + + Ok(Self { + attestation_1: attestation_1.try_into()?, + attestation_2: attestation_2.try_into()?, + }) + } + } + + impl From for types::BeaconBlockHeader { + fn from( + BeaconBlockHeader { + slot, + proposer_index, + parent_root, + state_root, + body_root, + }: BeaconBlockHeader, + ) -> Self { + Self { + slot: slot.into(), + proposer_index, + parent_root: H256::from_slice(parent_root.as_slice()), + state_root: H256::from_slice(state_root.as_slice()), + body_root: H256::from_slice(body_root.as_slice()), + } + } + } + + impl TryFrom for types::BlsToExecutionChange { + type Error = StreamingFastProtosError; + + fn try_from( + BlsToExecutionChange { + validator_index, + from_bls_pub_key, + to_execution_address, + }: BlsToExecutionChange, + ) -> Result { + Ok(Self { + validator_index, + from_bls_pubkey: bls::generics::GenericPublicKeyBytes::deserialize( + from_bls_pub_key.as_slice(), + ) + .map_err(|e| StreamingFastProtosError::Bls(format!("{e:?}")))?, + to_execution_address: Address::from_slice(to_execution_address.as_slice()), + }) + } + } + + impl From for types::Checkpoint { + fn from(Checkpoint { epoch, root }: Checkpoint) -> Self { + Self { + epoch: epoch.into(), + root: H256::from_slice(root.as_slice()), + } + } + } + + impl TryFrom for types::ExecutionPayloadDeneb { + type Error = StreamingFastProtosError; + + fn try_from( + DenebExecutionPayload { + parent_hash, + fee_recipient, + state_root, + receipts_root, + logs_bloom, + prev_randao, + block_number, + gas_limit, + gas_used, + timestamp, + extra_data, + base_fee_per_gas, + block_hash, + transactions, + withdrawals, + blob_gas_used, + excess_blob_gas, + }: DenebExecutionPayload, + ) -> Result { + Ok(Self { + parent_hash: ExecutionBlockHash::from_root(H256::from_slice( + parent_hash.as_slice(), + )), + fee_recipient: Address::from_slice(fee_recipient.as_slice()), + state_root: H256::from_slice(state_root.as_slice()), + receipts_root: H256::from_slice(receipts_root.as_slice()), + logs_bloom: FixedVector::from(logs_bloom), + prev_randao: H256::from_slice(prev_randao.as_slice()), + block_number, + gas_limit, + gas_used, + timestamp: timestamp + .map(|ts| ts.seconds as u64 * 1_000_000_000 + ts.nanos as u64) + .unwrap_or_default(), + extra_data: extra_data.into(), + base_fee_per_gas: U256::from_big_endian(base_fee_per_gas.as_slice()), + block_hash: ExecutionBlockHash(H256::from_slice(block_hash.as_slice())), + transactions: transactions + .into_iter() + .map(Into::into) + .collect::>() + .into(), + withdrawals: withdrawals + .into_iter() + .map(Into::into) + .collect::>() + .into(), + blob_gas_used, + excess_blob_gas, + }) + } + } + + impl TryFrom for types::Deposit { + type Error = StreamingFastProtosError; + + fn try_from(Deposit { proof, data }: Deposit) -> Result { + Ok(Self { + proof: proof + .into_iter() + .map(|v| H256::from_slice(v.as_slice())) + .collect::>() + .into(), + data: data + .ok_or(StreamingFastProtosError::NullDepositData)? + .try_into()?, + }) + } + } + + impl TryFrom for types::DepositData { + type Error = StreamingFastProtosError; + + fn try_from( + DepositData { + public_key, + withdrawal_credentials, + gwei, + signature, + }: DepositData, + ) -> Result { + Ok(Self { + pubkey: bls::generics::GenericPublicKeyBytes::deserialize( + public_key.as_slice(), + ) + .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + withdrawal_credentials: H256::from_slice(withdrawal_credentials.as_slice()), + amount: gwei, + signature: bls::generics::GenericSignatureBytes::deserialize( + signature.as_slice(), + ) + .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + }) + } + } + + impl From for types::Eth1Data { + fn from( + Eth1Data { + deposit_root, + deposit_count, + block_hash, + }: Eth1Data, + ) -> Self { + Self { + deposit_root: H256::from_slice(deposit_root.as_slice()), + deposit_count, + block_hash: H256::from_slice(block_hash.as_slice()), + } + } + } + + impl TryFrom for types::IndexedAttestationBase { + type Error = StreamingFastProtosError; + + fn try_from( + IndexedAttestation { + attesting_indices, + data, + signature, + }: IndexedAttestation, + ) -> Result { + Ok(IndexedAttestationBase { + attesting_indices: attesting_indices.into(), + data: data.unwrap().try_into()?, + signature: bls::generics::GenericAggregateSignature::deserialize( + signature.as_slice(), + ) + .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + }) + } + } + + impl TryFrom for types::ProposerSlashing { + type Error = StreamingFastProtosError; + + fn try_from( + ProposerSlashing { + signed_header_1, + signed_header_2, + }: ProposerSlashing, + ) -> Result { + Ok(Self { + signed_header_1: signed_header_1 + .ok_or(StreamingFastProtosError::NullSigner)? + .try_into()?, + signed_header_2: signed_header_2 + .ok_or(StreamingFastProtosError::NullSigner)? + .try_into()?, + }) + } + } + + impl TryFrom for types::SignedBeaconBlockHeader { + type Error = StreamingFastProtosError; + + fn try_from( + SignedBeaconBlockHeader { message, signature }: SignedBeaconBlockHeader, + ) -> Result { + Ok(Self { + message: message.unwrap().into(), + signature: bls::generics::GenericSignature::deserialize( + signature.as_slice(), + ) + .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + }) + } + } + + impl TryFrom for types::SignedBlsToExecutionChange { + type Error = StreamingFastProtosError; + + fn try_from( + SignedBlsToExecutionChange { message, signature }: SignedBlsToExecutionChange, + ) -> Result { + Ok(Self { + message: message + .ok_or(StreamingFastProtosError::NullBlsToExecutionChange)? + .try_into()?, + signature: bls::generics::GenericSignature::deserialize( + signature.as_slice(), + ) + .expect("Failed to deserialize signature"), + }) + } + } + + impl TryFrom for types::SignedVoluntaryExit { + type Error = StreamingFastProtosError; + + fn try_from( + SignedVoluntaryExit { message, signature }: SignedVoluntaryExit, + ) -> Result { + Ok(Self { + message: message + .ok_or(StreamingFastProtosError::NullVoluntaryExit)? + .into(), + signature: bls::generics::GenericSignature::deserialize( + signature.as_slice(), + ) + .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + }) + } + } + impl TryFrom for Block { type Error = StreamingFastProtosError; @@ -215,15 +552,144 @@ pub mod beacon { } } - // TODO: Not efficient, but it works. - impl TryFrom for BeaconBlock> { + impl TryFrom for types::SyncAggregate { type Error = StreamingFastProtosError; - fn try_from(block: Block) -> Result { - let json_string = serde_json::to_string(&block)?; - let beacon_block: BeaconBlock = serde_json::from_str(&json_string)?; + fn try_from( + SyncAggregate { + sync_commitee_bits, + sync_comittee_signature, + }: SyncAggregate, + ) -> Result { + Ok(Self { + sync_committee_bits: + Bitfield::::SyncCommitteeSize>>::from_bytes( + sync_commitee_bits.as_slice().into(), + ) + .map_err(|e| { + StreamingFastProtosError::SszTypesError(format!("{:?}", e)) + })?, + sync_committee_signature: + bls::generics::GenericAggregateSignature::deserialize( + sync_comittee_signature.as_slice(), + ) + .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + }) + } + } + + impl From for types::VoluntaryExit { + fn from( + VoluntaryExit { + epoch, + validator_index, + }: VoluntaryExit, + ) -> Self { + Self { + epoch: epoch.into(), + validator_index, + } + } + } - Ok(beacon_block) + impl From for types::Withdrawal { + fn from( + Withdrawal { + withdrawal_index, + validator_index, + address, + gwei, + }: Withdrawal, + ) -> Self { + Self { + index: withdrawal_index, + validator_index, + address: Address::from_slice(address.as_slice()), + amount: gwei, + } + } + } + + impl TryFrom for types::BeaconBlockBodyDeneb { + type Error = StreamingFastProtosError; + + fn try_from( + DenebBody { + rando_reveal, + eth1_data, + graffiti, + proposer_slashings, + attester_slashings, + attestations, + deposits, + voluntary_exits, + sync_aggregate, + execution_payload, + bls_to_execution_changes, + blob_kzg_commitments, + // Blobs not included. + .. + }: DenebBody, + ) -> Result { + let beacon_block_body = BeaconBlockBodyDeneb { + randao_reveal: bls::generics::GenericSignature::deserialize(&rando_reveal) + .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + eth1_data: eth1_data + .map(|eth1_data| eth1_data.into()) + .unwrap_or_default(), + graffiti: Graffiti::from( + <[u8; GRAFFITI_BYTES_LEN]>::try_from(graffiti.as_slice()) + .map_err(|_| StreamingFastProtosError::GraffitiInvalid)?, + ), + proposer_slashings: proposer_slashings + .into_iter() + .map(|proposer_slashing| proposer_slashing.try_into()) + .collect::, _>>()? + .into(), + attester_slashings: attester_slashings + .into_iter() + .map(|attester_slashing| attester_slashing.try_into()) + .collect::, _>>()? + .into(), + attestations: attestations + .into_iter() + .map(|attestation| attestation.try_into()) + .collect::, _>>()? + .into(), + deposits: deposits + .into_iter() + .map(|deposit| deposit.try_into()) + .collect::, _>>()? + .into(), + voluntary_exits: voluntary_exits + .into_iter() + .map(|voluntary_exit| voluntary_exit.try_into()) + .collect::, _>>()? + .into(), + sync_aggregate: sync_aggregate + .map(|sync_aggregate| sync_aggregate.try_into()) + .transpose()? + .unwrap_or_else(types::SyncAggregate::new), + execution_payload: execution_payload + .ok_or(StreamingFastProtosError::NullExecutionPayload) + .and_then(types::ExecutionPayloadDeneb::try_from)? + .into(), + bls_to_execution_changes: bls_to_execution_changes + .into_iter() + .map(|bls_to_execution_change| bls_to_execution_change.try_into()) + .collect::, _>>()? + .into(), + blob_kzg_commitments: blob_kzg_commitments + .into_iter() + .map(|blob_kzg_commitment| { + <[u8; 48]>::try_from(blob_kzg_commitment.as_slice()) + .map(types::KzgCommitment) + .map_err(|_| StreamingFastProtosError::KzgCommitmentInvalid) + }) + .collect::, _>>()? + .into(), + }; + Ok(beacon_block_body) } } } @@ -235,12 +701,42 @@ pub enum StreamingFastProtosError { #[error("Block conversion error")] BlockConversionError, + #[error("BLS error: {0}")] + Bls(String), + #[error("Error in decoding block: {0}")] DecodeError(#[from] prost::DecodeError), - #[error("JSON error: {0}")] - JsonError(#[from] serde_json::Error), + #[error("GraffitiInvalid")] + GraffitiInvalid, + + #[error("KzgCommitmentInvalid")] + KzgCommitmentInvalid, + + #[error("Null attestation data")] + NullAttestationData, #[error("Null block field in block response")] NullBlock, + + #[error("Null BlsToExecutionChange")] + NullBlsToExecutionChange, + + #[error("Null checkpoint")] + NullCheckpoint, + + #[error("Null deposit data")] + NullDepositData, + + #[error("Null execution payload")] + NullExecutionPayload, + + #[error("Proposer Slashing null signer")] + NullSigner, + + #[error("Null voluntary exit")] + NullVoluntaryExit, + + #[error("SSZ Types error: {0}")] + SszTypesError(String), } diff --git a/tests/data/exec-layer-block-20562650-header.json b/tests/data/block-20562650-header.json similarity index 100% rename from tests/data/exec-layer-block-20562650-header.json rename to tests/data/block-20562650-header.json From 28b910a4af0ed78239f62b221c0a4d3a095f436a Mon Sep 17 00:00:00 2001 From: Joseph Livesey Date: Fri, 13 Sep 2024 18:31:38 -0400 Subject: [PATCH 3/4] refactor: rename error type --- src/lib.rs | 118 +++++++++++++++++++++++------------------------------ 1 file changed, 52 insertions(+), 66 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ac57bde..7b0b1df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,18 +15,18 @@ pub mod ethereum { use reth_primitives::TxType; use transaction_trace::Type; - use crate::StreamingFastProtosError; + use crate::ProtosError; tonic::include_proto!("sf.ethereum.r#type.v2"); impl TryFrom<&Block> for Header { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from(block: &Block) -> Result { let block_header = block .header .as_ref() - .ok_or(StreamingFastProtosError::BlockConversionError)?; + .ok_or(ProtosError::BlockConversionError)?; let parent_hash = FixedBytes::from_slice(block_header.parent_hash.as_slice()); let uncles_hash = FixedBytes::from_slice(block_header.uncle_hash.as_slice()); @@ -41,7 +41,7 @@ pub mod ethereum { block_header .difficulty .as_ref() - .ok_or(StreamingFastProtosError::BlockConversionError)? + .ok_or(ProtosError::BlockConversionError)? .bytes .as_slice(), ); @@ -51,7 +51,7 @@ pub mod ethereum { let timestamp = block_header .timestamp .as_ref() - .ok_or(StreamingFastProtosError::BlockConversionError)? + .ok_or(ProtosError::BlockConversionError)? .seconds as u64; let extra_data = block_header.extra_data.clone(); let mix_hash = Some(FixedBytes::from_slice(block_header.mix_hash.as_slice())); @@ -169,25 +169,25 @@ pub mod firehose { pub mod v2 { use prost::Message; - use crate::{ethereum::r#type::v2::Block, StreamingFastProtosError}; + use crate::{ethereum::r#type::v2::Block, ProtosError}; tonic::include_proto!("sf.firehose.v2"); impl TryFrom for Block { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from(response: SingleBlockResponse) -> Result { - let any = response.block.ok_or(StreamingFastProtosError::NullBlock)?; + let any = response.block.ok_or(ProtosError::NullBlock)?; let block = Block::decode(any.value.as_ref())?; Ok(block) } } impl TryFrom for Block { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from(response: Response) -> Result { - let any = response.block.ok_or(StreamingFastProtosError::NullBlock)?; + let any = response.block.ok_or(ProtosError::NullBlock)?; let block = Block::decode(any.value.as_ref())?; Ok(block) } @@ -198,7 +198,7 @@ pub mod firehose { pub mod beacon { pub mod r#type { pub mod v1 { - use crate::{firehose::v2::SingleBlockResponse, StreamingFastProtosError}; + use crate::{firehose::v2::SingleBlockResponse, ProtosError}; use primitive_types::{H256, U256}; use prost::Message; use ssz_types::{length::Fixed, Bitfield, FixedVector}; @@ -210,7 +210,7 @@ pub mod beacon { tonic::include_proto!("sf.beacon.r#type.v1"); impl TryFrom for types::AttestationBase { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from( Attestation { @@ -222,21 +222,19 @@ pub mod beacon { Ok(Self { aggregation_bits: BitList::from_bytes(aggregation_bits.as_slice().into()) .map_err(|e| { - StreamingFastProtosError::SszTypesError(format!("{:?}", e)) + ProtosError::SszTypesError(format!("{:?}", e)) })?, - data: data - .ok_or(StreamingFastProtosError::NullAttestationData)? - .try_into()?, + data: data.ok_or(ProtosError::NullAttestationData)?.try_into()?, signature: bls::generics::GenericAggregateSignature::deserialize( signature.as_slice(), ) - .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + .map_err(|e| ProtosError::Bls(format!("{:?}", e)))?, }) } } impl TryFrom for types::AttestationData { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from( AttestationData { @@ -251,18 +249,14 @@ pub mod beacon { slot: slot.into(), index: committee_index, beacon_block_root: H256::from_slice(beacon_block_root.as_slice()), - source: source - .ok_or(StreamingFastProtosError::NullCheckpoint)? - .into(), - target: target - .ok_or(StreamingFastProtosError::NullCheckpoint)? - .into(), + source: source.ok_or(ProtosError::NullCheckpoint)?.into(), + target: target.ok_or(ProtosError::NullCheckpoint)?.into(), }) } } impl TryFrom for types::AttesterSlashingBase { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from( AttesterSlashing { @@ -270,10 +264,8 @@ pub mod beacon { attestation_2, }: AttesterSlashing, ) -> Result { - let attestation_1 = - attestation_1.ok_or(StreamingFastProtosError::NullSigner)?; - let attestation_2 = - attestation_2.ok_or(StreamingFastProtosError::NullSigner)?; + let attestation_1 = attestation_1.ok_or(ProtosError::NullSigner)?; + let attestation_2 = attestation_2.ok_or(ProtosError::NullSigner)?; Ok(Self { attestation_1: attestation_1.try_into()?, @@ -303,7 +295,7 @@ pub mod beacon { } impl TryFrom for types::BlsToExecutionChange { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from( BlsToExecutionChange { @@ -317,7 +309,7 @@ pub mod beacon { from_bls_pubkey: bls::generics::GenericPublicKeyBytes::deserialize( from_bls_pub_key.as_slice(), ) - .map_err(|e| StreamingFastProtosError::Bls(format!("{e:?}")))?, + .map_err(|e| ProtosError::Bls(format!("{e:?}")))?, to_execution_address: Address::from_slice(to_execution_address.as_slice()), }) } @@ -333,7 +325,7 @@ pub mod beacon { } impl TryFrom for types::ExecutionPayloadDeneb { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from( DenebExecutionPayload { @@ -391,7 +383,7 @@ pub mod beacon { } impl TryFrom for types::Deposit { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from(Deposit { proof, data }: Deposit) -> Result { Ok(Self { @@ -400,15 +392,13 @@ pub mod beacon { .map(|v| H256::from_slice(v.as_slice())) .collect::>() .into(), - data: data - .ok_or(StreamingFastProtosError::NullDepositData)? - .try_into()?, + data: data.ok_or(ProtosError::NullDepositData)?.try_into()?, }) } } impl TryFrom for types::DepositData { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from( DepositData { @@ -422,13 +412,13 @@ pub mod beacon { pubkey: bls::generics::GenericPublicKeyBytes::deserialize( public_key.as_slice(), ) - .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + .map_err(|e| ProtosError::Bls(format!("{:?}", e)))?, withdrawal_credentials: H256::from_slice(withdrawal_credentials.as_slice()), amount: gwei, signature: bls::generics::GenericSignatureBytes::deserialize( signature.as_slice(), ) - .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + .map_err(|e| ProtosError::Bls(format!("{:?}", e)))?, }) } } @@ -450,7 +440,7 @@ pub mod beacon { } impl TryFrom for types::IndexedAttestationBase { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from( IndexedAttestation { @@ -465,13 +455,13 @@ pub mod beacon { signature: bls::generics::GenericAggregateSignature::deserialize( signature.as_slice(), ) - .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + .map_err(|e| ProtosError::Bls(format!("{:?}", e)))?, }) } } impl TryFrom for types::ProposerSlashing { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from( ProposerSlashing { @@ -481,17 +471,17 @@ pub mod beacon { ) -> Result { Ok(Self { signed_header_1: signed_header_1 - .ok_or(StreamingFastProtosError::NullSigner)? + .ok_or(ProtosError::NullSigner)? .try_into()?, signed_header_2: signed_header_2 - .ok_or(StreamingFastProtosError::NullSigner)? + .ok_or(ProtosError::NullSigner)? .try_into()?, }) } } impl TryFrom for types::SignedBeaconBlockHeader { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from( SignedBeaconBlockHeader { message, signature }: SignedBeaconBlockHeader, @@ -501,20 +491,20 @@ pub mod beacon { signature: bls::generics::GenericSignature::deserialize( signature.as_slice(), ) - .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + .map_err(|e| ProtosError::Bls(format!("{:?}", e)))?, }) } } impl TryFrom for types::SignedBlsToExecutionChange { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from( SignedBlsToExecutionChange { message, signature }: SignedBlsToExecutionChange, ) -> Result { Ok(Self { message: message - .ok_or(StreamingFastProtosError::NullBlsToExecutionChange)? + .ok_or(ProtosError::NullBlsToExecutionChange)? .try_into()?, signature: bls::generics::GenericSignature::deserialize( signature.as_slice(), @@ -525,35 +515,33 @@ pub mod beacon { } impl TryFrom for types::SignedVoluntaryExit { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from( SignedVoluntaryExit { message, signature }: SignedVoluntaryExit, ) -> Result { Ok(Self { - message: message - .ok_or(StreamingFastProtosError::NullVoluntaryExit)? - .into(), + message: message.ok_or(ProtosError::NullVoluntaryExit)?.into(), signature: bls::generics::GenericSignature::deserialize( signature.as_slice(), ) - .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + .map_err(|e| ProtosError::Bls(format!("{:?}", e)))?, }) } } impl TryFrom for Block { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from(response: SingleBlockResponse) -> Result { - let any = response.block.ok_or(StreamingFastProtosError::NullBlock)?; + let any = response.block.ok_or(ProtosError::NullBlock)?; let block = Block::decode(any.value.as_ref())?; Ok(block) } } impl TryFrom for types::SyncAggregate { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from( SyncAggregate { @@ -566,14 +554,12 @@ pub mod beacon { Bitfield::::SyncCommitteeSize>>::from_bytes( sync_commitee_bits.as_slice().into(), ) - .map_err(|e| { - StreamingFastProtosError::SszTypesError(format!("{:?}", e)) - })?, + .map_err(|e| ProtosError::SszTypesError(format!("{:?}", e)))?, sync_committee_signature: bls::generics::GenericAggregateSignature::deserialize( sync_comittee_signature.as_slice(), ) - .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + .map_err(|e| ProtosError::Bls(format!("{:?}", e)))?, }) } } @@ -611,7 +597,7 @@ pub mod beacon { } impl TryFrom for types::BeaconBlockBodyDeneb { - type Error = StreamingFastProtosError; + type Error = ProtosError; fn try_from( DenebBody { @@ -633,13 +619,13 @@ pub mod beacon { ) -> Result { let beacon_block_body = BeaconBlockBodyDeneb { randao_reveal: bls::generics::GenericSignature::deserialize(&rando_reveal) - .map_err(|e| StreamingFastProtosError::Bls(format!("{:?}", e)))?, + .map_err(|e| ProtosError::Bls(format!("{:?}", e)))?, eth1_data: eth1_data .map(|eth1_data| eth1_data.into()) .unwrap_or_default(), graffiti: Graffiti::from( <[u8; GRAFFITI_BYTES_LEN]>::try_from(graffiti.as_slice()) - .map_err(|_| StreamingFastProtosError::GraffitiInvalid)?, + .map_err(|_| ProtosError::GraffitiInvalid)?, ), proposer_slashings: proposer_slashings .into_iter() @@ -671,7 +657,7 @@ pub mod beacon { .transpose()? .unwrap_or_else(types::SyncAggregate::new), execution_payload: execution_payload - .ok_or(StreamingFastProtosError::NullExecutionPayload) + .ok_or(ProtosError::NullExecutionPayload) .and_then(types::ExecutionPayloadDeneb::try_from)? .into(), bls_to_execution_changes: bls_to_execution_changes @@ -684,7 +670,7 @@ pub mod beacon { .map(|blob_kzg_commitment| { <[u8; 48]>::try_from(blob_kzg_commitment.as_slice()) .map(types::KzgCommitment) - .map_err(|_| StreamingFastProtosError::KzgCommitmentInvalid) + .map_err(|_| ProtosError::KzgCommitmentInvalid) }) .collect::, _>>()? .into(), @@ -697,7 +683,7 @@ pub mod beacon { } #[derive(Error, Debug)] -pub enum StreamingFastProtosError { +pub enum ProtosError { #[error("Block conversion error")] BlockConversionError, From d2f23efcbc851a2c2823355e68b5160ae2503f7b Mon Sep 17 00:00:00 2001 From: Joseph Livesey Date: Fri, 13 Sep 2024 18:35:59 -0400 Subject: [PATCH 4/4] build: fix position of protos folder --- build.rs | 10 +++++----- {src/protos => protos}/block.proto | 0 {src/protos => protos}/bstream.proto | 0 {src/protos => protos}/firehose.proto | 0 {src/protos => protos}/type.proto | 0 5 files changed, 5 insertions(+), 5 deletions(-) rename {src/protos => protos}/block.proto (100%) rename {src/protos => protos}/bstream.proto (100%) rename {src/protos => protos}/firehose.proto (100%) rename {src/protos => protos}/type.proto (100%) diff --git a/build.rs b/build.rs index 29534ef..1990aea 100644 --- a/build.rs +++ b/build.rs @@ -18,12 +18,12 @@ fn main() { .compile_with_config( config, &[ - "src/protos/block.proto", - "src/protos/bstream.proto", - "src/protos/firehose.proto", - "src/protos/type.proto", + "protos/block.proto", + "protos/bstream.proto", + "protos/firehose.proto", + "protos/type.proto", ], - &["src/protos/"], + &["protos/"], ) .unwrap(); } diff --git a/src/protos/block.proto b/protos/block.proto similarity index 100% rename from src/protos/block.proto rename to protos/block.proto diff --git a/src/protos/bstream.proto b/protos/bstream.proto similarity index 100% rename from src/protos/bstream.proto rename to protos/bstream.proto diff --git a/src/protos/firehose.proto b/protos/firehose.proto similarity index 100% rename from src/protos/firehose.proto rename to protos/firehose.proto diff --git a/src/protos/type.proto b/protos/type.proto similarity index 100% rename from src/protos/type.proto rename to protos/type.proto