Skip to content

Commit

Permalink
chore(clippy): fix lints and improve clippy script
Browse files Browse the repository at this point in the history
  • Loading branch information
petarvujovic98 committed Jun 14, 2024
1 parent 03e3fbf commit 3fc369a
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 58 deletions.
11 changes: 2 additions & 9 deletions .github/workflows/ci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,8 @@ jobs:
steps:
- uses: actions/checkout@v4

- uses: risc0/risc0/.github/actions/[email protected]

- uses: risc0/risc0/.github/actions/[email protected]

- uses: risc0/clippy-action@main
with:
reporter: 'github-pr-check'
fail_on_error: true
clippy_flags: --workspace --all-targets --all-features -- -D warnings
- name: Run clippy check for all targets
run: make clippy

fmt:
name: fmt
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub fn merge(a: &mut Value, b: &Value) {
merge(a.entry(k).or_insert(Value::Null), v);
}
}
(a, b) if !b.is_null() => *a = b.to_owned(),
(a, b) if !b.is_null() => b.clone_into(a),
// If b is null, just keep a (which means do nothing).
_ => {}
}
Expand Down
13 changes: 7 additions & 6 deletions core/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ impl Prover for NativeProver {
output: &GuestOutput,
config: &ProverConfig,
) -> ProverResult<Proof> {
let param = config
.get("native")
.map(|v| NativeParam::deserialize(v))
.ok_or(ProverError::Param(serde_json::Error::custom(
"native param not provided",
)))??;
let param =
config
.get("native")
.map(NativeParam::deserialize)
.ok_or(ProverError::Param(serde_json::Error::custom(
"native param not provided",
)))??;

if let Some(path) = param.write_guest_input_path {
let path = Path::new(&path);
Expand Down
4 changes: 4 additions & 0 deletions harness/core/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ impl AssertionLog {
self.assertions.len()
}

pub fn is_empty(&self) -> bool {
self.len() == 0
}

pub fn display_failures(&self, start: usize, end: usize) {
for i in start..end {
if let Some(assertion) = self.assertions.get(i) {
Expand Down
68 changes: 39 additions & 29 deletions harness/macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, punctuated::Punctuated, Ident, Item, ItemFn, ItemMod, Path, Token};
use syn::{parse_macro_input, Item, ItemFn, ItemMod};

#[cfg(any(feature = "sp1", feature = "risc0"))]
use syn::{punctuated::Punctuated, Ident, Path, Token};

// Helper struct to parse input
#[cfg(any(feature = "sp1", feature = "risc0"))]
struct EntryArgs {
main_entry: Ident,
test_modules: Option<Punctuated<Path, Token![,]>>,
}

#[cfg(any(feature = "sp1", feature = "risc0"))]
impl syn::parse::Parse for EntryArgs {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let main_entry: Ident = input.parse()?;
let test_modules: Option<Punctuated<Path, Token![,]>> = if input.peek(Token![,]) {
input.parse::<Token![,]>()?; // Parse and consume the comma
// Now parse a list of module paths if they are present
Some(input.parse_terminated(Path::parse)?)
} else {
None
};

Ok(EntryArgs {
main_entry,
test_modules,
})
}
}

#[proc_macro]
#[cfg(any(feature = "sp1", feature = "risc0"))]
pub fn entrypoint(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as EntryArgs);
let main_entry = input.main_entry;
Expand Down Expand Up @@ -70,41 +100,21 @@ pub fn entrypoint(input: TokenStream) -> TokenStream {
}
};

#[cfg(all(not(feature = "sp1"), not(feature = "risc0")))]
let output = quote! {
output.into()
}

#[proc_macro]
#[cfg(not(any(feature = "sp1", feature = "risc0")))]
pub fn entrypoint(_input: TokenStream) -> TokenStream {
quote! {
mod generated_main {
#[no_mangle]
fn main() {
super::ENTRY()
}
}
};

output.into()
}

// Helper struct to parse input
struct EntryArgs {
main_entry: Ident,
test_modules: Option<Punctuated<Path, Token![,]>>,
}

impl syn::parse::Parse for EntryArgs {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let main_entry: Ident = input.parse()?;
let test_modules: Option<Punctuated<Path, Token![,]>> = if input.peek(Token![,]) {
input.parse::<Token![,]>()?; // Parse and consume the comma
// Now parse a list of module paths if they are present
Some(input.parse_terminated(Path::parse)?)
} else {
None
};

Ok(EntryArgs {
main_entry,
test_modules,
})
}
.into()
}

#[proc_macro]
Expand Down
4 changes: 2 additions & 2 deletions host/src/server/api/v1/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async fn validate_cache_input(

// double check if cache is valid
if cached_block_hash == real_block_hash {
return Ok(cache_input);
Ok(cache_input)
} else {
Err(HostError::InvalidRequestConfig(
"Cached input is not valid".to_owned(),
Expand Down Expand Up @@ -251,7 +251,7 @@ mod test {
block_number: u64,
) -> (GuestInput, RpcBlockDataProvider) {
let l1_chain_spec = SupportedChainSpecs::default()
.get_chain_spec(&l1_network)
.get_chain_spec(l1_network)
.unwrap();
let taiko_chain_spec = SupportedChainSpecs::default()
.get_chain_spec(network)
Expand Down
16 changes: 8 additions & 8 deletions lib/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,14 @@ pub enum Network {
TaikoMainnet,
}

impl ToString for Network {
fn to_string(&self) -> String {
match self {
Network::Ethereum => "ethereum".to_string(),
Network::Holesky => "holesky".to_string(),
Network::TaikoA7 => "taiko_a7".to_string(),
Network::TaikoMainnet => "taiko_mainnet".to_string(),
}
impl std::fmt::Display for Network {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(match self {
Network::Ethereum => "ethereum",
Network::Holesky => "holesky",
Network::TaikoA7 => "taiko_a7",
Network::TaikoMainnet => "taiko_mainnet",
})
}
}

Expand Down
4 changes: 1 addition & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ fmt:
@cargo fmt --all --check

clippy:
@rustup toolchain add nightly-2024-04-18
@cargo +nightly-2024-04-18 check --features "sgx,sp1,risc0"
@cargo +nightly-2024-04-18 clippy --workspace --features "sgx,sp1,risc0" --all-targets -- -D warnings
./script/clippy.sh $(TARGET)
52 changes: 52 additions & 0 deletions script/clippy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bash

# Any error will result in failure
set -e

TOOLCHAIN_RISC0=+nightly-2024-04-18
TOOLCHAIN_SP1=+nightly-2024-04-18
TOOLCHAIN_SGX=+nightly-2024-04-18

check_toolchain() {
local TOOLCHAIN=$1

# Remove the plus sign from the toolchain name
TOOLCHAIN=${TOOLCHAIN#+}

# Function to check if the toolchain is installed
exist() {
rustup toolchain list | grep "$TOOLCHAIN" >/dev/null
}

# Main script logic
if exist; then
echo "Toolchain $TOOLCHAIN exists"
else
echo "Installing Rust toolchain: $TOOLCHAIN"
rustup install "$TOOLCHAIN"
fi
}

# NATIVE
if [ -z "$1" ] || [ "$1" == "native" ]; then
cargo clippy -- -D warnings
fi

# SGX
if [ -z "$1" ] || [ "$1" == "sgx" ]; then
check_toolchain $TOOLCHAIN_SGX
cargo ${TOOLCHAIN_SGX} clippy -p raiko-host -p sgx-prover -F "sgx enable" -- -D warnings
fi

# SP1
if [ -z "$1" ] || [ "$1" == "sp1" ]; then
check_toolchain $TOOLCHAIN_SP1
cargo ${TOOLCHAIN_SP1} clippy -p raiko-host -p sp1-builder -p sp1-driver -F "sp1 enable"
fi

# RISC0
if [ -z "$1" ] || [ "$1" == "risc0" ]; then
check_toolchain $TOOLCHAIN_RISC0
./script/setup-bonsai.sh
cargo ${TOOLCHAIN_RISC0} clippy -p raiko-host -p risc0-builder -p risc0-driver -F "risc0 enable"
fi

0 comments on commit 3fc369a

Please sign in to comment.