Skip to content

Commit

Permalink
Synchronizer accept forkids that are the same as in database (#3452)
Browse files Browse the repository at this point in the history
* #3451 accept same forkid from L1
* if same forkid recived and is the last one and same FromBatchNumber update blockNumber
  • Loading branch information
joanestebanr authored Mar 13, 2024
1 parent 5361315 commit 8ffd1dc
Show file tree
Hide file tree
Showing 10 changed files with 822 additions and 60 deletions.
3 changes: 2 additions & 1 deletion state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ type storage interface {
GetBatchByForcedBatchNum(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (*Batch, error)
AddForkID(ctx context.Context, forkID ForkIDInterval, dbTx pgx.Tx) error
GetForkIDs(ctx context.Context, dbTx pgx.Tx) ([]ForkIDInterval, error)
UpdateForkID(ctx context.Context, forkID ForkIDInterval, dbTx pgx.Tx) error
UpdateForkIDToBatchNumber(ctx context.Context, forkID ForkIDInterval, dbTx pgx.Tx) error
UpdateForkIDBlockNumber(ctx context.Context, forkdID uint64, newBlockNumber uint64, updateMemCache bool, dbTx pgx.Tx) error
GetNativeBlockHashesInRange(ctx context.Context, fromBlock, toBlock uint64, dbTx pgx.Tx) ([]common.Hash, error)
GetDSGenesisBlock(ctx context.Context, dbTx pgx.Tx) (*DSL2Block, error)
GetDSBatches(ctx context.Context, firstBatchNumber, lastBatchNumber uint64, readWIPBatch bool, dbTx pgx.Tx) ([]*DSBatch, error)
Expand Down
82 changes: 66 additions & 16 deletions state/mocks/mock_storage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions state/pgstatestorage/forkid.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (p *PostgresStorage) GetForkIDs(ctx context.Context, dbTx pgx.Tx) ([]state.
}

// UpdateForkID updates the forkID stored in db
func (p *PostgresStorage) UpdateForkID(ctx context.Context, forkID state.ForkIDInterval, dbTx pgx.Tx) error {
func (p *PostgresStorage) UpdateForkIDToBatchNumber(ctx context.Context, forkID state.ForkIDInterval, dbTx pgx.Tx) error {
const updateForkIDSQL = "UPDATE state.fork_id SET to_batch_num = $1 WHERE fork_id = $2"
e := p.getExecQuerier(dbTx)
if _, err := e.Exec(ctx, updateForkIDSQL, forkID.ToBatchNumber, forkID.ForkId); err != nil {
Expand All @@ -60,6 +60,25 @@ func (p *PostgresStorage) UpdateForkID(ctx context.Context, forkID state.ForkIDI
return nil
}

// UpdateForkID updates the forkID stored in db
func (p *PostgresStorage) UpdateForkIDBlockNumber(ctx context.Context, forkdID uint64, newBlockNumber uint64, updateMemCache bool, dbTx pgx.Tx) error {
const sql = "UPDATE state.fork_id SET block_num = $1 WHERE fork_id = $2"
e := p.getExecQuerier(dbTx)
if _, err := e.Exec(ctx, sql, forkdID, newBlockNumber); err != nil {
return err
}
if updateMemCache {
log.Debugf("Updating forkID %d in memory", forkdID)
forkIDs, err := p.GetForkIDs(ctx, dbTx)
if err != nil {
log.Error("error getting oldForkIDs. Error: ", err)
return err
}
p.UpdateForkIDIntervalsInMemory(forkIDs)
}
return nil
}

// UpdateForkIDIntervalsInMemory updates the forkID intervals in memory
func (p *PostgresStorage) UpdateForkIDIntervalsInMemory(intervals []state.ForkIDInterval) {
log.Infof("Updating forkIDs. Setting %d forkIDs", len(intervals))
Expand Down Expand Up @@ -88,7 +107,7 @@ func (p *PostgresStorage) AddForkIDInterval(ctx context.Context, newForkID state
return err
}
forkIDs[len(forkIDs)-1].ToBatchNumber = newForkID.FromBatchNumber - 1
err := p.UpdateForkID(ctx, forkIDs[len(forkIDs)-1], dbTx)
err := p.UpdateForkIDToBatchNumber(ctx, forkIDs[len(forkIDs)-1], dbTx)
if err != nil {
log.Errorf("error updating forkID: %d. Error: %v", forkIDs[len(forkIDs)-1].ForkId, err)
return err
Expand Down
2 changes: 1 addition & 1 deletion state/pgstatestorage/pgstatestorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ func TestForkIDs(t *testing.T) {
require.Equal(t, forks[i].Version, forkId.Version)
}
forkID3.ToBatchNumber = 18446744073709551615
err = testState.UpdateForkID(ctx, forkID3, dbTx)
err = testState.UpdateForkIDToBatchNumber(ctx, forkID3, dbTx)
require.NoError(t, err)

forkIDs, err = testState.GetForkIDs(ctx, dbTx)
Expand Down
Loading

0 comments on commit 8ffd1dc

Please sign in to comment.