Skip to content

Commit

Permalink
fix gas station (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
wanliqun authored Jan 2, 2025
1 parent d996862 commit 22a3a88
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
15 changes: 13 additions & 2 deletions rpc/handler/cfx_gasstation.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (h *CfxGasStationHandler) trySync(cfx sdk.ClientOperator) (bool, error) {
return false, err
}

if latestEpoch == nil { // This shouldn't happen, but just in case...
if latestEpoch == nil {
return false, errors.New("latest epoch is nil")
}

Expand All @@ -127,7 +127,7 @@ func (h *CfxGasStationHandler) trySync(cfx sdk.ClientOperator) (bool, error) {
return false, err
}

if pivotBlock == nil { // This shouldn't happen, but just in case...
if pivotBlock == nil {
return false, errors.New("pivot block is nil")
}

Expand Down Expand Up @@ -172,6 +172,10 @@ func (h *CfxGasStationHandler) fetchBlocks(
return nil, nil, err
}

if len(blockHashes) == 0 {
return nil, nil, errors.New("empty block hashes")
}

pivotHash := blockHashes[len(blockHashes)-1]
if pivotBlock.Hash != pivotHash { // abandon this epoch due to pivot switched
return nil, nil, errors.New("pivot switched")
Expand All @@ -183,6 +187,9 @@ func (h *CfxGasStationHandler) fetchBlocks(
if err != nil {
return nil, nil, err
}
if block.GasUsed == nil {
return nil, nil, errors.New("unexecuted block")
}
blocks = append(blocks, &block)
}

Expand All @@ -204,6 +211,10 @@ func (h *CfxGasStationHandler) handleReorg() {
}

func (h *CfxGasStationHandler) handleBlock(block *cfxtypes.Block) {
if block.GasLimit == nil || block.GasLimit.ToInt().Int64() == 0 {
return
}

ratio, _ := new(big.Rat).SetFrac(block.GasUsed.ToInt(), block.GasLimit.ToInt()).Float64()
blockFee := &BlockPriorityFee{
number: block.BlockNumber.ToInt().Uint64(),
Expand Down
6 changes: 5 additions & 1 deletion rpc/handler/eth_gasstation.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (h *EthGasStationHandler) trySync(eth *node.Web3goClient) (bool, error) {
return false, err
}

if block == nil { // this shouldn't happen, but just in case...
if block == nil {
return false, errors.New("invalid nil block")
}

Expand Down Expand Up @@ -150,6 +150,10 @@ func (h *EthGasStationHandler) handleReorg() {
}

func (h *EthGasStationHandler) handleBlock(block *ethtypes.Block) {
if block.GasLimit == 0 {
return
}

ratio := float64(block.GasUsed) / float64(block.GasLimit)
blockFee := &BlockPriorityFee{
number: block.Number.Uint64(),
Expand Down
12 changes: 10 additions & 2 deletions rpc/handler/gasstation.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (w *PriorityFeeWindow) Push(blockFee *BlockPriorityFee) {
w.mu.Lock()
defer w.mu.Unlock()

if _, ok := w.hashToFee[blockFee.hash]; ok { // Block already exists
if _, ok := w.hashToFee[blockFee.hash]; ok { // Block already exists?
return
}

Expand All @@ -223,7 +223,11 @@ func (w *PriorityFeeWindow) Push(blockFee *BlockPriorityFee) {

// If the window is full, prune the oldest block.
for w.feeChain.Len() > w.capacity {
w.feeChain.Remove(w.feeChain.Front())
e := w.feeChain.Front()
blockFee := e.Value.(*BlockPriorityFee)

delete(w.hashToFee, blockFee.hash)
w.feeChain.Remove(e)
}
}

Expand All @@ -245,6 +249,10 @@ func (w *PriorityFeeWindow) insertBlock(blockFee *BlockPriorityFee) {
}

func (w *PriorityFeeWindow) updateHistoricalBaseFeeRange(blockFee *BlockPriorityFee) {
if blockFee.baseFee == nil {
return
}

// Update the historical base fee range
if w.historicalBaseFeeRanges == nil { // initial setup
w.historicalBaseFeeRanges = []*big.Int{
Expand Down

0 comments on commit 22a3a88

Please sign in to comment.