Skip to content

Commit

Permalink
feat: use governance v1 to fetch proposals (needed for mars)
Browse files Browse the repository at this point in the history
  • Loading branch information
PFC-developer committed Nov 15, 2023
1 parent 4f3fbb5 commit 3b094dc
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 72 deletions.
57 changes: 41 additions & 16 deletions pkg/exporter/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
query "github.com/cosmos/cosmos-sdk/types/query"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypeV1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

Expand Down Expand Up @@ -435,23 +436,47 @@ func GetGeneralMetrics(wg *sync.WaitGroup, sublogger *zerolog.Logger, metrics *G
}
}()
*/
wg.Add(1)
go func() {
defer wg.Done()
sublogger.Debug().Msg("Started querying global gov params")
govClient := govtypes.NewQueryClient(s.GrpcConn)
proposals, err := govClient.Proposals(context.Background(), &govtypes.QueryProposalsRequest{
ProposalStatus: govtypes.StatusVotingPeriod,
})
if err != nil {
sublogger.Error().
Err(err).
Msg("Could not get active proposals")
}

proposalsCount := len(proposals.GetProposals())
metrics.govVotingPeriodProposals.Set(float64(proposalsCount))
}()
if config.PropV1 {
wg.Add(1)
go func() {
defer wg.Done()

sublogger.Debug().Msg("Started querying global gov V1 params")

govClient := govtypeV1.NewQueryClient(s.GrpcConn)
proposals, err := govClient.Proposals(context.Background(), &govtypeV1.QueryProposalsRequest{
ProposalStatus: govtypeV1.StatusVotingPeriod,
})
if err != nil {
sublogger.Error().
Err(err).
Msg("Could not get active proposals")
}
proposalsCount := len(proposals.GetProposals())
metrics.govVotingPeriodProposals.Set(float64(proposalsCount))
}()
} else {
wg.Add(1)
go func() {
defer wg.Done()

sublogger.Debug().Msg("Started querying global gov v1beta1 params")

govClient := govtypes.NewQueryClient(s.GrpcConn)
proposals, err := govClient.Proposals(context.Background(), &govtypes.QueryProposalsRequest{
ProposalStatus: govtypes.StatusVotingPeriod,
})
if err != nil {
sublogger.Error().
Err(err).
Msg("Could not get active proposals")
}

proposalsCount := len(proposals.GetProposals())
metrics.govVotingPeriodProposals.Set(float64(proposalsCount))
}()
}

}

Expand Down
195 changes: 140 additions & 55 deletions pkg/exporter/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exporter

import (
"context"
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand All @@ -11,6 +12,7 @@ import (
"sync"
"time"

govtypeV1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -21,6 +23,10 @@ type ProposalsMetrics struct {
proposalsGauge *prometheus.GaugeVec
}

type proposalMeta struct {
Title string `json:"title"`
}

func NewProposalsMetrics(reg prometheus.Registerer, config *ServiceConfig) *ProposalsMetrics {
m := &ProposalsMetrics{
proposalsGauge: prometheus.NewGaugeVec(
Expand All @@ -35,67 +41,146 @@ func NewProposalsMetrics(reg prometheus.Registerer, config *ServiceConfig) *Prop
reg.MustRegister(m.proposalsGauge)
return m
}
func GetProposalsMetrics(wg *sync.WaitGroup, sublogger *zerolog.Logger, metrics *ProposalsMetrics, s *Service, _ *ServiceConfig, activeOnly bool) {

wg.Add(1)
go func() {
defer wg.Done()

var proposals []govtypes.Proposal

sublogger.Debug().Msg("Started querying proposals")
queryStart := time.Now()

govClient := govtypes.NewQueryClient(s.GrpcConn)

var propReq govtypes.QueryProposalsRequest
if activeOnly {
propReq = govtypes.QueryProposalsRequest{ProposalStatus: govtypes.StatusVotingPeriod, Pagination: &query.PageRequest{Reverse: true}}
} else {
propReq = govtypes.QueryProposalsRequest{Pagination: &query.PageRequest{Reverse: true}}
}
proposalsResponse, err := govClient.Proposals(
context.Background(),
&propReq,
)
if err != nil {
sublogger.Error().Err(err).Msg("Could not get proposals")
return
}

sublogger.Debug().
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying proposals")
proposals = proposalsResponse.Proposals

sublogger.Debug().
Int("proposalsLength", len(proposals)).
Msg("Proposals info")

cdcRegistry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(cdcRegistry)
for _, proposal := range proposals {

var content govtypes.TextProposal
err := cdc.Unmarshal(proposal.Content.Value, &content)
func GetProposalsMetrics(wg *sync.WaitGroup, sublogger *zerolog.Logger, metrics *ProposalsMetrics, s *Service, config *ServiceConfig, activeOnly bool) {
if config.PropV1 {
wg.Add(1)
go func() {
defer wg.Done()

sublogger.Debug().Msg("Started querying v1 proposals")
queryStart := time.Now()

govClient := govtypeV1.NewQueryClient(s.GrpcConn)

var propReq govtypeV1.QueryProposalsRequest
if activeOnly {
propReq = govtypeV1.QueryProposalsRequest{ProposalStatus: govtypeV1.StatusVotingPeriod, Pagination: &query.PageRequest{Reverse: true}}
} else {
propReq = govtypeV1.QueryProposalsRequest{Pagination: &query.PageRequest{Reverse: true}}
}
proposalsResponse, err := govClient.Proposals(
context.Background(),
&propReq,
)
if err != nil {
sublogger.Error().Err(err).Msg("Could not get proposals")
return
}

sublogger.Debug().
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying proposals")
var proposals = proposalsResponse.Proposals

sublogger.Debug().
Int("proposalsLength", len(proposals)).
Msg("Proposals info")

//cdcRegistry := codectypes.NewInterfaceRegistry()
//cdc := codec.NewProtoCodec(cdcRegistry)
for _, proposal := range proposals {
var title string = ""
if len(proposal.Metadata) > 0 {
var metadata proposalMeta

err := json.Unmarshal([]byte(proposal.Metadata), &metadata)
if err != nil {
sublogger.Error().
Str("proposal_id", fmt.Sprint(proposal.Id)).
Err(err).
Msg("Could not parse proposal metadata field")
} else {
title = metadata.Title
}
sublogger.Info().
Str("proposal_id", fmt.Sprint(proposal.Id)).
Str("metadata", metadata.Title).
Msg("metadata")

} else {
sublogger.Info().
Str("proposal_id", fmt.Sprint(proposal.Id)).
Msg("Does not have metadata?")
title = fmt.Sprintf("Proposal %d has no metadata", proposal.Id)
}
if proposal.VotingStartTime == nil || proposal.VotingEndTime == nil {
metrics.proposalsGauge.With(prometheus.Labels{
"title": title,
"status": proposal.Status.String(),
"voting_start_time": "nil",
"voting_end_time": "nil",
}).Set(float64(proposal.Id))
} else {
metrics.proposalsGauge.With(prometheus.Labels{
"title": title,
"status": proposal.Status.String(),
"voting_start_time": proposal.VotingStartTime.String(),
"voting_end_time": proposal.VotingEndTime.String(),
}).Set(float64(proposal.Id))
}

}
}()
} else {
wg.Add(1)
go func() {
defer wg.Done()

var proposals []govtypes.Proposal

sublogger.Debug().Msg("Started querying v1beta1 proposals")
queryStart := time.Now()

govClient := govtypes.NewQueryClient(s.GrpcConn)

var propReq govtypes.QueryProposalsRequest
if activeOnly {
propReq = govtypes.QueryProposalsRequest{ProposalStatus: govtypes.StatusVotingPeriod, Pagination: &query.PageRequest{Reverse: true}}
} else {
propReq = govtypes.QueryProposalsRequest{Pagination: &query.PageRequest{Reverse: true}}
}
proposalsResponse, err := govClient.Proposals(
context.Background(),
&propReq,
)
if err != nil {
sublogger.Error().
Str("proposal_id", fmt.Sprint(proposal.ProposalId)).
Err(err).
Msg("Could not parse proposal content")
sublogger.Error().Err(err).Msg("Could not get proposals")
return
}

metrics.proposalsGauge.With(prometheus.Labels{
"title": content.Title,
"status": proposal.Status.String(),
"voting_start_time": proposal.VotingStartTime.String(),
"voting_end_time": proposal.VotingEndTime.String(),
}).Set(float64(proposal.ProposalId))
sublogger.Debug().
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying proposals")
proposals = proposalsResponse.Proposals

sublogger.Debug().
Int("proposalsLength", len(proposals)).
Msg("Proposals info")

cdcRegistry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(cdcRegistry)
for _, proposal := range proposals {

}
}()
var content govtypes.TextProposal
err := cdc.Unmarshal(proposal.Content.Value, &content)

if err != nil {
sublogger.Error().
Str("proposal_id", fmt.Sprint(proposal.ProposalId)).
Err(err).
Msg("Could not parse proposal content")
}

metrics.proposalsGauge.With(prometheus.Labels{
"title": content.Title,
"status": proposal.Status.String(),
"voting_start_time": proposal.VotingStartTime.String(),
"voting_end_time": proposal.VotingEndTime.String(),
}).Set(float64(proposal.ProposalId))

}
}()
}
}
func (s *Service) ProposalsHandler(w http.ResponseWriter, r *http.Request) {
requestStart := time.Now()
Expand Down
5 changes: 4 additions & 1 deletion pkg/exporter/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type ServiceConfig struct {
Proposals bool
Params bool
TokenPrice bool
PropV1 bool
}

type Service struct {
Expand Down Expand Up @@ -223,6 +224,7 @@ func (config *ServiceConfig) SetCommonParameters(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVar(&config.TokenPrice, "price", true, "fetch token price")
cmd.PersistentFlags().StringSliceVar(&config.Wallets, "wallets", nil, "serve info about passed wallets")
cmd.PersistentFlags().StringSliceVar(&config.Validators, "validators", nil, "serve info about passed validators")
cmd.PersistentFlags().BoolVar(&config.PropV1, "propv1", false, "use PropV1 instead of PropV1Beta calls")
}
func (config *ServiceConfig) LogConfig(event *zerolog.Event) *zerolog.Event {
return event.
Expand All @@ -245,7 +247,8 @@ func (config *ServiceConfig) LogConfig(event *zerolog.Event) *zerolog.Event {
Bool("--proposals", config.Proposals).
Bool("--params", config.Params).
Bool("--upgrades", config.Upgrades).
Bool("--price", config.TokenPrice)
Bool("--price", config.TokenPrice).
Bool("--propv1", config.PropV1)
}

func (config *ServiceConfig) SetBechPrefixes(cmd *cobra.Command) {
Expand Down

0 comments on commit 3b094dc

Please sign in to comment.