Skip to content
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

Add orchestrator command to CLI #1801

Merged
merged 7 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ var (
// DefaultCLIHome is the default home directories for the application CLI
DefaultCLIHome = os.ExpandEnv("$HOME/.mesg-cli")

// DefaultNodeHome sets the folder where the applcation data and configuration will be stored
DefaultNodeHome = os.ExpandEnv("$HOME/.mesg/tendermint")
// DefaultNodeHome sets the folder where the application data and configuration will be stored
DefaultNodeHome = os.ExpandEnv("$HOME/.mesg-node")

// ModuleBasics The module BasicManager is in charge of setting up basic,
// non-dependant module elements, such as codec registration
Expand Down
2 changes: 2 additions & 0 deletions cmd/mesg-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func main() {
queryCmd(cdc),
txCmd(cdc),
flags.LineBreak,
orchestratorCmd(cdc),
flags.LineBreak,
lcd.ServeCommand(cdc, registerRoutes),
flags.LineBreak,
keys.Commands(),
Expand Down
141 changes: 141 additions & 0 deletions cmd/mesg-cli/orchestrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package main

import (
"fmt"
"os"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/mesg-foundation/engine/cosmos"
"github.com/mesg-foundation/engine/event/publisher"
"github.com/mesg-foundation/engine/ext/xsignal"
"github.com/mesg-foundation/engine/orchestrator"
"github.com/mesg-foundation/engine/server/grpc"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/log"
)

const (
accName = "orchestrator"
accPass = "password"
)

func orchestratorCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "orchestrator",
Short: "Orchestrator subcommands",
}
cmd.AddCommand(flags.GetCommands(
startOrchestratorCmd(cdc),
)...)
return cmd
}

func startOrchestratorCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "Start the Orchestrator",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

if viper.GetString(flagMnemonic) == "" {
return fmt.Errorf("mnemonic is required. use flag --mnemonic or config file")
}
if cliCtx.ChainID == "" {
return fmt.Errorf("chain-id is required. use flag --chain-id or config file")
}

logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "orchestrator")
client, err := cliCtx.GetNode()
if err != nil {
return err
}

// init rpc client
logger.Info("starting rpc client")
if err := client.Start(); err != nil {
return err
}
defer func() {
logger.Info("stopping rpc client")
if err := client.Stop(); err != nil {
logger.Error(err.Error())
}
}()

kb := cosmos.NewInMemoryKeybase()
if _, err := kb.CreateAccount(accName, viper.GetString(flagMnemonic), "", accPass, keys.CreateHDPath(viper.GetUint32(flagAccNumber), viper.GetUint32(flagAccIndex)).String(), cosmos.DefaultAlgo); err != nil {
fmt.Println("keybase error")
return err
}

// create rpc client
rpc, err := cosmos.NewRPC(client, cdc, kb, cliCtx.ChainID, accName, accPass, viper.GetString(flagGasPrices))
if err != nil {
return err
}

// init event publisher
ep := publisher.New(rpc)

// init gRPC server.
logger.Info("starting grpc server")
server := grpc.New(rpc, ep, viper.GetStringSlice(flagAuthorizedPubKeys))
defer func() {
logger.Info("stopping grpc server")
server.Close()
}()

go func() {
if err := server.Serve(viper.GetString(flagGrpcAddr)); err != nil {
logger.Error(err.Error())
panic(err)
}
}()

// orchestrator
logger.Info("starting orchestrator")
orch := orchestrator.New(rpc, ep, viper.GetString(flagExecPrice))
defer func() {
logger.Info("stopping orchestrator")
orch.Stop()
}()
go func() {
if err := orch.Start(); err != nil {
logger.Error(err.Error())
panic(err)
}
}()
go func() {
for err := range orch.ErrC {
logger.Error(err.Error())
}
}()

<-xsignal.WaitForInterrupt()

return nil
},
}
cmd.Flags().String(flagGrpcAddr, ":50052", "The address for the gRPC server to expose")
cmd.Flags().String(flagAuthorizedPubKeys, "", "The authorized pubkeys to communicate with the gRPC server")
cmd.Flags().String(flagMnemonic, "", "The account's mnemonic that will be used to sign transactions")
cmd.Flags().String(flagGasPrices, "1.0atto", "The gas price to sign tx")
cmd.Flags().String(flagExecPrice, "10000atto", "The execution price to create execution")
cmd.Flags().String(flagAccNumber, "0", "The account number of the hd path to use to derive the mnemonic")
cmd.Flags().String(flagAccIndex, "0", "The account index of the hd path to use to derive the mnemonic")
return cmd
}

const (
flagGrpcAddr = "grpc-addr"
flagAuthorizedPubKeys = "authorized-pubkeys"
flagMnemonic = "mnemonic"
flagGasPrices = "gas-prices"
flagExecPrice = "exec-price"
flagAccNumber = "acc-number"
flagAccIndex = "acc-index"
)
2 changes: 1 addition & 1 deletion cmd/mesg-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func main() {
server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators)

// prepare and add flags
executor := cli.PrepareBaseCmd(rootCmd, "AU", app.DefaultNodeHome)
executor := cli.PrepareBaseCmd(rootCmd, "MESG", app.DefaultNodeHome)
rootCmd.PersistentFlags().UintVar(&invCheckPeriod, flagInvCheckPeriod,
0, "Assert registered invariants every N blocks")
if err := executor.Execute(); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions cosmos/keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func NewKeybase(dir string) (*Keybase, error) {
}, nil
}

// NewInMemoryKeybase initializes a in memory keybase.
func NewInMemoryKeybase() *Keybase {
return &Keybase{
kb: clientkey.NewInMemoryKeyBase(),
privKeysCache: make(map[[sha256.Size]byte]crypto.PrivKey),
}
}

// NewMnemonic returns a new mnemonic phrase.
func (kb *Keybase) NewMnemonic() (string, error) {
// read entropy seed straight from crypto.Rand and convert to mnemonic
Expand Down
10 changes: 9 additions & 1 deletion ext/xstrings/strings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package xstrings

import "math/rand"
import (
"math/rand"

"github.com/mesg-foundation/engine/ext/xrand"
)

func init() {
xrand.SeedInit()
}

// SliceContains returns true if slice a contains e element, false otherwise.
func SliceContains(a []string, e string) bool {
Expand Down
5 changes: 5 additions & 0 deletions orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/mesg-foundation/engine/event"
"github.com/mesg-foundation/engine/event/publisher"
"github.com/mesg-foundation/engine/execution"
"github.com/mesg-foundation/engine/ext/xrand"
"github.com/mesg-foundation/engine/ext/xstrings"
"github.com/mesg-foundation/engine/hash"
"github.com/mesg-foundation/engine/process"
Expand All @@ -22,6 +23,10 @@ import (
"github.com/sirupsen/logrus"
)

func init() {
xrand.SeedInit()
}

// New creates a new Process instance
func New(rpc *cosmos.RPC, ep *publisher.EventPublisher, execPrice string) *Orchestrator {
return &Orchestrator{
Expand Down