Skip to content

Commit

Permalink
fix sync_info table (0xPolygonHermez#2055)
Browse files Browse the repository at this point in the history
* fix

* linter

* merge set last batch info seen methods

---------

Co-authored-by: Thiago Lemos <[email protected]>
  • Loading branch information
ARR552 and tclemos authored May 4, 2023
1 parent 123fe95 commit 540ae7f
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 63 deletions.
45 changes: 14 additions & 31 deletions state/pgstatestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,32 +542,27 @@ func (p *PostgresStorage) GetLatestVirtualBatchTimestamp(ctx context.Context, db
return timestamp, nil
}

// SetLastBatchNumberSeenOnEthereum sets the last batch number that affected
// the roll-up in order to allow the components to know if the state
// is synchronized or not
func (p *PostgresStorage) SetLastBatchNumberSeenOnEthereum(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) error {
const updateLastBatchSeenSQL = "UPDATE state.sync_info SET last_batch_num_seen = $1"
// SetLastBatchInfoSeenOnEthereum sets the last batch number that affected
// the roll-up and the last batch number that was consolidated on ethereum
// in order to allow the components to know if the state is synchronized or not
func (p *PostgresStorage) SetLastBatchInfoSeenOnEthereum(ctx context.Context, lastBatchNumberSeen, lastBatchNumberVerified uint64, dbTx pgx.Tx) error {
const query = `
UPDATE state.sync_info
SET last_batch_num_seen = $1
, last_batch_num_consolidated = $2`

e := p.getExecQuerier(dbTx)
_, err := e.Exec(ctx, updateLastBatchSeenSQL, batchNumber)
_, err := e.Exec(ctx, query, lastBatchNumberSeen, lastBatchNumberVerified)
return err
}

// GetLastBatchNumberSeenOnEthereum returns the last batch number stored
// in the state that represents the last batch number that affected the
// roll-up in the Ethereum network.
func (p *PostgresStorage) GetLastBatchNumberSeenOnEthereum(ctx context.Context, dbTx pgx.Tx) (uint64, error) {
var batchNumber uint64
const getLastBatchSeenSQL = "SELECT last_batch_num_seen FROM state.sync_info LIMIT 1"
// SetInitSyncBatch sets the initial batch number where the synchronization started
func (p *PostgresStorage) SetInitSyncBatch(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) error {
updateInitBatchSQL := "UPDATE state.sync_info SET init_sync_batch = $1"

e := p.getExecQuerier(dbTx)
err := e.QueryRow(ctx, getLastBatchSeenSQL).Scan(&batchNumber)

if err != nil {
return 0, err
}

return batchNumber, nil
_, err := e.Exec(ctx, updateInitBatchSQL, batchNumber)
return err
}

// GetBatchByNumber returns the batch with the given number.
Expand Down Expand Up @@ -1575,18 +1570,6 @@ func (p *PostgresStorage) GetLastL2Block(ctx context.Context, dbTx pgx.Tx) (*typ
return block, nil
}

// GetLastVerifiedBatchNumberSeenOnEthereum gets last verified batch number seen on ethereum
func (p *PostgresStorage) GetLastVerifiedBatchNumberSeenOnEthereum(ctx context.Context, dbTx pgx.Tx) (uint64, error) {
const getLastVerifiedBatchSeenSQL = "SELECT last_batch_num_verified FROM state.sync_info LIMIT 1"
var batchNumber uint64
e := p.getExecQuerier(dbTx)
err := e.QueryRow(ctx, getLastVerifiedBatchSeenSQL).Scan(&batchNumber)
if err != nil {
return 0, err
}
return batchNumber, nil
}

// GetLastVerifiedBatch gets last verified batch
func (p *PostgresStorage) GetLastVerifiedBatch(ctx context.Context, dbTx pgx.Tx) (*VerifiedBatch, error) {
const query = "SELECT block_num, batch_num, tx_hash, aggregator FROM state.verified_batch ORDER BY batch_num DESC LIMIT 1"
Expand Down
4 changes: 3 additions & 1 deletion synchronizer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ethermanInterface interface {
GetTrustedSequencerURL() (string, error)
VerifyGenBlockNumber(ctx context.Context, genBlockNumber uint64) (bool, error)
GetForks(ctx context.Context) ([]state.ForkIDInterval, error)
GetLatestVerifiedBatchNum() (uint64, error)
}

// stateInterface gathers the methods required to interact with the state.
Expand Down Expand Up @@ -56,7 +57,8 @@ type stateInterface interface {
ResetForkID(ctx context.Context, batchNumber, forkID uint64, version string, dbTx pgx.Tx) error
GetForkIDTrustedReorgCount(ctx context.Context, forkID uint64, version string, dbTx pgx.Tx) (uint64, error)
UpdateForkIDIntervals(intervals []state.ForkIDInterval)

SetLastBatchInfoSeenOnEthereum(ctx context.Context, lastBatchNumberSeen, lastBatchNumberVerified uint64, dbTx pgx.Tx) error
SetInitSyncBatch(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) error
BeginStateTransaction(ctx context.Context) (pgx.Tx, error)
}

Expand Down
24 changes: 24 additions & 0 deletions synchronizer/mock_etherman.go

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

28 changes: 28 additions & 0 deletions synchronizer/mock_state.go

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

85 changes: 76 additions & 9 deletions synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ func (s *ClientSynchronizer) Sync() error {
log.Info("Sync started")
dbTx, err := s.state.BeginStateTransaction(s.ctx)
if err != nil {
log.Fatalf("error creating db transaction to get latest block")
log.Errorf("error creating db transaction to get latest block. Error: %v", err)
return err
}
lastEthBlockSynced, err := s.state.GetLastBlock(s.ctx, dbTx)
if err != nil {
Expand All @@ -84,15 +85,31 @@ func (s *ClientSynchronizer) Sync() error {
valid, err := s.etherMan.VerifyGenBlockNumber(s.ctx, s.genesis.GenesisBlockNum)
if err != nil {
log.Error("error checking genesis block number. Error: ", err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Errorf("error rolling back state. RollbackErr: %v, err: %s", rollbackErr, err.Error())
return rollbackErr
}
return err
} else if !valid {
log.Error("genesis Block number configured is not valid. It is required the block number where the PolygonZkEVM smc was deployed")
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Errorf("error rolling back state. RollbackErr: %v", rollbackErr)
return rollbackErr
}
return fmt.Errorf("genesis Block number configured is not valid. It is required the block number where the PolygonZkEVM smc was deployed")
}
log.Info("Setting genesis block")
header, err := s.etherMan.HeaderByNumber(s.ctx, big.NewInt(0).SetUint64(s.genesis.GenesisBlockNum))
if err != nil {
log.Fatal("error getting l1 block header for block ", s.genesis.GenesisBlockNum, " : ", err)
log.Errorf("error getting l1 block header for block %d. Error: %v", s.genesis.GenesisBlockNum, err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Errorf("error rolling back state. RollbackErr: %v, err: %s", rollbackErr, err.Error())
return rollbackErr
}
return err
}
lastEthBlockSynced = &state.Block{
BlockNumber: header.Number.Uint64(),
Expand All @@ -102,26 +119,64 @@ func (s *ClientSynchronizer) Sync() error {
}
newRoot, err := s.state.SetGenesis(s.ctx, *lastEthBlockSynced, s.genesis, dbTx)
if err != nil {
log.Fatal("error setting genesis: ", err)
log.Error("error setting genesis: ", err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Errorf("error rolling back state. RollbackErr: %v, err: %s", rollbackErr, err.Error())
return rollbackErr
}
return err
}
var root common.Hash
root.SetBytes(newRoot)
if root != s.genesis.Root {
log.Fatal("Calculated newRoot should be ", s.genesis.Root, " instead of ", root)
log.Errorf("Calculated newRoot should be %s instead of %s", s.genesis.Root.String(), root.String())
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Errorf("error rolling back state. RollbackErr: %v", rollbackErr)
return rollbackErr
}
return fmt.Errorf("Calculated newRoot should be %s instead of %s", s.genesis.Root.String(), root.String())
}
log.Debug("Genesis root matches!")
} else {
log.Fatal("unexpected error getting the latest ethereum block. Error: ", err)
log.Error("unexpected error getting the latest ethereum block. Error: ", err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Errorf("error rolling back state. RollbackErr: %v, err: %s", rollbackErr, err.Error())
return rollbackErr
}
return err
}
}
initBatchNumber, err := s.state.GetLastBatchNumber(s.ctx, dbTx)
if err != nil {
log.Error("error getting latest batchNumber synced. Error: ", err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Errorf("error rolling back state. RollbackErr: %v, err: %s", rollbackErr, err.Error())
return rollbackErr
}
return err
}
err = s.state.SetInitSyncBatch(s.ctx, initBatchNumber, dbTx)
if err != nil {
log.Error("error setting initial batch number. Error: ", err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Errorf("error rolling back state. RollbackErr: %v, err: %s", rollbackErr, err.Error())
return rollbackErr
}
return err
}
if err := dbTx.Commit(s.ctx); err != nil {
log.Errorf("error committing dbTx, err: %v", err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Fatalf("error rolling back state. RollbackErr: %s, err: %v",
rollbackErr.Error(), err)
log.Errorf("error rolling back state. RollbackErr: %v, err: %s", rollbackErr, err.Error())
return rollbackErr
}
log.Fatalf("error committing dbTx, err: %v", err)
return err
}

for {
Expand All @@ -136,9 +191,21 @@ func (s *ClientSynchronizer) Sync() error {
}
latestSyncedBatch, err := s.state.GetLastBatchNumber(s.ctx, nil)
if err != nil {
log.Warn("error getting latest batch synced. Error: ", err)
log.Warn("error getting latest batch synced in the db. Error: ", err)
continue
}
// Check the latest verified Batch number in the smc
lastVerifiedBatchNumber, err := s.etherMan.GetLatestVerifiedBatchNum()
if err != nil {
log.Warn("error getting last verified batch in the rollup. Error: ", err)
continue
}
err = s.state.SetLastBatchInfoSeenOnEthereum(s.ctx, latestSequencedBatchNumber, lastVerifiedBatchNumber, nil)
if err != nil {
log.Warn("error setting latest batch info into db. Error: ", err)
continue
}

// Sync trusted state
if latestSyncedBatch >= latestSequencedBatchNumber {
log.Info("L1 state fully synchronized")
Expand Down
Loading

0 comments on commit 540ae7f

Please sign in to comment.