mukan-sdk/x/gov/migrations/v4/store.go
Mukan Erkin Törük abb1ff956e
Some checks are pending
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
refactor: complete sovereign stack cleanup — all github.com upstream refs purged
2026-05-11 03:46:06 +03:00

139 lines
4.2 KiB
Go

package v4
import (
"fmt"
"slices"
corestoretypes "cosmossdk.io/core/store"
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
"git.cw.tr/mukan-network/mukan-sdk/codec"
"git.cw.tr/mukan-network/mukan-sdk/runtime"
sdk "git.cw.tr/mukan-network/mukan-sdk/types"
"git.cw.tr/mukan-network/mukan-sdk/x/gov/exported"
v1 "git.cw.tr/mukan-network/mukan-sdk/x/gov/migrations/v1"
"git.cw.tr/mukan-network/mukan-sdk/x/gov/types"
govv1 "git.cw.tr/mukan-network/mukan-sdk/x/gov/types/v1"
)
func migrateParams(ctx sdk.Context, store storetypes.KVStore, legacySubspace exported.ParamSubspace, cdc codec.BinaryCodec) error {
dp := govv1.DepositParams{}
vp := govv1.VotingParams{}
tp := govv1.TallyParams{}
legacySubspace.Get(ctx, govv1.ParamStoreKeyDepositParams, &dp)
legacySubspace.Get(ctx, govv1.ParamStoreKeyVotingParams, &vp)
legacySubspace.Get(ctx, govv1.ParamStoreKeyTallyParams, &tp)
defaultParams := govv1.DefaultParams()
params := govv1.NewParams(
dp.MinDeposit,
defaultParams.ExpeditedMinDeposit,
*dp.MaxDepositPeriod,
*vp.VotingPeriod,
*defaultParams.ExpeditedVotingPeriod,
tp.Quorum,
tp.Threshold,
defaultParams.ExpeditedThreshold,
tp.VetoThreshold,
defaultParams.MinInitialDepositRatio,
defaultParams.ProposalCancelRatio,
defaultParams.ProposalCancelDest,
defaultParams.BurnProposalDepositPrevote,
defaultParams.BurnVoteQuorum,
defaultParams.BurnVoteVeto,
defaultParams.MinDepositRatio,
)
bz, err := cdc.Marshal(&params)
if err != nil {
return err
}
store.Set(ParamsKey, bz)
return nil
}
func migrateProposalVotingPeriod(ctx sdk.Context, store storetypes.KVStore, cdc codec.BinaryCodec) error {
propStore := prefix.NewStore(store, v1.ProposalsKeyPrefix)
iter := propStore.Iterator(nil, nil)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var prop govv1.Proposal
err := cdc.Unmarshal(iter.Value(), &prop)
if err != nil {
return err
}
if prop.Status == govv1.StatusVotingPeriod {
store.Set(VotingPeriodProposalKey(prop.Id), []byte{1})
}
}
return nil
}
// MigrateStore performs in-place store migrations from v3 (v0.46) to v4 (v0.47). The
// migration includes:
//
// Params migrations from x/params to gov
// Addition of the new min initial deposit ratio parameter that is set to 0 by default.
// Proposals in voting period are tracked in a separate index.
func MigrateStore(ctx sdk.Context, storeService corestoretypes.KVStoreService, legacySubspace exported.ParamSubspace, cdc codec.BinaryCodec) error {
store := runtime.KVStoreAdapter(storeService.OpenKVStore(ctx))
if err := migrateProposalVotingPeriod(ctx, store, cdc); err != nil {
return err
}
return migrateParams(ctx, store, legacySubspace, cdc)
}
// AddProposerAddressToProposal will add proposer to proposal and set to the store. This function is optional.
func AddProposerAddressToProposal(ctx sdk.Context, storeService corestoretypes.KVStoreService, cdc codec.BinaryCodec, proposals map[uint64]string) error {
proposalIDs := make([]uint64, 0, len(proposals))
for proposalID := range proposals {
proposalIDs = append(proposalIDs, proposalID)
}
// sort the proposalIDs
slices.Sort(proposalIDs)
store := runtime.KVStoreAdapter(storeService.OpenKVStore(ctx))
for _, proposalID := range proposalIDs {
if len(proposals[proposalID]) == 0 {
return fmt.Errorf("found missing proposer for proposal ID: %d", proposalID)
}
if _, err := sdk.AccAddressFromBech32(proposals[proposalID]); err != nil {
return fmt.Errorf("invalid proposer address : %s", proposals[proposalID])
}
bz := store.Get(append(types.ProposalsKeyPrefix, sdk.Uint64ToBigEndian(proposalID)...))
var proposal govv1.Proposal
if err := cdc.Unmarshal(bz, &proposal); err != nil {
panic(err)
}
// Check if proposal is active
if proposal.Status != govv1.ProposalStatus_PROPOSAL_STATUS_VOTING_PERIOD &&
proposal.Status != govv1.ProposalStatus_PROPOSAL_STATUS_DEPOSIT_PERIOD {
return fmt.Errorf("invalid proposal : %s, proposal not active", proposals[proposalID])
}
proposal.Proposer = proposals[proposalID]
// set the new proposal with proposer
bz, err := cdc.Marshal(&proposal)
if err != nil {
panic(err)
}
store.Set(append(types.ProposalsKeyPrefix, sdk.Uint64ToBigEndian(proposalID)...), bz)
}
return nil
}