-
Notifications
You must be signed in to change notification settings - Fork 185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple leader selection utility #7160
Open
jordanschalm
wants to merge
4
commits into
master
Choose a base branch
from
jord/leader-selection-util
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package leaders | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/onflow/flow-go/cmd" | ||
"github.com/onflow/flow-go/consensus/hotstuff/committees/leader" | ||
"github.com/onflow/flow-go/model/flow" | ||
"github.com/onflow/flow-go/state/protocol/inmem" | ||
) | ||
|
||
var ( | ||
flagStartView uint64 | ||
flagEndView uint64 | ||
) | ||
var Cmd = &cobra.Command{ | ||
Use: "leaders", | ||
Short: "Get leader selection for a view range.", | ||
Long: `Get leader selection for a view range in the current epoch for a provided snapshot. | ||
Expects a valid protocol state snapshot JSON to be piped into STDIN. Writes a JSON list of leaders for the given view range to STDOUT.`, | ||
Run: run, | ||
} | ||
|
||
func init() { | ||
|
||
Cmd.Flags().Uint64Var(&flagStartView, "start-view", 0, "the inclusive first view to get leader selection for") | ||
Cmd.Flags().Uint64Var(&flagEndView, "end-view", 0, "the inclusive last view to get leader selection for") | ||
cmd.MarkFlagRequired(Cmd, "start-view") | ||
cmd.MarkFlagRequired(Cmd, "end-view") | ||
} | ||
|
||
func run(*cobra.Command, []string) { | ||
|
||
var snapshot inmem.EncodableSnapshot | ||
err := json.NewDecoder(os.Stdin).Decode(&snapshot) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Failed to read snapshot from stdin") | ||
} | ||
|
||
snap := inmem.SnapshotFromEncodable(snapshot) | ||
epoch, err := snap.Epochs().Current() | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Failed to read current epoch") | ||
} | ||
|
||
// Should match https://github.com/onflow/flow-go/blob/48b6db32d4491903aa0ffa541377c8f239da3bcc/consensus/hotstuff/committees/consensus_committee.go#L74-L78 | ||
selection, err := leader.SelectionForConsensus( | ||
epoch.InitialIdentities(), | ||
epoch.RandomSource(), | ||
epoch.FirstView(), | ||
epoch.FinalView(), | ||
) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Failed to read current leader selection") | ||
} | ||
|
||
type LeaderForView struct { | ||
View uint64 | ||
LeaderID flow.Identifier | ||
} | ||
|
||
leaders := make([]LeaderForView, 0, flagEndView-flagStartView+1) | ||
for view := flagStartView; view <= flagEndView; view++ { | ||
leaderID, err := selection.LeaderForView(view) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Failed to read leader for view") | ||
} | ||
leaders = append(leaders, LeaderForView{View: view, LeaderID: leaderID}) | ||
} | ||
|
||
err = json.NewEncoder(os.Stdout).Encode(leaders) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Failed to encode leaders") | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the usage? something like
would it make sense to accept a path to the snapshot file as an argument instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is the usage. We were starting from some
jq
queries so the shortest path was something with the same input scheme. I don't mind adding the file IO in the command -- that is probably more consistent with the rest of the util commands.