Skip to content

Commit

Permalink
fix: get open proposal ids in deposit/voting period by block time ins…
Browse files Browse the repository at this point in the history
…tead of current time (#465)

## Description

Closes: #XXXX



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch
- [ ] provided a link to the relevant issue or specification
- [x] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
huichiaotsou authored Sep 15, 2022
1 parent 48f05f0 commit 4dccf53
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### 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

#### Daily refetch
- ([\#454](https://github.com/forbole/bdjuno/pull/454)) Added `daily refetch` module to refetch missing blocks every day
Expand Down
9 changes: 5 additions & 4 deletions database/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"encoding/json"
"fmt"
"time"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
Expand Down Expand Up @@ -204,8 +205,8 @@ func (db *Db) GetProposal(id uint64) (*types.Proposal, error) {
return &proposal, nil
}

// GetOpenProposalsIds returns all the ids of the proposals that are currently in deposit or voting period
func (db *Db) GetOpenProposalsIds() ([]uint64, error) {
// GetOpenProposalsIds returns all the ids of the proposals that are in deposit or voting period at the given block time
func (db *Db) GetOpenProposalsIds(blockTime time.Time) ([]uint64, error) {
var ids []uint64
stmt := `SELECT id FROM proposal WHERE status = $1 OR status = $2`
err := db.Sqlx.Select(&ids, stmt, govtypes.StatusDepositPeriod.String(), govtypes.StatusVotingPeriod.String())
Expand All @@ -215,8 +216,8 @@ func (db *Db) GetOpenProposalsIds() ([]uint64, error) {

// Get also the invalid status proposals due to gRPC failure but still are in deposit period or voting period
var idsInvalid []uint64
stmt = `SELECT id FROM proposal WHERE status = $1 AND (voting_end_time > NOW() OR deposit_end_time > NOW())`
err = db.Sqlx.Select(&idsInvalid, stmt, types.ProposalStatusInvalid)
stmt = `SELECT id FROM proposal WHERE status = $1 AND (voting_end_time > $2 OR deposit_end_time > $2)`
err = db.Sqlx.Select(&idsInvalid, stmt, types.ProposalStatusInvalid, blockTime)
ids = append(ids, idsInvalid...)

return ids, err
Expand Down
20 changes: 18 additions & 2 deletions database/gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,20 @@ func (suite *DbTestSuite) TestBigDipperDb_GetOpenProposalsIds() {

content1 := govtypes.NewTextProposal("title", "description")
content2 := govtypes.NewTextProposal("title1", "description1")

invalidProposal := types.NewProposal(
6,
"proposalRoute1",
"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),
proposer2.String(),
)

input := []types.Proposal{
types.NewProposal(
1,
Expand Down Expand Up @@ -264,14 +278,16 @@ func (suite *DbTestSuite) TestBigDipperDb_GetOpenProposalsIds() {
time.Date(2020, 1, 2, 03, 00, 00, 000, time.UTC),
proposer2.String(),
),
invalidProposal,
}

err := suite.database.SaveProposals(input)
suite.Require().NoError(err)

ids, err := suite.database.GetOpenProposalsIds()
timeBeforeDepositEnd := invalidProposal.DepositEndTime.Add(-1 * time.Hour)
ids, err := suite.database.GetOpenProposalsIds(timeBeforeDepositEnd)
suite.Require().NoError(err)
suite.Require().Equal([]uint64{1, 2}, ids)
suite.Require().Equal([]uint64{1, 2, 6}, ids)
}

func (suite *DbTestSuite) TestBigDipperDb_UpdateProposal() {
Expand Down
7 changes: 4 additions & 3 deletions modules/gov/handle_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gov

import (
"fmt"
"time"

juno "github.com/forbole/juno/v3/types"

Expand All @@ -14,7 +15,7 @@ import (
func (m *Module) HandleBlock(
b *tmctypes.ResultBlock, _ *tmctypes.ResultBlockResults, _ []*juno.Tx, vals *tmctypes.ResultValidators,
) error {
err := m.updateProposals(b.Block.Height, vals)
err := m.updateProposals(b.Block.Height, b.Block.Time, vals)
if err != nil {
log.Error().Str("module", "gov").Int64("height", b.Block.Height).
Err(err).Msg("error while updating proposals")
Expand All @@ -23,8 +24,8 @@ func (m *Module) HandleBlock(
}

// updateProposals updates the proposals
func (m *Module) updateProposals(height int64, blockVals *tmctypes.ResultValidators) error {
ids, err := m.db.GetOpenProposalsIds()
func (m *Module) updateProposals(height int64, blockTime time.Time, blockVals *tmctypes.ResultValidators) error {
ids, err := m.db.GetOpenProposalsIds(blockTime)
if err != nil {
log.Error().Err(err).Str("module", "gov").Msg("error while getting open ids")
}
Expand Down

0 comments on commit 4dccf53

Please sign in to comment.