Skip to content

Commit

Permalink
Bundle L1 block# in CommitBlockInfoV1 (#41)
Browse files Browse the repository at this point in the history
When exporting snapshot files from L1, each snapshot needs information
where each storage log entry originated from, i.e. the L1 block number
for given storage log entry. It's added to `CommitBlockInfoV1` as an
optional field which is not serialized into JSON.
  • Loading branch information
tuommaki authored Nov 16, 2023
1 parent 5408f3f commit 48c7029
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
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

0 comments on commit 48c7029

Please sign in to comment.