From c2c1a86d753e02742e3653fb3fc6ad1b47f6d605 Mon Sep 17 00:00:00 2001 From: Sorawit Suriyakarn Date: Fri, 3 Jul 2020 23:16:05 +0700 Subject: [PATCH 1/5] chain/result: Separate resolve functions into success/failure/expired cases --- chain/x/oracle/keeper/owasm.go | 5 ++-- chain/x/oracle/keeper/request.go | 2 +- chain/x/oracle/keeper/result.go | 42 ++++++++++++++++++++++++++------ chain/x/oracle/types/events.go | 1 + 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/chain/x/oracle/keeper/owasm.go b/chain/x/oracle/keeper/owasm.go index 1ef99dbdea..baafc1655b 100644 --- a/chain/x/oracle/keeper/owasm.go +++ b/chain/x/oracle/keeper/owasm.go @@ -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) } } diff --git a/chain/x/oracle/keeper/request.go b/chain/x/oracle/keeper/request.go index 4032b93d97..1965833052 100644 --- a/chain/x/oracle/keeper/request.go +++ b/chain/x/oracle/keeper/request.go @@ -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 { diff --git a/chain/x/oracle/keeper/result.go b/chain/x/oracle/keeper/result.go index ad05707134..86b6721083 100644 --- a/chain/x/oracle/keeper/result.go +++ b/chain/x/oracle/keeper/result.go @@ -41,8 +41,41 @@ 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)), + )) +} + +// ResolveSuccess 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_Success)), + sdk.NewAttribute(types.AttributeKeyReason, reason), + )) +} + +// ResolveSuccess 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 @@ -61,9 +94,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)), - )) } diff --git a/chain/x/oracle/types/events.go b/chain/x/oracle/types/events.go index d9bf8113ab..35ab77e3c2 100644 --- a/chain/x/oracle/types/events.go +++ b/chain/x/oracle/types/events.go @@ -27,4 +27,5 @@ const ( AttributeKeyAskCount = "ask_count" AttributeKeyMinCount = "min_count" AttributeKeyResolveStatus = "resolve_status" + AttributeKeyReason = "reason" ) From f5ea2d599f1b5417eaa796ee8caac08d926e512a Mon Sep 17 00:00:00 2001 From: Sorawit Suriyakarn Date: Fri, 3 Jul 2020 23:17:16 +0700 Subject: [PATCH 2/5] Add changelog --- CHANGELOG_UNRELEASED.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG_UNRELEASED.md b/CHANGELOG_UNRELEASED.md index 38c44bd33a..b4850f7023 100644 --- a/CHANGELOG_UNRELEASED.md +++ b/CHANGELOG_UNRELEASED.md @@ -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. From 317ce1e8ac788cc7a1f686f465c4474c144680bd Mon Sep 17 00:00:00 2001 From: Sorawit Suriyakarn Date: Fri, 3 Jul 2020 23:17:44 +0700 Subject: [PATCH 3/5] fix typo --- chain/x/oracle/keeper/result.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chain/x/oracle/keeper/result.go b/chain/x/oracle/keeper/result.go index 86b6721083..c93b7a8f21 100644 --- a/chain/x/oracle/keeper/result.go +++ b/chain/x/oracle/keeper/result.go @@ -57,7 +57,7 @@ func (k Keeper) ResolveFailure(ctx sdk.Context, id types.RequestID, reason strin 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.AttributeKeyResolveStatus, fmt.Sprintf("%d", types.ResolveStatus_Failure)), sdk.NewAttribute(types.AttributeKeyReason, reason), )) } From d95cf4e303f80ca37a07fedb8775917f274847d0 Mon Sep 17 00:00:00 2001 From: Sorawit Suriyakarn Date: Fri, 3 Jul 2020 23:19:26 +0700 Subject: [PATCH 4/5] Fix comment and add result key in resolve success --- chain/x/oracle/keeper/result.go | 6 ++++-- chain/x/oracle/types/events.go | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/chain/x/oracle/keeper/result.go b/chain/x/oracle/keeper/result.go index c93b7a8f21..48f43862d9 100644 --- a/chain/x/oracle/keeper/result.go +++ b/chain/x/oracle/keeper/result.go @@ -1,6 +1,7 @@ package keeper import ( + "encoding/hex" "fmt" sdk "github.com/cosmos/cosmos-sdk/types" @@ -48,10 +49,11 @@ func (k Keeper) ResolveSuccess(ctx sdk.Context, id types.RequestID, result []byt 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)), )) } -// ResolveSuccess resolves the given request as failure with the given reason. +// 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( @@ -62,7 +64,7 @@ func (k Keeper) ResolveFailure(ctx sdk.Context, id types.RequestID, reason strin )) } -// ResolveSuccess resolves the given request as expired. +// 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( diff --git a/chain/x/oracle/types/events.go b/chain/x/oracle/types/events.go index 35ab77e3c2..e132c294aa 100644 --- a/chain/x/oracle/types/events.go +++ b/chain/x/oracle/types/events.go @@ -27,5 +27,6 @@ const ( AttributeKeyAskCount = "ask_count" AttributeKeyMinCount = "min_count" AttributeKeyResolveStatus = "resolve_status" + AttributeKeyResult = "result" AttributeKeyReason = "reason" ) From 03b05eadb1000c730e15782b9787d37c29fde368 Mon Sep 17 00:00:00 2001 From: Sorawit Suriyakarn Date: Fri, 3 Jul 2020 23:36:00 +0700 Subject: [PATCH 5/5] Fix test --- chain/x/oracle/app_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/chain/x/oracle/app_test.go b/chain/x/oracle/app_test.go index afa54d4c19..01fd623e0d 100644 --- a/chain/x/oracle/app_test.go +++ b/chain/x/oracle/app_test.go @@ -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())