Skip to content

Commit

Permalink
chg: handle edge cases with string blockNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
marcello33 committed Nov 13, 2024
1 parent 59b085a commit 9cb6fca
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
4 changes: 2 additions & 2 deletions eth/filters/IBackend.go

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

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ require (
github.com/kylelemons/godebug v1.1.0
github.com/maticnetwork/crand v1.0.2
github.com/maticnetwork/heimdall v1.0.7
github.com/maticnetwork/polyproto v0.0.3
github.com/maticnetwork/polyproto v0.0.4-0.20241113101917-6744479e4d59
github.com/mattn/go-colorable v0.1.13
github.com/mattn/go-isatty v0.0.20
github.com/mitchellh/cli v1.1.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1707,8 +1707,8 @@ github.com/maticnetwork/heimdall v1.0.4/go.mod h1:Xh7KFvtbs/SVNjOI8IgYmk6JdzYx89
github.com/maticnetwork/heimdall v1.0.7 h1:QStn+hbZKxfE+PqecaorA/uATDPuQoi+U9Z7IIonb60=
github.com/maticnetwork/heimdall v1.0.7/go.mod h1:+ANI5+VV28ahwfdl7oMzrcNwaTEs1Fn6z39BqBGcvaA=
github.com/maticnetwork/polyproto v0.0.3-0.20230216113155-340ea926ca53/go.mod h1:e1mU2EXSwEpn5jM7GfNwu3AupsV6WAGoPFFfswXOF0o=
github.com/maticnetwork/polyproto v0.0.3 h1:a69rIp97fcl3ABY4LlVX9B2t1qhLa0Jhny3HNOzReBU=
github.com/maticnetwork/polyproto v0.0.3/go.mod h1:e1mU2EXSwEpn5jM7GfNwu3AupsV6WAGoPFFfswXOF0o=
github.com/maticnetwork/polyproto v0.0.4-0.20241113101917-6744479e4d59 h1:FAezvZ+jt3b1all6taYql6+mafszYBpwfD5KYQqQdNg=
github.com/maticnetwork/polyproto v0.0.4-0.20241113101917-6744479e4d59/go.mod h1:e1mU2EXSwEpn5jM7GfNwu3AupsV6WAGoPFFfswXOF0o=
github.com/maticnetwork/tendermint v0.33.0 h1:f+vORM02BoUOlCvnu3Zjw5rv6l6JSNVchWjH03rUuR8=
github.com/maticnetwork/tendermint v0.33.0/go.mod h1:D2fcnxGk6bje+LoPwImuKSSYLiK7/G06IynGNDSEcJk=
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
Expand Down
36 changes: 34 additions & 2 deletions internal/cli/server/api_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ func headerToProtoborHeader(h *types.Header) *protobor.Header {
}

func (s *Server) HeaderByNumber(ctx context.Context, req *protobor.GetHeaderByNumberRequest) (*protobor.GetHeaderByNumberResponse, error) {
header, err := s.backend.APIBackend.HeaderByNumber(ctx, rpc.BlockNumber(req.Number))
bN, err := getRpcBlockNumberFromString(req.Number)
if err != nil {
return nil, err
}
header, err := s.backend.APIBackend.HeaderByNumber(ctx, bN)
if err != nil {
return nil, err
}
Expand All @@ -47,7 +51,11 @@ func (s *Server) HeaderByNumber(ctx context.Context, req *protobor.GetHeaderByNu
}

func (s *Server) BlockByNumber(ctx context.Context, req *protobor.GetBlockByNumberRequest) (*protobor.GetBlockByNumberResponse, error) {
block, err := s.backend.APIBackend.BlockByNumber(ctx, rpc.BlockNumber(req.Number))
bN, err := getRpcBlockNumberFromString(req.Number)
if err != nil {
return nil, err
}
block, err := s.backend.APIBackend.BlockByNumber(ctx, bN)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -91,3 +99,27 @@ func (s *Server) BorBlockReceipt(ctx context.Context, req *protobor.ReceiptReque

return &protobor.ReceiptResponse{Receipt: ConvertReceiptToProtoReceipt(receipt)}, nil
}

func getRpcBlockNumberFromString(blockNumber string) (rpc.BlockNumber, error) {
if blockNumber == "latest" {
return rpc.LatestBlockNumber, nil
}

if blockNumber == "earliest" {
return rpc.EarliestBlockNumber, nil
}

if blockNumber == "pending" {
return rpc.PendingBlockNumber, nil
}

if blockNumber == "finalized" {
return rpc.FinalizedBlockNumber, nil
}

if blockNumber == "safe" {
return rpc.SafeBlockNumber, nil
}

return rpc.BlockNumber(0), errors.New("invalid block number")
}

0 comments on commit 9cb6fca

Please sign in to comment.