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

fix: update proposal to latest status in parse proposal cmd #401

Merged
merged 6 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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 {
huichiaotsou marked this conversation as resolved.
Show resolved Hide resolved
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we replace all the usages of this custom value with the correct govtypes.StatusXXX value as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no such status in the sdk so we created it here. It is used when the proposal is not found on chain and so we consider it deleted, also where we set PROPOSAL_STATUS_INVALID.

ProposalStatusPassed = "PROPOSAL_STATUS_PASSED"
)

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