diff --git a/CHANGELOG.md b/CHANGELOG.md index 05447ac6d..5b8b9516a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/cmd/parse/gov/proposal.go b/cmd/parse/gov/proposal.go index 83e532411..49360d93e 100644 --- a/cmd/parse/gov/proposal.go +++ b/cmd/parse/gov/proposal.go @@ -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" @@ -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" ) @@ -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 { @@ -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 { @@ -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 } @@ -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 } @@ -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 } diff --git a/modules/gov/handle_block.go b/modules/gov/handle_block.go index fd9d865ba..d584b48a1 100644 --- a/modules/gov/handle_block.go +++ b/modules/gov/handle_block.go @@ -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 } diff --git a/modules/gov/utils_proposal.go b/modules/gov/utils_proposal.go index 80e170666..84b7f072b 100644 --- a/modules/gov/utils_proposal.go +++ b/modules/gov/utils_proposal.go @@ -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 { @@ -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) } @@ -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 } @@ -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 { diff --git a/types/gov.go b/types/gov.go index af186687f..411bb477b 100644 --- a/types/gov.go +++ b/types/gov.go @@ -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