From fcccf47f5519002e5a00c729ff3a02ccbf8b7172 Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Wed, 19 Jul 2023 11:42:11 +1000 Subject: [PATCH 01/12] explicitly set rust version --- .github/workflows/mips.yml | 2 +- .github/workflows/rust.yml | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/mips.yml b/.github/workflows/mips.yml index 51049c5..0078408 100644 --- a/.github/workflows/mips.yml +++ b/.github/workflows/mips.yml @@ -38,7 +38,7 @@ jobs: - uses: actions-rs/toolchain@v1 with: profile: minimal - toolchain: nightly + toolchain: nightly-2023-05-03 default: true override: true components: rust-src diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 2be5ae9..5481655 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -35,8 +35,8 @@ jobs: tool: cargo-nextest,just - name: Install `rust` toolchain run: | - rustup toolchain install nightly --no-self-update --profile minimal - rustup default nightly + rustup toolchain install nightly-2023-05-03 --no-self-update --profile minimal + rustup default nightly-2023-05-03 - name: Setup protoc uses: arduino/setup-protoc@v1.1.2 - name: Download consensus-spec-tests @@ -84,7 +84,7 @@ jobs: - uses: actions-rs/toolchain@v1 with: profile: minimal - toolchain: nightly + toolchain: nightly-2023-05-03 override: true components: clippy,rustfmt - name: Run cargo fmt From f5fff1c7daea415606742ef31c99ea410fad83aa Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Wed, 19 Jul 2023 11:46:25 +1000 Subject: [PATCH 02/12] update build.sh --- zipline-state-transition-mips/build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zipline-state-transition-mips/build.sh b/zipline-state-transition-mips/build.sh index a0d7a1d..151061a 100755 --- a/zipline-state-transition-mips/build.sh +++ b/zipline-state-transition-mips/build.sh @@ -12,7 +12,7 @@ CXX_mips_unknown_none=mips-linux-gnu-g++ \ CARGO_TARGET_MIPS_UNKNOWN_NONE_LINKER=mips-linux-gnu-gcc \ RUSTFLAGS="-Clink-arg=-e_start" \ CARGO_TARGET_DIR=${SPEC}_target \ - cargo +nightly build --verbose --release --target=mips-unknown-none.json -Zbuild-std --no-default-features --features="$SPEC" + cargo +nightly-2023-05-03 build --verbose --release --target=mips-unknown-none.json -Zbuild-std --no-default-features --features="$SPEC" python3 -m venv venv From 927550e0715dd4eb5f078820bc4f6eb3a2b9a4e7 Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Wed, 19 Jul 2023 12:01:02 +1000 Subject: [PATCH 03/12] add back version lock for ethreum-consensus crate --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index b5345cb..29ff6f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ exclude = [ [patch.crates-io] blst = { git = "https://github.com/ec2/blst", rev = "179bf0e" } [patch."https://github.com/ralexstokes/ethereum-consensus"] -# ethereum-consensus = { git = "https://github.com/ralexstokes//ethereum-consensus", rev = "ef89b4a4ef97cdd53a66ddb52e554667aca0beb2" } +ethereum-consensus = { git = "https://github.com/ralexstokes//ethereum-consensus", rev = "ef89b4a4ef97cdd53a66ddb52e554667aca0beb2" } [patch."https://github.com/ralexstokes/ssz-rs"] # patched version to build in restricted_std. Compatible with version of ethereum-consensus above ssz-rs = { path = "./finality-client/libs/ssz-rs/ssz-rs" } From efaaf2acb8a9b70299824a463fc396c7fd7701cf Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Wed, 19 Jul 2023 12:18:29 +1000 Subject: [PATCH 04/12] use workspace dependencies to avoid feature leak error --- Cargo.toml | 3 +++ finality-client/Cargo.toml | 2 +- finality-client/libs/test-utils/Cargo.toml | 2 +- finality-client/libs/zipline-spec/Cargo.toml | 2 +- zipline-state-transition-mips/Cargo.toml | 4 ++++ zipline-state-transition-mips/Dockerfile | 9 ++++----- 6 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 29ff6f5..b67859a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,9 @@ exclude = [ "zipline-state-transition-mips" ] +[workspace.dependencies] +ssz-rs = { path = "./finality-client/libs/ssz-rs/ssz-rs", default-features = false } + [patch.crates-io] blst = { git = "https://github.com/ec2/blst", rev = "179bf0e" } [patch."https://github.com/ralexstokes/ethereum-consensus"] diff --git a/finality-client/Cargo.toml b/finality-client/Cargo.toml index 8b88997..ffaba90 100644 --- a/finality-client/Cargo.toml +++ b/finality-client/Cargo.toml @@ -9,7 +9,7 @@ crypto = { path = "libs/crypto" } zipline-spec = { path = "libs/zipline-spec" } validator-shuffling = { path = "libs/validator-shuffling" } log = "0.4.17" -ssz-rs = { git = "https://github.com/ralexstokes/ssz-rs", default-features = false } +ssz-rs = { workspace = true } hex = { version = "0.4.3", default-features = false } typenum = {version = "1.16.0", feature=["no_std"]} # snap = "1.1.0" # breaks MIPS build. We could easily patch it though once we need it diff --git a/finality-client/libs/test-utils/Cargo.toml b/finality-client/libs/test-utils/Cargo.toml index 8106246..3aa6aea 100644 --- a/finality-client/libs/test-utils/Cargo.toml +++ b/finality-client/libs/test-utils/Cargo.toml @@ -9,4 +9,4 @@ edition = "2021" serde = "1.0.158" serde_yaml = "0.9.19" snap = "1.1.0" -ssz-rs = { git = "https://github.com/ralexstokes/ssz-rs", default-features = false } +ssz-rs = { workspace = true } diff --git a/finality-client/libs/zipline-spec/Cargo.toml b/finality-client/libs/zipline-spec/Cargo.toml index 4ff1a72..27c51da 100644 --- a/finality-client/libs/zipline-spec/Cargo.toml +++ b/finality-client/libs/zipline-spec/Cargo.toml @@ -5,5 +5,5 @@ edition = "2021" [dependencies] typenum = "1.16.0" -ssz-rs = { git = "https://github.com/willemolding/ssz-rs", rev = "c689476", default-features = false } +ssz-rs = { workspace = true } hex-literal = "0.3.4" diff --git a/zipline-state-transition-mips/Cargo.toml b/zipline-state-transition-mips/Cargo.toml index 110ac1b..0a9757e 100644 --- a/zipline-state-transition-mips/Cargo.toml +++ b/zipline-state-transition-mips/Cargo.toml @@ -29,6 +29,10 @@ minimal = [] default = ["mainnet"] # need to patch here as well because this crate isn't part of the workspace +[patch.crates-io] +blst = { git = "https://github.com/ec2/blst", rev = "179bf0e" } +[patch."https://github.com/ralexstokes/ethereum-consensus"] +ethereum-consensus = { git = "https://github.com/ralexstokes//ethereum-consensus", rev = "ef89b4a4ef97cdd53a66ddb52e554667aca0beb2" } [patch."https://github.com/ralexstokes/ssz-rs"] # patched version to build in restricted_std. Compatible with version of ethereum-consensus above ssz-rs = { git = "https://github.com/willemolding/ssz-rs", rev = "c689476" } diff --git a/zipline-state-transition-mips/Dockerfile b/zipline-state-transition-mips/Dockerfile index 8542ea3..07e0715 100644 --- a/zipline-state-transition-mips/Dockerfile +++ b/zipline-state-transition-mips/Dockerfile @@ -1,12 +1,11 @@ -FROM ubuntu:20.04 +FROM ubuntu:22.04 ENV SHELL=/bin/bash ENV DEBIAN_FRONTEND noninteractive -# Use this specific version of rust. This is needed because versions of rust after this one broke -# support for -Zbuild-std for the embedded targets. -ENV RUST_VERSION nightly +ENV RUST_VERSION nightly-2023-05-03 RUN apt-get update && apt-get install --assume-yes --no-install-recommends \ + ca-certificates \ build-essential \ curl \ g++-mips-linux-gnu \ @@ -16,7 +15,7 @@ RUN apt-get update && apt-get install --assume-yes --no-install-recommends \ make \ cmake \ git \ - python3 python3.8-venv python3-pip + python3 python3-venv python3-pip RUN pip3 install wheel From d48b4edadfc3cea36f59f5532b64e86309c00cf8 Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Wed, 19 Jul 2023 12:23:45 +1000 Subject: [PATCH 05/12] remove unused patches --- zipline-state-transition-mips/Cargo.toml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/zipline-state-transition-mips/Cargo.toml b/zipline-state-transition-mips/Cargo.toml index 0a9757e..fce668a 100644 --- a/zipline-state-transition-mips/Cargo.toml +++ b/zipline-state-transition-mips/Cargo.toml @@ -31,8 +31,3 @@ default = ["mainnet"] # need to patch here as well because this crate isn't part of the workspace [patch.crates-io] blst = { git = "https://github.com/ec2/blst", rev = "179bf0e" } -[patch."https://github.com/ralexstokes/ethereum-consensus"] -ethereum-consensus = { git = "https://github.com/ralexstokes//ethereum-consensus", rev = "ef89b4a4ef97cdd53a66ddb52e554667aca0beb2" } -[patch."https://github.com/ralexstokes/ssz-rs"] -# patched version to build in restricted_std. Compatible with version of ethereum-consensus above -ssz-rs = { git = "https://github.com/willemolding/ssz-rs", rev = "c689476" } From a1055f5d0b1fd47aefaf05b9d8a6937e1a614bb6 Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Wed, 19 Jul 2023 12:27:36 +1000 Subject: [PATCH 06/12] removes rust caching --- .github/workflows/rust.yml | 5 ----- Cargo.toml | 1 + finality-client/Cargo.toml | 2 +- finality-client/libs/crypto/Cargo.toml | 2 +- 4 files changed, 3 insertions(+), 7 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 5481655..15e7b95 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -24,11 +24,6 @@ jobs: uses: actions/checkout@v3 with: lfs: true - - uses: Swatinem/rust-cache@v2 - - name: Run sccache-cache - uses: mozilla-actions/sccache-action@v0.0.3 - with: - token: ${{ secrets.GITHUB_TOKEN }} - name: Install development tools uses: taiki-e/install-action@v2.4.2 with: diff --git a/Cargo.toml b/Cargo.toml index b67859a..8bbd7a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ exclude = [ [workspace.dependencies] ssz-rs = { path = "./finality-client/libs/ssz-rs/ssz-rs", default-features = false } +ethereum-consensus = { git = "https://github.com/ralexstokes/ethereum-consensus" } [patch.crates-io] blst = { git = "https://github.com/ec2/blst", rev = "179bf0e" } diff --git a/finality-client/Cargo.toml b/finality-client/Cargo.toml index ffaba90..65ecde7 100644 --- a/finality-client/Cargo.toml +++ b/finality-client/Cargo.toml @@ -17,7 +17,7 @@ once_cell = {version = "1.17", default-features = false} [dev-dependencies] env_logger = "0.10.0" -ethereum-consensus = { git = "https://github.com/ralexstokes/ethereum-consensus" } +ethereum-consensus = { workspace = true } serde = "1.0.158" test-utils = { path = "libs/test-utils" } diff --git a/finality-client/libs/crypto/Cargo.toml b/finality-client/libs/crypto/Cargo.toml index 77956c6..acd25d6 100644 --- a/finality-client/libs/crypto/Cargo.toml +++ b/finality-client/libs/crypto/Cargo.toml @@ -11,7 +11,7 @@ sha2 = { version = "0.10.6", default-features = false } [dev-dependencies] test-utils = { path = "../test-utils" } -ethereum-consensus = { git = "https://github.com/ralexstokes/ethereum-consensus" } +ethereum-consensus = { workspace = true } serde = "1.0.158" serde_with = "1.13.0" From 08b499b36b76b524a5141c3a5eba04e94989d904 Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Wed, 19 Jul 2023 12:30:05 +1000 Subject: [PATCH 07/12] complete removing cache --- .github/workflows/rust.yml | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 15e7b95..f433a09 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -8,9 +8,6 @@ on: pull_request: branches: - main -env: - SCCACHE_GHA_ENABLED: "true" - RUSTC_WRAPPER: "sccache" jobs: build-mips: @@ -55,11 +52,6 @@ jobs: steps: - name: Checkout Source uses: actions/checkout@v3 - - uses: Swatinem/rust-cache@v2 - - name: Run sccache-cache - uses: mozilla-actions/sccache-action@v0.0.3 - with: - token: ${{ secrets.GITHUB_TOKEN }} - name: Setup protoc uses: arduino/setup-protoc@v1.1.2 - uses: actions-rs/cargo@v1 @@ -71,11 +63,6 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - uses: Swatinem/rust-cache@v2 - - name: Run sccache-cache - uses: mozilla-actions/sccache-action@v0.0.3 - with: - token: ${{ secrets.GITHUB_TOKEN }} - uses: actions-rs/toolchain@v1 with: profile: minimal From 2e3518efbc0dcb31a0ccca06f39fd526b112f963 Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Wed, 19 Jul 2023 12:31:40 +1000 Subject: [PATCH 08/12] complete rust fmt on internal ssz-rs --- .../libs/ssz-rs/ssz-rs-derive/src/lib.rs | 5 +-- .../examples/container_with_some_types.rs | 4 +-- .../libs/ssz-rs/ssz-rs/src/array.rs | 8 ++--- .../libs/ssz-rs/ssz-rs/src/bitlist.rs | 10 +++--- .../libs/ssz-rs/ssz-rs/src/bitvector.rs | 10 +++--- finality-client/libs/ssz-rs/ssz-rs/src/de.rs | 6 ++-- finality-client/libs/ssz-rs/ssz-rs/src/lib.rs | 2 +- .../libs/ssz-rs/ssz-rs/src/list.rs | 4 +-- .../ssz-rs/ssz-rs/src/merkleization/mod.rs | 10 +++--- .../ssz-rs/ssz-rs/src/merkleization/proofs.rs | 31 ++++++++++++------- finality-client/libs/ssz-rs/ssz-rs/src/ser.rs | 2 +- .../libs/ssz-rs/ssz-rs/src/uint.rs | 8 ++--- .../libs/ssz-rs/ssz-rs/src/union.rs | 2 +- .../libs/ssz-rs/ssz-rs/src/vector.rs | 10 +++--- 14 files changed, 58 insertions(+), 54 deletions(-) diff --git a/finality-client/libs/ssz-rs/ssz-rs-derive/src/lib.rs b/finality-client/libs/ssz-rs/ssz-rs-derive/src/lib.rs index d38e109..8abdeb0 100644 --- a/finality-client/libs/ssz-rs/ssz-rs-derive/src/lib.rs +++ b/finality-client/libs/ssz-rs/ssz-rs-derive/src/lib.rs @@ -180,7 +180,7 @@ fn derive_deserialize_impl(data: &Data) -> TokenStream { Ok(container) } - }; + } } _ => unimplemented!( "this type of struct is currently not supported by this derive macro" @@ -469,12 +469,9 @@ fn derive_treeify_impl(data: &Data) -> TokenStream { quote_spanned! { variant.span() => Self::#variant_name(value) => { let mut tree = value.to_merkle_tree()?; - let selector = #i; let data_root = value.hash_tree_root()?; tree.push(ssz_rs::__internal::mix_in_selector_tree(&data_root, selector)); - - Ok(tree) } } diff --git a/finality-client/libs/ssz-rs/ssz-rs/examples/container_with_some_types.rs b/finality-client/libs/ssz-rs/ssz-rs/examples/container_with_some_types.rs index 1ffd586..6e6f5c7 100644 --- a/finality-client/libs/ssz-rs/ssz-rs/examples/container_with_some_types.rs +++ b/finality-client/libs/ssz-rs/ssz-rs/examples/container_with_some_types.rs @@ -50,7 +50,7 @@ fn main() { Ok(encoding) => encoding, Err(err) => { eprintln!("some error encoding: {err}"); - return; + return } }; @@ -58,7 +58,7 @@ fn main() { Ok(value) => value, Err(err) => { eprintln!("some error decoding: {err}"); - return; + return } }; diff --git a/finality-client/libs/ssz-rs/ssz-rs/src/array.rs b/finality-client/libs/ssz-rs/ssz-rs/src/array.rs index b5c1e94..c61f4b8 100644 --- a/finality-client/libs/ssz-rs/ssz-rs/src/array.rs +++ b/finality-client/libs/ssz-rs/ssz-rs/src/array.rs @@ -35,7 +35,7 @@ macro_rules! define_ssz_for_array_of_size { { fn serialize(&self, buffer: &mut Vec) -> Result { if $n == 0 { - return Err(TypeError::InvalidBound($n).into()); + return Err(TypeError::InvalidBound($n).into()) } serialize_composite(self, buffer) } @@ -47,7 +47,7 @@ macro_rules! define_ssz_for_array_of_size { { fn deserialize(encoding: &[u8]) -> Result { if $n == 0 { - return Err(TypeError::InvalidBound($n).into()); + return Err(TypeError::InvalidBound($n).into()) } if !T::is_variable_size() { @@ -56,13 +56,13 @@ macro_rules! define_ssz_for_array_of_size { return Err(DeserializeError::ExpectedFurtherInput { provided: encoding.len(), expected: expected_length, - }); + }) } if encoding.len() > expected_length { return Err(DeserializeError::AdditionalInput { provided: encoding.len(), expected: expected_length, - }); + }) } } let elements = deserialize_homogeneous_composite(encoding)?; diff --git a/finality-client/libs/ssz-rs/ssz-rs/src/bitlist.rs b/finality-client/libs/ssz-rs/ssz-rs/src/bitlist.rs index fc68653..df35908 100644 --- a/finality-client/libs/ssz-rs/ssz-rs/src/bitlist.rs +++ b/finality-client/libs/ssz-rs/ssz-rs/src/bitlist.rs @@ -98,7 +98,7 @@ impl Bitlist { with_length_bit: bool, ) -> Result { if self.len() > N { - return Err(InstanceError::Bounded { bound: N, provided: self.len() }.into()); + return Err(InstanceError::Bounded { bound: N, provided: self.len() }.into()) } let start_len = buffer.len(); buffer.extend_from_slice(self.as_raw_slice()); @@ -151,14 +151,14 @@ impl Deserialize for Bitlist { fn deserialize(encoding: &[u8]) -> Result { let max_len = byte_length(N); if encoding.is_empty() { - return Err(DeserializeError::ExpectedFurtherInput { provided: 0, expected: max_len }); + return Err(DeserializeError::ExpectedFurtherInput { provided: 0, expected: max_len }) } if encoding.len() > max_len { return Err(DeserializeError::AdditionalInput { provided: encoding.len(), expected: max_len, - }); + }) } let (last_byte, prefix) = encoding.split_last().unwrap(); @@ -167,7 +167,7 @@ impl Deserialize for Bitlist { let high_bit_index = 8 - last.trailing_zeros(); if !last[high_bit_index - 1] { - return Err(DeserializeError::InvalidByte(*last_byte)); + return Err(DeserializeError::InvalidByte(*last_byte)) } for bit in last.iter().take(high_bit_index - 1) { @@ -175,7 +175,7 @@ impl Deserialize for Bitlist { } // TODO: this seems redundant... if result.len() > N { - return Err(InstanceError::Bounded { bound: N, provided: result.len() }.into()); + return Err(InstanceError::Bounded { bound: N, provided: result.len() }.into()) } Ok(Self(result)) } diff --git a/finality-client/libs/ssz-rs/ssz-rs/src/bitvector.rs b/finality-client/libs/ssz-rs/ssz-rs/src/bitvector.rs index b43c9d9..c43be5f 100644 --- a/finality-client/libs/ssz-rs/ssz-rs/src/bitvector.rs +++ b/finality-client/libs/ssz-rs/ssz-rs/src/bitvector.rs @@ -127,7 +127,7 @@ impl Sized for Bitvector { impl Serialize for Bitvector { fn serialize(&self, buffer: &mut Vec) -> Result { if N == 0 { - return Err(TypeError::InvalidBound(N).into()); + return Err(TypeError::InvalidBound(N).into()) } let bytes_to_write = Self::size_hint(); buffer.reserve(bytes_to_write); @@ -141,7 +141,7 @@ impl Serialize for Bitvector { impl Deserialize for Bitvector { fn deserialize(encoding: &[u8]) -> Result { if N == 0 { - return Err(TypeError::InvalidBound(N).into()); + return Err(TypeError::InvalidBound(N).into()) } let expected_length = byte_length(N); @@ -149,13 +149,13 @@ impl Deserialize for Bitvector { return Err(DeserializeError::ExpectedFurtherInput { provided: encoding.len(), expected: expected_length, - }); + }) } if encoding.len() > expected_length { return Err(DeserializeError::AdditionalInput { provided: encoding.len(), expected: expected_length, - }); + }) } let mut result = Self::default(); @@ -167,7 +167,7 @@ impl Deserialize for Bitvector { let last_byte = encoding.last().unwrap(); let remainder_bits = last_byte >> remainder_count; if remainder_bits != 0 { - return Err(DeserializeError::InvalidByte(*last_byte)); + return Err(DeserializeError::InvalidByte(*last_byte)) } } Ok(result) diff --git a/finality-client/libs/ssz-rs/ssz-rs/src/de.rs b/finality-client/libs/ssz-rs/ssz-rs/src/de.rs index c2ec8b5..3763560 100644 --- a/finality-client/libs/ssz-rs/ssz-rs/src/de.rs +++ b/finality-client/libs/ssz-rs/ssz-rs/src/de.rs @@ -59,7 +59,7 @@ where return Err(DeserializeError::AdditionalInput { provided: encoding.len(), expected: encoding.len() - remainder, - }); + }) } let mut elements = vec![]; @@ -77,7 +77,7 @@ where T: SimpleSerialize, { if encoding.is_empty() { - return Ok(vec![]); + return Ok(vec![]) } let data_pointer = u32::deserialize(&encoding[..BYTES_PER_LENGTH_OFFSET])?; @@ -86,7 +86,7 @@ where return Err(DeserializeError::ExpectedFurtherInput { provided: encoding.len(), expected: data_pointer, - }); + }) } let offsets = &mut encoding[..data_pointer] diff --git a/finality-client/libs/ssz-rs/ssz-rs/src/lib.rs b/finality-client/libs/ssz-rs/ssz-rs/src/lib.rs index e0dd590..7b44dad 100644 --- a/finality-client/libs/ssz-rs/ssz-rs/src/lib.rs +++ b/finality-client/libs/ssz-rs/ssz-rs/src/lib.rs @@ -26,7 +26,7 @@ pub use crate::{ de::{Deserialize, DeserializeError}, error::{Error as SimpleSerializeError, InstanceError, TypeError}, list::List, - merkleization::{is_valid_merkle_branch, compute_proof, MerkleizationError, Merkleized, Node}, + merkleization::{compute_proof, is_valid_merkle_branch, MerkleizationError, Merkleized, Node}, ser::{Serialize, SerializeError}, uint::U256, utils::{deserialize, serialize}, diff --git a/finality-client/libs/ssz-rs/ssz-rs/src/list.rs b/finality-client/libs/ssz-rs/ssz-rs/src/list.rs index 73f093e..0428952 100644 --- a/finality-client/libs/ssz-rs/ssz-rs/src/list.rs +++ b/finality-client/libs/ssz-rs/ssz-rs/src/list.rs @@ -188,7 +188,7 @@ where { fn serialize(&self, buffer: &mut Vec) -> Result { if self.len() > N { - return Err(InstanceError::Bounded { bound: N, provided: self.len() }.into()); + return Err(InstanceError::Bounded { bound: N, provided: self.len() }.into()) } serialize_composite(&self.data, buffer) } @@ -201,7 +201,7 @@ where fn deserialize(encoding: &[u8]) -> Result { let result = deserialize_homogeneous_composite(encoding)?; if result.len() > N { - return Err(InstanceError::Bounded { bound: N, provided: result.len() }.into()); + return Err(InstanceError::Bounded { bound: N, provided: result.len() }.into()) } let result = result.try_into().map_err(|(_, err)| match err { Error::Instance(err) => DeserializeError::InvalidInstance(err), diff --git a/finality-client/libs/ssz-rs/ssz-rs/src/merkleization/mod.rs b/finality-client/libs/ssz-rs/ssz-rs/src/merkleization/mod.rs index a0ab47c..adf141b 100644 --- a/finality-client/libs/ssz-rs/ssz-rs/src/merkleization/mod.rs +++ b/finality-client/libs/ssz-rs/ssz-rs/src/merkleization/mod.rs @@ -10,7 +10,7 @@ use sha2::{Digest, Sha256}; pub use cache::Cache as MerkleCache; pub use node::Node; -pub use proofs::{is_valid_merkle_branch, compute_proof}; +pub use proofs::{compute_proof, is_valid_merkle_branch}; pub(crate) const BYTES_PER_CHUNK: usize = 32; @@ -118,7 +118,7 @@ fn merkleize_chunks_with_virtual_padding( if chunk_count == 0 { let depth = height - 1; - return Ok(CONTEXT[depth as usize].try_into().expect("can produce a single root chunk")); + return Ok(CONTEXT[depth as usize].try_into().expect("can produce a single root chunk")) } let mut layer = chunks.to_vec(); @@ -193,7 +193,7 @@ fn treeify_chunks_with_virtual_padding( let mut ret = vec![]; if chunk_count == 0 { - return Ok(ret); + return Ok(ret) } let mut layer = chunks.to_vec(); @@ -281,7 +281,7 @@ pub fn merkleize(chunks: &[u8], limit: Option) -> Result( value == *root } -// only use this method for very small trees. It is extremely inefficient and holds the whole tree in memory (and clones it :()) -pub fn compute_proof(root: &Node, gindex: usize, tree: &[([u8; 32], [u8; 64])]) -> Result, MerkleizationError> { - let tree_map: HashMap<[u8; 32], [u8; 64]> = tree.iter().cloned().collect(); - let root: [u8;32] = root.as_ref().try_into().unwrap(); - let (_, proof): (_, Vec<[u8;32]>) = gindex +// only use this method for very small trees. It is extremely inefficient and holds the whole tree +// in memory (and clones it :()) +pub fn compute_proof( + root: &Node, + gindex: usize, + tree: &[([u8; 32], [u8; 64])], +) -> Result, MerkleizationError> { + let tree_map: HashMap<[u8; 32], [u8; 64]> = tree.iter().cloned().collect(); + let root: [u8; 32] = root.as_ref().try_into().unwrap(); + let (_, proof): (_, Vec<[u8; 32]>) = gindex .view_bits::() .iter() .skip_while(|b| b.as_ref() == &false) // skip the leading zeros .skip(1) // skip the first 1, this just indicates the root .try_fold((root, vec![]), |(hash, mut proof), direction| { let leaves = tree_map.get(hash.as_ref()).ok_or(MerkleizationError::MissingNode)?; - let mut left = [0_u8;32]; - let mut right = [0_u8;32]; + let mut left = [0_u8; 32]; + let mut right = [0_u8; 32]; left.copy_from_slice(&leaves[0..32]); right.copy_from_slice(&leaves[32..64]); match direction.as_ref() { - false => { // left + false => { + // left proof.insert(0, right.try_into().unwrap()); Ok::<_, MerkleizationError>((left, proof)) } - true => { //right + true => { + //right proof.insert(0, left.try_into().unwrap()); Ok((right, proof)) } diff --git a/finality-client/libs/ssz-rs/ssz-rs/src/ser.rs b/finality-client/libs/ssz-rs/ssz-rs/src/ser.rs index 8ee62d9..58f7916 100644 --- a/finality-client/libs/ssz-rs/ssz-rs/src/ser.rs +++ b/finality-client/libs/ssz-rs/ssz-rs/src/ser.rs @@ -58,7 +58,7 @@ pub fn serialize_composite_from_components( ) -> Result { let total_size = fixed_lengths_sum + variable_lengths.iter().sum::(); if total_size as u64 >= MAXIMUM_LENGTH { - return Err(SerializeError::MaximumEncodedLengthExceeded(total_size)); + return Err(SerializeError::MaximumEncodedLengthExceeded(total_size)) } let mut total_bytes_written = 0; diff --git a/finality-client/libs/ssz-rs/ssz-rs/src/uint.rs b/finality-client/libs/ssz-rs/ssz-rs/src/uint.rs index 5ae0c0a..775f2df 100644 --- a/finality-client/libs/ssz-rs/ssz-rs/src/uint.rs +++ b/finality-client/libs/ssz-rs/ssz-rs/src/uint.rs @@ -33,13 +33,13 @@ macro_rules! define_uint { return Err(DeserializeError::ExpectedFurtherInput { provided: encoding.len(), expected: byte_size, - }); + }) } if encoding.len() > byte_size { return Err(DeserializeError::AdditionalInput { provided: encoding.len(), expected: byte_size, - }); + }) } let bytes = encoding[..byte_size].try_into().expect("slice has right length"); @@ -155,13 +155,13 @@ impl Deserialize for U256 { return Err(DeserializeError::ExpectedFurtherInput { provided: encoding.len(), expected: byte_size, - }); + }) } if encoding.len() > byte_size { return Err(DeserializeError::AdditionalInput { provided: encoding.len(), expected: byte_size, - }); + }) } let value = BigUint::from_bytes_le(&encoding[..byte_size]); diff --git a/finality-client/libs/ssz-rs/ssz-rs/src/union.rs b/finality-client/libs/ssz-rs/ssz-rs/src/union.rs index 0a6cb48..9db1b75 100644 --- a/finality-client/libs/ssz-rs/ssz-rs/src/union.rs +++ b/finality-client/libs/ssz-rs/ssz-rs/src/union.rs @@ -44,7 +44,7 @@ where { fn deserialize(encoding: &[u8]) -> Result { if encoding.is_empty() { - return Err(DeserializeError::ExpectedFurtherInput { provided: 0, expected: 1 }); + return Err(DeserializeError::ExpectedFurtherInput { provided: 0, expected: 1 }) } match encoding[0] { diff --git a/finality-client/libs/ssz-rs/ssz-rs/src/vector.rs b/finality-client/libs/ssz-rs/ssz-rs/src/vector.rs index 6112bb9..eea035b 100644 --- a/finality-client/libs/ssz-rs/ssz-rs/src/vector.rs +++ b/finality-client/libs/ssz-rs/ssz-rs/src/vector.rs @@ -89,7 +89,7 @@ impl TryFrom> for Vector { fn try_from(data: Vec) -> Result { if N == 0 { - return Err((data, Error::Type(TypeError::InvalidBound(N)))); + return Err((data, Error::Type(TypeError::InvalidBound(N)))) } if data.len() != N { let len = data.len(); @@ -187,7 +187,7 @@ where { fn serialize(&self, buffer: &mut Vec) -> Result { if N == 0 { - return Err(TypeError::InvalidBound(N).into()); + return Err(TypeError::InvalidBound(N).into()) } serialize_composite(&self.data, buffer) } @@ -199,7 +199,7 @@ where { fn deserialize(encoding: &[u8]) -> Result { if N == 0 { - return Err(TypeError::InvalidBound(N).into()); + return Err(TypeError::InvalidBound(N).into()) } if !T::is_variable_size() { let expected_length = N * T::size_hint(); @@ -207,13 +207,13 @@ where return Err(DeserializeError::ExpectedFurtherInput { provided: encoding.len(), expected: expected_length, - }); + }) } if encoding.len() > expected_length { return Err(DeserializeError::AdditionalInput { provided: encoding.len(), expected: expected_length, - }); + }) } } let inner = deserialize_homogeneous_composite(encoding)?; From 3e5bf4cb4163a517c6b6abaac17b3b8f9ab5c17f Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Wed, 19 Jul 2023 12:54:26 +1000 Subject: [PATCH 09/12] clippy fix --- finality-client/tests/verify.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/finality-client/tests/verify.rs b/finality-client/tests/verify.rs index 4f76b44..ce7dbeb 100644 --- a/finality-client/tests/verify.rs +++ b/finality-client/tests/verify.rs @@ -169,7 +169,7 @@ fn ssz_mainnet() { use std::io::Read; let mut preims = alloc::collections::btree_map::BTreeMap::new(); - while let Ok(_) = preimages_file.read_exact(&mut im) { + while preimages_file.read_exact(&mut im).is_ok() { preimages_file.read_exact(&mut pre).unwrap(); preims.insert(im, pre.to_vec()); } @@ -178,7 +178,7 @@ fn ssz_mainnet() { let inputs_deser: ZiplineInput<2048, 10000, 256> = deserialize(&inputs).unwrap(); let input_hash = hash(&serialize(&inputs_deser).unwrap()); - preims.insert(input_hash.clone().try_into().unwrap(), inputs); + preims.insert(input_hash.try_into().unwrap(), inputs); let hashmap_oracle = HashMapOracle::from(preims); let reader = SszStateReader::new( @@ -196,7 +196,7 @@ fn ssz_mainnet() { >(reader, inputs_deser) .unwrap(); - assert_eq!(result, true); + assert!(result); } // Ignore because it takes too long to run @@ -212,7 +212,7 @@ fn unicorn_mainnet() { let mut im = [0; 32]; use std::io::Read; let mut preims = alloc::collections::btree_map::BTreeMap::new(); - while let Ok(_) = preimages_file.read_exact(&mut im) { + while preimages_file.read_exact(&mut im).is_ok() { preimages_file.read_exact(&mut pre).unwrap(); preims.insert(im, pre.to_vec()); } @@ -375,7 +375,7 @@ fn make_test_oracle_provider< let ssz_nodes = state.to_merkle_tree().unwrap(); let mut provider: Map<[u8; 32], Vec> = - Map::from_iter(ssz_nodes.iter().map(|(k, v)| (k.clone(), v.to_vec()))); + Map::from_iter(ssz_nodes.iter().map(|(k, v)| (*k, v.to_vec()))); let input_bytes = serialize(input).unwrap(); let input_hash = hash(&input_bytes); From aa662ab72d3c3d6fd7e5890b71fd3f30a929daf7 Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Wed, 19 Jul 2023 12:57:24 +1000 Subject: [PATCH 10/12] ignore clippy for the ssz crate --- finality-client/libs/ssz-rs/ssz-rs-derive/src/lib.rs | 1 + finality-client/libs/ssz-rs/ssz-rs/src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/finality-client/libs/ssz-rs/ssz-rs-derive/src/lib.rs b/finality-client/libs/ssz-rs/ssz-rs-derive/src/lib.rs index 8abdeb0..a770aea 100644 --- a/finality-client/libs/ssz-rs/ssz-rs-derive/src/lib.rs +++ b/finality-client/libs/ssz-rs/ssz-rs-derive/src/lib.rs @@ -1,3 +1,4 @@ +#![allow(clippy::all)] //! `SimpleSerialize` provides a macro to derive SSZ containers and union types from //! native Rust structs and enums. //! Refer to the `examples` in the `ssz_rs` crate for a better idea on how to use this derive macro. diff --git a/finality-client/libs/ssz-rs/ssz-rs/src/lib.rs b/finality-client/libs/ssz-rs/ssz-rs/src/lib.rs index 7b44dad..fd28a91 100644 --- a/finality-client/libs/ssz-rs/ssz-rs/src/lib.rs +++ b/finality-client/libs/ssz-rs/ssz-rs/src/lib.rs @@ -1,3 +1,4 @@ +#![allow(clippy::all)] #![cfg_attr(not(feature = "std"), no_std)] #[cfg(not(feature = "std"))] From 597e97c14086a41ce71c37c8b6b887cb05deffc2 Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Wed, 19 Jul 2023 13:03:42 +1000 Subject: [PATCH 11/12] add switch for different map types --- .../libs/ssz-rs/ssz-rs/src/merkleization/proofs.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/finality-client/libs/ssz-rs/ssz-rs/src/merkleization/proofs.rs b/finality-client/libs/ssz-rs/ssz-rs/src/merkleization/proofs.rs index 353e774..54b043b 100644 --- a/finality-client/libs/ssz-rs/ssz-rs/src/merkleization/proofs.rs +++ b/finality-client/libs/ssz-rs/ssz-rs/src/merkleization/proofs.rs @@ -1,7 +1,12 @@ use crate::merkleization::{MerkleizationError, Node}; use bitvec::prelude::*; use sha2::{Digest, Sha256}; -use std::collections::HashMap; + +#[cfg(not(feature = "std"))] +use alloc::{vec, vec::Vec, collections::BTreeMap as Map}; + +#[cfg(feature = "std")] +use std::collections::HashMap as Map; /// `is_valid_merkle_branch` verifies the Merkle proof /// against the `root` given the other metadata. @@ -39,7 +44,7 @@ pub fn compute_proof( gindex: usize, tree: &[([u8; 32], [u8; 64])], ) -> Result, MerkleizationError> { - let tree_map: HashMap<[u8; 32], [u8; 64]> = tree.iter().cloned().collect(); + let tree_map: Map<[u8; 32], [u8; 64]> = tree.iter().cloned().collect(); let root: [u8; 32] = root.as_ref().try_into().unwrap(); let (_, proof): (_, Vec<[u8; 32]>) = gindex .view_bits::() From 1bbe97c28053962272360c3487cbcafdf0fd5382 Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Wed, 19 Jul 2023 13:05:39 +1000 Subject: [PATCH 12/12] fmt --- finality-client/libs/ssz-rs/ssz-rs/src/merkleization/proofs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/finality-client/libs/ssz-rs/ssz-rs/src/merkleization/proofs.rs b/finality-client/libs/ssz-rs/ssz-rs/src/merkleization/proofs.rs index 54b043b..0758146 100644 --- a/finality-client/libs/ssz-rs/ssz-rs/src/merkleization/proofs.rs +++ b/finality-client/libs/ssz-rs/ssz-rs/src/merkleization/proofs.rs @@ -3,7 +3,7 @@ use bitvec::prelude::*; use sha2::{Digest, Sha256}; #[cfg(not(feature = "std"))] -use alloc::{vec, vec::Vec, collections::BTreeMap as Map}; +use alloc::{collections::BTreeMap as Map, vec, vec::Vec}; #[cfg(feature = "std")] use std::collections::HashMap as Map;