address various pr comments

This commit is contained in:
rhuairahrighairigh 2020-04-25 00:22:56 +01:00
parent ccad1f82e2
commit ebb6366837
10 changed files with 120 additions and 110 deletions

View File

@ -41,7 +41,7 @@ func (suite *ModuleTestSuite) TestBeginBlock() {
Members: suite.addresses[:2],
Permissions: []committee.Permission{committee.GodPermission{}},
VoteThreshold: d("0.8"),
MaxProposalDuration: time.Hour * 24 * 7,
ProposalDuration: time.Hour * 24 * 7,
}
suite.keeper.SetCommittee(suite.ctx, normalCom)
@ -55,7 +55,7 @@ func (suite *ModuleTestSuite) TestBeginBlock() {
suite.NoError(err)
// Run BeginBlocker
proposalDurationLaterCtx := suite.ctx.WithBlockTime(suite.ctx.BlockTime().Add(normalCom.MaxProposalDuration))
proposalDurationLaterCtx := suite.ctx.WithBlockTime(suite.ctx.BlockTime().Add(normalCom.ProposalDuration))
suite.NotPanics(func() {
committee.BeginBlocker(proposalDurationLaterCtx, abci.RequestBeginBlock{}, suite.keeper)
})

View File

@ -10,7 +10,6 @@ import (
const (
RestProposalID = "proposal-id"
RestCommitteeID = "committee-id"
RestVoter = "voter"
)
// RegisterRoutes - Central function to define routes that get registered by the main application

View File

@ -2,16 +2,18 @@ package keeper_test
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/x/committee/keeper"
"github.com/kava-labs/kava/x/committee/types"
)
// proposalVoteMap collects up votes into a map indexed by proposalID
func getProposalVoteMap(k keeper.Keeper, ctx sdk.Context) map[uint64]([]types.Vote) {
proposalVoteMap = map[uint64]([]types.Vote){}
proposalVoteMap := map[uint64]([]types.Vote){}
keeper.IterateProposals(suite.ctx, func(p types.Proposal) bool {
keeper.IterateVotes(suite.ctx, p.ID, func(v types.Vote) bool {
k.IterateProposals(ctx, func(p types.Proposal) bool {
k.IterateVotes(ctx, p.ID, func(v types.Vote) bool {
proposalVoteMap[p.ID] = append(proposalVoteMap[p.ID], v)
return false
})

View File

@ -42,7 +42,7 @@ func (suite *KeeperTestSuite) TestGetSetDeleteCommittee() {
Members: suite.addresses,
Permissions: []types.Permission{types.GodPermission{}},
VoteThreshold: d("0.667"),
MaxProposalDuration: time.Hour * 24 * 7,
ProposalDuration: time.Hour * 24 * 7,
}
// write and read from store

View File

@ -30,7 +30,7 @@ func (k Keeper) SubmitProposal(ctx sdk.Context, proposer sdk.AccAddress, committ
}
// Get a new ID and store the proposal
deadline := ctx.BlockTime().Add(com.MaxProposalDuration)
deadline := ctx.BlockTime().Add(com.ProposalDuration)
proposalID, err := k.StoreNewProposal(ctx, pubProposal, committeeID, deadline)
if err != nil {
return 0, err

View File

@ -22,7 +22,7 @@ func (suite *KeeperTestSuite) TestSubmitProposal() {
Members: suite.addresses[:2],
Permissions: []types.Permission{types.GodPermission{}},
VoteThreshold: d("0.667"),
MaxProposalDuration: time.Hour * 24 * 7,
ProposalDuration: time.Hour * 24 * 7,
}
noPermissionsCom := normalCom
noPermissionsCom.Permissions = []types.Permission{}
@ -96,7 +96,7 @@ func (suite *KeeperTestSuite) TestSubmitProposal() {
pr, found := keeper.GetProposal(ctx, id)
suite.True(found)
suite.Equal(tc.committeeID, pr.CommitteeID)
suite.Equal(ctx.BlockTime().Add(tc.committee.MaxProposalDuration), pr.Deadline)
suite.Equal(ctx.BlockTime().Add(tc.committee.ProposalDuration), pr.Deadline)
} else {
suite.NotNil(err)
}
@ -141,7 +141,7 @@ func (suite *KeeperTestSuite) TestAddVote() {
name: "proposal expired",
proposalID: types.DefaultNextProposalID,
voter: normalCom.Members[0],
voteTime: firstBlockTime.Add(normalCom.MaxProposalDuration),
voteTime: firstBlockTime.Add(normalCom.ProposalDuration),
expectPass: false,
},
}
@ -180,7 +180,7 @@ func (suite *KeeperTestSuite) TestGetProposalResult() {
Members: suite.addresses[:5],
Permissions: []types.Permission{types.GodPermission{}},
VoteThreshold: d("0.667"),
MaxProposalDuration: time.Hour * 24 * 7,
ProposalDuration: time.Hour * 24 * 7,
}
var defaultID uint64 = 1
firstBlockTime := time.Date(1998, time.January, 1, 1, 0, 0, 0, time.UTC)
@ -327,7 +327,7 @@ func (suite *KeeperTestSuite) TestCloseExpiredProposals() {
// Setup test state
firstBlockTime := time.Date(1998, time.January, 1, 1, 0, 0, 0, time.UTC)
testGenesis = types.NewGenesisState(
testGenesis := types.NewGenesisState(
3,
[]types.Committee{
{
@ -336,14 +336,14 @@ func (suite *KeeperTestSuite) TestCloseExpiredProposals() {
Members: suite.addresses[:3],
Permissions: []types.Permission{types.GodPermission{}},
VoteThreshold: d("0.667"),
MaxProposalDuration: time.Hour * 24 * 7,
ProposalDuration: time.Hour * 24 * 7,
},
{
ID: 2,
Members: suite.addresses[2:],
Permissions: nil,
VoteThreshold: d("0.667"),
MaxProposalDuration: time.Hour * 24 * 7,
ProposalDuration: time.Hour * 24 * 7,
},
},
[]types.Proposal{
@ -367,16 +367,16 @@ func (suite *KeeperTestSuite) TestCloseExpiredProposals() {
},
)
suite.app.InitializeFromGenesisStates(
NewCommitteeGenesisState(suite.cdc, testGenesis),
NewCommitteeGenesisState(suite.app.Codec(), testGenesis),
)
// close proposals
ctx := tApp.NewContext(true, abci.Header{Height: 1, Time: firstBlockTime})
ctx := suite.app.NewContext(true, abci.Header{Height: 1, Time: firstBlockTime})
suite.keeper.CloseExpiredProposals(ctx)
// check
for _, p := range testGenesis.Proposals {
_, found := k.GetProposal(ctx, p.ID)
_, found := suite.keeper.GetProposal(ctx, p.ID)
votes := getProposalVoteMap(suite.keeper, ctx)
if ctx.BlockTime().After(p.Deadline) {
@ -389,15 +389,15 @@ func (suite *KeeperTestSuite) TestCloseExpiredProposals() {
}
// close (later time)
ctx := tApp.NewContext(true, abci.Header{Height: 1, Time: firstBlockTime.Add(7 * 24 * time.Hour)})
ctx = suite.app.NewContext(true, abci.Header{Height: 1, Time: firstBlockTime.Add(7 * 24 * time.Hour)})
suite.keeper.CloseExpiredProposals(ctx)
// check
for _, p := range testGenesis.Proposals {
_, found := k.GetProposal(ctx, p.ID)
_, found := suite.keeper.GetProposal(ctx, p.ID)
votes := getProposalVoteMap(suite.keeper, ctx)
if ctx.BlockTime().After(p.Deadline) {
if ctx.BlockTime().Equal(p.Deadline) || ctx.BlockTime().After(p.Deadline) {
suite.False(found)
suite.Empty(votes[p.ID])
} else {

View File

@ -60,14 +60,14 @@ func (suite *QuerierTestSuite) SetupTest() {
Members: suite.addresses[:3],
Permissions: []types.Permission{types.GodPermission{}},
VoteThreshold: d("0.667"),
MaxProposalDuration: time.Hour * 24 * 7,
ProposalDuration: time.Hour * 24 * 7,
},
{
ID: 2,
Members: suite.addresses[2:],
Permissions: nil,
VoteThreshold: d("0.667"),
MaxProposalDuration: time.Hour * 24 * 7,
ProposalDuration: time.Hour * 24 * 7,
},
},
[]types.Proposal{

View File

@ -43,14 +43,14 @@ func (suite *ProposalHandlerTestSuite) SetupTest() {
Members: suite.addresses[:3],
Permissions: []types.Permission{types.GodPermission{}},
VoteThreshold: d("0.667"),
MaxProposalDuration: time.Hour * 24 * 7,
ProposalDuration: time.Hour * 24 * 7,
},
{
ID: 2,
Members: suite.addresses[2:],
Permissions: nil,
VoteThreshold: d("0.667"),
MaxProposalDuration: time.Hour * 24 * 7,
ProposalDuration: time.Hour * 24 * 7,
},
},
[]committee.Proposal{
@ -77,7 +77,7 @@ func (suite *ProposalHandlerTestSuite) TestProposalHandler_ChangeCommittee() {
ID: 34,
Members: suite.addresses[:1],
VoteThreshold: d("1"),
MaxProposalDuration: time.Hour * 24,
ProposalDuration: time.Hour * 24,
},
),
expectPass: true,
@ -92,7 +92,7 @@ func (suite *ProposalHandlerTestSuite) TestProposalHandler_ChangeCommittee() {
Members: suite.addresses, // add new members
Permissions: suite.testGenesis.Committees[0].Permissions,
VoteThreshold: suite.testGenesis.Committees[0].VoteThreshold,
MaxProposalDuration: suite.testGenesis.Committees[0].MaxProposalDuration,
ProposalDuration: suite.testGenesis.Committees[0].ProposalDuration,
},
),
expectPass: true,
@ -116,7 +116,7 @@ func (suite *ProposalHandlerTestSuite) TestProposalHandler_ChangeCommittee() {
Members: append(suite.addresses, suite.addresses[0]), // duplicate address
Permissions: suite.testGenesis.Committees[0].Permissions,
VoteThreshold: suite.testGenesis.Committees[0].VoteThreshold,
MaxProposalDuration: suite.testGenesis.Committees[0].MaxProposalDuration,
ProposalDuration: suite.testGenesis.Committees[0].ProposalDuration,
},
),
expectPass: false,

View File

@ -30,7 +30,7 @@ func TestGenesisState_Validate(t *testing.T) {
Members: addresses[:3],
Permissions: []Permission{GodPermission{}},
VoteThreshold: d("0.667"),
MaxProposalDuration: time.Hour * 24 * 7,
ProposalDuration: time.Hour * 24 * 7,
},
{
ID: 2,
@ -38,7 +38,7 @@ func TestGenesisState_Validate(t *testing.T) {
Members: addresses[2:],
Permissions: nil,
VoteThreshold: d("0.8"),
MaxProposalDuration: time.Hour * 24 * 21,
ProposalDuration: time.Hour * 24 * 21,
},
},
Proposals: []Proposal{

View File

@ -22,7 +22,7 @@ type Committee struct {
Members []sdk.AccAddress `json:"members" yaml:"members"`
Permissions []Permission `json:"permissions" yaml:"permissions"`
VoteThreshold sdk.Dec `json:"vote_threshold" yaml:"vote_threshold"`
MaxProposalDuration time.Duration `json:"max_proposal_duration" yaml:"max_proposal_duration"`
ProposalDuration time.Duration `json:"proposal_duration" yaml:"proposal_duration"`
}
func NewCommittee(id uint64, description string, members []sdk.AccAddress, permissions []Permission, threshold sdk.Dec, duration time.Duration) Committee {
@ -32,7 +32,7 @@ func NewCommittee(id uint64, description string, members []sdk.AccAddress, permi
Members: members,
Permissions: permissions,
VoteThreshold: threshold,
MaxProposalDuration: duration,
ProposalDuration: duration,
}
}
@ -84,7 +84,7 @@ func (c Committee) Validate() error {
return fmt.Errorf("invalid threshold")
}
if c.MaxProposalDuration < 0 {
if c.ProposalDuration < 0 {
return fmt.Errorf("invalid time")
}
@ -110,6 +110,15 @@ type Proposal struct {
Deadline time.Time `json:"deadline" yaml:"deadline"`
}
func NewProposal(pubProposal PubProposal, id uint64, committeeID uint64, deadline time.Time) Proposal {
return Proposal{
PubProposal: pubProposal,
ID: id,
CommitteeID: committeeID,
Deadline: deadline,
}
}
// HasExpiredBy calculates if the proposal will have expired by a certain time.
// All votes must be cast before deadline, those cast at time == deadline are not valid
func (p Proposal) HasExpiredBy(time time.Time) bool {