From 0c35067036a7bdb23dac9740965955df2ecb5096 Mon Sep 17 00:00:00 2001 From: nomaxg Date: Fri, 5 Jan 2024 10:04:27 -0500 Subject: [PATCH] add seen at field to virtual batch --- db/migrations/state/0001.sql | 1 + state/batch.go | 1 + state/pgstatestorage.go | 8 ++++---- synchronizer/synchronizer.go | 8 ++++---- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/db/migrations/state/0001.sql b/db/migrations/state/0001.sql index f09e06d..77d7d5c 100644 --- a/db/migrations/state/0001.sql +++ b/db/migrations/state/0001.sql @@ -41,6 +41,7 @@ CREATE TABLE state.virtual_batch tx_hash VARCHAR, coinbase VARCHAR, block_num BIGINT NOT NULL REFERENCES state.block (block_num) ON DELETE CASCADE + seen_at BIGINT NOT NULL REFERENCES state.block (block_num) ON DELETE CASCADE ); CREATE TABLE state.verified_batch diff --git a/state/batch.go b/state/batch.go index 82c1f5d..3c0a345 100644 --- a/state/batch.go +++ b/state/batch.go @@ -58,6 +58,7 @@ type VirtualBatch struct { Coinbase common.Address SequencerAddr common.Address BlockNumber uint64 + SeenAt uint64 } // Sequence represents the sequence interval diff --git a/state/pgstatestorage.go b/state/pgstatestorage.go index 28a8a33..1ef059b 100644 --- a/state/pgstatestorage.go +++ b/state/pgstatestorage.go @@ -18,8 +18,8 @@ import ( const maxTopics = 4 const ( - addGlobalExitRootSQL = "INSERT INTO state.exit_root (block_num, timestamp, mainnet_exit_root, rollup_exit_root, global_exit_root) VALUES ($1, $2, $3, $4, $5)" - getLatestExitRootBlockNumSQL = "SELECT block_num FROM state.exit_root ORDER BY id DESC LIMIT 1" + addGlobalExitRootSQL = "INSERT INTO state.exit_root (block_num, timestamp, mainnet_exit_root, rollup_exit_root, global_exit_root) VALUES ($1, $2, $3, $4, $5)" + getLatestExitRootBlockNumSQL = "SELECT block_num FROM state.exit_root ORDER BY id DESC LIMIT 1" // When using preconfirmations, there are two streams that might add an L1 block: the L1 // synchronizer and the preconfirmations synchronizer. We need this insert statement not to fail // when one stream tries to insert a block that the other stream has already added. Hence, the @@ -920,9 +920,9 @@ func (p *PostgresStorage) GetTxsHashesByBatchNumber(ctx context.Context, batchNu // AddVirtualBatch adds a new virtual batch to the storage. func (p *PostgresStorage) AddVirtualBatch(ctx context.Context, virtualBatch *VirtualBatch, dbTx pgx.Tx) error { - const addVirtualBatchSQL = "INSERT INTO state.virtual_batch (batch_num, tx_hash, coinbase, block_num, sequencer_addr) VALUES ($1, $2, $3, $4, $5)" + const addVirtualBatchSQL = "INSERT INTO state.virtual_batch (batch_num, tx_hash, coinbase, block_num, seen_at, sequencer_addr) VALUES ($1, $2, $3, $4, $5, $6)" e := p.getExecQuerier(dbTx) - _, err := e.Exec(ctx, addVirtualBatchSQL, virtualBatch.BatchNumber, virtualBatch.TxHash.String(), virtualBatch.Coinbase.String(), virtualBatch.BlockNumber, virtualBatch.SequencerAddr.String()) + _, err := e.Exec(ctx, addVirtualBatchSQL, virtualBatch.BatchNumber, virtualBatch.TxHash.String(), virtualBatch.Coinbase.String(), virtualBatch.BlockNumber, virtualBatch.SeenAt, virtualBatch.SequencerAddr.String()) return err } diff --git a/synchronizer/synchronizer.go b/synchronizer/synchronizer.go index 2f9f65e..cc70188 100644 --- a/synchronizer/synchronizer.go +++ b/synchronizer/synchronizer.go @@ -446,7 +446,7 @@ func (s *ClientSynchronizer) processBlockRange(blocks []etherman.Block, order ma for _, element := range order[blocks[i].BlockHash] { switch element.Name { case etherman.SequenceBatchesOrder: - err = s.processSequenceBatches(blocks[i].SequencedBatches[element.Pos], dbTx) + err = s.processSequenceBatches(blocks[i].SequencedBatches[element.Pos], dbTx, blocks[i].BlockNumber) if err != nil { return err } @@ -491,7 +491,7 @@ func (s *ClientSynchronizer) resetState(blockNumber uint64) error { // that it can resume syncing. defer func() { s.preconfReorg <- nil - } () + }() } dbTx, err := s.state.BeginStateTransaction(s.ctx) @@ -635,7 +635,7 @@ func (s *ClientSynchronizer) checkTrustedState(batch state.Batch, tBatch *state. return false } -func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.SequencedBatch, dbTx pgx.Tx) error { +func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.SequencedBatch, dbTx pgx.Tx, batchesSeenAtBlock uint64) error { if len(sequencedBatches) == 0 { log.Warn("Empty sequencedBatches array detected, ignoring...") return nil @@ -683,10 +683,10 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman. } } - virtualBatch := state.VirtualBatch{ BatchNumber: sbatch.BatchNumber, TxHash: sbatch.TxHash, + SeenAt: batchesSeenAtBlock, Coinbase: sbatch.Coinbase, BlockNumber: sbatch.BlockNumber, SequencerAddr: sbatch.SequencerAddr,