Skip to content
Merged
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
2 changes: 1 addition & 1 deletion batcher/Cargo.lock

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

35 changes: 35 additions & 0 deletions batcher/aligned-sdk/src/core/errors.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,7 @@ pub enum AlignedError {
NonceError(NonceError),
ChainIdError(ChainIdError),
MaxFeeEstimateError(MaxFeeEstimateError),
FileError(FileError),
}

impl From<SubmitError> for AlignedError {
Expand DownExpand Up@@ -48,6 +49,12 @@ impl From<MaxFeeEstimateError> for AlignedError {
}
}

impl From<FileError> for AlignedError {
fn from(e: FileError) -> Self {
AlignedError::FileError(e)
}
}

impl fmt::Display for AlignedError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expand All@@ -56,6 +63,7 @@ impl fmt::Display for AlignedError {
AlignedError::NonceError(e) => write!(f, "Nonce error: {}", e),
AlignedError::ChainIdError(e) => write!(f, "Chain ID error: {}", e),
AlignedError::MaxFeeEstimateError(e) => write!(f, "Max fee estimate error: {}", e),
AlignedError::FileError(e) => write!(f, "File error: {}", e),
}
}
}
Expand DownExpand Up@@ -321,3 +329,30 @@ pub enum BalanceError {
EthereumProviderError(String),
EthereumCallError(String),
}

#[derive(Debug)]
pub enum FileError {
IoError(PathBuf, io::Error),
SerializationError(SerializationError),
}

impl From<SerializationError> for FileError {
fn from(e: SerializationError) -> Self {
FileError::SerializationError(e)
}
}

impl From<io::Error> for FileError {
fn from(e: io::Error) -> Self {
FileError::IoError(PathBuf::new(), e)
}
}

impl fmt::Display for FileError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
FileError::IoError(path, e) => write!(f, "IO error: {}: {}", path.display(), e),
FileError::SerializationError(e) => write!(f, "Serialization error: {}", e),
}
}
}
93 changes: 92 additions & 1 deletion batcher/aligned-sdk/src/sdk.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@ use crate::{
batch::await_batch_verification,
messaging::{receive, send_messages, ResponseStream},
protocol::check_protocol_version,
serialization::cbor_serialize,
},
core::{
constants::{
Expand DownExpand Up@@ -34,13 +35,18 @@ use std::{str::FromStr, sync::Arc};
use tokio::{net::TcpStream, sync::Mutex};
use tokio_tungstenite::{connect_async, tungstenite::Message, MaybeTlsStream, WebSocketStream};

use log::debug;
use log::{debug, info};

use futures_util::{
stream::{SplitSink, SplitStream},
StreamExt, TryStreamExt,
};

use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

use serde_json::json;
/// Submits multiple proofs to the batcher to be verified in Aligned and waits for the verification on-chain.
/// # Arguments
/// * `batcher_url` - The url of the batcher to which the proof will be submitted.
Expand DownExpand Up@@ -655,6 +661,91 @@ pub async fn get_balance_in_aligned(
}
}

/// Saves AlignedVerificationData in a file.
/// # Arguments
/// * `batch_inclusion_data_directory_path` - The path of the directory where the data will be saved.
/// * `aligned_verification_data` - The aligned verification data to be saved.
/// # Returns
/// * Ok if the data is saved successfully.
/// # Errors
/// * `FileError` if there is an error writing the data to the file.
pub fn save_response(
batch_inclusion_data_directory_path: PathBuf,
aligned_verification_data: &AlignedVerificationData,
) -> Result<(), errors::FileError> {
save_response_cbor(
batch_inclusion_data_directory_path.clone(),
&aligned_verification_data.clone(),
)?;
save_response_json(
batch_inclusion_data_directory_path,
aligned_verification_data,
)
}
fn save_response_cbor(
batch_inclusion_data_directory_path: PathBuf,
aligned_verification_data: &AlignedVerificationData,
) -> Result<(), errors::FileError> {
let batch_merkle_root = &hex::encode(aligned_verification_data.batch_merkle_root)[..8];
let batch_inclusion_data_file_name = batch_merkle_root.to_owned()
+ "_"
+ &aligned_verification_data.index_in_batch.to_string()
+ ".cbor";

let batch_inclusion_data_path =
batch_inclusion_data_directory_path.join(batch_inclusion_data_file_name);

let data = cbor_serialize(&aligned_verification_data)?;

let mut file = File::create(&batch_inclusion_data_path)?;
file.write_all(data.as_slice())?;
info!(
"Batch inclusion data written into {}",
batch_inclusion_data_path.display()
);

Ok(())
}
fn save_response_json(
batch_inclusion_data_directory_path: PathBuf,
aligned_verification_data: &AlignedVerificationData,
) -> Result<(), errors::FileError> {
let batch_merkle_root = &hex::encode(aligned_verification_data.batch_merkle_root)[..8];
let batch_inclusion_data_file_name = batch_merkle_root.to_owned()
+ "_"
+ &aligned_verification_data.index_in_batch.to_string()
+ ".json";

let batch_inclusion_data_path =
batch_inclusion_data_directory_path.join(batch_inclusion_data_file_name);

let merkle_proof = aligned_verification_data
.batch_inclusion_proof
.merkle_path
.iter()
.map(hex::encode)
.collect::<Vec<String>>()
.join("");
let data = json!({
"proof_commitment": hex::encode(aligned_verification_data.verification_data_commitment.proof_commitment),
"pub_input_commitment": hex::encode(aligned_verification_data.verification_data_commitment.pub_input_commitment),
"program_id_commitment": hex::encode(aligned_verification_data.verification_data_commitment.proving_system_aux_data_commitment),
"proof_generator_addr": hex::encode(aligned_verification_data.verification_data_commitment.proof_generator_addr),
"batch_merkle_root": hex::encode(aligned_verification_data.batch_merkle_root),
"verification_data_batch_index": aligned_verification_data.index_in_batch,
"merkle_proof": merkle_proof,
});
let mut file = File::create(&batch_inclusion_data_path)?;
file.write_all(serde_json::to_string_pretty(&data).unwrap().as_bytes())?;

info!(
"Batch inclusion data written into {}",
batch_inclusion_data_path.display()
);

Ok(())
}

#[cfg(test)]
mod test {
//Public constants for convenience
Expand Down
30 changes: 1 addition & 29 deletions batcher/aligned/src/main.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,15 +7,14 @@ use std::path::PathBuf;
use std::str::FromStr;

use aligned_sdk::communication::serialization::cbor_deserialize;
use aligned_sdk::communication::serialization::cbor_serialize;
use aligned_sdk::core::{
errors::{AlignedError, SubmitError},
types::{AlignedVerificationData, Network, ProvingSystemId, VerificationData},
};
use aligned_sdk::sdk::get_chain_id;
use aligned_sdk::sdk::get_next_nonce;
use aligned_sdk::sdk::{deposit_to_aligned, get_balance_in_aligned};
use aligned_sdk::sdk::{get_vk_commitment, is_proof_verified, submit_multiple};
use aligned_sdk::sdk::{get_vk_commitment, is_proof_verified, save_response, submit_multiple};
use clap::Parser;
use clap::Subcommand;
use clap::ValueEnum;
Expand DownExpand Up@@ -610,33 +609,6 @@ async fn get_nonce(
Ok(nonce)
}

fn save_response(
batch_inclusion_data_directory_path: PathBuf,
aligned_verification_data: &AlignedVerificationData,
) -> Result<(), SubmitError> {
let batch_merkle_root = &hex::encode(aligned_verification_data.batch_merkle_root)[..8];
let batch_inclusion_data_file_name = batch_merkle_root.to_owned()
+ "_"
+ &aligned_verification_data.index_in_batch.to_string()
+ ".json";

let batch_inclusion_data_path =
batch_inclusion_data_directory_path.join(batch_inclusion_data_file_name);

let data = cbor_serialize(&aligned_verification_data)?;

let mut file = File::create(&batch_inclusion_data_path)
.map_err(|e| SubmitError::IoError(batch_inclusion_data_path.clone(), e))?;
file.write_all(data.as_slice())
.map_err(|e| SubmitError::IoError(batch_inclusion_data_path.clone(), e))?;
info!(
"Batch inclusion data written into {}",
batch_inclusion_data_path.display()
);

Ok(())
}

pub async fn get_user_balance(
provider: Provider<Http>,
contract_address: Address,
Expand Down
2 changes: 1 addition & 1 deletion explorer/lib/explorer_web/live/utils.ex
Original file line numberDiff line numberDiff line change
Expand Up@@ -186,7 +186,7 @@ defmodule Utils do
def calculate_proof_hashes(deserialized_batch) do
deserialized_batch
|> Enum.map(fn s3_object ->
:crypto.hash(:sha3_256, s3_object["proof"])
ExKeccak.hash_256(:erlang.list_to_binary(s3_object["proof"]))
end)
end

Expand Down
3 changes: 2 additions & 1 deletion telemetry_api/mix.exs
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,7 +57,8 @@ defmodule TelemetryApi.MixProject do
{:ethers, "~> 0.4.4"},
{:opentelemetry, "~> 1.3"},
{:opentelemetry_api, "~> 1.2"},
{:opentelemetry_exporter, "~> 1.6"}
{:opentelemetry_exporter, "~> 1.6"},
{:ex_keccak, "~> 0.7.5"}
]
end

Expand Down