-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into yurii/5807-collection-proposal-engine-refa…
…ctoring
- Loading branch information
Showing
19 changed files
with
582 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
"google.golang.org/grpc" | ||
|
||
sdk "github.com/onflow/flow-go-sdk" | ||
"github.com/onflow/flow-go-sdk/client" | ||
"github.com/onflow/flow-go/cmd" | ||
model "github.com/onflow/flow-go/model/bootstrap" | ||
"github.com/onflow/flow-go/module/epochs" | ||
) | ||
|
||
var ( | ||
flagAccessAPIAddress string | ||
) | ||
|
||
var checkMachineAccountCmd = &cobra.Command{ | ||
Use: "check-machine-account", | ||
Short: "checks a machine account configuration", | ||
Run: checkMachineAccountRun, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(checkMachineAccountCmd) | ||
|
||
checkMachineAccountCmd.Flags().StringVar(&flagAccessAPIAddress, "access-address", "", "network address of an Access Node") | ||
cmd.MarkFlagRequired(checkMachineAccountCmd, "access-address") | ||
} | ||
|
||
func checkMachineAccountRun(_ *cobra.Command, _ []string) { | ||
|
||
// read nodeID written to boostrap dir by `bootstrap key` | ||
nodeID, err := readNodeID() | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("could not read node id") | ||
} | ||
|
||
// read the private node information - used to get the role | ||
var nodeInfoPriv model.NodeInfoPriv | ||
readJSON(filepath.Join(flagOutdir, fmt.Sprintf(model.PathNodeInfoPriv, nodeID)), &nodeInfoPriv) | ||
|
||
// read the machine account info file | ||
machineAccountInfo := readMachineAccountInfo(nodeID) | ||
|
||
machineAccountPrivKey, err := machineAccountInfo.PrivateKey() | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("could not decode machine account private key") | ||
} | ||
|
||
// print public machine account info | ||
log.Debug(). | ||
Str("machine_account_address", machineAccountInfo.Address). | ||
Str("machine_account_pub_key", fmt.Sprintf("%x", encodedRuntimeAccountPubKey(machineAccountPrivKey))). | ||
Uint("key_index", machineAccountInfo.KeyIndex). | ||
Str("signing_algo", machineAccountInfo.SigningAlgorithm.String()). | ||
Str("hash_algo", machineAccountInfo.HashAlgorithm.String()). | ||
Msg("read machine account info from disk") | ||
|
||
flowClient, err := client.New(flagAccessAPIAddress, grpc.WithInsecure()) | ||
if err != nil { | ||
log.Fatal().Err(err).Msgf("could not connect to access API at address %s", flagAccessAPIAddress) | ||
} | ||
|
||
// retrieve the on-chain machine account info | ||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cancel() | ||
onChainAccount, err := flowClient.GetAccount(ctx, sdk.HexToAddress(machineAccountInfo.Address)) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("could not read account") | ||
} | ||
|
||
// check the local machine account config with the on-chain account | ||
// this will log non-critical warnings, and return an error for critical problems | ||
err = epochs.CheckMachineAccountInfo( | ||
log, | ||
nodeInfoPriv.Role, | ||
machineAccountInfo, | ||
onChainAccount, | ||
) | ||
if err != nil { | ||
log.Error().Err(err).Msg("⚠️ machine account is misconfigured") | ||
return | ||
} | ||
log.Info().Msg("🤖 machine account is configured correctly") | ||
} | ||
|
||
// readMachineAccountInfo reads the machine account info from disk | ||
func readMachineAccountInfo(nodeID string) model.NodeMachineAccountInfo { | ||
var machineAccountInfo model.NodeMachineAccountInfo | ||
|
||
path := filepath.Join(flagOutdir, fmt.Sprintf(model.PathNodeMachineAccountInfoPriv, nodeID)) | ||
readJSON(path, &machineAccountInfo) | ||
|
||
return machineAccountInfo | ||
} |
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
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,15 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// MarkFlagRequired marks a flag added to a cobra command as required. Panics | ||
// if the flag has not been added to the cobra command (indicates misconfiguration | ||
// or typo). | ||
func MarkFlagRequired(command *cobra.Command, flagName string) { | ||
err := command.MarkFlagRequired(flagName) | ||
if err != nil { | ||
panic("marked unknown flag as required: " + err.Error()) | ||
} | ||
} |
Oops, something went wrong.