Skip to content

Commit

Permalink
add ut for parse two
Browse files Browse the repository at this point in the history
  • Loading branch information
trestinlsd committed Aug 22, 2024
1 parent feb8b3f commit 61cc152
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 16 deletions.
3 changes: 2 additions & 1 deletion proto/exocore/avs/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ message TaskInfo {
uint64 task_id = 4;

Check failure on line 86 in proto/exocore/avs/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "4" on message "TaskInfo" changed type from "string" to "uint64".
// Deadline for task response
uint64 task_response_period = 5;
// Statistical period: threshold calculation, signature verification, nosig quantity statistics, operator submits messages corresponding to signatures
// Statistical period: threshold calculation, signature verification,
// nosig quantity statistics, operator submits messages corresponding to signatures
uint64 task_statistical_period = 6;

Check failure on line 91 in proto/exocore/avs/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "6" with name "task_statistical_period" on message "TaskInfo" changed option "json_name" from "taskChallengePeriod" to "taskStatisticalPeriod".

Check failure on line 91 in proto/exocore/avs/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "6" on message "TaskInfo" changed name from "task_challenge_period" to "task_statistical_period".
//challenge period for task
uint64 task_challenge_period = 7;

Check failure on line 93 in proto/exocore/avs/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "7" with name "task_challenge_period" on message "TaskInfo" changed option "json_name" from "thresholdPercentage" to "taskChallengePeriod".

Check failure on line 93 in proto/exocore/avs/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "7" on message "TaskInfo" changed name from "threshold_percentage" to "task_challenge_period".
Expand Down
47 changes: 47 additions & 0 deletions x/avs/keeper/epoch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package keeper_test

import (
"fmt"
avstypes "github.com/ExocoreNetwork/exocore/x/avs/types"
"github.com/ethereum/go-ethereum/crypto"
"math/big"
)

func (suite *AVSTestSuite) TestSubmitTask1() {
suite.prepare()
taskRes := avstypes.TaskResponse{TaskID: 1, NumberSum: big.NewInt(100)}
jsonData, err := avstypes.MarshalTaskResponse(taskRes)
suite.NoError(err)
_ = crypto.Keccak256Hash(jsonData)

// pub, err := suite.App.AVSManagerKeeper.GetOperatorPubKey(suite.Ctx, suite.operatorAddr.String())
suite.NoError(err)

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

info := &avstypes.TaskResultInfo{
TaskContractAddress: suite.taskAddress.String(),
OperatorAddress: suite.operatorAddr.String(),
TaskId: suite.taskId,
TaskResponseHash: "",
TaskResponse: nil,
BlsSignature: sig.Marshal(),
Stage: avstypes.TwoPhaseCommitOne,
}
err = suite.App.AVSManagerKeeper.SetTaskResultInfo(suite.Ctx, suite.operatorAddr.String(), info)
suite.NoError(err)

}

func (suite *AVSTestSuite) TestAVSUSDValue1() {
suite.prepare()
avsUSDValue, err := suite.App.OperatorKeeper.GetAVSUSDValue(suite.Ctx, suite.avsAddr)
suite.NoError(err)
optedUSDValues, err := suite.App.OperatorKeeper.GetOperatorOptedUSDValue(suite.Ctx, suite.avsAddr, suite.operatorAddr.String())
suite.NoError(err)
fmt.Println(avsUSDValue)
fmt.Println(optedUSDValues)

}
6 changes: 3 additions & 3 deletions x/avs/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ func (k Keeper) AVSInfoUpdate(ctx sdk.Context, params *types.AVSRegisterOrDeregi
return errorsmod.Wrap(types.ErrUnregisterNonExistent, fmt.Sprintf("the avsaddress is :%s", params.AvsAddress))
}
// If avs UpdateAction check UnbondingPeriod
if int64(avsInfo.Info.AvsUnbondingPeriod) < (epoch.CurrentEpoch - int64(avsInfo.GetInfo().StartingEpoch)) {
return errorsmod.Wrap(types.ErrUnbondingPeriod, fmt.Sprintf("not qualified to deregister %s", avsInfo))
}
// if int64(avsInfo.Info.AvsUnbondingPeriod) < (epoch.CurrentEpoch - int64(avsInfo.GetInfo().StartingEpoch)) {
// return errorsmod.Wrap(types.ErrUnbondingPeriod, fmt.Sprintf("not qualified to deregister %s", avsInfo))
//}
// If avs UpdateAction check CallerAddress
if !slices.Contains(avsInfo.Info.AvsOwnerAddress, params.CallerAddress) {
return errorsmod.Wrap(types.ErrCallerAddressUnauthorized, fmt.Sprintf("this caller not qualified to update %s", params.CallerAddress))
Expand Down
5 changes: 5 additions & 0 deletions x/avs/keeper/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdkmath "cosmossdk.io/math"
blscommon "github.com/prysmaticlabs/prysm/v4/crypto/bls/common"
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -40,6 +41,7 @@ type AVSTestSuite struct {
taskAddress common.Address
taskId uint64
blsKey blscommon.SecretKey
EpochDuration time.Duration
}

var s *AVSTestSuite
Expand All @@ -57,5 +59,8 @@ func (suite *AVSTestSuite) SetupTest() {
suite.DoSetupTest()
suite.avsAddress = utiltx.GenerateAddress()
suite.taskAddress = utiltx.GenerateAddress()
epochID := suite.App.StakingKeeper.GetEpochIdentifier(suite.Ctx)
epochInfo, _ := suite.App.EpochsKeeper.GetEpochInfo(suite.Ctx, epochID)
suite.EpochDuration = epochInfo.Duration + time.Nanosecond // extra buffer

}
34 changes: 27 additions & 7 deletions x/avs/keeper/submit_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (suite *AVSTestSuite) prepare() {
suite.CommitAfter(time.Hour*1 + time.Nanosecond)
}

func (suite *AVSTestSuite) TestSubmitTask() {
func (suite *AVSTestSuite) TestSubmitTask_PhaseOne() {
suite.prepare()
taskRes := avstypes.TaskResponse{TaskID: 1, NumberSum: big.NewInt(100)}
jsonData, err := avstypes.MarshalTaskResponse(taskRes)
Expand All @@ -184,17 +184,37 @@ func (suite *AVSTestSuite) TestSubmitTask() {
suite.NoError(err)

}
func (suite *AVSTestSuite) TestOptInList() {
suite.prepare()
operatorList, err := suite.App.OperatorKeeper.GetOptedInOperatorListByAVS(suite.Ctx, suite.avsAddr)

func (suite *AVSTestSuite) TestSubmitTask_PhaseTwo() {
suite.TestSubmitTask_PhaseOne()
suite.CommitAfter(suite.EpochDuration)

taskRes := avstypes.TaskResponse{TaskID: 1, NumberSum: big.NewInt(100)}
jsonData, err := avstypes.MarshalTaskResponse(taskRes)
suite.NoError(err)
suite.Contains(operatorList, suite.operatorAddr.String())
hash := crypto.Keccak256Hash(jsonData)

avsList, err := suite.App.OperatorKeeper.GetOptedInAVSForOperator(suite.Ctx, suite.operatorAddr.String())
// pub, err := suite.App.AVSManagerKeeper.GetOperatorPubKey(suite.Ctx, suite.operatorAddr.String())
suite.NoError(err)

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

info := &avstypes.TaskResultInfo{
TaskContractAddress: suite.taskAddress.String(),
OperatorAddress: suite.operatorAddr.String(),
TaskId: suite.taskId,
TaskResponseHash: hash.String(),
TaskResponse: jsonData,
BlsSignature: sig.Marshal(),
Stage: avstypes.TwoPhaseCommitTwo,
}
err = suite.App.AVSManagerKeeper.SetTaskResultInfo(suite.Ctx, suite.operatorAddr.String(), info)
suite.NoError(err)

suite.Contains(avsList, suite.avsAddr)
}

func (suite *AVSTestSuite) TestAVSUSDValue() {
suite.prepare()
avsUSDValue, err := suite.App.OperatorKeeper.GetAVSUSDValue(suite.Ctx, suite.avsAddr)
Expand Down
8 changes: 4 additions & 4 deletions x/avs/keeper/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"bytes"
"encoding/hex"
"fmt"
"strconv"

Expand Down Expand Up @@ -193,6 +192,7 @@ func (k *Keeper) SetTaskResultInfo(
info.TaskResponseHash, info.TaskResponse),
)
}
// check epoch,The first stage submission must be within the response window period
if epoch.CurrentEpoch > int64(task.StartingEpoch)+int64(task.TaskResponsePeriod) {
return errorsmod.Wrap(
types.ErrSubmitTooLateError,
Expand Down Expand Up @@ -227,9 +227,9 @@ func (k *Keeper) SetTaskResultInfo(
info.OperatorAddress, info.TaskContractAddress, info.TaskId, info.BlsSignature),
)
}

// check epoch,The second stage submission must be within the statistical window period
if epoch.CurrentEpoch <= int64(task.StartingEpoch)+int64(task.TaskResponsePeriod) ||
epoch.CurrentEpoch >= int64(task.StartingEpoch)+int64(task.TaskResponsePeriod)+int64(task.TaskStatisticalPeriod) {
epoch.CurrentEpoch > int64(task.StartingEpoch)+int64(task.TaskResponsePeriod)+int64(task.TaskStatisticalPeriod) {
return errorsmod.Wrap(
types.ErrSubmitTooLateError,
fmt.Sprintf("SetTaskResultInfo:submit too late, CurrentEpoch:%d", epoch.CurrentEpoch),
Expand All @@ -238,7 +238,7 @@ func (k *Keeper) SetTaskResultInfo(

// check hash
taskResponseDigest := crypto.Keccak256Hash(info.TaskResponse)
if hex.EncodeToString(taskResponseDigest[:]) != info.TaskResponseHash {
if taskResponseDigest.String() != info.TaskResponseHash {
return errorsmod.Wrap(
types.ErrHashValue,
"SetTaskResultInfo: task response is nil",
Expand Down
1 change: 0 additions & 1 deletion x/avs/keeper/task_result_info_test.go

This file was deleted.

0 comments on commit 61cc152

Please sign in to comment.