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

Bundle L1 block# in CommitBlockInfoV1 #41

Merged
merged 3 commits into from
Nov 16, 2023
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
19 changes: 13 additions & 6 deletions state-reconstruct-fetcher/src/l1_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ impl L1Fetcher {
fn spawn_tx_handler(
&self,
mut hash_rx: mpsc::Receiver<H256>,
calldata_tx: mpsc::Sender<Bytes>,
l1_tx_tx: mpsc::Sender<Transaction>,
mut last_block: u64,
) -> tokio::task::JoinHandle<()> {
let metrics = self.metrics.clone();
Expand Down Expand Up @@ -336,24 +336,25 @@ impl L1Fetcher {
}
}

calldata_tx.send(tx.input).await.unwrap();
l1_tx_tx.send(tx).await.unwrap();
}
}
})
}

fn spawn_parsing_handler(
&self,
mut calldata_rx: mpsc::Receiver<Bytes>,
mut l1_tx_rx: mpsc::Receiver<Transaction>,
sink: mpsc::Sender<CommitBlockInfoV1>,
) -> Result<tokio::task::JoinHandle<()>> {
let metrics = self.metrics.clone();
let function = self.contract.functions_by_name("commitBlocks")?[0].clone();

Ok(tokio::spawn({
async move {
while let Some(calldata) = calldata_rx.recv().await {
let blocks = match parse_calldata(&function, &calldata) {
while let Some(tx) = l1_tx_rx.recv().await {
let block_number = tx.block_number.map(|v| v.as_u64());
let blocks = match parse_calldata(block_number, &function, &tx.input) {
Ok(blks) => blks,
Err(e) => {
tracing::error!("failed to parse calldata: {e}");
Expand Down Expand Up @@ -391,6 +392,7 @@ impl L1Fetcher {
}

pub fn parse_calldata(
l1_block_number: Option<u64>,
commit_blocks_fn: &Function,
calldata: &[u8],
) -> Result<Vec<CommitBlockInfoV1>> {
Expand Down Expand Up @@ -435,7 +437,12 @@ pub fn parse_calldata(
// TODO: What to do here?
// assert_eq!(previous_enumeration_index, tree.next_enumeration_index());

parse_commit_block_info(&new_blocks_data)
// Supplement every CommitBlockInfoV1 element with L1 block number information.
parse_commit_block_info(&new_blocks_data).map(|mut vec| {
vec.iter_mut()
.for_each(|e| e.l1_block_number = l1_block_number);
vec
})
}

fn parse_commit_block_info(data: &abi::Token) -> Result<Vec<CommitBlockInfoV1>> {
Expand Down
4 changes: 4 additions & 0 deletions state-reconstruct-fetcher/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub enum ParseError {
/// Data needed to commit new block
#[derive(Debug, Serialize, Deserialize)]
pub struct CommitBlockInfoV1 {
/// L1 block number.
#[serde(skip_serializing)]
pub l1_block_number: Option<u64>,
/// L2 block number.
pub block_number: u64,
/// Unix timestamp denoting the start of the block execution.
Expand Down Expand Up @@ -98,6 +101,7 @@ impl TryFrom<&abi::Token> for CommitBlockInfoV1 {
);

let mut blk = CommitBlockInfoV1 {
l1_block_number: None,
block_number: new_l2_block_number.as_u64(),
timestamp: timestamp.as_u64(),
index_repeated_storage_changes: new_enumeration_index,
Expand Down