Skip to content

Commit

Permalink
fix the problems identified by code review and add a function to get …
Browse files Browse the repository at this point in the history
…all operators
  • Loading branch information
TimmyExogenous committed Apr 9, 2024
1 parent 84b76c1 commit 2726d1d
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 124 deletions.
6 changes: 0 additions & 6 deletions proto/exocore/operator/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ message OperatorInfo {
ClientChainEarningAddrList client_chain_earnings_addr = 4;
// commission defines the commission parameters.
cosmos.staking.v1beta1.Commission commission = 5 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
// min_self_delegation is the validator's self declared minimum self delegation.
string min_self_delegation = 6 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}

// OptedInfo is the opted information about operator
Expand Down
16 changes: 14 additions & 2 deletions x/operator/keeper/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ func (k *Keeper) SetOperatorInfo(ctx sdk.Context, addr string, info *operatortyp
if err != nil {
return errorsmod.Wrap(err, "SetOperatorInfo: error occurred when parse acc address from Bech32")
}
// todo: to check the validation of input info
store := prefix.NewStore(ctx.KVStore(k.storeKey), operatortypes.KeyPrefixOperatorInfo)
// todo: think about the difference between init and update in future

// key := common.HexToAddress(incentive.Contract)
bz := k.cdc.MustMarshal(info)

store.Set(opAccAddr, bz)
return nil
}
Expand All @@ -49,6 +47,20 @@ func (k *Keeper) OperatorInfo(ctx sdk.Context, addr string) (info *operatortypes
return &ret, nil
}

// AllOperators return the address list of all operators
func (k *Keeper) AllOperators(ctx sdk.Context) []string {
store := prefix.NewStore(ctx.KVStore(k.storeKey), operatortypes.KeyPrefixOperatorInfo)
iterator := sdk.KVStorePrefixIterator(store, nil)
defer iterator.Close()

ret := make([]string, 0)
for ; iterator.Valid(); iterator.Next() {
accAddr := sdk.AccAddress(iterator.Key())
ret = append(ret, accAddr.String())
}
return ret
}

func (k *Keeper) IsOperator(ctx sdk.Context, addr sdk.AccAddress) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), operatortypes.KeyPrefixOperatorInfo)
return store.Has(addr)
Expand Down
14 changes: 12 additions & 2 deletions x/operator/keeper/operator_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ func (suite *OperatorTestSuite) TestOperatorInfo() {
{101, "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984"},
},
},
Commission: stakingtypes.NewCommission(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec()),
MinSelfDelegation: math.NewInt(0),
Commission: stakingtypes.NewCommission(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec()),
}
err := suite.App.OperatorKeeper.SetOperatorInfo(suite.Ctx, suite.AccAddress.String(), info)
suite.NoError(err)
Expand All @@ -28,6 +27,17 @@ func (suite *OperatorTestSuite) TestOperatorInfo() {
suite.Equal(*info, *getOperatorInfo)
}

func (suite *OperatorTestSuite) TestAllOperators() {
suite.prepare()
operators := []string{suite.operatorAddr.String(), suite.AccAddress.String()}
info := &operatortype.OperatorInfo{}
err := suite.App.OperatorKeeper.SetOperatorInfo(suite.Ctx, suite.AccAddress.String(), info)
suite.NoError(err)

getOperators := suite.App.OperatorKeeper.AllOperators(suite.Ctx)
suite.Equal(operators, getOperators)
}

func (suite *OperatorTestSuite) TestHistoricalOperatorInfo() {
height := suite.Ctx.BlockHeight()
info := &operatortype.OperatorInfo{
Expand Down
6 changes: 6 additions & 0 deletions x/operator/keeper/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
"fmt"

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

errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"

Expand Down Expand Up @@ -87,6 +89,10 @@ func (k *Keeper) UpdateOptedInAssetsState(ctx sdk.Context, stakerID, assetID, op

// OptIn call this function to opt in AVS
func (k *Keeper) OptIn(ctx sdk.Context, operatorAddress sdk.AccAddress, avsAddr string) error {
// avsAddr should be an evm contract address
if common.IsHexAddress(avsAddr) {
return types.ErrInvalidAvsAddr
}
// check optedIn info
if k.IsOptedIn(ctx, operatorAddress.String(), avsAddr) {
return types.ErrAlreadyOptedIn
Expand Down
4 changes: 4 additions & 0 deletions x/operator/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ var (
ErrAlreadyOptingOut = errorsmod.Register(
ModuleName, 11, "operator already opting out",
)

ErrInvalidAvsAddr = errorsmod.Register(
ModuleName, 12, "avs address should be a hex evm contract address",
)
)
178 changes: 64 additions & 114 deletions x/operator/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2726d1d

Please sign in to comment.