mukan-sdk/x/group/migrations/v2/migrate.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

93 lines
2.8 KiB
Go

package v2
import (
"encoding/binary"
"fmt"
storetypes "cosmossdk.io/store/types"
sdk "git.cw.tr/mukan-network/mukan-sdk/types"
"git.cw.tr/mukan-network/mukan-sdk/types/address"
authtypes "git.cw.tr/mukan-network/mukan-sdk/x/auth/types"
"git.cw.tr/mukan-network/mukan-sdk/x/group"
"git.cw.tr/mukan-network/mukan-sdk/x/group/internal/orm"
)
const (
ModuleName = "group"
// Group Policy Table
GroupPolicyTablePrefix byte = 0x20
GroupPolicyTableSeqPrefix byte = 0x21
)
// Migrate migrates the x/group module state from the consensus version 1 to version 2.
// Specifically, it changes the group policy account from module account to base account.
func Migrate(
ctx sdk.Context,
storeKey storetypes.StoreKey,
accountKeeper group.AccountKeeper,
groupPolicySeq orm.Sequence,
groupPolicyTable orm.PrimaryKeyTable,
) error {
store := ctx.KVStore(storeKey)
curAccVal := groupPolicySeq.CurVal(store)
groupPolicyAccountDerivationKey := make(map[string][]byte, 0)
policyKey := []byte{GroupPolicyTablePrefix}
for i := uint64(0); i <= curAccVal; i++ {
derivationKey := make([]byte, 8)
binary.BigEndian.PutUint64(derivationKey, i)
groupPolicyAcc := sdk.AccAddress(address.Module(group.ModuleName, policyKey, derivationKey))
groupPolicyAccountDerivationKey[groupPolicyAcc.String()] = derivationKey
}
// get all group policies
var groupPolicies []*group.GroupPolicyInfo
if _, err := groupPolicyTable.Export(store, &groupPolicies); err != nil {
return fmt.Errorf("failed to get group policies: %w", err)
}
for _, policy := range groupPolicies {
addr, err := accountKeeper.AddressCodec().StringToBytes(policy.Address)
if err != nil {
return fmt.Errorf("failed to convert group policy account address: %w", err)
}
// get the account address by acc id
oldAcc := accountKeeper.GetAccount(ctx, addr)
// remove the old account
accountKeeper.RemoveAccount(ctx, oldAcc)
// create the group policy account
derivationKey, ok := groupPolicyAccountDerivationKey[policy.Address]
if !ok {
// should never happen
panic(fmt.Errorf("group policy account %s derivation key not found", policy.Address))
}
ac, err := authtypes.NewModuleCredential(group.ModuleName, []byte{GroupPolicyTablePrefix}, derivationKey)
if err != nil {
return err
}
baseAccount, err := authtypes.NewBaseAccountWithPubKey(ac)
if err != nil {
return fmt.Errorf("failed to create new group policy account: %w", err)
}
// set account number
err = baseAccount.SetAccountNumber(oldAcc.GetAccountNumber())
if err != nil {
return err
}
// NOTE: we do not call NewAccount because we do not want to bump the account number
// set new account
// because we have only changed the account type, so we can use:
// - the same account number
// - the same address
accountKeeper.SetAccount(ctx, baseAccount)
}
return nil
}