Skip to content

Commit

Permalink
feat: WeightedVote support
Browse files Browse the repository at this point in the history
  • Loading branch information
clockworkgr committed Mar 6, 2024
1 parent 30b9b57 commit dda0c20
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 72 deletions.
123 changes: 61 additions & 62 deletions database/gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

"github.com/forbole/bdjuno/v4/types"

sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/atomone-hub/govgen/x/gov/types"
sdk "github.com/cosmos/cosmos-sdk/types"

dbtypes "github.com/forbole/bdjuno/v4/database/types"
)
Expand Down Expand Up @@ -84,10 +84,10 @@ func (suite *DbTestSuite) getProposalRow(id int) types.Proposal {
proposalType,
govtypes.NewTextProposal(title, description),
govtypes.StatusPassed.String(),
time.Date(2020, 1, 1, 00, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 01, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 02, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 03, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 0o0, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o1, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o2, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o3, 0o0, 0o0, 0o00, time.UTC),
proposer.String(),
)

Expand Down Expand Up @@ -123,10 +123,10 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposals() {
"proposalType",
content1,
govtypes.StatusDepositPeriod.String(),
time.Date(2020, 1, 1, 00, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 01, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 02, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 03, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 0o0, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o1, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o2, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o3, 0o0, 0o0, 0o00, time.UTC),
proposer1.String(),
),
types.NewProposal(
Expand All @@ -135,10 +135,10 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposals() {
"proposalType1",
content2,
govtypes.StatusPassed.String(),
time.Date(2020, 1, 2, 00, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 01, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 02, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 03, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 0o0, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 2, 0o1, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 2, 0o2, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 2, 0o3, 0o0, 0o0, 0o00, time.UTC),
proposer2.String(),
),
}
Expand All @@ -158,10 +158,10 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposals() {
"title",
"description",
suite.encodeProposalContent(content1),
time.Date(2020, 1, 1, 00, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 01, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 02, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 03, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 0o0, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o1, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o2, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o3, 0o0, 0o0, 0o00, time.UTC),
proposer1.String(),
govtypes.StatusDepositPeriod.String(),
),
Expand All @@ -172,10 +172,10 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposals() {
"title1",
"description1",
suite.encodeProposalContent(content2),
time.Date(2020, 1, 2, 00, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 01, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 02, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 03, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 0o0, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 2, 0o1, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 2, 0o2, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 2, 0o3, 0o0, 0o0, 0o00, time.UTC),
proposer2.String(),
govtypes.StatusPassed.String(),
),
Expand All @@ -194,10 +194,10 @@ func (suite *DbTestSuite) TestBigDipperDb_GetProposal() {
"proposalType",
content,
govtypes.StatusDepositPeriod.String(),
time.Date(2020, 1, 1, 00, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 01, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 02, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 03, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 0o0, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o1, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o2, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o3, 0o0, 0o0, 0o00, time.UTC),
proposer.String(),
)
input := []types.Proposal{proposal}
Expand All @@ -223,10 +223,10 @@ func (suite *DbTestSuite) TestBigDipperDb_GetOpenProposalsIds() {
"proposalType1",
content2,
types.ProposalStatusInvalid,
time.Date(2020, 1, 2, 00, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 01, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 02, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 03, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 0o0, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 2, 0o1, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 2, 0o2, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 2, 0o3, 0o0, 0o0, 0o00, time.UTC),
proposer2.String(),
)

Expand All @@ -237,10 +237,10 @@ func (suite *DbTestSuite) TestBigDipperDb_GetOpenProposalsIds() {
"proposalType",
content1,
govtypes.StatusVotingPeriod.String(),
time.Date(2020, 1, 1, 00, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 01, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 02, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 03, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 0o0, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o1, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o2, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o3, 0o0, 0o0, 0o00, time.UTC),
proposer1.String(),
),
types.NewProposal(
Expand All @@ -249,10 +249,10 @@ func (suite *DbTestSuite) TestBigDipperDb_GetOpenProposalsIds() {
"proposalType",
content1,
govtypes.StatusDepositPeriod.String(),
time.Date(2020, 1, 1, 00, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 01, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 02, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 03, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 0o0, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o1, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o2, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o3, 0o0, 0o0, 0o00, time.UTC),
proposer1.String(),
),
types.NewProposal(
Expand All @@ -261,10 +261,10 @@ func (suite *DbTestSuite) TestBigDipperDb_GetOpenProposalsIds() {
"proposalType1",
content2,
govtypes.StatusPassed.String(),
time.Date(2020, 1, 2, 00, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 01, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 02, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 03, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 0o0, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 2, 0o1, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 2, 0o2, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 2, 0o3, 0o0, 0o0, 0o00, time.UTC),
proposer2.String(),
),
types.NewProposal(
Expand All @@ -273,10 +273,10 @@ func (suite *DbTestSuite) TestBigDipperDb_GetOpenProposalsIds() {
"proposalType1",
content2,
govtypes.StatusRejected.String(),
time.Date(2020, 1, 2, 00, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 01, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 02, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 03, 00, 00, 000, time.UTC),
time.Date(2020, 1, 2, 0o0, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 2, 0o1, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 2, 0o2, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 2, 0o3, 0o0, 0o0, 0o00, time.UTC),
proposer2.String(),
),
invalidProposal,
Expand All @@ -299,8 +299,8 @@ func (suite *DbTestSuite) TestBigDipperDb_UpdateProposal() {
update := types.NewProposalUpdate(
proposal.ProposalID,
govtypes.StatusPassed.String(),
time.Date(2020, 1, 1, 00, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 01, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 0o0, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o1, 0o0, 0o0, 0o00, time.UTC),
)

err = suite.database.UpdateProposal(update)
Expand All @@ -315,8 +315,8 @@ func (suite *DbTestSuite) TestBigDipperDb_UpdateProposal() {
suite.encodeProposalContent(proposal.Content),
proposal.SubmitTime,
proposal.DepositEndTime,
time.Date(2020, 1, 1, 00, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 01, 00, 00, 000, time.UTC),
time.Date(2020, 1, 1, 0o0, 0o0, 0o0, 0o00, time.UTC),
time.Date(2020, 1, 1, 0o1, 0o0, 0o0, 0o00, time.UTC),
proposer.String(),
govtypes.StatusPassed.String(),
)
Expand Down Expand Up @@ -348,9 +348,9 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveDeposits() {
depositor3 := suite.getAccount("cosmos1gyds87lg3m52hex9yqta2mtwzw89pfukx3jl7g")
amount3 := sdk.NewCoins(sdk.NewCoin("desmos", sdk.NewInt(50000)))

timestamp1 := time.Date(2020, 1, 1, 15, 00, 00, 000, time.UTC)
timestamp2 := time.Date(2020, 1, 1, 16, 00, 00, 000, time.UTC)
timestamp3 := time.Date(2020, 1, 1, 17, 00, 00, 000, time.UTC)
timestamp1 := time.Date(2020, 1, 1, 15, 0o0, 0o0, 0o00, time.UTC)
timestamp2 := time.Date(2020, 1, 1, 16, 0o0, 0o0, 0o00, time.UTC)
timestamp3 := time.Date(2020, 1, 1, 17, 0o0, 0o0, 0o00, time.UTC)

deposit := []types.Deposit{
types.NewDeposit(proposal.ProposalID, depositor.String(), amount, timestamp1, 10),
Expand Down Expand Up @@ -414,9 +414,9 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveVote() {
proposal := suite.getProposalRow(1)
voter := suite.getAccount("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs")

timestamp := time.Date(2020, 1, 1, 15, 00, 00, 000, time.UTC)
timestamp := time.Date(2020, 1, 1, 15, 0o0, 0o0, 0o00, time.UTC)

vote := types.NewVote(1, voter.String(), govtypes.OptionYes, timestamp, 1)
vote := types.NewVote(1, voter.String(), govtypes.OptionYes, "1.0", timestamp, 1)
err := suite.database.SaveVote(vote)
suite.Require().NoError(err)

Expand All @@ -429,7 +429,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveVote() {
suite.Require().True(expected.Equals(result[0]))

// Update with lower height should not change option
vote = types.NewVote(1, voter.String(), govtypes.OptionNo, timestamp, 0)
vote = types.NewVote(1, voter.String(), govtypes.OptionNo, "1.0", timestamp, 0)
err = suite.database.SaveVote(vote)
suite.Require().NoError(err)

Expand All @@ -440,7 +440,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveVote() {
suite.Require().True(expected.Equals(result[0]))

// Update with same height should change option
vote = types.NewVote(1, voter.String(), govtypes.OptionAbstain, timestamp, 1)
vote = types.NewVote(1, voter.String(), govtypes.OptionAbstain, "1.0", timestamp, 1)
err = suite.database.SaveVote(vote)
suite.Require().NoError(err)

Expand All @@ -453,7 +453,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveVote() {
suite.Require().True(expected.Equals(result[0]))

// Update with higher height should change option
vote = types.NewVote(1, voter.String(), govtypes.OptionNoWithVeto, timestamp, 2)
vote = types.NewVote(1, voter.String(), govtypes.OptionNoWithVeto, "1.0", timestamp, 2)
err = suite.database.SaveVote(vote)
suite.Require().NoError(err)

Expand Down Expand Up @@ -631,7 +631,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalValidatorsStatusesSnapshot
// ----------------------------------------------------------------------------------------------------------------
// Save snapshots

var snapshots = []types.ProposalValidatorStatusSnapshot{
snapshots := []types.ProposalValidatorStatusSnapshot{
types.NewProposalValidatorStatusSnapshot(
1,
validator1.GetConsAddr(),
Expand Down Expand Up @@ -830,7 +830,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveSoftwareUpgradePlan() {

// ----------------------------------------------------------------------------------------------------------------
// Save software upgrade plan at height 10 with upgrade height at 100
var plan = upgradetypes.Plan{
plan := upgradetypes.Plan{
Name: "name",
Height: 100,
Info: "info",
Expand Down Expand Up @@ -909,7 +909,7 @@ func (suite *DbTestSuite) TestBigDipperDb_DeleteSoftwareUpgradePlan() {
_ = suite.getProposalRow(1)

// Save software upgrade plan at height 10 with upgrade height at 100
var plan = upgradetypes.Plan{
plan := upgradetypes.Plan{
Name: "name",
Height: 100,
Info: "info",
Expand All @@ -926,14 +926,13 @@ func (suite *DbTestSuite) TestBigDipperDb_DeleteSoftwareUpgradePlan() {
err = suite.database.Sqlx.Select(&rows, `SELECT * FROM software_upgrade_plan`)
suite.Require().NoError(err)
suite.Require().Len(rows, 0)

}

func (suite *DbTestSuite) TestBigDipperDb_CheckSoftwareUpgradePlan() {
_ = suite.getProposalRow(1)

// Save software upgrade plan at height 10 with upgrade height at 100
var plan = upgradetypes.Plan{
plan := upgradetypes.Plan{
Name: "name",
// the Height here is the upgrade height
Height: 100,
Expand Down
20 changes: 19 additions & 1 deletion modules/gov/handle_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,25 @@ func (m *Module) handleMsgVote(tx *juno.Tx, msg *govtypes.MsgVote) error {
return fmt.Errorf("error while parsing time: %s", err)
}

vote := types.NewVote(msg.ProposalId, msg.Voter, msg.Option, txTimestamp, tx.Height)
vote := types.NewVote(msg.ProposalId, msg.Voter, msg.Option, "1.0", txTimestamp, tx.Height)

return m.db.SaveVote(vote)
}

func (m *Module) handleMsgVoteWeighted(tx *juno.Tx, msg *govtypes.MsgVoteWeighted) error {
txTimestamp, err := time.Parse(time.RFC3339, tx.Timestamp)
if err != nil {
return fmt.Errorf("error while parsing time: %s", err)
}

for _, option := range msg.Options {
vote := types.NewVote(msg.ProposalId, msg.Voter, option.Option, option.Weight.String(), txTimestamp, tx.Height)
err = m.db.SaveVote(vote)
if err != nil {
return fmt.Errorf("error while saving weighted vote for address %s: %s", msg.Voter, err)
}
}

// update tally result for given proposal
return m.updateProposalTallyResult(msg.ProposalId, tx.Height)
}
13 changes: 4 additions & 9 deletions modules/gov/utils_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (m *Module) UpdateProposal(height int64, blockTime time.Time, id uint64) er
return fmt.Errorf("error while updating proposal status: %s", err)
}

err = m.updateProposalTallyResult(proposal)
err = m.updateProposalTallyResult(proposal.ProposalId, height)
if err != nil {
return fmt.Errorf("error while updating proposal tally result: %s", err)
}
Expand Down Expand Up @@ -150,20 +150,15 @@ func (m *Module) updateProposalStatus(proposal govtypes.Proposal) error {
}

// updateProposalTallyResult updates the tally result associated with the given proposal
func (m *Module) updateProposalTallyResult(proposal govtypes.Proposal) error {
height, err := m.db.GetLastBlockHeight()
if err != nil {
return err
}

result, err := m.source.TallyResult(height, proposal.ProposalId)
func (m *Module) updateProposalTallyResult(proposalId uint64, height int64) error {
result, err := m.source.TallyResult(height, proposalId)
if err != nil {
return fmt.Errorf("error while getting tally result: %s", err)
}

return m.db.SaveTallyResults([]types.TallyResult{
types.NewTallyResult(
proposal.ProposalId,
proposalId,
result.Yes.String(),
result.Abstain.String(),
result.No.String(),
Expand Down
3 changes: 3 additions & 0 deletions types/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ type Vote struct {
ProposalID uint64
Voter string
Option govtypes.VoteOption
Weight string
Timestamp time.Time
Height int64
}
Expand All @@ -203,13 +204,15 @@ func NewVote(
proposalID uint64,
voter string,
option govtypes.VoteOption,
weight string,
timestamp time.Time,
height int64,
) Vote {
return Vote{
ProposalID: proposalID,
Voter: voter,
Option: option,
Weight: weight,
Timestamp: timestamp,
Height: height,
}
Expand Down

0 comments on commit dda0c20

Please sign in to comment.