Skip to content

Commit

Permalink
add task TwoPhaseCommitOne UT
Browse files Browse the repository at this point in the history
  • Loading branch information
trestinlsd committed Aug 21, 2024
1 parent 879c9d7 commit 326a200
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 117 deletions.
4 changes: 2 additions & 2 deletions x/avs/keeper/bls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func (suite *AVSTestSuite) TestOperator_pubkey() {
suite.NoError(err)
suite.Equal(publicKey.Marshal(), pub.PubKey)

taskres := types.TaskResponse{TaskId: 1, NumberSum: big.NewInt(100)}
taskRes := types.TaskResponse{TaskId: 1, NumberSum: big.NewInt(100)}

msg, _ := types.GetTaskResponseDigest(taskres)
msg, _ := types.GetTaskResponseDigest(taskRes)
msgBytes := msg[:]
sig := privateKey.Sign(msgBytes)

Expand Down
6 changes: 6 additions & 0 deletions x/avs/keeper/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper_test

import (
sdkmath "cosmossdk.io/math"
blscommon "github.com/prysmaticlabs/prysm/v4/crypto/bls/common"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -36,6 +37,9 @@ type AVSTestSuite struct {
queryClientEvm evm.QueryClient
consAddress sdk.ConsAddress
avsAddress common.Address
taskAddress common.Address
taskId uint64
blsKey blscommon.SecretKey
}

var s *AVSTestSuite
Expand All @@ -52,4 +56,6 @@ func TestKeeperTestSuite(t *testing.T) {
func (suite *AVSTestSuite) SetupTest() {
suite.DoSetupTest()
suite.avsAddress = utiltx.GenerateAddress()
suite.taskAddress = utiltx.GenerateAddress()

}
94 changes: 90 additions & 4 deletions x/avs/keeper/submit_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
operatorTypes "github.com/ExocoreNetwork/exocore/x/operator/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/prysmaticlabs/prysm/v4/crypto/bls/blst"
"math/big"
"strconv"
"time"
)

Expand Down Expand Up @@ -72,18 +76,71 @@ func (suite *AVSTestSuite) prepareDelegation(isDelegation bool, assetAddr common
}
func (suite *AVSTestSuite) prepareAvs(assetIDs []string) {
err := suite.App.AVSManagerKeeper.AVSInfoUpdate(suite.Ctx, &avstypes.AVSRegisterOrDeregisterParams{
Action: avskeeper.RegisterAction,
EpochIdentifier: epochstypes.HourEpochID,
AvsAddress: suite.avsAddr,
AssetID: assetIDs,
AvsName: "avs01",
Action: avskeeper.RegisterAction,
EpochIdentifier: epochstypes.HourEpochID,
AvsAddress: suite.avsAddr,
AssetID: assetIDs,
TaskAddr: suite.taskAddress.String(),
SlashContractAddr: "",
RewardContractAddr: "",
MinSelfDelegation: 3,
AvsOwnerAddress: nil,
UnbondingPeriod: 7,
MinOptInOperators: 3,
MinStakeAmount: 2,
MinTotalStakeAmount: 2,
AvsSlash: 2,
AvsReward: 3,
})

suite.NoError(err)
}
func (suite *AVSTestSuite) prepareOptIn() {
err := suite.App.OperatorKeeper.OptIn(suite.Ctx, suite.operatorAddr, suite.avsAddr)
suite.NoError(err)
suite.CommitAfter(time.Hour*1 + time.Nanosecond)
suite.CommitAfter(time.Hour*1 + time.Nanosecond)
suite.CommitAfter(time.Hour*1 + time.Nanosecond)
}
func (suite *AVSTestSuite) prepareOperatorubkey() {
privateKey, err := blst.RandKey()
suite.blsKey = privateKey
publicKey := privateKey.PublicKey()
blsPub := &avstypes.BlsPubKeyInfo{
Operator: suite.operatorAddr.String(),
PubKey: publicKey.Marshal(),
Name: "",
}

err = suite.App.AVSManagerKeeper.SetOperatorPubKey(suite.Ctx, blsPub)
suite.NoError(err)
}
func (suite *AVSTestSuite) prepareTaskInfo() {
suite.taskId = suite.App.AVSManagerKeeper.GetTaskID(suite.Ctx, suite.taskAddress)
epoch, _ := suite.App.EpochsKeeper.GetEpochInfo(suite.Ctx, epochstypes.HourEpochID)
operatorList, err := suite.App.OperatorKeeper.GetOptedInOperatorListByAVS(suite.Ctx, suite.avsAddr)

info := &avstypes.TaskInfo{
TaskContractAddress: suite.taskAddress.String(),
Name: "test-avsTask",
TaskId: suite.taskId,
Hash: []byte("req-struct"),
TaskResponsePeriod: 2,
TaskStatisticalPeriod: 1,
TaskChallengePeriod: 2,
ThresholdPercentage: 60,
StartingEpoch: uint64(epoch.CurrentEpoch + 1),
ActualThreshold: 0,
OptInOperators: operatorList,
}
err = suite.App.AVSManagerKeeper.SetTaskInfo(suite.Ctx, info)
suite.NoError(err)

getTaskInfo, err := suite.App.AVSManagerKeeper.GetTaskInfo(suite.Ctx, strconv.FormatUint(suite.taskId, 10), common.Address(suite.taskAddress.Bytes()).String())
suite.NoError(err)
suite.Equal(*info, *getTaskInfo)
}
func (suite *AVSTestSuite) prepare() {
usdtAddress := common.HexToAddress("0xdAC17F958D2ee523a2206206994597C13D831ec7")
depositAmount := sdkmath.NewInt(100)
Expand All @@ -93,11 +150,40 @@ func (suite *AVSTestSuite) prepare() {
suite.prepareDelegation(true, usdtAddress, delegationAmount)
suite.prepareAvs([]string{"0xdac17f958d2ee523a2206206994597c13d831ec7_0x65"})
suite.prepareOptIn()
suite.prepareOperatorubkey()
suite.prepareTaskInfo()
suite.CommitAfter(time.Hour*1 + time.Nanosecond)
suite.CommitAfter(time.Hour*1 + time.Nanosecond)
suite.CommitAfter(time.Hour*1 + time.Nanosecond)
}

func (suite *AVSTestSuite) TestSubmitTask() {
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) TestOptInList() {
suite.prepare()
operatorList, err := suite.App.OperatorKeeper.GetOptedInOperatorListByAVS(suite.Ctx, suite.avsAddr)
Expand Down
14 changes: 5 additions & 9 deletions x/avs/keeper/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import (
"bytes"
"encoding/hex"
"fmt"
"github.com/ethereum/go-ethereum/crypto"
"strconv"

errorsmod "cosmossdk.io/errors"

assetstype "github.com/ExocoreNetwork/exocore/x/assets/types"
delegationtypes "github.com/ExocoreNetwork/exocore/x/delegation/types"
"github.com/ethereum/go-ethereum/common"
"github.com/prysmaticlabs/prysm/v4/crypto/bls/blst"
"golang.org/x/crypto/sha3"

"github.com/ExocoreNetwork/exocore/x/avs/types"
delegationtypes "github.com/ExocoreNetwork/exocore/x/delegation/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/prysmaticlabs/prysm/v4/crypto/bls/blst"
)

func (k Keeper) SetTaskInfo(ctx sdk.Context, task *types.TaskInfo) (err error) {
Expand Down Expand Up @@ -237,10 +236,7 @@ func (k *Keeper) SetTaskResultInfo(
}

// check hash
var taskResponseDigest [32]byte
hasher := sha3.NewLegacyKeccak256()
hasher.Write(info.TaskResponse)
copy(taskResponseDigest[:], hasher.Sum(nil)[:32])
taskResponseDigest := crypto.Keccak256Hash(info.TaskResponse)
if hex.EncodeToString(taskResponseDigest[:]) != info.TaskResponseHash {
return errorsmod.Wrap(
types.ErrHashValue,
Expand Down
95 changes: 0 additions & 95 deletions x/avs/keeper/task_result_info_test.go
Original file line number Diff line number Diff line change
@@ -1,96 +1 @@
package keeper

import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/prysmaticlabs/prysm/v4/crypto/bls/blst"
blscommon "github.com/prysmaticlabs/prysm/v4/crypto/bls/common"
"golang.org/x/crypto/sha3"
"math/big"
"testing"
)

type TaskResponse struct {
TaskId uint64
NumberSum *big.Int
}

func Test_bls_sig(t *testing.T) {

privateKeys := make([]blscommon.SecretKey, 3)
for i := 0; i < 3; i++ {
privateKeys[i], _ = blst.RandKey()
}

publicKeys := make([]blscommon.PublicKey, 3)
for i := 0; i < 3; i++ {
publicKeys[i] = privateKeys[i].PublicKey()
}

taskres := TaskResponse{TaskId: 1, NumberSum: big.NewInt(100)}

msg, _ := GetTaskResponseDigest(taskres)
msgBytes := msg[:]

signatures := make([]blscommon.Signature, 3)
for i := 0; i < 3; i++ {
signatures[i] = privateKeys[i].Sign(msgBytes)
}

aggsignature := blst.AggregateSignatures(signatures)

valid2 := aggsignature.FastAggregateVerify(publicKeys, msg)
valid3 := aggsignature.Eth2FastAggregateVerify(publicKeys, msg)
fmt.Println("Aggregate signature2 is valid for all messages:", valid2)
fmt.Println("Aggregate signature3 is valid for all messages:", valid3)

sigN := privateKeys[1].Sign(msgBytes)
a := sigN.Marshal()
fmt.Println(a)
valid := sigN.Verify(publicKeys[1], msgBytes)
fmt.Println(" sigN is valid for all messages:", valid)

b, _ := blst.VerifySignature(a, msg, publicKeys[1])
fmt.Println(" b is valid for all messages:", b)

}

// GetTaskResponseDigest returns the hash of the TaskResponse, which is what operators sign over
func GetTaskResponseDigest(h TaskResponse) ([32]byte, error) {

jsonData, err := json.Marshal(h)
if err != nil {
fmt.Println("Error marshalling struct to JSON:", err)
return [32]byte{}, err
}

fmt.Println(jsonData)
fmt.Println(string(jsonData))

var newPerson TaskResponse
err = json.Unmarshal(jsonData, &newPerson)
if err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return [32]byte{}, err
}
fmt.Println(newPerson)

var taskResponseDigest [32]byte
hasher := sha3.NewLegacyKeccak256()
hasher.Write(jsonData)
copy(taskResponseDigest[:], hasher.Sum(nil)[:32])

return taskResponseDigest, nil
}

func Test_hash(t *testing.T) {
taskres := TaskResponse{TaskId: 1, NumberSum: big.NewInt(100)}
jsonData, _ := json.Marshal(taskres)

var taskResponseDigest [32]byte
hasher := sha3.NewLegacyKeccak256()
hasher.Write(jsonData)
copy(taskResponseDigest[:], hasher.Sum(nil)[:32])
fmt.Println(hex.EncodeToString(taskResponseDigest[:]))
}
3 changes: 2 additions & 1 deletion x/avs/keeper/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
types "github.com/ExocoreNetwork/exocore/x/avs/types"
"github.com/ethereum/go-ethereum/common"
"strconv"
)

func (suite *AVSTestSuite) TestTaskInfo() {
Expand All @@ -18,7 +19,7 @@ func (suite *AVSTestSuite) TestTaskInfo() {
err := suite.App.AVSManagerKeeper.SetTaskInfo(suite.Ctx, info)
suite.NoError(err)

getTaskInfo, err := suite.App.AVSManagerKeeper.GetTaskInfo(suite.Ctx, "avstask01", common.Address(suite.AccAddress.Bytes()).String())
getTaskInfo, err := suite.App.AVSManagerKeeper.GetTaskInfo(suite.Ctx, strconv.Itoa(3), common.Address(suite.AccAddress.Bytes()).String())
suite.NoError(err)
suite.Equal(*info, *getTaskInfo)
}
Expand Down
7 changes: 1 addition & 6 deletions x/avs/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"encoding/json"
"golang.org/x/crypto/sha3"
"math/big"
"strings"

Expand Down Expand Up @@ -120,10 +119,6 @@ func GetTaskResponseDigest(h TaskResponse) ([32]byte, error) {
if err != nil {
return [32]byte{}, err
}
var taskResponseDigest [32]byte
hasher := sha3.NewLegacyKeccak256()
hasher.Write(jsonData)
copy(taskResponseDigest[:], hasher.Sum(nil)[:32])

taskResponseDigest := crypto.Keccak256Hash(jsonData)
return taskResponseDigest, nil
}

0 comments on commit 326a200

Please sign in to comment.