Skip to content

Commit

Permalink
fix set task result info err
Browse files Browse the repository at this point in the history
  • Loading branch information
trestinlsd committed Nov 1, 2024
1 parent 7e54c08 commit 7b8b7ad
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 173 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -994,8 +994,8 @@ func NewExocoreApp(
evmtypes.ModuleName, // must be before avs, since dogfood calls avs which calls this
exominttypes.ModuleName,
assetsTypes.ModuleName,
avsManagerTypes.ModuleName, // before dogfood, since dogfood registers itself as an AVS
operatorTypes.ModuleName, // must be before delegation
avsManagerTypes.ModuleName, // before dogfood, since dogfood registers itself as an AVS
delegationTypes.ModuleName,
stakingtypes.ModuleName, // must be after delegation
// must be after staking to `IterateValidators` but it is not implemented anyway
Expand Down
5 changes: 1 addition & 4 deletions x/avs/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) {
}
// Set all the task result infos
for _, elem := range state.TaskResultInfos {
err := k.SetTaskResultInfo(ctx, elem.OperatorAddress, &elem) //nolint:gosec
if err != nil {
panic(errorsmod.Wrap(err, "failed to set all task result info"))
}
k.SetTaskResultInfo(ctx, &elem)
}
// Set all the task challenge infos
err := k.SetAllTaskChallengedInfo(ctx, state.ChallengeInfos)
Expand Down
152 changes: 152 additions & 0 deletions x/avs/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package keeper

import (
"bytes"
"encoding/hex"
"fmt"
"github.com/ethereum/go-ethereum/crypto"

Check failure on line 7 in x/avs/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

File is not `gofumpt`-ed (gofumpt)
"slices"
"strconv"

Check failure on line 9 in x/avs/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

File is not `gofumpt`-ed (gofumpt)

Expand Down Expand Up @@ -462,3 +464,153 @@ func (k Keeper) RaiseAndResolveChallenge(ctx sdk.Context, params *ChallengeParam
return k.SetTaskChallengedInfo(ctx, params.TaskID, params.OperatorAddress.String(), params.CallerAddress,
params.TaskContractAddress)
}

func (k Keeper) SubmitTaskResult(ctx sdk.Context, addr string, info *types.TaskResultInfo) error {
// the operator's `addr` must match the from address.
if addr != info.OperatorAddress {
return errorsmod.Wrap(
types.ErrInvalidAddr,
"SetTaskResultInfo:from address is not equal to the operator address",
)
}
opAccAddr, _ := sdk.AccAddressFromBech32(info.OperatorAddress)
// check operator
if !k.operatorKeeper.IsOperator(ctx, opAccAddr) {
return errorsmod.Wrap(
delegationtypes.ErrOperatorNotExist,
fmt.Sprintf("SetTaskResultInfo:invalid operator address:%s", opAccAddr),
)
}
// check operator bls pubkey
keyInfo, err := k.GetOperatorPubKey(ctx, info.OperatorAddress)
if err != nil || keyInfo.PubKey == nil {
return errorsmod.Wrap(
types.ErrPubKeyIsNotExists,
fmt.Sprintf("SetTaskResultInfo:get operator address:%s", opAccAddr),
)
}
pubKey, err := blst.PublicKeyFromBytes(keyInfo.PubKey)
if err != nil || pubKey == nil {
return errorsmod.Wrap(
types.ErrParsePubKey,
fmt.Sprintf("SetTaskResultInfo:get operator address:%s", opAccAddr),
)
}
// check task contract
task, err := k.GetTaskInfo(ctx, strconv.FormatUint(info.TaskId, 10), info.TaskContractAddress)
if err != nil || task.TaskContractAddress == "" {
return errorsmod.Wrap(
types.ErrTaskIsNotExists,
fmt.Sprintf("SetTaskResultInfo: task info not found: %s (Task ID: %d)",
info.TaskContractAddress, info.TaskId),
)
}

// check prescribed period
// If submitted in the first stage, in order to avoid plagiarism by other operators,
// TaskResponse and TaskResponseHash must be null values
// At the same time, it must be submitted within the response deadline in the first stage
avsInfo := k.GetAVSInfoByTaskAddress(ctx, info.TaskContractAddress)
epoch, found := k.epochsKeeper.GetEpochInfo(ctx, avsInfo.EpochIdentifier)
if !found {
return errorsmod.Wrap(types.ErrEpochNotFound, fmt.Sprintf("epoch info not found %s",
avsInfo.EpochIdentifier))
}

switch info.Stage {
case types.TwoPhaseCommitOne:
if k.IsExistTaskResultInfo(ctx, info.OperatorAddress, info.TaskContractAddress, info.TaskId) {
return errorsmod.Wrap(
types.ErrResAlreadyExists,
fmt.Sprintf("SetTaskResultInfo: task result is already exists, "+
"OperatorAddress: %s (TaskContractAddress: %s),(Task ID: %d)",
info.OperatorAddress, info.TaskContractAddress, info.TaskId),
)
}
// check parameters
if info.BlsSignature == nil {
return errorsmod.Wrap(
types.ErrParamNotEmptyError,
fmt.Sprintf("SetTaskResultInfo: invalid param BlsSignature is not be null (BlsSignature: %s)", info.BlsSignature),
)
}
if info.TaskResponseHash != "" || info.TaskResponse != nil {
return errorsmod.Wrap(
types.ErrParamNotEmptyError,
fmt.Sprintf("SetTaskResultInfo: invalid param TaskResponseHash: %s (TaskResponse: %d)",
info.TaskResponseHash, info.TaskResponse),
)
}
// check epoch,The first stage submission must be within the response window period
// #nosec G115
if epoch.CurrentEpoch > int64(task.StartingEpoch)+int64(task.TaskResponsePeriod) {
return errorsmod.Wrap(
types.ErrSubmitTooLateError,
fmt.Sprintf("SetTaskResultInfo:submit too late, CurrentEpoch:%d", epoch.CurrentEpoch),
)
}
k.SetTaskResultInfo(ctx, info)
return nil

case types.TwoPhaseCommitTwo:
// check task response
if info.TaskResponse == nil {
return errorsmod.Wrap(
types.ErrNotNull,
fmt.Sprintf("SetTaskResultInfo: invalid param (TaskResponse: %d)",
info.TaskResponse),
)
}
// check parameters
res, err := k.GetTaskResultInfo(ctx, info.OperatorAddress, info.TaskContractAddress, info.TaskId)
if err != nil || !bytes.Equal(res.BlsSignature, info.BlsSignature) {
return errorsmod.Wrap(
types.ErrInconsistentParams,
fmt.Sprintf("SetTaskResultInfo: invalid param OperatorAddress: %s ,(TaskContractAddress: %s)"+
",(TaskId: %d),(BlsSignature: %s)",
info.OperatorAddress, info.TaskContractAddress, info.TaskId, info.BlsSignature),
)
}
// check epoch,The second stage submission must be within the statistical window period
// #nosec G115
if epoch.CurrentEpoch <= int64(task.StartingEpoch)+int64(task.TaskResponsePeriod) {
return errorsmod.Wrap(
types.ErrSubmitTooSoonError,
fmt.Sprintf("SetTaskResultInfo:the TaskResponse period has not started , CurrentEpoch:%d", epoch.CurrentEpoch),
)
}
if 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),
)
}

// calculate hash by original task
taskResponseDigest := crypto.Keccak256Hash(info.TaskResponse)
info.TaskResponseHash = taskResponseDigest.String()
// check taskID
resp, err := types.UnmarshalTaskResponse(info.TaskResponse)
if err != nil || info.TaskId != resp.TaskID {
return errorsmod.Wrap(
types.ErrInconsistentParams,
fmt.Sprintf("SetTaskResultInfo: invalid TaskId param value:%s", info.TaskResponse),
)
}
// check bls sig
flag, err := blst.VerifySignature(info.BlsSignature, taskResponseDigest, pubKey)
if !flag || err != nil {
return errorsmod.Wrap(
types.ErrSigVerifyError,
fmt.Sprintf("SetTaskResultInfo: invalid task address: %s (Task ID: %d)", info.TaskContractAddress, info.TaskId),
)
}
k.SetTaskResultInfo(ctx, info)
return nil
default:
return errorsmod.Wrap(
types.ErrParamError,
fmt.Sprintf("SetTaskResultInfo: invalid param value:%s", info.Stage),
)
}
}
2 changes: 1 addition & 1 deletion x/avs/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var _ types.MsgServer = &MsgServerImpl{}
func (m MsgServerImpl) SubmitTaskResult(goCtx context.Context, req *types.SubmitTaskResultReq) (*types.SubmitTaskResultResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

if err := m.keeper.SetTaskResultInfo(ctx, req.FromAddress, req.Info); err != nil {
if err := m.keeper.SubmitTaskResult(ctx, req.FromAddress, req.Info); err != nil {
return nil, err
}
return &types.SubmitTaskResultResponse{}, nil
Expand Down
4 changes: 2 additions & 2 deletions x/avs/keeper/multi_operator_submit_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (suite *AVSTestSuite) TestSubmitTask_OnlyPhaseOne_Mul() {
BlsSignature: sig.Marshal(),
Stage: avstypes.TwoPhaseCommitOne,
}
err := suite.App.AVSManagerKeeper.SetTaskResultInfo(suite.Ctx, operatorAddress, info)
err := suite.App.AVSManagerKeeper.SubmitTaskResult(suite.Ctx, operatorAddress, info)
suite.Require().NoError(err)
}
}
Expand Down Expand Up @@ -232,7 +232,7 @@ func (suite *AVSTestSuite) TestSubmitTask_OnlyPhaseTwo_Mul() {
BlsSignature: sig.Marshal(),
Stage: avstypes.TwoPhaseCommitTwo,
}
err = suite.App.AVSManagerKeeper.SetTaskResultInfo(suite.Ctx, operatorAddress, info)
err = suite.App.AVSManagerKeeper.SubmitTaskResult(suite.Ctx, operatorAddress, info)
suite.NoError(err)
}
}
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 @@ -190,7 +190,7 @@ func (suite *AVSTestSuite) TestSubmitTask_OnlyPhaseOne() {
BlsSignature: sig.Marshal(),
Stage: avstypes.TwoPhaseCommitOne,
}
err = suite.App.AVSManagerKeeper.SetTaskResultInfo(suite.Ctx, suite.operatorAddr.String(), info)
err = suite.App.AVSManagerKeeper.SubmitTaskResult(suite.Ctx, suite.operatorAddr.String(), info)
suite.NoError(err)
}

Expand Down Expand Up @@ -219,6 +219,6 @@ func (suite *AVSTestSuite) TestSubmitTask_OnlyPhaseTwo() {
BlsSignature: sig.Marshal(),
Stage: avstypes.TwoPhaseCommitTwo,
}
err = suite.App.AVSManagerKeeper.SetTaskResultInfo(suite.Ctx, suite.operatorAddr.String(), info)
err = suite.App.AVSManagerKeeper.SubmitTaskResult(suite.Ctx, suite.operatorAddr.String(), info)
suite.NoError(err)
}
Loading

0 comments on commit 7b8b7ad

Please sign in to comment.