Skip to content

Commit

Permalink
add seen at field to virtual batch
Browse files Browse the repository at this point in the history
  • Loading branch information
nomaxg committed Jan 5, 2024
1 parent e84d96d commit 0c35067
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions db/migrations/state/0001.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions state/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type VirtualBatch struct {
Coinbase common.Address
SequencerAddr common.Address
BlockNumber uint64
SeenAt uint64
}

// Sequence represents the sequence interval
Expand Down
8 changes: 4 additions & 4 deletions state/pgstatestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0c35067

Please sign in to comment.