Skip to content

Commit

Permalink
fix test err
Browse files Browse the repository at this point in the history
  • Loading branch information
trestinlsd committed Sep 6, 2024
1 parent 358bb83 commit 7b3bd60
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 53 deletions.
21 changes: 7 additions & 14 deletions precompiles/avs/avs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (suite *AVSManagerPrecompileSuite) TestRegisterAVS() {
commonMalleate := func() (common.Address, []byte) {
input, err := suite.precompile.Pack(
avs.MethodRegisterAVS,
suite.Address,
avsName,
minStakeAmount,
common.HexToAddress(taskAddr),
Expand Down Expand Up @@ -210,6 +211,7 @@ func (suite *AVSManagerPrecompileSuite) TestDeregisterAVS() {
// prepare the call input for delegation test
input, err := suite.precompile.Pack(
avs.MethodDeregisterAVS,
suite.Address,
avsName,
)
suite.Require().NoError(err, "failed to pack input")
Expand Down Expand Up @@ -312,13 +314,14 @@ func (suite *AVSManagerPrecompileSuite) TestUpdateAVS() {
sdk.AccAddress(utiltx.GenerateAddress().Bytes()).String(),
}
assetID := []string{"11", "22", "33"}
minStakeAmount, taskAddr := uint64(3), "0xDF907c29719154eb9872f021d21CAE6E5025d7aB"
minStakeAmount, taskAddr := uint64(3), "0x3e108c058e8066DA635321Dc3018294cA82ddEdf"
avsUnbondingPeriod, minSelfDelegation := uint64(3), uint64(3)
epochIdentifier := epochstypes.DayEpochID
params := []uint64{2, 3, 4, 4}
commonMalleate := func() (common.Address, []byte) {
input, err := suite.precompile.Pack(
avs.MethodUpdateAVS,
suite.Address,
avsName,
minStakeAmount,
common.HexToAddress(taskAddr),
Expand Down Expand Up @@ -441,6 +444,7 @@ func (suite *AVSManagerPrecompileSuite) TestRegisterOperatorToAVS() {
commonMalleate := func() (common.Address, []byte) {
input, err := suite.precompile.Pack(
avs.MethodRegisterOperatorToAVS,
suite.Address,
)
suite.Require().NoError(err, "failed to pack input")
return common.HexToAddress("0x3e108c058e8066DA635321Dc3018294cA82ddEdf"), input
Expand Down Expand Up @@ -536,22 +540,10 @@ func (suite *AVSManagerPrecompileSuite) TestRegisterOperatorToAVS() {
}

func (suite *AVSManagerPrecompileSuite) TestDeregisterOperatorFromAVS() {
// from := s.Address
// operatorAddress, err := util.ProcessAddress(from.String())

// registerOperator := func() {
// registerReq := &operatortypes.RegisterOperatorReq{
// FromAddress: operatorAddress,
// Info: &operatortypes.OperatorInfo{
// EarningsAddr: operatorAddress,
// },
// }
// _, err := s.OperatorMsgServer.RegisterOperator(sdk.WrapSDKContext(s.Ctx), registerReq)
// s.NoError(err)
// }
commonMalleate := func() (common.Address, []byte) {
input, err := suite.precompile.Pack(
avs.MethodDeregisterOperatorFromAVS,
suite.Address,
)
suite.Require().NoError(err, "failed to pack input")
return common.HexToAddress("0x3e108c058e8066DA635321Dc3018294cA82ddEdf"), input
Expand Down Expand Up @@ -680,6 +672,7 @@ func (suite *AVSManagerPrecompileSuite) TestRunRegTaskInfo() {
commonMalleate := func() (common.Address, []byte) {
input, err := suite.precompile.Pack(
avs.MethodCreateAVSTask,
suite.Address,
"test-avstask",
rand.Bytes(3),
uint64(3),
Expand Down
3 changes: 3 additions & 0 deletions x/avs/keeper/avs.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func (k *Keeper) GetEpochEndAVSs(ctx sdk.Context, epochIdentifier string, ending
// TODO:this function is frequently used while its implementation iterates over existing avs to find the target avs by task contract address, we should use a reverse mapping to avoid iteration
func (k *Keeper) GetAVSInfoByTaskAddress(ctx sdk.Context, taskAddr string) types.AVSInfo {
avs := types.AVSInfo{}
if taskAddr == "" {
return avs
}
k.IterateAVSInfo(ctx, func(_ int64, avsInfo types.AVSInfo) (stop bool) {
if taskAddr == avsInfo.GetTaskAddr() {
avs = avsInfo
Expand Down
16 changes: 2 additions & 14 deletions x/avs/keeper/epoch_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package keeper_test

import (
sdkmath "cosmossdk.io/math"
avstypes "github.com/ExocoreNetwork/exocore/x/avs/types"
operatortypes "github.com/ExocoreNetwork/exocore/x/operator/types"
"github.com/ethereum/go-ethereum/common"
"strconv"
)
Expand All @@ -20,19 +18,9 @@ func (suite *AVSTestSuite) Test_GroupStatistics() {
}
func (suite *AVSTestSuite) TestEpochEnd_TaskCalculation() {
suite.TestSubmitTask_OnlyPhaseTwo_Mul()
err := suite.App.OperatorKeeper.SetAVSUSDValue(suite.Ctx, suite.avsAddr, sdkmath.LegacyNewDec(500))
for _, operatorAddress := range suite.operatorAddresses {
delta := operatortypes.DeltaOperatorUSDInfo{
SelfUSDValue: sdkmath.LegacyNewDec(100),
TotalUSDValue: sdkmath.LegacyNewDec(100),
ActiveUSDValue: sdkmath.LegacyNewDec(100),
}
suite.App.OperatorKeeper.UpdateOperatorUSDValue(suite.Ctx, suite.avsAddr, operatorAddress, delta)
}

suite.NoError(err)
suite.CommitAfter(suite.EpochDuration)

suite.CommitAfter(suite.EpochDuration)
suite.CommitAfter(suite.EpochDuration)
info, err := suite.App.AVSManagerKeeper.GetTaskInfo(suite.Ctx, strconv.FormatUint(suite.taskId, 10), common.Address(suite.taskAddress.Bytes()).String())
suite.NoError(err)
expectInfo := &avstypes.TaskInfo{
Expand Down
14 changes: 7 additions & 7 deletions x/avs/keeper/impl_epoch_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (wrapper EpochsHooksWrapper) AfterEpochEnd(
if err != nil || power.ActiveUSDValue.IsNegative() {
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
// continue
}

operatorSelfPower := &types.OperatorActivePowerInfo{
Expand All @@ -75,7 +75,7 @@ func (wrapper EpochsHooksWrapper) AfterEpochEnd(
if err != nil {
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
// continue
}
diff := types.Difference(taskInfo.OptInOperators, signedOperatorList)
taskInfo.SignedOperators = signedOperatorList
Expand All @@ -87,24 +87,24 @@ func (wrapper EpochsHooksWrapper) AfterEpochEnd(
if err != nil || taskPowerTotal.IsZero() || operatorPowerTotal.IsZero() {
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
// continue
}

actualThreshold := taskPowerTotal.Quo(operatorPowerTotal).Mul(sdk.NewDec(100))
// actualThreshold := taskPowerTotal.Quo(operatorPowerTotal).Mul(sdk.NewDec(100))
if err != nil {
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
// continue
}
taskInfo.TaskTotalPower = taskPowerTotal
taskInfo.ActualThreshold = actualThreshold.BigInt().Uint64()
// taskInfo.ActualThreshold = actualThreshold.BigInt().Uint64()

// Update the taskInfo in the state
err = wrapper.keeper.SetTaskInfo(ctx, taskInfo)
if err != nil {
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
// continue
}
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
}
Expand Down
10 changes: 6 additions & 4 deletions x/avs/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,14 @@ func (k Keeper) UpdateAVSInfo(ctx sdk.Context, params *types.AVSRegisterOrDeregi
if !found {
return errorsmod.Wrap(types.ErrEpochNotFound, fmt.Sprintf("epoch info not found %s", epochIdentifier))
}

if k.GetAVSInfoByTaskAddress(ctx, params.TaskAddr).AvsAddress != "" {
return errorsmod.Wrap(types.ErrAlreadyRegistered, fmt.Sprintf("this TaskAddr has already been used by other AVS,the TaskAddr is :%s", params.TaskAddr))
}
switch action {
case RegisterAction:
if avsInfo != nil {
return errorsmod.Wrap(types.ErrAlreadyRegistered, fmt.Sprintf("the avsaddress is :%s", params.AvsAddress))
}
if k.GetAVSInfoByTaskAddress(ctx, params.TaskAddr).AvsAddress != "" {
return errorsmod.Wrap(types.ErrAlreadyRegistered, fmt.Sprintf("this TaskAddr has already been used by other AVS,the TaskAddr is :%s", params.TaskAddr))
}
startingEpoch := uint64(epoch.CurrentEpoch + 1)
if params.ChainID == types.ChainIDWithoutRevision(ctx.ChainID()) {
// TODO: handle this better
Expand Down Expand Up @@ -131,6 +130,9 @@ func (k Keeper) UpdateAVSInfo(ctx sdk.Context, params *types.AVSRegisterOrDeregi
if avsInfo == nil {
return errorsmod.Wrap(types.ErrUnregisterNonExistent, fmt.Sprintf("the avsaddress is :%s", params.AvsAddress))
}
if k.GetAVSInfoByTaskAddress(ctx, params.TaskAddr).AvsAddress != "" {
return errorsmod.Wrap(types.ErrAlreadyRegistered, fmt.Sprintf("this TaskAddr has already been used by other AVS,the TaskAddr is :%s", params.TaskAddr))
}
// TODO: The AvsUnbondingPeriod is used for undelegation, but this check currently blocks updates to AVS information. Remove this check to allow AVS updates, while detailed control mechanisms for updates should be considered and implemented in the future.
// If avs UpdateAction check UnbondingPeriod

Expand Down
14 changes: 12 additions & 2 deletions x/avs/keeper/multi_operator_submit_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ func (suite *AVSTestSuite) prepareMul() {
suite.prepareMulOptIn()
suite.prepareMulOperatorubkey()
suite.prepareMulTaskInfo()
suite.App.OperatorKeeper.SetAVSUSDValue(suite.Ctx, suite.avsAddr, sdkmath.LegacyNewDec(500))
for _, operatorAddress := range suite.operatorAddresses {
delta := operatorTypes.DeltaOperatorUSDInfo{
SelfUSDValue: sdkmath.LegacyNewDec(100),
TotalUSDValue: sdkmath.LegacyNewDec(100),
ActiveUSDValue: sdkmath.LegacyNewDec(100),
}
suite.App.OperatorKeeper.UpdateOperatorUSDValue(suite.Ctx, suite.avsAddr, operatorAddress, delta)
}

suite.CommitAfter(time.Hour*1 + time.Nanosecond)
suite.CommitAfter(time.Hour*1 + time.Nanosecond)
suite.CommitAfter(time.Hour*1 + time.Nanosecond)
Expand All @@ -176,7 +186,7 @@ func (suite *AVSTestSuite) TestSubmitTask_OnlyPhaseOne_Mul() {
suite.prepareMul()
for index, operatorAddress := range suite.operatorAddresses {
taskRes := avstypes.TaskResponse{TaskID: 1, NumberSum: big.NewInt(100)}
msg, _ := avstypes.GetTaskResponseDigestEncodeByAbi(taskRes)
msg, _ := avstypes.GetTaskResponseDigestEncodeByjson(taskRes)
msgBytes := msg[:]
sig := suite.blsKeys[index].Sign(msgBytes)

Expand Down Expand Up @@ -205,7 +215,7 @@ func (suite *AVSTestSuite) TestSubmitTask_OnlyPhaseTwo_Mul() {
hash := crypto.Keccak256Hash(jsonData)
// pub, err := suite.App.AVSManagerKeeper.GetOperatorPubKey(suite.Ctx, suite.operatorAddr.String())
suite.NoError(err)
msg, _ := avstypes.GetTaskResponseDigestEncodeByAbi(taskRes)
msg, _ := avstypes.GetTaskResponseDigestEncodeByjson(taskRes)
msgBytes := msg[:]
sig := suite.blsKeys[index].Sign(msgBytes)

Expand Down
4 changes: 2 additions & 2 deletions x/avs/keeper/submit_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (suite *AVSTestSuite) TestSubmitTask_OnlyPhaseOne() {
// pub, err := suite.App.AVSManagerKeeper.GetOperatorPubKey(suite.Ctx, suite.operatorAddr.String())
suite.NoError(err)

msg, _ := avstypes.GetTaskResponseDigestEncodeByAbi(taskRes)
msg, _ := avstypes.GetTaskResponseDigestEncodeByjson(taskRes)
msgBytes := msg[:]
sig := suite.blsKey.Sign(msgBytes)

Expand Down Expand Up @@ -197,7 +197,7 @@ func (suite *AVSTestSuite) TestSubmitTask_OnlyPhaseTwo() {
// pub, err := suite.App.AVSManagerKeeper.GetOperatorPubKey(suite.Ctx, suite.operatorAddr.String())
suite.NoError(err)

msg, _ := avstypes.GetTaskResponseDigestEncodeByAbi(taskRes)
msg, _ := avstypes.GetTaskResponseDigestEncodeByjson(taskRes)
msgBytes := msg[:]
sig := suite.blsKey.Sign(msgBytes)

Expand Down
19 changes: 9 additions & 10 deletions x/avs/keeper/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"sort"
"strconv"
"strings"

"github.com/ethereum/go-ethereum/crypto"

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

// check hash
taskResponseDigest := crypto.Keccak256Hash(info.TaskResponse)
if info.TaskResponseHash != "" {
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)
Expand Down

0 comments on commit 7b3bd60

Please sign in to comment.