Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

chain/result: Separate resolve functions into success/failure/expired cases #2143

Merged
merged 5 commits into from
Jul 3, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG_UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Chain

- (impv) [\#2143](https://github.com/bandprotocol/bandchain/pull/2143) Separate resolve functions into success/failure/expired cases.
- (chore) [\#2139](https://github.com/bandprotocol/bandchain/pull/2139) Remove db in favor of emitter.
- (chore) [\#2108](https://github.com/bandprotocol/bandchain/pull/2108) Add script to run bandchain with emitter and flusher locally.
- (impv) [\#2132](https://github.com/bandprotocol/bandchain/pull/2132) Implement emitter handler for bank messages.
Expand Down
1 change: 1 addition & 0 deletions chain/x/oracle/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func TestSuccessRequestOracleData(t *testing.T) {
expectEvents = []abci.Event{{Type: types.EventTypeResolve, Attributes: []kv.Pair{
{Key: []byte(types.AttributeKeyID), Value: parseEventAttribute(resPacket.RequestID)},
{Key: []byte(types.AttributeKeyResolveStatus), Value: parseEventAttribute(uint32(resPacket.ResolveStatus))},
{Key: []byte(types.AttributeKeyResult), Value: []byte("62656562")},
}}}

require.Equal(t, expectEvents, result.GetEvents())
Expand Down
5 changes: 2 additions & 3 deletions chain/x/oracle/keeper/owasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ func (k Keeper) ResolveRequest(ctx sdk.Context, reqID types.RequestID) {
code := k.GetFile(script.Filename)
err := owasm.Execute(code, types.WasmExecuteGas, types.MaxDataSize, env)
if err != nil {
k.Logger(ctx).Info(fmt.Sprintf("failed to execute request id: %d with error: %s", reqID, err.Error()))
k.Resolve(ctx, reqID, types.ResolveStatus_Failure, []byte{})
k.ResolveFailure(ctx, reqID, err.Error())
} else {
k.Resolve(ctx, reqID, types.ResolveStatus_Success, env.Retdata)
k.ResolveSuccess(ctx, reqID, env.Retdata)
}
}
2 changes: 1 addition & 1 deletion chain/x/oracle/keeper/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (k Keeper) ProcessExpiredRequests(ctx sdk.Context) {
// If the number of reports still doesn't reach the minimum, that means this request
// is never resolved. Here we process the response as EXPIRED.
if k.GetReportCount(ctx, currentReqID) < req.MinCount {
k.Resolve(ctx, currentReqID, types.ResolveStatus_Expired, []byte{})
k.ResolveExpired(ctx, currentReqID)
}
// Deactivate all validators that do not report to this request.
for _, val := range req.RequestedValidators {
Expand Down
44 changes: 37 additions & 7 deletions chain/x/oracle/keeper/result.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"encoding/hex"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -41,8 +42,42 @@ func (k Keeper) MustGetResult(ctx sdk.Context, id types.RequestID) types.Result
return result
}

// Resolve saves the result packets for the given request and emits the resolve event.
func (k Keeper) Resolve(ctx sdk.Context, id types.RequestID, status types.ResolveStatus, result []byte) {
// ResolveSuccess resolves the given request as success with the given result.
func (k Keeper) ResolveSuccess(ctx sdk.Context, id types.RequestID, result []byte) {
k.SaveResult(ctx, id, types.ResolveStatus_Success, result)
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeResolve,
sdk.NewAttribute(types.AttributeKeyID, fmt.Sprintf("%d", id)),
sdk.NewAttribute(types.AttributeKeyResolveStatus, fmt.Sprintf("%d", types.ResolveStatus_Success)),
sdk.NewAttribute(types.AttributeKeyResult, hex.EncodeToString(result)),
))
}

// ResolveFailure resolves the given request as failure with the given reason.
func (k Keeper) ResolveFailure(ctx sdk.Context, id types.RequestID, reason string) {
k.SaveResult(ctx, id, types.ResolveStatus_Failure, []byte{})
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeResolve,
sdk.NewAttribute(types.AttributeKeyID, fmt.Sprintf("%d", id)),
sdk.NewAttribute(types.AttributeKeyResolveStatus, fmt.Sprintf("%d", types.ResolveStatus_Failure)),
sdk.NewAttribute(types.AttributeKeyReason, reason),
))
}

// ResolveExpired resolves the given request as expired.
func (k Keeper) ResolveExpired(ctx sdk.Context, id types.RequestID) {
k.SaveResult(ctx, id, types.ResolveStatus_Expired, []byte{})
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeResolve,
sdk.NewAttribute(types.AttributeKeyID, fmt.Sprintf("%d", id)),
sdk.NewAttribute(types.AttributeKeyResolveStatus, fmt.Sprintf("%d", types.ResolveStatus_Expired)),
))
}

// SaveResult saves the result packets for the request with the given resolve status and result.
func (k Keeper) SaveResult(
ctx sdk.Context, id types.RequestID, status types.ResolveStatus, result []byte,
) {
r := k.MustGetRequest(ctx, id)
reqPacket := types.NewOracleRequestPacketData(
r.ClientID, // ClientID
Expand All @@ -61,9 +96,4 @@ func (k Keeper) Resolve(ctx sdk.Context, id types.RequestID, status types.Resolv
result, // Result
)
k.SetResult(ctx, id, types.NewResult(reqPacket, resPacket))
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeResolve,
sdk.NewAttribute(types.AttributeKeyID, fmt.Sprintf("%d", id)),
sdk.NewAttribute(types.AttributeKeyResolveStatus, fmt.Sprintf("%d", status)),
))
}
2 changes: 2 additions & 0 deletions chain/x/oracle/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ const (
AttributeKeyAskCount = "ask_count"
AttributeKeyMinCount = "min_count"
AttributeKeyResolveStatus = "resolve_status"
AttributeKeyResult = "result"
AttributeKeyReason = "reason"
)