diff --git a/contracts/Cargo.toml b/contracts/Cargo.toml index b0362c05..357afeb7 100644 --- a/contracts/Cargo.toml +++ b/contracts/Cargo.toml @@ -15,7 +15,7 @@ rustdoc-args = ["--cfg", "docsrs"] anyhow = "1.0" [dependencies] -alloy = { workspace = true, features = ["contract"] } +alloy = { workspace = true, optional = true, features = ["essentials"] } alloy-primitives = { workspace = true, optional = true } alloy-sol-types = { workspace = true } anyhow = { workspace = true } @@ -30,17 +30,17 @@ tracing = { workspace = true } [dev-dependencies] hex = { workspace = true } regex = "1.10" -tokio = { workspace = true, features = ["macros", "rt"] } [lib] doctest = false [features] default = [] +service = ["dep:alloy"] unstable = [ - "dep:alloy-primitives", - "dep:hex", - "dep:risc0-aggregation", + "dep:alloy-primitives", + "dep:hex", + "dep:risc0-aggregation", "dep:serde", "risc0-aggregation/verify" ] diff --git a/contracts/src/lib.rs b/contracts/src/lib.rs index f8416643..25903ad1 100644 --- a/contracts/src/lib.rs +++ b/contracts/src/lib.rs @@ -17,15 +17,10 @@ pub mod groth16; -/// Re-export of [alloy], provided to ensure that the correct version of the types used in the -/// public API are available in case multiple versions of [alloy] are in use. -/// -/// Because [alloy] is a v0.x crate, it is not covered under the semver policy of this crate. -pub use alloy; - // NOTE: Placing the cfg directly on the `pub mod` statement doesn't work when tried with Rust 1.81 cfg_if::cfg_if! { if #[cfg(feature = "unstable")] { + #[cfg(feature = "service")] pub mod set_verifier; pub mod event_query; pub mod receipt; @@ -33,51 +28,29 @@ cfg_if::cfg_if! { } } -use core::str::FromStr; - use anyhow::{bail, Result}; use risc0_zkvm::{sha::Digestible, InnerReceipt}; -#[cfg(not(target_os = "zkvm"))] -use alloy::{primitives::Bytes, sol_types::SolInterface, transports::TransportError}; - -alloy::sol!( - #![sol(rpc, all_derives)] - "src/IRiscZeroVerifier.sol" -); - -alloy::sol!( - #![sol(rpc, all_derives)] - "src/IRiscZeroSetVerifier.sol" -); - -#[cfg(not(target_os = "zkvm"))] -pub use IRiscZeroSetVerifier::IRiscZeroSetVerifierErrors; - -#[cfg(not(target_os = "zkvm"))] -#[derive(thiserror::Error, Debug)] -pub enum Error { - #[error("SetVerifier error: {0:?}")] - SetVerifierError(IRiscZeroSetVerifierErrors), - - #[error("contract error: {0}")] - ContractError(alloy::contract::Error), - - #[error("decoding error: {0}")] - DecodingError(#[from] DecodingError), -} - -#[cfg(not(target_os = "zkvm"))] -#[derive(thiserror::Error, Debug)] -pub enum DecodingError { - #[error("missing data, code: {0} msg: {1}")] - MissingData(i64, String), - - #[error("error creating bytes from string")] - BytesFromStrError, - - #[error("abi decoder error: {0} - {1}")] - Abi(alloy::sol_types::Error, Bytes), +cfg_if::cfg_if! { + if #[cfg(all(feature = "unstable", feature = "service"))] { + alloy::sol!( + #![sol(rpc, all_derives)] + "src/IRiscZeroVerifier.sol" + ); + alloy::sol!( + #![sol(rpc, all_derives)] + "src/IRiscZeroSetVerifier.sol" + ); + } else { + alloy_sol_types::sol!( + #![sol(all_derives)] + "src/IRiscZeroVerifier.sol" + ); + alloy_sol_types::sol!( + #![sol(all_derives)] + "src/IRiscZeroSetVerifier.sol" + ); + } } /// Encode the seal of the given receipt for use with EVM smart contract verifiers. @@ -109,42 +82,3 @@ pub fn encode_seal(receipt: &risc0_zkvm::Receipt) -> Result> { }; Ok(seal) } - -#[cfg(not(target_os = "zkvm"))] -fn decode_contract_err(err: alloy::contract::Error) -> Result { - match err { - alloy::contract::Error::TransportError(TransportError::ErrorResp(ts_err)) => { - let Some(data) = ts_err.data else { - return Err( - DecodingError::MissingData(ts_err.code, ts_err.message.to_string()).into(), - ); - }; - - let data = data.get().trim_matches('"'); - - let Ok(data) = Bytes::from_str(data) else { - return Err(DecodingError::BytesFromStrError.into()); - }; - - let decoded_error = match T::abi_decode(&data) { - Ok(res) => res, - Err(err) => { - return Err(DecodingError::Abi(err, data).into()); - } - }; - - Ok(decoded_error) - } - _ => Err(Error::ContractError(err)), - } -} - -#[cfg(not(target_os = "zkvm"))] -impl IRiscZeroSetVerifierErrors { - pub fn decode_error(err: alloy::contract::Error) -> Error { - match decode_contract_err(err) { - Ok(res) => Error::SetVerifierError(res), - Err(decode_err) => decode_err, - } - } -} diff --git a/contracts/src/set_verifier.rs b/contracts/src/set_verifier.rs index 93e95b8c..d3581126 100644 --- a/contracts/src/set_verifier.rs +++ b/contracts/src/set_verifier.rs @@ -83,10 +83,13 @@ where pub async fn submit_merkle_root(&self, root: B256, seal: Bytes) -> Result<()> { tracing::debug!("Calling submitMerkleRoot({:?},{:?})", root, seal); let call = self.instance.submitMerkleRoot(root, seal).from(self.caller); - let pending_tx = call - .send() - .await - .map_err(IRiscZeroSetVerifierErrors::decode_error)?; + let pending_tx = match call.send().await { + Ok(tx) => tx, + Err(err) => match err.as_decoded_interface_error::() { + None => bail!(err), + Some(interface_error) => bail!("SetVerifier error: {interface_error:?}"), + }, + }; tracing::debug!("Broadcasting tx {}", pending_tx.tx_hash()); let tx_hash = pending_tx .with_timeout(Some(self.tx_timeout))