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
115 lines
4.7 KiB
Go
115 lines
4.7 KiB
Go
package keeper_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"cosmossdk.io/collections"
|
|
"cosmossdk.io/math"
|
|
|
|
"git.cw.tr/mukan-network/mukan-sdk/codec/address"
|
|
simtestutil "git.cw.tr/mukan-network/mukan-sdk/testutil/sims"
|
|
v1 "git.cw.tr/mukan-network/mukan-sdk/x/gov/types/v1"
|
|
)
|
|
|
|
func TestVoteRemovalAfterTally(t *testing.T) {
|
|
govKeeper, authKeeper, bankKeeper, stakingKeeper, _, _, ctx := setupGovKeeper(t)
|
|
authKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()
|
|
stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()
|
|
|
|
addrs := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, ctx, 3, math.NewInt(30000000))
|
|
|
|
// Create a test proposal
|
|
tp := TestProposal
|
|
proposal, err := govKeeper.SubmitProposal(ctx, tp, "", "test", "summary", addrs[0], false)
|
|
require.NoError(t, err)
|
|
proposalID := proposal.Id
|
|
|
|
// Activate voting period
|
|
proposal.Status = v1.StatusVotingPeriod
|
|
require.NoError(t, govKeeper.SetProposal(ctx, proposal))
|
|
|
|
// Add votes from different addresses
|
|
require.NoError(t, govKeeper.AddVote(ctx, proposalID, addrs[0], v1.NewNonSplitVoteOption(v1.OptionYes), ""))
|
|
require.NoError(t, govKeeper.AddVote(ctx, proposalID, addrs[1], v1.NewNonSplitVoteOption(v1.OptionNo), ""))
|
|
require.NoError(t, govKeeper.AddVote(ctx, proposalID, addrs[2], v1.NewNonSplitVoteOption(v1.OptionAbstain), ""))
|
|
|
|
// verify votes were added to state
|
|
for i, addr := range addrs {
|
|
vote, err := govKeeper.Votes.Get(ctx, collections.Join(proposalID, addr))
|
|
require.NoError(t, err, "Vote for address %d should exist before tally", i)
|
|
require.NotNil(t, vote, "Vote for address %d should not be nil before tally", i)
|
|
}
|
|
|
|
// tally the proposal
|
|
proposal, err = govKeeper.Proposals.Get(ctx, proposalID)
|
|
require.NoError(t, err)
|
|
_, _, _, err = govKeeper.Tally(ctx, proposal)
|
|
require.NoError(t, err)
|
|
|
|
// votes should be deleted.
|
|
for i, addr := range addrs {
|
|
_, err := govKeeper.Votes.Get(ctx, collections.Join(proposalID, addr))
|
|
require.Error(t, err, "Vote for address %d should be removed after tally", i)
|
|
require.ErrorIs(t, err, collections.ErrNotFound, "Error should be ErrNotFound for address %d after tally", i)
|
|
}
|
|
}
|
|
|
|
// TestMultipleProposalsVoteRemoval verifies that votes for one proposal are removed
|
|
// while votes for another proposal are preserved during tallying
|
|
func TestMultipleProposalsVoteRemoval(t *testing.T) {
|
|
govKeeper, authKeeper, bankKeeper, stakingKeeper, _, _, ctx := setupGovKeeper(t)
|
|
authKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()
|
|
stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()
|
|
|
|
addrs := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, ctx, 2, math.NewInt(30000000))
|
|
|
|
tp := TestProposal
|
|
proposal1, err := govKeeper.SubmitProposal(ctx, tp, "", "test1", "summary", addrs[0], false)
|
|
require.NoError(t, err)
|
|
proposal1ID := proposal1.Id
|
|
|
|
proposal2, err := govKeeper.SubmitProposal(ctx, tp, "", "test2", "summary", addrs[0], false)
|
|
require.NoError(t, err)
|
|
proposal2ID := proposal2.Id
|
|
|
|
// activate both proposals
|
|
proposal1.Status = v1.StatusVotingPeriod
|
|
require.NoError(t, govKeeper.SetProposal(ctx, proposal1))
|
|
proposal2.Status = v1.StatusVotingPeriod
|
|
require.NoError(t, govKeeper.SetProposal(ctx, proposal2))
|
|
|
|
// add some votes for both proposals
|
|
require.NoError(t, govKeeper.AddVote(ctx, proposal1ID, addrs[0], v1.NewNonSplitVoteOption(v1.OptionYes), ""))
|
|
require.NoError(t, govKeeper.AddVote(ctx, proposal1ID, addrs[1], v1.NewNonSplitVoteOption(v1.OptionNo), ""))
|
|
|
|
require.NoError(t, govKeeper.AddVote(ctx, proposal2ID, addrs[0], v1.NewNonSplitVoteOption(v1.OptionNo), ""))
|
|
require.NoError(t, govKeeper.AddVote(ctx, proposal2ID, addrs[1], v1.NewNonSplitVoteOption(v1.OptionYes), ""))
|
|
|
|
// votes should eixst
|
|
vote1Addr0, err := govKeeper.Votes.Get(ctx, collections.Join(proposal1ID, addrs[0]))
|
|
require.NoError(t, err)
|
|
require.Equal(t, v1.OptionYes, vote1Addr0.Options[0].Option)
|
|
vote2Addr0, err := govKeeper.Votes.Get(ctx, collections.Join(proposal2ID, addrs[0]))
|
|
require.NoError(t, err)
|
|
require.Equal(t, v1.OptionNo, vote2Addr0.Options[0].Option)
|
|
|
|
// only tally proposal1
|
|
proposal1, err = govKeeper.Proposals.Get(ctx, proposal1ID)
|
|
require.NoError(t, err)
|
|
_, _, _, err = govKeeper.Tally(ctx, proposal1)
|
|
require.NoError(t, err)
|
|
|
|
// check votes
|
|
for _, addr := range addrs {
|
|
// proposal1 votes should be deleted
|
|
_, err := govKeeper.Votes.Get(ctx, collections.Join(proposal1ID, addr))
|
|
require.Error(t, err)
|
|
require.ErrorIs(t, err, collections.ErrNotFound)
|
|
|
|
// proposal2 votes should still exist.
|
|
_, err = govKeeper.Votes.Get(ctx, collections.Join(proposal2ID, addr))
|
|
require.NoError(t, err)
|
|
}
|
|
}
|