Skip to content

Commit 7efb80e

Browse files
committed
Merge branch 'feat/aggregation-mode' of github.com:yetanotherco/aligned_layer into feat/aggregation-mode
2 parents 34a0c55 + 18be091 commit 7efb80e

File tree

4 files changed

+86
-14
lines changed

4 files changed

+86
-14
lines changed

aggregation-mode/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aggregation-mode/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ serde_yaml = "0.9"
1313
tracing = { version = "0.1", features = ["log"] }
1414
tracing-subscriber = { version = "0.3.0", features = ["env-filter"] }
1515
alloy = { version = "0.11", features = ["default", "signer-keystore"] }
16+
c-kzg = "1.0.3"
1617
bincode = "1.3.3"
1718
tokio = { version = "1", features = ["time"]}
1819
sha3 = "0.10.8"

aggregation-mode/src/backend/mod.rs

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ use crate::zk::{
1010
Proof, ZKVMEngine,
1111
};
1212
use alloy::{
13-
network::EthereumWallet,
14-
primitives::Address,
15-
providers::{PendingTransactionError, ProviderBuilder},
16-
rpc::types::TransactionReceipt,
13+
consensus::{Blob, BlobTransactionSidecar},
14+
eips::eip4844::BYTES_PER_BLOB,
15+
hex,
16+
network::{EthereumWallet, TransactionBuilder4844},
17+
primitives::{Address, FixedBytes},
18+
providers::{PendingTransactionError, Provider, ProviderBuilder, WalletProvider},
19+
rpc::types::{TransactionReceipt, TransactionRequest},
1720
signers::local::LocalSigner,
1821
};
1922
use config::Config;
@@ -22,7 +25,9 @@ use merkle_tree::compute_proofs_merkle_root;
2225
use sp1_sdk::HashableKey;
2326
use std::str::FromStr;
2427
use tracing::{error, info, warn};
25-
use types::{AlignedProofAggregationService, AlignedProofAggregationServiceContract};
28+
use types::{
29+
AlignedProofAggregationService, AlignedProofAggregationServiceContract, RPCProviderWithSigner,
30+
};
2631

2732
#[derive(Debug)]
2833
pub enum AggregatedProofSubmissionError {
@@ -35,6 +40,7 @@ pub enum AggregatedProofSubmissionError {
3540

3641
pub struct ProofAggregator {
3742
engine: ZKVMEngine,
43+
rpc_provider: RPCProviderWithSigner,
3844
proof_aggregation_service: AlignedProofAggregationServiceContract,
3945
fetcher: ProofsFetcher,
4046
}
@@ -48,18 +54,19 @@ impl ProofAggregator {
4854
)
4955
.expect("Correct keystore signer");
5056
let wallet = EthereumWallet::from(signer);
51-
let provider = ProviderBuilder::new().wallet(wallet).on_http(rpc_url);
57+
let rpc_provider = ProviderBuilder::new().wallet(wallet).on_http(rpc_url);
5258
let proof_aggregation_service: AlignedProofAggregationService::AlignedProofAggregationServiceInstance<(), alloy::providers::fillers::FillProvider<alloy::providers::fillers::JoinFill<alloy::providers::fillers::JoinFill<alloy::providers::Identity, alloy::providers::fillers::JoinFill<alloy::providers::fillers::GasFiller, alloy::providers::fillers::JoinFill<alloy::providers::fillers::BlobGasFiller, alloy::providers::fillers::JoinFill<alloy::providers::fillers::NonceFiller, alloy::providers::fillers::ChainIdFiller>>>>, alloy::providers::fillers::WalletFiller<EthereumWallet>>, alloy::providers::RootProvider>> = AlignedProofAggregationService::new(
5359
Address::from_str(&config.proof_aggregation_service_address)
5460
.expect("Address to be correct"),
55-
provider,
61+
rpc_provider.clone(),
5662
);
5763
let fetcher = ProofsFetcher::new(config);
5864

5965
Self {
6066
engine: ZKVMEngine::SP1,
6167
proof_aggregation_service,
6268
fetcher,
69+
rpc_provider,
6370
}
6471
}
6572

@@ -98,7 +105,11 @@ impl ProofAggregator {
98105
return Ok(());
99106
}
100107

108+
info!("Proofs fetched, constructing merkle root...");
101109
let (merkle_root, leaves) = compute_proofs_merkle_root(&proofs);
110+
info!("Merkle root constructed: {}", hex::encode(merkle_root));
111+
112+
info!("Starting proof aggregation program...");
102113
let output = match self.engine {
103114
ZKVMEngine::SP1 => {
104115
// only SP1 compressed proofs are supported
@@ -118,14 +129,18 @@ impl ProofAggregator {
118129
.map_err(AggregatedProofSubmissionError::Aggregation)?
119130
}
120131
};
132+
info!("Proof aggregation program finished");
121133

122134
info!("Sending blob transaction...");
123-
let blob_tx_hash = self.send_blob_transaction(leaves).await?;
124-
info!("Blob transaction sent, hash: {:?}", blob_tx_hash);
135+
let blob_receipt = self.send_blob_transaction(leaves).await?;
136+
info!(
137+
"Blob transaction sent, hash: {:?}",
138+
blob_receipt.transaction_hash
139+
);
125140

126141
info!("Sending proof to ProofAggregationService contract...");
127142
let receipt = self
128-
.send_proof_to_verify_on_chain(&blob_tx_hash, output.proof)
143+
.send_proof_to_verify_on_chain(&blob_receipt.transaction_hash, output.proof)
129144
.await?;
130145
info!(
131146
"Proof sent and verified, tx hash {:?}",
@@ -163,12 +178,47 @@ impl ProofAggregator {
163178
}
164179
}
165180

166-
// TODO
167181
async fn send_blob_transaction(
168182
&self,
169-
_leaves: Vec<[u8; 32]>,
170-
) -> Result<[u8; 32], AggregatedProofSubmissionError> {
171-
Ok([0u8; 32])
183+
leaves: Vec<[u8; 32]>,
184+
) -> Result<TransactionReceipt, AggregatedProofSubmissionError> {
185+
let data: Vec<u8> = leaves.iter().flat_map(|arr| arr.iter().copied()).collect();
186+
let mut blob_data: [u8; BYTES_PER_BLOB] = [0u8; BYTES_PER_BLOB];
187+
188+
for (i, byte) in data.iter().enumerate() {
189+
blob_data[i] = *byte;
190+
}
191+
192+
// calculate kzg commitments for blob
193+
let settings = c_kzg::ethereum_kzg_settings();
194+
let blob = c_kzg::Blob::new(blob_data);
195+
let commitment = c_kzg::KzgCommitment::blob_to_kzg_commitment(&blob, settings)
196+
.map_err(|_| AggregatedProofSubmissionError::SendBlobTransaction)?;
197+
let proof =
198+
c_kzg::KzgProof::compute_blob_kzg_proof(&blob, &commitment.to_bytes(), settings)
199+
.map_err(|_| AggregatedProofSubmissionError::SendBlobTransaction)?;
200+
201+
// convert to alloy types
202+
let blob = Blob::from_slice(&blob_data);
203+
let commitment: FixedBytes<48> = FixedBytes::from_slice(commitment.to_bytes().as_slice());
204+
let proof: FixedBytes<48> = FixedBytes::from_slice(proof.to_bytes().as_slice());
205+
206+
let blob_sidecar = BlobTransactionSidecar::new(vec![blob], vec![commitment], vec![proof]);
207+
// send transaction to itself
208+
let to = self.rpc_provider.signer_addresses().collect::<Vec<_>>()[0];
209+
let tx = TransactionRequest::default()
210+
.to(to)
211+
.with_blob_sidecar(blob_sidecar);
212+
213+
let res = self
214+
.rpc_provider
215+
.send_transaction(tx)
216+
.await
217+
.map_err(|_| AggregatedProofSubmissionError::SendBlobTransaction)?;
218+
219+
res.get_receipt()
220+
.await
221+
.map_err(AggregatedProofSubmissionError::ReceiptError)
172222
}
173223

174224
async fn set_aggregated_proof_as_missed(

aggregation-mode/src/backend/types.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,23 @@ pub type RPCProvider = alloy::providers::fillers::FillProvider<
6565
>,
6666
alloy::providers::RootProvider,
6767
>;
68+
69+
pub type RPCProviderWithSigner = alloy::providers::fillers::FillProvider<
70+
alloy::providers::fillers::JoinFill<
71+
alloy::providers::fillers::JoinFill<
72+
alloy::providers::Identity,
73+
alloy::providers::fillers::JoinFill<
74+
alloy::providers::fillers::GasFiller,
75+
alloy::providers::fillers::JoinFill<
76+
alloy::providers::fillers::BlobGasFiller,
77+
alloy::providers::fillers::JoinFill<
78+
alloy::providers::fillers::NonceFiller,
79+
alloy::providers::fillers::ChainIdFiller,
80+
>,
81+
>,
82+
>,
83+
>,
84+
alloy::providers::fillers::WalletFiller<EthereumWallet>,
85+
>,
86+
alloy::providers::RootProvider,
87+
>;

0 commit comments

Comments
 (0)