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

Add gov multi vote support. #77

Merged
merged 1 commit into from
Jan 9, 2025
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
12 changes: 7 additions & 5 deletions modules/gov/handle_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,18 @@ func (m *Module) handleVoteEvent(tx *juno.Transaction, voter string, events sdk.
}

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

vote := types.NewVote(proposalID, voter, weightVoteOption.Option, weightVoteOption.Weight, txTimestamp, int64(tx.Height))
for _, weightVoteOption := range weightVoteOptions {
vote := types.NewVote(proposalID, voter, weightVoteOption.Option, weightVoteOption.Weight, txTimestamp, int64(tx.Height))

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

// update tally result for given proposal
Expand Down
25 changes: 11 additions & 14 deletions modules/gov/utils_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,54 +24,51 @@ func ProposalIDFromEvents(events sdk.StringEvents) (uint64, error) {
return 0, fmt.Errorf("no proposal id found")
}

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

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

// parseWeightVoteOption returns the vote option from the given string
// parseWeightVoteOptions 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\":1,\"weight\":\"1.000000000000000000\"}"
// 3. "option:VOTE_OPTION_NO weight:\"1.000000000000000000\""
func parseWeightVoteOption(optionValue string) (govtypesv1.WeightedVoteOption, error) {
func parseWeightVoteOptions(optionValue string) ([]govtypesv1.WeightedVoteOption, error) {
// try parse json options value
var weightedVoteOptions []govtypesv1.WeightedVoteOption
err := json.Unmarshal([]byte(optionValue), &weightedVoteOptions)
if err == nil {
if len(weightedVoteOptions) > 1 {
return govtypesv1.WeightedVoteOption{}, fmt.Errorf("failed to parse vote option %s: too many options", optionValue)
}
return weightedVoteOptions[0], nil
return weightedVoteOptions, nil
}

// try parse json option value
var weightedVoteOption govtypesv1.WeightedVoteOption
err = json.Unmarshal([]byte(optionValue), &weightedVoteOption)
if err == nil {
return weightedVoteOption, nil
return []govtypesv1.WeightedVoteOption{weightedVoteOption}, nil
}

// try parse string option value
// 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)
return nil, fmt.Errorf("failed to parse vote option %s", optionValue)
}

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

return govtypesv1.WeightedVoteOption{Option: voteOption, Weight: weight}, nil
return []govtypesv1.WeightedVoteOption{{Option: voteOption, Weight: weight}}, nil
}
26 changes: 21 additions & 5 deletions modules/gov/utils_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestWeightVoteOptionFromEvents(t *testing.T) {
tests := []struct {
name string
events sdk.StringEvents
expected govtypesv1.WeightedVoteOption
expected []govtypesv1.WeightedVoteOption
shouldErr bool
}{
{
Expand All @@ -27,7 +27,23 @@ func TestWeightVoteOptionFromEvents(t *testing.T) {
},
},
},
govtypesv1.WeightedVoteOption{Option: govtypesv1.OptionYes, Weight: "1.000000000000000000"},
[]govtypesv1.WeightedVoteOption{{Option: govtypesv1.OptionYes, Weight: "1.000000000000000000"}},
false,
},
{
"json options from vote event returns properly",
sdk.StringEvents{
sdk.StringEvent{
Type: "vote",
Attributes: []sdk.Attribute{
sdk.NewAttribute(govtypes.AttributeKeyOption, "[{\"option\":3,\"weight\":\"0.300000000000000000\"},{\"option\":4,\"weight\":\"0.700000000000000000\"}]"),
},
},
},
[]govtypesv1.WeightedVoteOption{
{Option: govtypesv1.OptionNo, Weight: "0.300000000000000000"},
{Option: govtypesv1.OptionNoWithVeto, Weight: "0.700000000000000000"},
},
false,
},
{
Expand All @@ -40,7 +56,7 @@ func TestWeightVoteOptionFromEvents(t *testing.T) {
},
},
},
govtypesv1.WeightedVoteOption{Option: govtypesv1.OptionNo, Weight: "1.000000000000000000"},
[]govtypesv1.WeightedVoteOption{{Option: govtypesv1.OptionNo, Weight: "1.000000000000000000"}},
false,
},
{
Expand All @@ -53,14 +69,14 @@ func TestWeightVoteOptionFromEvents(t *testing.T) {
},
},
},
govtypesv1.WeightedVoteOption{},
nil,
true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := gov.WeightVoteOptionFromEvents(test.events)
result, err := gov.WeightVoteOptionsFromEvents(test.events)
if test.shouldErr {
require.Error(t, err)
} else {
Expand Down
Loading