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
116 lines
3.8 KiB
Go
116 lines
3.8 KiB
Go
package v2_test
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"cosmossdk.io/math"
|
|
storetypes "cosmossdk.io/store/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/runtime"
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/address"
|
|
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
|
v1 "github.com/cosmos/cosmos-sdk/x/gov/migrations/v1"
|
|
v2 "github.com/cosmos/cosmos-sdk/x/gov/migrations/v2"
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
|
|
)
|
|
|
|
func TestMigrateStore(t *testing.T) {
|
|
cdc := moduletestutil.MakeTestEncodingConfig().Codec
|
|
govKey := storetypes.NewKVStoreKey("gov")
|
|
ctx := testutil.DefaultContext(govKey, storetypes.NewTransientStoreKey("transient_test"))
|
|
store := ctx.KVStore(govKey)
|
|
|
|
_, _, addr1 := testdata.KeyTestPubAddr()
|
|
proposalID := uint64(6)
|
|
now := time.Now()
|
|
// Use dummy value for keys where we don't test values.
|
|
dummyValue := []byte("foo")
|
|
// Use real values for votes, as we're testing weighted votes.
|
|
oldVote := v1beta1.Vote{ProposalId: 1, Voter: "foobar", Option: v1beta1.OptionNoWithVeto}
|
|
oldVoteValue := cdc.MustMarshal(&oldVote)
|
|
newVote := v1beta1.Vote{ProposalId: 1, Voter: "foobar", Options: v1beta1.WeightedVoteOptions{{Option: v1beta1.OptionNoWithVeto, Weight: math.LegacyNewDec(1)}}}
|
|
newVoteValue := cdc.MustMarshal(&newVote)
|
|
|
|
testCases := []struct {
|
|
name string
|
|
oldKey, oldValue, newKey, newValue []byte
|
|
}{
|
|
{
|
|
"ProposalKey",
|
|
v1.ProposalKey(proposalID), dummyValue,
|
|
append(types.ProposalsKeyPrefix, sdk.Uint64ToBigEndian(proposalID)...), dummyValue,
|
|
},
|
|
{
|
|
"ActiveProposalQueue",
|
|
v1.ActiveProposalQueueKey(proposalID, now), dummyValue,
|
|
activeProposalQueueKey(proposalID, now), dummyValue,
|
|
},
|
|
{
|
|
"InactiveProposalQueue",
|
|
v1.InactiveProposalQueueKey(proposalID, now), dummyValue,
|
|
inactiveProposalQueueKey(proposalID, now), dummyValue,
|
|
},
|
|
{
|
|
"ProposalIDKey",
|
|
v1.ProposalIDKey, dummyValue,
|
|
types.ProposalIDKey, dummyValue,
|
|
},
|
|
{
|
|
"DepositKey",
|
|
v1.DepositKey(proposalID, addr1), dummyValue,
|
|
depositKey(proposalID, addr1), dummyValue,
|
|
},
|
|
{
|
|
"VotesKeyPrefix",
|
|
v1.VoteKey(proposalID, addr1), oldVoteValue,
|
|
voteKey(proposalID, addr1), newVoteValue,
|
|
},
|
|
}
|
|
|
|
// Set all the old keys to the store
|
|
for _, tc := range testCases {
|
|
store.Set(tc.oldKey, tc.oldValue)
|
|
}
|
|
|
|
// Run migration
|
|
storeService := runtime.NewKVStoreService(govKey)
|
|
err := v2.MigrateStore(ctx, storeService, cdc)
|
|
require.NoError(t, err)
|
|
|
|
// Make sure the new keys are set and old keys are deleted.
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if !bytes.Equal(tc.oldKey, tc.newKey) {
|
|
require.Nil(t, store.Get(tc.oldKey))
|
|
}
|
|
require.Equal(t, tc.newValue, store.Get(tc.newKey))
|
|
})
|
|
}
|
|
}
|
|
|
|
// TODO(tip): remove all the functions below once we delete the migrations
|
|
|
|
func depositKey(proposalID uint64, depositorAddr sdk.AccAddress) []byte {
|
|
return append(append(types.DepositsKeyPrefix, sdk.Uint64ToBigEndian(proposalID)...), address.MustLengthPrefix(depositorAddr.Bytes())...)
|
|
}
|
|
|
|
func voteKey(proposalID uint64, addr sdk.AccAddress) []byte {
|
|
return append(append(types.VotesKeyPrefix, sdk.Uint64ToBigEndian(proposalID)...), address.MustLengthPrefix(addr.Bytes())...)
|
|
}
|
|
|
|
func activeProposalQueueKey(proposalID uint64, endTime time.Time) []byte {
|
|
return append(append(types.ActiveProposalQueuePrefix, sdk.FormatTimeBytes(endTime)...), sdk.Uint64ToBigEndian(proposalID)...)
|
|
}
|
|
|
|
// InactiveProposalQueueKey returns the key for a proposalID in the inactiveProposalQueue
|
|
func inactiveProposalQueueKey(proposalID uint64, endTime time.Time) []byte {
|
|
return append(append(types.InactiveProposalQueuePrefix, sdk.FormatTimeBytes(endTime)...), sdk.Uint64ToBigEndian(proposalID)...)
|
|
}
|