-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
imp(voting): only vote with accounts that have delegations (#2)
* suppress error in case voting fails for any keys * add account type and convert to unmarshalling json instead of parsing text from response * WIP parse delegation * fix test * nits * add make install * only vote with accounts that have a delegation * use keyring type to unpack keys output * fix output * Update CHANGELOG.md * fix test
- Loading branch information
1 parent
0e3d55b
commit 6e0470b
Showing
7 changed files
with
167 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
# ---------------------------------- | ||
# Installation | ||
install: | ||
@go install ./... | ||
|
||
|
||
# ---------------------------------- | ||
# Tests | ||
test: test-unit | ||
|
||
test-unit: | ||
go test -mod=readonly ./... | ||
@go test -mod=readonly ./... |
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 |
---|---|---|
@@ -1,36 +1,96 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"regexp" | ||
|
||
cryptokeyring "github.com/cosmos/cosmos-sdk/crypto/keyring" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
) | ||
|
||
// getKeys returns the list of keys from the current running local node | ||
func getKeys() ([]string, error) { | ||
out, err := executeShellCommand([]string{"keys", "list"}, evmosdHome, "", false) | ||
// Account is the type for a single account. | ||
type Account struct { | ||
Name string `json:"name"` | ||
Type string `json:"type"` | ||
Address string `json:"address"` | ||
PubKey string `json:"pubkey"` | ||
Delegations []stakingtypes.Delegation `json:"delegations"` | ||
} | ||
|
||
// getAccounts returns the list of keys from the current running local node | ||
func getAccounts() ([]Account, error) { | ||
out, err := executeShellCommand([]string{"keys", "list", "--output=json"}, evmosdHome, "", false, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
accounts, err := parseAccountsFromOut(out) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return parseKeysFromOut(out) | ||
return stakingAccounts(accounts) | ||
} | ||
|
||
func parseKeysFromOut(out string) ([]string, error) { | ||
// Define the regular expression pattern | ||
pattern := `\s+name:\s*(\w+)` | ||
// stakingAccounts filters the given list of accounts for those, which are used for staking. | ||
func stakingAccounts(accounts []Account) ([]Account, error) { | ||
var stakingAccs []Account | ||
|
||
for _, acc := range accounts { | ||
out, err := executeShellCommand([]string{"query", "staking", "delegations", acc.Address, "--output=json"}, evmosdHome, "", false, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
delegations, err := parseDelegationsFromResponse(out) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
acc.Delegations = delegations | ||
if len(delegations) > 0 { | ||
stakingAccs = append(stakingAccs, acc) | ||
} | ||
} | ||
|
||
return stakingAccs, nil | ||
} | ||
|
||
// Compile the regular expression | ||
re := regexp.MustCompile(pattern) | ||
// parseDelegationsFromResponse parses the delegations from the given response. | ||
func parseDelegationsFromResponse(out string) ([]stakingtypes.Delegation, error) { | ||
var res stakingtypes.QueryDelegatorDelegationsResponse | ||
err := cdc.UnmarshalJSON([]byte(out), &res) | ||
if err != nil { | ||
return nil, fmt.Errorf("error unmarshalling delegations: %w", err) | ||
} | ||
|
||
matches := re.FindAllStringSubmatch(out, -1) | ||
if len(matches) == 0 { | ||
return nil, fmt.Errorf("no keys found in output") | ||
var delegations = make([]stakingtypes.Delegation, len(res.DelegationResponses)) | ||
for i, delegation := range res.DelegationResponses { | ||
delegations[i] = delegation.Delegation | ||
} | ||
|
||
var keys []string | ||
for _, match := range matches { | ||
keys = append(keys, match[1]) | ||
return delegations, nil | ||
} | ||
|
||
// parseAccountsFromOut parses the keys from the given output from the keys list command. | ||
func parseAccountsFromOut(out string) ([]Account, error) { | ||
var ( | ||
accounts []Account | ||
keys []cryptokeyring.KeyOutput | ||
) | ||
|
||
err := json.Unmarshal([]byte(out), &keys) | ||
if err != nil { | ||
return nil, fmt.Errorf("error unmarshalling keys: %w", err) | ||
} | ||
|
||
return keys, nil | ||
for _, key := range keys { | ||
accounts = append(accounts, Account{ | ||
Name: key.Name, | ||
Type: key.Type, | ||
Address: key.Address, | ||
PubKey: key.PubKey, | ||
}) | ||
} | ||
return accounts, nil | ||
} |
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