Some checks failed
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
Build & Push SDK Proto Builder / build (push) Has been cancelled
102 lines
3 KiB
Go
102 lines
3 KiB
Go
package v2
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
corestoretypes "cosmossdk.io/core/store"
|
|
"cosmossdk.io/store/prefix"
|
|
storetypes "cosmossdk.io/store/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
"github.com/cosmos/cosmos-sdk/runtime"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/address"
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
|
|
)
|
|
|
|
const proposalIDLen = 8
|
|
|
|
// migratePrefixProposalAddress is a helper function that migrates all keys of format:
|
|
// <prefix_bytes><proposal_id (8 bytes)><address_bytes>
|
|
// into format:
|
|
// <prefix_bytes><proposal_id (8 bytes)><address_len (1 byte)><address_bytes>
|
|
func migratePrefixProposalAddress(store corestoretypes.KVStore, prefixBz []byte) error {
|
|
oldStore := prefix.NewStore(runtime.KVStoreAdapter(store), prefixBz)
|
|
|
|
oldStoreIter := oldStore.Iterator(nil, nil)
|
|
defer oldStoreIter.Close()
|
|
|
|
for ; oldStoreIter.Valid(); oldStoreIter.Next() {
|
|
proposalID := oldStoreIter.Key()[:proposalIDLen]
|
|
addr := oldStoreIter.Key()[proposalIDLen:]
|
|
newStoreKey := append(append(prefixBz, proposalID...), address.MustLengthPrefix(addr)...)
|
|
|
|
// Set new key on store. Values don't change.
|
|
err := store.Set(newStoreKey, oldStoreIter.Value())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
oldStore.Delete(oldStoreIter.Key())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// migrateStoreWeightedVotes migrates a legacy vote to an ADR-037 weighted vote.
|
|
// Important: the `oldVote` has its `Option` field set, whereas the new weighted
|
|
// vote has its `Options` field set.
|
|
func migrateVote(oldVote v1beta1.Vote) v1beta1.Vote {
|
|
return v1beta1.Vote{
|
|
ProposalId: oldVote.ProposalId,
|
|
Voter: oldVote.Voter,
|
|
Options: v1beta1.NewNonSplitVoteOption(oldVote.Option),
|
|
}
|
|
}
|
|
|
|
// migrateStoreWeightedVotes migrates in-place all legacy votes to ADR-037 weighted votes.
|
|
func migrateStoreWeightedVotes(store corestoretypes.KVStore, cdc codec.BinaryCodec) error {
|
|
iterator := storetypes.KVStorePrefixIterator(runtime.KVStoreAdapter(store), types.VotesKeyPrefix)
|
|
|
|
defer iterator.Close()
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
var oldVote v1beta1.Vote
|
|
err := cdc.Unmarshal(iterator.Value(), &oldVote)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
newVote := migrateVote(oldVote)
|
|
fmt.Println("migrateStoreWeightedVotes newVote=", newVote)
|
|
bz, err := cdc.Marshal(&newVote)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = store.Set(iterator.Key(), bz)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// MigrateStore performs in-place store migrations from v1 (v0.40) to v2 (v0.43). The
|
|
// migration includes:
|
|
//
|
|
// - Change addresses to be length-prefixed.
|
|
// - Change all legacy votes to ADR-037 weighted votes.
|
|
func MigrateStore(ctx sdk.Context, storeService corestoretypes.KVStoreService, cdc codec.BinaryCodec) error {
|
|
store := storeService.OpenKVStore(ctx)
|
|
err := migratePrefixProposalAddress(store, types.DepositsKeyPrefix)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = migratePrefixProposalAddress(store, types.VotesKeyPrefix)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return migrateStoreWeightedVotes(store, cdc)
|
|
}
|