Skip to content

Commit e28d063

Browse files
committed
refactor: move sequenced user op insertion inside sqlite
1 parent 0713807 commit e28d063

4 files changed

Lines changed: 20 additions & 23 deletions

File tree

sequencer/src/storage/db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
88
use super::sql::{
99
sql_count_user_ops_for_frame, sql_insert_open_batch, sql_insert_open_batch_with_index,
1010
sql_insert_open_frame, sql_insert_safe_inputs_batch,
11-
sql_insert_sequenced_direct_inputs_for_frame, sql_insert_user_ops_and_sequenced_batch,
11+
sql_insert_sequenced_direct_inputs_for_frame, sql_insert_user_ops_batch,
1212
sql_select_batch_policy, sql_select_frames_for_batch, sql_select_latest_batch_index,
1313
sql_select_latest_batch_with_user_op_count, sql_select_latest_frame_in_batch_for_batch,
1414
sql_select_max_safe_input_index, sql_select_ordered_l2_tx_count,
@@ -308,7 +308,7 @@ impl Storage {
308308
// observe the same database snapshot.
309309
assert_write_head_matches_open_state(&tx, head)?;
310310

311-
sql_insert_user_ops_and_sequenced_batch(
311+
sql_insert_user_ops_batch(
312312
&tx,
313313
u64_to_i64(head.batch_index),
314314
i64::from(head.frame_in_batch),

sequencer/src/storage/migrations/0001_schema.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ CREATE TABLE IF NOT EXISTS user_ops (
2929
UNIQUE(sender, nonce)
3030
);
3131

32+
-- Automatically sequence every user-op into the global replay order on insert.
33+
CREATE TRIGGER IF NOT EXISTS trg_sequence_user_op AFTER INSERT ON user_ops
34+
BEGIN
35+
INSERT INTO sequenced_l2_txs (
36+
batch_index, frame_in_batch, user_op_pos_in_frame, safe_input_index
37+
) VALUES (NEW.batch_index, NEW.frame_in_batch, NEW.pos_in_frame, NULL);
38+
END;
39+
3240
CREATE TABLE IF NOT EXISTS safe_inputs (
3341
safe_input_index INTEGER PRIMARY KEY,
3442
sender BLOB NOT NULL CHECK (length(sender) = 20),

sequencer/src/storage/queries/insert_sequenced_user_op.sql

Lines changed: 0 additions & 6 deletions
This file was deleted.

sequencer/src/storage/sql.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const SQL_SELECT_SAFE_BLOCK: &str =
3232
"SELECT block_number FROM l1_safe_head WHERE singleton_id = 0 LIMIT 1";
3333
const SQL_INSERT_SAFE_INPUT: &str = "INSERT INTO safe_inputs (safe_input_index, sender, payload, block_number) VALUES (?1, ?2, ?3, ?4)";
3434
const SQL_INSERT_USER_OP: &str = include_str!("queries/insert_user_op.sql");
35-
const SQL_INSERT_SEQUENCED_USER_OP: &str = include_str!("queries/insert_sequenced_user_op.sql");
3635
const SQL_INSERT_SEQUENCED_DIRECT_INPUT: &str =
3736
include_str!("queries/insert_sequenced_direct_input.sql");
3837
const SQL_UPDATE_BATCH_POLICY_GAS_PRICE: &str =
@@ -190,7 +189,10 @@ pub(super) fn sql_insert_safe_inputs_batch(
190189
Ok(())
191190
}
192191

193-
pub(super) fn sql_insert_user_ops_and_sequenced_batch(
192+
/// Insert user-ops into the `user_ops` table.
193+
/// The `trg_sequence_user_op` trigger automatically appends a corresponding row
194+
/// to `sequenced_l2_txs` for each inserted user-op.
195+
pub(super) fn sql_insert_user_ops_batch(
194196
tx: &Transaction<'_>,
195197
batch_index: i64,
196198
frame_in_batch: i64,
@@ -201,12 +203,11 @@ pub(super) fn sql_insert_user_ops_and_sequenced_batch(
201203
return Ok(());
202204
}
203205

204-
let mut user_ops_stmt = tx.prepare_cached(SQL_INSERT_USER_OP)?;
205-
let mut sequenced_stmt = tx.prepare_cached(SQL_INSERT_SEQUENCED_USER_OP)?;
206+
let mut stmt = tx.prepare_cached(SQL_INSERT_USER_OP)?;
206207
for (offset, item) in user_ops.iter().enumerate() {
207208
let pos_in_frame = frame_pos_start.saturating_add(offset as u32);
208209
let sig = item.signed.signature.as_bytes();
209-
user_ops_stmt.execute(params![
210+
stmt.execute(params![
210211
batch_index,
211212
frame_in_batch,
212213
i64::from(pos_in_frame),
@@ -217,11 +218,6 @@ pub(super) fn sql_insert_user_ops_and_sequenced_batch(
217218
&sig[..],
218219
to_unix_ms(item.received_at),
219220
])?;
220-
sequenced_stmt.execute(params![
221-
batch_index,
222-
frame_in_batch,
223-
i64::from(pos_in_frame),
224-
])?;
225221
}
226222
Ok(())
227223
}
@@ -411,9 +407,9 @@ fn u64_to_i64(value: u64) -> i64 {
411407
mod tests {
412408
use super::{
413409
FrameHeaderRow, SQL_INSERT_SAFE_INPUT, SQL_INSERT_SEQUENCED_DIRECT_INPUT,
414-
SQL_INSERT_SEQUENCED_USER_OP, SQL_INSERT_USER_OP, sql_insert_open_batch,
410+
SQL_INSERT_USER_OP, sql_insert_open_batch,
415411
sql_insert_open_batch_with_index, sql_insert_open_frame, sql_insert_safe_inputs_batch,
416-
sql_insert_sequenced_direct_inputs_for_frame, sql_insert_user_ops_and_sequenced_batch,
412+
sql_insert_sequenced_direct_inputs_for_frame, sql_insert_user_ops_batch,
417413
sql_select_batch_policy, sql_select_frames_for_batch, sql_select_latest_batch_index,
418414
sql_select_latest_batch_with_user_op_count, sql_select_max_safe_input_index,
419415
sql_select_ordered_l2_tx_count, sql_select_ordered_l2_txs_from_offset,
@@ -561,13 +557,12 @@ mod tests {
561557
],
562558
)
563559
.expect("insert user op");
560+
// The trg_sequence_user_op trigger automatically inserts the sequenced row.
564561
conn.execute(
565562
SQL_INSERT_SAFE_INPUT,
566563
params![0_i64, vec![0x11_u8; 20], vec![0xaa_u8], 10_i64],
567564
)
568565
.expect("insert direct input");
569-
conn.execute(SQL_INSERT_SEQUENCED_USER_OP, params![0_i64, 0_i64, 0_i64])
570-
.expect("insert sequenced user op");
571566
conn.execute(
572567
SQL_INSERT_SEQUENCED_DIRECT_INPUT,
573568
params![0_i64, 0_i64, 0_i64],
@@ -758,7 +753,7 @@ mod tests {
758753
sample_pending_user_op(0x20, 0, 1),
759754
sample_pending_user_op(0x21, 1, 1),
760755
];
761-
sql_insert_user_ops_and_sequenced_batch(&tx, 0, 0, 0, user_ops.as_slice())
756+
sql_insert_user_ops_batch(&tx, 0, 0, 0, user_ops.as_slice())
762757
.expect("insert user ops + sequenced batch");
763758

764759
sql_insert_sequenced_direct_inputs_for_frame(

0 commit comments

Comments
 (0)