Skip to content

Commit

Permalink
Get rid of anyhow in receipt_from_proof
Browse files Browse the repository at this point in the history
  • Loading branch information
kpp committed Feb 8, 2025
1 parent a8f6e5d commit 2ee034c
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ serde_json = { version = "1", default-features = false, features = ["alloc"] }
sha2 = { version = "0.10.8", default-features = false, features = ["asm"] }
schemars = { version = "0.8.16", features = ["derive"] }
secp256k1 = { version = "0.29.0", default-features = false, features = ["global-context", "recovery"] }
thiserror = "1.0.50"
thiserror = { version = "1.0.50", default-features = false }
tracing = { version = "0.1.41", default-features = false, features = ["attributes"] }
tracing-subscriber = { version = "0.3.19", features = ["env-filter", "json", "fmt"] }
toml = "0.8.0"
Expand Down
4 changes: 2 additions & 2 deletions crates/risc0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ bincode = { workspace = true }
borsh = { workspace = true }
hex = { workspace = true }
metrics = { workspace = true, optional = true }
risc0-zkp = { workspace = true, optional = true }
risc0-zkp = { workspace = true }
risc0-zkvm = { workspace = true, default-features = false, features = ["std"] }
serde = { workspace = true }
sov-db = { path = "../sovereign-sdk/full-node/db/sov-db", optional = true }
sov-rollup-interface = { path = "../sovereign-sdk/rollup-interface" }
thiserror = { workspace = true }
tracing = { workspace = true }

[features]
Expand All @@ -32,7 +33,6 @@ native = [
"dep:metrics",
"risc0-zkvm/bonsai",
"risc0-zkvm/client",
"risc0-zkp",
"sov-rollup-interface/native",
]
bench = ["native"]
37 changes: 26 additions & 11 deletions crates/risc0/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
//!
//! This crate contains an adapter allowing the Risc0 to be used as a proof system for
//! Sovereign SDK rollups.
use anyhow::bail;
use risc0_zkp::verify::VerificationError;
pub use risc0_zkvm::sha::Digest;
use risc0_zkvm::{InnerReceipt, Receipt};
use risc0_zkvm::{InnerReceipt, PrunedValueError, Receipt};
use serde::{Deserialize, Serialize};
use sov_rollup_interface::zk::Matches;
use thiserror::Error;

pub mod guest;
#[cfg(feature = "native")]
Expand Down Expand Up @@ -60,30 +61,44 @@ impl From<[u32; 8]> for Risc0MethodId {
}
}

#[derive(Error, Debug)]
enum RestoreReceiptErr {
#[error("Failed deserialize output")]
Deserialize(#[from] bincode::Error),
#[error("Failed to extract claim")]
ClaimError(#[source] VerificationError),
#[error("Claim is pruned")]
ClaimPruned(#[source] PrunedValueError),
#[error("Output is pruned")]
OutputPruned(#[source] PrunedValueError),
#[error("Output is empty")]
OutputEmpty,
#[error("Journal is pruned")]
JournalPruned(#[source] PrunedValueError),
}

/// Try to restore Receipt from InnerReceipt from attached journal
fn receipt_from_inner(inner: InnerReceipt) -> anyhow::Result<Receipt> {
let mb_claim = inner.claim().or_else(|_| bail!("Claim is empty"))?;
let claim = mb_claim
.value()
.or_else(|_| bail!("Claim content is empty"))?;
fn receipt_from_inner(inner: InnerReceipt) -> Result<Receipt, RestoreReceiptErr> {
let mb_claim = inner.claim().map_err(RestoreReceiptErr::ClaimError)?;
let claim = mb_claim.value().map_err(RestoreReceiptErr::ClaimPruned)?;
let output = claim
.output
.value()
.or_else(|_| bail!("Output content is empty"))?;
.map_err(RestoreReceiptErr::OutputPruned)?;
let Some(output) = output else {
bail!("Output body is empty");
return Err(RestoreReceiptErr::OutputEmpty)?;
};
let journal = output
.journal
.value()
.or_else(|_| bail!("Journal content is empty"))?;
.map_err(RestoreReceiptErr::JournalPruned)?;
Ok(Receipt::new(inner, journal))
}

/// Parse Receipt from serialized proof (based on proof format)
/// 1. Try to parse proof as InnerReceipt and restore Receipt from it
/// 2. Otherwise try to parse proof as Receipt
pub(crate) fn receipt_from_proof(serialized_proof: &[u8]) -> anyhow::Result<Receipt> {
pub(crate) fn receipt_from_proof(serialized_proof: &[u8]) -> Result<Receipt, RestoreReceiptErr> {
match bincode::deserialize::<InnerReceipt>(serialized_proof) {
Ok(inner) => {
// PostFork2 uses InnerReceipt
Expand Down
2 changes: 2 additions & 0 deletions guests/risc0/batch-proof/bitcoin/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions guests/risc0/batch-proof/mock/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions guests/risc0/light-client-proof/bitcoin/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions guests/risc0/light-client-proof/mock/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2ee034c

Please sign in to comment.