@@ -43,7 +43,9 @@ func (w *VotesWatcher) Start(ctx context.Context) error {
4343 if node == nil {
4444 log .Warn ().Msg ("no node available to fetch proposals" )
4545 } else if err := w .fetchProposals (ctx , node ); err != nil {
46- log .Error ().Err (err ).Msg ("failed to fetch pending proposals" )
46+ log .Error ().Err (err ).
47+ Str ("node" , node .Redacted ()).
48+ Msg ("failed to fetch pending proposals" )
4749 }
4850
4951 select {
@@ -55,17 +57,37 @@ func (w *VotesWatcher) Start(ctx context.Context) error {
5557}
5658
5759func (w * VotesWatcher ) fetchProposals (ctx context.Context , node * rpc.Node ) error {
58- w .metrics .Vote .Reset ()
60+ var (
61+ votes map [uint64 ]map [TrackedValidator ]bool
62+ err error
63+ )
5964
6065 switch w .options .GovModuleVersion {
6166 case "v1beta1" :
62- return w .fetchProposalsV1Beta1 (ctx , node )
67+ votes , err = w .fetchProposalsV1Beta1 (ctx , node )
6368 default : // v1
64- return w .fetchProposalsV1 (ctx , node )
69+ votes , err = w .fetchProposalsV1 (ctx , node )
70+ }
71+
72+ if err != nil {
73+ return err
74+ }
75+
76+ w .metrics .Vote .Reset ()
77+ for proposalId , votes := range votes {
78+ for validator , voted := range votes {
79+ w .metrics .Vote .
80+ WithLabelValues (node .ChainID (), validator .Address , validator .Name , fmt .Sprintf ("%d" , proposalId )).
81+ Set (metrics .BoolToFloat64 (voted ))
82+ }
6583 }
84+
85+ return nil
6686}
6787
68- func (w * VotesWatcher ) fetchProposalsV1 (ctx context.Context , node * rpc.Node ) error {
88+ func (w * VotesWatcher ) fetchProposalsV1 (ctx context.Context , node * rpc.Node ) (map [uint64 ]map [TrackedValidator ]bool , error ) {
89+ votes := make (map [uint64 ]map [TrackedValidator ]bool )
90+
6991 clientCtx := (client.Context {}).WithClient (node .Client )
7092 queryClient := gov .NewQueryClient (clientCtx )
7193
@@ -74,13 +96,14 @@ func (w *VotesWatcher) fetchProposalsV1(ctx context.Context, node *rpc.Node) err
7496 ProposalStatus : gov .StatusVotingPeriod ,
7597 })
7698 if err != nil {
77- return fmt .Errorf ("failed to get proposals: %w" , err )
99+ return votes , fmt .Errorf ("failed to fetch proposals in voting period : %w" , err )
78100 }
79101
80102 chainID := node .ChainID ()
81103
82104 // For each proposal, fetch validators vote
83105 for _ , proposal := range proposalsResp .GetProposals () {
106+ votes [proposal .Id ] = make (map [TrackedValidator ]bool )
84107 w .metrics .ProposalEndTime .WithLabelValues (chainID , fmt .Sprintf ("%d" , proposal .Id )).Set (float64 (proposal .VotingEndTime .Unix ()))
85108
86109 for _ , validator := range w .validators {
@@ -95,34 +118,29 @@ func (w *VotesWatcher) fetchProposalsV1(ctx context.Context, node *rpc.Node) err
95118 })
96119
97120 if isInvalidArgumentError (err ) {
98- w . handleVoteV1 ( chainID , validator , proposal .Id , nil )
121+ votes [ proposal.Id ][ validator ] = false
99122 } else if err != nil {
100- return fmt .Errorf ("failed to get validator vote for proposal %d: %w" , proposal .Id , err )
123+ return votes , fmt .Errorf ("failed to get validator vote for proposal %d: %w" , proposal .Id , err )
101124 } else {
102125 vote := voteResp .GetVote ()
103- w .handleVoteV1 (chainID , validator , proposal .Id , vote .Options )
126+ voted := false
127+ for _ , option := range vote .Options {
128+ if option .Option != gov .OptionEmpty {
129+ voted = true
130+ break
131+ }
132+ }
133+ votes [proposal.Id ][validator ] = voted
104134 }
105135 }
106136 }
107137
108- return nil
138+ return votes , nil
109139}
110140
111- func (w * VotesWatcher ) handleVoteV1 (chainID string , validator TrackedValidator , proposalId uint64 , votes []* gov.WeightedVoteOption ) {
112- voted := false
113- for _ , option := range votes {
114- if option .Option != gov .OptionEmpty {
115- voted = true
116- break
117- }
118- }
141+ func (w * VotesWatcher ) fetchProposalsV1Beta1 (ctx context.Context , node * rpc.Node ) (map [uint64 ]map [TrackedValidator ]bool , error ) {
142+ votes := make (map [uint64 ]map [TrackedValidator ]bool )
119143
120- w .metrics .Vote .
121- WithLabelValues (chainID , validator .Address , validator .Name , fmt .Sprintf ("%d" , proposalId )).
122- Set (metrics .BoolToFloat64 (voted ))
123- }
124-
125- func (w * VotesWatcher ) fetchProposalsV1Beta1 (ctx context.Context , node * rpc.Node ) error {
126144 clientCtx := (client.Context {}).WithClient (node .Client )
127145 queryClient := govbeta .NewQueryClient (clientCtx )
128146
@@ -131,13 +149,14 @@ func (w *VotesWatcher) fetchProposalsV1Beta1(ctx context.Context, node *rpc.Node
131149 ProposalStatus : govbeta .StatusVotingPeriod ,
132150 })
133151 if err != nil {
134- return fmt .Errorf ("failed to get proposals: %w" , err )
152+ return votes , fmt .Errorf ("failed to fetch proposals in voting period : %w" , err )
135153 }
136154
137155 chainID := node .ChainID ()
138156
139157 // For each proposal, fetch validators vote
140158 for _ , proposal := range proposalsResp .GetProposals () {
159+ votes [proposal .ProposalId ] = make (map [TrackedValidator ]bool )
141160 w .metrics .ProposalEndTime .WithLabelValues (chainID , fmt .Sprintf ("%d" , proposal .ProposalId )).Set (float64 (proposal .VotingEndTime .Unix ()))
142161
143162 for _ , validator := range w .validators {
@@ -152,17 +171,24 @@ func (w *VotesWatcher) fetchProposalsV1Beta1(ctx context.Context, node *rpc.Node
152171 })
153172
154173 if isInvalidArgumentError (err ) {
155- w . handleVoteV1Beta1 ( chainID , validator , proposal .ProposalId , nil )
174+ votes [ proposal.ProposalId ][ validator ] = false
156175 } else if err != nil {
157- return fmt .Errorf ("failed to get validator vote for proposal %d: %w" , proposal .ProposalId , err )
176+ return votes , fmt .Errorf ("failed to get validator vote for proposal %d: %w" , proposal .ProposalId , err )
158177 } else {
159178 vote := voteResp .GetVote ()
160- w .handleVoteV1Beta1 (chainID , validator , proposal .ProposalId , vote .Options )
179+ voted := false
180+ for _ , option := range vote .Options {
181+ if option .Option != govbeta .OptionEmpty {
182+ voted = true
183+ break
184+ }
185+ }
186+ votes [proposal.ProposalId ][validator ] = voted
161187 }
162188 }
163189 }
164190
165- return nil
191+ return votes , nil
166192}
167193
168194func (w * VotesWatcher ) handleVoteV1Beta1 (chainID string , validator TrackedValidator , proposalId uint64 , votes []govbeta.WeightedVoteOption ) {
0 commit comments