Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"
]
108 changes: 21 additions & 87 deletions contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,40 @@

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;

Comment on lines -20 to -25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should remove this, because it might break downstream users and I think this is actually still helpful.

// 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;
pub mod selector;
}
}

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.
Expand Down Expand Up @@ -109,42 +82,3 @@ pub fn encode_seal(receipt: &risc0_zkvm::Receipt) -> Result<Vec<u8>> {
};
Ok(seal)
}

#[cfg(not(target_os = "zkvm"))]
fn decode_contract_err<T: SolInterface>(err: alloy::contract::Error) -> Result<T, Error> {
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,
}
}
}
11 changes: 7 additions & 4 deletions contracts/src/set_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<IRiscZeroSetVerifierErrors>() {
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))
Expand Down
Loading