Skip to content

Commit

Permalink
fix parentHash when storing a new L2 block (#3171)
Browse files Browse the repository at this point in the history
  • Loading branch information
agnusmor authored Jan 30, 2024
1 parent f5346fa commit efbd360
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
1 change: 1 addition & 0 deletions state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type storage interface {
GetBatchNumberOfL2Block(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (uint64, error)
BatchNumberByL2BlockNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (uint64, error)
GetL2BlockByNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (*L2Block, error)
GetL2BlockHashByNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetL2BlocksByBatchNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) ([]L2Block, error)
GetLastL2BlockCreatedAt(ctx context.Context, dbTx pgx.Tx) (*time.Time, error)
GetTransactionByHash(ctx context.Context, transactionHash common.Hash, dbTx pgx.Tx) (*types.Transaction, error)
Expand Down
60 changes: 60 additions & 0 deletions state/mocks/mock_storage.go

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

21 changes: 21 additions & 0 deletions state/pgstatestorage/l2block.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,27 @@ func (p *PostgresStorage) GetL2BlockHeaderByNumber(ctx context.Context, blockNum
return header, nil
}

// GetL2BlockHashByNumber gets the block hash by block number
func (p *PostgresStorage) GetL2BlockHashByNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (common.Hash, error) {
const getL2BlockHeaderByNumberSQL = "SELECT block_hash FROM state.l2block b WHERE b.block_num = $1"

blockHash := state.ZeroHash

var blockHashStr string
q := p.getExecQuerier(dbTx)
err := q.QueryRow(ctx, getL2BlockHeaderByNumberSQL, blockNumber).Scan(&blockHashStr)

if errors.Is(err, pgx.ErrNoRows) {
return blockHash, state.ErrNotFound
} else if err != nil {
return blockHash, err
}

blockHash = common.HexToHash(blockHashStr)

return blockHash, nil
}

// GetL2BlockHashesSince gets the block hashes added since the provided date
func (p *PostgresStorage) GetL2BlockHashesSince(ctx context.Context, since time.Time, dbTx pgx.Tx) ([]common.Hash, error) {
const getL2BlockHashesSinceSQL = "SELECT block_hash FROM state.l2block WHERE created_at >= $1"
Expand Down
9 changes: 7 additions & 2 deletions state/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,16 @@ func (s *State) StoreL2Block(ctx context.Context, batchNumber uint64, l2Block *P
log.Debugf("storing l2 block %d, txs %d, hash %s", l2Block.BlockNumber, len(l2Block.TransactionResponses), l2Block.BlockHash.String())
start := time.Now()

prevL2BlockHash, err := s.GetL2BlockHashByNumber(ctx, l2Block.BlockNumber-1, dbTx)
if err != nil {
return err
}

header := &types.Header{
Number: new(big.Int).SetUint64(l2Block.BlockNumber),
ParentHash: l2Block.ParentHash,
ParentHash: prevL2BlockHash,
Coinbase: l2Block.Coinbase,
Root: l2Block.BlockHash, //BlockHash is the StateRoot in Etrog
Root: l2Block.BlockHash, //BlockHash returned by the executor is the StateRoot in Etrog
GasUsed: l2Block.GasUsed,
GasLimit: s.cfg.MaxCumulativeGasUsed,
Time: l2Block.Timestamp,
Expand Down

0 comments on commit efbd360

Please sign in to comment.