Skip to content

Commit

Permalink
add common errors filtering for responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ranlavanet committed Feb 8, 2024
1 parent 8c5f523 commit 9d7675a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
11 changes: 11 additions & 0 deletions protocol/chainlib/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/lavanet/lava/utils"
pairingtypes "github.com/lavanet/lava/x/pairing/types"
spectypes "github.com/lavanet/lava/x/spec/types"
"golang.org/x/exp/slices"
"google.golang.org/grpc/metadata"
)

Expand All @@ -25,6 +26,8 @@ const (
debug = false
)

var InvalidResponses = []string{"null", "", "nil", "undefined"}

type VerificationKey struct {
Extension string
Addon string
Expand Down Expand Up @@ -339,3 +342,11 @@ func truncateAndPadString(s string, maxLength int) string {

return s
}

// return if response is valid or not - true
func ValidateNilResponse(responseString string) error {
if !slices.Contains(InvalidResponses, responseString) {
return fmt.Errorf("Response returned an empty value: %s", responseString)
}
return nil
}
8 changes: 8 additions & 0 deletions protocol/chainlib/jsonRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,14 @@ func (cp *JrpcChainProxy) SendNodeMsg(ctx context.Context, ch chan interface{},
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 responseIsNilValidationError != nil {
return nil, "", nil, responseIsNilValidationError
}
}

replyMsg = *replyMessage
err := cp.ValidateRequestAndResponseIds(nodeMessage.ID, replyMessage.ID)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion protocol/chainlib/tendermintRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,14 @@ func (cp *tendermintRpcChainProxy) SendRPC(ctx context.Context, nodeMessage *rpc
if err != nil {
return nil, "", nil, utils.LavaFormatError("tendermingRPC error", err)
}

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

err := cp.ValidateRequestAndResponseIds(nodeMessage.ID, rpcMessage.ID)
Expand Down

0 comments on commit 9d7675a

Please sign in to comment.