forked from blockpane/govstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
175 lines (161 loc) · 4.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"context"
"fmt"
"gopkg.in/yaml.v3"
"log"
"os"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
"github.com/cosmos/cosmos-sdk/types/query"
gov1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
gov1b1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)
type Chain struct {
ChainID string `yaml:"chain_id"`
Validator string `yaml:"validator"`
Node string `yaml:"node"`
}
type chains struct {
Chains []Chain `yaml:"chains"`
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
chainsYaml, err := os.ReadFile("chains.yml")
if err != nil {
log.Fatal(err)
}
chains := &chains{}
err = yaml.Unmarshal(chainsYaml, chains)
if err != nil {
log.Fatal(err)
}
for _, chain := range chains.Chains {
client, err := rpchttp.New(chain.Node, "/websocket")
if err != nil {
log.Printf("error connecting to %s: %s\n", chain.ChainID, err)
continue
}
status, err := client.Status(context.Background())
if err != nil {
log.Printf("error getting status %s\n", err)
continue
}
if status.NodeInfo.Network != chain.ChainID {
log.Printf("chain id mismatch %s != %s\n", status.NodeInfo.Network, chain.ChainID)
continue
}
// this seems to marshal the same on v1 and v1beta1
qpr := gov1b1.QueryProposalsRequest{
ProposalStatus: gov1b1.StatusVotingPeriod,
Pagination: &query.PageRequest{Limit: 100, Offset: 0},
}
q, err := qpr.Marshal()
if err != nil {
log.Printf("error marshaling %s\n", err)
continue
}
response1b1 := &gov1b1.QueryProposalsResponse{}
response1 := &gov1.QueryProposalsResponse{}
var path string
var props int
for _, path = range []string{"v1", "v1beta1"} {
pResp, err := client.ABCIQuery(context.Background(), fmt.Sprintf("/cosmos.gov.%s.Query/Proposals", path), q)
if err != nil {
log.Printf("error getting votes %s\n", err)
continue
}
switch path {
case "v1beta1":
err = response1b1.Unmarshal(pResp.Response.Value)
case "v1":
err = response1.Unmarshal(pResp.Response.Value)
}
if err != nil {
log.Printf("error unmarshaling %s\n", err)
continue
}
switch path {
case "v1beta1":
props = len(response1b1.Proposals)
case "v1":
props = len(response1.Proposals)
}
if props != 0 {
break
}
}
if props == 0 {
fmt.Print("* no proposals on ", chain.ChainID, "\n\n")
continue
}
fmt.Printf("* found %d proposals for %s\n", props, chain.ChainID)
switch path {
case "v1beta1":
v1b1Response(response1b1, client, chain)
case "v1":
v1Response(response1, client, chain)
}
}
}
func v1Response(response *gov1.QueryProposalsResponse, client *rpchttp.HTTP, chain Chain) {
for _, proposal := range response.Proposals {
qr := gov1.QueryVoteRequest{
ProposalId: proposal.Id,
Voter: chain.Validator,
}
q, err := qr.Marshal()
if err != nil {
log.Printf("error marshaling %s\n", err)
continue
}
pResp, err := client.ABCIQuery(context.Background(), "/cosmos.gov.v1.Query/Vote", q)
if err != nil {
log.Printf("error getting vote %s\n", err)
continue
}
// fmt.Println(len(pResp.Response.Value))
response := &gov1.QueryVoteResponse{}
err = response.Unmarshal(pResp.Response.Value)
if err != nil {
log.Printf("error unmarshaling %s\n", err)
continue
}
voted := "❌"
if response.Vote != nil && response.Vote.Voter == chain.Validator {
voted = "✅"
}
fmt.Printf("%s proposal %d ends: %s\n", voted, proposal.Id, proposal.VotingEndTime.Local().Format("2006-01-02 15:04:05"))
}
fmt.Println("")
}
func v1b1Response(response *gov1b1.QueryProposalsResponse, client *rpchttp.HTTP, chain Chain) {
for _, proposal := range response.Proposals {
qr := gov1b1.QueryVoteRequest{
ProposalId: proposal.ProposalId,
Voter: chain.Validator,
}
q, err := qr.Marshal()
if err != nil {
log.Printf("error marshaling %s\n", err)
continue
}
pResp, err := client.ABCIQuery(context.Background(), "/cosmos.gov.v1beta1.Query/Vote", q)
if err != nil {
log.Printf("error getting vote %s\n", err)
continue
}
// fmt.Println(len(pResp.Response.Value))
response := &gov1b1.QueryVoteResponse{}
err = response.Unmarshal(pResp.Response.Value)
if err != nil {
log.Printf("error unmarshaling %s\n", err)
continue
}
voted := "❌"
if response.Vote.Voter == chain.Validator {
voted = "✅"
}
fmt.Printf("%s proposal %d ends: %s\n", voted, proposal.ProposalId, proposal.VotingEndTime.Local().Format("2006-01-02 15:04:05"))
}
fmt.Println("")
}