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

Feature/#1506 forced batch #1523

Merged
merged 7 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 15 additions & 12 deletions state/pgstatestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const (
addBlockSQL = "INSERT INTO state.block (block_num, block_hash, parent_hash, received_at) VALUES ($1, $2, $3, $4)"
getLastBlockSQL = "SELECT block_num, block_hash, parent_hash, received_at FROM state.block ORDER BY block_num DESC LIMIT 1"
getPreviousBlockSQL = "SELECT block_num, block_hash, parent_hash, received_at FROM state.block ORDER BY block_num DESC LIMIT 1 OFFSET $1"
resetSQL = "DELETE FROM state.block WHERE block_num > $1"
resetTrustedStateSQL = "DELETE FROM state.batch WHERE batch_num > $1"
addVerifiedBatchSQL = "INSERT INTO state.verified_batch (block_num, batch_num, tx_hash, aggregator, state_root) VALUES ($1, $2, $3, $4, $5)"
getVerifiedBatchSQL = "SELECT block_num, batch_num, tx_hash, aggregator, state_root FROM state.verified_batch WHERE batch_num = $1"
getLastBatchNumberSQL = "SELECT batch_num FROM state.batch ORDER BY batch_num DESC LIMIT 1"
Expand All @@ -44,7 +42,6 @@ const (
getTransactionHashesByBatchNumberSQL = "SELECT hash FROM state.transaction t INNER JOIN state.l2block b ON t.l2_block_num = b.block_num WHERE b.batch_num = $1 ORDER BY l2_block_num ASC"
getLastBatchSeenSQL = "SELECT last_batch_num_seen FROM state.sync_info LIMIT 1"
updateLastBatchSeenSQL = "UPDATE state.sync_info SET last_batch_num_seen = $1"
resetTrustedBatchSQL = "DELETE FROM state.batch WHERE batch_num > $1"
isBatchClosedSQL = "SELECT global_exit_root IS NOT NULL AND state_root IS NOT NULL FROM state.batch WHERE batch_num = $1 LIMIT 1"
addGenesisBatchSQL = "INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, acc_input_hash, state_root, timestamp, coinbase, raw_txs_data) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"
openBatchSQL = "INSERT INTO state.batch (batch_num, global_exit_root, timestamp, coinbase) VALUES ($1, $2, $3, $4)"
Expand Down Expand Up @@ -112,21 +109,34 @@ func (p *PostgresStorage) getExecQuerier(dbTx pgx.Tx) execQuerier {
// Reset resets the state to a block for the given DB tx
func (p *PostgresStorage) Reset(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) error {
ARR552 marked this conversation as resolved.
Show resolved Hide resolved
e := p.getExecQuerier(dbTx)
const resetForcedBatchSQL = `
UPDATE state.forced_batch
SET batch_num = null
FROM state.virtual_batch
INNER JOIN state.block ON state.block.block_num = state.virtual_batch.block_num
ARR552 marked this conversation as resolved.
Show resolved Hide resolved
WHERE state.block.block_num > $1 AND state.forced_batch.batch_num = state.virtual_batch.batch_num;
`
if _, err := e.Exec(ctx, resetForcedBatchSQL, blockNumber); err != nil {
return err
}

const resetSQL = "DELETE FROM state.block WHERE block_num > $1"
if _, err := e.Exec(ctx, resetSQL, blockNumber); err != nil {
return err
}
// TODO: Remove consolidations

return nil
}

// ResetTrustedState removes the batches with number greater than the given one
// from the database.
func (p *PostgresStorage) ResetTrustedState(ctx context.Context, batchNum uint64, dbTx pgx.Tx) error {
const resetTrustedStateSQL = "DELETE FROM state.batch WHERE batch_num > $1"
e := p.getExecQuerier(dbTx)
if _, err := e.Exec(ctx, resetTrustedStateSQL, batchNum); err != nil {
return err
}
// TODO: Remove consolidations
// TODO Find a way to put txs in the pool again
return nil
}

Expand Down Expand Up @@ -836,13 +846,6 @@ func (p *PostgresStorage) GetTxsHashesByBatchNumber(ctx context.Context, batchNu
return txs, nil
}

// ResetTrustedBatch resets the batches which the batch number is higher than the input.
func (p *PostgresStorage) ResetTrustedBatch(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) error {
e := p.getExecQuerier(dbTx)
_, err := e.Exec(ctx, resetTrustedBatchSQL, batchNumber)
return err
}

// AddVirtualBatch adds a new virtual batch to the storage.
func (p *PostgresStorage) AddVirtualBatch(ctx context.Context, virtualBatch *VirtualBatch, dbTx pgx.Tx) error {
e := p.getExecQuerier(dbTx)
Expand Down
11 changes: 3 additions & 8 deletions synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
BatchL2Data: sbatch.Transactions,
}
// ForcedBatch must be processed
if sbatch.MinForcedTimestamp > 0 {
if sbatch.MinForcedTimestamp > 0 { // If this is true means that the batch is forced
// Read forcedBatches from db
forcedBatches, err := s.state.GetNextForcedBatches(s.ctx, 1, dbTx)
if err != nil {
Expand All @@ -581,20 +581,17 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
log.Errorf("error rolling back state. BatchNumber: %d, BlockNumber: %d, rollbackErr: %w", sbatch.BatchNumber, blockNumber, rollbackErr)
return rollbackErr
}
log.Errorf("error: empty forcedBatches array read from db. BatchNumber: %d", sbatch.BatchNumber)
return fmt.Errorf("error: empty forcedBatches array read from db. BatchNumber: %d", sbatch.BatchNumber)
}
if uint64(forcedBatches[0].ForcedAt.Unix()) != sbatch.MinForcedTimestamp ||
forcedBatches[0].GlobalExitRoot != sbatch.GlobalExitRoot ||
common.Bytes2Hex(forcedBatches[0].RawTxsData) != common.Bytes2Hex(sbatch.Transactions) ||
forcedBatches[0].Sequencer != sbatch.Coinbase {
common.Bytes2Hex(forcedBatches[0].RawTxsData) != common.Bytes2Hex(sbatch.Transactions) {
log.Errorf("error: forcedBatch received doesn't match with the next expected forcedBatch stored in db. Expected: %+v, Synced: %+v", forcedBatches, sbatch)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Errorf("error rolling back state. BatchNumber: %d, BlockNumber: %d, rollbackErr: %w", virtualBatch.BatchNumber, blockNumber, rollbackErr)
return rollbackErr
}
log.Errorf("error: forcedBatch received doesn't match with the next expected forcedBatch stored in db. Expected: %+v, Synced: %+v", forcedBatches, sbatch)
return fmt.Errorf("error: forcedBatch received doesn't match with the next expected forcedBatch stored in db. Expected: %+v, Synced: %+v", forcedBatches, sbatch)
}
// Store batchNumber in forced_batch table
Expand Down Expand Up @@ -758,15 +755,13 @@ func (s *ClientSynchronizer) processSequenceForceBatch(sequenceForceBatch []ethe
for i, fbatch := range sequenceForceBatch {
if uint64(forcedBatches[i].ForcedAt.Unix()) != fbatch.MinForcedTimestamp ||
forcedBatches[i].GlobalExitRoot != fbatch.GlobalExitRoot ||
common.Bytes2Hex(forcedBatches[i].RawTxsData) != common.Bytes2Hex(fbatch.Transactions) ||
forcedBatches[i].Sequencer != fbatch.Coinbase {
common.Bytes2Hex(forcedBatches[i].RawTxsData) != common.Bytes2Hex(fbatch.Transactions) {
log.Errorf("error: forcedBatch received doesn't match with the next expected forcedBatch stored in db. Expected: %+v, Synced: %+v", forcedBatches[i], fbatch)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Errorf("error rolling back state. BatchNumber: %d, BlockNumber: %d, rollbackErr: %w", fbatch.BatchNumber, block.BlockNumber, rollbackErr)
return rollbackErr
}
log.Errorf("error: forcedBatch received doesn't match with the next expected forcedBatch stored in db. Expected: %+v, Synced: %+v", forcedBatches[i], fbatch)
return fmt.Errorf("error: forcedBatch received doesn't match with the next expected forcedBatch stored in db. Expected: %+v, Synced: %+v", forcedBatches[i], fbatch)
}
virtualBatch := state.VirtualBatch{
Expand Down