Skip to content

Commit 5885ff0

Browse files
avilagaston9SDartayet
authored andcommitted
feat(l2): exchange commit hash in node-prover communication (#3339)
**Motivation** We want to prevent a divergence between the code that is running in the L2 node and the prover. **Description** - Updates the client version to use `GIT_BRANCH` and `GIT_SHA` instead of `RUSTC_COMMIT_HASH`. - Adds a `build.rs` script for both the node and prover, using `vergen_git2` to export the git env vars. - Adds a `code_version` field to the `BatchRequest` message. - Introduces a new `ProofData` message: `InvalidCodeVersion`. ## How to test You can create an empty commit with: ```bash git commit --allow-empty -m "empty commit" ``` Then run the node and the prover using different commits. > [!WARNING] > Remember to run `make build-prover` whenever you change the commit Closes #3311
1 parent 8a463af commit 5885ff0

File tree

15 files changed

+376
-78
lines changed

15 files changed

+376
-78
lines changed

Cargo.lock

Lines changed: 66 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/ethrex/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,5 @@ name = "build_block_benchmark"
105105
harness = false
106106

107107
[build-dependencies]
108-
vergen = { version = "9.0.0", features = ["rustc"] }
108+
vergen-git2 = { version = "1.0.7", features = ["rustc"] }
109+

cmd/ethrex/build.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
use std::error::Error;
2-
use vergen::{Emitter, RustcBuilder};
2+
use vergen_git2::{Emitter, Git2Builder, RustcBuilder};
33

44
// This build code is needed to add some env vars in order to construct the node client version
5-
// VERGEN_RUSTC_COMMIT_HASH to get the commit hash
65
// VERGEN_RUSTC_HOST_TRIPLE to get the build OS
76
// VERGEN_RUSTC_SEMVER to get the rustc version
7+
// VERGEN_GIT_BRANCH to get the git branch name
8+
// VERGEN_GIT_SHA to get the git commit hash
89

910
fn main() -> Result<(), Box<dyn Error>> {
10-
let rustc = RustcBuilder::all_rustc()?;
11+
// Export build OS and rustc version as environment variables
12+
let rustc = RustcBuilder::default()
13+
.semver(true)
14+
.host_triple(true)
15+
.build()?;
1116

12-
Emitter::default().add_instructions(&rustc)?.emit()?;
17+
// Export git commit hash and branch name as environment variables
18+
let git2 = Git2Builder::default().branch(true).sha(true).build()?;
19+
20+
Emitter::default()
21+
.add_instructions(&rustc)?
22+
.add_instructions(&git2)?
23+
.emit()?;
1324
Ok(())
1425
}

cmd/ethrex/utils.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,11 @@ pub fn parse_hex(s: &str) -> eyre::Result<Bytes, FromHexError> {
153153

154154
pub fn get_client_version() -> String {
155155
format!(
156-
"{}/v{}-develop-{}/{}/rustc-v{}",
156+
"{}/v{}-{}-{}/{}/rustc-v{}",
157157
env!("CARGO_PKG_NAME"),
158158
env!("CARGO_PKG_VERSION"),
159-
&env!("VERGEN_RUSTC_COMMIT_HASH")[0..6],
159+
env!("VERGEN_GIT_BRANCH"),
160+
env!("VERGEN_GIT_SHA"),
160161
env!("VERGEN_RUSTC_HOST_TRIPLE"),
161162
env!("VERGEN_RUSTC_SEMVER")
162163
)

crates/l2/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ zkvm_interface = { path = "./prover/zkvm/interface/" }
5151
[dev-dependencies]
5252
rand = "0.8.5"
5353

54+
[build-dependencies]
55+
vergen-git2 = { version = "1.0.7"}
56+
5457
[lib]
5558
path = "./l2.rs"
5659

crates/l2/build.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::error::Error;
2+
use vergen_git2::{Emitter, Git2Builder};
3+
4+
// This build code is needed to add some env vars in order to construct the code version
5+
// VERGEN_GIT_SHA to get the git commit hash
6+
7+
fn main() -> Result<(), Box<dyn Error>> {
8+
// Export git commit hash and branch name as environment variables
9+
let git2 = Git2Builder::default().sha(true).build()?;
10+
11+
Emitter::default().add_instructions(&git2)?.emit()?;
12+
Ok(())
13+
}

crates/l2/prover/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "ethrex-prover"
33
version.workspace = true
44
edition.workspace = true
5+
build = "build.rs"
56

67
[dependencies]
78
serde_json.workspace = true
@@ -42,6 +43,9 @@ sp1-recursion-gnark-ffi = { version = "5.0.0", optional = true }
4243
[dev-dependencies]
4344
ethrex-storage.workspace = true
4445

46+
[build-dependencies]
47+
vergen-git2 = { version = "1.0.7"}
48+
4549
[lib]
4650
name = "ethrex_prover_lib"
4751
path = "src/lib.rs"

crates/l2/prover/build.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::error::Error;
2+
use vergen_git2::{Emitter, Git2Builder};
3+
4+
// This build code is needed to add some env vars in order to construct the code version
5+
// VERGEN_GIT_SHA to get the git commit hash
6+
7+
fn main() -> Result<(), Box<dyn Error>> {
8+
// Export git commit hash and branch name as environment variables
9+
let git2 = Git2Builder::default().sha(true).build()?;
10+
11+
Emitter::default().add_instructions(&git2)?.emit()?;
12+
Ok(())
13+
}

crates/l2/prover/src/prover.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{config::ProverConfig, prove, to_batch_proof};
2-
use ethrex_l2::sequencer::proof_coordinator::ProofData;
2+
use ethrex_l2::sequencer::proof_coordinator::{ProofData, get_commit_hash};
33
use ethrex_l2_common::prover::BatchProof;
44
use std::time::Duration;
55
use tokio::{
@@ -24,6 +24,7 @@ struct Prover {
2424
prover_server_endpoint: String,
2525
proving_time_ms: u64,
2626
aligned_mode: bool,
27+
commit_hash: String,
2728
}
2829

2930
impl Prover {
@@ -32,6 +33,7 @@ impl Prover {
3233
prover_server_endpoint: format!("{}:{}", cfg.http_addr, cfg.http_port),
3334
proving_time_ms: cfg.proving_time_ms,
3435
aligned_mode: cfg.aligned_mode,
36+
commit_hash: get_commit_hash(),
3537
}
3638
}
3739

@@ -43,10 +45,15 @@ impl Prover {
4345
let Ok(prover_data) = self
4446
.request_new_input()
4547
.await
46-
.inspect_err(|e| warn!("Failed to request new data: {e}"))
48+
.inspect_err(|e| error!("Failed to request new data: {e}"))
4749
else {
4850
continue;
4951
};
52+
53+
let Some(prover_data) = prover_data else {
54+
continue;
55+
};
56+
5057
// If we get the input
5158
// Generate the Proof
5259
let Ok(batch_proof) = prove(prover_data.input, self.aligned_mode)
@@ -65,30 +72,36 @@ impl Prover {
6572
}
6673
}
6774

68-
async fn request_new_input(&self) -> Result<ProverData, String> {
75+
async fn request_new_input(&self) -> Result<Option<ProverData>, String> {
6976
// Request the input with the correct batch_number
70-
let request = ProofData::batch_request();
77+
let request = ProofData::batch_request(self.commit_hash.clone());
7178
let response = connect_to_prover_server_wr(&self.prover_server_endpoint, &request)
7279
.await
7380
.map_err(|e| format!("Failed to get Response: {e}"))?;
7481

75-
let ProofData::BatchResponse {
76-
batch_number,
77-
input,
78-
} = response
79-
else {
80-
return Err("Expecting ProofData::Response".to_owned());
82+
let (batch_number, input) = match response {
83+
ProofData::BatchResponse {
84+
batch_number,
85+
input,
86+
} => (batch_number, input),
87+
ProofData::InvalidCodeVersion { commit_hash } => {
88+
return Err(format!(
89+
"Invalid code version received. Server commit_hash: {}, Prover commit_hash: {}",
90+
commit_hash, self.commit_hash
91+
));
92+
}
93+
_ => return Err("Expecting ProofData::Response".to_owned()),
8194
};
8295

8396
let (Some(batch_number), Some(input)) = (batch_number, input) else {
84-
return Err(
85-
"Received Empty Response, meaning that the ProverServer doesn't have batches to prove.\nThe Prover may be advancing faster than the Proposer."
86-
.to_owned(),
87-
);
97+
warn!(
98+
"Received Empty Response, meaning that the ProverServer doesn't have batches to prove.\nThe Prover may be advancing faster than the Proposer."
99+
);
100+
return Ok(None);
88101
};
89102

90103
info!("Received Response for batch_number: {batch_number}");
91-
Ok(ProverData {
104+
Ok(Some(ProverData {
92105
batch_number,
93106
input: ProgramInput {
94107
blocks: input.blocks,
@@ -99,7 +112,7 @@ impl Prover {
99112
#[cfg(feature = "l2")]
100113
blob_proof: input.blob_proof,
101114
},
102-
})
115+
}))
103116
}
104117

105118
async fn submit_proof(&self, batch_number: u64, batch_proof: BatchProof) -> Result<(), String> {

0 commit comments

Comments
 (0)