2022-01-11 00:56:35 -08:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"encoding/json"
|
|
|
|
"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/crypto/keys/secp256k1"
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/group"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/group/keeper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GenesisTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
app *simapp.SimApp
|
|
|
|
ctx context.Context
|
|
|
|
sdkCtx sdk.Context
|
|
|
|
keeper keeper.Keeper
|
|
|
|
cdc *codec.ProtoCodec
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenesisTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(GenesisTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
memberPub = secp256k1.GenPrivKey().PubKey()
|
|
|
|
accPub = secp256k1.GenPrivKey().PubKey()
|
|
|
|
accAddr = sdk.AccAddress(accPub.Address())
|
|
|
|
memberAddr = sdk.AccAddress(memberPub.Address())
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *GenesisTestSuite) SetupSuite() {
|
|
|
|
checkTx := false
|
|
|
|
db := dbm.NewMemDB()
|
|
|
|
encCdc := simapp.MakeTestEncodingConfig()
|
|
|
|
app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, simapp.DefaultNodeHome, 5, encCdc, simapp.EmptyAppOptions{})
|
|
|
|
|
|
|
|
s.app = app
|
|
|
|
s.sdkCtx = app.BaseApp.NewUncachedContext(checkTx, tmproto.Header{})
|
|
|
|
s.keeper = app.GroupKeeper
|
|
|
|
s.cdc = codec.NewProtoCodec(app.InterfaceRegistry())
|
|
|
|
s.ctx = sdk.WrapSDKContext(s.sdkCtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GenesisTestSuite) TestInitExportGenesis() {
|
|
|
|
sdkCtx := s.sdkCtx
|
|
|
|
ctx := s.ctx
|
|
|
|
cdc := s.cdc
|
|
|
|
|
|
|
|
submittedAt := time.Now().UTC()
|
|
|
|
timeout := submittedAt.Add(time.Second * 1).UTC()
|
|
|
|
|
|
|
|
groupPolicy := &group.GroupPolicyInfo{
|
|
|
|
Address: accAddr.String(),
|
|
|
|
GroupId: 1,
|
|
|
|
Admin: accAddr.String(),
|
|
|
|
Version: 1,
|
2022-03-01 00:35:37 -08:00
|
|
|
Metadata: "policy metadata",
|
2022-01-11 00:56:35 -08:00
|
|
|
}
|
|
|
|
err := groupPolicy.SetDecisionPolicy(&group.ThresholdDecisionPolicy{
|
|
|
|
Threshold: "1",
|
2022-03-02 04:00:59 -08:00
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second,
|
|
|
|
},
|
2022-01-11 00:56:35 -08:00
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
proposal := &group.Proposal{
|
2022-02-14 05:48:17 -08:00
|
|
|
Id: 1,
|
2022-04-06 04:16:23 -07:00
|
|
|
GroupPolicyAddress: accAddr.String(),
|
2022-03-01 00:35:37 -08:00
|
|
|
Metadata: "proposal metadata",
|
2022-01-11 00:56:35 -08:00
|
|
|
GroupVersion: 1,
|
|
|
|
GroupPolicyVersion: 1,
|
|
|
|
Proposers: []string{
|
|
|
|
memberAddr.String(),
|
|
|
|
},
|
2022-02-14 05:48:17 -08:00
|
|
|
SubmitTime: submittedAt,
|
2022-04-06 04:16:23 -07:00
|
|
|
Status: group.PROPOSAL_STATUS_ACCEPTED,
|
2022-02-14 05:48:17 -08:00
|
|
|
FinalTallyResult: group.TallyResult{
|
|
|
|
YesCount: "1",
|
|
|
|
NoCount: "0",
|
|
|
|
AbstainCount: "0",
|
|
|
|
NoWithVetoCount: "0",
|
2022-01-11 00:56:35 -08:00
|
|
|
},
|
2022-03-02 04:00:59 -08:00
|
|
|
VotingPeriodEnd: timeout,
|
|
|
|
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_SUCCESS,
|
2022-01-11 00:56:35 -08:00
|
|
|
}
|
|
|
|
err = proposal.SetMsgs([]sdk.Msg{&banktypes.MsgSend{
|
|
|
|
FromAddress: accAddr.String(),
|
|
|
|
ToAddress: memberAddr.String(),
|
|
|
|
Amount: sdk.Coins{sdk.NewInt64Coin("test", 100)},
|
|
|
|
}})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
genesisState := &group.GenesisState{
|
2022-02-14 05:48:17 -08:00
|
|
|
GroupSeq: 2,
|
2022-03-01 00:35:37 -08:00
|
|
|
Groups: []*group.GroupInfo{{Id: 1, Admin: accAddr.String(), Metadata: "1", Version: 1, TotalWeight: "1"}, {Id: 2, Admin: accAddr.String(), Metadata: "2", Version: 2, TotalWeight: "2"}},
|
|
|
|
GroupMembers: []*group.GroupMember{{GroupId: 1, Member: &group.Member{Address: memberAddr.String(), Weight: "1", Metadata: "member metadata"}}, {GroupId: 2, Member: &group.Member{Address: memberAddr.String(), Weight: "2", Metadata: "member metadata"}}},
|
2022-01-21 02:52:26 -08:00
|
|
|
GroupPolicySeq: 1,
|
2022-02-14 05:48:17 -08:00
|
|
|
GroupPolicies: []*group.GroupPolicyInfo{groupPolicy},
|
|
|
|
ProposalSeq: 1,
|
|
|
|
Proposals: []*group.Proposal{proposal},
|
|
|
|
Votes: []*group.Vote{{ProposalId: proposal.Id, Voter: memberAddr.String(), SubmitTime: submittedAt, Option: group.VOTE_OPTION_YES}},
|
2022-01-11 00:56:35 -08:00
|
|
|
}
|
|
|
|
genesisBytes, err := cdc.MarshalJSON(genesisState)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
genesisData := map[string]json.RawMessage{
|
|
|
|
group.ModuleName: genesisBytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
s.keeper.InitGenesis(sdkCtx, cdc, genesisData[group.ModuleName])
|
|
|
|
|
|
|
|
for i, g := range genesisState.Groups {
|
|
|
|
res, err := s.keeper.GroupInfo(ctx, &group.QueryGroupInfoRequest{
|
2022-02-14 05:48:17 -08:00
|
|
|
GroupId: g.Id,
|
2022-01-11 00:56:35 -08:00
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Equal(g, res.Info)
|
|
|
|
|
|
|
|
membersRes, err := s.keeper.GroupMembers(ctx, &group.QueryGroupMembersRequest{
|
2022-02-14 05:48:17 -08:00
|
|
|
GroupId: g.Id,
|
2022-01-11 00:56:35 -08:00
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Equal(len(membersRes.Members), 1)
|
|
|
|
s.Require().Equal(membersRes.Members[0], genesisState.GroupMembers[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, g := range genesisState.GroupPolicies {
|
|
|
|
res, err := s.keeper.GroupPolicyInfo(ctx, &group.QueryGroupPolicyInfoRequest{
|
|
|
|
Address: g.Address,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.assertGroupPoliciesEqual(g, res.Info)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, g := range genesisState.Proposals {
|
|
|
|
res, err := s.keeper.Proposal(ctx, &group.QueryProposalRequest{
|
2022-02-14 05:48:17 -08:00
|
|
|
ProposalId: g.Id,
|
2022-01-11 00:56:35 -08:00
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.assertProposalsEqual(g, res.Proposal)
|
|
|
|
|
|
|
|
votesRes, err := s.keeper.VotesByProposal(ctx, &group.QueryVotesByProposalRequest{
|
2022-02-14 05:48:17 -08:00
|
|
|
ProposalId: g.Id,
|
2022-01-11 00:56:35 -08:00
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Equal(len(votesRes.Votes), 1)
|
|
|
|
s.Require().Equal(votesRes.Votes[0], genesisState.Votes[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
exported := s.keeper.ExportGenesis(sdkCtx, cdc)
|
|
|
|
bz, err := cdc.MarshalJSON(exported)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
var exportedGenesisState group.GenesisState
|
|
|
|
err = cdc.UnmarshalJSON(bz, &exportedGenesisState)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Require().Equal(genesisState.Groups, exportedGenesisState.Groups)
|
|
|
|
s.Require().Equal(genesisState.GroupMembers, exportedGenesisState.GroupMembers)
|
|
|
|
|
|
|
|
s.Require().Equal(len(genesisState.GroupPolicies), len(exportedGenesisState.GroupPolicies))
|
|
|
|
for i, g := range genesisState.GroupPolicies {
|
|
|
|
res := exportedGenesisState.GroupPolicies[i]
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.assertGroupPoliciesEqual(g, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Require().Equal(len(genesisState.Proposals), len(exportedGenesisState.Proposals))
|
|
|
|
for i, g := range genesisState.Proposals {
|
|
|
|
res := exportedGenesisState.Proposals[i]
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.assertProposalsEqual(g, res)
|
|
|
|
}
|
|
|
|
s.Require().Equal(genesisState.Votes, exportedGenesisState.Votes)
|
|
|
|
|
|
|
|
s.Require().Equal(genesisState.GroupSeq, exportedGenesisState.GroupSeq)
|
2022-01-21 02:52:26 -08:00
|
|
|
s.Require().Equal(genesisState.GroupPolicySeq, exportedGenesisState.GroupPolicySeq)
|
2022-01-11 00:56:35 -08:00
|
|
|
s.Require().Equal(genesisState.ProposalSeq, exportedGenesisState.ProposalSeq)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GenesisTestSuite) assertGroupPoliciesEqual(g *group.GroupPolicyInfo, other *group.GroupPolicyInfo) {
|
|
|
|
require := s.Require()
|
|
|
|
require.Equal(g.Address, other.Address)
|
|
|
|
require.Equal(g.GroupId, other.GroupId)
|
|
|
|
require.Equal(g.Admin, other.Admin)
|
|
|
|
require.Equal(g.Metadata, other.Metadata)
|
|
|
|
require.Equal(g.Version, other.Version)
|
2022-04-15 03:35:45 -07:00
|
|
|
dp1, err := g.GetDecisionPolicy()
|
|
|
|
require.NoError(err)
|
|
|
|
dp2, err := other.GetDecisionPolicy()
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(dp1, dp2)
|
2022-01-11 00:56:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GenesisTestSuite) assertProposalsEqual(g *group.Proposal, other *group.Proposal) {
|
|
|
|
require := s.Require()
|
2022-02-14 05:48:17 -08:00
|
|
|
require.Equal(g.Id, other.Id)
|
2022-04-06 04:16:23 -07:00
|
|
|
require.Equal(g.GroupPolicyAddress, other.GroupPolicyAddress)
|
2022-01-11 00:56:35 -08:00
|
|
|
require.Equal(g.Metadata, other.Metadata)
|
|
|
|
require.Equal(g.Proposers, other.Proposers)
|
2022-02-14 05:48:17 -08:00
|
|
|
require.Equal(g.SubmitTime, other.SubmitTime)
|
2022-01-11 00:56:35 -08:00
|
|
|
require.Equal(g.GroupVersion, other.GroupVersion)
|
|
|
|
require.Equal(g.GroupPolicyVersion, other.GroupPolicyVersion)
|
|
|
|
require.Equal(g.Status, other.Status)
|
2022-02-14 05:48:17 -08:00
|
|
|
require.Equal(g.FinalTallyResult, other.FinalTallyResult)
|
2022-03-02 04:00:59 -08:00
|
|
|
require.Equal(g.VotingPeriodEnd, other.VotingPeriodEnd)
|
2022-01-11 00:56:35 -08:00
|
|
|
require.Equal(g.ExecutorResult, other.ExecutorResult)
|
2022-04-15 03:35:45 -07:00
|
|
|
msgs1, err := g.GetMsgs()
|
|
|
|
require.NoError(err)
|
|
|
|
msgs2, err := other.GetMsgs()
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(msgs1, msgs2)
|
2022-01-11 00:56:35 -08:00
|
|
|
}
|