Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tee): introduce get_tee_proofs RPC method for TEE proofs #2474

Merged
merged 30 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f4b0580
feat(tee): add tee/get_proof endpoint to TEE Prover Gateway
pbeza Jul 24, 2024
402efe7
Get rid of .unwrap() in TEE proof generation DAL
pbeza Jul 25, 2024
3e3ef00
Fix a typo in the proof_data_handler endpoint
pbeza Jul 25, 2024
1d51ee0
Merge remote-tracking branch 'origin/main' into tee/attestation_fetcher
pbeza Jul 25, 2024
45cd290
Add unit test
pbeza Jul 26, 2024
7f4b859
Merge remote-tracking branch 'origin/main' into tee/attestation_fetcher
pbeza Jul 26, 2024
cea2cf9
Restructure db tables for TEE proof generation
pbeza Jul 29, 2024
90f4a07
One proof instance per batch number per TEE type
pbeza Jul 31, 2024
96142e8
Merge remote-tracking branch 'origin/main' into tee/attestation_fetcher
pbeza Jul 31, 2024
f9e9cdb
Fix SQL query (identified by unit test)
pbeza Aug 1, 2024
cb69c23
Merge remote-tracking branch 'origin/main' into tee/attestation_fetcher
pbeza Aug 1, 2024
ab382d1
Simplify code
pbeza Aug 1, 2024
4c95d0b
Update docs (Mermaid diagram)
pbeza Aug 1, 2024
8e3ac82
Merge remote-tracking branch 'origin/main' into tee/attestation_fetcher
pbeza Aug 5, 2024
aa9d363
Fix docs
pbeza Aug 5, 2024
50a65c9
fixup! Fix docs
pbeza Aug 5, 2024
6e5d39f
Fix db migrations
pbeza Aug 5, 2024
ab300c9
fixup! Fix db migrations
pbeza Aug 5, 2024
962007f
fixup! Fix db migrations
pbeza Aug 6, 2024
e2b3dce
Merge remote-tracking branch 'origin/main' into tee/attestation_fetcher
pbeza Aug 6, 2024
bd6d373
Merge remote-tracking branch 'origin/main' into tee/attestation_fetcher
pbeza Aug 7, 2024
328e9a1
Address @perekopskiy's code review comments
pbeza Aug 7, 2024
51e59a4
fixup! Address @perekopskiy's code review comments
pbeza Aug 7, 2024
cf27ac6
Merge remote-tracking branch 'origin/main' into tee/attestation_fetcher
pbeza Aug 7, 2024
5a063b9
Merge remote-tracking branch 'origin/main' into tee/attestation_fetcher
pbeza Aug 8, 2024
3caf0f1
Merge remote-tracking branch 'origin/main' into tee/attestation_fetcher
pbeza Aug 8, 2024
3aa30b2
Code review fix: idempotent SQL queries
pbeza Aug 8, 2024
787bc8d
fixup! Code review fix: idempotent SQL queries
pbeza Aug 8, 2024
caea7a5
Code review fix: single migration file
pbeza Aug 8, 2024
22b4dd3
Update docs
pbeza Aug 8, 2024
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

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

80 changes: 66 additions & 14 deletions core/lib/dal/src/tee_proof_generation_dal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::time::Duration;

use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};
use zksync_db_connection::{
connection::Connection,
Expand All @@ -16,6 +17,19 @@ pub struct TeeProofGenerationDal<'a, 'c> {
pub(crate) storage: &'a mut Connection<'c, Core>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TeeProof {
// signature generated within the TEE enclave, using the privkey corresponding to the pubkey
pub signature: Option<Vec<u8>>,
// pubkey used for signature verification; each key pair is attested by the TEE attestation
// stored in the db
pub pubkey: Option<Vec<u8>>,
// data that was signed
pub proof: Option<Vec<u8>>,
// type of TEE used for attestation
pub tee_type: Option<TeeType>,
}
pbeza marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Debug, EnumString, Display)]
enum TeeProofGenerationJobStatus {
#[strum(serialize = "ready_to_be_proven")]
Expand All @@ -34,7 +48,7 @@ impl TeeProofGenerationDal<'_, '_> {
processing_timeout: Duration,
) -> DalResult<Option<L1BatchNumber>> {
let processing_timeout = pg_interval_from_duration(processing_timeout);
let result: Option<L1BatchNumber> = sqlx::query!(
let query = sqlx::query!(
r#"
UPDATE tee_proof_generation_details
SET
Expand Down Expand Up @@ -68,13 +82,15 @@ impl TeeProofGenerationDal<'_, '_> {
tee_proof_generation_details.l1_batch_number
"#,
&processing_timeout,
)
.fetch_optional(self.storage.conn())
.await
.unwrap()
.map(|row| L1BatchNumber(row.l1_batch_number as u32));
);
let batch_number = Instrumented::new("get_next_block_to_be_proven")
.with_arg("processing_timeout", &processing_timeout)
.with(query)
.fetch_optional(self.storage)
.await?
.map(|row| L1BatchNumber(row.l1_batch_number as u32));

Ok(result)
Ok(batch_number)
}

pub async fn save_proof_artifacts_metadata(
Expand Down Expand Up @@ -149,7 +165,7 @@ impl TeeProofGenerationDal<'_, '_> {
}

pub async fn get_oldest_unpicked_batch(&mut self) -> DalResult<Option<L1BatchNumber>> {
let result: Option<L1BatchNumber> = sqlx::query!(
let query = sqlx::query!(
r#"
SELECT
proofs.l1_batch_number
Expand All @@ -164,13 +180,14 @@ impl TeeProofGenerationDal<'_, '_> {
LIMIT
1
"#,
)
.fetch_optional(self.storage.conn())
.await
.unwrap()
.map(|row| L1BatchNumber(row.l1_batch_number as u32));
);
let batch_number = Instrumented::new("get_oldest_unpicked_batch")
.with(query)
.fetch_optional(self.storage)
.await?
.map(|row| L1BatchNumber(row.l1_batch_number as u32));

Ok(result)
Ok(batch_number)
}

pub async fn save_attestation(&mut self, pubkey: &[u8], attestation: &[u8]) -> DalResult<()> {
Expand Down Expand Up @@ -202,4 +219,39 @@ impl TeeProofGenerationDal<'_, '_> {

Ok(())
}

pub async fn get_tee_proof(
&mut self,
block_number: L1BatchNumber,
) -> DalResult<Option<TeeProof>> {
let query = sqlx::query!(
r#"
SELECT
signature,
pubkey,
proof,
tee_type
FROM
tee_proof_generation_details
WHERE
l1_batch_number = $1
"#,
i64::from(block_number.0)
);
let proof = Instrumented::new("get_tee_proof")
.with_arg("l1_batch_number", &block_number)
.with(query)
.fetch_optional(self.storage)
.await?
.map(|row| TeeProof {
signature: row.signature,
pubkey: row.pubkey,
proof: row.proof,
tee_type: row
.tee_type
.map(|tt| tt.parse::<TeeType>().expect("Invalid TEE type")),
});

Ok(proof)
}
}
16 changes: 15 additions & 1 deletion core/node/proof_data_handler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::{net::SocketAddr, sync::Arc};

use anyhow::Context as _;
use axum::{extract::Path, routing::post, Json, Router};
use axum::{
extract::Path,
routing::{get, post},
Json, Router,
};
use request_processor::RequestProcessor;
use tee_request_processor::TeeRequestProcessor;
use tokio::sync::watch;
Expand Down Expand Up @@ -91,6 +95,7 @@ fn create_proof_processing_router(
TeeRequestProcessor::new(blob_store, connection_pool, config.clone());
let submit_tee_proof_processor = get_tee_proof_gen_processor.clone();
let register_tee_attestation_processor = get_tee_proof_gen_processor.clone();
let get_tee_proof_processor = get_tee_proof_gen_processor.clone();

router = router.route(
"/tee/proof_inputs",
Expand Down Expand Up @@ -121,6 +126,15 @@ fn create_proof_processing_router(
.await
},
),
)
.route("/tee/get_proof/:l1_batch_number",
pbeza marked this conversation as resolved.
Show resolved Hide resolved
get(
move |l1_batch_number: Path<u32>| async move {
get_tee_proof_processor
.get_proof(l1_batch_number)
.await
},
)
);
}

Expand Down
21 changes: 20 additions & 1 deletion core/node/proof_data_handler/src/tee_request_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use axum::{extract::Path, Json};
use zksync_config::configs::ProofDataHandlerConfig;
use zksync_dal::{ConnectionPool, Core, CoreDal};
use zksync_dal::{tee_proof_generation_dal::TeeProof, ConnectionPool, Core, CoreDal};
use zksync_object_store::ObjectStore;
use zksync_prover_interface::{
api::{
Expand Down Expand Up @@ -118,4 +118,23 @@ impl TeeRequestProcessor {

Ok(Json(RegisterTeeAttestationResponse::Success))
}

pub(crate) async fn get_proof(
&self,
Path(l1_batch_number): Path<u32>,
) -> Result<Json<TeeProof>, RequestProcessorError> {
let mut connection = self
.pool
.connection()
.await
.map_err(RequestProcessorError::Dal)?;
let mut dal = connection.tee_proof_generation_dal();
let l1_batch_number = L1BatchNumber(l1_batch_number);
let tee_proof = dal
.get_tee_proof(l1_batch_number)
.await
.map_err(RequestProcessorError::Dal)?;

Ok(Json(tee_proof.unwrap()))
}
}
Loading