Skip to content

Commit

Permalink
upgrade to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
keithsue committed Jan 20, 2025
1 parent 2695d06 commit 20fc247
Show file tree
Hide file tree
Showing 8 changed files with 2,674 additions and 190 deletions.
1,797 changes: 1,690 additions & 107 deletions api/side/btcbridge/params.pulsar.go

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ import (
incentivekeeper "github.com/sideprotocol/side/x/incentive/keeper"
incentivemodule "github.com/sideprotocol/side/x/incentive/module"
incentivetypes "github.com/sideprotocol/side/x/incentive/types"

// this line is used by starport scaffolding # stargate/app/moduleImport

upgradev2 "github.com/sideprotocol/side/app/upgrades/v2"
)

const (
Expand Down Expand Up @@ -857,6 +860,9 @@ func New(
panic(err)
}

// set upgrade handlers
app.SetUpgradeHandlers()

autocliv1.RegisterQueryServer(app.GRPCQueryRouter(), runtimeservices.NewAutoCLIQueryService(app.ModuleManager.Modules))

reflectionSvc, err := runtimeservices.NewReflectionService()
Expand Down Expand Up @@ -1133,6 +1139,11 @@ func BlockedAddresses() map[string]bool {
return modAccAddrs
}

// SetUpgradeHandlers sets the upgrade handlers
func (app *App) SetUpgradeHandlers() {
app.UpgradeKeeper.SetUpgradeHandler(upgradev2.UpgradeName, upgradev2.CreateUpgradeHandler(app.ModuleManager, app.configurator))
}

func GetWasmOpts(appOpts servertypes.AppOptions) []wasmkeeper.Option {
var wasmOpts []wasmkeeper.Option
if cast.ToBool(appOpts.Get("telemetry.enabled")) {
Expand Down
21 changes: 21 additions & 0 deletions app/upgrades/v2/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package v2

import (
"context"

upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/cosmos/cosmos-sdk/types/module"
)

// UpgradeName is the upgrade version name
const UpgradeName = "v2"

// CreateUpgradeHandler creates the upgrade handler
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
) upgradetypes.UpgradeHandler {
return func(ctx context.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
return mm.RunMigrations(ctx, configurator, vm)
}
}
30 changes: 30 additions & 0 deletions proto/side/btcbridge/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,36 @@ message Params {
TSSParams tss_params = 14 [(gogoproto.nullable) = false];
}

// ParamsV1 defines the V1 parameters for the module.
message ParamsV1 {
// The minimum number of confirmations required for a block to be accepted
int32 confirmations = 1;
// Indicates the maximum depth or distance from the latest block up to which transactions are considered for acceptance.
uint64 max_acceptable_block_depth = 2;
// The denomination of the voucher
string btc_voucher_denom = 3;
// Indicates if deposit is enabled
bool deposit_enabled = 4;
// Indicates if withdrawal is enabled
bool withdraw_enabled = 5;
// Trusted relayers for non-btc asset deposit
repeated string trusted_non_btc_relayers = 6;
// Trusted fee providers to submit bitcoin fee rate
repeated string trusted_oracles = 7;
// Period of validity for the fee rate
int64 fee_rate_validity_period = 8;
// Asset vaults
repeated Vault vaults = 9;
// Withdrawal params
WithdrawParams withdraw_params = 10 [(gogoproto.nullable) = false];
// Protocol limitations
ProtocolLimits protocol_limits = 11 [(gogoproto.nullable) = false];
// Protocol fees
ProtocolFees protocol_fees = 12 [(gogoproto.nullable) = false];
// TSS params
TSSParams tss_params = 13 [(gogoproto.nullable) = false];
}

// AssetType defines the type of asset
enum AssetType {
// Unspecified asset type
Expand Down
22 changes: 22 additions & 0 deletions x/btcbridge/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

v2 "github.com/sideprotocol/side/x/btcbridge/migrations/v2"
)

// Migrator is a struct for handling in-place store migrations
type Migrator struct {
keeper Keeper
}

// NewMigrator returns a new Migrator
func NewMigrator(keeper Keeper) Migrator {
return Migrator{keeper: keeper}
}

// Migrate1to2 migrates from version 1 to 2
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)
}
51 changes: 51 additions & 0 deletions x/btcbridge/migrations/v2/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package v2

import (
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/sideprotocol/side/x/btcbridge/types"
)

// MigrateStore migrates the x/btcbridge module state from the consensus version 1 to
// version 2
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
migrateParams(ctx, storeKey, cdc)

return nil
}

// migrateParams migrates the params
func migrateParams(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) {
store := ctx.KVStore(storeKey)

// get current params
var paramsV1 types.ParamsV1
bz := store.Get(types.ParamsStoreKey)
cdc.MustUnmarshal(bz, &paramsV1)

// build new params
params := &types.Params{
Confirmations: paramsV1.Confirmations,
MaxAcceptableBlockDepth: paramsV1.MaxAcceptableBlockDepth,
BtcVoucherDenom: paramsV1.BtcVoucherDenom,
DepositEnabled: paramsV1.DepositEnabled,
WithdrawEnabled: paramsV1.WithdrawEnabled,
TrustedNonBtcRelayers: paramsV1.TrustedNonBtcRelayers,
Vaults: paramsV1.Vaults,
WithdrawParams: paramsV1.WithdrawParams,
ProtocolLimits: paramsV1.ProtocolLimits,
ProtocolFees: paramsV1.ProtocolFees,
TssParams: types.TSSParams{
DkgTimeoutPeriod: paramsV1.TssParams.DkgTimeoutPeriod,
ParticipantUpdateTransitionPeriod: paramsV1.TssParams.ParticipantUpdateTransitionPeriod,
},
}

params.TrustedFeeProviders = paramsV1.TrustedOracles
params.TrustedBtcRelayers = []string{}

bz = cdc.MustMarshal(params)
store.Set(types.ParamsStoreKey, bz)
}
9 changes: 8 additions & 1 deletion x/btcbridge/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/sideprotocol/side/x/btcbridge/types"
)

const ConsensusVersion = 2

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
Expand Down Expand Up @@ -118,6 +120,11 @@ func (am AppModule) IsAppModule() {}
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)

m := keeper.NewMigrator(am.keeper)
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err))
}
}

// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
Expand All @@ -141,7 +148,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(_ context.Context) error {
Expand Down
Loading

0 comments on commit 20fc247

Please sign in to comment.