Skip to content

Commit

Permalink
add new transaction to update operator address of observer
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Nov 7, 2023
1 parent cdcb3b1 commit 8d17a72
Show file tree
Hide file tree
Showing 28 changed files with 1,364 additions and 151 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ func New(
keys[zetaObserverModuleTypes.MemStoreKey],
app.GetSubspace(zetaObserverModuleTypes.ModuleName),
&stakingKeeper,
app.SlashingKeeper,
)

// register the staking hooks
Expand Down
9 changes: 9 additions & 0 deletions docs/openapi/openapi.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51095,6 +51095,8 @@ definitions:
type: object
observerMsgUpdateKeygenResponse:
type: object
observerMsgUpdateObserverResponse:
type: object
observerNode:
type: object
properties:
Expand Down Expand Up @@ -51158,6 +51160,13 @@ definitions:
type: string
is_supported:
type: boolean
observerObserverUpdateReason:
type: string
enum:
- EmptyObserverUpdateReason
- Tombstoned
- AdminUpdate
default: EmptyObserverUpdateReason
observerPolicy_Type:
type: string
enum:
Expand Down
11 changes: 11 additions & 0 deletions docs/spec/observer/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ message MsgAddObserver {
}
```

## MsgUpdateObserver

```proto
message MsgUpdateObserver {
string creator = 1;
string old_observer_address = 2;
string new_observer_address = 3;
ObserverUpdateReason update_reason = 4;
}
```

## MsgUpdateCoreParams

UpdateCoreParams updates core parameters for a specific chain. Core parameters include
Expand Down
7 changes: 7 additions & 0 deletions proto/observer/observer.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ enum ObservationType {
TSSKeySign = 4;
}

enum ObserverUpdateReason {
option (gogoproto.goproto_enum_stringer) = true;
EmptyObserverUpdateReason = 0;
Tombstoned = 1;
AdminUpdate = 2;
}

message ObserverMapper {
string index = 1;
common.Chain observer_chain = 2;
Expand Down
9 changes: 9 additions & 0 deletions proto/observer/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,22 @@ option go_package = "github.com/zeta-chain/zetacore/x/observer/types";
// Msg defines the Msg service.
service Msg {
rpc AddObserver(MsgAddObserver) returns (MsgAddObserverResponse);
rpc UpdateObserver(MsgUpdateObserver) returns (MsgUpdateObserverResponse);
rpc UpdateCoreParams(MsgUpdateCoreParams) returns (MsgUpdateCoreParamsResponse);
rpc AddBlameVote(MsgAddBlameVote) returns (MsgAddBlameVoteResponse);
rpc UpdateCrosschainFlags(MsgUpdateCrosschainFlags) returns (MsgUpdateCrosschainFlagsResponse);
rpc UpdateKeygen(MsgUpdateKeygen) returns (MsgUpdateKeygenResponse);
rpc AddBlockHeader(MsgAddBlockHeader) returns (MsgAddBlockHeaderResponse);
}

message MsgUpdateObserver {
string creator = 1;
string old_observer_address = 2;
string new_observer_address = 3;
ObserverUpdateReason update_reason = 4;
}
message MsgUpdateObserverResponse {}

message MsgAddBlockHeader {
string creator = 1;
int64 chain_id = 2;
Expand Down
1 change: 1 addition & 0 deletions testutil/keeper/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func CrosschainKeeperWithMocks(
db,
stateStore,
sdkKeepers.StakingKeeper,
sdkKeepers.SlashingKeeper,
sdkKeepers.ParamsKeeper,
)
fungiblekeeperTmp := initFungibleKeeper(
Expand Down
1 change: 1 addition & 0 deletions testutil/keeper/fungible.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func FungibleKeeperWithMocks(t testing.TB, mockOptions FungibleMockOptions) (*ke
db,
stateStore,
sdkKeepers.StakingKeeper,
sdkKeepers.SlashingKeeper,
sdkKeepers.ParamsKeeper,
)
zetaKeepers := ZetaKeepers{
Expand Down
14 changes: 13 additions & 1 deletion testutil/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"testing"
"time"

slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand Down Expand Up @@ -81,6 +84,7 @@ type SDKKeepers struct {
AuthKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
StakingKeeper stakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
FeeMarketKeeper feemarketkeeper.Keeper
EvmKeeper *evmkeeper.Keeper
}
Expand Down Expand Up @@ -199,6 +203,13 @@ func StakingKeeper(
)
}

// SlashingKeeper instantiates a slashing keeper for testing purposes
func SlashingKeeper(cdc codec.Codec, db *tmdb.MemDB, ss store.CommitMultiStore, stakingKeeper stakingkeeper.Keeper, paramKeeper paramskeeper.Keeper) slashingkeeper.Keeper {
storeKey := sdk.NewKVStoreKey(slashingtypes.StoreKey)
ss.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
return slashingkeeper.NewKeeper(cdc, storeKey, stakingKeeper, paramKeeper.Subspace(slashingtypes.ModuleName))
}

// DistributionKeeper instantiates a distribution keeper for testing purposes
func DistributionKeeper(
cdc codec.Codec,
Expand Down Expand Up @@ -319,14 +330,15 @@ func NewSDKKeepers(
stakingKeeper := StakingKeeper(cdc, db, ss, authKeeper, bankKeeper, paramsKeeper)
feeMarketKeeper := FeeMarketKeeper(cdc, db, ss, paramsKeeper)
evmKeeper := EVMKeeper(cdc, db, ss, authKeeper, bankKeeper, stakingKeeper, feeMarketKeeper, paramsKeeper)

slashingKeeper := SlashingKeeper(cdc, db, ss, stakingKeeper, paramsKeeper)
return SDKKeepers{
ParamsKeeper: paramsKeeper,
AuthKeeper: authKeeper,
BankKeeper: bankKeeper,
StakingKeeper: stakingKeeper,
FeeMarketKeeper: feeMarketKeeper,
EvmKeeper: evmKeeper,
SlashingKeeper: slashingKeeper,
}
}

Expand Down
5 changes: 5 additions & 0 deletions testutil/keeper/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
"testing"

slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand All @@ -20,6 +22,7 @@ func initObserverKeeper(
db *tmdb.MemDB,
ss store.CommitMultiStore,
stakingKeeper stakingkeeper.Keeper,
slashingKeeper slashingkeeper.Keeper,
paramKeeper paramskeeper.Keeper,
) *keeper.Keeper {
storeKey := sdk.NewKVStoreKey(types.StoreKey)
Expand All @@ -33,6 +36,7 @@ func initObserverKeeper(
memKey,
paramKeeper.Subspace(types.ModuleName),
stakingKeeper,
slashingKeeper,
)
}

Expand Down Expand Up @@ -68,6 +72,7 @@ func ObserverKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
memStoreKey,
sdkKeepers.ParamsKeeper.Subspace(types.ModuleName),
sdkKeepers.StakingKeeper,
sdkKeepers.SlashingKeeper,
)

k.SetParams(ctx, types.DefaultParams())
Expand Down
4 changes: 4 additions & 0 deletions testutil/sample/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func AccAddress() string {
return sdk.AccAddress(addr).String()
}

func ConsAddress() sdk.ConsAddress {
return sdk.ConsAddress(PubKey(newRandFromSeed(1)).Address())
}

// ValAddress returns a sample validator operator address
func ValAddress(r *rand.Rand) sdk.ValAddress {
return sdk.ValAddress(PubKey(r).Address())
Expand Down
4 changes: 2 additions & 2 deletions x/emissions/types/events.pb.go

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

2 changes: 1 addition & 1 deletion x/observer/client/cli/cli_node_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func CmdListNodeAccount() *cobra.Command {

func CmdShowNodeAccount() *cobra.Command {
cmd := &cobra.Command{
Use: "show-node-account [index]",
Use: "show-node-account [operator_address]",
Short: "shows a NodeAccount",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
1 change: 1 addition & 0 deletions x/observer/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func GetTxCmd() *cobra.Command {
CmdUpdateCrosschainFlags(),
CmdUpdateKeygen(),
CmdAddBlameVote(),
CmdUpdateObserver(),
CmdEncode(),
)

Expand Down
62 changes: 62 additions & 0 deletions x/observer/client/cli/tx_update_observer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cli

import (
"strconv"

errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/spf13/cobra"
"github.com/zeta-chain/zetacore/x/observer/types"
)

func CmdUpdateObserver() *cobra.Command {
cmd := &cobra.Command{
Use: "update-observer [old-observer-address] [new-observer-address] [update-reason]",
Short: "Broadcast message add-observer",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
updateReasonInt, err := strconv.ParseInt(args[2], 10, 32)
if err != nil {
return err
}
updateReason, err := ParseUpdateReason(int32(updateReasonInt))
if err != nil {
return err
}
msg := types.NewMsgUpdateObserver(
clientCtx.GetFromAddress().String(),
args[0],
args[1],
updateReason,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

func ParseUpdateReason(i int32) (types.ObserverUpdateReason, error) {
if _, ok := types.ObserverUpdateReason_name[i]; ok {
switch i {
case 1:
return types.ObserverUpdateReason_Tombstoned, nil
case 2:
return types.ObserverUpdateReason_AdminUpdate, nil
}
}
return types.ObserverUpdateReason_Tombstoned, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid update reason")
}
1 change: 0 additions & 1 deletion x/observer/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func (k Keeper) CleanSlashedValidator(ctx sdk.Context, valAddress sdk.ValAddress
if !found {
return types.ErrNotValidator
}

accAddress, err := types.GetAccAddressFromOperatorAddress(valAddress.String())
if err != nil {
return err
Expand Down
31 changes: 21 additions & 10 deletions x/observer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,30 @@ import (

type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
stakingKeeper types.StakingKeeper
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
stakingKeeper types.StakingKeeper
slashingKeeper types.SlashingKeeper
}
)

func (k Keeper) GetSlashingKeeper() types.SlashingKeeper {
return k.slashingKeeper
}

func (k Keeper) GetStakingKeeper() types.StakingKeeper {
return k.stakingKeeper
}

func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey storetypes.StoreKey,
ps paramtypes.Subspace,
stakingKeeper types.StakingKeeper,
slashinKeeper types.SlashingKeeper,

) *Keeper {
// set KeyTable if it has not already been set
Expand All @@ -36,11 +46,12 @@ func NewKeeper(
}

return &Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
stakingKeeper: stakingKeeper,
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
stakingKeeper: stakingKeeper,
slashingKeeper: slashinKeeper,
}
}

Expand Down
3 changes: 3 additions & 0 deletions x/observer/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
"testing"

slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
Expand Down Expand Up @@ -42,6 +44,7 @@ func SetupKeeper(t testing.TB) (*Keeper, sdk.Context) {
memStoreKey,
paramsSubspace,
stakingkeeper.Keeper{},
slashingkeeper.Keeper{},
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, nil)
Expand Down
16 changes: 16 additions & 0 deletions x/observer/keeper/keeper_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,23 @@ func (k Keeper) IsValidator(ctx sdk.Context, creator string) error {
return types.ErrValidatorStatus
}
return nil
}

func (k Keeper) IsOperatorTombstoned(ctx sdk.Context, creator string) (bool, error) {
valAddress, err := types.GetOperatorAddressFromAccAddress(creator)
if err != nil {
return false, err
}
validator, found := k.stakingKeeper.GetValidator(ctx, valAddress)
if !found {
return false, types.ErrNotValidator
}

consAddress, err := validator.GetConsAddr()
if err != nil {
return false, err
}
return k.slashingKeeper.IsTombstoned(ctx, consAddress), nil
}

func (k Keeper) CheckObserverDelegation(ctx sdk.Context, accAddress string, chain *common.Chain) error {
Expand Down
Loading

0 comments on commit 8d17a72

Please sign in to comment.