Skip to content

Commit

Permalink
fix: update proposal to latest status in parse proposal cmd (#401)
Browse files Browse the repository at this point in the history
## Description

Closes: #XXXX



Currently proposal status is not updated with `bdjuno parse gov proposal [id]`

---

### 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 Jun 22, 2022
1 parent e13edfe commit c3d890b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## Unreleased
### Changes
#### Gov module
- ([\#401](https://github.com/forbole/bdjuno/pull/401)) Update the proposal status to the latest in `bdjuno parse gov proposal [id]` command

### Dependencies
- ([\#412](https://github.com/forbole/bdjuno/pull/412)) Updated Juno to `v3.2.1`

Expand Down
48 changes: 40 additions & 8 deletions cmd/parse/gov/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package gov
import (
"encoding/hex"
"fmt"
"strconv"

modulestypes "github.com/forbole/bdjuno/v3/modules/types"
"github.com/rs/zerolog/log"

govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
parsecmdtypes "github.com/forbole/juno/v3/cmd/parse/types"
Expand All @@ -14,7 +16,11 @@ import (
"github.com/forbole/juno/v3/parser"

"github.com/forbole/bdjuno/v3/database"
"github.com/forbole/bdjuno/v3/modules/distribution"
"github.com/forbole/bdjuno/v3/modules/gov"
"github.com/forbole/bdjuno/v3/modules/mint"
"github.com/forbole/bdjuno/v3/modules/slashing"
"github.com/forbole/bdjuno/v3/modules/staking"
"github.com/forbole/bdjuno/v3/utils"
)

Expand All @@ -24,7 +30,10 @@ func proposalCmd(parseConfig *parsecmdtypes.Config) *cobra.Command {
Use: "proposal [id]",
Short: "Get the description, votes and everything related to a proposal given its id",
RunE: func(cmd *cobra.Command, args []string) error {
proposalID := args[0]
proposalID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return err
}

parseCtx, err := parsecmdtypes.GetParserContext(config.Cfg, parseConfig)
if err != nil {
Expand All @@ -39,8 +48,14 @@ func proposalCmd(parseConfig *parsecmdtypes.Config) *cobra.Command {
// Get the database
db := database.Cast(parseCtx.Database)

// Build expected modules of gov modules for handleParamChangeProposal
distrModule := distribution.NewModule(sources.DistrSource, parseCtx.EncodingConfig.Marshaler, db)
mintModule := mint.NewModule(sources.MintSource, parseCtx.EncodingConfig.Marshaler, db)
slashingModule := slashing.NewModule(sources.SlashingSource, parseCtx.EncodingConfig.Marshaler, db)
stakingModule := staking.NewModule(sources.StakingSource, slashingModule, parseCtx.EncodingConfig.Marshaler, db)

// Build the gov module
govModule := gov.NewModule(sources.GovSource, nil, nil, nil, nil, nil, parseCtx.EncodingConfig.Marshaler, db)
govModule := gov.NewModule(sources.GovSource, nil, distrModule, mintModule, slashingModule, stakingModule, parseCtx.EncodingConfig.Marshaler, db)

err = refreshProposalDetails(parseCtx, proposalID, govModule)
if err != nil {
Expand All @@ -57,14 +72,27 @@ func proposalCmd(parseConfig *parsecmdtypes.Config) *cobra.Command {
return err
}

// Update the proposal to the latest status
height, err := parseCtx.Node.LatestHeight()
if err != nil {
return fmt.Errorf("error while getting chain latest block height: %s", err)
}

err = govModule.UpdateProposal(height, proposalID)
if err != nil {
return err
}

return nil
},
}
}

func refreshProposalDetails(parseCtx *parser.Context, proposalID string, govModule *gov.Module) error {
func refreshProposalDetails(parseCtx *parser.Context, proposalID uint64, govModule *gov.Module) error {
log.Debug().Msg("refreshing proposal details")

// Get the tx that created the proposal
txs, err := utils.QueryTxs(parseCtx.Node, fmt.Sprintf("submit_proposal.proposal_id=%s", proposalID))
txs, err := utils.QueryTxs(parseCtx.Node, fmt.Sprintf("submit_proposal.proposal_id=%d", proposalID))
if err != nil {
return err
}
Expand Down Expand Up @@ -94,9 +122,11 @@ func refreshProposalDetails(parseCtx *parser.Context, proposalID string, govModu
return nil
}

func refreshProposalDeposits(parseCtx *parser.Context, proposalID string, govModule *gov.Module) error {
func refreshProposalDeposits(parseCtx *parser.Context, proposalID uint64, govModule *gov.Module) error {
log.Debug().Msg("refreshing proposal deposits")

// Get the tx that deposited to the proposal
txs, err := utils.QueryTxs(parseCtx.Node, fmt.Sprintf("proposal_deposit.proposal_id=%s", proposalID))
txs, err := utils.QueryTxs(parseCtx.Node, fmt.Sprintf("proposal_deposit.proposal_id=%d", proposalID))
if err != nil {
return err
}
Expand Down Expand Up @@ -124,9 +154,11 @@ func refreshProposalDeposits(parseCtx *parser.Context, proposalID string, govMod
return nil
}

func refreshProposalVotes(parseCtx *parser.Context, proposalID string, govModule *gov.Module) error {
func refreshProposalVotes(parseCtx *parser.Context, proposalID uint64, govModule *gov.Module) error {
log.Debug().Msg("refreshing proposal votes")

// Get the tx that voted the proposal
txs, err := utils.QueryTxs(parseCtx.Node, fmt.Sprintf("proposal_vote.proposal_id=%s", proposalID))
txs, err := utils.QueryTxs(parseCtx.Node, fmt.Sprintf("proposal_vote.proposal_id=%d", proposalID))
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion modules/gov/handle_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ func (m *Module) updateProposals(height int64, blockVals *tmctypes.ResultValidat
}

for _, id := range ids {
err = m.UpdateProposal(height, blockVals, id)
err = m.UpdateProposal(height, id)
if err != nil {
return fmt.Errorf("error while updating proposal: %s", err)
}

err = m.UpdateProposalSnapshots(height, blockVals, id)
if err != nil {
return fmt.Errorf("error while updating proposal snapshots: %s", err)
}
}
return nil
}
10 changes: 7 additions & 3 deletions modules/gov/utils_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)

func (m *Module) UpdateProposal(height int64, blockVals *tmctypes.ResultValidators, id uint64) error {
func (m *Module) UpdateProposal(height int64, id uint64) error {
// Get the proposal
proposal, err := m.source.Proposal(height, id)
if err != nil {
Expand Down Expand Up @@ -50,8 +50,11 @@ func (m *Module) UpdateProposal(height int64, blockVals *tmctypes.ResultValidato
if err != nil {
return fmt.Errorf("error while updating account: %s", err)
}
return nil
}

err = m.updateProposalStakingPoolSnapshot(height, id)
func (m *Module) UpdateProposalSnapshots(height int64, blockVals *tmctypes.ResultValidators, id uint64) error {
err := m.updateProposalStakingPoolSnapshot(height, id)
if err != nil {
return fmt.Errorf("error while updating proposal staking pool snapshot: %s", err)
}
Expand Down Expand Up @@ -84,7 +87,7 @@ func (m *Module) updateDeletedProposalStatus(id uint64) error {

// handleParamChangeProposal updates params to the corresponding modules if a ParamChangeProposal has passed
func (m *Module) handleParamChangeProposal(height int64, proposal govtypes.Proposal) error {
if proposal.Status.String() != types.ProposalStatusPassed {
if proposal.Status != govtypes.StatusPassed {
// If the status of ParamChangeProposal is not passed, do nothing
return nil
}
Expand All @@ -99,6 +102,7 @@ func (m *Module) handleParamChangeProposal(height int64, proposal govtypes.Propo
if !ok {
return nil
}

for _, change := range paramChangeProposal.Changes {
// Update the params for corresponding modules
switch change.Subspace {
Expand Down
1 change: 0 additions & 1 deletion types/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

const (
ProposalStatusInvalid = "PROPOSAL_STATUS_INVALID"
ProposalStatusPassed = "PROPOSAL_STATUS_PASSED"
)

// DepositParams contains the data of the deposit parameters of the x/gov module
Expand Down

0 comments on commit c3d890b

Please sign in to comment.