Skip to content

Commit

Permalink
correctly handle finalized&safe block number (#605)
Browse files Browse the repository at this point in the history
* better naming

* update eth/api_backend.go

* update eth/gasprice/feehistory.go
  • Loading branch information
0xmountaintop authored Dec 22, 2023
1 parent 44fed24 commit 35b8da7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
4 changes: 2 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ func SetNodeConfig(ctx *cli.Context, cfg *node.Config) {
}
}

func unmarshalBlockNumber(input string) (rpc.BlockNumber, error) {
func unmarshalL1BlockNumber(input string) (rpc.BlockNumber, error) {
switch input {
case "finalized":
return rpc.FinalizedBlockNumber, nil
Expand All @@ -1514,7 +1514,7 @@ func setL1(ctx *cli.Context, cfg *node.Config) {
cfg.L1Endpoint = ctx.String(L1EndpointFlag.Name)
}
if ctx.IsSet(L1ConfirmationsFlag.Name) {
cfg.L1Confirmations, err = unmarshalBlockNumber(ctx.String(L1ConfirmationsFlag.Name))
cfg.L1Confirmations, err = unmarshalL1BlockNumber(ctx.String(L1ConfirmationsFlag.Name))
if err != nil {
panic(fmt.Sprintf("invalid value for flag %s: %s", L1ConfirmationsFlag.Name, ctx.String(L1ConfirmationsFlag.Name)))
}
Expand Down
60 changes: 38 additions & 22 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,27 @@ func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumb
if number == rpc.LatestBlockNumber {
return b.eth.blockchain.CurrentBlock(), nil
}
// if number == rpc.FinalizedBlockNumber {
// block := b.eth.blockchain.CurrentFinalBlock()
// if block == nil {
// return nil, errors.New("finalized block not found")
// }
// return block, nil
// }
// if number == rpc.SafeBlockNumber {
// block := b.eth.blockchain.CurrentSafeBlock()
// if block == nil {
// return nil, errors.New("safe block not found")
// }
// return block, nil
// }
if number == rpc.FinalizedBlockNumber {
block := b.eth.blockchain.CurrentFinalBlock()
if block == nil {
return nil, errors.New("finalized block not found")
}
return block, nil
}
if number == rpc.SafeBlockNumber {
block := b.eth.blockchain.CurrentSafeBlock()
if block == nil {
return nil, errors.New("safe block not found")
finalizedBlockHeightPtr := rawdb.ReadFinalizedL2BlockNumber(b.eth.ChainDb())
if finalizedBlockHeightPtr == nil {
return nil, errors.New("L2 finalized block height not found in database")
}
return block, nil
number = rpc.BlockNumber(*finalizedBlockHeightPtr)
return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil
}
return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil
}
Expand Down Expand Up @@ -129,19 +137,27 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe
header := b.eth.blockchain.CurrentBlock()
return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil
}
// if number == rpc.FinalizedBlockNumber {
// header := b.eth.blockchain.CurrentFinalBlock()
// if header == nil {
// return nil, errors.New("finalized block not found")
// }
// return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil
// }
// if number == rpc.SafeBlockNumber {
// header := b.eth.blockchain.CurrentSafeBlock()
// if header == nil {
// return nil, errors.New("safe block not found")
// }
// return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil
// }
if number == rpc.FinalizedBlockNumber {
header := b.eth.blockchain.CurrentFinalBlock()
if header == nil {
return nil, errors.New("finalized block not found")
}
return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil
}
if number == rpc.SafeBlockNumber {
header := b.eth.blockchain.CurrentSafeBlock()
if header == nil {
return nil, errors.New("safe block not found")
finalizedBlockHeightPtr := rawdb.ReadFinalizedL2BlockNumber(b.eth.ChainDb())
if finalizedBlockHeightPtr == nil {
return nil, errors.New("L2 finalized block height not found in database")
}
return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil
number = rpc.BlockNumber(*finalizedBlockHeightPtr)
return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil
}
return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil
}
Expand Down
4 changes: 2 additions & 2 deletions eth/gasprice/feehistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ func (oracle *Oracle) resolveBlockRange(ctx context.Context, reqEnd rpc.BlockNum
case rpc.LatestBlockNumber:
// Retrieved above.
resolved = headBlock
case rpc.SafeBlockNumber:
resolved, err = oracle.backend.HeaderByNumber(ctx, rpc.SafeBlockNumber)
// case rpc.SafeBlockNumber:
// resolved, err = oracle.backend.HeaderByNumber(ctx, rpc.SafeBlockNumber)
case rpc.FinalizedBlockNumber:
resolved, err = oracle.backend.HeaderByNumber(ctx, rpc.FinalizedBlockNumber)
case rpc.EarliestBlockNumber:
Expand Down

0 comments on commit 35b8da7

Please sign in to comment.