Skip to content

Commit

Permalink
Update mainnet branch (#2)
Browse files Browse the repository at this point in the history
* feat: Keep proposal/height history

* fix: remove proposal constraints

* fix: remove constraint reference

* feat: WeightedVote support

* fix: Weighted votes

* fix: Update tally update scheduling

* feat: Add past vote invalidation

* feat: adjust metadata

* feat: add computed fields to gov sql

* updated metadata
  • Loading branch information
clockworkgr authored May 29, 2024
1 parent d2b3ade commit cd06a6f
Show file tree
Hide file tree
Showing 61 changed files with 1,157 additions and 1,066 deletions.
44 changes: 12 additions & 32 deletions database/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,25 +284,28 @@ WHERE proposal_deposit.height <= excluded.height`
}

// --------------------------------------------------------------------------------------------------------------------
func (db *Db) CleanVote(proposal_id uint64, voter string) error {
flagQuery := `UPDATE proposal_vote SET is_valid=FALSE WHERE proposal_id=$1 AND voter_address=$2`
_, err := db.SQL.Exec(flagQuery, proposal_id, voter)
if err != nil {
return fmt.Errorf("error while updating past vote: %s", err)
}
return nil
}

// SaveVote allows to save for the given height and the message vote
func (db *Db) SaveVote(vote types.Vote) error {
query := `
INSERT INTO proposal_vote (proposal_id, voter_address, option, timestamp, height)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT ON CONSTRAINT unique_vote DO UPDATE
SET option = excluded.option,
timestamp = excluded.timestamp,
height = excluded.height
WHERE proposal_vote.height <= excluded.height`
INSERT INTO proposal_vote (proposal_id, voter_address, is_valid, option, weight, timestamp, height)
VALUES ($1, $2, TRUE, $3, $4, $5, $6)`

// Store the voter account
err := db.SaveAccounts([]types.Account{types.NewAccount(vote.Voter)})
if err != nil {
return fmt.Errorf("error while storing voter account: %s", err)
}

_, err = db.SQL.Exec(query, vote.ProposalID, vote.Voter, vote.Option.String(), vote.Timestamp, vote.Height)
_, err = db.SQL.Exec(query, vote.ProposalID, vote.Voter, vote.Option.String(), vote.Weight, vote.Timestamp, vote.Height)
if err != nil {
return fmt.Errorf("error while storing vote: %s", err)
}
Expand Down Expand Up @@ -332,14 +335,6 @@ func (db *Db) SaveTallyResults(tallys []types.TallyResult) error {
}

query = query[:len(query)-1] // Remove trailing ","
query += `
ON CONFLICT ON CONSTRAINT unique_tally_result DO UPDATE
SET yes = excluded.yes,
abstain = excluded.abstain,
no = excluded.no,
no_with_veto = excluded.no_with_veto,
height = excluded.height
WHERE proposal_tally_result.height <= excluded.height`
_, err := db.SQL.Exec(query, param...)
if err != nil {
return fmt.Errorf("error while storing tally result: %s", err)
Expand All @@ -354,13 +349,7 @@ WHERE proposal_tally_result.height <= excluded.height`
func (db *Db) SaveProposalStakingPoolSnapshot(snapshot types.ProposalStakingPoolSnapshot) error {
stmt := `
INSERT INTO proposal_staking_pool_snapshot (proposal_id, bonded_tokens, not_bonded_tokens, height)
VALUES ($1, $2, $3, $4)
ON CONFLICT ON CONSTRAINT unique_staking_pool_snapshot DO UPDATE SET
proposal_id = excluded.proposal_id,
bonded_tokens = excluded.bonded_tokens,
not_bonded_tokens = excluded.not_bonded_tokens,
height = excluded.height
WHERE proposal_staking_pool_snapshot.height <= excluded.height`
VALUES ($1, $2, $3, $4)`

_, err := db.SQL.Exec(stmt,
snapshot.ProposalID, snapshot.Pool.BondedTokens.String(), snapshot.Pool.NotBondedTokens.String(), snapshot.Pool.Height)
Expand Down Expand Up @@ -392,15 +381,6 @@ VALUES `
}

stmt = stmt[:len(stmt)-1]
stmt += `
ON CONFLICT ON CONSTRAINT unique_validator_status_snapshot DO UPDATE
SET proposal_id = excluded.proposal_id,
validator_address = excluded.validator_address,
voting_power = excluded.voting_power,
status = excluded.status,
jailed = excluded.jailed,
height = excluded.height
WHERE proposal_validator_status_snapshot.height <= excluded.height`
_, err := db.SQL.Exec(stmt, args...)
if err != nil {
return fmt.Errorf("error while storing proposal validator statuses snapshot: %s", err)
Expand Down
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
Loading

0 comments on commit cd06a6f

Please sign in to comment.