diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f033e78..4dcc93c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: run: cargo fmt --all -- --check - name: Clippy - run: cargo clippy --workspace --all-targets -- -D warnings + run: cargo clippy --workspace --all-targets -- -D warnings -D clippy::unwrap_used -D clippy::expect_used -D clippy::panic -D clippy::float_cmp - name: Tests run: cargo test --workspace --all-targets diff --git a/Cargo.toml b/Cargo.toml index b7a77b3..67aaa81 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,9 @@ version = "0.1.0" edition = "2021" license = "Apache-2.0" repository = "https://github.com//DiscOS" + +[workspace.lints.clippy] +unwrap_used = "deny" +expect_used = "deny" +panic = "deny" +float_cmp = "deny" diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 0000000..7bada54 --- /dev/null +++ b/clippy.toml @@ -0,0 +1,3 @@ +allow-unwrap-in-tests = true +allow-expect-in-tests = true +allow-panic-in-tests = true diff --git a/crates/discos-core/src/boundary.rs b/crates/discos-core/src/boundary.rs index b22eb7e..692afa1 100644 --- a/crates/discos-core/src/boundary.rs +++ b/crates/discos-core/src/boundary.rs @@ -233,11 +233,9 @@ pub async fn attacker_ternary_evidenceos( let a2 = oracles.accuracy_oracle(x2).await?; q += 2; - if a1.is_none() || a2.is_none() { + let (Some(a1), Some(a2)) = (a1, a2) else { break; - } - let a1 = a1.unwrap(); - let a2 = a2.unwrap(); + }; if a1 < a2 { lo = x1; @@ -268,6 +266,7 @@ pub async fn attacker_ternary_evidenceos( } #[cfg(test)] +#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] mod tests { use super::*; @@ -314,6 +313,67 @@ mod tests { assert!(accuracy_value_det(dbg.x_submit, b) > 0.999); } + #[tokio::test] + async fn attacker_ternary_evidenceos_handles_oracle_none_without_panic() -> anyhow::Result<()> { + #[derive(Debug)] + struct NoneAccuracyOracle; + + #[async_trait] + impl BudgetedBoundaryOracles for NoneAccuracyOracle { + async fn accuracy_oracle(&mut self, _x: f64) -> anyhow::Result> { + Ok(None) + } + + async fn safety_oracle(&mut self, _x: f64) -> anyhow::Result> { + Ok(None) + } + + fn num_buckets(&self) -> u32 { + 256 + } + + fn bits_per_acc_query(&self) -> f64 { + 8.0 + } + + fn joint_bits_budget(&self) -> f64 { + 128.0 + } + + fn bits_spent(&self) -> f64 { + 0.0 + } + + fn frozen(&self) -> bool { + false + } + + fn acc_queries(&self) -> u64 { + 0 + } + + fn safe_queries(&self) -> u64 { + 0 + } + } + + let mut none_acc = NoneAccuracyOracle; + let dbg = attacker_ternary_evidenceos(&mut none_acc, 60, 2e-4, 0.999).await; + assert!(dbg.is_ok()); + if let Ok(dbg) = dbg { + assert!(dbg.safety_response.is_none()); + } + + let mut exhausted = LocalEvidenceOsBoundaryOracles::new(0.42, 256, 0.0)?; + let dbg = attacker_ternary_evidenceos(&mut exhausted, 60, 2e-4, 0.999).await; + assert!(dbg.is_ok()); + if let Ok(dbg) = dbg { + assert!(dbg.safety_response.is_none()); + assert!(dbg.frozen); + } + + Ok(()) + } #[tokio::test] async fn ternary_evidenceos_runs() { let b = 0.42; diff --git a/crates/evidenceos-protocol/build.rs b/crates/evidenceos-protocol/build.rs index 051e878..bc00231 100644 --- a/crates/evidenceos-protocol/build.rs +++ b/crates/evidenceos-protocol/build.rs @@ -1,12 +1,11 @@ -fn main() { - let protoc = protoc_bin_vendored::protoc_bin_path().expect("vendored protoc"); +fn main() -> Result<(), Box> { + let protoc = protoc_bin_vendored::protoc_bin_path()?; std::env::set_var("PROTOC", protoc); + let out_dir = std::env::var("OUT_DIR")?; tonic_build::configure() - .file_descriptor_set_path( - std::path::PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR")) - .join("evidenceos_descriptor.bin"), - ) - .compile_protos(&["proto/evidenceos.proto"], &["proto"]) - .expect("compile protos"); + .file_descriptor_set_path(std::path::PathBuf::from(out_dir).join("evidenceos_descriptor.bin")) + .compile_protos(&["proto/evidenceos.proto"], &["proto"])?; + + Ok(()) }