Skip to content

Commit

Permalink
Merge pull request #32 from irisnet/service-response-validate
Browse files Browse the repository at this point in the history
R4R: Fix service response validate
  • Loading branch information
dgsbl committed Oct 26, 2020
2 parents 3fa6b59 + 29229a9 commit 3142108
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
12 changes: 4 additions & 8 deletions modules/service/keeper/invocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,15 +868,11 @@ func (k Keeper) AddResponse(
return request, response, sdkerrors.Wrap(types.ErrInvalidResponse, "request is not active")
}

if len(output) > 0 && types.ValidateResponseOutput(output) != nil {
if err = k.Slash(ctx, requestID); err != nil {
panic(err)
}
if err := types.ValidateResponseOutput(output); err != nil {
return request, response, sdkerrors.Wrap(types.ErrInvalidResponseOutput, err.Error())
}

if err := k.RefundServiceFee(ctx, request.Consumer, request.ServiceFee); err != nil {
panic(err)
}
} else if err := k.AddEarnedFee(ctx, provider, request.ServiceFee); err != nil {
if err := k.AddEarnedFee(ctx, provider, request.ServiceFee); err != nil {
return request, response, err
}

Expand Down
2 changes: 1 addition & 1 deletion modules/service/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func (suite *KeeperTestSuite) TestKeeper_Respond_Service() {
suite.Equal(uint64(1), volume)

// respond request 2
_, _, err = suite.keeper.AddResponse(ctx, requestID2, provider, testOutput, "")
_, _, err = suite.keeper.AddResponse(ctx, requestID2, provider, testResult, testOutput)
suite.NoError(err)

requestContext, _ = suite.keeper.GetRequestContext(ctx, requestContextID)
Expand Down
8 changes: 4 additions & 4 deletions modules/service/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,8 +1044,8 @@ func ValidateInput(input string) error {
return sdkerrors.Wrap(ErrInvalidRequestInput, "input missing")
}

if !json.Valid([]byte(input)) {
return sdkerrors.Wrap(ErrInvalidRequestInput, "input is not valid JSON")
if ValidateRequestInput(input) != nil {
return sdkerrors.Wrap(ErrInvalidRequestInput, "invalid input")
}

return nil
Expand All @@ -1060,8 +1060,8 @@ func ValidateOutput(code uint16, output string) error {
return sdkerrors.Wrap(ErrInvalidResponse, "output should not be specified when the result code is not 200")
}

if len(output) > 0 && !json.Valid([]byte(output)) {
return sdkerrors.Wrap(ErrInvalidResponse, "output is not valid JSON")
if len(output) > 0 && ValidateResponseOutput(output) != nil {
return sdkerrors.Wrap(ErrInvalidResponse, "invalid output")
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions modules/service/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ var (

testConsumer = sdk.AccAddress([]byte("test-consumer"))
testProviders = []sdk.AccAddress{testProvider}
testInput = `{"pair":"iris-usdt"}`
testInput = `{"header":{},"body":{"pair":"iris-usdt"}}`
testServiceFeeCap = sdk.NewCoins(testCoin2)
testTimeout = int64(100)
testRepeatedFreq = uint64(120)
testRepeatedTotal = int64(100)

testResult = `{"code":200,"message":""}`
testOutput = `{"last":"100"}`
testOutput = `{"header":{},"body":{"last":"100"}}`

testRequestContextID = GenerateRequestContextID(tmhash.Sum([]byte("test-request-context-id")), 0)
testRequestID = GenerateRequestID(testRequestContextID, 1, 1, 1)
Expand Down Expand Up @@ -720,7 +720,7 @@ func TestMsgCallServiceGetSignBytes(t *testing.T) {
)
res := msg.GetSignBytes()

expected := `{"type":"irismod/service/MsgCallService","value":{"consumer":"cosmos1w3jhxapdvdhkuum4d4jhyt34ks5","input":"{\"pair\":\"iris-usdt\"}","providers":["cosmos1w3jhxapdwpex7anfv3jhy8anr90"],"repeated":true,"repeated_frequency":"120","repeated_total":"100","service_fee_cap":[{"amount":"100","denom":"stake"}],"service_name":"test-service","super_mode":true,"timeout":"100"}}`
expected := `{"type":"irismod/service/MsgCallService","value":{"consumer":"cosmos1w3jhxapdvdhkuum4d4jhyt34ks5","input":"{\"header\":{},\"body\":{\"pair\":\"iris-usdt\"}}","providers":["cosmos1w3jhxapdwpex7anfv3jhy8anr90"],"repeated":true,"repeated_frequency":"120","repeated_total":"100","service_fee_cap":[{"amount":"100","denom":"stake"}],"service_name":"test-service","super_mode":true,"timeout":"100"}}`
require.Equal(t, expected, string(res))
}

Expand Down Expand Up @@ -811,7 +811,7 @@ func TestMsgRespondServiceGetSignBytes(t *testing.T) {
msg := NewMsgRespondService(testRequestID, testProvider, testResult, testOutput)
res := msg.GetSignBytes()

expected := `{"type":"irismod/service/MsgRespondService","value":{"output":"{\"last\":\"100\"}","provider":"cosmos1w3jhxapdwpex7anfv3jhy8anr90","request_id":"3DB0FA99DCB058BC86041BADBD614D6839F8FA20E17CF8AD3BA14C3F1BF613BD0000000000000000000000000000000100000000000000010001","result":"{\"code\":200,\"message\":\"\"}"}}`
expected := `{"type":"irismod/service/MsgRespondService","value":{"output":"{\"header\":{},\"body\":{\"last\":\"100\"}}","provider":"cosmos1w3jhxapdwpex7anfv3jhy8anr90","request_id":"3DB0FA99DCB058BC86041BADBD614D6839F8FA20E17CF8AD3BA14C3F1BF613BD0000000000000000000000000000000100000000000000010001","result":"{\"code\":200,\"message\":\"\"}"}}`
require.Equal(t, expected, string(res))
}

Expand Down

0 comments on commit 3142108

Please sign in to comment.