Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 3575157

Browse files
authored
cherry pick: 3451 synchronizer accept old forkids (#3468)
* Synchronizer accept forkids that are the same as in database (#3452) * #3451 accept same forkid from L1 * if same forkid recived and is the last one and same FromBatchNumber update blockNumber * fix conflicts
1 parent c1bd729 commit 3575157

File tree

11 files changed

+832
-72
lines changed

11 files changed

+832
-72
lines changed

state/interfaces.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ type storage interface {
121121
GetBatchByForcedBatchNum(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (*Batch, error)
122122
AddForkID(ctx context.Context, forkID ForkIDInterval, dbTx pgx.Tx) error
123123
GetForkIDs(ctx context.Context, dbTx pgx.Tx) ([]ForkIDInterval, error)
124-
UpdateForkID(ctx context.Context, forkID ForkIDInterval, dbTx pgx.Tx) error
124+
UpdateForkIDToBatchNumber(ctx context.Context, forkID ForkIDInterval, dbTx pgx.Tx) error
125+
UpdateForkIDBlockNumber(ctx context.Context, forkdID uint64, newBlockNumber uint64, updateMemCache bool, dbTx pgx.Tx) error
125126
GetNativeBlockHashesInRange(ctx context.Context, fromBlock, toBlock uint64, dbTx pgx.Tx) ([]common.Hash, error)
126127
GetDSGenesisBlock(ctx context.Context, dbTx pgx.Tx) (*DSL2Block, error)
127128
GetDSBatches(ctx context.Context, firstBatchNumber, lastBatchNumber uint64, readWIPBatch bool, dbTx pgx.Tx) ([]*DSBatch, error)

state/mocks/mock_storage.go

Lines changed: 66 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

state/pgstatestorage/forkid.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (p *PostgresStorage) GetForkIDs(ctx context.Context, dbTx pgx.Tx) ([]state.
5151
}
5252

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

63+
// UpdateForkID updates the forkID stored in db
64+
func (p *PostgresStorage) UpdateForkIDBlockNumber(ctx context.Context, forkdID uint64, newBlockNumber uint64, updateMemCache bool, dbTx pgx.Tx) error {
65+
const sql = "UPDATE state.fork_id SET block_num = $1 WHERE fork_id = $2"
66+
e := p.getExecQuerier(dbTx)
67+
if _, err := e.Exec(ctx, sql, forkdID, newBlockNumber); err != nil {
68+
return err
69+
}
70+
if updateMemCache {
71+
log.Debugf("Updating forkID %d in memory", forkdID)
72+
forkIDs, err := p.GetForkIDs(ctx, dbTx)
73+
if err != nil {
74+
log.Error("error getting oldForkIDs. Error: ", err)
75+
return err
76+
}
77+
p.UpdateForkIDIntervalsInMemory(forkIDs)
78+
}
79+
return nil
80+
}
81+
6382
// UpdateForkIDIntervalsInMemory updates the forkID intervals in memory
6483
func (p *PostgresStorage) UpdateForkIDIntervalsInMemory(intervals []state.ForkIDInterval) {
6584
log.Infof("Updating forkIDs. Setting %d forkIDs", len(intervals))
@@ -88,7 +107,7 @@ func (p *PostgresStorage) AddForkIDInterval(ctx context.Context, newForkID state
88107
return err
89108
}
90109
forkIDs[len(forkIDs)-1].ToBatchNumber = newForkID.FromBatchNumber - 1
91-
err := p.UpdateForkID(ctx, forkIDs[len(forkIDs)-1], dbTx)
110+
err := p.UpdateForkIDToBatchNumber(ctx, forkIDs[len(forkIDs)-1], dbTx)
92111
if err != nil {
93112
log.Errorf("error updating forkID: %d. Error: %v", forkIDs[len(forkIDs)-1].ForkId, err)
94113
return err

state/pgstatestorage/pgstatestorage_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ func TestForkIDs(t *testing.T) {
658658
require.Equal(t, forks[i].Version, forkId.Version)
659659
}
660660
forkID3.ToBatchNumber = 18446744073709551615
661-
err = testState.UpdateForkID(ctx, forkID3, dbTx)
661+
err = testState.UpdateForkIDToBatchNumber(ctx, forkID3, dbTx)
662662
require.NoError(t, err)
663663

664664
forkIDs, err = testState.GetForkIDs(ctx, dbTx)

0 commit comments

Comments
 (0)