Skip to content

Commit

Permalink
feat: update crosschain gov v0.47 (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgao001 authored Apr 13, 2023
1 parent e801e09 commit c324261
Show file tree
Hide file tree
Showing 29 changed files with 1,289 additions and 187 deletions.
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
2 changes: 2 additions & 0 deletions tests/integration/gov/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
_ "github.com/cosmos/cosmos-sdk/x/consensus"
_ "github.com/cosmos/cosmos-sdk/x/crosschain"
_ "github.com/cosmos/cosmos-sdk/x/distribution"
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
Expand Down Expand Up @@ -52,6 +53,7 @@ var appConfig = configurator.NewAppConfig(
configurator.StakingModule(),
configurator.BankModule(),
configurator.GovModule(),
configurator.CrossChainModule(),
configurator.DistributionModule(),
configurator.MintModule(),
configurator.ConsensusModule(),
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/gov/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
_ "github.com/cosmos/cosmos-sdk/x/authz/module"
_ "github.com/cosmos/cosmos-sdk/x/crosschain"
_ "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/gov/types"
_ "github.com/cosmos/cosmos-sdk/x/mint"
Expand All @@ -26,6 +27,7 @@ func TestItCreatesModuleAccountOnInitBlock(t *testing.T) {
configurator.StakingModule(),
configurator.BankModule(),
configurator.GovModule(),
configurator.CrossChainModule(),
configurator.DistributionModule(),
configurator.ConsensusModule(),
),
Expand Down
27 changes: 27 additions & 0 deletions testutil/configurator/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
authzmodulev1 "cosmossdk.io/api/cosmos/authz/module/v1"
bankmodulev1 "cosmossdk.io/api/cosmos/bank/module/v1"
consensusmodulev1 "cosmossdk.io/api/cosmos/consensus/module/v1"
crosschainmodulev1 "cosmossdk.io/api/cosmos/crosschain/module/v1"
distrmodulev1 "cosmossdk.io/api/cosmos/distribution/module/v1"
evidencemodulev1 "cosmossdk.io/api/cosmos/evidence/module/v1"
feegrantmodulev1 "cosmossdk.io/api/cosmos/feegrant/module/v1"
Expand All @@ -15,6 +16,7 @@ import (
groupmodulev1 "cosmossdk.io/api/cosmos/group/module/v1"
mintmodulev1 "cosmossdk.io/api/cosmos/mint/module/v1"
nftmodulev1 "cosmossdk.io/api/cosmos/nft/module/v1"
oraclemodulev1 "cosmossdk.io/api/cosmos/oracle/module/v1"
paramsmodulev1 "cosmossdk.io/api/cosmos/params/module/v1"
slashingmodulev1 "cosmossdk.io/api/cosmos/slashing/module/v1"
stakingmodulev1 "cosmossdk.io/api/cosmos/staking/module/v1"
Expand Down Expand Up @@ -43,6 +45,8 @@ var beginBlockOrder = []string{
"params",
"consensus",
"vesting",
"crosschain",
"oracle",
}

var endBlockersOrder = []string{
Expand All @@ -64,6 +68,8 @@ var endBlockersOrder = []string{
"consensus",
"upgrade",
"vesting",
"crosschain",
"oracle",
}

var initGenesisOrder = []string{
Expand All @@ -85,6 +91,8 @@ var initGenesisOrder = []string{
"consensus",
"upgrade",
"vesting",
"crosschain",
"oracle",
}

type appConfig struct {
Expand Down Expand Up @@ -117,6 +125,7 @@ func AuthModule() ModuleOption {
{Account: "not_bonded_tokens_pool", Permissions: []string{"burner", "staking"}},
{Account: "gov", Permissions: []string{"burner"}},
{Account: "nft"},
{Account: "crosschain", Permissions: []string{"minter"}},
},
}),
}
Expand Down Expand Up @@ -264,6 +273,24 @@ func NFTModule() ModuleOption {
}
}

func CrossChainModule() ModuleOption {
return func(config *appConfig) {
config.moduleConfigs["crosschain"] = &appv1alpha1.ModuleConfig{
Name: "crosschain",
Config: appconfig.WrapAny(&crosschainmodulev1.Module{}),
}
}
}

func OracleModule() ModuleOption {
return func(config *appConfig) {
config.moduleConfigs["oracle"] = &appv1alpha1.ModuleConfig{
Name: "oracle",
Config: appconfig.WrapAny(&oraclemodulev1.Module{}),
}
}
}

func OmitInitGenesis() ModuleOption {
return func(config *appConfig) {
config.setInitGenesis = false
Expand Down
2 changes: 2 additions & 0 deletions x/bank/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
"github.com/cosmos/cosmos-sdk/x/bank/types"
_ "github.com/cosmos/cosmos-sdk/x/consensus"
_ "github.com/cosmos/cosmos-sdk/x/crosschain"
_ "github.com/cosmos/cosmos-sdk/x/distribution"
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
_ "github.com/cosmos/cosmos-sdk/x/gov"
Expand Down Expand Up @@ -118,6 +119,7 @@ func createTestSuite(t *testing.T, genesisAccounts []authtypes.GenesisAccount) s
configurator.ConsensusModule(),
configurator.BankModule(),
configurator.GovModule(),
configurator.CrossChainModule(),
configurator.DistributionModule(),
),
startupCfg, &res.BankKeeper, &res.AccountKeeper, &res.DistributionKeeper)
Expand Down
2 changes: 2 additions & 0 deletions x/gov/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
_ "github.com/cosmos/cosmos-sdk/x/bank"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
_ "github.com/cosmos/cosmos-sdk/x/consensus"
_ "github.com/cosmos/cosmos-sdk/x/crosschain"
_ "github.com/cosmos/cosmos-sdk/x/distribution"
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
"github.com/cosmos/cosmos-sdk/x/gov/keeper"
Expand Down Expand Up @@ -123,6 +124,7 @@ func createTestSuite(t *testing.T) suite {
configurator.BankModule(),
configurator.AuthzModule(),
configurator.GovModule(),
configurator.CrossChainModule(),
configurator.ConsensusModule(),
configurator.DistributionModule(),
),
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
75 changes: 75 additions & 0 deletions x/gov/keeper/crosschain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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 = sdk.MustAccAddressFromHex(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 := sdk.MustAccAddressFromHex(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, _ *sdk.CrossChainAppContext, payload []byte) sdk.ExecuteResult {
k.Logger(ctx).Error("received sync params sync package", "payload", hex.EncodeToString(payload))
return sdk.ExecuteResult{}
}

func (k Keeper) ExecuteAckPackage(ctx sdk.Context, _ *sdk.CrossChainAppContext, payload []byte) sdk.ExecuteResult {
k.Logger(ctx).Error("received sync params in ack package", "payload", hex.EncodeToString(payload))
return sdk.ExecuteResult{}
}

func (k Keeper) ExecuteFailAckPackage(ctx sdk.Context, _ *sdk.CrossChainAppContext, payload []byte) sdk.ExecuteResult {
k.Logger(ctx).Error("received sync params fail ack package", "payload", hex.EncodeToString(payload))
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
Loading

0 comments on commit c324261

Please sign in to comment.