2021-12-17 04:09:38 -08:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
|
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
|
|
dbm "github.com/tendermint/tm-db"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/store"
|
|
|
|
|
|
|
|
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/group"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/group/internal/orm"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/group/keeper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type invariantTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
2022-03-02 04:00:59 -08:00
|
|
|
ctx sdk.Context
|
|
|
|
cdc *codec.ProtoCodec
|
|
|
|
key *storetypes.KVStoreKey
|
2021-12-17 04:09:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvariantTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(invariantTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *invariantTestSuite) SetupSuite() {
|
|
|
|
interfaceRegistry := types.NewInterfaceRegistry()
|
|
|
|
group.RegisterInterfaces(interfaceRegistry)
|
|
|
|
cdc := codec.NewProtoCodec(interfaceRegistry)
|
|
|
|
key := sdk.NewKVStoreKey(group.ModuleName)
|
|
|
|
db := dbm.NewMemDB()
|
|
|
|
cms := store.NewCommitMultiStore(db)
|
|
|
|
cms.MountStoreWithDB(key, storetypes.StoreTypeIAVL, db)
|
|
|
|
_ = cms.LoadLatestVersion()
|
|
|
|
sdkCtx := sdk.NewContext(cms, tmproto.Header{}, false, log.NewNopLogger())
|
|
|
|
|
|
|
|
s.ctx = sdkCtx
|
|
|
|
s.cdc = cdc
|
|
|
|
s.key = key
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *invariantTestSuite) TestTallyVotesInvariant() {
|
|
|
|
sdkCtx, _ := s.ctx.CacheContext()
|
|
|
|
curCtx, cdc, key := sdkCtx, s.cdc, s.key
|
|
|
|
prevCtx, _ := curCtx.CacheContext()
|
|
|
|
prevCtx = prevCtx.WithBlockHeight(curCtx.BlockHeight() - 1)
|
|
|
|
|
|
|
|
// Proposal Table
|
|
|
|
proposalTable, err := orm.NewAutoUInt64Table([2]byte{keeper.ProposalTablePrefix}, keeper.ProposalTableSeqPrefix, &group.Proposal{}, cdc)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
_, _, addr1 := testdata.KeyTestPubAddr()
|
|
|
|
_, _, addr2 := testdata.KeyTestPubAddr()
|
|
|
|
|
|
|
|
specs := map[string]struct {
|
|
|
|
prevProposal *group.Proposal
|
|
|
|
curProposal *group.Proposal
|
|
|
|
expBroken bool
|
|
|
|
}{
|
|
|
|
"invariant not broken": {
|
|
|
|
prevProposal: &group.Proposal{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2022-01-05 00:54:52 -08:00
|
|
|
Address: addr1.String(),
|
|
|
|
Proposers: []string{addr1.String()},
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: prevCtx.BlockTime(),
|
2022-01-05 00:54:52 -08:00
|
|
|
GroupVersion: 1,
|
|
|
|
GroupPolicyVersion: 1,
|
2022-02-14 05:48:17 -08:00
|
|
|
Status: group.PROPOSAL_STATUS_SUBMITTED,
|
|
|
|
Result: group.PROPOSAL_RESULT_UNFINALIZED,
|
|
|
|
FinalTallyResult: group.TallyResult{YesCount: "1", NoCount: "0", AbstainCount: "0", NoWithVetoCount: "0"},
|
2022-03-02 04:00:59 -08:00
|
|
|
VotingPeriodEnd: prevCtx.BlockTime().Add(time.Second * 600),
|
2022-02-14 05:48:17 -08:00
|
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
curProposal: &group.Proposal{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2022-01-05 00:54:52 -08:00
|
|
|
Address: addr2.String(),
|
|
|
|
Proposers: []string{addr2.String()},
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: curCtx.BlockTime(),
|
2022-01-05 00:54:52 -08:00
|
|
|
GroupVersion: 1,
|
|
|
|
GroupPolicyVersion: 1,
|
2022-02-14 05:48:17 -08:00
|
|
|
Status: group.PROPOSAL_STATUS_SUBMITTED,
|
|
|
|
Result: group.PROPOSAL_RESULT_UNFINALIZED,
|
|
|
|
FinalTallyResult: group.TallyResult{YesCount: "2", NoCount: "0", AbstainCount: "0", NoWithVetoCount: "0"},
|
2022-03-02 04:00:59 -08:00
|
|
|
VotingPeriodEnd: curCtx.BlockTime().Add(time.Second * 600),
|
2022-02-14 05:48:17 -08:00
|
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"current block yes vote count must be greater than previous block yes vote count": {
|
|
|
|
prevProposal: &group.Proposal{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2022-01-05 00:54:52 -08:00
|
|
|
Address: addr1.String(),
|
|
|
|
Proposers: []string{addr1.String()},
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: prevCtx.BlockTime(),
|
2022-01-05 00:54:52 -08:00
|
|
|
GroupVersion: 1,
|
|
|
|
GroupPolicyVersion: 1,
|
2022-02-14 05:48:17 -08:00
|
|
|
Status: group.PROPOSAL_STATUS_SUBMITTED,
|
|
|
|
Result: group.PROPOSAL_RESULT_UNFINALIZED,
|
|
|
|
FinalTallyResult: group.TallyResult{YesCount: "2", NoCount: "0", AbstainCount: "0", NoWithVetoCount: "0"},
|
2022-03-02 04:00:59 -08:00
|
|
|
VotingPeriodEnd: prevCtx.BlockTime().Add(time.Second * 600),
|
2022-02-14 05:48:17 -08:00
|
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
curProposal: &group.Proposal{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2022-01-05 00:54:52 -08:00
|
|
|
Address: addr2.String(),
|
|
|
|
Proposers: []string{addr2.String()},
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: curCtx.BlockTime(),
|
2022-01-05 00:54:52 -08:00
|
|
|
GroupVersion: 1,
|
|
|
|
GroupPolicyVersion: 1,
|
2022-02-14 05:48:17 -08:00
|
|
|
Status: group.PROPOSAL_STATUS_SUBMITTED,
|
|
|
|
Result: group.PROPOSAL_RESULT_UNFINALIZED,
|
|
|
|
FinalTallyResult: group.TallyResult{YesCount: "1", NoCount: "0", AbstainCount: "0", NoWithVetoCount: "0"},
|
2022-03-02 04:00:59 -08:00
|
|
|
VotingPeriodEnd: curCtx.BlockTime().Add(time.Second * 600),
|
2022-02-14 05:48:17 -08:00
|
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
expBroken: true,
|
|
|
|
},
|
|
|
|
"current block no vote count must be greater than previous block no vote count": {
|
|
|
|
prevProposal: &group.Proposal{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2022-01-05 00:54:52 -08:00
|
|
|
Address: addr1.String(),
|
|
|
|
Proposers: []string{addr1.String()},
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: prevCtx.BlockTime(),
|
2022-01-05 00:54:52 -08:00
|
|
|
GroupVersion: 1,
|
|
|
|
GroupPolicyVersion: 1,
|
2022-02-14 05:48:17 -08:00
|
|
|
Status: group.PROPOSAL_STATUS_SUBMITTED,
|
|
|
|
Result: group.PROPOSAL_RESULT_UNFINALIZED,
|
|
|
|
FinalTallyResult: group.TallyResult{YesCount: "0", NoCount: "2", AbstainCount: "0", NoWithVetoCount: "0"},
|
2022-03-02 04:00:59 -08:00
|
|
|
VotingPeriodEnd: prevCtx.BlockTime().Add(time.Second * 600),
|
2022-02-14 05:48:17 -08:00
|
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
curProposal: &group.Proposal{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2022-01-05 00:54:52 -08:00
|
|
|
Address: addr2.String(),
|
|
|
|
Proposers: []string{addr2.String()},
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: curCtx.BlockTime(),
|
2022-01-05 00:54:52 -08:00
|
|
|
GroupVersion: 1,
|
|
|
|
GroupPolicyVersion: 1,
|
2022-02-14 05:48:17 -08:00
|
|
|
Status: group.PROPOSAL_STATUS_SUBMITTED,
|
|
|
|
Result: group.PROPOSAL_RESULT_UNFINALIZED,
|
|
|
|
FinalTallyResult: group.TallyResult{YesCount: "0", NoCount: "1", AbstainCount: "0", NoWithVetoCount: "0"},
|
2022-03-02 04:00:59 -08:00
|
|
|
VotingPeriodEnd: curCtx.BlockTime().Add(time.Second * 600),
|
2022-02-14 05:48:17 -08:00
|
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
expBroken: true,
|
|
|
|
},
|
|
|
|
"current block abstain vote count must be greater than previous block abstain vote count": {
|
|
|
|
prevProposal: &group.Proposal{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2022-01-05 00:54:52 -08:00
|
|
|
Address: addr1.String(),
|
|
|
|
Proposers: []string{addr1.String()},
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: prevCtx.BlockTime(),
|
2022-01-05 00:54:52 -08:00
|
|
|
GroupVersion: 1,
|
|
|
|
GroupPolicyVersion: 1,
|
2022-02-14 05:48:17 -08:00
|
|
|
Status: group.PROPOSAL_STATUS_SUBMITTED,
|
|
|
|
Result: group.PROPOSAL_RESULT_UNFINALIZED,
|
|
|
|
FinalTallyResult: group.TallyResult{YesCount: "0", NoCount: "0", AbstainCount: "2", NoWithVetoCount: "0"},
|
2022-03-02 04:00:59 -08:00
|
|
|
VotingPeriodEnd: prevCtx.BlockTime().Add(time.Second * 600),
|
2022-02-14 05:48:17 -08:00
|
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
curProposal: &group.Proposal{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2022-01-05 00:54:52 -08:00
|
|
|
Address: addr2.String(),
|
|
|
|
Proposers: []string{addr2.String()},
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: curCtx.BlockTime(),
|
2022-01-05 00:54:52 -08:00
|
|
|
GroupVersion: 1,
|
|
|
|
GroupPolicyVersion: 1,
|
2022-02-14 05:48:17 -08:00
|
|
|
Status: group.PROPOSAL_STATUS_SUBMITTED,
|
|
|
|
Result: group.PROPOSAL_RESULT_UNFINALIZED,
|
|
|
|
FinalTallyResult: group.TallyResult{YesCount: "0", NoCount: "0", AbstainCount: "1", NoWithVetoCount: "0"},
|
2022-03-02 04:00:59 -08:00
|
|
|
VotingPeriodEnd: curCtx.BlockTime().Add(time.Second * 600),
|
2022-02-14 05:48:17 -08:00
|
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
expBroken: true,
|
|
|
|
},
|
|
|
|
"current block veto vote count must be greater than previous block veto vote count": {
|
|
|
|
prevProposal: &group.Proposal{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2022-01-05 00:54:52 -08:00
|
|
|
Address: addr1.String(),
|
|
|
|
Proposers: []string{addr1.String()},
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: prevCtx.BlockTime(),
|
2022-01-05 00:54:52 -08:00
|
|
|
GroupVersion: 1,
|
|
|
|
GroupPolicyVersion: 1,
|
2022-02-14 05:48:17 -08:00
|
|
|
Status: group.PROPOSAL_STATUS_SUBMITTED,
|
|
|
|
Result: group.PROPOSAL_RESULT_UNFINALIZED,
|
|
|
|
FinalTallyResult: group.TallyResult{YesCount: "0", NoCount: "0", AbstainCount: "0", NoWithVetoCount: "2"},
|
2022-03-02 04:00:59 -08:00
|
|
|
VotingPeriodEnd: prevCtx.BlockTime().Add(time.Second * 600),
|
2022-02-14 05:48:17 -08:00
|
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
curProposal: &group.Proposal{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2022-01-05 00:54:52 -08:00
|
|
|
Address: addr2.String(),
|
|
|
|
Proposers: []string{addr2.String()},
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: curCtx.BlockTime(),
|
2022-01-05 00:54:52 -08:00
|
|
|
GroupVersion: 1,
|
|
|
|
GroupPolicyVersion: 1,
|
2022-02-14 05:48:17 -08:00
|
|
|
Status: group.PROPOSAL_STATUS_SUBMITTED,
|
|
|
|
Result: group.PROPOSAL_RESULT_UNFINALIZED,
|
|
|
|
FinalTallyResult: group.TallyResult{YesCount: "0", NoCount: "0", AbstainCount: "0", NoWithVetoCount: "1"},
|
2022-03-02 04:00:59 -08:00
|
|
|
VotingPeriodEnd: curCtx.BlockTime().Add(time.Second * 600),
|
2022-02-14 05:48:17 -08:00
|
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
expBroken: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, spec := range specs {
|
|
|
|
|
|
|
|
prevProposal := spec.prevProposal
|
|
|
|
curProposal := spec.curProposal
|
|
|
|
|
|
|
|
cachePrevCtx, _ := prevCtx.CacheContext()
|
|
|
|
cacheCurCtx, _ := curCtx.CacheContext()
|
|
|
|
|
|
|
|
_, err = proposalTable.Create(cachePrevCtx.KVStore(key), prevProposal)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
_, err = proposalTable.Create(cacheCurCtx.KVStore(key), curProposal)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
_, broken := keeper.TallyVotesInvariantHelper(cacheCurCtx, cachePrevCtx, key, *proposalTable)
|
|
|
|
s.Require().Equal(spec.expBroken, broken)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *invariantTestSuite) TestGroupTotalWeightInvariant() {
|
|
|
|
sdkCtx, _ := s.ctx.CacheContext()
|
|
|
|
curCtx, cdc, key := sdkCtx, s.cdc, s.key
|
|
|
|
|
|
|
|
// Group Table
|
|
|
|
groupTable, err := orm.NewAutoUInt64Table([2]byte{keeper.GroupTablePrefix}, keeper.GroupTableSeqPrefix, &group.GroupInfo{}, cdc)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Group Member Table
|
|
|
|
groupMemberTable, err := orm.NewPrimaryKeyTable([2]byte{keeper.GroupMemberTablePrefix}, &group.GroupMember{}, cdc)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
groupMemberByGroupIndex, err := orm.NewIndex(groupMemberTable, keeper.GroupMemberByGroupIndexPrefix, func(val interface{}) ([]interface{}, error) {
|
|
|
|
group := val.(*group.GroupMember).GroupId
|
|
|
|
return []interface{}{group}, nil
|
|
|
|
}, group.GroupMember{}.GroupId)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
_, _, addr1 := testdata.KeyTestPubAddr()
|
|
|
|
_, _, addr2 := testdata.KeyTestPubAddr()
|
|
|
|
|
|
|
|
specs := map[string]struct {
|
|
|
|
groupsInfo *group.GroupInfo
|
|
|
|
groupMembers []*group.GroupMember
|
|
|
|
expBroken bool
|
|
|
|
}{
|
|
|
|
"invariant not broken": {
|
|
|
|
groupsInfo: &group.GroupInfo{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2021-12-17 04:09:38 -08:00
|
|
|
Admin: addr1.String(),
|
|
|
|
Version: 1,
|
|
|
|
TotalWeight: "3",
|
|
|
|
},
|
|
|
|
groupMembers: []*group.GroupMember{
|
|
|
|
{
|
|
|
|
GroupId: 1,
|
|
|
|
Member: &group.Member{
|
|
|
|
Address: addr1.String(),
|
|
|
|
Weight: "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GroupId: 1,
|
|
|
|
Member: &group.Member{
|
|
|
|
Address: addr2.String(),
|
|
|
|
Weight: "2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expBroken: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
"group's TotalWeight must be equal to sum of its members weight ": {
|
|
|
|
groupsInfo: &group.GroupInfo{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2021-12-17 04:09:38 -08:00
|
|
|
Admin: addr1.String(),
|
|
|
|
Version: 1,
|
|
|
|
TotalWeight: "3",
|
|
|
|
},
|
|
|
|
groupMembers: []*group.GroupMember{
|
|
|
|
{
|
|
|
|
GroupId: 1,
|
|
|
|
Member: &group.Member{
|
|
|
|
Address: addr1.String(),
|
|
|
|
Weight: "2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GroupId: 1,
|
|
|
|
Member: &group.Member{
|
|
|
|
Address: addr2.String(),
|
|
|
|
Weight: "2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expBroken: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, spec := range specs {
|
|
|
|
cacheCurCtx, _ := curCtx.CacheContext()
|
|
|
|
groupsInfo := spec.groupsInfo
|
|
|
|
groupMembers := spec.groupMembers
|
|
|
|
|
|
|
|
_, err := groupTable.Create(cacheCurCtx.KVStore(key), groupsInfo)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
for i := 0; i < len(groupMembers); i++ {
|
|
|
|
err := groupMemberTable.Create(cacheCurCtx.KVStore(key), groupMembers[i])
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, broken := keeper.GroupTotalWeightInvariantHelper(cacheCurCtx, key, *groupTable, groupMemberByGroupIndex)
|
|
|
|
s.Require().Equal(spec.expBroken, broken)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *invariantTestSuite) TestTallyVotesSumInvariant() {
|
|
|
|
sdkCtx, _ := s.ctx.CacheContext()
|
|
|
|
curCtx, cdc, key := sdkCtx, s.cdc, s.key
|
|
|
|
|
|
|
|
// Group Table
|
|
|
|
groupTable, err := orm.NewAutoUInt64Table([2]byte{keeper.GroupTablePrefix}, keeper.GroupTableSeqPrefix, &group.GroupInfo{}, cdc)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2022-01-05 00:54:52 -08:00
|
|
|
// Group Policy Table
|
|
|
|
groupPolicyTable, err := orm.NewPrimaryKeyTable([2]byte{keeper.GroupPolicyTablePrefix}, &group.GroupPolicyInfo{}, cdc)
|
2021-12-17 04:09:38 -08:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Group Member Table
|
|
|
|
groupMemberTable, err := orm.NewPrimaryKeyTable([2]byte{keeper.GroupMemberTablePrefix}, &group.GroupMember{}, cdc)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Proposal Table
|
|
|
|
proposalTable, err := orm.NewAutoUInt64Table([2]byte{keeper.ProposalTablePrefix}, keeper.ProposalTableSeqPrefix, &group.Proposal{}, cdc)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Vote Table
|
|
|
|
voteTable, err := orm.NewPrimaryKeyTable([2]byte{keeper.VoteTablePrefix}, &group.Vote{}, cdc)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
voteByProposalIndex, err := orm.NewIndex(voteTable, keeper.VoteByProposalIndexPrefix, func(value interface{}) ([]interface{}, error) {
|
|
|
|
return []interface{}{value.(*group.Vote).ProposalId}, nil
|
|
|
|
}, group.Vote{}.ProposalId)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
_, _, adminAddr := testdata.KeyTestPubAddr()
|
|
|
|
_, _, addr1 := testdata.KeyTestPubAddr()
|
|
|
|
_, _, addr2 := testdata.KeyTestPubAddr()
|
|
|
|
|
|
|
|
specs := map[string]struct {
|
|
|
|
groupsInfo *group.GroupInfo
|
2022-01-05 00:54:52 -08:00
|
|
|
groupPolicy *group.GroupPolicyInfo
|
2021-12-17 04:09:38 -08:00
|
|
|
groupMembers []*group.GroupMember
|
|
|
|
proposal *group.Proposal
|
|
|
|
votes []*group.Vote
|
|
|
|
expBroken bool
|
|
|
|
}{
|
|
|
|
"invariant not broken": {
|
|
|
|
groupsInfo: &group.GroupInfo{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2021-12-17 04:09:38 -08:00
|
|
|
Admin: adminAddr.String(),
|
|
|
|
Version: 1,
|
|
|
|
TotalWeight: "7",
|
|
|
|
},
|
2022-01-05 00:54:52 -08:00
|
|
|
groupPolicy: &group.GroupPolicyInfo{
|
2021-12-17 04:09:38 -08:00
|
|
|
Address: addr1.String(),
|
|
|
|
GroupId: 1,
|
|
|
|
Admin: adminAddr.String(),
|
|
|
|
Version: 1,
|
|
|
|
},
|
|
|
|
groupMembers: []*group.GroupMember{
|
|
|
|
{
|
|
|
|
GroupId: 1,
|
|
|
|
Member: &group.Member{
|
|
|
|
Address: addr1.String(),
|
|
|
|
Weight: "4",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GroupId: 1,
|
|
|
|
Member: &group.Member{
|
|
|
|
Address: addr2.String(),
|
|
|
|
Weight: "3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
proposal: &group.Proposal{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2022-01-05 00:54:52 -08:00
|
|
|
Address: addr1.String(),
|
|
|
|
Proposers: []string{addr1.String()},
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: curCtx.BlockTime(),
|
2022-01-05 00:54:52 -08:00
|
|
|
GroupVersion: 1,
|
|
|
|
GroupPolicyVersion: 1,
|
2022-02-14 05:48:17 -08:00
|
|
|
Status: group.PROPOSAL_STATUS_SUBMITTED,
|
|
|
|
Result: group.PROPOSAL_RESULT_UNFINALIZED,
|
|
|
|
FinalTallyResult: group.TallyResult{YesCount: "4", NoCount: "3", AbstainCount: "0", NoWithVetoCount: "0"},
|
2022-03-02 04:00:59 -08:00
|
|
|
VotingPeriodEnd: curCtx.BlockTime().Add(time.Second * 600),
|
2022-02-14 05:48:17 -08:00
|
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
votes: []*group.Vote{
|
|
|
|
{
|
2022-02-14 05:48:17 -08:00
|
|
|
ProposalId: 1,
|
|
|
|
Voter: addr1.String(),
|
|
|
|
Option: group.VOTE_OPTION_YES,
|
|
|
|
SubmitTime: curCtx.BlockTime(),
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
{
|
2022-02-14 05:48:17 -08:00
|
|
|
ProposalId: 1,
|
|
|
|
Voter: addr2.String(),
|
|
|
|
Option: group.VOTE_OPTION_NO,
|
|
|
|
SubmitTime: curCtx.BlockTime(),
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expBroken: false,
|
|
|
|
},
|
|
|
|
"proposal tally must correspond to the sum of vote weights": {
|
|
|
|
groupsInfo: &group.GroupInfo{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2021-12-17 04:09:38 -08:00
|
|
|
Admin: adminAddr.String(),
|
|
|
|
Version: 1,
|
|
|
|
TotalWeight: "5",
|
|
|
|
},
|
2022-01-05 00:54:52 -08:00
|
|
|
groupPolicy: &group.GroupPolicyInfo{
|
2021-12-17 04:09:38 -08:00
|
|
|
Address: addr1.String(),
|
|
|
|
GroupId: 1,
|
|
|
|
Admin: adminAddr.String(),
|
|
|
|
Version: 1,
|
|
|
|
},
|
|
|
|
groupMembers: []*group.GroupMember{
|
|
|
|
{
|
|
|
|
GroupId: 1,
|
|
|
|
Member: &group.Member{
|
|
|
|
Address: addr1.String(),
|
|
|
|
Weight: "2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GroupId: 1,
|
|
|
|
Member: &group.Member{
|
|
|
|
Address: addr2.String(),
|
|
|
|
Weight: "3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
proposal: &group.Proposal{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2022-01-05 00:54:52 -08:00
|
|
|
Address: addr1.String(),
|
|
|
|
Proposers: []string{addr1.String()},
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: curCtx.BlockTime(),
|
2022-01-05 00:54:52 -08:00
|
|
|
GroupVersion: 1,
|
|
|
|
GroupPolicyVersion: 1,
|
2022-02-14 05:48:17 -08:00
|
|
|
Status: group.PROPOSAL_STATUS_SUBMITTED,
|
|
|
|
Result: group.PROPOSAL_RESULT_UNFINALIZED,
|
|
|
|
FinalTallyResult: group.TallyResult{YesCount: "6", NoCount: "0", AbstainCount: "0", NoWithVetoCount: "0"},
|
2022-03-02 04:00:59 -08:00
|
|
|
VotingPeriodEnd: curCtx.BlockTime().Add(time.Second * 600),
|
2022-02-14 05:48:17 -08:00
|
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
votes: []*group.Vote{
|
|
|
|
{
|
2022-02-14 05:48:17 -08:00
|
|
|
ProposalId: 1,
|
|
|
|
Voter: addr1.String(),
|
|
|
|
Option: group.VOTE_OPTION_YES,
|
|
|
|
SubmitTime: curCtx.BlockTime(),
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
{
|
2022-02-14 05:48:17 -08:00
|
|
|
ProposalId: 1,
|
|
|
|
Voter: addr2.String(),
|
|
|
|
Option: group.VOTE_OPTION_YES,
|
|
|
|
SubmitTime: curCtx.BlockTime(),
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expBroken: true,
|
|
|
|
},
|
2022-02-14 05:48:17 -08:00
|
|
|
"proposal FinalTallyResult must correspond to the vote option": {
|
2021-12-17 04:09:38 -08:00
|
|
|
groupsInfo: &group.GroupInfo{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2021-12-17 04:09:38 -08:00
|
|
|
Admin: adminAddr.String(),
|
|
|
|
Version: 1,
|
|
|
|
TotalWeight: "7",
|
|
|
|
},
|
2022-01-05 00:54:52 -08:00
|
|
|
groupPolicy: &group.GroupPolicyInfo{
|
2021-12-17 04:09:38 -08:00
|
|
|
Address: addr1.String(),
|
|
|
|
GroupId: 1,
|
|
|
|
Admin: adminAddr.String(),
|
|
|
|
Version: 1,
|
|
|
|
},
|
|
|
|
groupMembers: []*group.GroupMember{
|
|
|
|
{
|
|
|
|
GroupId: 1,
|
|
|
|
Member: &group.Member{
|
|
|
|
Address: addr1.String(),
|
|
|
|
Weight: "4",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GroupId: 1,
|
|
|
|
Member: &group.Member{
|
|
|
|
Address: addr2.String(),
|
|
|
|
Weight: "3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
proposal: &group.Proposal{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2022-01-05 00:54:52 -08:00
|
|
|
Address: addr1.String(),
|
|
|
|
Proposers: []string{addr1.String()},
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: curCtx.BlockTime(),
|
2022-01-05 00:54:52 -08:00
|
|
|
GroupVersion: 1,
|
|
|
|
GroupPolicyVersion: 1,
|
2022-02-14 05:48:17 -08:00
|
|
|
Status: group.PROPOSAL_STATUS_SUBMITTED,
|
|
|
|
Result: group.PROPOSAL_RESULT_UNFINALIZED,
|
|
|
|
FinalTallyResult: group.TallyResult{YesCount: "4", NoCount: "3", AbstainCount: "0", NoWithVetoCount: "0"},
|
2022-03-02 04:00:59 -08:00
|
|
|
VotingPeriodEnd: curCtx.BlockTime().Add(time.Second * 600),
|
2022-02-14 05:48:17 -08:00
|
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
votes: []*group.Vote{
|
|
|
|
{
|
2022-02-14 05:48:17 -08:00
|
|
|
ProposalId: 1,
|
|
|
|
Voter: addr1.String(),
|
|
|
|
Option: group.VOTE_OPTION_YES,
|
|
|
|
SubmitTime: curCtx.BlockTime(),
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
{
|
2022-02-14 05:48:17 -08:00
|
|
|
ProposalId: 1,
|
|
|
|
Voter: addr2.String(),
|
|
|
|
Option: group.VOTE_OPTION_ABSTAIN,
|
|
|
|
SubmitTime: curCtx.BlockTime(),
|
2021-12-17 04:09:38 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expBroken: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, spec := range specs {
|
|
|
|
cacheCurCtx, _ := curCtx.CacheContext()
|
|
|
|
groupsInfo := spec.groupsInfo
|
|
|
|
proposal := spec.proposal
|
2022-01-05 00:54:52 -08:00
|
|
|
groupPolicy := spec.groupPolicy
|
2021-12-17 04:09:38 -08:00
|
|
|
groupMembers := spec.groupMembers
|
|
|
|
votes := spec.votes
|
|
|
|
|
|
|
|
_, err := groupTable.Create(cacheCurCtx.KVStore(key), groupsInfo)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2022-03-02 04:00:59 -08:00
|
|
|
err = groupPolicy.SetDecisionPolicy(group.NewThresholdDecisionPolicy("1", time.Second, 0))
|
2021-12-17 04:09:38 -08:00
|
|
|
s.Require().NoError(err)
|
2022-01-05 00:54:52 -08:00
|
|
|
err = groupPolicyTable.Create(cacheCurCtx.KVStore(key), groupPolicy)
|
2021-12-17 04:09:38 -08:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
for i := 0; i < len(groupMembers); i++ {
|
|
|
|
err = groupMemberTable.Create(cacheCurCtx.KVStore(key), groupMembers[i])
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = proposalTable.Create(cacheCurCtx.KVStore(key), proposal)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
for i := 0; i < len(votes); i++ {
|
|
|
|
err = voteTable.Create(cacheCurCtx.KVStore(key), votes[i])
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
2022-01-05 00:54:52 -08:00
|
|
|
_, broken := keeper.TallyVotesSumInvariantHelper(cacheCurCtx, key, *groupTable, *proposalTable, *groupMemberTable, voteByProposalIndex, *groupPolicyTable)
|
2021-12-17 04:09:38 -08:00
|
|
|
s.Require().Equal(spec.expBroken, broken)
|
|
|
|
}
|
|
|
|
}
|