Skip to content

Commit

Permalink
refactor(x/tally): log detailed errors and return simple errors in Fi…
Browse files Browse the repository at this point in the history
…lterAndTally
  • Loading branch information
hacheigriega committed Dec 11, 2024
1 parent 439e213 commit 5592b43
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
29 changes: 17 additions & 12 deletions x/tally/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"strconv"
"strings"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/sedaprotocol/seda-wasm-vm/tallyvm/v2"
Expand Down Expand Up @@ -107,8 +105,6 @@ func (k Keeper) ProcessTallies(ctx sdk.Context, coreContract sdk.AccAddress) err
k.Logger(ctx).Info("data request's number of reveals did not meet replication factor", "request_id", req.ID)
default:
result, err := k.FilterAndTally(ctx, req)
dataResults[i].Consensus = result.consensus
dataResults[i].GasUsed = result.execGasUsed + result.tallyGasUsed
if err != nil {
dataResults[i].ExitCode = batchingtypes.TallyExitCodeFailedToExecute
dataResults[i].Result = []byte(err.Error())
Expand All @@ -117,6 +113,9 @@ func (k Keeper) ProcessTallies(ctx sdk.Context, coreContract sdk.AccAddress) err
dataResults[i].ExitCode = uint32(result.exitInfo.ExitCode)
dataResults[i].Result = result.result
}
dataResults[i].Consensus = result.consensus
dataResults[i].GasUsed = result.execGasUsed + result.tallyGasUsed

k.Logger(ctx).Info("completed tally execution", "request_id", req.ID)
k.Logger(ctx).Debug("tally execution result", "request_id", req.ID, "tally_result", result)
}
Expand Down Expand Up @@ -216,41 +215,40 @@ func (k Keeper) FilterAndTally(ctx sdk.Context, req types.Request) (TallyResult,

filter, err := base64.StdEncoding.DecodeString(req.ConsensusFilter)
if err != nil {
return result, errorsmod.Wrap(err, "failed to decode consensus filter")
return result, k.logErrAndRet(ctx, err, types.ErrDecodingConsensusFilter, req)
}
// Convert base64-encoded payback address to hex encoding that
// the tally VM expects.
decodedBytes, err := base64.StdEncoding.DecodeString(req.PaybackAddress)
if err != nil {
return result, errorsmod.Wrap(err, "failed to decode payback address")
return result, k.logErrAndRet(ctx, err, types.ErrDecodingPaybackAddress, req)
}
paybackAddrHex := hex.EncodeToString(decodedBytes)

var outliers []int
outliers, result.consensus, result.proxyPubKeys, err = ApplyFilter(filter, reveals)
if err != nil {
return result, errorsmod.Wrap(err, "error while applying filter")
return result, k.logErrAndRet(ctx, err, types.ErrApplyingFilter, req)
}

tallyProgram, err := k.wasmStorageKeeper.GetOracleProgram(ctx, req.TallyProgramID)
if err != nil {
return result, err
return result, k.logErrAndRet(ctx, err, types.ErrFindingTallyProgram, req)
}
tallyInputs, err := base64.StdEncoding.DecodeString(req.TallyInputs)
if err != nil {
return result, errorsmod.Wrap(err, "failed to decode tally inputs")
return result, k.logErrAndRet(ctx, err, types.ErrDecodingTallyInputs, req)
}

args, err := tallyVMArg(tallyInputs, reveals, outliers)
if err != nil {
return result, errorsmod.Wrap(err, "failed to construct tally VM arguments")
return result, k.logErrAndRet(ctx, err, types.ErrConstructingTallyVMArgs, req)
}

maxGasLimit, err := k.GetMaxTallyGasLimit(ctx)
if err != nil {
return result, errorsmod.Wrap(err, "failed to get max tally gas limit")
return result, k.logErrAndRet(ctx, err, types.ErrGettingMaxTallyGasLimit, req)
}

gasLimit := min(req.TallyGasLimit, maxGasLimit)

k.Logger(ctx).Info(
Expand Down Expand Up @@ -286,6 +284,13 @@ func (k Keeper) FilterAndTally(ctx sdk.Context, req types.Request) (TallyResult,
return result, nil
}

// logErrAndRet logs the base error along with the request ID for
// debugging and returns the registered error.
func (k Keeper) logErrAndRet(ctx sdk.Context, baseErr, registeredErr error, req types.Request) error {
k.Logger(ctx).Debug(baseErr.Error(), "request_id", req.ID, "error", registeredErr)
return registeredErr
}

func tallyVMArg(inputArgs []byte, reveals []types.RevealBody, outliers []int) ([]string, error) {
arg := []string{hex.EncodeToString(inputArgs)}

Expand Down
8 changes: 8 additions & 0 deletions x/tally/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ var (
ErrInvalidNumberType = errors.Register("tally", 9, "invalid number type specified")
ErrFilterUnexpected = errors.Register("tally", 10, "unexpected error occurred in filter")
ErrInvalidSaltLength = errors.Register("tally", 11, "salt should be 32-byte long")
// Errors from FilterAndTally:
ErrDecodingConsensusFilter = errors.Register("tally", 12, "failed to decode consensus filter")
ErrDecodingPaybackAddress = errors.Register("tally", 13, "failed to decode payback address")
ErrApplyingFilter = errors.Register("tally", 14, "failed to apply filter")
ErrFindingTallyProgram = errors.Register("tally", 15, "failed to find tally program")
ErrDecodingTallyInputs = errors.Register("tally", 16, "failed to decode tally inputs")
ErrConstructingTallyVMArgs = errors.Register("tally", 17, "failed to construct tally VM arguments")
ErrGettingMaxTallyGasLimit = errors.Register("tally", 18, "failed to get max tally gas limit")
)

0 comments on commit 5592b43

Please sign in to comment.