Skip to content

Commit

Permalink
fix: add x/gov module proposal status change from deposit to voting (f…
Browse files Browse the repository at this point in the history
…orbole#681)

Currently the bdjuno doesn't cover the proposal status change from the
deposit to voting. If a user creates a proposal with a minimum deposit
the proposal has the status `deposit`. Later if a user adds more deposit
to activate voting, the bdjuno doesn't change the proposals's state, and
it remains `deposit`, however, the correct state is voting. The PR
contains changes to update the state of the proposal if the block tx
events contain proper events.

Cosmos SDK ref:
https://github.com/cosmos/cosmos-sdk/blob/release/v0.47.x/x/gov/keeper/msg_server.go#L182

Resolves: forbole#680

<!-- Add a description of the changes that this PR introduces and the
files that
are the most critical to review. -->

---

*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
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch
- [x] provided a link to the relevant issue or specification
- [x] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go
code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] confirmed all CI checks have passed

*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)

---------

Co-authored-by: Magic Cat <37407870+MonikaCat@users.noreply.github.com>
  • Loading branch information
dzmitryhil and MonikaCat committed Feb 5, 2024
1 parent 316267d commit a79b4da
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
- ([\#489](https://github.com/forbole/bdjuno/pull/489)) Remove block height foreign key from proposal_vote and proposal_deposit tables and add column timestamp
- ([\#499](https://github.com/forbole/bdjuno/pull/499)) Check if proposal has passed voting end time before marking it invalid
- ([\#523](https://github.com/forbole/bdjuno/pull/523)) Update proposal snapshots handling on block

- ([\#681](https://github.com/forbole/bdjuno/pull/681)) Handle proposal status change from deposit to voting
-
#### Daily refetch
- ([\#454](https://github.com/forbole/bdjuno/pull/454)) Added `daily refetch` module to refetch missing blocks every day

Expand Down
66 changes: 57 additions & 9 deletions modules/gov/handle_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,48 @@ package gov

import (
"fmt"
"strconv"
"time"

govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
juno "github.com/forbole/juno/v4/types"

tmctypes "github.com/tendermint/tendermint/rpc/core/types"

"github.com/rs/zerolog/log"
abci "github.com/tendermint/tendermint/abci/types"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"
)

// HandleBlock implements modules.BlockModule
func (m *Module) HandleBlock(
b *tmctypes.ResultBlock, _ *tmctypes.ResultBlockResults, _ []*juno.Tx, vals *tmctypes.ResultValidators,
b *tmctypes.ResultBlock, blockResults *tmctypes.ResultBlockResults, txs []*juno.Tx, vals *tmctypes.ResultValidators,
) error {
err := m.updateProposals(b.Block.Height, b.Block.Time, vals)
txEvents := collectTxEvents(txs)
err := m.updateProposalsStatus(b.Block.Height, b.Block.Time, txEvents, blockResults.EndBlockEvents, vals)
if err != nil {
log.Error().Str("module", "gov").Int64("height", b.Block.Height).
Err(err).Msg("error while updating proposals")
}
return nil
}

// updateProposals updates the proposals
func (m *Module) updateProposals(height int64, blockTime time.Time, blockVals *tmctypes.ResultValidators) error {
ids, err := m.db.GetOpenProposalsIds(blockTime)
// updateProposalsStatus updates the status of proposals if they have been included in the EndBlockEvents or status
// was changed from deposit to voting
func (m *Module) updateProposalsStatus(height int64, blockTime time.Time, txEvents, endBlockEvents []abci.Event, blockVals *tmctypes.ResultValidators) error {
var ids []uint64
// check if EndBlockEvents contains active_proposal event
endBlockIDs, err := findProposalIDsInEvents(endBlockEvents, govtypes.EventTypeActiveProposal, govtypes.AttributeKeyProposalID)
if err != nil {
log.Error().Err(err).Str("module", "gov").Msg("error while getting open ids")
return err
}
ids = append(ids, endBlockIDs...)

// the proposal changes state from the deposit to voting
txIDs, err := findProposalIDsInEvents(txEvents, govtypes.EventTypeProposalDeposit, govtypes.AttributeKeyVotingPeriodStart)
if err != nil {
return err
}
ids = append(ids, txIDs...)

// update status for proposals IDs stored in ids array
for _, id := range ids {
err = m.UpdateProposal(height, blockTime, id)
if err != nil {
Expand All @@ -48,3 +62,37 @@ func (m *Module) updateProposals(height int64, blockTime time.Time, blockVals *t
}
return nil
}

func findProposalIDsInEvents(events []abci.Event, eventType, attrKey string) ([]uint64, error) {
ids := make([]uint64, 0)
for _, event := range events {
if event.Type != eventType {
continue
}
for _, attr := range event.Attributes {
if string(attr.Key) != attrKey {
continue
}
// parse proposal ID from []byte to unit64
id, err := strconv.ParseUint(string(attr.Value), 10, 64)
if err != nil {
return nil, fmt.Errorf("error while parsing proposal id: %s", err)
}
// add proposal ID to ids array
ids = append(ids, id)
}
}

return ids, nil
}

func collectTxEvents(txs []*juno.Tx) []abci.Event {
events := make([]abci.Event, 0)
for _, tx := range txs {
for _, ev := range tx.Events {
events = append(events, abci.Event{Type: ev.Type, Attributes: ev.Attributes})
}
}

return events
}

0 comments on commit a79b4da

Please sign in to comment.