Skip to content

Commit

Permalink
add governance address for message
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Feb 29, 2024
1 parent d4d0510 commit 4d7b101
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 46 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ func New(
appCodec,
keys[authoritytypes.StoreKey],
keys[authoritytypes.MemStoreKey],
authtypes.NewModuleAddress(govtypes.ModuleName),
)

app.ObserverKeeper = observerkeeper.NewKeeper(
Expand Down
2 changes: 1 addition & 1 deletion docs/spec/authority/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ UpdatePolicies updates policies

```proto
message MsgUpdatePolicies {
string authority_address = 1;
string signer = 1;
Policies policies = 2;
}
```
Expand Down
4 changes: 2 additions & 2 deletions docs/spec/crosschain/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ message MsgWhitelistERC20 {

## MsgUpdateTssAddress

Authorized: admin policy group 2.
UpdateTssAddress updates the TSS address.

```proto
message MsgUpdateTssAddress {
Expand All @@ -226,7 +226,7 @@ message MsgUpdateTssAddress {

## MsgMigrateTssFunds

Authorized: admin policy group 2.
MigrateTssFunds migrates the funds from the current TSS to the new TSS

```proto
message MsgMigrateTssFunds {
Expand Down
2 changes: 1 addition & 1 deletion docs/spec/fungible/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ message MsgUpdateContractBytecode {

## MsgUpdateZRC20WithdrawFee

Authorized: admin policy group 2.
UpdateZRC20WithdrawFee updates the withdraw fee and gas limit of a zrc20 token

```proto
message MsgUpdateZRC20WithdrawFee {
Expand Down
2 changes: 1 addition & 1 deletion proto/authority/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ service Msg {

// MsgUpdatePolicies defines the MsgUpdatePolicies service.
message MsgUpdatePolicies {
string authority_address = 1;
string signer = 1;
Policies policies = 2 [(gogoproto.nullable) = false];
}

Expand Down
9 changes: 8 additions & 1 deletion testutil/keeper/authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ import (
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
mock "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
tmdb "github.com/tendermint/tm-db"
"github.com/zeta-chain/zetacore/testutil/sample"
"github.com/zeta-chain/zetacore/x/authority/keeper"
"github.com/zeta-chain/zetacore/x/authority/types"
)

var (
AuthorityGovAddress = sample.Bech32AccAddress()
)

func initAuthorityKeeper(
cdc codec.Codec,
db *tmdb.MemDB,
Expand All @@ -28,6 +33,7 @@ func initAuthorityKeeper(
cdc,
storeKey,
memKey,
AuthorityGovAddress,
)
}

Expand Down Expand Up @@ -61,6 +67,7 @@ func AuthorityKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
cdc,
storeKey,
memStoreKey,
AuthorityGovAddress,
)

return &k, ctx
Expand Down
4 changes: 2 additions & 2 deletions typescript/authority/tx_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import type { Policies } from "./policies_pb.js";
*/
export declare class MsgUpdatePolicies extends Message<MsgUpdatePolicies> {
/**
* @generated from field: string authority_address = 1;
* @generated from field: string signer = 1;
*/
authorityAddress: string;
signer: string;

/**
* @generated from field: zetachain.zetacore.authority.Policies policies = 2;
Expand Down
4 changes: 4 additions & 0 deletions x/authority/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ type Keeper struct {
cdc codec.Codec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
// the address capable of executing a MsgUpdatePolicies message. Typically, this should be the x/gov module account.
govAddr sdk.AccAddress
}

// NewKeeper creates new instances of the authority Keeper
func NewKeeper(
cdc codec.Codec,
storeKey,
memKey storetypes.StoreKey,
govAddr sdk.AccAddress,
) Keeper {
return Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
govAddr: govAddr,
}
}

Expand Down
13 changes: 12 additions & 1 deletion x/authority/keeper/msg_server_update_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@ package keeper
import (
"context"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/zeta-chain/zetacore/x/authority/types"
)

// UpdatePolicies updates policies
func (k msgServer) UpdatePolicies(goCtx context.Context, msg *types.MsgUpdatePolicies) (*types.MsgUpdatePoliciesResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: check if authorized
// check called by governance
if k.govAddr.String() != msg.Signer {
return nil, errorsmod.Wrapf(
govtypes.ErrInvalidSigner,
"invalid authority, expected %s, got %s",
k.govAddr.String(),
msg.Signer,
)
}

// set policies
k.SetPolicies(ctx, msg.Policies)

return &types.MsgUpdatePoliciesResponse{}, nil
Expand Down
19 changes: 17 additions & 2 deletions x/authority/keeper/msg_server_update_policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper_test
import (
"testing"

govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
Expand All @@ -12,15 +14,28 @@ import (
)

func TestMsgServer_UpdatePolicies(t *testing.T) {
t.Run("can't update policies with invalid signer", func(t *testing.T) {
k, ctx := keepertest.AuthorityKeeper(t)
msgServer := keeper.NewMsgServerImpl(*k)

policies := sample.Policies()

_, err := msgServer.UpdatePolicies(sdk.WrapSDKContext(ctx), &types.MsgUpdatePolicies{
Signer: sample.AccAddress(),
Policies: policies,
})
require.ErrorIs(t, err, govtypes.ErrInvalidSigner)
})

t.Run("can update policies", func(t *testing.T) {
k, ctx := keepertest.AuthorityKeeper(t)
msgServer := keeper.NewMsgServerImpl(*k)

policies := sample.Policies()

res, err := msgServer.UpdatePolicies(sdk.WrapSDKContext(ctx), &types.MsgUpdatePolicies{
AuthorityAddress: sample.AccAddress(),
Policies: policies,
Signer: keepertest.AuthorityGovAddress.String(),
Policies: policies,
})
require.NotNil(t, res)
require.NoError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions x/authority/types/message_update_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const TypeMsgUpdatePolicies = "UpdatePolicies"

var _ sdk.Msg = &MsgUpdatePolicies{}

func NewMsgUpdatePolicies(creator string, policies Policies) *MsgUpdatePolicies {
func NewMsgUpdatePolicies(signer string, policies Policies) *MsgUpdatePolicies {
return &MsgUpdatePolicies{
AuthorityAddress: creator,
Policies: policies,
Signer: signer,
Policies: policies,
}
}

Expand All @@ -26,7 +26,7 @@ func (msg *MsgUpdatePolicies) Type() string {
}

func (msg *MsgUpdatePolicies) GetSigners() []sdk.AccAddress {
creator, err := sdk.AccAddressFromBech32(msg.AuthorityAddress)
creator, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
Expand All @@ -39,9 +39,9 @@ func (msg *MsgUpdatePolicies) GetSignBytes() []byte {
}

func (msg *MsgUpdatePolicies) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.AuthorityAddress)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid signer address (%s)", err)
}

if err := msg.Policies.Validate(); err != nil {
Expand Down
50 changes: 25 additions & 25 deletions x/authority/types/tx.pb.go

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

5 changes: 3 additions & 2 deletions x/crosschain/keeper/gas_payment_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package keeper_test

import (
"github.com/zeta-chain/zetacore/testutil/sample"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"math/big"
"testing"

"github.com/zeta-chain/zetacore/testutil/sample"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"

"cosmossdk.io/math"

"github.com/stretchr/testify/require"
Expand Down
1 change: 1 addition & 0 deletions x/crosschain/keeper/msg_server_update_tss.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"

authoritytypes "github.com/zeta-chain/zetacore/x/authority/types"

errorsmod "cosmossdk.io/errors"
Expand Down
3 changes: 2 additions & 1 deletion x/crosschain/keeper/msg_server_whitelist_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package keeper
import (
"context"
"fmt"
authoritytypes "github.com/zeta-chain/zetacore/x/authority/types"
"math/big"

authoritytypes "github.com/zeta-chain/zetacore/x/authority/types"

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

Expand Down
3 changes: 2 additions & 1 deletion x/crosschain/keeper/msg_server_whitelist_erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package keeper_test

import (
"fmt"
authoritytypes "github.com/zeta-chain/zetacore/x/authority/types"
"testing"

authoritytypes "github.com/zeta-chain/zetacore/x/authority/types"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
Expand Down

0 comments on commit 4d7b101

Please sign in to comment.