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

fix: Disallow non null updates for transactions #1951

Merged
merged 8 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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

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.

134 changes: 124 additions & 10 deletions core/lib/dal/src/blocks_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,17 +402,18 @@ impl BlocksDal<'_, '_> {
number_range: ops::RangeInclusive<L1BatchNumber>,
eth_tx_id: u32,
aggregation_type: AggregatedActionType,
) -> DalResult<()> {
) -> anyhow::Result<()> {
Artemka374 marked this conversation as resolved.
Show resolved Hide resolved
match aggregation_type {
AggregatedActionType::Commit => {
sqlx::query!(
let result = sqlx::query!(
r#"
UPDATE l1_batches
SET
eth_commit_tx_id = $1,
updated_at = NOW()
WHERE
number BETWEEN $2 AND $3
AND eth_commit_tx_id IS NULL
"#,
eth_tx_id as i32,
i64::from(number_range.start().0),
Expand All @@ -423,16 +424,23 @@ impl BlocksDal<'_, '_> {
.with_arg("eth_tx_id", &eth_tx_id)
.execute(self.storage)
.await?;

if result.rows_affected() == 0 {
return Err(anyhow::anyhow!(
"Update eth_commit_tx_id that is is not null is not allowed"
));
}
}
AggregatedActionType::PublishProofOnchain => {
sqlx::query!(
let result = sqlx::query!(
r#"
UPDATE l1_batches
SET
eth_prove_tx_id = $1,
updated_at = NOW()
WHERE
number BETWEEN $2 AND $3
AND eth_prove_tx_id IS NULL
"#,
eth_tx_id as i32,
i64::from(number_range.start().0),
Expand All @@ -443,16 +451,23 @@ impl BlocksDal<'_, '_> {
.with_arg("eth_tx_id", &eth_tx_id)
.execute(self.storage)
.await?;

if result.rows_affected() == 0 {
return Err(anyhow::anyhow!(
"Update eth_prove_tx_id that is is not null is not allowed"
));
}
}
AggregatedActionType::Execute => {
sqlx::query!(
let result = sqlx::query!(
r#"
UPDATE l1_batches
SET
eth_execute_tx_id = $1,
updated_at = NOW()
WHERE
number BETWEEN $2 AND $3
AND eth_execute_tx_id IS NULL
"#,
eth_tx_id as i32,
i64::from(number_range.start().0),
Expand All @@ -463,6 +478,12 @@ impl BlocksDal<'_, '_> {
.with_arg("eth_tx_id", &eth_tx_id)
.execute(self.storage)
.await?;

if result.rows_affected() == 0 {
return Err(anyhow::anyhow!(
"Update eth_execute_tx_id that is is not null is not allowed"
));
}
}
}
Ok(())
Expand Down Expand Up @@ -2243,15 +2264,14 @@ mod tests {
use super::*;
use crate::{ConnectionPool, Core, CoreDal};

#[tokio::test]
async fn loading_l1_batch_header() {
let pool = ConnectionPool::<Core>::test_pool().await;
let mut conn = pool.connection().await.unwrap();
conn.protocol_versions_dal()
.save_protocol_version_with_tx(&ProtocolVersion::default())
async fn save_mock_eth_tx(action_type: AggregatedActionType, conn: &mut Connection<'_, Core>) {
conn.eth_sender_dal()
.save_eth_tx(1, vec![], action_type, Address::default(), 1, None, None)
.await
.unwrap();
}

fn mock_l1_batch_header() -> L1BatchHeader {
let mut header = L1BatchHeader::new(
L1BatchNumber(1),
100,
Expand All @@ -2274,6 +2294,100 @@ mod tests {
header.l2_to_l1_messages.push(vec![22; 22]);
header.l2_to_l1_messages.push(vec![33; 33]);

header
}

#[tokio::test]
async fn set_tx_id_works_correctly() {
let pool = ConnectionPool::<Core>::test_pool().await;
let mut conn = pool.connection().await.unwrap();

conn.protocol_versions_dal()
.save_protocol_version_with_tx(&ProtocolVersion::default())
.await
.unwrap();

conn.blocks_dal()
.insert_mock_l1_batch(&mock_l1_batch_header())
.await
.unwrap();

save_mock_eth_tx(AggregatedActionType::Commit, &mut conn).await;
save_mock_eth_tx(AggregatedActionType::PublishProofOnchain, &mut conn).await;
save_mock_eth_tx(AggregatedActionType::Execute, &mut conn).await;

assert!(conn
.blocks_dal()
.set_eth_tx_id(
L1BatchNumber(1)..=L1BatchNumber(1),
1,
AggregatedActionType::Commit,
)
.await
.is_ok());

assert!(conn
.blocks_dal()
.set_eth_tx_id(
L1BatchNumber(1)..=L1BatchNumber(1),
2,
AggregatedActionType::Commit,
)
.await
.is_err());

assert!(conn
.blocks_dal()
.set_eth_tx_id(
L1BatchNumber(1)..=L1BatchNumber(1),
1,
AggregatedActionType::PublishProofOnchain,
)
.await
.is_ok());

assert!(conn
.blocks_dal()
.set_eth_tx_id(
L1BatchNumber(1)..=L1BatchNumber(1),
2,
AggregatedActionType::PublishProofOnchain,
)
.await
.is_err());

assert!(conn
.blocks_dal()
.set_eth_tx_id(
L1BatchNumber(1)..=L1BatchNumber(1),
1,
AggregatedActionType::Execute,
)
.await
.is_ok());

assert!(conn
.blocks_dal()
.set_eth_tx_id(
L1BatchNumber(1)..=L1BatchNumber(1),
2,
AggregatedActionType::Execute,
)
.await
.is_err());
}

#[tokio::test]
async fn loading_l1_batch_header() {
let pool = ConnectionPool::<Core>::test_pool().await;
let mut conn = pool.connection().await.unwrap();
conn.protocol_versions_dal()
.save_protocol_version_with_tx(&ProtocolVersion::default())
.await
.unwrap();

let header = mock_l1_batch_header();

conn.blocks_dal()
.insert_mock_l1_batch(&header)
.await
Expand Down
Loading