-
Notifications
You must be signed in to change notification settings - Fork 81
feat(l2): implement batch endpoint #3374
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
Changes from all commits
8635065
352d397
cc7fa2e
98a603c
f6fee06
57a856a
c84e37e
39feb9b
9291011
fadee23
ab4577a
2b32a6f
470ed97
1a8cf99
de7d232
3d912cf
2da451a
fb87845
8664cbb
b1eea4d
73a57a5
45563b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
use serde::Serialize; | ||
|
||
use crate::H256; | ||
|
||
use super::BlobsBundle; | ||
|
||
#[derive(Clone)] | ||
#[derive(Clone, Serialize)] | ||
pub struct Batch { | ||
pub number: u64, | ||
pub first_block: u64, | ||
pub last_block: u64, | ||
pub state_root: H256, | ||
pub deposit_logs_hash: H256, | ||
pub message_hashes: Vec<H256>, | ||
#[serde(skip_serializing)] | ||
pub blobs_bundle: BlobsBundle, | ||
pub commit_tx: Option<H256>, | ||
pub verify_tx: Option<H256>, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use ethrex_common::types::{BlockHash, batch::Batch}; | ||
use ethrex_storage::Store; | ||
use serde::Serialize; | ||
use serde_json::Value; | ||
use tracing::info; | ||
|
||
use crate::{ | ||
rpc::{RpcApiContext, RpcHandler}, | ||
utils::RpcErr, | ||
}; | ||
|
||
#[derive(Serialize)] | ||
pub struct RpcBatch { | ||
#[serde(flatten)] | ||
pub batch: Batch, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub block_hashes: Option<Vec<BlockHash>>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If block hashes can be none, why not simply use an empty vec? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} | ||
|
||
impl RpcBatch { | ||
pub async fn build(batch: Batch, block_hashes: bool, store: &Store) -> Result<Self, RpcErr> { | ||
let block_hashes = if block_hashes { | ||
Some(get_block_hashes( | ||
batch.first_block, | ||
batch.last_block, | ||
store, | ||
)?) | ||
} else { | ||
None | ||
}; | ||
|
||
Ok(RpcBatch { | ||
batch, | ||
block_hashes, | ||
}) | ||
} | ||
} | ||
|
||
fn get_block_hashes( | ||
first_block: u64, | ||
last_block: u64, | ||
store: &Store, | ||
) -> Result<Vec<BlockHash>, RpcErr> { | ||
let mut block_hashes = Vec::new(); | ||
for block_number in first_block..=last_block { | ||
let header = store | ||
.get_block_header(block_number)? | ||
.ok_or(RpcErr::Internal(format!( | ||
"Failed to retrieve block header for block number {block_number}" | ||
)))?; | ||
let hash = header.hash(); | ||
block_hashes.push(hash); | ||
} | ||
Ok(block_hashes) | ||
} | ||
|
||
pub struct GetBatchByBatchNumberRequest { | ||
pub batch_number: u64, | ||
pub block_hashes: bool, | ||
} | ||
|
||
impl RpcHandler for GetBatchByBatchNumberRequest { | ||
fn parse(params: &Option<Vec<Value>>) -> Result<GetBatchByBatchNumberRequest, RpcErr> { | ||
let params = params.as_ref().ok_or(ethrex_rpc::RpcErr::BadParams( | ||
"No params provided".to_owned(), | ||
))?; | ||
if params.len() != 2 { | ||
return Err(ethrex_rpc::RpcErr::BadParams( | ||
"Expected 2 params".to_owned(), | ||
))?; | ||
}; | ||
// Parse BatchNumber | ||
let hex_str = serde_json::from_value::<String>(params[0].clone()) | ||
.map_err(|e| ethrex_rpc::RpcErr::BadParams(e.to_string()))?; | ||
|
||
// Check that the BatchNumber is 0x prefixed | ||
let hex_str = hex_str | ||
.strip_prefix("0x") | ||
.ok_or(ethrex_rpc::RpcErr::BadHexFormat(0))?; | ||
|
||
// Parse hex string | ||
let batch_number = | ||
u64::from_str_radix(hex_str, 16).map_err(|_| ethrex_rpc::RpcErr::BadHexFormat(0))?; | ||
|
||
let block_hashes = serde_json::from_value(params[1].clone())?; | ||
|
||
Ok(GetBatchByBatchNumberRequest { | ||
batch_number, | ||
block_hashes, | ||
}) | ||
} | ||
|
||
async fn handle(&self, context: RpcApiContext) -> Result<Value, RpcErr> { | ||
info!("Requested batch with number: {}", self.batch_number); | ||
let Some(batch) = context.rollup_store.get_batch(self.batch_number).await? else { | ||
return Ok(Value::Null); | ||
}; | ||
let rpc_batch = RpcBatch::build(batch, self.block_hashes, &context.l1_ctx.storage).await?; | ||
|
||
serde_json::to_value(&rpc_batch).map_err(|error| RpcErr::Internal(error.to_string())) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod batch; | ||
pub mod l1_message; | ||
pub mod transaction; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,6 +185,14 @@ impl L1ProofVerifier { | |
) | ||
.await?; | ||
|
||
// Store the verify transaction hash for each batch that was aggregated. | ||
for i in 0..aggregated_proofs_count { | ||
let batch_number = first_batch_number + i; | ||
self.rollup_store | ||
.store_verify_tx_by_batch(batch_number, verify_tx_hash) | ||
.await?; | ||
} | ||
|
||
Ok(Some(verify_tx_hash)) | ||
Comment on lines
+188
to
196
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to create a function that takes a range of batch numbers and stores them all in a single transaction There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created #3432! |
||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider returning a BTreeMap<BatchNumber, Log> instead of a Vec with tuples if possible. This will save us the call to sorting here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part is simply a refactor to also process verified_logs. I just copied and pasted what was done before. We can review this in the future.