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

Commit

Permalink
Merge pull request #1446 from bandprotocol/cleanup-result-a-bit
Browse files Browse the repository at this point in the history
chain: Clean up result keeper a bit
  • Loading branch information
sorawit authored Apr 29, 2020
2 parents 590d0e8 + 5641914 commit 39670e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
52 changes: 21 additions & 31 deletions chain/x/oracle/keeper/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// HasReport checks if the result of this request ID exists in the storage.
func (k Keeper) HasResult(ctx sdk.Context, id types.RID) bool {
return ctx.KVStore(k.storeKey).Has(types.ResultStoreKey(id))
}

// GetDataSource returns the result bytes for the given request ID or error if not exists.
func (k Keeper) GetResult(ctx sdk.Context, id types.RID) ([]byte, error) {
bz := ctx.KVStore(k.storeKey).Get(types.ResultStoreKey(id))
if bz == nil {
return nil, sdkerrors.Wrapf(types.ErrResultNotFound, "id: %d", id)
}
return bz, nil
}

// AddResult validates the result's size and saves it to the store.
func (k Keeper) AddResult(
ctx sdk.Context, requestID types.RequestID,
requestPacket types.OracleRequestPacketData,
responsePacket types.OracleResponsePacketData,
ctx sdk.Context, id types.RID,
req types.OracleRequestPacketData, res types.OracleResponsePacketData,
) ([]byte, error) {
result, err := hex.DecodeString(responsePacket.Result)
result, err := hex.DecodeString(res.Result)
if err != nil {
return nil, err
}
Expand All @@ -28,40 +41,17 @@ func (k Keeper) AddResult(
store := ctx.KVStore(k.storeKey)

h := sha256.New()
h.Write(k.cdc.MustMarshalBinaryBare(requestPacket))
h.Write(k.cdc.MustMarshalBinaryBare(req))
reqPacketHash := h.Sum(nil)

h = sha256.New()
h.Write(k.cdc.MustMarshalBinaryBare(responsePacket))
h.Write(k.cdc.MustMarshalBinaryBare(res))
resPacketHash := h.Sum(nil)

h = sha256.New()
h.Write(append(reqPacketHash, resPacketHash...))
resultHash := h.Sum(nil)
store.Set(
types.ResultStoreKey(requestID),
resultHash,
)
return resultHash, nil
}

// GetResult returns the result bytes in the store.
func (k Keeper) GetResult(
ctx sdk.Context, requestID types.RequestID,
) ([]byte, error) {
if !k.HasResult(ctx, requestID) {
return nil, sdkerrors.Wrapf(types.ErrItemNotFound,
"GetResult: Result for request ID %d is not available.", requestID,
)
}
store := ctx.KVStore(k.storeKey)
return store.Get(types.ResultStoreKey(requestID)), nil
}

// HasResult checks whether the result at this request id exists in the store.
func (k Keeper) HasResult(
ctx sdk.Context, requestID types.RequestID,
) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.ResultStoreKey(requestID))
store.Set(types.ResultStoreKey(id), resultHash)
return resultHash, nil
}
1 change: 1 addition & 0 deletions chain/x/oracle/types/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
ErrRequestNotFound = sdkerrors.Register(ModuleName, 13, "request not found")
ErrRawRequestNotFound = sdkerrors.Register(ModuleName, 14, "raw request not found")
ErrReporterNotFound = sdkerrors.Register(ModuleName, 15, "reporter not found")
ErrResultNotFound = sdkerrors.Register(ModuleName, 16, "result not found")

ErrRawRequestAlreadyExists = sdkerrors.Register(ModuleName, 20, "raw request already exists")
ErrReporterAlreadyExists = sdkerrors.Register(ModuleName, 21, "reporter already exists")
Expand Down

0 comments on commit 39670e1

Please sign in to comment.