Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove block height foreign key from propoval vote and deposit, add timestamp column #489

Merged
merged 6 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
## Unreleased
### Changes

#### Upgrade Module
- ([\#467](https://github.com/forbole/bdjuno/pull/467)) Store software upgrade plan and refresh data at upgrade height


#### Staking Module
- ([\#443](https://github.com/forbole/bdjuno/pull/443)) Remove tombstone status from staking module(already stored in slashing module)
- ([\#455](https://github.com/forbole/bdjuno/pull/455)) Added `unbonding_tokens` and `staked_not_bonded_tokens` values to staking pool table

#### Gov Module
- ([\#461](https://github.com/forbole/bdjuno/pull/461)) Parse `x/gov` genesis with `genesisDoc.InitialHeight` instead of the hard-coded height 1
- ([\#465](https://github.com/forbole/bdjuno/pull/465)) Get open proposal ids in deposit or voting period by block time instead of current time
- ([\#489](https://github.com/forbole/bdjuno/pull/489)) Remove block height foreign key from proposal_vote and proposal_deposit tables and add column timestamp

#### Daily refetch
- ([\#454](https://github.com/forbole/bdjuno/pull/454)) Added `daily refetch` module to refetch missing blocks every day
Expand Down
15 changes: 9 additions & 6 deletions database/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,22 +248,24 @@ func (db *Db) SaveDeposits(deposits []types.Deposit) error {
return nil
}

query := `INSERT INTO proposal_deposit (proposal_id, depositor_address, amount, height) VALUES `
query := `INSERT INTO proposal_deposit (proposal_id, depositor_address, amount, timestamp, height) VALUES `
var param []interface{}

for i, deposit := range deposits {
vi := i * 4
query += fmt.Sprintf("($%d,$%d,$%d,$%d),", vi+1, vi+2, vi+3, vi+4)
vi := i * 5
query += fmt.Sprintf("($%d,$%d,$%d,$%d,$%d),", vi+1, vi+2, vi+3, vi+4, vi+5)
param = append(param, deposit.ProposalID,
deposit.Depositor,
pq.Array(dbtypes.NewDbCoins(deposit.Amount)),
deposit.Timestamp,
deposit.Height,
)
}
query = query[:len(query)-1] // Remove trailing ","
query += `
ON CONFLICT ON CONSTRAINT unique_deposit DO UPDATE
SET amount = excluded.amount,
timestamp = excluded.timestamp,
height = excluded.height
WHERE proposal_deposit.height <= excluded.height`
_, err := db.Sql.Exec(query, param...)
Expand All @@ -279,10 +281,11 @@ WHERE proposal_deposit.height <= excluded.height`
// 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, height)
VALUES ($1, $2, $3, $4)
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`

Expand All @@ -292,7 +295,7 @@ WHERE proposal_vote.height <= excluded.height`
return fmt.Errorf("error while storing voter account: %s", err)
}

_, err = db.Sql.Exec(query, vote.ProposalID, vote.Voter, vote.Option.String(), vote.Height)
_, err = db.Sql.Exec(query, vote.ProposalID, vote.Voter, vote.Option.String(), vote.Timestamp, vote.Height)
if err != nil {
return fmt.Errorf("error while storing vote: %s", err)
}
Expand Down
44 changes: 25 additions & 19 deletions database/gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,19 +348,23 @@ 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)

deposit := []types.Deposit{
types.NewDeposit(proposal.ProposalID, depositor.String(), amount, 10),
types.NewDeposit(proposal.ProposalID, depositor2.String(), amount2, 10),
types.NewDeposit(proposal.ProposalID, depositor3.String(), amount3, 10),
types.NewDeposit(proposal.ProposalID, depositor.String(), amount, timestamp1, 10),
types.NewDeposit(proposal.ProposalID, depositor2.String(), amount2, timestamp2, 10),
types.NewDeposit(proposal.ProposalID, depositor3.String(), amount3, timestamp3, 10),
}

err := suite.database.SaveDeposits(deposit)
suite.Require().NoError(err)

expected := []dbtypes.DepositRow{
dbtypes.NewDepositRow(1, depositor.String(), dbtypes.NewDbCoins(amount), 10),
dbtypes.NewDepositRow(1, depositor2.String(), dbtypes.NewDbCoins(amount2), 10),
dbtypes.NewDepositRow(1, depositor3.String(), dbtypes.NewDbCoins(amount3), 10),
dbtypes.NewDepositRow(1, depositor.String(), dbtypes.NewDbCoins(amount), timestamp1, 10),
dbtypes.NewDepositRow(1, depositor2.String(), dbtypes.NewDbCoins(amount2), timestamp2, 10),
dbtypes.NewDepositRow(1, depositor3.String(), dbtypes.NewDbCoins(amount3), timestamp3, 10),
}
var result []dbtypes.DepositRow
err = suite.database.Sqlx.Select(&result, `SELECT * FROM proposal_deposit`)
Expand All @@ -377,19 +381,19 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveDeposits() {
amount3 = sdk.NewCoins(sdk.NewCoin("desmos", sdk.NewInt(30)))

deposit = []types.Deposit{
types.NewDeposit(proposal.ProposalID, depositor.String(), amount, 9),
types.NewDeposit(proposal.ProposalID, depositor2.String(), amount2, 10),
types.NewDeposit(proposal.ProposalID, depositor3.String(), amount3, 11),
types.NewDeposit(proposal.ProposalID, depositor.String(), amount, timestamp1, 9),
types.NewDeposit(proposal.ProposalID, depositor2.String(), amount2, timestamp2, 10),
types.NewDeposit(proposal.ProposalID, depositor3.String(), amount3, timestamp3, 11),
}

err = suite.database.SaveDeposits(deposit)
suite.Require().NoError(err)

expected = []dbtypes.DepositRow{
dbtypes.NewDepositRow(1, depositor.String(), dbtypes.NewDbCoins(
sdk.NewCoins(sdk.NewCoin("desmos", sdk.NewInt(10000)))), 10),
dbtypes.NewDepositRow(1, depositor2.String(), dbtypes.NewDbCoins(amount2), 10),
dbtypes.NewDepositRow(1, depositor3.String(), dbtypes.NewDbCoins(amount3), 11),
sdk.NewCoins(sdk.NewCoin("desmos", sdk.NewInt(10000)))), timestamp1, 10),
dbtypes.NewDepositRow(1, depositor2.String(), dbtypes.NewDbCoins(amount2), timestamp2, 10),
dbtypes.NewDepositRow(1, depositor3.String(), dbtypes.NewDbCoins(amount3), timestamp3, 11),
}

result = []dbtypes.DepositRow{}
Expand All @@ -410,11 +414,13 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveVote() {
proposal := suite.getProposalRow(1)
voter := suite.getAccount("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs")

vote := types.NewVote(1, voter.String(), govtypes.OptionYes, 1)
timestamp := time.Date(2020, 1, 1, 15, 00, 00, 000, time.UTC)

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

expected := dbtypes.NewVoteRow(int64(proposal.ProposalID), voter.String(), govtypes.OptionYes.String(), 1)
expected := dbtypes.NewVoteRow(int64(proposal.ProposalID), voter.String(), govtypes.OptionYes.String(), timestamp, 1)

var result []dbtypes.VoteRow
err = suite.database.Sqlx.Select(&result, `SELECT * FROM proposal_vote`)
Expand All @@ -423,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, 0)
vote = types.NewVote(1, voter.String(), govtypes.OptionNo, timestamp, 0)
err = suite.database.SaveVote(vote)
suite.Require().NoError(err)

Expand All @@ -434,11 +440,11 @@ 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, 1)
vote = types.NewVote(1, voter.String(), govtypes.OptionAbstain, timestamp, 1)
err = suite.database.SaveVote(vote)
suite.Require().NoError(err)

expected = dbtypes.NewVoteRow(int64(proposal.ProposalID), voter.String(), govtypes.OptionAbstain.String(), 1)
expected = dbtypes.NewVoteRow(int64(proposal.ProposalID), voter.String(), govtypes.OptionAbstain.String(), timestamp, 1)

result = []dbtypes.VoteRow{}
err = suite.database.Sqlx.Select(&result, `SELECT * FROM proposal_vote`)
Expand All @@ -447,11 +453,11 @@ 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, 2)
vote = types.NewVote(1, voter.String(), govtypes.OptionNoWithVeto, timestamp, 2)
err = suite.database.SaveVote(vote)
suite.Require().NoError(err)

expected = dbtypes.NewVoteRow(int64(proposal.ProposalID), voter.String(), govtypes.OptionNoWithVeto.String(), 2)
expected = dbtypes.NewVoteRow(int64(proposal.ProposalID), voter.String(), govtypes.OptionNoWithVeto.String(), timestamp, 2)

result = []dbtypes.VoteRow{}
err = suite.database.Sqlx.Select(&result, `SELECT * FROM proposal_vote`)
Expand Down
6 changes: 4 additions & 2 deletions database/schema/08-gov.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ CREATE TABLE proposal_deposit
proposal_id INTEGER NOT NULL REFERENCES proposal (id),
depositor_address TEXT REFERENCES account (address),
amount COIN[],
height BIGINT NOT NULL REFERENCES block (height),
timestamp TIMESTAMP,
height BIGINT NOT NULL,
CONSTRAINT unique_deposit UNIQUE (proposal_id, depositor_address)
);
CREATE INDEX proposal_deposit_proposal_id_index ON proposal_deposit (proposal_id);
Expand All @@ -42,7 +43,8 @@ CREATE TABLE proposal_vote
proposal_id INTEGER NOT NULL REFERENCES proposal (id),
voter_address TEXT NOT NULL REFERENCES account (address),
option TEXT NOT NULL,
height BIGINT NOT NULL REFERENCES block (height),
timestamp TIMESTAMP,
height BIGINT NOT NULL,
CONSTRAINT unique_vote UNIQUE (proposal_id, voter_address)
);
CREATE INDEX proposal_vote_proposal_id_index ON proposal_vote (proposal_id);
Expand Down
24 changes: 16 additions & 8 deletions database/types/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,26 @@ func (w TallyResultRow) Equals(v TallyResultRow) bool {

// VoteRow represents a single row inside the vote table
type VoteRow struct {
ProposalID int64 `db:"proposal_id"`
Voter string `db:"voter_address"`
Option string `db:"option"`
Height int64 `db:"height"`
ProposalID int64 `db:"proposal_id"`
Voter string `db:"voter_address"`
Option string `db:"option"`
Timestamp time.Time `db:"timestamp"`
Height int64 `db:"height"`
}

// NewVoteRow allows to easily create a new VoteRow
func NewVoteRow(
proposalID int64,
voter string,
option string,
timestamp time.Time,
height int64,
) VoteRow {
return VoteRow{
ProposalID: proposalID,
Voter: voter,
Option: option,
Timestamp: timestamp,
Height: height,
}
}
Expand All @@ -144,28 +147,32 @@ func (w VoteRow) Equals(v VoteRow) bool {
return w.ProposalID == v.ProposalID &&
w.Voter == v.Voter &&
w.Option == v.Option &&
w.Timestamp.Equal(v.Timestamp) &&
w.Height == v.Height
}

// DepositRow represents a single row inside the deposit table
type DepositRow struct {
ProposalID int64 `db:"proposal_id"`
Depositor string `db:"depositor_address"`
Amount DbCoins `db:"amount"`
Height int64 `db:"height"`
ProposalID int64 `db:"proposal_id"`
Depositor string `db:"depositor_address"`
Amount DbCoins `db:"amount"`
Timestamp time.Time `db:"timestamp"`
Height int64 `db:"height"`
}

// NewDepositRow allows to easily create a new NewDepositRow
func NewDepositRow(
proposalID int64,
depositor string,
amount DbCoins,
timestamp time.Time,
height int64,
) DepositRow {
return DepositRow{
ProposalID: proposalID,
Depositor: depositor,
Amount: amount,
Timestamp: timestamp,
Height: height,
}
}
Expand All @@ -175,6 +182,7 @@ func (w DepositRow) Equals(v DepositRow) bool {
return w.ProposalID == v.ProposalID &&
w.Depositor == v.Depositor &&
w.Amount.Equal(&v.Amount) &&
w.Timestamp.Equal(v.Timestamp) &&
w.Height == v.Height
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ select_permissions:
- proposal_id
- depositor_address
- amount
- timestamp
- height
filter: {}
role: anonymous
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ object_relationships:
foreign_key_constraint_on: voter_address
- name: block
using:
foreign_key_constraint_on: height
manual_configuration:
column_mapping:
height: height
insertion_order: null
remote_table:
name: block
schema: public
- name: proposal
using:
foreign_key_constraint_on: proposal_id
Expand All @@ -18,6 +24,7 @@ select_permissions:
- proposal_id
- voter_address
- option
- timestamp
- height
filter: {}
role: anonymous
9 changes: 5 additions & 4 deletions modules/gov/handle_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (m *Module) HandleGenesis(doc *tmtypes.GenesisDoc, appState map[string]json
}

// Save the proposals
err = m.saveProposals(genState.Proposals, doc.InitialHeight)
err = m.saveProposals(genState.Proposals, doc)
if err != nil {
return fmt.Errorf("error while storing genesis governance proposals: %s", err)
}
Expand All @@ -44,7 +44,7 @@ func (m *Module) HandleGenesis(doc *tmtypes.GenesisDoc, appState map[string]json
}

// saveProposals save proposals from genesis file
func (m *Module) saveProposals(slice govtypes.Proposals, genHeight int64) error {
func (m *Module) saveProposals(slice govtypes.Proposals, genDoc *tmtypes.GenesisDoc) error {
proposals := make([]types.Proposal, len(slice))
tallyResults := make([]types.TallyResult, len(slice))
deposits := make([]types.Deposit, len(slice))
Expand All @@ -70,14 +70,15 @@ func (m *Module) saveProposals(slice govtypes.Proposals, genHeight int64) error
proposal.FinalTallyResult.Abstain.String(),
proposal.FinalTallyResult.No.String(),
proposal.FinalTallyResult.NoWithVeto.String(),
genHeight,
genDoc.InitialHeight,
)

deposits[index] = types.NewDeposit(
proposal.ProposalId,
"",
proposal.TotalDeposit,
genHeight,
genDoc.GenesisTime,
genDoc.InitialHeight,
)
}

Expand Down
23 changes: 20 additions & 3 deletions modules/gov/handle_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gov

import (
"fmt"
"time"

"strconv"

Expand Down Expand Up @@ -74,8 +75,13 @@ func (m *Module) handleMsgSubmitProposal(tx *juno.Tx, index int, msg *govtypes.M
return err
}

txTimestamp, err := time.Parse(time.RFC3339, tx.Timestamp)
if err != nil {
return fmt.Errorf("error while parsing time: %s", err)
}

// Store the deposit
deposit := types.NewDeposit(proposal.ProposalId, msg.Proposer, msg.InitialDeposit, tx.Height)
deposit := types.NewDeposit(proposal.ProposalId, msg.Proposer, msg.InitialDeposit, txTimestamp, tx.Height)
return m.db.SaveDeposits([]types.Deposit{deposit})
}

Expand All @@ -86,13 +92,24 @@ func (m *Module) handleMsgDeposit(tx *juno.Tx, msg *govtypes.MsgDeposit) error {
return fmt.Errorf("error while getting proposal deposit: %s", err)
}

txTimestamp, err := time.Parse(time.RFC3339, tx.Timestamp)
if err != nil {
return fmt.Errorf("error while parsing time: %s", err)
}

return m.db.SaveDeposits([]types.Deposit{
types.NewDeposit(msg.ProposalId, msg.Depositor, deposit.Amount, tx.Height),
types.NewDeposit(msg.ProposalId, msg.Depositor, deposit.Amount, txTimestamp, tx.Height),
})
}

// handleMsgVote allows to properly handle a handleMsgVote
func (m *Module) handleMsgVote(tx *juno.Tx, msg *govtypes.MsgVote) error {
vote := types.NewVote(msg.ProposalId, msg.Voter, msg.Option, tx.Height)
txTimestamp, err := time.Parse(time.RFC3339, tx.Timestamp)
if err != nil {
return fmt.Errorf("error while parsing time: %s", err)
}

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

return m.db.SaveVote(vote)
}
Loading