Skip to content

Commit

Permalink
fix apphash mismatch caused by unordered grouping of maps
Browse files Browse the repository at this point in the history
  • Loading branch information
trestinlsd committed Sep 5, 2024
1 parent 85d4b48 commit 358bb83
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
12 changes: 6 additions & 6 deletions x/avs/keeper/impl_epoch_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (wrapper EpochsHooksWrapper) AfterEpochEnd(
// get all the task info bypass the epoch end
// threshold calculation, signature verification, nosig quantity statistics
taskResList := wrapper.keeper.GetTaskStatisticalEpochEndAVSs(ctx, epochIdentifier, epochNumber)

// TODO:There should be a retry mechanism or compensation mechanism to handle cases of failure
if len(taskResList) != 0 {
groupedTasks := wrapper.keeper.GroupTasksByIDAndAddress(taskResList)
for _, value := range groupedTasks {
Expand All @@ -58,7 +58,7 @@ func (wrapper EpochsHooksWrapper) AfterEpochEnd(
}
power, err := wrapper.keeper.operatorKeeper.GetOperatorOptedUSDValue(ctx, avsAddr, res.OperatorAddress)
if err != nil || power.ActiveUSDValue.IsNegative() {
ctx.Logger().Error("Failed to update task result statistics", "task result", taskAddr, "error", err)
ctx.Logger().Error("Failed to update task result statistics,GetOperatorOptedUSDValue call failed!", "task result", taskAddr, "error", err)
// Handle the error gracefully, continue to the next
continue
}
Expand All @@ -73,7 +73,7 @@ func (wrapper EpochsHooksWrapper) AfterEpochEnd(
}
taskInfo, err := wrapper.keeper.GetTaskInfo(ctx, strconv.FormatUint(taskID, 10), taskAddr)
if err != nil {
ctx.Logger().Error("Failed to update task result statistics", "task result", taskAddr, "error", err)
ctx.Logger().Error("Failed to update task result statistics,GetTaskInfo call failed!", "task result", taskAddr, "error", err)
// Handle the error gracefully, continue to the next
continue
}
Expand All @@ -85,14 +85,14 @@ func (wrapper EpochsHooksWrapper) AfterEpochEnd(
taskPowerTotal, err := wrapper.keeper.operatorKeeper.GetAVSUSDValue(ctx, avsAddr)

if err != nil || taskPowerTotal.IsZero() || operatorPowerTotal.IsZero() {
ctx.Logger().Error("Failed to update task result statistics", "task result", taskAddr, "error", err)
ctx.Logger().Error("Failed to update task result statistics,GetAVSUSDValue call failed!", "task result", taskAddr, "error", err)
// Handle the error gracefully, continue to the next
continue
}

actualThreshold := taskPowerTotal.Quo(operatorPowerTotal).Mul(sdk.NewDec(100))
if err != nil {
ctx.Logger().Error("Failed to update task result statistics", "task result", taskAddr, "error", err)
ctx.Logger().Error("Failed to update task result statistics,Calculation of actualThreshold ratio failed!", "task result", taskAddr, "error", err)
// Handle the error gracefully, continue to the next
continue
}
Expand All @@ -102,7 +102,7 @@ func (wrapper EpochsHooksWrapper) AfterEpochEnd(
// Update the taskInfo in the state
err = wrapper.keeper.SetTaskInfo(ctx, taskInfo)
if err != nil {
ctx.Logger().Error("Failed to update task result statistics", "task result", taskAddr, "error", err)
ctx.Logger().Error("Failed to update task result statistics,SetTaskInfo call failed!", "task result", taskAddr, "error", err)
// Handle the error gracefully, continue to the next
continue
}
Expand Down
24 changes: 18 additions & 6 deletions x/avs/keeper/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"bytes"
"fmt"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -251,13 +252,16 @@ func (k *Keeper) SetTaskResultInfo(

// check hash
taskResponseDigest := crypto.Keccak256Hash(info.TaskResponse)
hashWithoutPrefix := strings.TrimPrefix(taskResponseDigest.String(), "0x")
if hashWithoutPrefix != info.TaskResponseHash {
return errorsmod.Wrap(
types.ErrHashValue,
"SetTaskResultInfo: task response is nil",
)
if info.TaskResponseHash != "" {
hashWithoutPrefix := strings.TrimPrefix(taskResponseDigest.String(), "0x")
if hashWithoutPrefix != info.TaskResponseHash {
return errorsmod.Wrap(
types.ErrHashValue,
"SetTaskResultInfo: task response is nil",
)
}
}

// TODO :check taskID
// resp, err := types.UnmarshalTaskResponse(info.TaskResponse)
// if err != nil || info.TaskId != resp.TaskID {
Expand Down Expand Up @@ -344,6 +348,14 @@ func (k Keeper) GroupTasksByIDAndAddress(tasks []types.TaskResultInfo) map[strin
key := task.TaskContractAddress + "_" + strconv.FormatUint(task.TaskId, 10)
taskMap[key] = append(taskMap[key], task)
}

// Sort tasks in each group by OperatorAddress
for key, taskGroup := range taskMap {
sort.Slice(taskGroup, func(i, j int) bool {
return taskGroup[i].OperatorAddress < taskGroup[j].OperatorAddress
})
taskMap[key] = taskGroup
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
return taskMap
}

Expand Down
10 changes: 4 additions & 6 deletions x/avs/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"math/big"
"sort"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down Expand Up @@ -168,16 +169,13 @@ func Difference(a, b []string) []string {
}
}

// Calculate the final size for the different slice
finalSize := len(different) + len(diffMap)

// Pre-allocate the different slice with the final size
different = make([]string, 0, finalSize)

// Add remaining elements from the map to different
for item := range diffMap {
different = append(different, item)
}

// Sort the different slice
sort.Strings(different)

return different
}

0 comments on commit 358bb83

Please sign in to comment.