-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: monitor validators vote on pending proposals (#24)
- Loading branch information
Showing
9 changed files
with
235 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package watcher | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/assert" | ||
) | ||
|
||
func TestTrackedValidator(t *testing.T) { | ||
|
||
t.Run("AccountAddress", func(t *testing.T) { | ||
testdata := []struct { | ||
Address string | ||
Account string | ||
}{ | ||
{ | ||
Address: "cosmosvaloper1uxlf7mvr8nep3gm7udf2u9remms2jyjqvwdul2", | ||
Account: "cosmos1uxlf7mvr8nep3gm7udf2u9remms2jyjqf6efne", | ||
}, | ||
{ | ||
Address: "cosmosvaloper1n229vhepft6wnkt5tjpwmxdmcnfz55jv3vp77d", | ||
Account: "cosmos1n229vhepft6wnkt5tjpwmxdmcnfz55jv5c4tj7", | ||
}, | ||
} | ||
|
||
for _, td := range testdata { | ||
|
||
v := TrackedValidator{ | ||
OperatorAddress: td.Address, | ||
} | ||
|
||
assert.Equal(t, v.AccountAddress(), td.Account) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package watcher | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
gov "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" | ||
"github.com/kilnfi/cosmos-validator-watcher/pkg/metrics" | ||
"github.com/kilnfi/cosmos-validator-watcher/pkg/rpc" | ||
"github.com/rs/zerolog/log" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type VotesWatcher struct { | ||
metrics *metrics.Metrics | ||
validators []TrackedValidator | ||
pool *rpc.Pool | ||
} | ||
|
||
func NewVotesWatcher(validators []TrackedValidator, metrics *metrics.Metrics, pool *rpc.Pool) *VotesWatcher { | ||
return &VotesWatcher{ | ||
metrics: metrics, | ||
validators: validators, | ||
pool: pool, | ||
} | ||
} | ||
|
||
func (w *VotesWatcher) Start(ctx context.Context) error { | ||
ticker := time.NewTicker(1 * time.Minute) | ||
|
||
for { | ||
node := w.pool.GetSyncedNode() | ||
if node == nil { | ||
log.Warn().Msg("no node available to fetch proposals") | ||
} else if err := w.fetchProposals(ctx, node); err != nil { | ||
log.Error().Err(err).Msg("failed to fetch pending proposals") | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-ticker.C: | ||
} | ||
} | ||
} | ||
|
||
func (w *VotesWatcher) fetchProposals(ctx context.Context, node *rpc.Node) error { | ||
clientCtx := (client.Context{}).WithClient(node.Client) | ||
queryClient := gov.NewQueryClient(clientCtx) | ||
|
||
// Fetch all proposals in voting period | ||
proposalsResp, err := queryClient.Proposals(ctx, &gov.QueryProposalsRequest{ | ||
ProposalStatus: gov.StatusVotingPeriod, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to get proposals: %w", err) | ||
} | ||
|
||
// For each proposal, fetch validators vote | ||
for _, proposal := range proposalsResp.GetProposals() { | ||
for _, validator := range w.validators { | ||
voter := validator.AccountAddress() | ||
if voter == "" { | ||
log.Warn().Str("validator", validator.Name).Msg("no account address for validator") | ||
continue | ||
} | ||
voteResp, err := queryClient.Vote(ctx, &gov.QueryVoteRequest{ | ||
ProposalId: proposal.ProposalId, | ||
Voter: voter, | ||
}) | ||
if isInvalidArgumentError(err) { | ||
w.handleVote(node.ChainID(), validator, proposal.ProposalId, nil) | ||
} else if err != nil { | ||
return fmt.Errorf("failed to get validator vote for proposal %d: %w", proposal.ProposalId, err) | ||
} else { | ||
vote := voteResp.GetVote() | ||
w.handleVote(node.ChainID(), validator, proposal.ProposalId, vote.Options) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (w *VotesWatcher) handleVote(chainID string, validator TrackedValidator, proposalId uint64, votes []gov.WeightedVoteOption) { | ||
voted := false | ||
for _, option := range votes { | ||
if option.Option != gov.OptionEmpty { | ||
voted = true | ||
break | ||
} | ||
} | ||
|
||
w.metrics.Vote. | ||
WithLabelValues(chainID, validator.Address, validator.Name, fmt.Sprintf("%d", proposalId)). | ||
Set(metrics.BoolToFloat64(voted)) | ||
} | ||
|
||
func isInvalidArgumentError(err error) bool { | ||
st, ok := status.FromError(err) | ||
if !ok { | ||
return false | ||
} | ||
return st.Code() == codes.InvalidArgument | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package watcher | ||
|
||
import ( | ||
"testing" | ||
|
||
gov "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" | ||
"github.com/kilnfi/cosmos-validator-watcher/pkg/metrics" | ||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
"gotest.tools/assert" | ||
) | ||
|
||
func TestVotesWatcher(t *testing.T) { | ||
var ( | ||
kilnAddress = "3DC4DD610817606AD4A8F9D762A068A81E8741E2" | ||
kilnName = "Kiln" | ||
chainID = "chain-42" | ||
validators = []TrackedValidator{ | ||
{ | ||
Address: kilnAddress, | ||
Name: kilnName, | ||
}, | ||
} | ||
) | ||
|
||
votesWatcher := NewVotesWatcher( | ||
validators, | ||
metrics.New("cosmos_validator_watcher"), | ||
nil, | ||
) | ||
|
||
t.Run("Handle Votes", func(t *testing.T) { | ||
votesWatcher.handleVote(chainID, validators[0], 40, nil) | ||
votesWatcher.handleVote(chainID, validators[0], 41, []gov.WeightedVoteOption{{Option: gov.OptionEmpty}}) | ||
votesWatcher.handleVote(chainID, validators[0], 42, []gov.WeightedVoteOption{{Option: gov.OptionYes}}) | ||
|
||
assert.Equal(t, float64(0), testutil.ToFloat64(votesWatcher.metrics.Vote.WithLabelValues(chainID, kilnAddress, kilnName, "40"))) | ||
assert.Equal(t, float64(0), testutil.ToFloat64(votesWatcher.metrics.Vote.WithLabelValues(chainID, kilnAddress, kilnName, "41"))) | ||
assert.Equal(t, float64(1), testutil.ToFloat64(votesWatcher.metrics.Vote.WithLabelValues(chainID, kilnAddress, kilnName, "42"))) | ||
}) | ||
} |