Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into alloy
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul committed Aug 2, 2024
2 parents 5331ee4 + 0b538a4 commit f28fd80
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 16 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,19 @@ jobs:
run: cargo test --release
- name: Check all examples, binaries, etc
run: cargo check --all-targets
coverage:
runs-on: ubuntu-latest
name: cargo-tarpaulin
steps:
- uses: actions/checkout@v3
- name: Get latest version of stable Rust
run: rustup update stable
- name: Install cargo-tarpaulin
uses: taiki-e/install-action@cargo-tarpaulin
- name: Check code coverage with cargo-tarpaulin
run: make coverage
- name: Upload to codecov.io
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Cargo.lock
target
cobertura.xml
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# These hacks are required for the test binaries depending on the dynamic library libstd-*.so
# See: https://github.com/rust-lang/cargo/issues/4651
#
# We also need to exclude the derive macro from coverage because it will always show as 0% by
# virtue of executing at compile-time outside the view of Tarpaulin.
coverage:
env LD_LIBRARY_PATH="$(shell rustc --print sysroot)/lib" \
cargo-tarpaulin --workspace --all-features --out xml --exclude ethereum_ssz_derive

.PHONY: coverage
8 changes: 8 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
coverage:
status:
project:
default:
informational: true
patch:
default:
informational: true
2 changes: 1 addition & 1 deletion ssz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ethereum_ssz_derive = { version = "0.5.4", path = "../ssz_derive" }
[dependencies]
alloy-primitives = "0.7.7"
smallvec = { version = "1.6.1", features = ["const_generics"] }
itertools = "0.10.3"
itertools = "0.13.0"

[features]
arbitrary = ["alloy-primitives/arbitrary"]
128 changes: 121 additions & 7 deletions ssz/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
use alloy_primitives::{Address, B256};
use ssz::{Decode, DecodeError, Encode};
use ssz_derive::{Decode, Encode};
use std::num::NonZeroUsize;

mod round_trip {
use alloy_primitives::B256;

use super::*;
use std::collections::BTreeMap;
use std::collections::{BTreeMap, BTreeSet};
use std::iter::FromIterator;
use std::sync::Arc;

fn round_trip<T: Encode + Decode + std::fmt::Debug + PartialEq>(items: Vec<T>) {
assert_eq!(
<T as Encode>::is_ssz_fixed_len(),
<T as Decode>::is_ssz_fixed_len()
);
assert_eq!(
<T as Encode>::ssz_fixed_len(),
<T as Decode>::ssz_fixed_len()
);

for item in items {
let encoded = &item.as_ssz_bytes();
assert_eq!(item.ssz_bytes_len(), encoded.len());
Expand Down Expand Up @@ -38,8 +48,33 @@ mod round_trip {
}

#[test]
fn b256() {
let items: Vec<B256> = vec![B256::ZERO, B256::from([1; 32]), B256::random()];
fn address() {
let items: Vec<Address> = vec![
Address::repeat_byte(0),
Address::from([1; 20]),
Address::random(),
];

round_trip(items);
}

#[test]
fn vec_of_address() {
let items: Vec<Vec<Address>> = vec![
vec![],
vec![
Address::repeat_byte(0),
Address::from([1; 20]),
Address::random(),
],
];

round_trip(items);
}

#[test]
fn h256() {
let items: Vec<B256> = vec![B256::repeat_byte(0), B256::from([1; 32]), B256::random()];

round_trip(items);
}
Expand Down Expand Up @@ -156,7 +191,7 @@ mod round_trip {
round_trip(items);
}

#[derive(Debug, PartialEq, Encode, Decode)]
#[derive(Debug, PartialEq, Eq, Encode, Decode)]
struct VariableLen {
a: u16,
b: Vec<u16>,
Expand Down Expand Up @@ -278,7 +313,7 @@ mod round_trip {
round_trip(items);
}

#[derive(Debug, PartialEq, Encode, Decode)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Encode, Decode)]
struct ThreeVariableLen {
a: u16,
b: Vec<u16>,
Expand Down Expand Up @@ -388,4 +423,83 @@ mod round_trip {
];
round_trip(data);
}

#[test]
fn btree_set_fixed() {
let data = vec![BTreeSet::new(), BTreeSet::from_iter(vec![0u16, 2, 4, 6])];
round_trip(data);
}

#[test]
fn btree_set_variable_len() {
let data = vec![
BTreeSet::new(),
BTreeSet::from_iter(vec![
ThreeVariableLen {
a: 1,
b: vec![3, 5, 7],
c: vec![],
d: vec![0, 0],
},
ThreeVariableLen {
a: 99,
b: vec![1],
c: vec![2, 3, 4, 5, 6, 7, 8, 9, 10],
d: vec![4, 5, 6, 7, 8],
},
ThreeVariableLen {
a: 0,
b: vec![],
c: vec![],
d: vec![],
},
]),
];
round_trip(data);
}

#[test]
fn non_zero_usize() {
let data = vec![
NonZeroUsize::new(1).unwrap(),
NonZeroUsize::new(u16::MAX as usize).unwrap(),
NonZeroUsize::new(usize::MAX).unwrap(),
];
round_trip(data);
}

#[test]
fn arc_u64() {
let data = vec![Arc::new(0u64), Arc::new(u64::MAX)];
round_trip(data);
}

#[test]
fn arc_vec_u64() {
let data = vec![Arc::new(vec![0u64]), Arc::new(vec![u64::MAX; 10])];
round_trip(data);
}
}

/// Decode tests that are expected to fail.
mod decode_fail {
use super::*;

#[test]
fn non_zero_usize() {
let zero_bytes = 0usize.as_ssz_bytes();
assert!(NonZeroUsize::from_ssz_bytes(&zero_bytes).is_err());
}

#[test]
fn hash160() {
let long_bytes = B256::repeat_byte(0xff).as_ssz_bytes();
assert!(Address::from_ssz_bytes(&long_bytes).is_err());
}

#[test]
fn hash256() {
let long_bytes = vec![0xff; 257];
assert!(B256::from_ssz_bytes(&long_bytes).is_err());
}
}
6 changes: 3 additions & 3 deletions ssz_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ name = "ssz_derive"
proc-macro = true

[dependencies]
syn = "1.0.42"
syn = "2.0.69"
proc-macro2 = "1.0.23"
quote = "1.0.7"
darling = "0.13.0"
quote = "1.0.18"
darling = "0.20.9"

[dev-dependencies]
ethereum_ssz = { path = "../ssz" }
11 changes: 6 additions & 5 deletions ssz_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,11 @@ fn parse_ssz_fields(
let field_opts_candidates = field
.attrs
.iter()
.filter(|attr| attr.path.get_ident().map_or(false, |ident| *ident == "ssz"))
.filter(|attr| {
attr.path()
.get_ident()
.map_or(false, |ident| *ident == "ssz")
})
.collect::<Vec<_>>();

if field_opts_candidates.len() > 1 {
Expand All @@ -297,10 +301,7 @@ fn parse_ssz_fields(

let field_opts = field_opts_candidates
.first()
.map(|attr| {
let meta = attr.parse_meta().unwrap();
FieldOpts::from_meta(&meta).unwrap()
})
.map(|attr| FieldOpts::from_meta(&attr.meta).unwrap())
.unwrap_or_default();

(ty, ident, field_opts)
Expand Down

0 comments on commit f28fd80

Please sign in to comment.