Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update crosschain gov v0.47 #171

Merged
merged 11 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions proto/cosmos/gov/v1/gov.proto
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,14 @@ message Params {
// burn deposits if quorum with vote type no_veto is met
bool burn_vote_veto = 15;
}

// CrossChainParamsChange defines the parameter change or contract upgrade
message CrossChainParamsChange {
// parameter to be updated or 'upgrade' for contract upgrade
string key = 1;
// values is a new parameter or slice of new contract addresses in hex format
repeated string values = 2;
// targets defines a slice of addresses string in hex format
repeated string targets = 3;
}

18 changes: 18 additions & 0 deletions proto/cosmos/gov/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ service Msg {
// Since: cosmos-sdk 0.47
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);

// UpdateCrossChainParams defines a method to send IBC package to update cross-chain params
rpc UpdateCrossChainParams(MsgUpdateCrossChainParams) returns (MsgUpdateCrossChainParamsResponse);

// CancelProposal defines a method to cancel governance proposal
//
// Since: cosmos-sdk 0.48
Expand Down Expand Up @@ -203,3 +206,18 @@ message MsgCancelProposalResponse {
// canceled_height defines the block height at which the proposal is canceled.
uint64 canceled_height = 3;
}

// MsgUpdateCrossChainParams for cross chain gov
message MsgUpdateCrossChainParams {
option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "cosmos-sdk/x/gov/v1/MsgUpdateCrossChainParams";

// authority is the address that controls the module (defaults to x/gov unless overwritten).
string authority = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// for cross chain param change or contract upgrade
CrossChainParamsChange params = 2 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
}

// MsgUpdateCrossChainParamsResponse defines the response structure for executing a MsgUpdateCrossChainParams message.
message MsgUpdateCrossChainParamsResponse {}
7 changes: 3 additions & 4 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ func NewSimApp(
// set the governance module account as the authority for conducting upgrades
app.UpgradeKeeper = upgradekeeper.NewKeeper(skipUpgradeHeights, keys[upgradetypes.StoreKey], appCodec, homePath, app.BaseApp, authtypes.NewModuleAddress(govtypes.ModuleName).String())

app.CrossChainKeeper = crosschainkeeper.NewKeeper(appCodec, keys[crosschaintypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String())
// Register the proposal types
// Deprecated: Avoid adding new handlers, instead use the new proposal flow
// by granting the governance module the right to execute the message.
Expand All @@ -361,21 +362,19 @@ func NewSimApp(
*/
govKeeper := govkeeper.NewKeeper(
appCodec, keys[govtypes.StoreKey], app.AccountKeeper, app.BankKeeper,
app.StakingKeeper, app.DistrKeeper, app.MsgServiceRouter(), govConfig, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
app.StakingKeeper, app.DistrKeeper, app.CrossChainKeeper, app.MsgServiceRouter(), govConfig, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

// Set legacy router for backwards compatibility with gov v1beta1
govKeeper.SetLegacyRouter(govRouter)

app.GovKeeper = *govKeeper.SetHooks(
govtypes.NewMultiGovHooks(
// register the governance hooks
// register the governance hooks
),
)

app.NFTKeeper = nftkeeper.NewKeeper(runtime.NewKVStoreService(keys[nftkeeper.StoreKey]), appCodec, app.AccountKeeper, app.BankKeeper)
app.CrossChainKeeper = crosschainkeeper.NewKeeper(appCodec, keys[crosschaintypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String())

// create evidence keeper with router
evidenceKeeper := evidencekeeper.NewKeeper(
appCodec, keys[evidencetypes.StoreKey], app.StakingKeeper, app.SlashingKeeper,
Expand Down
8 changes: 6 additions & 2 deletions x/gov/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func setupGovKeeper(t *testing.T) (
*govtestutil.MockBankKeeper,
*govtestutil.MockStakingKeeper,
*govtestutil.MockDistributionKeeper,
*govtestutil.MockCrossChainKeeper,
moduletestutil.TestEncodingConfig,
sdk.Context,
) {
Expand All @@ -75,6 +76,7 @@ func setupGovKeeper(t *testing.T) (
bankKeeper := govtestutil.NewMockBankKeeper(ctrl)
stakingKeeper := govtestutil.NewMockStakingKeeper(ctrl)
distributionKeeper := govtestutil.NewMockDistributionKeeper(ctrl)
crossChainKeeper := govtestutil.NewMockCrossChainKeeper(ctrl)

acctKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(govAcct).AnyTimes()
acctKeeper.EXPECT().GetModuleAddress(disttypes.ModuleName).Return(distAcct).AnyTimes()
Expand All @@ -90,19 +92,21 @@ func setupGovKeeper(t *testing.T) (
distributionKeeper.EXPECT().FundCommunityPool(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()

// Gov keeper initializations
govKeeper := keeper.NewKeeper(encCfg.Codec, key, acctKeeper, bankKeeper, stakingKeeper, distributionKeeper, msr, types.DefaultConfig(), govAcct.String())
govKeeper := keeper.NewKeeper(encCfg.Codec, key, acctKeeper, bankKeeper, stakingKeeper, distributionKeeper, crossChainKeeper, msr, types.DefaultConfig(), govAcct.String())
govKeeper.SetProposalID(ctx, 1)
govRouter := v1beta1.NewRouter() // Also register legacy gov handlers to test them too.
govRouter.AddRoute(types.RouterKey, v1beta1.ProposalHandler)
govKeeper.SetLegacyRouter(govRouter)
govKeeper.SetParams(ctx, v1.DefaultParams())

crossChainKeeper.EXPECT().CreateRawIBCPackageWithFee(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(uint64(0), nil).AnyTimes()

// Register all handlers for the MegServiceRouter.
msr.SetInterfaceRegistry(encCfg.InterfaceRegistry)
v1.RegisterMsgServer(msr, keeper.NewMsgServerImpl(govKeeper))
banktypes.RegisterMsgServer(msr, nil) // Nil is fine here as long as we never execute the proposal's Msgs.

return govKeeper, acctKeeper, bankKeeper, stakingKeeper, distributionKeeper, encCfg, ctx
return govKeeper, acctKeeper, bankKeeper, stakingKeeper, distributionKeeper, crossChainKeeper, encCfg, ctx
}

// trackMockBalances sets up expected calls on the Mock BankKeeper, and also
Expand Down
82 changes: 82 additions & 0 deletions x/gov/keeper/crosschain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package keeper

import (
"encoding/hex"
"math/big"

sdkerrors "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/bsc/rlp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
)

func (k Keeper) RegisterCrossChainSyncParamsApp() error {
return k.crossChainKeeper.RegisterChannel(types.SyncParamsChannel, types.SyncParamsChannelID, k)
}

func (k Keeper) SyncParams(ctx sdk.Context, cpc govv1.CrossChainParamsChange) error {
if err := cpc.ValidateBasic(); err != nil {
return err
}
values := make([]byte, 0)
addresses := make([]byte, 0)

for i, v := range cpc.Values {
var value []byte
var err error
if cpc.Key == types.KeyUpgrade {
value, err = sdk.AccAddressFromHexUnsafe(v)
if err != nil {
return sdkerrors.Wrapf(types.ErrAddressNotValid, "smart contract address is not valid %s", v)
}
} else {
value, err = hex.DecodeString(v)
if err != nil {
return sdkerrors.Wrapf(types.ErrInvalidValue, "value is not valid %s", v)
}
}
values = append(values, value...)

addr, err := sdk.AccAddressFromHexUnsafe(cpc.Targets[i])
if err != nil {
return sdkerrors.Wrapf(types.ErrAddressNotValid, "smart contract address is not valid %s", cpc.Targets[i])
}
addresses = append(addresses, addr.Bytes()...)
}

pack := types.SyncParamsPackage{
Key: cpc.Key,
Value: values,
Target: addresses,
}

encodedPackage, err := rlp.EncodeToBytes(pack)
if err != nil {
return sdkerrors.Wrapf(types.ErrInvalidUpgradeProposal, "encode sync params package error")
}
_, err = k.crossChainKeeper.CreateRawIBCPackageWithFee(
ctx,
types.SyncParamsChannelID,
sdk.SynCrossChainPackageType,
encodedPackage,
big.NewInt(0),
big.NewInt(0),
)
return err
}

func (k Keeper) ExecuteSynPackage(ctx sdk.Context, appCtx *sdk.CrossChainAppContext, payload []byte) sdk.ExecuteResult {
k.Logger(ctx).Error("received sync params sync package", "payload", hex.EncodeToString(payload))
forcodedancing marked this conversation as resolved.
Show resolved Hide resolved
return sdk.ExecuteResult{}
}

func (k Keeper) ExecuteAckPackage(ctx sdk.Context, header *sdk.CrossChainAppContext, payload []byte) sdk.ExecuteResult {
k.Logger(ctx).Error("received sync params in ack package", "payload", hex.EncodeToString(payload))
forcodedancing marked this conversation as resolved.
Show resolved Hide resolved
return sdk.ExecuteResult{}
}

func (k Keeper) ExecuteFailAckPackage(ctx sdk.Context, header *sdk.CrossChainAppContext, payload []byte) sdk.ExecuteResult {
k.Logger(ctx).Error("received sync params fail ack package", "payload", hex.EncodeToString(payload))
forcodedancing marked this conversation as resolved.
Show resolved Hide resolved
return sdk.ExecuteResult{}
}
6 changes: 3 additions & 3 deletions x/gov/keeper/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestDeposits(t *testing.T) {

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
govKeeper, _, bankKeeper, stakingKeeper, distKeeper, _, ctx := setupGovKeeper(t)
govKeeper, _, bankKeeper, stakingKeeper, distKeeper, _, _, ctx := setupGovKeeper(t)
trackMockBalances(bankKeeper, distKeeper)

// With expedited proposals the minimum deposit is higher, so we must
Expand Down Expand Up @@ -235,7 +235,7 @@ func TestValidateInitialDeposit(t *testing.T) {

for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
govKeeper, _, _, _, _, _, ctx := setupGovKeeper(t)
govKeeper, _, _, _, _, _, _, ctx := setupGovKeeper(t)

params := v1.DefaultParams()
if tc.expedited {
Expand Down Expand Up @@ -297,7 +297,7 @@ func TestChargeDeposit(t *testing.T) {
}

t.Run(testName(i), func(t *testing.T) {
govKeeper, _, bankKeeper, stakingKeeper, _, _, ctx := setupGovKeeper(t)
govKeeper, _, bankKeeper, stakingKeeper, _, _, _, ctx := setupGovKeeper(t)
params := v1.DefaultParams()
params.ProposalCancelRatio = tc.proposalCancelRatio
TestAddrs := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, ctx, 2, sdk.NewInt(10000000000))
Expand Down
2 changes: 1 addition & 1 deletion x/gov/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (h *MockGovHooksReceiver) AfterProposalVotingPeriodEnded(ctx sdk.Context, p

func TestHooks(t *testing.T) {
minDeposit := v1.DefaultParams().MinDeposit
govKeeper, _, bankKeeper, stakingKeeper, _, _, ctx := setupGovKeeper(t)
govKeeper, _, bankKeeper, stakingKeeper, _, _, _, ctx := setupGovKeeper(t)
addrs := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 1, minDeposit[0].Amount)

govHooksReceiver := MockGovHooksReceiver{}
Expand Down
28 changes: 15 additions & 13 deletions x/gov/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (

// Keeper defines the governance module Keeper
type Keeper struct {
authKeeper types.AccountKeeper
bankKeeper types.BankKeeper
distrkeeper types.DistributionKeeper
authKeeper types.AccountKeeper
bankKeeper types.BankKeeper
distrkeeper types.DistributionKeeper
crossChainKeeper types.CrossChainKeeper

// The reference to the DelegationSet and ValidatorSet to get information about validators and delegators
sk types.StakingKeeper
Expand Down Expand Up @@ -61,7 +62,7 @@ func (k Keeper) GetAuthority() string {
func NewKeeper(
cdc codec.BinaryCodec, key storetypes.StoreKey, authKeeper types.AccountKeeper,
bankKeeper types.BankKeeper, sk types.StakingKeeper, distrkeeper types.DistributionKeeper,
router baseapp.MessageRouter, config types.Config, authority string,
crossChainKeeper types.CrossChainKeeper, router baseapp.MessageRouter, config types.Config, authority string,
) *Keeper {
// ensure governance module account is set
if addr := authKeeper.GetModuleAddress(types.ModuleName); addr == nil {
Expand All @@ -78,15 +79,16 @@ func NewKeeper(
}

return &Keeper{
storeKey: key,
authKeeper: authKeeper,
bankKeeper: bankKeeper,
distrkeeper: distrkeeper,
sk: sk,
cdc: cdc,
router: router,
config: config,
authority: authority,
storeKey: key,
authKeeper: authKeeper,
bankKeeper: bankKeeper,
distrkeeper: distrkeeper,
crossChainKeeper: crossChainKeeper,
sk: sk,
cdc: cdc,
router: router,
config: config,
authority: authority,
}
}

Expand Down
8 changes: 5 additions & 3 deletions x/gov/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type KeeperTestSuite struct {
bankKeeper *govtestutil.MockBankKeeper
stakingKeeper *govtestutil.MockStakingKeeper
distKeeper *govtestutil.MockDistributionKeeper
crossChainKeeper *govtestutil.MockCrossChainKeeper
queryClient v1.QueryClient
legacyQueryClient v1beta1.QueryClient
addrs []sdk.AccAddress
Expand All @@ -40,7 +41,7 @@ func (suite *KeeperTestSuite) SetupSuite() {
}

func (suite *KeeperTestSuite) reset() {
govKeeper, acctKeeper, bankKeeper, stakingKeeper, distKeeper, encCfg, ctx := setupGovKeeper(suite.T())
govKeeper, acctKeeper, bankKeeper, stakingKeeper, distKeeper, crossChainKeeper, encCfg, ctx := setupGovKeeper(suite.T())

// Populate the gov account with some coins, as the TestProposal we have
// is a MsgSend from the gov account.
Expand All @@ -63,6 +64,7 @@ func (suite *KeeperTestSuite) reset() {
suite.bankKeeper = bankKeeper
suite.stakingKeeper = stakingKeeper
suite.distKeeper = distKeeper
suite.crossChainKeeper = crossChainKeeper
suite.cdc = encCfg.Codec
suite.queryClient = queryClient
suite.legacyQueryClient = legacyQueryClient
Expand All @@ -73,7 +75,7 @@ func (suite *KeeperTestSuite) reset() {
}

func TestIncrementProposalNumber(t *testing.T) {
govKeeper, _, _, _, _, _, ctx := setupGovKeeper(t) //nolint:dogsled
govKeeper, _, _, _, _, _, _, ctx := setupGovKeeper(t) //nolint:dogsled

tp := TestProposal
_, err := govKeeper.SubmitProposal(ctx, tp, "", "test", "summary", sdk.AccAddress("0x45f3624b98fCfc4D7A6b37B0957b656878636773"), false)
Expand All @@ -93,7 +95,7 @@ func TestIncrementProposalNumber(t *testing.T) {
}

func TestProposalQueues(t *testing.T) {
govKeeper, _, _, _, _, _, ctx := setupGovKeeper(t) //nolint:dogsled
govKeeper, _, _, _, _, _, _, ctx := setupGovKeeper(t) //nolint:dogsled

// create test proposals
tp := TestProposal
Expand Down
14 changes: 13 additions & 1 deletion x/gov/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (k msgServer) SubmitProposal(goCtx context.Context, msg *v1.MsgSubmitPropos
}, nil
}

// CancelProposals implements the MsgServer.CancelProposal method.
// CancelProposal CancelProposals implements the MsgServer.CancelProposal method.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CancelProposal implements the MsgServer.CancelProposal method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

func (k msgServer) CancelProposal(goCtx context.Context, msg *v1.MsgCancelProposal) (*v1.MsgCancelProposalResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
_, err := sdk.AccAddressFromHexUnsafe(msg.Proposer)
Expand Down Expand Up @@ -229,6 +229,18 @@ func (k msgServer) UpdateParams(goCtx context.Context, msg *v1.MsgUpdateParams)
return &v1.MsgUpdateParamsResponse{}, nil
}

// UpdateCrossChainParams implements the MsgServer.UpdateCrossChainParams method.
func (k msgServer) UpdateCrossChainParams(goCtx context.Context, msg *v1.MsgUpdateCrossChainParams) (*v1.MsgUpdateCrossChainParamsResponse, error) {
if k.authority != msg.Authority {
return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, msg.Authority)
}
ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.SyncParams(ctx, msg.Params); err != nil {
return nil, err
}
return &v1.MsgUpdateCrossChainParamsResponse{}, nil
}

type legacyMsgServer struct {
govAcct string
server v1.MsgServer
Expand Down
Loading