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: re-add support for gov v1beta1 messages #725

Merged
merged 5 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 30 additions & 19 deletions cmd/parse/gov/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
modulestypes "github.com/forbole/callisto/v4/modules/types"

govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
parsecmdtypes "github.com/forbole/juno/v5/cmd/parse/types"
"github.com/forbole/juno/v5/parser"
"github.com/forbole/juno/v5/types/config"
Expand Down Expand Up @@ -195,29 +196,39 @@ func refreshProposalVotes(parseCtx *parser.Context, proposalID uint64, govModule

// Handle the MsgVote messages
for index, msg := range junoTx.GetMsgs() {
_, isMsgVoteV1 := msg.(*govtypesv1.MsgVote)
_, isMsgVoteV1Beta1 := msg.(*govtypesv1beta1.MsgVote)
_, isMsgVoteWeightedV1 := msg.(*govtypesv1.MsgVoteWeighted)
_, isMsgVoteWeightedV1Beta1 := msg.(*govtypesv1beta1.MsgVoteWeighted)
var msgProposalID uint64

switch cosmosMsg := msg.(type) {
case *govtypesv1.MsgVote:
msgProposalID = cosmosMsg.ProposalId

case *govtypesv1beta1.MsgVote:
msgProposalID = cosmosMsg.ProposalId

case *govtypesv1.MsgVoteWeighted:
msgProposalID = cosmosMsg.ProposalId

case *govtypesv1beta1.MsgVoteWeighted:
msgProposalID = cosmosMsg.ProposalId

// Skip if the message is not a vote message
if !isMsgVoteV1 && !isMsgVoteV1Beta1 && !isMsgVoteWeightedV1 && !isMsgVoteWeightedV1Beta1 {
default:
continue
} else {
// check if requested proposal ID is the same as proposal ID returned
// from the msg as some txs may contain multiple MsgVote msgs
// for different proposals which can cause error if one of the proposals
// info is not stored in database
if proposalID == msgVote.ProposalId {
err = govModule.HandleMsg(index, msg, junoTx)
if err != nil {
return fmt.Errorf("error while handling MsgVote: %s", err)
}
} else {
// skip votes for proposals with IDs
// different than requested in the query
continue
}

// check if requested proposal ID is the same as proposal ID returned
// from the msg as some txs may contain multiple MsgVote msgs
// for different proposals which can cause error if one of the proposals
// info is not stored in database
if proposalID == msgProposalID {
err = govModule.HandleMsg(index, msg, junoTx)
if err != nil {
return fmt.Errorf("error while handling MsgVote: %s", err)
}
} else {
// skip votes for proposals with IDs
// different than requested in the query
continue
}
}
}
Expand Down
38 changes: 10 additions & 28 deletions modules/gov/handle_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"

distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"

Expand Down Expand Up @@ -112,10 +112,11 @@ func (m *Module) handleSubmitProposalEvent(tx *juno.Tx, proposer string, events

// Store the proposal
proposalObj := types.NewProposal(
proposal.ProposalId,
proposal.GetContent().ProposalRoute(),
proposal.GetContent().ProposalType(),
proposal.GetContent(),
proposal.Id,
proposal.Title,
proposal.Summary,
proposal.Metadata,
proposal.Messages,
proposal.Status.String(),
*proposal.SubmitTime,
*proposal.DepositEndTime,
Expand Down Expand Up @@ -151,7 +152,7 @@ func (m *Module) handleDepositEvent(tx *juno.Tx, depositor string, events sdk.St
}

return m.db.SaveDeposits([]types.Deposit{
types.NewDeposit(proposalID, depositor, deposit.Amount, txTimestamp, tx.Height),
types.NewDeposit(proposalID, depositor, deposit.Amount, txTimestamp, tx.TxHash, tx.Height),
})
}

Expand All @@ -169,37 +170,18 @@ func (m *Module) handleVoteEvent(tx *juno.Tx, voter string, events sdk.StringEve
}

// Get the vote option
voteOption, err := VoteOptionFromEvents(events)
weightVoteOption, err := WeightVoteOptionFromEvents(events)
if err != nil {
return fmt.Errorf("error while getting vote option: %s", err)
}

vote := types.NewVote(proposalID, voter, voteOption, txTimestamp, tx.Height)
vote := types.NewVote(proposalID, voter, weightVoteOption.Option, weightVoteOption.Weight, txTimestamp, tx.Height)

err = m.db.SaveVote(vote)
if err != nil {
return fmt.Errorf("error while saving vote: %s", err)
}

// update tally result for given proposal
return m.UpdateProposalTallyResult(msg.ProposalId, tx.Height)
}

// handleMsgVoteWeighted allows to properly handle a MsgVoteWeighted
func (m *Module) handleMsgVoteWeighted(tx *juno.Tx, msg *govtypesv1.MsgVoteWeighted) error {
Copy link
Contributor Author

@dadamu dadamu Mar 21, 2024

Choose a reason for hiding this comment

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

It shares the same vote event as MsgVote, so it can be removed safely.

txTimestamp, err := time.Parse(time.RFC3339, tx.Timestamp)
if err != nil {
return fmt.Errorf("error while parsing time: %s", err)
}

for _, option := range msg.Options {
vote := types.NewVote(msg.ProposalId, msg.Voter, option.Option, option.Weight, txTimestamp, tx.Height)
err = m.db.SaveVote(vote)
if err != nil {
return fmt.Errorf("error while saving weighted vote for address %s: %s", msg.Voter, err)
}
}

// update tally result for given proposal
return m.UpdateProposalTallyResult(msg.ProposalId, tx.Height)
return m.UpdateProposalTallyResult(proposalID, tx.Height)
}
33 changes: 18 additions & 15 deletions modules/gov/utils_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,43 @@ func ProposalIDFromEvents(events sdk.StringEvents) (uint64, error) {
return 0, fmt.Errorf("no proposal id found")
}

// VoteOptionFromEvents returns the vote option from the given events
func VoteOptionFromEvents(events sdk.StringEvents) (govtypesv1.VoteOption, error) {
// WeightVoteOptionFromEvents returns the vote option from the given events
func WeightVoteOptionFromEvents(events sdk.StringEvents) (govtypesv1.WeightedVoteOption, error) {
for _, event := range events {
attribute, ok := eventsutil.FindAttributeByKey(event, govtypes.AttributeKeyOption)
if ok {
return parseVoteOption(attribute.Value)
return parseWeightVoteOption(attribute.Value)
}
}

return 0, fmt.Errorf("no vote option found")
return govtypesv1.WeightedVoteOption{}, fmt.Errorf("no vote option found")
}

// parseVoteOption returns the vote option from the given string
// option value in string could be 2 cases, for example:
// parseWeightVoteOption returns the vote option from the given string
// option value in string has 2 cases, for example:
// 1. "{\"option\":1,\"weight\":\"1.000000000000000000\"}"
// 2. "option:VOTE_OPTION_NO weight:\"1.000000000000000000\""
func parseVoteOption(optionValue string) (govtypesv1.VoteOption, error) {
func parseWeightVoteOption(optionValue string) (govtypesv1.WeightedVoteOption, error) {
// try parse option value as json
type voteOptionJSON struct {
Option govtypesv1.VoteOption `json:"option"`
}
var voteOptionParsedJSON voteOptionJSON
err := json.Unmarshal([]byte(optionValue), &voteOptionParsedJSON)
var weightedVoteOption govtypesv1.WeightedVoteOption
err := json.Unmarshal([]byte(optionValue), &weightedVoteOption)
if err == nil {
return voteOptionParsedJSON.Option, nil
return weightedVoteOption, nil
}

// try parse option value as string
// option:VOTE_OPTION_NO weight:"1.000000000000000000"
voteOptionParsed := strings.Split(optionValue, " ")
if len(voteOptionParsed) != 2 {
return govtypesv1.WeightedVoteOption{}, fmt.Errorf("failed to parse vote option %s", optionValue)
}

voteOption, err := govtypesv1.VoteOptionFromString(strings.ReplaceAll(voteOptionParsed[0], "option:", ""))
if err != nil {
return 0, fmt.Errorf("failed to parse vote option %s: %s", optionValue, err)
return govtypesv1.WeightedVoteOption{}, fmt.Errorf("failed to parse vote option %s: %s", optionValue, err)
}
weight := strings.ReplaceAll(voteOptionParsed[1], "weight:", "")
weight = strings.ReplaceAll(weight, "\\", "")

return voteOption, nil
return govtypesv1.WeightedVoteOption{Option: voteOption, Weight: weight}, nil
}
Loading