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

New Runner gRPC API #1767

Merged
merged 12 commits into from
Apr 2, 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
2 changes: 2 additions & 0 deletions cmd/mesg-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func main() {
lcd.ServeCommand(cdc, registerRoutes),
flags.LineBreak,
keys.Commands(),
signCommand(),
verifySignCommand(),
flags.LineBreak,
version.Cmd,
flags.NewCompletionCmd(rootCmd, true),
Expand Down
76 changes: 76 additions & 0 deletions cmd/mesg-cli/sign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"bufio"
"encoding/hex"
"fmt"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func signCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "sign <name> <message>",
Short: "Sign the given message",
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return fmt.Errorf("must have exactly 2 args: name of the account to use and the message to sign")
}
buf := bufio.NewReader(cmd.InOrStdin())
kb, err := keys.NewKeyring(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), buf)
if err != nil {
return err
}
signature, _, err := kb.Sign(args[0], "", []byte(args[1]))
if err != nil {
return err
}
fmt.Println(hex.EncodeToString(signature))
return nil
},
}
cmd.PersistentFlags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|test)")
viper.BindPFlag(flags.FlagKeyringBackend, cmd.Flags().Lookup(flags.FlagKeyringBackend))
return cmd
}

func verifySignCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "verifySign <name> <message>",
Short: "Verify the signature of the given message",
Args: cobra.MinimumNArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 3 {
return fmt.Errorf("must have exactly 2 args: name of the account to use and the message used to sign, and the signature")
}
buf := bufio.NewReader(cmd.InOrStdin())
kb, err := keys.NewKeyring(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), buf)
if err != nil {
return err
}
acc, err := kb.Get(args[0])
if err != nil {
return err
}
signature, err := hex.DecodeString(args[2])
if err != nil {
return err
}
verify := acc.GetPubKey().VerifyBytes([]byte(args[1]), signature)
if verify {
fmt.Println("verification succeed")
} else {
fmt.Println("verification failed")
}
return nil
},
}
cmd.PersistentFlags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|test)")
viper.BindPFlag(flags.FlagKeyringBackend, cmd.Flags().Lookup(flags.FlagKeyringBackend))
return cmd
}
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

sdkcosmos "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/go-bip39"
"github.com/go-playground/validator/v10"
homedir "github.com/mitchellh/go-homedir"
"github.com/sirupsen/logrus"
tmconfig "github.com/tendermint/tendermint/config"
"gopkg.in/go-playground/validator.v9"
"gopkg.in/yaml.v2"
)

Expand Down
5 changes: 5 additions & 0 deletions cosmos/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func NewRPC(client rpcclient.Client, cdc *codec.Codec, kb keys.Keybase, chainID,
}, nil
}

// Codec returns the codec used by RPC.
func (c *RPC) Codec() *codec.Codec {
return c.cdc
}

// QueryJSON is abci.query wrapper with errors check and decode data.
func (c *RPC) QueryJSON(path string, qdata, ptr interface{}) error {
var data []byte
Expand Down
4 changes: 4 additions & 0 deletions e2e/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var (
kb *cosmos.Keybase
cfg *config.Config
engineAddress sdk.AccAddress
engineAccountName string
engineAccountPassword string
cont container.Container
ipfsEndpoint string
engineName string
Expand Down Expand Up @@ -75,6 +77,8 @@ func TestAPI(t *testing.T) {
acc, err := kb.CreateAccount(cfg.Account.Name, cfg.Account.Mnemonic, "", cfg.Account.Password, keys.CreateHDPath(cfg.Account.Number, cfg.Account.Index).String(), cosmos.DefaultAlgo)
require.NoError(t, err)
engineAddress = acc.GetAddress()
engineAccountName = cfg.Account.Name
engineAccountPassword = cfg.Account.Password
}

// init container
Expand Down
31 changes: 22 additions & 9 deletions e2e/complex_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/mesg-foundation/engine/protobuf/acknowledgement"
pb "github.com/mesg-foundation/engine/protobuf/api"
"github.com/mesg-foundation/engine/runner/builder"
grpcrunner "github.com/mesg-foundation/engine/server/grpc/runner"
"github.com/mesg-foundation/engine/service"
runnermodule "github.com/mesg-foundation/engine/x/runner"
runnerrest "github.com/mesg-foundation/engine/x/runner/client/rest"
Expand Down Expand Up @@ -58,19 +59,31 @@ func testComplexService(t *testing.T) {
require.NoError(t, err)
})

t.Run("start runner", func(t *testing.T) {
require.NoError(t, builder.Start(cont, testServiceComplexStruct, testInstanceComplexHash, testRunnerComplexHash, testServiceComplexImageHash, testInstanceComplexEnv, engineName, enginePort))
})

t.Run("register runner", func(t *testing.T) {
msg := runnermodule.MsgCreate{
Owner: engineAddress,
t.Run("create msg, sign it and inject into env", func(t *testing.T) {
value := grpcrunner.RegisterRequestPayload_Value{
ServiceHash: testServiceComplexHash,
EnvHash: testInstanceComplexEnvHash,
}
result, err := lcd.BroadcastMsg(msg)

encodedValue, err := cdc.MarshalJSON(value)
require.NoError(t, err)

signature, _, err := kb.Sign(engineAccountName, engineAccountPassword, encodedValue)
require.NoError(t, err)

payload := grpcrunner.RegisterRequestPayload{
Signature: signature,
Value: value,
}

encodedPayload, err := cdc.MarshalJSON(payload)
require.NoError(t, err)
require.True(t, testRunnerComplexHash.Equal(result))

testInstanceComplexEnv = append(testInstanceComplexEnv, "MESG_REGISTER_PAYLOAD="+string(encodedPayload))
})

t.Run("start runner", func(t *testing.T) {
require.NoError(t, builder.Start(cont, testServiceComplexStruct, testInstanceComplexHash, testRunnerComplexHash, testServiceComplexImageHash, testInstanceComplexEnv, engineName, enginePort))
})

stream, err := client.EventClient.Stream(context.Background(), &pb.StreamEventRequest{})
Expand Down
4 changes: 2 additions & 2 deletions e2e/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var testComplexCreateServiceMsg = &servicemodule.MsgCreate{
{Key: "resolve_dependence_ok"},
{Key: "resolve_dependence_error"},
},
Source: "QmSuVcdic2dhS5QKQGWp66SJQUkDRqAqCHpU6Sx9uXJcdc",
Source: "Qmeb4usAtvVuJmF9oh21P8pywC1o1eLrEn6dMvq1x7mHeq",
}

var testCreateServiceMsg = &servicemodule.MsgCreate{
Expand Down Expand Up @@ -183,5 +183,5 @@ var testCreateServiceMsg = &servicemodule.MsgCreate{
},
},
},
Source: "QmXcPDajWN55n1UPV5VNJDEKE96xJFJMe3X7GwN3qx8p7r",
Source: "QmYvof2HbuHn1BwajHCJeyqhkdmvdFerhLWxxDFFxhWgD3",
}
36 changes: 25 additions & 11 deletions e2e/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
pb "github.com/mesg-foundation/engine/protobuf/api"
"github.com/mesg-foundation/engine/runner"
"github.com/mesg-foundation/engine/runner/builder"
grpcrunner "github.com/mesg-foundation/engine/server/grpc/runner"
runnermodule "github.com/mesg-foundation/engine/x/runner"
runnerrest "github.com/mesg-foundation/engine/x/runner/client/rest"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -46,11 +47,29 @@ func testRunner(t *testing.T) {
require.NoError(t, err)
})

t.Run("start", func(t *testing.T) {
require.NoError(t, builder.Start(cont, testServiceStruct, testInstanceHash, testRunnerHash, testServiceImageHash, testInstanceEnv, engineName, enginePort))
t.Run("create msg, sign it and inject into env", func(t *testing.T) {
value := grpcrunner.RegisterRequestPayload_Value{
ServiceHash: testServiceHash,
EnvHash: testInstanceEnvHash,
}

encodedValue, err := cdc.MarshalJSON(value)
require.NoError(t, err)

signature, _, err := kb.Sign(engineAccountName, engineAccountPassword, encodedValue)
require.NoError(t, err)

payload := grpcrunner.RegisterRequestPayload{
Signature: signature,
Value: value,
}
encodedPayload, err := cdc.MarshalJSON(payload)
require.NoError(t, err)

testInstanceEnv = append(testInstanceEnv, "MESG_REGISTER_PAYLOAD="+string(encodedPayload))
})

t.Run("register", func(t *testing.T) {
t.Run("wait for service to be ready", func(t *testing.T) {
stream, err := client.EventClient.Stream(context.Background(), &pb.StreamEventRequest{
Filter: &pb.StreamEventRequest_Filter{
Key: "test_service_ready",
Expand All @@ -59,14 +78,9 @@ func testRunner(t *testing.T) {
require.NoError(t, err)
acknowledgement.WaitForStreamToBeReady(stream)

msg := runnermodule.MsgCreate{
Owner: engineAddress,
ServiceHash: testServiceHash,
EnvHash: testInstanceEnvHash,
}
result, err := lcd.BroadcastMsg(msg)
require.NoError(t, err)
require.True(t, testRunnerHash.Equal(result))
t.Run("start", func(t *testing.T) {
require.NoError(t, builder.Start(cont, testServiceStruct, testInstanceHash, testRunnerHash, testServiceImageHash, testInstanceEnv, engineName, enginePort))
})

// wait for service to be ready
_, err = stream.Recv()
Expand Down
4 changes: 2 additions & 2 deletions e2e/testdata/test-complex-service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module main
go 1.13

require (
github.com/mesg-foundation/engine v0.15.0
google.golang.org/grpc v1.24.0
github.com/mesg-foundation/engine v0.18.4-0.20200402055258-ff1bb6a08721
google.golang.org/grpc v1.28.0
)
Loading