Skip to content

Commit

Permalink
Merge branch 'main' into CNS-cosmos-47.10
Browse files Browse the repository at this point in the history
  • Loading branch information
ranlavanet authored Mar 27, 2024
2 parents 96b55ef + 591d584 commit 5201719
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.inputs.release_tag == '' && github.event.inputs.release_tag || github.ref_name }}
ref: ${{ github.event.inputs.release_tag != '' && github.event.inputs.release_tag || github.ref_name }}

- name: Fetch all tags
run: |
Expand Down
3 changes: 2 additions & 1 deletion protocol/chainlib/base_chain_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/lavanet/lava/protocol/chainlib/extensionslib"
"github.com/lavanet/lava/protocol/common"
"github.com/lavanet/lava/utils"
epochstorage "github.com/lavanet/lava/x/epochstorage/types"
pairingtypes "github.com/lavanet/lava/x/pairing/types"
Expand Down Expand Up @@ -299,7 +300,7 @@ func (apip *BaseChainParser) getSupportedApi(name, connectionType string) (*ApiC

// Return an error if spec does not exist
if !ok {
return nil, utils.LavaFormatInfo("api not supported", utils.Attribute{Key: "name", Value: name}, utils.Attribute{Key: "connectionType", Value: connectionType})
return nil, common.APINotSupportedError
}

// Return an error if api is disabled
Expand Down
6 changes: 5 additions & 1 deletion protocol/chainlib/jsonRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (apip *JsonRPCChainParser) ParseMsg(url string, data []byte, connectionType
// Check api is supported and save it in nodeMsg
apiCont, err := apip.getSupportedApi(msg.Method, connectionType)
if err != nil {
return nil, utils.LavaFormatInfo("getSupportedApi jsonrpc failed", utils.LogAttr("reason", err), utils.Attribute{Key: "method", Value: msg.Method})
return nil, utils.LavaFormatWarning("getSupportedApi jsonrpc failed", err, utils.LogAttr("method", msg.Method))
}

apiCollectionForMessage, err := apip.getApiCollection(connectionType, apiCont.collectionKey.InternalPath, apiCont.collectionKey.Addon)
Expand Down Expand Up @@ -458,6 +458,10 @@ func (apil *JsonRPCChainListener) Serve(ctx context.Context, cmdFlags common.Con
reply := relayResult.GetReply()
go apil.logger.AddMetricForHttp(metricsData, err, fiberCtx.GetReqHeaders())
if err != nil {
if common.APINotSupportedError.Is(err) {
return fiberCtx.Status(fiber.StatusOK).JSON(common.JsonRpcMethodNotFoundError)
}

// Get unique GUID response
errMasking := apil.logger.GetUniqueGuidResponseForError(err, msgSeed)

Expand Down
9 changes: 8 additions & 1 deletion protocol/chainlib/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ func (apip *RestChainParser) getSupportedApi(name, connectionType string) (*ApiC

// Return an error if spec does not exist
if !ok {
return nil, errors.New("rest api not supported " + name)
return nil, utils.LavaFormatWarning("rest api not supported", common.APINotSupportedError,
utils.LogAttr("name", name),
utils.LogAttr("connectionType", connectionType),
)
}
api := apiCont.api

Expand Down Expand Up @@ -379,6 +382,10 @@ func (apil *RestChainListener) Serve(ctx context.Context, cmdFlags common.Consum
reply := relayResult.GetReply()
go apil.logger.AddMetricForHttp(analytics, err, fiberCtx.GetReqHeaders())
if err != nil {
if common.APINotSupportedError.Is(err) {
return common.CreateRestMethodNotFoundError(fiberCtx, chainID)
}

// Get unique GUID response
errMasking := apil.logger.GetUniqueGuidResponseForError(err, msgSeed)

Expand Down
4 changes: 3 additions & 1 deletion protocol/chainlib/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/lavanet/lava/protocol/chainlib/chainproxy"
"github.com/lavanet/lava/protocol/chainlib/chainproxy/rpcInterfaceMessages"
"github.com/lavanet/lava/protocol/chainlib/extensionslib"
"github.com/lavanet/lava/protocol/common"
"github.com/lavanet/lava/protocol/parser"
pairingtypes "github.com/lavanet/lava/x/pairing/types"
spectypes "github.com/lavanet/lava/x/spec/types"
Expand Down Expand Up @@ -89,7 +90,8 @@ func TestRestGetSupportedApi(t *testing.T) {
}
_, err = apip.getSupportedApi("API2", connectionType_test)
assert.Error(t, err)
assert.Equal(t, "rest api not supported API2", err.Error())
assert.ErrorIs(t, err, common.APINotSupportedError)
assert.Equal(t, "rest api not supported ErrMsg: api not supported {name:API2,connectionType:test}: api not supported", err.Error())

// Test case 3: Returns error if the API is disabled
apip = &RestChainParser{
Expand Down
6 changes: 5 additions & 1 deletion protocol/chainlib/tendermintRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (apip *TendermintChainParser) ParseMsg(urlPath string, data []byte, connect
// Check api is supported and save it in nodeMsg
apiCont, err := apip.getSupportedApi(msg.Method, connectionType)
if err != nil {
return nil, utils.LavaFormatInfo("getSupportedApi jsonrpc failed", utils.LogAttr("reason", err), utils.Attribute{Key: "method", Value: msg.Method})
return nil, utils.LavaFormatWarning("getSupportedApi jsonrpc failed", err, utils.LogAttr("method", msg.Method))
}

apiCollectionForMessage, err := apip.getApiCollection(connectionType, apiCont.collectionKey.InternalPath, apiCont.collectionKey.Addon)
Expand Down Expand Up @@ -482,6 +482,10 @@ func (apil *TendermintRpcChainListener) Serve(ctx context.Context, cmdFlags comm
go apil.logger.AddMetricForHttp(metricsData, err, fiberCtx.GetReqHeaders())

if err != nil {
if common.APINotSupportedError.Is(err) {
return fiberCtx.Status(fiber.StatusOK).JSON(common.JsonRpcMethodNotFoundError)
}

// Get unique GUID response
errMasking := apil.logger.GetUniqueGuidResponseForError(err, msgSeed)

Expand Down
1 change: 1 addition & 0 deletions protocol/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ var (
StatusCodeError504 = sdkerrors.New("Disallowed StatusCode Error", 504, "Disallowed status code error")
StatusCodeError429 = sdkerrors.New("Disallowed StatusCode Error", 429, "Disallowed status code error")
StatusCodeErrorStrict = sdkerrors.New("Disallowed StatusCode Error", 800, "Disallowed status code error")
APINotSupportedError = sdkerrors.New("APINotSupported Error", 900, "api not supported")
)
70 changes: 70 additions & 0 deletions protocol/common/return_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package common

import "github.com/gofiber/fiber/v2"

// #######
// JsonRPC
// #######

type JsonRPCError struct {
Code int `json:"code"`
Message string `json:"message"`
}

type JsonRPCErrorMessage struct {
JsonRPC string `json:"jsonrpc"`
Id int `json:"id"`
Error JsonRPCError `json:"error"`
}

var JsonRpcMethodNotFoundError = JsonRPCErrorMessage{
JsonRPC: "2.0",
Id: 1,
Error: JsonRPCError{
Code: -32601,
Message: "Method not found",
},
}

// #######
// Rest
// #######

type RestError struct {
Code int `json:"code"`
Message string `json:"message"`
Details []interface{} `json:"details"`
}

var RestMethodNotFoundError = RestError{
Code: 12,
Message: "Not Implemented",
Details: []interface{}{},
}

// #######
// Rest - Aptos
// #######

type RestAptosError struct {
Message string `json:"message"`
ErrorCode string `json:"error_code"`
VmErrorCode interface{} `json:"vm_error_code"`
}

var RestAptosMethodNotFoundError = RestAptosError{
Message: "not found",
ErrorCode: "web_framework_error",
VmErrorCode: nil,
}

func CreateRestMethodNotFoundError(fiberCtx *fiber.Ctx, chainId string) error {
switch chainId {
case "APT1":
// Aptos node returns a different error body than the rest of the chains
// This solution is temporary until we change the spec to state how the error looks like
return fiberCtx.Status(fiber.StatusNotImplemented).JSON(RestAptosMethodNotFoundError)
default:
return fiberCtx.Status(fiber.StatusNotImplemented).JSON(RestMethodNotFoundError)
}
}

0 comments on commit 5201719

Please sign in to comment.