Skip to content

Commit

Permalink
fix(gas-oracle): fetch block header (#984)
Browse files Browse the repository at this point in the history
Co-authored-by: colinlyguo <colinlyguo@users.noreply.github.com>
  • Loading branch information
colinlyguo and colinlyguo authored Oct 10, 2023
1 parent d494f44 commit 25d5fab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
2 changes: 1 addition & 1 deletion common/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"runtime/debug"
)

var tag = "v4.3.33"
var tag = "v4.3.34"

var commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {
Expand Down
56 changes: 22 additions & 34 deletions rollup/internal/controller/watcher/l1_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package watcher

import (
"context"
"errors"
"math/big"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -116,52 +117,39 @@ func (w *L1WatcherClient) SetConfirmations(confirmations rpc.BlockNumber) {
// FetchBlockHeader pull latest L1 blocks and save in DB
func (w *L1WatcherClient) FetchBlockHeader(blockHeight uint64) error {
w.metrics.l1WatcherFetchBlockHeaderTotal.Inc()
fromBlock := int64(w.processedBlockHeight) + 1
toBlock := int64(blockHeight)
if toBlock < fromBlock {
return nil

var block *gethTypes.Header
block, err := w.client.HeaderByNumber(w.ctx, big.NewInt(int64(blockHeight)))
if err != nil {
log.Warn("Failed to get block", "height", blockHeight, "err", err)
return err
}
if toBlock > fromBlock+contractEventsBlocksFetchLimit {
toBlock = fromBlock + contractEventsBlocksFetchLimit - 1

if block == nil {
log.Warn("Received nil block", "height", blockHeight)
return errors.New("received nil block")
}

var blocks []orm.L1Block
var err error
height := fromBlock
for ; height <= toBlock; height++ {
var block *gethTypes.Header
block, err = w.client.HeaderByNumber(w.ctx, big.NewInt(height))
if err != nil {
log.Warn("Failed to get block", "height", height, "err", err)
break
}
var baseFee uint64
if block.BaseFee != nil {
baseFee = block.BaseFee.Uint64()
}
blocks = append(blocks, orm.L1Block{
Number: uint64(height),
Hash: block.Hash().String(),
BaseFee: baseFee,
GasOracleStatus: int16(types.GasOraclePending),
})
var baseFee uint64
if block.BaseFee != nil {
baseFee = block.BaseFee.Uint64()
}

// failed at first block, return with the error
if height == fromBlock {
return err
l1Block := orm.L1Block{
Number: blockHeight,
Hash: block.Hash().String(),
BaseFee: baseFee,
GasOracleStatus: int16(types.GasOraclePending),
}
toBlock = height - 1

// insert succeed blocks
err = w.l1BlockOrm.InsertL1Blocks(w.ctx, blocks)
err = w.l1BlockOrm.InsertL1Blocks(w.ctx, []orm.L1Block{l1Block})
if err != nil {
log.Warn("Failed to insert L1 block to db", "fromBlock", fromBlock, "toBlock", toBlock, "err", err)
log.Warn("Failed to insert L1 block to db", "blockHeight", blockHeight, "err", err)
return err
}

// update processed height
w.processedBlockHeight = uint64(toBlock)
w.processedBlockHeight = blockHeight
w.metrics.l1WatcherFetchBlockHeaderProcessedBlockHeight.Set(float64(w.processedBlockHeight))
return nil
}
Expand Down

0 comments on commit 25d5fab

Please sign in to comment.