Skip to content

Commit

Permalink
chore: refactor vote type
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Dec 20, 2023
1 parent fd8fd54 commit 70c0cae
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 76 deletions.
10 changes: 5 additions & 5 deletions pkg/data/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package data

import (
"fmt"
"main/pkg/tendermint"
"main/pkg/fetchers/cosmos"
"main/pkg/types"
"strconv"
"sync"
Expand Down Expand Up @@ -34,10 +34,10 @@ func (m *Manager) GetTallies() (map[string]types.ChainTallyInfos, error) {
tallies := make(map[string]map[string]types.Tally, 0)

for _, chain := range m.Chains {
rpc := tendermint.NewRPC(chain, m.Logger)
rpc := cosmos.NewRPC(chain, m.Logger)

wg.Add(1)
go func(c *types.Chain, rpc *tendermint.RPC) {
go func(c *types.Chain, rpc *cosmos.RPC) {
defer wg.Done()

pool, err := rpc.GetStakingPool()
Expand All @@ -57,7 +57,7 @@ func (m *Manager) GetTallies() (map[string]types.ChainTallyInfos, error) {
}(chain, rpc)

wg.Add(1)
go func(c *types.Chain, rpc *tendermint.RPC) {
go func(c *types.Chain, rpc *cosmos.RPC) {
defer wg.Done()

chainProposals, err := rpc.GetAllProposals()
Expand Down Expand Up @@ -168,7 +168,7 @@ func (m *Manager) GetChainParams(chain *types.Chain) (*types.ChainWithVotingPara
errors := make([]error, 0)
params := &types.ParamsResponse{}

rpc := tendermint.NewRPC(chain, m.Logger)
rpc := cosmos.NewRPC(chain, m.Logger)

wg.Add(3)

Expand Down
25 changes: 20 additions & 5 deletions pkg/tendermint/tendermint.go → pkg/fetchers/cosmos/fetcher.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package tendermint
package cosmos

import (
"encoding/json"
"errors"
"fmt"
"main/pkg/fetchers/cosmos/responses"
"main/pkg/utils"
"net/http"
"strings"
Expand Down Expand Up @@ -118,28 +119,42 @@ func (rpc *RPC) GetAllV1Proposals() ([]types.Proposal, *types.QueryError) {
return proposals, nil
}

func (rpc *RPC) GetVote(proposal, voter string) (*types.VoteRPCResponse, *types.QueryError) {
func (rpc *RPC) GetVote(proposal, voter string) (*types.Vote, *types.QueryError) {
url := fmt.Sprintf(
"/cosmos/gov/v1beta1/proposals/%s/votes/%s",
proposal,
voter,
)

var vote types.VoteRPCResponse
var vote responses.VoteRPCResponse
if errs := rpc.Get(url, &vote); len(errs) > 0 {
return nil, &types.QueryError{
QueryError: nil,
NodeErrors: errs,
}
}

if vote.IsError() && !strings.Contains(vote.Message, "not found") {
if vote.IsError() {
// not voted
if strings.Contains(vote.Message, "not found") {
return nil, nil
}

// some other errors
return nil, &types.QueryError{
QueryError: errors.New(vote.Message),
}
}

return &vote, nil
voteParsed, err := vote.ToVote()
if err != nil {
return nil, &types.QueryError{
QueryError: err,
NodeErrors: nil,
}
}

return voteParsed, nil
}

func (rpc *RPC) GetTally(proposal string) (*types.TallyRPCResponse, *types.QueryError) {
Expand Down
62 changes: 62 additions & 0 deletions pkg/fetchers/cosmos/responses/vote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package responses

import (
"main/pkg/types"
"strconv"
)

// cosmos/gov/v1beta1/proposals/:id/votes/:wallet

type Vote struct {
ProposalID string `json:"proposal_id"`
Voter string `json:"voter"`
Option string `json:"option"`
Options []VoteOption `json:"options"`
}

type VoteOption struct {
Option string `json:"option"`
Weight string `json:"weight"`
}

type VoteRPCResponse struct {
Code int64 `json:"code"`
Message string `json:"message"`
Vote *Vote `json:"vote"`
}

func (v VoteRPCResponse) IsError() bool {
return v.Code != 0
}

func (v VoteRPCResponse) ToVote() (*types.Vote, error) {
var options []types.VoteOption

if len(v.Vote.Options) > 0 {
options = make([]types.VoteOption, len(v.Vote.Options))

for index, option := range v.Vote.Options {
weight, err := strconv.ParseFloat(option.Weight, 64)
if err != nil {
return nil, err
}

options[index] = types.VoteOption{
Option: types.VoteType(option.Option),
Weight: weight,
}
}
} else {
options = make([]types.VoteOption, 1)
options[0] = types.VoteOption{
Option: types.VoteType(v.Vote.Option),
Weight: 1,
}
}

return &types.Vote{
ProposalID: v.Vote.ProposalID,
Voter: v.Vote.Voter,
Options: options,
}, nil
}
6 changes: 3 additions & 3 deletions pkg/report/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package report

import (
"main/pkg/events"
"main/pkg/fetchers/cosmos"
"main/pkg/report/entry"
"main/pkg/reporters"
"main/pkg/state"
"main/pkg/tendermint"
"main/pkg/types"

"github.com/rs/zerolog"
Expand All @@ -14,7 +14,7 @@ import (
type Generator struct {
StateManager *state.Manager
Chains types.Chains
RPC *tendermint.RPC
RPC *cosmos.RPC
Logger zerolog.Logger
}

Expand Down Expand Up @@ -105,7 +105,7 @@ func (g *Generator) GenerateReport(oldState, newState state.State) reporters.Rep
}

// Changed its vote - only notify via Telegram.
if newVote.HasVoted() && oldVote.HasVoted() && newVote.Vote.Option != oldVote.Vote.Option {
if newVote.HasVoted() && oldVote.HasVoted() && !newVote.Vote.VotesEquals(oldVote.Vote) {
g.Logger.Debug().
Str("chain", chainName).
Str("proposal", proposal.ID).
Expand Down
6 changes: 3 additions & 3 deletions pkg/report/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestReportGeneratorWithVoted(t *testing.T) {
Votes: map[string]state.ProposalVote{
"wallet": {
Vote: &types.Vote{
Option: "YES",
Options: []types.VoteOption{{Option: "YES", Weight: 1}},
},
},
},
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestReportGeneratorWithRevoted(t *testing.T) {
Votes: map[string]state.ProposalVote{
"wallet": {
Vote: &types.Vote{
Option: "NO",
Options: []types.VoteOption{{Option: "NO", Weight: 1}},
},
},
},
Expand All @@ -204,7 +204,7 @@ func TestReportGeneratorWithRevoted(t *testing.T) {
Votes: map[string]state.ProposalVote{
"wallet": {
Vote: &types.Vote{
Option: "YES",
Options: []types.VoteOption{{Option: "YES", Weight: 1}},
},
},
},
Expand Down
12 changes: 6 additions & 6 deletions pkg/state/generator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package state

import (
"main/pkg/tendermint"
"main/pkg/fetchers/cosmos"
"main/pkg/types"
"sync"

Expand Down Expand Up @@ -44,7 +44,7 @@ func (g *Generator) ProcessChain(
state State,
oldState State,
) {
rpc := tendermint.NewRPC(chain, g.Logger)
rpc := cosmos.NewRPC(chain, g.Logger)

proposals, err := rpc.GetAllProposals()
if err != nil {
Expand Down Expand Up @@ -94,15 +94,15 @@ func (g *Generator) ProcessChain(
func (g *Generator) ProcessProposalAndWallet(
chain *types.Chain,
proposal types.Proposal,
rpc *tendermint.RPC,
rpc *cosmos.RPC,
wallet *types.Wallet,
state State,
oldState State,
) {
oldVote, _, found := oldState.GetVoteAndProposal(chain.Name, proposal.ID, wallet.Address)
voteResponse, err := rpc.GetVote(proposal.ID, wallet.Address)
vote, err := rpc.GetVote(proposal.ID, wallet.Address)

if found && oldVote.HasVoted() && voteResponse.Vote == nil {
if found && oldVote.HasVoted() && vote == nil {
g.Logger.Trace().
Str("chain", chain.Name).
Str("proposal", proposal.ID).
Expand All @@ -126,7 +126,7 @@ func (g *Generator) ProcessProposalAndWallet(
if err != nil {
proposalVote.Error = err
} else {
proposalVote.Vote = voteResponse.Vote
proposalVote.Vote = vote
}

g.Mutex.Lock()
Expand Down
8 changes: 5 additions & 3 deletions pkg/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestSetVoteWithoutChainInfo(t *testing.T) {
&types.Wallet{Address: "wallet"},
ProposalVote{
Vote: &types.Vote{
Option: "yep",
Options: []types.VoteOption{{Option: "YES", Weight: 1}},
},
},
)
Expand Down Expand Up @@ -61,7 +61,9 @@ func TestSetVotes(t *testing.T) {
"proposal": {
Votes: map[string]ProposalVote{
"wallet": {
Vote: &types.Vote{Option: "YES"},
Vote: &types.Vote{
Options: []types.VoteOption{{Option: "YES", Weight: 1}},
},
},
},
},
Expand Down Expand Up @@ -229,7 +231,7 @@ func TestHasVotedWithWalletVotePresent(t *testing.T) {
Votes: map[string]ProposalVote{
"wallet": {
Vote: &types.Vote{
Option: "YEP",
Options: []types.VoteOption{{Option: "YES", Weight: 1}},
},
},
},
Expand Down
36 changes: 0 additions & 36 deletions pkg/types/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,42 +92,6 @@ type V1ProposalsRPCResponse struct {
Proposals []V1Proposal `json:"proposals"`
}

// cosmos/gov/v1beta1/proposals/:id/votes/:wallet

type Vote struct {
ProposalID string `json:"proposal_id"`
Voter string `json:"voter"`
Option string `json:"option"`
Options []VoteOption `json:"options"`
}

type VoteOption struct {
Option string `json:"option"`
Weight string `json:"weight"`
}

func (v Vote) ResolveVote() string {
if len(v.Options) > 0 {
optionsStrings := utils.Map(v.Options, func(v VoteOption) string {
return utils.ResolveVote(v.Option)
})

return strings.Join(optionsStrings, ", ")
}

return utils.ResolveVote(v.Option)
}

type VoteRPCResponse struct {
Code int64 `json:"code"`
Message string `json:"message"`
Vote *Vote `json:"vote"`
}

func (v VoteRPCResponse) IsError() bool {
return v.Code != 0
}

type TallyRPCResponse struct {
Code int64 `json:"code"`
Message string `json:"message"`
Expand Down
59 changes: 59 additions & 0 deletions pkg/types/vote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package types

import (
"main/pkg/utils"
"strings"
)

// cosmos/gov/v1beta1/proposals/:id/votes/:wallet

type VoteType string

func (v VoteType) Resolve() string {
votes := map[string]string{
"VOTE_OPTION_YES": "Yes",
"VOTE_OPTION_ABSTAIN": "Abstain",
"VOTE_OPTION_NO": "No",
"VOTE_OPTION_NO_WITH_VETO": "No with veto",
}

if vote, ok := votes[string(v)]; ok && v != "" {
return vote
}

return string(v)
}

type VoteOption struct {
Option VoteType
Weight float64
}
type VoteOptions []VoteOption

type Vote struct {
ProposalID string
Voter string
Options VoteOptions
}

func (v Vote) ResolveVote() string {
optionsStrings := utils.Map(v.Options, func(v VoteOption) string {
return v.Option.Resolve()
})

return strings.Join(optionsStrings, ", ")
}

func (v Vote) VotesEquals(other *Vote) bool {
if len(v.Options) != len(other.Options) {
return false
}

for index, option := range v.Options {
if option.Weight != other.Options[index].Weight || option.Option != other.Options[index].Option {
return false
}
}

return true
}
Loading

0 comments on commit 70c0cae

Please sign in to comment.