Skip to content

Commit

Permalink
chore: handle gov proposal v1 and v1beta1
Browse files Browse the repository at this point in the history
  • Loading branch information
albttx committed Nov 5, 2023
1 parent da86c21 commit 42a41f6
Show file tree
Hide file tree
Showing 4 changed files with 2,922 additions and 164 deletions.
50 changes: 43 additions & 7 deletions general.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,59 @@ import (
"math/big"
"net/http"
"strconv"
"strings"
"sync"
"time"

"github.com/nysa-network/cosmos-exporter/pkg/cosmosdirectory"
"google.golang.org/grpc"

banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)


func queryGovCount(conn *grpc.ClientConn) (int, error) {
// Handle v1beta1
govv1beta1Client := govv1beta1.NewQueryClient(conn)
{
proposals, err := govv1beta1Client.Proposals(context.Background(), &govv1beta1.QueryProposalsRequest{
ProposalStatus: govv1beta1.StatusVotingPeriod,
})
if err != nil {
if !strings.Contains(err.Error(), `invalid type: can't convert a gov/v1 Proposal to gov/v1beta1 Proposal`) {
return 0, err
}
} else if err == nil {
pp := proposals.GetProposals()
return len(pp), nil
}
}

govv1Client := govv1.NewQueryClient(conn)
{
proposals, err := govv1Client.Proposals(context.Background(), &govv1.QueryProposalsRequest{
ProposalStatus: govv1.StatusVotingPeriod,
})
if err != nil {
return 0, err
}
pp := proposals.GetProposals()
return len(pp), nil
}

return 0, nil
}



func (s *service) GeneralHandler(w http.ResponseWriter, r *http.Request) {
requestStart := time.Now()

Expand Down Expand Up @@ -311,18 +349,16 @@ func (s *service) GeneralHandler(w http.ResponseWriter, r *http.Request) {
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,
})

govCount, err := queryGovCount(s.grpcConn)
if err != nil {
sublogger.Error().
Err(err).
Msg("Could not get active proposals")
govCount = -1
}

proposalsCount := len(proposals.GetProposals())
paramsGovVotingPeriodProposals.Set(float64(proposalsCount))
paramsGovVotingPeriodProposals.Set(float64(govCount))
}()

wg.Wait()
Expand Down
74 changes: 74 additions & 0 deletions general_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"context"
"fmt"
"strings"
"testing"

govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)

func queryGov(conn *grpc.ClientConn) error {
// Handle v1beta1
govv1beta1Client := govv1beta1.NewQueryClient(conn)
{
proposals, err := govv1beta1Client.Proposals(context.Background(), &govv1beta1.QueryProposalsRequest{
ProposalStatus: govv1beta1.StatusVotingPeriod,
})
if err != nil {
if !strings.Contains(err.Error(), `invalid type: can't convert a gov/v1 Proposal to gov/v1beta1 Proposal`) {
return err
}
} else if err == nil {
pp := proposals.GetProposals()
fmt.Printf("proposals: %T\n", proposals)
fmt.Printf("proposals: %v\n", len(pp))
return nil
}
}

govv1Client := govv1.NewQueryClient(conn)
{
proposals, err := govv1Client.Proposals(context.Background(), &govv1.QueryProposalsRequest{
ProposalStatus: govv1.StatusVotingPeriod,
})
if err != nil {
return err
}
pp := proposals.GetProposals()
fmt.Printf("proposals: %T\n", proposals)
fmt.Printf("proposals: %v\n", len(pp))
}

return nil
}

func TestGetActiveProposalsJUNO(t *testing.T) {
conn, err := grpc.Dial(
"juno-grpc.nysa.network:9090",
grpc.WithInsecure(),
)
require.NoError(t, err)

defer conn.Close()

err = queryGov(conn)
require.NoError(t, err)
}

func TestGetActiveProposalsArchway(t *testing.T) {
conn, err := grpc.Dial(
"archway-grpc.nysa.network:9090",
grpc.WithInsecure(),
)
require.NoError(t, err)

defer conn.Close()

err = queryGov(conn)
require.NoError(t, err)
}
22 changes: 12 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ go 1.16

replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

replace google.golang.org/grpc => google.golang.org/grpc v1.33.2
// replace google.golang.org/grpc => google.golang.org/grpc v1.33.2

replace github.com/tendermint/tendermint => github.com/informalsystems/tendermint v0.34.26

require (
github.com/cosmos/cosmos-sdk v0.42.4
github.com/google/uuid v1.2.0
github.com/prometheus/client_golang v1.8.0
github.com/rs/zerolog v1.20.0
github.com/spf13/cobra v1.1.1
github.com/cosmos/cosmos-sdk v0.46.10
github.com/google/uuid v1.3.0
github.com/prometheus/client_golang v1.14.0
github.com/rs/zerolog v1.27.0
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
github.com/tendermint/tendermint v0.34.9
google.golang.org/grpc v1.35.0
github.com/spf13/viper v1.13.0
github.com/stretchr/testify v1.8.1
github.com/tendermint/tendermint v0.34.26
google.golang.org/grpc v1.50.1
)
Loading

0 comments on commit 42a41f6

Please sign in to comment.