Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: PRT - fix node error handling on error codes 300+ / 200 - #1770

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions protocol/chainlib/jsonRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,6 @@ func (cp *JrpcChainProxy) SendNodeMsg(ctx context.Context, ch chan interface{},

// Call our node
var rpcMessage *rpcclient.JsonrpcMessage
var replyMessage *rpcInterfaceMessages.JsonrpcMessage
var sub *rpcclient.ClientSubscription
// support setting headers
if len(nodeMessage.GetHeaders()) > 0 {
Expand All @@ -671,41 +670,43 @@ func (cp *JrpcChainProxy) SendNodeMsg(ctx context.Context, ch chan interface{},

cp.NodeUrl.SetIpForwardingIfNecessary(ctx, rpc.SetHeader)
rpcMessage, nodeErr = rpc.CallContext(connectCtx, nodeMessage.ID, nodeMessage.Method, nodeMessage.Params, true, nodeMessage.GetDisableErrorHandling())
if err != nil {
if nodeErr != nil {
// here we are getting an error for every code that is not 200-300
if common.StatusCodeError504.Is(err) || common.StatusCodeError429.Is(err) || common.StatusCodeErrorStrict.Is(err) {
return nil, "", nil, utils.LavaFormatWarning("Received invalid status code", err, utils.Attribute{Key: "chainID", Value: cp.BaseChainProxy.ChainID}, utils.Attribute{Key: "apiName", Value: chainMessage.GetApi().Name})
if common.StatusCodeError504.Is(nodeErr) || common.StatusCodeError429.Is(nodeErr) || common.StatusCodeErrorStrict.Is(nodeErr) {
return nil, "", nil, utils.LavaFormatWarning("Received invalid status code", nodeErr, utils.Attribute{Key: "chainID", Value: cp.BaseChainProxy.ChainID}, utils.Attribute{Key: "apiName", Value: chainMessage.GetApi().Name})
}
// Validate if the error is related to the provider connection to the node or it is a valid error
// in case the error is valid (e.g. bad input parameters) the error will return in the form of a valid error reply
if parsedError := cp.HandleNodeError(ctx, err); parsedError != nil {
if parsedError := cp.HandleNodeError(ctx, nodeErr); parsedError != nil {
return nil, "", nil, parsedError
}
}
}

var replyMsg rpcInterfaceMessages.JsonrpcMessage
var replyMsg *rpcInterfaceMessages.JsonrpcMessage
// the error check here would only wrap errors not from the rpc

if nodeErr != nil {
utils.LavaFormatDebug("got error from node", utils.LogAttr("GUID", ctx), utils.LogAttr("nodeErr", nodeErr))
return nil, "", nil, nodeErr
// try to parse node error as json message
rpcMessage = TryRecoverNodeErrorFromClientError(nodeErr)
if rpcMessage == nil {
utils.LavaFormatDebug("got error from node", utils.LogAttr("GUID", ctx), utils.LogAttr("nodeErr", nodeErr))
return nil, "", nil, nodeErr
}
}

replyMessage, err = rpcInterfaceMessages.ConvertJsonRPCMsg(rpcMessage)
replyMsg, err = rpcInterfaceMessages.ConvertJsonRPCMsg(rpcMessage)
if err != nil {
return nil, "", nil, utils.LavaFormatError("jsonRPC error", err, utils.Attribute{Key: "GUID", Value: ctx})
}
// validate result is valid
if replyMessage.Error == nil {
responseIsNilValidationError := ValidateNilResponse(string(replyMessage.Result))
if replyMsg.Error == nil {
responseIsNilValidationError := ValidateNilResponse(string(replyMsg.Result))
if responseIsNilValidationError != nil {
return nil, "", nil, responseIsNilValidationError
}
}

replyMsg = *replyMessage
err = cp.ValidateRequestAndResponseIds(nodeMessage.ID, replyMessage.ID)
err = cp.ValidateRequestAndResponseIds(nodeMessage.ID, replyMsg.ID)
if err != nil {
return nil, "", nil, utils.LavaFormatError("jsonRPC ID mismatch error", err,
utils.Attribute{Key: "GUID", Value: ctx},
Expand Down
14 changes: 14 additions & 0 deletions protocol/chainlib/node_error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ func (geh *genericErrorHandler) ValidateRequestAndResponseIds(nodeMessageID json
return nil
}

func TryRecoverNodeErrorFromClientError(nodeErr error) *rpcclient.JsonrpcMessage {
// try to parse node error as json message
httpError, ok := nodeErr.(rpcclient.HTTPError)
if ok {
jsonMessage := &rpcclient.JsonrpcMessage{}
err := json.Unmarshal(httpError.Body, jsonMessage)
if err == nil {
utils.LavaFormatDebug("Successfully recovered HTTPError to node message", utils.LogAttr("jsonMessage", jsonMessage))
return jsonMessage
}
}
return nil
}

type RestErrorHandler struct{ genericErrorHandler }

// Validating if the error is related to the provider connection or not
Expand Down
25 changes: 12 additions & 13 deletions protocol/chainlib/tendermintRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,6 @@ func (cp *tendermintRpcChainProxy) SendRPC(ctx context.Context, nodeMessage *rpc

// create variables for the rpc message and reply message
var rpcMessage *rpcclient.JsonrpcMessage
var replyMessage *rpcInterfaceMessages.RPCResponse
var sub *rpcclient.ClientSubscription
if len(nodeMessage.GetHeaders()) > 0 {
for _, metadata := range nodeMessage.GetHeaders() {
Expand Down Expand Up @@ -768,41 +767,41 @@ func (cp *tendermintRpcChainProxy) SendRPC(ctx context.Context, nodeMessage *rpc
cp.NodeUrl.SetIpForwardingIfNecessary(ctx, rpc.SetHeader)
// perform the rpc call
rpcMessage, nodeErr = rpc.CallContext(connectCtx, nodeMessage.ID, nodeMessage.Method, nodeMessage.Params, false, nodeMessage.GetDisableErrorHandling())
if err != nil {
if common.StatusCodeError504.Is(err) || common.StatusCodeError429.Is(err) || common.StatusCodeErrorStrict.Is(err) {
return nil, "", nil, utils.LavaFormatWarning("Received invalid status code", err, utils.Attribute{Key: "chainID", Value: cp.BaseChainProxy.ChainID}, utils.Attribute{Key: "apiName", Value: chainMessage.GetApi().Name})
if nodeErr != nil {
if common.StatusCodeError504.Is(nodeErr) || common.StatusCodeError429.Is(nodeErr) || common.StatusCodeErrorStrict.Is(nodeErr) {
return nil, "", nil, utils.LavaFormatWarning("Received invalid status code", nodeErr, utils.Attribute{Key: "chainID", Value: cp.BaseChainProxy.ChainID}, utils.Attribute{Key: "apiName", Value: chainMessage.GetApi().Name})
}
// Validate if the error is related to the provider connection to the node or it is a valid error
// in case the error is valid (e.g. bad input parameters) the error will return in the form of a valid error reply
if parsedError := cp.HandleNodeError(ctx, err); parsedError != nil {
if parsedError := cp.HandleNodeError(ctx, nodeErr); parsedError != nil {
return nil, "", nil, parsedError
}
}
}

var replyMsg *rpcInterfaceMessages.RPCResponse
// the error check here would only wrap errors not from the rpc

if nodeErr != nil {
utils.LavaFormatDebug("got error from node", utils.LogAttr("GUID", ctx), utils.LogAttr("nodeErr", nodeErr))
return nil, "", nil, nodeErr
rpcMessage = TryRecoverNodeErrorFromClientError(nodeErr)
if rpcMessage == nil {
utils.LavaFormatDebug("got error from node", utils.LogAttr("GUID", ctx), utils.LogAttr("nodeErr", nodeErr))
return nil, "", nil, nodeErr
}
}

replyMessage, err = rpcInterfaceMessages.ConvertTendermintMsg(rpcMessage)
replyMsg, err = rpcInterfaceMessages.ConvertTendermintMsg(rpcMessage)
if err != nil {
return nil, "", nil, utils.LavaFormatError("tendermintRPC error", err)
}

// if we didn't get a node error.
if replyMessage.Error == nil {
if replyMsg.Error == nil {
// validate result is valid
responseIsNilValidationError := ValidateNilResponse(string(replyMessage.Result))
responseIsNilValidationError := ValidateNilResponse(string(replyMsg.Result))
if responseIsNilValidationError != nil {
return nil, "", nil, responseIsNilValidationError
}
}
replyMsg = replyMessage

err = cp.ValidateRequestAndResponseIds(nodeMessage.ID, rpcMessage.ID)
if err != nil {
return nil, "", nil, utils.LavaFormatError("tendermintRPC ID mismatch error", err,
Expand Down
Loading