From 9cb6fcac0b7e6b7478950eb86327a75778585e2a Mon Sep 17 00:00:00 2001 From: marcello33 Date: Wed, 13 Nov 2024 11:33:02 +0100 Subject: [PATCH] chg: handle edge cases with string blockNumber --- eth/filters/IBackend.go | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- internal/cli/server/api_service.go | 36 ++++++++++++++++++++++++++++-- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/eth/filters/IBackend.go b/eth/filters/IBackend.go index 7dfd509f83..3a69c90d2e 100644 --- a/eth/filters/IBackend.go +++ b/eth/filters/IBackend.go @@ -132,7 +132,7 @@ func (mr *MockBackendMockRecorder) GetBorBlockLogs(arg0, arg1 interface{}) *gomo // GetBorBlockReceipt mocks base method. func (m *MockBackend) GetBorBlockReceipt(arg0 context.Context, arg1 common.Hash) (*types.Receipt, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "BorBlockReceipt", arg0, arg1) + ret := m.ctrl.Call(m, "GetBorBlockReceipt", arg0, arg1) ret0, _ := ret[0].(*types.Receipt) ret1, _ := ret[1].(error) return ret0, ret1 @@ -141,7 +141,7 @@ func (m *MockBackend) GetBorBlockReceipt(arg0 context.Context, arg1 common.Hash) // GetBorBlockReceipt indicates an expected call of GetBorBlockReceipt. func (mr *MockBackendMockRecorder) GetBorBlockReceipt(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BorBlockReceipt", reflect.TypeOf((*MockBackend)(nil).GetBorBlockReceipt), arg0, arg1) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBorBlockReceipt", reflect.TypeOf((*MockBackend)(nil).GetBorBlockReceipt), arg0, arg1) } // GetLogs mocks base method. diff --git a/go.mod b/go.mod index a976c95e21..f8258fb3d8 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 4ed86b6fc1..b60ff0b105 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/cli/server/api_service.go b/internal/cli/server/api_service.go index 67e7c0924c..670cb65dbe 100644 --- a/internal/cli/server/api_service.go +++ b/internal/cli/server/api_service.go @@ -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 } @@ -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 } @@ -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") +}