Skip to content

Commit

Permalink
Merge pull request #157 from iov-one/configuration-migration
Browse files Browse the repository at this point in the history
Bump configuration module to v2 and adds a migration for escrow params
  • Loading branch information
merge-when-green[bot] authored Dec 17, 2021
2 parents b6ed248 + 4b7c546 commit 578e2d2
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func getCosmosSDKv44UpgradeHandler(app *WasmApp) upgradeData {

// Custom modules
"burner": 1, // the burner module has no state but it implements AppModule so its better to put it here
"configuration": 1,
"configuration": 1, // the configuration module will be updated to version 2 (adding the escrow conf)
"starname": 1,
"wasm": 1,

Expand Down
1 change: 0 additions & 1 deletion x/configuration/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package configuration

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

"github.com/iov-one/starnamed/x/configuration/types"
)

Expand Down
42 changes: 42 additions & 0 deletions x/configuration/migrations/v2/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package v2

import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/iov-one/starnamed/x/configuration/types"
)

// MigrateStore performs in-place store migrations from version 1 to version 2
// This adds the escrow parameters to the configuration
func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)

confBytes := store.Get([]byte(types.ConfigKey))
if confBytes == nil {
return fmt.Errorf("no configuration available")
}

feesBytes := store.Get([]byte(types.FeeKey))
if feesBytes == nil {
return fmt.Errorf("no fees available")
}

defaultState := types.DefaultGenesisState()

// Get the default state for the configuration
config := defaultState.Config
// Overwrite with the values present in the chain for old fields
cdc.MustUnmarshal(confBytes, &config)

// Get the default state for the fees
fees := defaultState.Fees
// Overwrite with the values present in the chain for old fields
cdc.MustUnmarshal(feesBytes, &fees)

// Write back the configuration and the fees
store.Set([]byte(types.ConfigKey), cdc.MustMarshal(&config))
store.Set([]byte(types.FeeKey), cdc.MustMarshal(&fees))

return nil
}
21 changes: 21 additions & 0 deletions x/configuration/migrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package configuration

import (
sdk "github.com/cosmos/cosmos-sdk/types"
v2 "github.com/iov-one/starnamed/x/configuration/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)
}
12 changes: 9 additions & 3 deletions x/configuration/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package configuration
import (
"context"
"encoding/json"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -43,7 +44,7 @@ func (AppModuleBasic) Name() string { return types.ModuleName }

// DefaultGenesis returns default genesis state as raw bytes for the configuration module.
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
defaultGenesisState := DefaultGenesisState()
defaultGenesisState := types.DefaultGenesisState()
return cdc.MustMarshalJSON(&defaultGenesisState)
}

Expand All @@ -54,7 +55,7 @@ func (b AppModuleBasic) ValidateGenesis(marshaler codec.JSONCodec, config client
if err != nil {
return err
}
return ValidateGenesis(data)
return types.ValidateGenesis(data)
}

// RegisterRESTRoutes registers the REST routes for the configuration module.
Expand Down Expand Up @@ -97,6 +98,11 @@ func (AppModule) Name() string { return types.ModuleName }
// RegisterServices allows a module to register services
func (a AppModule) RegisterServices(configurator module.Configurator) {
types.RegisterQueryServer(configurator.QueryServer(), NewQuerier(&a.keeper))

m := NewMigrator(a.keeper)
if err := configurator.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
panic(sdkerrors.Wrapf(err, "Error while registering the configuration module migration from version 1 to 2"))
}
}

// LegacyQuerierHandler provides an sdk.Querier object that uses the legacy amino codec.
Expand Down Expand Up @@ -138,4 +144,4 @@ func (a AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawM
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return 2 }
63 changes: 63 additions & 0 deletions x/configuration/types/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package types

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

// NewGenesisState is GenesisState constructor
func NewGenesisState(conf Config, fees *Fees) GenesisState {
return GenesisState{
Config: conf,
Fees: *fees,
}
}

// ValidateGenesis makes sure that the genesis state is valid
func ValidateGenesis(data GenesisState) error {
conf := data.Config
if err := conf.Validate(); err != nil {
return err
}
if err := data.Fees.Validate(); err != nil {
return err
}
return nil
}

// DefaultGenesisState returns the default genesis state
// TODO this needs to be updated, although it will be imported from iovns chain
func DefaultGenesisState() GenesisState {
// set default configs
config := Config{
Configurer: "star1d3lhm5vtta78cm7c7ytzqh7z5pcgktmautntqv", // msig1
ValidDomainName: "^[-_a-z0-9]{4,16}$",
ValidAccountName: "^[-_\\.a-z0-9]{1,64}$",
ValidURI: "^[-a-z0-9A-Z:]+$",
ValidResource: "^[a-z0-9A-Z]+$",
DomainRenewalPeriod: 31557600 * 1e9,
DomainRenewalCountMax: 2,
DomainGracePeriod: 2592000 * 1e9,
AccountRenewalPeriod: 31557600 * 1e9,
AccountRenewalCountMax: 3,
AccountGracePeriod: 2592000 * 1e9,
ResourcesMax: 3,
CertificateSizeMax: 10000,
CertificateCountMax: 3,
MetadataSizeMax: 86400,
EscrowCommission: sdk.NewDecFromInt(sdk.NewInt(1)).QuoInt(sdk.NewInt(100)), // 1%
EscrowBroker: "star1nrnx8mft8mks3l2akduxdjlf8rwqs8r9l36a78", // to IOV msig account
EscrowMaxPeriod: 7890000 * 1e9, // 3 months
}
// set fees
// add domain module fees
feeCoinDenom := "tiov" // set coin denom used for fees
// generate new fees
fees := NewFees()
// set default fees
fees.SetDefaults(feeCoinDenom)
// return genesis
return GenesisState{
Config: config,
Fees: *fees,
}
}
3 changes: 2 additions & 1 deletion x/escrow/test/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"encoding/hex"
configurationtypes "github.com/iov-one/starnamed/x/configuration/types"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -213,7 +214,7 @@ func NewTestKeeper(coinHolders []sdk.AccAddress, isModuleEnabled bool) (keeper.K
defaultFees.SetDefaults(Denom)
configKeeper.SetFees(ctx, defaultFees)

defaultConfig := configuration.DefaultGenesisState().Config
defaultConfig := configurationtypes.DefaultGenesisState().Config
configKeeper.SetConfig(ctx, defaultConfig)

// register blocked addresses
Expand Down

0 comments on commit 578e2d2

Please sign in to comment.