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

PRT-88: Prevent potential panic in the tendermintRPC #139

Merged
merged 6 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 9 additions & 0 deletions relayer/chainproxy/chainproxyErrors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package chainproxy

import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

var (
errFailedToConvertMessage = sdkerrors.New("RPC error", 1000, "failed to convert a message")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually errors would be with a capital letter so they will be available for other packages as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, we need to make our code as code as possible :D

I usually make them exportable only if they are used outside, but cool will follow structure we have 9d86272

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the build error 9b3d0d6

)
16 changes: 13 additions & 3 deletions relayer/chainproxy/jsonRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ func (j *JrpcMessage) setMessageResult(result json.RawMessage) {
j.msg.Result = result
}

func convertMsg(rpcMsg *rpcclient.JsonrpcMessage) *JsonrpcMessage {
func convertMsg(rpcMsg *rpcclient.JsonrpcMessage) (*JsonrpcMessage, error) {
// Return an error if the message was not sent
if rpcMsg == nil {
return nil, errFailedToConvertMessage
}

msg := &JsonrpcMessage{
Version: rpcMsg.Version,
ID: rpcMsg.ID,
Expand All @@ -54,7 +59,8 @@ func convertMsg(rpcMsg *rpcclient.JsonrpcMessage) *JsonrpcMessage {
Error: rpcMsg.Error,
Result: rpcMsg.Result,
}
return msg

return msg, nil
}

type JrpcChainProxy struct {
Expand Down Expand Up @@ -369,7 +375,11 @@ func (nm *JrpcMessage) Send(ctx context.Context, ch chan interface{}) (relayRepl
Message: fmt.Sprintf("%s", err),
}
} else {
replyMessage = convertMsg(rpcMessage)
replyMessage, err = convertMsg(rpcMessage)
if err != nil {
return nil, "", nil, utils.LavaFormatError("jsonRPC error", err, nil)
}

nm.msg = replyMessage
replyMsg = *replyMessage
}
Expand Down
7 changes: 5 additions & 2 deletions relayer/chainproxy/tendermintRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,10 @@ func (nm *TendemintRpcMessage) Send(ctx context.Context, ch chan interface{}) (r
var sub *rpcclient.ClientSubscription
if ch != nil {
sub, rpcMessage, err = rpc.Subscribe(context.Background(), nm.msg.ID, nm.msg.Method, ch, nm.msg.Params)
replyMessage = convertMsg(rpcMessage)
} else {
connectCtx, cancel := context.WithTimeout(ctx, DefaultTimeout)
defer cancel()
rpcMessage, err = rpc.CallContext(connectCtx, nm.msg.ID, nm.msg.Method, nm.msg.Params)
replyMessage = convertMsg(rpcMessage)
}

var replyMsg JsonrpcMessage
Expand All @@ -358,6 +356,11 @@ func (nm *TendemintRpcMessage) Send(ctx context.Context, ch chan interface{}) (r
Message: fmt.Sprintf("%s", err),
}
} else {
replyMessage, err = convertMsg(rpcMessage)
if err != nil {
return nil, "", nil, utils.LavaFormatError("tendermingRPC error", err, nil)
}

nm.msg = replyMessage
replyMsg = *replyMessage
}
Expand Down