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(gateway): refine l2 l1 logs proofs #3078

Merged
merged 17 commits into from
Oct 31, 2024
Merged
5 changes: 5 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions core/bin/zksync_server/src/node_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ impl MainNodeBuilder {
.as_ref()
.and_then(|x| Some(x.gas_adjuster?.settlement_mode))
.unwrap_or(SettlementMode::SettlesToL1),
self.genesis_config.l2_chain_id,
));
Ok(self)
}
Expand Down
8 changes: 7 additions & 1 deletion core/lib/basic_types/src/web3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,20 @@ pub struct Filter {
}

#[derive(Default, Debug, PartialEq, Clone)]
pub struct ValueOrArray<T>(Vec<T>);
pub struct ValueOrArray<T>(pub Vec<T>);

impl<T> ValueOrArray<T> {
pub fn flatten(self) -> Vec<T> {
self.0
}
}

impl<T> From<T> for ValueOrArray<T> {
fn from(value: T) -> Self {
Self(vec![value])
}
}

impl<T> Serialize for ValueOrArray<T>
where
T: Serialize,
Expand Down
13 changes: 11 additions & 2 deletions core/lib/constants/src/message_root.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
// Position of `FullTree::_height` in `MessageRoot`'s storage layout.
/// Position of `chainCount` in `MessageRoot`'s storage layout.
pub const CHAIN_COUNT_KEY: usize = 0;

/// Position of `chainIndexToId` in `MessageRoot`'s storage layout.
pub const CHAIN_INDEX_TO_ID_KEY: usize = 2;

/// Position of `FullTree::_height` in `MessageRoot`'s storage layout.
pub const AGG_TREE_HEIGHT_KEY: usize = 3;

// Position of `FullTree::nodes` in `MessageRoot`'s storage layout.
/// Position of `FullTree::nodes` in `MessageRoot`'s storage layout.
pub const AGG_TREE_NODES_KEY: usize = 5;

/// Position of `chainTree` in `MessageRoot`'s storage layout.
pub const CHAIN_TREE_KEY: usize = 7;

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

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

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

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

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

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

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE l1_batches
DROP COLUMN batch_chain_merkle_path BYTEA;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE l1_batches
ADD COLUMN batch_chain_merkle_path BYTEA;

-- postgres doesn't allow dropping enum variant, so nothing is done in down.sql
ALTER TYPE event_type ADD VALUE 'ChainBatchRoot';
148 changes: 146 additions & 2 deletions core/lib/dal/src/blocks_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use zksync_types::{
},
commitment::{L1BatchCommitmentArtifacts, L1BatchWithMetadata},
fee_model::BatchFeeInput,
l2_to_l1_log::UserL2ToL1Log,
l2_to_l1_log::{BatchAndChainMerklePath, UserL2ToL1Log},
writes::TreeWrite,
Address, Bloom, L1BatchNumber, L2BlockNumber, ProtocolVersionId, H256, U256,
Address, Bloom, L1BatchNumber, L2BlockNumber, ProtocolVersionId, SLChainId, H256, U256,
};
use zksync_vm_interface::CircuitStatistic;

Expand Down Expand Up @@ -2004,6 +2004,150 @@ impl BlocksDal<'_, '_> {
Ok(Some((H256::from_slice(&hash), row.timestamp as u64)))
}

pub async fn get_l1_batch_local_root(
&mut self,
number: L1BatchNumber,
) -> DalResult<Option<H256>> {
let Some(row) = sqlx::query!(
r#"
SELECT
local_root
FROM
l1_batches
WHERE
number = $1
"#,
i64::from(number.0)
)
.instrument("get_l1_batch_local_root")
.with_arg("number", &number)
.fetch_optional(self.storage)
.await?
else {
return Ok(None);
};
let Some(local_root) = row.local_root else {
return Ok(None);
};
Ok(Some(H256::from_slice(&local_root)))
}

pub async fn get_l1_batch_l2_l1_merkle_root(
&mut self,
number: L1BatchNumber,
) -> DalResult<Option<H256>> {
let Some(row) = sqlx::query!(
r#"
SELECT
l2_l1_merkle_root
FROM
l1_batches
WHERE
number = $1
"#,
i64::from(number.0)
)
.instrument("get_l1_batch_l2_l1_merkle_root")
.with_arg("number", &number)
.fetch_optional(self.storage)
.await?
else {
return Ok(None);
};
let Some(l2_l1_merkle_root) = row.l2_l1_merkle_root else {
return Ok(None);
};
Ok(Some(H256::from_slice(&l2_l1_merkle_root)))
}

pub async fn get_l1_batch_chain_merkle_path(
&mut self,
number: L1BatchNumber,
) -> DalResult<Option<BatchAndChainMerklePath>> {
let Some(row) = sqlx::query!(
r#"
SELECT
batch_chain_merkle_path
FROM
l1_batches
WHERE
number = $1
"#,
i64::from(number.0)
)
.instrument("get_l1_batch_chain_merkle_path")
.with_arg("number", &number)
.fetch_optional(self.storage)
.await?
else {
return Ok(None);
};
let Some(batch_chain_merkle_path) = row.batch_chain_merkle_path else {
return Ok(None);
};
Ok(Some(
bincode::deserialize(&batch_chain_merkle_path).unwrap(),
))
}

pub async fn get_executed_batch_roots_on_sl(
&mut self,
sl_chain_id: SLChainId,
) -> DalResult<Vec<(L1BatchNumber, H256)>> {
let result = sqlx::query!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case on an emergency if the execute transaction happen outside the eth sender, how hard will it be make this function work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be simple: insert corresponding rows to eth_txs_history and eth_tx tables. However, if it happens and server restarts before we insert these rows it potentially can lead to incorrect batch_chain_merkle_path being saved. I guess I will add a sanity check to events processor: batch root calculated on eth watcher side should be the same as on SL

r#"
SELECT
number, l2_l1_merkle_root
FROM
l1_batches
JOIN eth_txs ON eth_txs.id = l1_batches.eth_execute_tx_id
WHERE
batch_chain_merkle_path IS NOT NULL
AND chain_id = $1
ORDER BY number
"#,
sl_chain_id.0 as i64
)
.instrument("get_executed_batch_roots_on_sl")
.with_arg("sl_chain_id", &sl_chain_id)
.fetch_all(self.storage)
.await?
.into_iter()
.map(|row| {
let number = L1BatchNumber(row.number as u32);
let root = H256::from_slice(&row.l2_l1_merkle_root.unwrap());
(number, root)
})
.collect();
Ok(result)
}

pub async fn set_batch_chain_merkle_path(
&mut self,
number: L1BatchNumber,
proof: BatchAndChainMerklePath,
) -> DalResult<()> {
let proof_bin = bincode::serialize(&proof).unwrap();
sqlx::query!(
r#"
UPDATE
l1_batches
SET
batch_chain_merkle_path = $2
WHERE
number = $1
"#,
i64::from(number.0),
&proof_bin
)
.instrument("set_batch_chain_merkle_path")
.with_arg("number", &number)
.execute(self.storage)
.await?;

Ok(())
}

pub async fn get_l1_batch_metadata(
&mut self,
number: L1BatchNumber,
Expand Down
1 change: 1 addition & 0 deletions core/lib/dal/src/eth_watcher_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct EthWatcherDal<'a, 'c> {
pub enum EventType {
ProtocolUpgrades,
PriorityTransactions,
ChainBatchRoot,
}

impl EthWatcherDal<'_, '_> {
Expand Down
Loading
Loading