Skip to content

Commit

Permalink
Merge tag 'v0.6.0' into update_to_0_6
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielbosio committed Sep 12, 2024
2 parents a0c893e + bdda0d2 commit c43d80f
Show file tree
Hide file tree
Showing 25 changed files with 324 additions and 124 deletions.
2 changes: 1 addition & 1 deletion batcher/aligned-sdk/abi/AlignedLayerServiceManager.json

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions batcher/aligned-sdk/src/communication/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ pub async fn await_batch_verification(
aligned_verification_data: &AlignedVerificationData,
rpc_url: &str,
chain: Chain,
payment_service_addr: &str,
) -> Result<(), errors::SubmitError> {
for _ in 0..RETRIES {
if is_proof_verified(aligned_verification_data, chain.clone(), rpc_url)
.await
.is_ok_and(|r| r)
if is_proof_verified(
aligned_verification_data,
chain.clone(),
rpc_url,
payment_service_addr,
)
.await
.is_ok_and(|r| r)
{
return Ok(());
}
Expand Down
39 changes: 36 additions & 3 deletions batcher/aligned-sdk/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use futures_util::{
/// * `verification_data` - An array of verification data of each proof.
/// * `wallet` - The wallet used to sign the proof.
/// * `nonce` - The nonce of the submitter address. See `get_next_nonce`.
/// * `payment_service_addr` - The address of the payment service contract.
/// # Returns
/// * An array of aligned verification data obtained when submitting the proof.
/// # Errors
Expand All @@ -66,13 +67,19 @@ pub async fn submit_multiple_and_wait_verification(
verification_data: &[VerificationData],
wallet: Wallet<SigningKey>,
nonce: U256,
payment_service_addr: &str,
) -> Result<Vec<AlignedVerificationData>, errors::SubmitError> {
let aligned_verification_data =
submit_multiple(batcher_url, verification_data, wallet, nonce).await?;

for aligned_verification_data_item in aligned_verification_data.iter() {
await_batch_verification(aligned_verification_data_item, eth_rpc_url, chain.clone())
.await?;
await_batch_verification(
aligned_verification_data_item,
eth_rpc_url,
chain.clone(),
payment_service_addr,
)
.await?;
}

Ok(aligned_verification_data)
Expand Down Expand Up @@ -182,6 +189,7 @@ async fn _submit_multiple(
/// * `verification_data` - The verification data of the proof.
/// * `wallet` - The wallet used to sign the proof.
/// * `nonce` - The nonce of the submitter address. See `get_next_nonce`.
/// * `payment_service_addr` - The address of the payment service contract.
/// # Returns
/// * The aligned verification data obtained when submitting the proof.
/// # Errors
Expand All @@ -208,6 +216,7 @@ pub async fn submit_and_wait_verification(
verification_data: &VerificationData,
wallet: Wallet<SigningKey>,
nonce: U256,
payment_service_addr: &str,
) -> Result<AlignedVerificationData, errors::SubmitError> {
let verification_data = vec![verification_data.clone()];

Expand All @@ -218,6 +227,7 @@ pub async fn submit_and_wait_verification(
&verification_data,
wallet,
nonce,
payment_service_addr,
)
.await?;

Expand Down Expand Up @@ -265,6 +275,7 @@ pub async fn submit(
/// * `aligned_verification_data` - The aligned verification data obtained when submitting the proofs.
/// * `chain` - The chain on which the verification will be done.
/// * `eth_rpc_url` - The URL of the Ethereum RPC node.
/// * `payment_service_addr` - The address of the payment service.
/// # Returns
/// * A boolean indicating whether the proof was verified on-chain and is included in the batch.
/// # Errors
Expand All @@ -275,18 +286,27 @@ pub async fn is_proof_verified(
aligned_verification_data: &AlignedVerificationData,
chain: Chain,
eth_rpc_url: &str,
payment_service_addr: &str,
) -> Result<bool, errors::VerificationError> {
let eth_rpc_provider =
Provider::<Http>::try_from(eth_rpc_url).map_err(|e: url::ParseError| {
errors::VerificationError::EthereumProviderError(e.to_string())
})?;
_is_proof_verified(aligned_verification_data, chain, eth_rpc_provider).await

_is_proof_verified(
aligned_verification_data,
chain,
eth_rpc_provider,
payment_service_addr,
)
.await
}

async fn _is_proof_verified(
aligned_verification_data: &AlignedVerificationData,
chain: Chain,
eth_rpc_provider: Provider<Http>,
payment_service_addr: &str,
) -> Result<bool, errors::VerificationError> {
let contract_address = match chain {
Chain::Devnet => "0x1613beB3B2C4f22Ee086B2b38C1476A3cE7f78E8",
Expand All @@ -295,6 +315,10 @@ async fn _is_proof_verified(
Chain::HoleskyStage => "0x9C5231FC88059C086Ea95712d105A2026048c39B",
};

let payment_service_addr = payment_service_addr
.parse::<Address>()
.map_err(|e| errors::VerificationError::HexDecodingError(e.to_string()))?;

// All the elements from the merkle proof have to be concatenated
let merkle_proof: Vec<u8> = aligned_verification_data
.batch_inclusion_proof
Expand All @@ -318,6 +342,7 @@ async fn _is_proof_verified(
aligned_verification_data.batch_merkle_root,
merkle_proof.into(),
aligned_verification_data.index_in_batch.into(),
payment_service_addr,
);

let result = call
Expand Down Expand Up @@ -406,6 +431,8 @@ mod test {

use ethers::signers::LocalWallet;

const BATCHER_PAYMENT_SERVICE_ADDR: &str = "0x7969c5eD335650692Bc04293B07F5BF2e7A673C0";

#[tokio::test]
async fn test_submit_success() {
let base_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Expand Down Expand Up @@ -439,6 +466,7 @@ mod test {
&verification_data,
wallet,
U256::zero(),
BATCHER_PAYMENT_SERVICE_ADDR,
)
.await
.unwrap();
Expand Down Expand Up @@ -472,6 +500,7 @@ mod test {
&verification_data,
wallet,
U256::zero(),
BATCHER_PAYMENT_SERVICE_ADDR,
)
.await;

Expand Down Expand Up @@ -513,6 +542,7 @@ mod test {
&verification_data,
wallet,
U256::zero(),
BATCHER_PAYMENT_SERVICE_ADDR,
)
.await
.unwrap();
Expand All @@ -523,6 +553,7 @@ mod test {
&aligned_verification_data[0],
Chain::Devnet,
"http://localhost:8545",
BATCHER_PAYMENT_SERVICE_ADDR,
)
.await
.unwrap();
Expand Down Expand Up @@ -563,6 +594,7 @@ mod test {
&verification_data,
wallet,
U256::zero(),
BATCHER_PAYMENT_SERVICE_ADDR,
)
.await
.unwrap();
Expand All @@ -578,6 +610,7 @@ mod test {
&aligned_verification_data_modified,
Chain::Devnet,
"http://localhost:8545",
BATCHER_PAYMENT_SERVICE_ADDR,
)
.await
.unwrap();
Expand Down
7 changes: 7 additions & 0 deletions batcher/aligned/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ pub struct VerifyProofOnchainArgs {
default_value = "devnet"
)]
chain: ChainArg,
#[arg(
name = "Batcher Payment Service Eth Address",
long = "payment_service_addr",
default_value = "0x7969c5eD335650692Bc04293B07F5BF2e7A673C0"
)]
payment_service_addr: String,
}

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -366,6 +372,7 @@ async fn main() -> Result<(), AlignedError> {
&aligned_verification_data,
chain,
&verify_inclusion_args.eth_rpc_url,
&verify_inclusion_args.payment_service_addr,
)
.await?;

Expand Down
6 changes: 6 additions & 0 deletions docs/guides/1_SDK.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub async fn submit_and_wait_verification(
verification_data: &VerificationData,
wallet: Wallet<SigningKey>,
nonce: U256,
payment_service_addr: &str,
) -> Result<AlignedVerificationData, errors::SubmitError>
```

Expand All @@ -125,6 +126,7 @@ pub async fn submit_and_wait_verification(
- `verification_data` - The verification data for the proof.
- `wallet` - The wallet used to sign the proof. Should be using correct chain id. See `get_chain_id`.
- `nonce` - The nonce of the submitter address. See `get_next_nonce`.
- `payment_service_addr` - The address of the batcher payment service contract.

#### Returns

Expand Down Expand Up @@ -162,6 +164,7 @@ pub async fn submit_multiple_and_wait_verification(
verification_data: &[VerificationData],
wallet: Wallet<SigningKey>,
nonce: U256,
payment_service_addr: &str,
) -> Result<Vec<AlignedVerificationData>, errors::SubmitError>
```

Expand All @@ -173,6 +176,7 @@ pub async fn submit_multiple_and_wait_verification(
- `verification_data` - A verification data array.
- `wallet` - The wallet used to sign the proof. Should be using correct chain id. See `get_chain_id`.
- `nonce` - The nonce of the submitter address. See `get_next_nonce`.
- `payment_service_addr` - The address of the batcher payment service contract.

#### Returns

Expand Down Expand Up @@ -206,6 +210,7 @@ pub async fn is_proof_verified(
aligned_verification_data: AlignedVerificationData,
chain: Chain,
eth_rpc_url: &str,
payment_service_addr: &str,
) -> Result<bool, errors::VerificationError>
```

Expand All @@ -214,6 +219,7 @@ pub async fn is_proof_verified(
- `aligned_verification_data` - The aligned verification data obtained when submitting the proofs.
- `chain` - The chain on which the verification will be done.
- `eth_rpc_url` - The URL of the Ethereum RPC node.
- `payment_service_addr` - The address of the batcher payment service contract.

#### Returns

Expand Down
66 changes: 26 additions & 40 deletions docs/guides/2_integrating_aligned_into_your_application.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ The proof submission and verification can be done either with the SDK or by usin

#### Using the SDK

To submit a proof using the SDK, you can use the `submit` function, and then you can use the `verify_proof_onchain` function to check if the proof was correctly verified in Aligned.

To submit a proof using the SDK, you can use the `submit_and_wait_verification` function.
This function submits the proof to aligned and waits for it to be verified in Aligned.
Alternatively you can call `submit` if you dont want to wait for proof verification.
The following code is an example of how to submit a proof using the SDK:

```rust
use aligned_sdk::sdk::{submit, get_next_nonce};
use aligned_sdk::sdk::{submit_and_wait_verification, get_next_nonce};
use aligned_sdk::types::{ProvingSystemId, VerificationData};
use ethers::prelude::*;

Expand All @@ -147,13 +148,33 @@ async fn submit_proof_to_aligned(
.await
.map_err(|e| anyhow::anyhow!("Failed to get next nonce: {:?}", e))?;

submit(BATCHER_URL, &verification_data, wallet, nonce).await
.map_err(|e| anyhow::anyhow!("Failed to submit proof: {:?}", e))
match submit_and_wait_verification(
BATCHER_URL,
&rpc_url,
Chain::Holesky,
&verification_data,
wallet.clone(),
nonce,
BATCHER_PAYMENTS_ADDRESS
)

submit_and_wait_verification(
BATCHER_URL,
RPC_URL,
Chain::Holesky,
&verification_data,
wallet,
nonce,
BATCHER_CONTRACT_ADDRESS
).await.map_err(|e| anyhow::anyhow!("Failed to submit proof: {:?}", e))
}

#[tokio::main]
async fn main() {
let wallet = // Initialize wallet

let wallet = wallet.with_chain_id(17000u64)

let proof = // Generate or obtain proof

match submit_proof_to_aligned(proof, wallet).await {
Expand All @@ -163,41 +184,6 @@ async fn main() {
}
```

The following code is an example of how to verify the proof was correctly verified in Aligned using the SDK:

```rust
use aligned_sdk::sdk::verify_proof_onchain;
use aligned_sdk::types::{AlignedVerificationData, Chain};
use ethers::prelude::*;
use tokio::time::{sleep, Duration};

async fn wait_for_proof_verification(
aligned_verification_data: AlignedVerificationData,
rpc_url: String,
) -> Result<(), anyhow::Error> {
for _ in 0..10 {
if verify_proof_onchain(aligned_verification_data.clone(), Chain::Holesky, rpc_url.as_str()).await.is_ok_and(|r| r) {
println!("Proof verified successfully.");
return Ok(());
}
println!("Proof not verified yet. Waiting 10 seconds before checking again...");
sleep(Duration::from_secs(10)).await;
}
anyhow::bail!("Proof verification failed")
}

#[tokio::main]
async fn main() {
let aligned_verification_data = // Obtain aligned verification data
let rpc_url = "https://ethereum-holesky-rpc.publicnode.com".to_string();

match wait_for_proof_verification(aligned_verification_data, rpc_url).await {
Ok(_) => println!("Proof verified"),
Err(err) => println!("Error: {:?}", err),
}
}
```

You can find an example of the proof submission and verification in the [ZKQuiz Program](../../examples/zkquiz/quiz/script/src/main.rs).

This example generates a proof, instantiates a wallet to submit the proof, and then submits the proof to Aligned for verification. It then waits for the proof to be verified in Aligned.
Expand Down
Loading

0 comments on commit c43d80f

Please sign in to comment.