Skip to content

Commit

Permalink
Merge branch 'extra-rest/v2.4.x' of https://github.com/bandprotocol/c…
Browse files Browse the repository at this point in the history
…hain into extra-rest/v2.5.x
  • Loading branch information
RogerKSI committed Apr 21, 2023
2 parents 4768bcf + efe85dd commit 61e869e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
2 changes: 2 additions & 0 deletions flusher/flusher/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ def Column(*args, **kwargs):
Column("min_count", sa.Integer),
Column("fee_limit", sa.String),
Column("prepare_gas", sa.Integer),
Column("prepare_gas_used", sa.Integer, default=0),
Column("execute_gas", sa.Integer),
Column("execute_gas_used", sa.Integer, default=0),
Column("sender", sa.String, nullable=True),
Column("client_id", sa.String),
Column("request_time", sa.Integer, nullable=True),
Expand Down
8 changes: 8 additions & 0 deletions hooks/emitter/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
channeltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"

oraclekeeper "github.com/bandprotocol/chain/v2/x/oracle/keeper"
oracletypes "github.com/bandprotocol/chain/v2/x/oracle/types"
)

Expand Down Expand Up @@ -184,6 +185,11 @@ func (h *Hook) extractOracleRequestPacket(
err := oracletypes.ModuleCdc.UnmarshalJSON(dataOfPacket, &data)
if err == nil {
if events, ok := evMap[oracletypes.EventTypeRequest+"."+oracletypes.AttributeKeyID]; ok {
var prepareGasUsed uint64
if eventRequestGasUsed, ok := evMap[oracletypes.EventTypeRequest+"."+oracletypes.AttributeKeyGasUsed]; ok {
prepareGasUsed = oraclekeeper.ConvertToGas(common.Atoui(eventRequestGasUsed[0]))
}

id := oracletypes.RequestID(common.Atoi(events[0]))
req := h.oracleKeeper.MustGetRequest(ctx, id)
h.Write("NEW_REQUEST", common.JsDict{
Expand All @@ -198,7 +204,9 @@ func (h *Hook) extractOracleRequestPacket(
"resolve_status": oracletypes.RESOLVE_STATUS_OPEN,
"timestamp": ctx.BlockTime().UnixNano(),
"prepare_gas": data.PrepareGas,
"prepare_gas_used": prepareGasUsed,
"execute_gas": data.ExecuteGas,
"execute_gas_used": uint64(0),
"fee_limit": data.FeeLimit.String(),
"total_fees": evMap[oracletypes.EventTypeRequest+"."+oracletypes.AttributeKeyTotalFees][0],
"is_ibc": req.IBCChannel != nil,
Expand Down
35 changes: 26 additions & 9 deletions hooks/emitter/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/bandprotocol/chain/v2/hooks/common"
oraclekeeper "github.com/bandprotocol/chain/v2/x/oracle/keeper"
"github.com/bandprotocol/chain/v2/x/oracle/types"
)

Expand Down Expand Up @@ -104,24 +105,32 @@ func (app *Hook) emitReportAndRawReport(
}
}

func (h *Hook) emitUpdateResult(ctx sdk.Context, id types.RequestID, reason string) {
func (h *Hook) emitUpdateResult(ctx sdk.Context, id types.RequestID, executeGasUsed uint64, reason string) {
result := h.oracleKeeper.MustGetResult(ctx, id)

h.Write("UPDATE_REQUEST", common.JsDict{
"id": id,
"request_time": result.RequestTime,
"resolve_time": result.ResolveTime,
"resolve_status": result.ResolveStatus,
"resolve_height": ctx.BlockHeight(),
"reason": reason,
"result": parseBytes(result.Result),
"id": id,
"execute_gas_used": executeGasUsed,
"request_time": result.RequestTime,
"resolve_time": result.ResolveTime,
"resolve_status": result.ResolveStatus,
"resolve_height": ctx.BlockHeight(),
"reason": reason,
"result": parseBytes(result.Result),
})
}

// handleMsgRequestData implements emitter handler for MsgRequestData.
func (h *Hook) handleMsgRequestData(
ctx sdk.Context, txHash []byte, msg *types.MsgRequestData, evMap common.EvMap, detail common.JsDict,
) {
var prepareGasUsed uint64
if eventRequestGasUsed, ok := evMap[types.EventTypeRequest+"."+types.AttributeKeyGasUsed]; ok {
prepareGasUsed = oraclekeeper.ConvertToGas(common.Atoui(eventRequestGasUsed[0]))
}

id := types.RequestID(common.Atoi(evMap[types.EventTypeRequest+"."+types.AttributeKeyID][0]))

req := h.oracleKeeper.MustGetRequest(ctx, id)
h.Write("NEW_REQUEST", common.JsDict{
"id": id,
Expand All @@ -135,7 +144,9 @@ func (h *Hook) handleMsgRequestData(
"resolve_status": types.RESOLVE_STATUS_OPEN,
"timestamp": ctx.BlockTime().UnixNano(),
"prepare_gas": msg.PrepareGas,
"prepare_gas_used": prepareGasUsed,
"execute_gas": msg.ExecuteGas,
"execute_gas_used": uint64(0),
"fee_limit": msg.FeeLimit.String(),
"total_fees": evMap[types.EventTypeRequest+"."+types.AttributeKeyTotalFees][0],
"is_ibc": req.IBCChannel != nil,
Expand Down Expand Up @@ -208,14 +219,20 @@ func (h *Hook) handleMsgEditOracleScript(

// handleEventRequestExecute implements emitter handler for EventRequestExecute.
func (h *Hook) handleEventRequestExecute(ctx sdk.Context, evMap common.EvMap) {
var executeGasUsed uint64
if eventResolveGasUsed, ok := evMap[types.EventTypeResolve+"."+types.AttributeKeyGasUsed]; ok {
executeGasUsed = oraclekeeper.ConvertToGas(common.Atoui(eventResolveGasUsed[0]))
}

if reasons, ok := evMap[types.EventTypeResolve+"."+types.AttributeKeyReason]; ok {
h.emitUpdateResult(
ctx,
types.RequestID(common.Atoi(evMap[types.EventTypeResolve+"."+types.AttributeKeyID][0])),
executeGasUsed,
reasons[0],
)
} else {
h.emitUpdateResult(ctx, types.RequestID(common.Atoi(evMap[types.EventTypeResolve+"."+types.AttributeKeyID][0])), "")
h.emitUpdateResult(ctx, types.RequestID(common.Atoi(evMap[types.EventTypeResolve+"."+types.AttributeKeyID][0])), executeGasUsed, "")
}
}

Expand Down
5 changes: 5 additions & 0 deletions x/oracle/keeper/owasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"encoding/hex"
"fmt"
"math"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -20,6 +21,10 @@ func ConvertToOwasmGas(cosmos uint64) uint64 {
return uint64(cosmos * gasConversionFactor)
}

func ConvertToGas(owasm uint64) uint64 {
return uint64(math.Ceil(float64(owasm) / float64(gasConversionFactor)))
}

// GetSpanSize return maximum value between MaxReportDataSize and MaxCallDataSize
func (k Keeper) GetSpanSize(ctx sdk.Context) uint64 {
if k.MaxReportDataSize(ctx) > k.MaxCalldataSize(ctx) {
Expand Down

0 comments on commit 61e869e

Please sign in to comment.