From 6629a211574d8b31f8f4e8d3a3ab3cbe789ff5ac Mon Sep 17 00:00:00 2001 From: krhubert Date: Tue, 4 Feb 2020 17:46:01 +0100 Subject: [PATCH 01/87] Update cosmos-sdk to 0.38.0 + fix all api change errors --- cmd/mesg-cosmos-daemon/main.go | 8 ++--- cmd/mesg-cosmos/main.go | 7 ++-- config/config.go | 2 +- core/main.go | 4 +-- cosmos/client.go | 19 ++++------- cosmos/errors.go | 24 ++++++++------ cosmos/genesis.go | 13 ++++---- cosmos/genesis_test.go | 10 ++++-- cosmos/keybase.go | 35 ++++++++++++-------- cosmos/keybase_test.go | 6 ++-- cosmos/module.go | 25 ++++++--------- go.mod | 13 +++----- go.sum | 58 ++++++++++++++++++++++++++++++++++ sdk/execution/module.go | 4 +-- sdk/execution/msgs.go | 16 ++++++---- sdk/instance/module.go | 3 +- sdk/modules.go | 17 +++------- sdk/ownership/module.go | 4 +-- sdk/process/module.go | 4 +-- sdk/process/msgs.go | 15 +++++---- sdk/runner/module.go | 4 +-- sdk/runner/msgs.go | 19 +++++------ sdk/service/module.go | 4 +-- sdk/service/msgs.go | 7 ++-- 24 files changed, 190 insertions(+), 131 deletions(-) diff --git a/cmd/mesg-cosmos-daemon/main.go b/cmd/mesg-cosmos-daemon/main.go index 88f7f8f9f..c671604ac 100644 --- a/cmd/mesg-cosmos-daemon/main.go +++ b/cmd/mesg-cosmos-daemon/main.go @@ -7,8 +7,7 @@ import ( "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/types/module" - "github.com/cosmos/cosmos-sdk/x/genaccounts" - genaccscli "github.com/cosmos/cosmos-sdk/x/genaccounts/client/cli" + "github.com/cosmos/cosmos-sdk/x/auth" genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" "github.com/cosmos/cosmos-sdk/x/staking" "github.com/mesg-foundation/engine/codec" @@ -51,13 +50,12 @@ func main() { // CLI commands to initialize the chain rootCmd.AddCommand( genutilcli.InitCmd(ctx, cdc, basicManager, cfg.Tendermint.Config.RootDir), - genutilcli.CollectGenTxsCmd(ctx, cdc, genaccounts.AppModuleBasic{}, cfg.Tendermint.Config.RootDir), + genutilcli.CollectGenTxsCmd(ctx, cdc, auth.GenesisAccountIterator{}, cfg.Tendermint.Config.RootDir), genutilcli.GenTxCmd( ctx, cdc, basicManager, staking.AppModuleBasic{}, - genaccounts.AppModuleBasic{}, cfg.Tendermint.Config.RootDir, defaultCLIHome, + auth.GenesisAccountIterator{}, cfg.Tendermint.Config.RootDir, defaultCLIHome, ), genutilcli.ValidateGenesisCmd(ctx, cdc, basicManager), - genaccscli.AddGenesisAccountCmd(ctx, cdc, cfg.Tendermint.Config.RootDir, defaultCLIHome), ) server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators) diff --git a/cmd/mesg-cosmos/main.go b/cmd/mesg-cosmos/main.go index 058284ed4..c6babdfe8 100644 --- a/cmd/mesg-cosmos/main.go +++ b/cmd/mesg-cosmos/main.go @@ -5,6 +5,7 @@ import ( "path" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/client/lcd" "github.com/cosmos/cosmos-sdk/client/rpc" @@ -44,7 +45,7 @@ func main() { } // Add --chain-id to persistent flags and mark it required - rootCmd.PersistentFlags().String(client.FlagChainID, "", "Chain ID of tendermint node") + rootCmd.PersistentFlags().String(flags.FlagChainID, "", "Chain ID of tendermint node") rootCmd.PersistentPreRunE = func(_ *cobra.Command, _ []string) error { return initConfig(rootCmd) } @@ -58,7 +59,7 @@ func main() { lcd.ServeCommand(cdc, registerRoutes), keys.Commands(), version.Cmd, - client.NewCompletionCmd(rootCmd, true), + flags.NewCompletionCmd(rootCmd, true), ) executor := cli.PrepareMainCmd(rootCmd, "MESG", defaultCLIHome) @@ -125,7 +126,7 @@ func initConfig(cmd *cobra.Command) error { return err } } - if err := viper.BindPFlag(client.FlagChainID, cmd.PersistentFlags().Lookup(client.FlagChainID)); err != nil { + if err := viper.BindPFlag(flags.FlagChainID, cmd.PersistentFlags().Lookup(flags.FlagChainID)); err != nil { return err } if err := viper.BindPFlag(cli.EncodingFlag, cmd.PersistentFlags().Lookup(cli.EncodingFlag)); err != nil { diff --git a/config/config.go b/config/config.go index 949cc8621..6b1bf2036 100644 --- a/config/config.go +++ b/config/config.go @@ -114,7 +114,7 @@ func defaultConfig() (*Config, error) { c.Cosmos.StakeTokenDenom = "atto" c.Cosmos.Bech32MainPrefix = "mesgtest" c.Cosmos.CoinType = 470 - c.Cosmos.FullFundraiserPath = "44'/470'/0'/0/0" + c.Cosmos.FullFundraiserPath = "44'/470'/0'/0/0" // TODO: is it really useful? c.Cosmos.PowerReduction = 18 c.DevGenesis.ChainID = "mesg-dev-chain" diff --git a/core/main.go b/core/main.go index c9216b880..ad9dfe1e2 100644 --- a/core/main.go +++ b/core/main.go @@ -69,7 +69,7 @@ func stopRunningServices(sdk *enginesdk.SDK, address string) error { func loadOrGenConfigAccount(kb *cosmos.Keybase, cfg *config.Config) (keys.Info, error) { if cfg.Account.Mnemonic != "" { logrus.WithField("module", "main").Warn("Config account mnemonic presents. Generating account with it...") - return kb.CreateAccount(cfg.Account.Name, cfg.Account.Mnemonic, "", cfg.Account.Password, cfg.Account.Number, cfg.Account.Index) + return kb.CreateAccount(cfg.Account.Name, cfg.Account.Mnemonic, "", cfg.Account.Password, keys.CreateHDPath(cfg.Account.Number, cfg.Account.Index).String(), cosmos.DefaultAlgo) } exist, err := kb.Exist(cfg.Account.Name) @@ -89,7 +89,7 @@ func loadOrGenConfigAccount(kb *cosmos.Keybase, cfg *config.Config) (keys.Info, "password": cfg.Account.Password, "mnemonic": mnemonic, }).Warn("Account") - return kb.CreateAccount(cfg.Account.Name, mnemonic, "", cfg.Account.Password, cfg.Account.Number, cfg.Account.Index) + return kb.CreateAccount(cfg.Account.Name, mnemonic, "", cfg.Account.Password, keys.CreateHDPath(cfg.Account.Number, cfg.Account.Index).String(), cosmos.DefaultAlgo) } func loadOrGenDevGenesis(basicManager *module.BasicManager, kb *cosmos.Keybase, cfg *config.Config) (*tmtypes.GenesisDoc, error) { diff --git a/cosmos/client.go b/cosmos/client.go index dc0df00b2..9e7015929 100644 --- a/cosmos/client.go +++ b/cosmos/client.go @@ -12,13 +12,13 @@ import ( sdktypes "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" authutils "github.com/cosmos/cosmos-sdk/x/auth/client/utils" + authExported "github.com/cosmos/cosmos-sdk/x/auth/exported" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/x/xreflect" "github.com/mesg-foundation/engine/x/xstrings" abci "github.com/tendermint/tendermint/abci/types" - tmquery "github.com/tendermint/tendermint/libs/pubsub/query" "github.com/tendermint/tendermint/node" rpcclient "github.com/tendermint/tendermint/rpc/client" tenderminttypes "github.com/tendermint/tendermint/types" @@ -34,7 +34,7 @@ type Client struct { minGasPrices sdktypes.DecCoins // Local state - acc auth.Account + acc authExported.Account getAccountMutex sync.Mutex broadcastMutex sync.Mutex } @@ -136,11 +136,7 @@ func (c *Client) BuildAndBroadcastMsg(msg sdktypes.Msg) (*abci.ResponseDeliverTx // Stream subscribes to the provided query and returns the hash of the matching ressources. func (c *Client) Stream(ctx context.Context, query string) (chan hash.Hash, chan error, error) { subscriber := xstrings.RandASCIILetters(8) - q, err := tmquery.New(query) - if err != nil { - return nil, nil, err - } - msgStream, err := c.EventBus.SubscribeUnbuffered(ctx, subscriber, q) + eventStream, err := c.Subscribe(ctx, subscriber, query, 0) if err != nil { return nil, nil, err } @@ -150,8 +146,8 @@ func (c *Client) Stream(ctx context.Context, query string) (chan hash.Hash, chan loop: for { select { - case msg := <-msgStream.Out(): - attrs := msg.Events()[EventHashType] + case event := <-eventStream: + attrs := event.Events[EventHashType] // The following error might be too much as MAYBE if one transaction contains many messages, the events will be merged across the whole transaction if len(attrs) != 1 { errC <- fmt.Errorf("event %s has %d tag(s), but only 1 is expected", EventHashType, len(attrs)) @@ -164,9 +160,6 @@ func (c *Client) Stream(ctx context.Context, query string) (chan hash.Hash, chan hashC <- hash } } - case <-msgStream.Cancelled(): - errC <- msgStream.Err() - break loop case <-ctx.Done(): break loop } @@ -179,7 +172,7 @@ func (c *Client) Stream(ctx context.Context, query string) (chan hash.Hash, chan } // GetAccount returns the local account. -func (c *Client) GetAccount() (auth.Account, error) { +func (c *Client) GetAccount() (authExported.Account, error) { c.getAccountMutex.Lock() defer c.getAccountMutex.Unlock() if c.acc == nil { diff --git a/cosmos/errors.go b/cosmos/errors.go index 5df27462e..2b98ae1d3 100644 --- a/cosmos/errors.go +++ b/cosmos/errors.go @@ -1,26 +1,30 @@ package cosmos import ( - "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) const ( // CodespaceMesg is a cosmos codespace for all mesg errors. - CodespaceMesg types.CodespaceType = "mesg" + CodespaceMesg = "mesg" ) // Base mesg codes. const ( - CodeInternal types.CodeType = 1000 - CodeValidation types.CodeType = 2000 + CodeInternal uint32 = 1000 + CodeValidation uint32 = 2000 + CodeMarshal uint32 = 2001 + CodeUnmarshal uint32 = 2002 ) -// NewMesgErrorf creates error with given code type and mesg codespace. -func NewMesgErrorf(ct types.CodeType, format string, a ...interface{}) types.Error { - return types.NewError(CodespaceMesg, ct, format, a...) -} +// mesg errors +var ( + ErrValidation = sdkerrors.Register(CodespaceMesg, CodeValidation, "validation failed") + ErrMarshal = sdkerrors.Register(CodespaceMesg, CodeMarshal, "failed to marshal") // TODO: to replace by cosmoserrors.ErrJSONMarshal if it makes sense + ErrUnmarshal = sdkerrors.Register(CodespaceMesg, CodeUnmarshal, "failed to unmarshal") // TODO: to replace by cosmoserrors.ErrJSONUnmarshal if it makes sense +) // NewMesgWrapError creates error with given code type and mesg codespace. -func NewMesgWrapError(ct types.CodeType, err error) types.Error { - return types.NewError(CodespaceMesg, ct, err.Error()) +func NewMesgWrapError(code uint32, err error) *sdkerrors.Error { + return sdkerrors.New(CodespaceMesg, code, err.Error()) } diff --git a/cosmos/genesis.go b/cosmos/genesis.go index 566a24dda..d2fe43be4 100644 --- a/cosmos/genesis.go +++ b/cosmos/genesis.go @@ -7,8 +7,8 @@ import ( sdktypes "github.com/cosmos/cosmos-sdk/types" authutils "github.com/cosmos/cosmos-sdk/x/auth/client/utils" + authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - "github.com/cosmos/cosmos-sdk/x/genaccounts" "github.com/cosmos/cosmos-sdk/x/genutil" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/mesg-foundation/engine/codec" @@ -126,23 +126,24 @@ func genGenesisDoc(appState map[string]json.RawMessage, chainID string, genesisT } func genGenesisAppState(defaultGenesisŚtate map[string]json.RawMessage, signedStdTx authtypes.StdTx, initialBalances string) (map[string]json.RawMessage, error) { - genAccs := []genaccounts.GenesisAccount{} - for _, signer := range signedStdTx.GetSigners() { + genAccs := authexported.GenesisAccounts{} + pubkeys := signedStdTx.GetPubKeys() + for i, signer := range signedStdTx.GetSigners() { initialB, err := sdktypes.ParseCoins(initialBalances) if err != nil { return nil, err } - genAcc := genaccounts.NewGenesisAccountRaw(signer, initialB, sdktypes.NewCoins(), 0, 0, "", "") + genAcc := authtypes.NewBaseAccount(signer, initialB, pubkeys[i], 0, 0) if err := genAcc.Validate(); err != nil { return nil, err } genAccs = append(genAccs, genAcc) } - genstate, err := codec.MarshalJSON(genaccounts.GenesisState(genAccs)) + genstate, err := codec.MarshalJSON(authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)) if err != nil { return nil, err } - defaultGenesisŚtate[genaccounts.ModuleName] = genstate + defaultGenesisŚtate[authtypes.ModuleName] = genstate return genutil.SetGenTxsInAppGenesisState(codec.Codec, defaultGenesisŚtate, []authtypes.StdTx{signedStdTx}) } diff --git a/cosmos/genesis_test.go b/cosmos/genesis_test.go index 173ab76eb..dceb6b04e 100644 --- a/cosmos/genesis_test.go +++ b/cosmos/genesis_test.go @@ -7,20 +7,24 @@ import ( "path/filepath" "testing" + "github.com/cosmos/cosmos-sdk/crypto/keys" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/mesg-foundation/engine/codec" "github.com/stretchr/testify/require" ) func TestGenesis(t *testing.T) { + // codec + authtypes.RegisterCodec(codec.Codec) + stakingtypes.RegisterCodec(codec.Codec) + // path path, _ := ioutil.TempDir("", "TestGenesis") defer os.RemoveAll(path) // keybase kb, err := NewKeybase(filepath.Join(path, "kb")) require.NoError(t, err) - // codec - stakingtypes.RegisterCodec(codec.Codec) // variables var ( chainID = "test-chainID" @@ -37,7 +41,7 @@ func TestGenesis(t *testing.T) { ) // init account mnemonic, _ := kb.NewMnemonic() - kb.CreateAccount(name, mnemonic, "", password, 0, 0) + kb.CreateAccount(name, mnemonic, "", password, keys.CreateHDPath(0, 0).String(), DefaultAlgo) // start tests t.Run("generate validator", func(t *testing.T) { v, err := NewGenesisValidator(kb, name, password, privValidatorKeyFile, privValidatorStateFile, nodeKeyFile) diff --git a/cosmos/keybase.go b/cosmos/keybase.go index f85c73013..120b6d275 100644 --- a/cosmos/keybase.go +++ b/cosmos/keybase.go @@ -6,14 +6,16 @@ import ( clientkey "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/crypto/keys" - "github.com/cosmos/cosmos-sdk/crypto/keys/hd" "github.com/cosmos/cosmos-sdk/crypto/keys/keyerror" "github.com/cosmos/cosmos-sdk/types" bip39 "github.com/cosmos/go-bip39" "github.com/tendermint/tendermint/crypto" ) -const mnemonicEntropySize = 256 +const ( + mnemonicEntropySize = 256 + DefaultAlgo = keys.Secp256k1 +) // Keybase is a standard cosmos keybase. type Keybase struct { @@ -111,17 +113,10 @@ func (kb *Keybase) CreateMnemonic(name string, language keys.Language, passwd st } // CreateAccount is a lock protected version of keys.CreateAccount -func (kb *Keybase) CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd string, account, index uint32) (keys.Info, error) { +func (kb *Keybase) CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd, hdPath string, algo keys.SigningAlgo) (keys.Info, error) { kb.mx.Lock() defer kb.mx.Unlock() - return kb.kb.CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd, account, index) -} - -// Derive is a lock protected version of keys.Derive -func (kb *Keybase) Derive(name, mnemonic, bip39Passwd, encryptPasswd string, params hd.BIP44Params) (keys.Info, error) { - kb.mx.Lock() - defer kb.mx.Unlock() - return kb.kb.Derive(name, mnemonic, bip39Passwd, encryptPasswd, params) + return kb.kb.CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd, hdPath, algo) } // CreateLedger is a lock protected version of keys.CreateLedger @@ -132,10 +127,10 @@ func (kb *Keybase) CreateLedger(name string, algo keys.SigningAlgo, hrp string, } // CreateOffline is a lock protected version of keys.CreateOffline -func (kb *Keybase) CreateOffline(name string, pubkey crypto.PubKey) (keys.Info, error) { +func (kb *Keybase) CreateOffline(name string, pubkey crypto.PubKey, algo keys.SigningAlgo) (keys.Info, error) { kb.mx.Lock() defer kb.mx.Unlock() - return kb.kb.CreateOffline(name, pubkey) + return kb.kb.CreateOffline(name, pubkey, algo) } // CreateMulti is a lock protected version of keys.CreateMulti @@ -201,6 +196,20 @@ func (kb *Keybase) ExportPrivateKeyObject(name string, passphrase string) (crypt return kb.kb.ExportPrivateKeyObject(name, passphrase) } +// SupportedAlgos returns a list of signing algorithms supported by the keybase +func (kb *Keybase) SupportedAlgos() []keys.SigningAlgo { + kb.mx.Lock() + defer kb.mx.Unlock() + return kb.kb.SupportedAlgos() +} + +// SupportedAlgosLedger returns a list of signing algorithms supported by the keybase's ledger integration +func (kb *Keybase) SupportedAlgosLedger() []keys.SigningAlgo { + kb.mx.Lock() + defer kb.mx.Unlock() + return kb.kb.SupportedAlgosLedger() +} + // CloseDB is a lock protected version of keys.CloseDB func (kb *Keybase) CloseDB() { kb.mx.Lock() diff --git a/cosmos/keybase_test.go b/cosmos/keybase_test.go index 8691a3841..052634517 100644 --- a/cosmos/keybase_test.go +++ b/cosmos/keybase_test.go @@ -6,6 +6,7 @@ import ( "os" "testing" + "github.com/cosmos/cosmos-sdk/crypto/keys" "github.com/stretchr/testify/require" ) @@ -27,7 +28,7 @@ func TestKeybase(t *testing.T) { }) t.Run("Create", func(t *testing.T) { - acc, err := kb.CreateAccount(name, mnemonic, "", password, 0, 0) + acc, err := kb.CreateAccount(name, mnemonic, "", password, keys.CreateHDPath(0, 0).String(), DefaultAlgo) require.NoError(t, err) require.Equal(t, name, acc.GetName()) }) @@ -45,7 +46,8 @@ func TestKeybase(t *testing.T) { t.Run("Sign", func(t *testing.T) { name2 := "name2" mnemonic2, _ := kb.NewMnemonic() - kb.CreateAccount(name2, mnemonic2, "", password, 0, 0) + _, err := kb.CreateAccount(name2, mnemonic2, "", password, keys.CreateHDPath(0, 0).String(), DefaultAlgo) + require.NoError(t, err) hash := sha256.Sum256([]byte(name + ":" + password)) hash2 := sha256.Sum256([]byte(name2 + ":" + password)) for i := 0; i < 1000; i++ { diff --git a/cosmos/module.go b/cosmos/module.go index d80596ff8..81cd8d77a 100644 --- a/cosmos/module.go +++ b/cosmos/module.go @@ -6,6 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" cosmoscodec "github.com/cosmos/cosmos-sdk/codec" cosmostypes "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/module" "github.com/gorilla/mux" "github.com/mesg-foundation/engine/codec" @@ -103,15 +104,12 @@ func (m *AppModule) Route() string { } // NewHandler returns the handler used to apply transactions. -func (m *AppModule) NewHandler() cosmostypes.Handler { - return func(request cosmostypes.Request, msg cosmostypes.Msg) cosmostypes.Result { +func (m AppModule) NewHandler() cosmostypes.Handler { + return func(request cosmostypes.Request, msg cosmostypes.Msg) (*cosmostypes.Result, error) { request = request.WithEventManager(cosmostypes.NewEventManager()) hash, err := m.handler(request, msg) if err != nil { - if errsdk, ok := err.(cosmostypes.Error); ok { - return errsdk.Result() - } - return cosmostypes.ErrInternal(err.Error()).Result() + return nil, err } event := cosmostypes.NewEvent( @@ -125,10 +123,10 @@ func (m *AppModule) NewHandler() cosmostypes.Handler { } request.EventManager().EmitEvent(event) - return cosmostypes.Result{ + return &cosmostypes.Result{ Data: hash, Events: request.EventManager().Events(), - } + }, nil } } @@ -138,18 +136,15 @@ func (m *AppModule) QuerierRoute() string { } // NewQuerierHandler returns the handler used to reply ABCI query. -func (m *AppModule) NewQuerierHandler() cosmostypes.Querier { - return func(request cosmostypes.Request, path []string, req abci.RequestQuery) ([]byte, cosmostypes.Error) { +func (m AppModule) NewQuerierHandler() cosmostypes.Querier { + return func(request cosmostypes.Request, path []string, req abci.RequestQuery) ([]byte, error) { data, err := m.querier(request, path, req) if err != nil { - if errsdk, ok := err.(cosmostypes.Error); ok { - return nil, errsdk - } - return nil, NewMesgWrapError(CodeInternal, err) + return nil, err } res, err := codec.MarshalBinaryBare(data) if err != nil { - return nil, NewMesgWrapError(CodeInternal, err) + return nil, sdkerrors.Wrap(ErrMarshal, err.Error()) } return res, nil } diff --git a/go.mod b/go.mod index dc918e22e..10f60ab64 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/btcsuite/btcd v0.0.0-20190807005414-4063feeff79a // indirect github.com/containerd/containerd v1.3.0 // indirect github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc // indirect - github.com/cosmos/cosmos-sdk v0.37.6 + github.com/cosmos/cosmos-sdk v0.38.0 github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d github.com/cskr/pubsub v1.0.2 github.com/docker/cli v0.0.0-20191011045415-5d85cdacd257 @@ -35,7 +35,6 @@ require ( github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect github.com/leodido/go-urn v1.1.0 // indirect github.com/lyft/protoc-gen-validate v0.1.0 // indirect - github.com/mattn/go-isatty v0.0.8 // indirect github.com/mitchellh/go-homedir v1.1.0 github.com/morikuni/aec v1.0.0 // indirect github.com/mr-tron/base58 v1.1.3 @@ -45,23 +44,21 @@ require ( github.com/opencontainers/go-digest v1.0.0-rc1 // indirect github.com/opencontainers/image-spec v1.0.1 // indirect github.com/opencontainers/runc v0.1.1 // indirect - github.com/pelletier/go-toml v1.4.0 // indirect github.com/prometheus/client_golang v1.1.0 github.com/pseudomuto/protoc-gen-doc v1.3.0 github.com/pseudomuto/protokit v0.2.0 // indirect - github.com/rakyll/statik v0.1.6 // indirect github.com/rcrowley/go-metrics v0.0.0-20190706150252-9beb055b7962 // indirect github.com/sirupsen/logrus v1.4.2 github.com/spf13/afero v1.2.2 // indirect github.com/spf13/cobra v0.0.5 github.com/spf13/viper v1.6.2 - github.com/stretchr/objx v0.2.0 // indirect github.com/stretchr/testify v1.4.0 + github.com/stumble/gorocksdb v0.0.3 // indirect github.com/tendermint/go-amino v0.15.1 - github.com/tendermint/tendermint v0.32.9 - github.com/tendermint/tm-db v0.2.0 + github.com/tendermint/tendermint v0.33.0 + github.com/tendermint/tm-db v0.4.0 github.com/vektra/mockery v0.0.0-20181123154057-e78b021dcbb5 - golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 + golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413 golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 // indirect golang.org/x/tools v0.0.0-20190813142322-97f12d73768f // indirect google.golang.org/grpc v1.27.0 diff --git a/go.sum b/go.sum index 39aacbaee..19f9b8d45 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,12 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/99designs/keyring v1.1.3 h1:mEV3iyZWjkxQ7R8ia8GcG97vCX5zQQ7n4o8R2BylwQY= +github.com/99designs/keyring v1.1.3/go.mod h1:657DQuMrBZRtuL/voxVyiyb6zpMehlm5vLB9Qwrv904= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/ChainSafe/go-schnorrkel v0.0.0-20200102211924-4bcbc698314f h1:4O1om+UVU+Hfcihr1timk8YNXHxzZWgCo7ofnrZRApw= +github.com/ChainSafe/go-schnorrkel v0.0.0-20200102211924-4bcbc698314f/go.mod h1:URdX5+vg25ts3aCh8H5IFZybJYKWhJHYMTnf+ULtoC4= github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg= github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= github.com/Masterminds/semver v1.4.2 h1:WBLTQ37jOCzSLtXNdoo8bNM8876KhNqOKvrlGITgsTc= @@ -58,19 +62,27 @@ github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8Nz github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= +github.com/cosmos/cosmos-sdk v0.37.4 h1:1ioXxkpiS+wOgaUbROeDIyuF7hciU5nti0TSyBmV2Ok= +github.com/cosmos/cosmos-sdk v0.37.4/go.mod h1:Axr+Q+G2Ffduxt4zMA6KwxxvyVSKPB9+nXZVPKgpC2c= github.com/cosmos/cosmos-sdk v0.37.6 h1:l7urou9aTPqW/x3Hq+SAWn6OHDzjma+1OVi0TT+O36M= github.com/cosmos/cosmos-sdk v0.37.6/go.mod h1:63Y8V75rRDzUrtRUwy+5cBOy5mr6xytI11PBk53tBrE= +github.com/cosmos/cosmos-sdk v0.38.0 h1:BrflLMrECI2ZfftRAq2iAlxlyk+W/4iKVCNCC3a+RPc= +github.com/cosmos/cosmos-sdk v0.38.0/go.mod h1:9ZZex0GKpyNCvilvVAPBoB+0n3A/aO1+/UhPVEaiCy4= github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8 h1:Iwin12wRQtyZhH6FV3ykFcdGNlYEzoeR0jN8Vn+JWsI= github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y= github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d h1:49RLWk1j44Xu4fjHb6JFYmeUnDORVwHNkDxaQ0ctCVU= github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y= github.com/cosmos/ledger-cosmos-go v0.10.3 h1:Qhi5yTR5Pg1CaTpd00pxlGwNl4sFRdtK1J96OTjeFFc= github.com/cosmos/ledger-cosmos-go v0.10.3/go.mod h1:J8//BsAGTo3OC/vDLjMRFLW6q0WAaXvHnVc7ZmE8iUY= +github.com/cosmos/ledger-cosmos-go v0.11.1 h1:9JIYsGnXP613pb2vPjFeMMjBI5lEDsEaF6oYorTy6J4= +github.com/cosmos/ledger-cosmos-go v0.11.1/go.mod h1:J8//BsAGTo3OC/vDLjMRFLW6q0WAaXvHnVc7ZmE8iUY= github.com/cosmos/ledger-go v0.9.2 h1:Nnao/dLwaVTk1Q5U9THldpUMMXU94BOTWPddSmVB6pI= github.com/cosmos/ledger-go v0.9.2/go.mod h1:oZJ2hHAZROdlHiwTg4t7kP+GKIIkBT+o6c9QWFanOyI= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/cskr/pubsub v1.0.2 h1:vlOzMhl6PFn60gRlTQQsIfVwaPB/B/8MziK8FhEPt/0= github.com/cskr/pubsub v1.0.2/go.mod h1:/8MzYXk/NJAz782G8RPkFzXTZVu63VotefPnR9TIRis= +github.com/danieljoos/wincred v1.0.2 h1:zf4bhty2iLuwgjgpraD2E9UbvO+fe54XXGJbOwe23fU= +github.com/danieljoos/wincred v1.0.2/go.mod h1:SnuYRW9lp1oJrZX/dXJqr0cPK5gYXqx3EJbmjhLdK9U= github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -88,6 +100,8 @@ github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKoh github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/dvsekhvalnov/jose2go v0.0.0-20180829124132-7f401d37b68a h1:mq+R6XEM6lJX5VlLyZIrUSP8tSuJp82xTK89hvBwJbU= +github.com/dvsekhvalnov/jose2go v0.0.0-20180829124132-7f401d37b68a/go.mod h1:7BvyPhdbLxMXIYTFPLsyJRFMsKmOZnQmzh6Gb+uquuM= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0 h1:EQciDnbrYxy13PgWoY8AqoxGiPrpgBZ1R8UNe3ddc+A= @@ -117,12 +131,16 @@ github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2 github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0 h1:MP4Eh7ZCb31lleYCFuwm0oe4/YGak+5l1vA2NOE80nA= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-logfmt/logfmt v0.5.0 h1:TrB8swr/68K7m9CcGut2g3UOihhbcbiMAYiuTXdEih4= +github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0= +github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= @@ -176,6 +194,14 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.2.0/go.mod h1:mJzapYve32yjrKlk9G github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= +github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU= +github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0= +github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f h1:8N8XWLZelZNibkhM1FuF+3Ad3YIbgirjdMiVA0eUkaM= +github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s= +github.com/gtank/ristretto255 v0.1.2 h1:JEqUCPA1NvLq5DwYtuzigd7ss8fwbYay9fi4/5uMzcc= +github.com/gtank/ristretto255 v0.1.2/go.mod h1:Ph5OpO6c7xKUGROZfWVLiJf9icMDwUeIvY4OmlYW69o= +github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= +github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= @@ -196,6 +222,8 @@ github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d h1:Z+RDyXzjKE0i2sTjZ/b1uxiGtPhFy34Ou/Tk0qwN0kM= +github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d/go.mod h1:JJNrCn9otv/2QP4D7SMJBgaleKpOf66PnW6F5WGNRIc= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= @@ -224,8 +252,12 @@ github.com/mattn/go-isatty v0.0.6 h1:SrwhHcpV4nWrMGdNcC2kXpMfcBVYGDuTArqyhocJgvA github.com/mattn/go-isatty v0.0.6/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 h1:hLDRPB66XQT/8+wG9WsDpiCvZf1yKO7sz7scAjSlBa0= +github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= @@ -260,9 +292,13 @@ github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181 github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.4.0 h1:u3Z1r+oOXJIkxqw34zVhyPgjBsm6X2wn21NWs/HfSeg= github.com/pelletier/go-toml v1.4.0/go.mod h1:PN7xzY2wHTK0K9p34ErDQMlFxa51Fk0OUruD3k1mMwo= +github.com/pelletier/go-toml v1.6.0 h1:aetoXYr0Tv7xRU/V4B4IZJ2QcbtMUFoNb3ORp7TzIK4= +github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= @@ -358,10 +394,16 @@ github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/syndtr/goleveldb v1.0.1-0.20190318030020-c3a204f8e965 h1:1oFLiOyVl+W7bnBzGhf7BbIv9loSFQcieWWYIjLqcAw= github.com/syndtr/goleveldb v1.0.1-0.20190318030020-c3a204f8e965/go.mod h1:9OrXJhf154huy1nPWmuSrkgjPUtUNhA+Zmy+6AESzuA= +github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d h1:gZZadD8H+fF+n9CmNhYL1Y0dJB+kLOmKd7FbPJLeGHs= +github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d/go.mod h1:9OrXJhf154huy1nPWmuSrkgjPUtUNhA+Zmy+6AESzuA= +github.com/tecbot/gorocksdb v0.0.0-20191017175515-d217d93fd4c5 h1:gVwAW5OwaZlDB5/CfqcGFM9p9C+KxvQKyNOltQ8orj0= +github.com/tecbot/gorocksdb v0.0.0-20191017175515-d217d93fd4c5/go.mod h1:ahpPrc7HpcfEWDQRZEmnXMzHY03mLDYMCxeDzy46i+8= github.com/tendermint/btcd v0.1.1 h1:0VcxPfflS2zZ3RiOAHkBiFUcPvbtRj5O7zHmcJWHV7s= github.com/tendermint/btcd v0.1.1/go.mod h1:DC6/m53jtQzr/NFmMNEu0rxf18/ktVoVtMrnDD5pN+U= github.com/tendermint/crypto v0.0.0-20180820045704-3764759f34a5 h1:u8i49c+BxloX3XQ55cvzFNXplizZP/q00i+IlttUjAU= github.com/tendermint/crypto v0.0.0-20180820045704-3764759f34a5/go.mod h1:z4YtwM70uOnk8h0pjJYlj3zdYwi9l03By6iAIF5j/Pk= +github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 h1:hqAk8riJvK4RMWx1aInLzndwxKalgi5rTqgfXxOxbEI= +github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15/go.mod h1:z4YtwM70uOnk8h0pjJYlj3zdYwi9l03By6iAIF5j/Pk= github.com/tendermint/go-amino v0.14.1/go.mod h1:i/UKE5Uocn+argJJBb12qTZsCDBcAYMbR92AaJVmKso= github.com/tendermint/go-amino v0.15.0 h1:TC4e66P59W7ML9+bxio17CPKnxW3nKIRAYskntMAoRk= github.com/tendermint/go-amino v0.15.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME= @@ -369,14 +411,24 @@ github.com/tendermint/go-amino v0.15.1 h1:D2uk35eT4iTsvJd9jWIetzthE5C0/k2QmMFkCN github.com/tendermint/go-amino v0.15.1/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME= github.com/tendermint/iavl v0.12.4 h1:hd1woxUGISKkfUWBA4mmmTwOua6PQZTJM/F0FDrmMV8= github.com/tendermint/iavl v0.12.4/go.mod h1:8LHakzt8/0G3/I8FUU0ReNx98S/EP6eyPJkAUvEXT/o= +github.com/tendermint/iavl v0.13.0 h1:r2sINvNFlJsLlLhGoqlqPlREWYkuK26BvMfkBt+XQnA= +github.com/tendermint/iavl v0.13.0/go.mod h1:7nSUPdrsHEZ2nNZa+9gaIrcJciWd1jCQZXtcyARU82k= github.com/tendermint/tendermint v0.32.1 h1:J8ddXMbCmG6GZjdCl/N1wgdXDU9uO91J2Y5CA9xYfGo= github.com/tendermint/tendermint v0.32.1/go.mod h1:jmPDAKuNkev9793/ivn/fTBnfpA9mGBww8MPRNPNxnU= +github.com/tendermint/tendermint v0.32.7 h1:Szu5Fm1L3pvn3t4uQxPAcP+7ndZEQKgLie/yokM56rU= +github.com/tendermint/tendermint v0.32.7/go.mod h1:D2+A3pNjY+Po72X0mTfaXorFhiVI8dh/Zg640FGyGtE= +github.com/tendermint/tendermint v0.32.8 h1:eOaLJGRi5x/Rb23fiVsxq9c5fZ/6O5QplExlGjNPDVI= +github.com/tendermint/tendermint v0.32.8/go.mod h1:5/B1XZjNYtVBso8o1l/Eg4A0Mhu42lDcmftoQl95j/E= github.com/tendermint/tendermint v0.32.9 h1:++dR46xpBq/yfQx2c5KyrZmb15p2jw9Q5iEtTB82d8s= github.com/tendermint/tendermint v0.32.9/go.mod h1:5/B1XZjNYtVBso8o1l/Eg4A0Mhu42lDcmftoQl95j/E= +github.com/tendermint/tendermint v0.33.0 h1:TW1g9sQs3YSqKM8o1+opL3/VmBy4Ke/VKV9MxYpqNbI= +github.com/tendermint/tendermint v0.33.0/go.mod h1:s5UoymnPIY+GcA3mMte4P9gpMP8vS7UH7HBXikT1pHI= github.com/tendermint/tm-db v0.1.1 h1:G3Xezy3sOk9+ekhjZ/kjArYIs1SmwV+1OUgNkj7RgV0= github.com/tendermint/tm-db v0.1.1/go.mod h1:0cPKWu2Mou3IlxecH+MEUSYc1Ch537alLe6CpFrKzgw= github.com/tendermint/tm-db v0.2.0 h1:rJxgdqn6fIiVJZy4zLpY1qVlyD0TU6vhkT4kEf71TQQ= github.com/tendermint/tm-db v0.2.0/go.mod h1:0cPKWu2Mou3IlxecH+MEUSYc1Ch537alLe6CpFrKzgw= +github.com/tendermint/tm-db v0.4.0 h1:iPbCcLbf4nwDFhS39Zo1lpdS1X/cT9CkTlUx17FHQgA= +github.com/tendermint/tm-db v0.4.0/go.mod h1:+Cwhgowrf7NBGXmsqFMbwEtbo80XmyrlY5Jsk95JubQ= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= @@ -400,6 +452,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc= golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413 h1:ULYEB3JvPRE/IfO+9uO7vKV/xzVTO7XPAwm8xbf4w2g= +golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= @@ -439,9 +493,12 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= @@ -479,6 +536,7 @@ google.golang.org/grpc v1.22.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyac google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1 h1:wdKvqQk7IttEw92GoRyKG2IDrUIpgpj6H6m81yfeMW0= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= diff --git a/sdk/execution/module.go b/sdk/execution/module.go index 5ee7daba1..af8821c07 100644 --- a/sdk/execution/module.go +++ b/sdk/execution/module.go @@ -4,6 +4,7 @@ import ( "fmt" cosmostypes "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/module" "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/hash" @@ -34,8 +35,7 @@ func handler(k *Keeper) cosmos.Handler { } return exec.Hash, nil default: - errmsg := fmt.Sprintf("Unrecognized execution Msg type: %v", msg.Type()) - return nil, cosmostypes.ErrUnknownRequest(errmsg) + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, fmt.Sprintf("Unrecognized execution Msg type: %v", msg.Type())) } } } diff --git a/sdk/execution/msgs.go b/sdk/execution/msgs.go index 3a4086d3d..8a72432f1 100644 --- a/sdk/execution/msgs.go +++ b/sdk/execution/msgs.go @@ -2,7 +2,9 @@ package executionsdk import ( cosmostypes "github.com/cosmos/cosmos-sdk/types" + cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/mesg-foundation/engine/codec" + "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/x/xvalidator" ) @@ -35,18 +37,18 @@ func (msg msgCreateExecution) Type() string { } // ValidateBasic runs stateless checks on the message. -func (msg msgCreateExecution) ValidateBasic() cosmostypes.Error { +func (msg msgCreateExecution) ValidateBasic() error { if err := xvalidator.Validate.Struct(msg); err != nil { - return cosmostypes.ErrInternal(err.Error()) + return err } if !msg.Request.ParentHash.IsZero() && !msg.Request.EventHash.IsZero() { - return cosmostypes.ErrInternal("cannot have both parent and event hash") + return cosmoserrors.Wrap(cosmos.ErrValidation, "cannot have both parent and event hash") } if msg.Request.ParentHash.IsZero() && msg.Request.EventHash.IsZero() { - return cosmostypes.ErrInternal("should have at least an event hash or parent hash") + return cosmoserrors.Wrap(cosmos.ErrValidation, "should have at least an event hash or parent hash") } if msg.Request.ExecutorHash.IsZero() { - return cosmostypes.ErrInternal("should have a executor hash") + return cosmoserrors.Wrap(cosmos.ErrValidation, "should have a executor hash") } return nil } @@ -86,9 +88,9 @@ func (msg msgUpdateExecution) Type() string { } // ValidateBasic runs stateless checks on the message. -func (msg msgUpdateExecution) ValidateBasic() cosmostypes.Error { +func (msg msgUpdateExecution) ValidateBasic() error { if msg.Executor.Empty() { - return cosmostypes.ErrInvalidAddress("executor is missing") + return cosmoserrors.Wrap(cosmoserrors.ErrInvalidAddress, "executor is missing") } return nil } diff --git a/sdk/instance/module.go b/sdk/instance/module.go index bae6f11f7..db691c72f 100644 --- a/sdk/instance/module.go +++ b/sdk/instance/module.go @@ -4,6 +4,7 @@ import ( "fmt" cosmostypes "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/module" "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/cosmos" @@ -23,7 +24,7 @@ func NewModule(k *Keeper) module.AppModule { func handler(k *Keeper) cosmos.Handler { return func(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { errmsg := fmt.Sprintf("Unrecognized instance Msg type: %v", msg.Type()) - return nil, cosmostypes.ErrUnknownRequest(errmsg) + return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errmsg) } } diff --git a/sdk/modules.go b/sdk/modules.go index 521679853..88d411b7d 100644 --- a/sdk/modules.go +++ b/sdk/modules.go @@ -9,7 +9,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/bank" "github.com/cosmos/cosmos-sdk/x/distribution" - "github.com/cosmos/cosmos-sdk/x/genaccounts" "github.com/cosmos/cosmos-sdk/x/genutil" "github.com/cosmos/cosmos-sdk/x/params" "github.com/cosmos/cosmos-sdk/x/slashing" @@ -32,7 +31,6 @@ import ( func NewBasicManager() *module.BasicManager { basicManager := module.NewBasicManager( params.AppModuleBasic{}, - genaccounts.AppModuleBasic{}, genutil.AppModuleBasic{}, auth.AppModuleBasic{}, bank.AppModuleBasic{}, @@ -89,8 +87,8 @@ func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, er codec.Codec, paramsStoreKey, paramsTStoreKey, - params.DefaultCodespace, ) + accountKeeper := auth.NewAccountKeeper( codec.Codec, paramsStoreKey, @@ -100,7 +98,6 @@ func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, er bankKeeper := bank.NewBaseKeeper( accountKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), - bank.DefaultCodespace, modAccAddrs, ) supplyKeeper := supply.NewKeeper( @@ -113,10 +110,8 @@ func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, er stakingKeeper := staking.NewKeeper( codec.Codec, stakingStoreKey, - stakingTStoreKey, supplyKeeper, paramsKeeper.Subspace(staking.DefaultParamspace), - staking.DefaultCodespace, ) distrKeeper := distribution.NewKeeper( codec.Codec, @@ -124,7 +119,6 @@ func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, er paramsKeeper.Subspace(distribution.DefaultParamspace), &stakingKeeper, supplyKeeper, - distribution.DefaultCodespace, auth.FeeCollectorName, modAccAddrs, ) @@ -133,7 +127,6 @@ func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, er slashingStoreKey, &stakingKeeper, paramsKeeper.Subspace(slashing.DefaultParamspace), - slashing.DefaultCodespace, ) stakingKeeper = *stakingKeeper.SetHooks( staking.NewMultiStakingHooks( @@ -154,14 +147,13 @@ func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, er // init module manager manager := module.NewManager( - genaccounts.NewAppModule(accountKeeper), genutil.NewAppModule(accountKeeper, stakingKeeper, app.DeliverTx), auth.NewAppModule(accountKeeper), bank.NewAppModule(bankKeeper, accountKeeper), supply.NewAppModule(supplyKeeper, accountKeeper), - distribution.NewAppModule(distrKeeper, supplyKeeper), - slashing.NewAppModule(slashingKeeper, stakingKeeper), - staking.NewAppModule(stakingKeeper, distrKeeper, accountKeeper, supplyKeeper), + distribution.NewAppModule(distrKeeper, accountKeeper, supplyKeeper, stakingKeeper), + slashing.NewAppModule(slashingKeeper, accountKeeper, stakingKeeper), + staking.NewAppModule(stakingKeeper, accountKeeper, supplyKeeper), ownershipsdk.NewModule(ownershipKeeper), servicesdk.NewModule(serviceKeeper), @@ -173,7 +165,6 @@ func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, er manager.SetOrderBeginBlockers(distribution.ModuleName, slashing.ModuleName) manager.SetOrderEndBlockers(staking.ModuleName) manager.SetOrderInitGenesis( - genaccounts.ModuleName, distribution.ModuleName, staking.ModuleName, auth.ModuleName, diff --git a/sdk/ownership/module.go b/sdk/ownership/module.go index 7bd38e07b..5ed26b8e8 100644 --- a/sdk/ownership/module.go +++ b/sdk/ownership/module.go @@ -4,6 +4,7 @@ import ( "fmt" cosmostypes "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/module" "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/hash" @@ -20,8 +21,7 @@ func NewModule(k *Keeper) module.AppModule { func handler(k *Keeper) cosmos.Handler { return func(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { - errmsg := fmt.Sprintf("Unrecognized ownership Msg type: %v", msg.Type()) - return nil, cosmostypes.ErrUnknownRequest(errmsg) + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "Unrecognized ownership Msg type: %v", msg.Type()) } } diff --git a/sdk/process/module.go b/sdk/process/module.go index 679547497..3d2cb92bf 100644 --- a/sdk/process/module.go +++ b/sdk/process/module.go @@ -4,6 +4,7 @@ import ( "fmt" cosmostypes "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/module" "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/hash" @@ -30,8 +31,7 @@ func handler(k *Keeper) cosmos.Handler { case msgDeleteProcess: return nil, k.Delete(request, &msg) default: - errmsg := fmt.Sprintf("unrecognized process msg type: %v", msg.Type()) - return nil, cosmostypes.ErrUnknownRequest(errmsg) + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized process msg type: %v", msg.Type()) } } } diff --git a/sdk/process/msgs.go b/sdk/process/msgs.go index e8780dfb1..73c053236 100644 --- a/sdk/process/msgs.go +++ b/sdk/process/msgs.go @@ -2,6 +2,7 @@ package processsdk import ( cosmostypes "github.com/cosmos/cosmos-sdk/types" + cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/process" @@ -34,12 +35,12 @@ func (msg msgCreateProcess) Type() string { } // ValidateBasic runs stateless checks on the message. -func (msg msgCreateProcess) ValidateBasic() cosmostypes.Error { +func (msg msgCreateProcess) ValidateBasic() error { if msg.Owner.Empty() { - return cosmostypes.ErrInvalidAddress("owner is missing") + return cosmoserrors.Wrap(cosmoserrors.ErrInvalidAddress, "owner is missing") } if err := xvalidator.Validate.Struct(msg); err != nil { - return cosmostypes.ErrInternal(err.Error()) + return err } p := &process.Process{ Name: msg.Request.Name, @@ -48,7 +49,7 @@ func (msg msgCreateProcess) ValidateBasic() cosmostypes.Error { } p.Hash = hash.Dump(p) if err := p.Validate(); err != nil { - return cosmostypes.ErrInternal(err.Error()) + return err } return nil } @@ -88,12 +89,12 @@ func (msg msgDeleteProcess) Type() string { } // ValidateBasic runs stateless checks on the message. -func (msg msgDeleteProcess) ValidateBasic() cosmostypes.Error { +func (msg msgDeleteProcess) ValidateBasic() error { if msg.Owner.Empty() { - return cosmostypes.ErrInvalidAddress("owner is missing") + return cosmoserrors.Wrap(cosmoserrors.ErrInvalidAddress, "owner is missing") } if err := xvalidator.Validate.Struct(msg); err != nil { - return cosmostypes.ErrInternal(err.Error()) + return err } return nil } diff --git a/sdk/runner/module.go b/sdk/runner/module.go index 26633935a..d4ba60573 100644 --- a/sdk/runner/module.go +++ b/sdk/runner/module.go @@ -4,6 +4,7 @@ import ( "fmt" cosmostypes "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/module" "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/hash" @@ -33,8 +34,7 @@ func handler(k *Keeper) cosmos.Handler { } return nil, nil default: - errmsg := fmt.Sprintf("Unrecognized runner Msg type: %v", msg.Type()) - return nil, cosmostypes.ErrUnknownRequest(errmsg) + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, fmt.Sprintf("Unrecognized runner Msg type: %v", msg.Type())) } } } diff --git a/sdk/runner/msgs.go b/sdk/runner/msgs.go index d1bc428bd..9e9be3f43 100644 --- a/sdk/runner/msgs.go +++ b/sdk/runner/msgs.go @@ -2,6 +2,7 @@ package runnersdk import ( cosmostypes "github.com/cosmos/cosmos-sdk/types" + cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/hash" @@ -35,18 +36,18 @@ func (msg msgCreateRunner) Type() string { } // ValidateBasic runs stateless checks on the message. -func (msg msgCreateRunner) ValidateBasic() cosmostypes.Error { +func (msg msgCreateRunner) ValidateBasic() error { if err := xvalidator.Validate.Struct(msg); err != nil { - return cosmostypes.ErrInternal(err.Error()) + return err } if msg.ServiceHash.IsZero() { - return cosmos.NewMesgErrorf(cosmos.CodeValidation, "serviceHash is missing") + return cosmoserrors.Wrap(cosmos.ErrValidation, "serviceHash is missing") } if msg.EnvHash.IsZero() { - return cosmos.NewMesgErrorf(cosmos.CodeValidation, "envHash is missing") + return cosmoserrors.Wrap(cosmos.ErrValidation, "envHash is missing") } if msg.Address.Empty() { - return cosmostypes.ErrInvalidAddress("address is missing") + return cosmoserrors.Wrap(cosmoserrors.ErrInvalidAddress, "address is missing") } return nil } @@ -86,15 +87,15 @@ func (msg msgDeleteRunner) Type() string { } // ValidateBasic runs stateless checks on the message. -func (msg msgDeleteRunner) ValidateBasic() cosmostypes.Error { +func (msg msgDeleteRunner) ValidateBasic() error { if err := xvalidator.Validate.Struct(msg); err != nil { - return cosmostypes.ErrInternal(err.Error()) + return err } if msg.RunnerHash.IsZero() { - return cosmos.NewMesgErrorf(cosmos.CodeValidation, "runnerHash is missing") + return cosmoserrors.Wrap(cosmos.ErrValidation, "runnerHash is missing") } if msg.Address.Empty() { - return cosmostypes.ErrInvalidAddress("address is missing") + return cosmoserrors.Wrap(cosmoserrors.ErrInvalidAddress, "address is missing") } return nil } diff --git a/sdk/service/module.go b/sdk/service/module.go index 08a941dea..aac85aaf8 100644 --- a/sdk/service/module.go +++ b/sdk/service/module.go @@ -4,6 +4,7 @@ import ( "fmt" cosmostypes "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/module" "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/cosmos" @@ -30,8 +31,7 @@ func handler(k *Keeper) cosmos.Handler { } return srv.Hash, nil default: - errmsg := fmt.Sprintf("Unrecognized service Msg type: %v", msg.Type()) - return nil, cosmostypes.ErrUnknownRequest(errmsg) + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "Unrecognized service Msg type: %v", msg.Type()) } } } diff --git a/sdk/service/msgs.go b/sdk/service/msgs.go index ae8751b66..2749b75d4 100644 --- a/sdk/service/msgs.go +++ b/sdk/service/msgs.go @@ -2,6 +2,7 @@ package servicesdk import ( cosmostypes "github.com/cosmos/cosmos-sdk/types" + cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/x/xvalidator" @@ -32,12 +33,12 @@ func (msg msgCreateService) Type() string { } // ValidateBasic runs stateless checks on the message. -func (msg msgCreateService) ValidateBasic() cosmostypes.Error { +func (msg msgCreateService) ValidateBasic() error { if err := xvalidator.Validate.Struct(msg); err != nil { - return cosmostypes.ErrInternal(err.Error()) + return err } if msg.Owner.Empty() { - return cosmostypes.ErrInvalidAddress("owner is missing") + return cosmoserrors.Wrap(cosmoserrors.ErrInvalidAddress, "owner is missing") } return nil } From 1694492730a8cd3f8c196dee1d3fdf9b236b8a16 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 6 Feb 2020 09:30:26 +0700 Subject: [PATCH 02/87] go mod tidy --- go.mod | 1 - go.sum | 63 ---------------------------------------------------------- 2 files changed, 64 deletions(-) diff --git a/go.mod b/go.mod index 10f60ab64..e0c33487e 100644 --- a/go.mod +++ b/go.mod @@ -53,7 +53,6 @@ require ( github.com/spf13/cobra v0.0.5 github.com/spf13/viper v1.6.2 github.com/stretchr/testify v1.4.0 - github.com/stumble/gorocksdb v0.0.3 // indirect github.com/tendermint/go-amino v0.15.1 github.com/tendermint/tendermint v0.33.0 github.com/tendermint/tm-db v0.4.0 diff --git a/go.sum b/go.sum index 19f9b8d45..ee9c26cd7 100644 --- a/go.sum +++ b/go.sum @@ -62,18 +62,10 @@ github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8Nz github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= -github.com/cosmos/cosmos-sdk v0.37.4 h1:1ioXxkpiS+wOgaUbROeDIyuF7hciU5nti0TSyBmV2Ok= -github.com/cosmos/cosmos-sdk v0.37.4/go.mod h1:Axr+Q+G2Ffduxt4zMA6KwxxvyVSKPB9+nXZVPKgpC2c= -github.com/cosmos/cosmos-sdk v0.37.6 h1:l7urou9aTPqW/x3Hq+SAWn6OHDzjma+1OVi0TT+O36M= -github.com/cosmos/cosmos-sdk v0.37.6/go.mod h1:63Y8V75rRDzUrtRUwy+5cBOy5mr6xytI11PBk53tBrE= github.com/cosmos/cosmos-sdk v0.38.0 h1:BrflLMrECI2ZfftRAq2iAlxlyk+W/4iKVCNCC3a+RPc= github.com/cosmos/cosmos-sdk v0.38.0/go.mod h1:9ZZex0GKpyNCvilvVAPBoB+0n3A/aO1+/UhPVEaiCy4= -github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8 h1:Iwin12wRQtyZhH6FV3ykFcdGNlYEzoeR0jN8Vn+JWsI= -github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y= github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d h1:49RLWk1j44Xu4fjHb6JFYmeUnDORVwHNkDxaQ0ctCVU= github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y= -github.com/cosmos/ledger-cosmos-go v0.10.3 h1:Qhi5yTR5Pg1CaTpd00pxlGwNl4sFRdtK1J96OTjeFFc= -github.com/cosmos/ledger-cosmos-go v0.10.3/go.mod h1:J8//BsAGTo3OC/vDLjMRFLW6q0WAaXvHnVc7ZmE8iUY= github.com/cosmos/ledger-cosmos-go v0.11.1 h1:9JIYsGnXP613pb2vPjFeMMjBI5lEDsEaF6oYorTy6J4= github.com/cosmos/ledger-cosmos-go v0.11.1/go.mod h1:J8//BsAGTo3OC/vDLjMRFLW6q0WAaXvHnVc7ZmE8iUY= github.com/cosmos/ledger-go v0.9.2 h1:Nnao/dLwaVTk1Q5U9THldpUMMXU94BOTWPddSmVB6pI= @@ -102,11 +94,9 @@ github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/dvsekhvalnov/jose2go v0.0.0-20180829124132-7f401d37b68a h1:mq+R6XEM6lJX5VlLyZIrUSP8tSuJp82xTK89hvBwJbU= github.com/dvsekhvalnov/jose2go v0.0.0-20180829124132-7f401d37b68a/go.mod h1:7BvyPhdbLxMXIYTFPLsyJRFMsKmOZnQmzh6Gb+uquuM= -github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0 h1:EQciDnbrYxy13PgWoY8AqoxGiPrpgBZ1R8UNe3ddc+A= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/etcd-io/bbolt v1.3.2/go.mod h1:ZF2nL25h33cCyBtcyWeZ2/I3HQOfTP+0PIEvHjkjCrw= github.com/etcd-io/bbolt v1.3.3 h1:gSJmxrs37LgTqR/oyJBWok6k6SvXEUerFTbltIhXkBM= github.com/etcd-io/bbolt v1.3.3/go.mod h1:ZF2nL25h33cCyBtcyWeZ2/I3HQOfTP+0PIEvHjkjCrw= github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51 h1:0JZ+dUmQeA8IIVUMzysrX4/AKuQwWhV2dYQuPZdvdSQ= @@ -115,7 +105,6 @@ github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 h1:JWuenKqqX8nojt github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg= github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870 h1:E2s37DuLxFhQDg5gKsWoLBOB0n+ZW8s599zru8FJ2/Y= github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0= -github.com/fortytw2/leaktest v1.2.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw= github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= @@ -123,7 +112,6 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-bindata/go-bindata v0.0.0-20181025070752-41975c0ccc30 h1:QCzzs6MtdCqGT1U1t6Ar/WFH9UmPKYCtMutthIC0XVM= github.com/go-bindata/go-bindata v0.0.0-20181025070752-41975c0ccc30/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo= -github.com/go-kit/kit v0.6.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.8.0 h1:Wz+5lgoB0kkuqLEc6NVmwRknTKP6dTGbSqvhZtBI/j0= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.9.0 h1:wDJmvq38kDhkVxi50ni9ykkdUr1PKgqKOoi01fa0Mdk= @@ -177,18 +165,14 @@ github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= -github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= -github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 h1:Iju5GlWwrvL6UBg4zJJt3btmonfrMlCDdsejg4CZE7c= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= -github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 h1:THDBEeQ9xZ8JEaCLyLQqXMMdRqNr0QAUJTIkQAUtFjg= -github.com/grpc-ecosystem/go-grpc-middleware v1.1.0/go.mod h1:f5nM7jw/oeRSadq3xCzHAvxcr8HZnzsqU6ILg/0NiiE= github.com/grpc-ecosystem/go-grpc-middleware v1.2.0 h1:0IKlLyQ3Hs9nDaiK5cSHAGmcQEIC8l2Ts1u6x5Dfrqg= github.com/grpc-ecosystem/go-grpc-middleware v1.2.0/go.mod h1:mJzapYve32yjrKlk9GbyCZHuPgZsrbyIbyKhSzOpg6s= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho= @@ -248,10 +232,6 @@ github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDe github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= -github.com/mattn/go-isatty v0.0.6 h1:SrwhHcpV4nWrMGdNcC2kXpMfcBVYGDuTArqyhocJgvA= -github.com/mattn/go-isatty v0.0.6/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE= -github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= @@ -290,8 +270,6 @@ github.com/opencontainers/runc v0.1.1/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59P github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= -github.com/pelletier/go-toml v1.4.0 h1:u3Z1r+oOXJIkxqw34zVhyPgjBsm6X2wn21NWs/HfSeg= -github.com/pelletier/go-toml v1.4.0/go.mod h1:PN7xzY2wHTK0K9p34ErDQMlFxa51Fk0OUruD3k1mMwo= github.com/pelletier/go-toml v1.6.0 h1:aetoXYr0Tv7xRU/V4B4IZJ2QcbtMUFoNb3ORp7TzIK4= github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -312,7 +290,6 @@ github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/prometheus/common v0.0.0-20181020173914-7e9e6cabbd39/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= @@ -328,7 +305,6 @@ github.com/pseudomuto/protoc-gen-doc v1.3.0 h1:wpwmaSCWY2lGwkzAxAaqYcGyoklZjZmeX github.com/pseudomuto/protoc-gen-doc v1.3.0/go.mod h1:fwtQAY9erXp3mC92O8OTECnDlJT2r0Ff4KSEKbGEmy0= github.com/pseudomuto/protokit v0.2.0 h1:hlnBDcy3YEDXH7kc9gV+NLaN0cDzhDvD1s7Y6FZ8RpM= github.com/pseudomuto/protokit v0.2.0/go.mod h1:2PdH30hxVHsup8KpBTOXTBeMVhJZVio3Q8ViKSAXT0Q= -github.com/rakyll/statik v0.1.5/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs= github.com/rakyll/statik v0.1.6 h1:uICcfUXpgqtw2VopbIncslhAmE5hwc4g20TEyEENBNs= github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs= github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165 h1:nkcn14uNmFEuGCb2mBZbBb24RdNRL08b/wb+xBOYpuk= @@ -336,8 +312,6 @@ github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165/go.mod h1:bCqn github.com/rcrowley/go-metrics v0.0.0-20190706150252-9beb055b7962 h1:eUm8ma4+yPknhXtkYlWh3tMkE6gBjXZToDned9s2gbQ= github.com/rcrowley/go-metrics v0.0.0-20190706150252-9beb055b7962/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= -github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI= -github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= @@ -369,10 +343,8 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.0.0/go.mod h1:A8kyI5cUJhb8N+3pkfONlcEcZbueH6nhAm0Fq7SrnBM= github.com/spf13/viper v1.3.2 h1:VUFqw5KcqRf7i70GOzW7N+Q7+gxVBkSSqiXB12+JQ4M= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= -github.com/spf13/viper v1.5.0/go.mod h1:AkYRkVJF8TkSG/xet6PzXX+l39KhhXa2pdqVSxnTcn4= github.com/spf13/viper v1.6.1 h1:VPZzIkznI1YhVMRi6vNFLHSwhnhReBfgTxIPccpfdZk= github.com/spf13/viper v1.6.1/go.mod h1:t3iDnF5Jlj76alVNuyFBk5oUMCvsrkbvZK0WQdfDi5k= github.com/spf13/viper v1.6.2 h1:7aKfF+e8/k68gda3LOjo5RxiUqddoFxVq4BKBPrxk5E= @@ -388,45 +360,23 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stumble/gorocksdb v0.0.3 h1:9UU+QA1pqFYJuf9+5p7z1IqdE5k0mma4UAeu2wmX8kA= -github.com/stumble/gorocksdb v0.0.3/go.mod h1:v6IHdFBXk5DJ1K4FZ0xi+eY737quiiBxYtSWXadLybY= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= -github.com/syndtr/goleveldb v1.0.1-0.20190318030020-c3a204f8e965 h1:1oFLiOyVl+W7bnBzGhf7BbIv9loSFQcieWWYIjLqcAw= -github.com/syndtr/goleveldb v1.0.1-0.20190318030020-c3a204f8e965/go.mod h1:9OrXJhf154huy1nPWmuSrkgjPUtUNhA+Zmy+6AESzuA= github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d h1:gZZadD8H+fF+n9CmNhYL1Y0dJB+kLOmKd7FbPJLeGHs= github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d/go.mod h1:9OrXJhf154huy1nPWmuSrkgjPUtUNhA+Zmy+6AESzuA= github.com/tecbot/gorocksdb v0.0.0-20191017175515-d217d93fd4c5 h1:gVwAW5OwaZlDB5/CfqcGFM9p9C+KxvQKyNOltQ8orj0= github.com/tecbot/gorocksdb v0.0.0-20191017175515-d217d93fd4c5/go.mod h1:ahpPrc7HpcfEWDQRZEmnXMzHY03mLDYMCxeDzy46i+8= github.com/tendermint/btcd v0.1.1 h1:0VcxPfflS2zZ3RiOAHkBiFUcPvbtRj5O7zHmcJWHV7s= github.com/tendermint/btcd v0.1.1/go.mod h1:DC6/m53jtQzr/NFmMNEu0rxf18/ktVoVtMrnDD5pN+U= -github.com/tendermint/crypto v0.0.0-20180820045704-3764759f34a5 h1:u8i49c+BxloX3XQ55cvzFNXplizZP/q00i+IlttUjAU= -github.com/tendermint/crypto v0.0.0-20180820045704-3764759f34a5/go.mod h1:z4YtwM70uOnk8h0pjJYlj3zdYwi9l03By6iAIF5j/Pk= github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 h1:hqAk8riJvK4RMWx1aInLzndwxKalgi5rTqgfXxOxbEI= github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15/go.mod h1:z4YtwM70uOnk8h0pjJYlj3zdYwi9l03By6iAIF5j/Pk= github.com/tendermint/go-amino v0.14.1/go.mod h1:i/UKE5Uocn+argJJBb12qTZsCDBcAYMbR92AaJVmKso= -github.com/tendermint/go-amino v0.15.0 h1:TC4e66P59W7ML9+bxio17CPKnxW3nKIRAYskntMAoRk= -github.com/tendermint/go-amino v0.15.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME= github.com/tendermint/go-amino v0.15.1 h1:D2uk35eT4iTsvJd9jWIetzthE5C0/k2QmMFkCN+4JgQ= github.com/tendermint/go-amino v0.15.1/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME= -github.com/tendermint/iavl v0.12.4 h1:hd1woxUGISKkfUWBA4mmmTwOua6PQZTJM/F0FDrmMV8= -github.com/tendermint/iavl v0.12.4/go.mod h1:8LHakzt8/0G3/I8FUU0ReNx98S/EP6eyPJkAUvEXT/o= github.com/tendermint/iavl v0.13.0 h1:r2sINvNFlJsLlLhGoqlqPlREWYkuK26BvMfkBt+XQnA= github.com/tendermint/iavl v0.13.0/go.mod h1:7nSUPdrsHEZ2nNZa+9gaIrcJciWd1jCQZXtcyARU82k= -github.com/tendermint/tendermint v0.32.1 h1:J8ddXMbCmG6GZjdCl/N1wgdXDU9uO91J2Y5CA9xYfGo= -github.com/tendermint/tendermint v0.32.1/go.mod h1:jmPDAKuNkev9793/ivn/fTBnfpA9mGBww8MPRNPNxnU= -github.com/tendermint/tendermint v0.32.7 h1:Szu5Fm1L3pvn3t4uQxPAcP+7ndZEQKgLie/yokM56rU= -github.com/tendermint/tendermint v0.32.7/go.mod h1:D2+A3pNjY+Po72X0mTfaXorFhiVI8dh/Zg640FGyGtE= -github.com/tendermint/tendermint v0.32.8 h1:eOaLJGRi5x/Rb23fiVsxq9c5fZ/6O5QplExlGjNPDVI= -github.com/tendermint/tendermint v0.32.8/go.mod h1:5/B1XZjNYtVBso8o1l/Eg4A0Mhu42lDcmftoQl95j/E= -github.com/tendermint/tendermint v0.32.9 h1:++dR46xpBq/yfQx2c5KyrZmb15p2jw9Q5iEtTB82d8s= -github.com/tendermint/tendermint v0.32.9/go.mod h1:5/B1XZjNYtVBso8o1l/Eg4A0Mhu42lDcmftoQl95j/E= github.com/tendermint/tendermint v0.33.0 h1:TW1g9sQs3YSqKM8o1+opL3/VmBy4Ke/VKV9MxYpqNbI= github.com/tendermint/tendermint v0.33.0/go.mod h1:s5UoymnPIY+GcA3mMte4P9gpMP8vS7UH7HBXikT1pHI= -github.com/tendermint/tm-db v0.1.1 h1:G3Xezy3sOk9+ekhjZ/kjArYIs1SmwV+1OUgNkj7RgV0= -github.com/tendermint/tm-db v0.1.1/go.mod h1:0cPKWu2Mou3IlxecH+MEUSYc1Ch537alLe6CpFrKzgw= -github.com/tendermint/tm-db v0.2.0 h1:rJxgdqn6fIiVJZy4zLpY1qVlyD0TU6vhkT4kEf71TQQ= -github.com/tendermint/tm-db v0.2.0/go.mod h1:0cPKWu2Mou3IlxecH+MEUSYc1Ch537alLe6CpFrKzgw= github.com/tendermint/tm-db v0.4.0 h1:iPbCcLbf4nwDFhS39Zo1lpdS1X/cT9CkTlUx17FHQgA= github.com/tendermint/tm-db v0.4.0/go.mod h1:+Cwhgowrf7NBGXmsqFMbwEtbo80XmyrlY5Jsk95JubQ= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= @@ -447,9 +397,7 @@ go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/crypto v0.0.0-20190228161510-8dd112bcdc25/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc= golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413 h1:ULYEB3JvPRE/IfO+9uO7vKV/xzVTO7XPAwm8xbf4w2g= @@ -490,7 +438,6 @@ golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -524,18 +471,12 @@ google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9Ywl google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= -google.golang.org/genproto v0.0.0-20181029155118-b69ba1387ce2/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/grpc v1.13.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= google.golang.org/grpc v1.19.0 h1:cfg4PD8YEdSFnm7qLV4++93WcmhH2nIUhMjhdCvl3j8= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= -google.golang.org/grpc v1.22.0 h1:J0UbZOIrCAl+fpTOf8YLs4dJo8L/owV4LYVtAXQoPkw= -google.golang.org/grpc v1.22.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= -google.golang.org/grpc v1.25.1 h1:wdKvqQk7IttEw92GoRyKG2IDrUIpgpj6H6m81yfeMW0= -google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= @@ -548,8 +489,6 @@ gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= -gopkg.in/go-playground/validator.v9 v9.30.2 h1:icxYLlYflpazIV3ufMoNB9h9SYMQ37DZ8CTwkU4pnOs= -gopkg.in/go-playground/validator.v9 v9.30.2/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= gopkg.in/go-playground/validator.v9 v9.31.0 h1:bmXmP2RSNtFES+bn4uYuHT7iJFJv7Vj+an+ZQdDaD1M= gopkg.in/go-playground/validator.v9 v9.31.0/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno= @@ -563,8 +502,6 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo= -gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= From c9c01fe3b41085e7e8f6e63f0119d1f1aa210497 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 6 Feb 2020 10:07:52 +0700 Subject: [PATCH 03/87] Add new stuff from the cosmos app template. Specially the vesting codec and app version. --- sdk/modules.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/sdk/modules.go b/sdk/modules.go index 88d411b7d..d7e1099e6 100644 --- a/sdk/modules.go +++ b/sdk/modules.go @@ -1,12 +1,13 @@ package sdk import ( - "encoding/json" - bam "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/simapp" cosmostypes "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" + "github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth/vesting" "github.com/cosmos/cosmos-sdk/x/bank" "github.com/cosmos/cosmos-sdk/x/distribution" "github.com/cosmos/cosmos-sdk/x/genutil" @@ -46,6 +47,7 @@ func NewBasicManager() *module.BasicManager { cosmos.NewAppModuleBasic(executionsdk.ModuleName), cosmos.NewAppModuleBasic(processsdk.ModuleName), ) + vesting.RegisterCodec(codec.Codec) basicManager.RegisterCodec(codec.Codec) return &basicManager } @@ -134,6 +136,7 @@ func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, er slashingKeeper.Hooks(), ), ) + ownershipKeeper := ownershipsdk.NewKeeper(ownershipStoreKey) serviceKeeper := servicesdk.NewKeeper(serviceStoreKey, ownershipKeeper) instanceKeeper := instancesdk.NewKeeper(instanceStoreKey) @@ -144,6 +147,7 @@ func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, er // init app // TODO: engine should be in config app := bam.NewBaseApp("engine", logger, db, auth.DefaultTxDecoder(codec.Codec), bam.SetMinGasPrices(minGasPrices)) + app.SetAppVersion(version.Version) // init module manager manager := module.NewManager( @@ -153,7 +157,6 @@ func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, er supply.NewAppModule(supplyKeeper, accountKeeper), distribution.NewAppModule(distrKeeper, accountKeeper, supplyKeeper, stakingKeeper), slashing.NewAppModule(slashingKeeper, accountKeeper, stakingKeeper), - staking.NewAppModule(stakingKeeper, accountKeeper, supplyKeeper), ownershipsdk.NewModule(ownershipKeeper), servicesdk.NewModule(serviceKeeper), @@ -161,6 +164,8 @@ func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, er runnersdk.NewModule(runnerKeeper), executionsdk.NewModule(executionKeeper), processsdk.NewModule(processKeeper), + + staking.NewAppModule(stakingKeeper, accountKeeper, supplyKeeper), ) manager.SetOrderBeginBlockers(distribution.ModuleName, slashing.ModuleName) manager.SetOrderEndBlockers(staking.ModuleName) @@ -170,7 +175,6 @@ func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, er auth.ModuleName, bank.ModuleName, slashing.ModuleName, - supply.ModuleName, // app module ownershipsdk.ModuleName, @@ -181,17 +185,17 @@ func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, er processsdk.ModuleName, // genutil should be last module + supply.ModuleName, genutil.ModuleName, ) // register app to manager manager.RegisterRoutes(app.Router(), app.QueryRouter()) + app.SetInitChainer(func(ctx cosmostypes.Context, req abci.RequestInitChain) abci.ResponseInitChain { - var genesisData map[string]json.RawMessage - if err := codec.UnmarshalJSON(req.AppStateBytes, &genesisData); err != nil { - panic(err) - } - return manager.InitGenesis(ctx, genesisData) + var genesisState simapp.GenesisState + codec.Codec.MustUnmarshalJSON(req.AppStateBytes, &genesisState) + return manager.InitGenesis(ctx, genesisState) }) app.SetBeginBlocker(manager.BeginBlock) app.SetEndBlocker(manager.EndBlock) @@ -213,6 +217,7 @@ func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, er executionStoreKey, processStoreKey, ) + app.SetAnteHandler(auth.NewAnteHandler(accountKeeper, supplyKeeper, auth.DefaultSigVerificationGasConsumer)) if err := app.LoadLatestVersion(mainStoreKey); err != nil { return nil, err From 5b638c17e03ab87c5be6750383e63d25cb07be94 Mon Sep 17 00:00:00 2001 From: antho1404 Date: Thu, 6 Feb 2020 10:41:15 +0700 Subject: [PATCH 04/87] index all tx in order to have data for `/txs` API --- config/config.go | 1 + 1 file changed, 1 insertion(+) diff --git a/config/config.go b/config/config.go index 949cc8621..ab2f30ed6 100644 --- a/config/config.go +++ b/config/config.go @@ -108,6 +108,7 @@ func defaultConfig() (*Config, error) { c.Tendermint.Config.Consensus.TimeoutCommit = 5 * time.Second c.Tendermint.Config.Instrumentation.Prometheus = true c.Tendermint.Config.Instrumentation.PrometheusListenAddr = "0.0.0.0:26660" + c.Tendermint.Config.TxIndex.IndexAllTags = true c.Cosmos.RelativePath = "cosmos" c.Cosmos.MinGasPrices = "1.0atto" From 9be16f9584450b7308965a8ac3929dd255716cb1 Mon Sep 17 00:00:00 2001 From: antho1404 Date: Thu, 6 Feb 2020 10:41:42 +0700 Subject: [PATCH 05/87] configure LCD with a trusted node to fix `/block` API --- core/main.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/main.go b/core/main.go index c9216b880..28b46ffb5 100644 --- a/core/main.go +++ b/core/main.go @@ -224,7 +224,10 @@ func main() { logrus.WithField("module", "main").Fatalln(err) } - cliCtx := context.NewCLIContext().WithCodec(codec.Codec).WithClient(client) + cliCtx := context.NewCLIContext(). + WithCodec(codec.Codec). + WithClient(client). + WithTrustNode(true) mux := mux.NewRouter() cosmosclient.RegisterRoutes(cliCtx, mux) authrest.RegisterTxRoutes(cliCtx, mux) From 3ad70b7bbec1c9ab71b99c3df992f11410d24c60 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Thu, 6 Feb 2020 05:44:49 +0000 Subject: [PATCH 06/87] Bump google.golang.org/grpc from 1.27.0 to 1.27.1 Bumps [google.golang.org/grpc](https://github.com/grpc/grpc-go) from 1.27.0 to 1.27.1. - [Release notes](https://github.com/grpc/grpc-go/releases) - [Commits](https://github.com/grpc/grpc-go/compare/v1.27.0...v1.27.1) Signed-off-by: dependabot-preview[bot] --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 2d3bcfff3..03f509f43 100644 --- a/go.mod +++ b/go.mod @@ -63,7 +63,7 @@ require ( golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 // indirect golang.org/x/tools v0.0.0-20190813142322-97f12d73768f // indirect - google.golang.org/grpc v1.27.0 + google.golang.org/grpc v1.27.1 gopkg.in/go-playground/assert.v1 v1.2.1 // indirect gopkg.in/go-playground/validator.v9 v9.31.0 gopkg.in/yaml.v2 v2.2.8 diff --git a/go.sum b/go.sum index 173902d77..356d426a2 100644 --- a/go.sum +++ b/go.sum @@ -491,6 +491,8 @@ google.golang.org/grpc v1.25.1 h1:wdKvqQk7IttEw92GoRyKG2IDrUIpgpj6H6m81yfeMW0= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk= +google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From fae99d69bc69faa794819856f6aea28c95e9c724 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 6 Feb 2020 18:27:42 +0700 Subject: [PATCH 07/87] Add new decorator to force context.IsChectTx to false in NewIncrementSequenceDecorator to allow multiple tx from same account in one block --- cosmos/decorator.go | 32 ++++++++++++++++++++++++++++++++ sdk/modules.go | 19 ++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 cosmos/decorator.go diff --git a/cosmos/decorator.go b/cosmos/decorator.go new file mode 100644 index 000000000..75f8c78b5 --- /dev/null +++ b/cosmos/decorator.go @@ -0,0 +1,32 @@ +package cosmos + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// ForceCheckTxProxyDecorator defines a proxy decorator that force the ctx.IsCheckTx to false. +type ForceCheckTxProxyDecorator struct { + d sdk.AnteDecorator +} + +// NewForceCheckTxProxyDecorator returns a proxy decorator that force the ctx.IsCheckTx to false. +func NewForceCheckTxProxyDecorator(d sdk.AnteDecorator) ForceCheckTxProxyDecorator { + return ForceCheckTxProxyDecorator{ + d: d, + } +} + +// AnteHandle creates a proxy on top of the passed AnteDecorator AnteHandle. +func (d ForceCheckTxProxyDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { + ctx = ctx.WithIsCheckTx(false).WithValue("mesgHackIsCheckTx", ctx.IsCheckTx()) + n := func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) { + checkTx, ok := ctx.Value("mesgHackIsCheckTx").(bool) + if !ok { + return ctx, fmt.Errorf("mesgHackIsCheckTx is not a bool") + } + return next(ctx.WithIsCheckTx(checkTx), tx, simulate) + } + return d.d.AnteHandle(ctx, tx, simulate, n) +} diff --git a/sdk/modules.go b/sdk/modules.go index d7e1099e6..0f37303e2 100644 --- a/sdk/modules.go +++ b/sdk/modules.go @@ -7,6 +7,7 @@ import ( "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth/ante" "github.com/cosmos/cosmos-sdk/x/auth/vesting" "github.com/cosmos/cosmos-sdk/x/bank" "github.com/cosmos/cosmos-sdk/x/distribution" @@ -218,7 +219,23 @@ func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, er processStoreKey, ) - app.SetAnteHandler(auth.NewAnteHandler(accountKeeper, supplyKeeper, auth.DefaultSigVerificationGasConsumer)) + app.SetAnteHandler( + cosmostypes.ChainAnteDecorators( + ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first + ante.NewMempoolFeeDecorator(), + ante.NewValidateBasicDecorator(), + ante.NewValidateMemoDecorator(accountKeeper), + ante.NewConsumeGasForTxSizeDecorator(accountKeeper), + ante.NewSetPubKeyDecorator(accountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators + ante.NewValidateSigCountDecorator(accountKeeper), + ante.NewDeductFeeDecorator(accountKeeper, supplyKeeper), + ante.NewSigGasConsumeDecorator(accountKeeper, auth.DefaultSigVerificationGasConsumer), + ante.NewSigVerificationDecorator(accountKeeper), + cosmos.NewForceCheckTxProxyDecorator(ante.NewIncrementSequenceDecorator(accountKeeper)), // innermost AnteDecorator + ), + // Previous implementation: + // auth.NewAnteHandler(accountKeeper, supplyKeeper, auth.DefaultSigVerificationGasConsumer) + ) if err := app.LoadLatestVersion(mainStoreKey); err != nil { return nil, err } From 27b988f857b4962aaa61d84b1279d8a59a769324 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 6 Feb 2020 18:31:46 +0700 Subject: [PATCH 08/87] fix tendermint config about IndexAllKeys --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 5bc85b61b..b66587c60 100644 --- a/config/config.go +++ b/config/config.go @@ -108,7 +108,7 @@ func defaultConfig() (*Config, error) { c.Tendermint.Config.Consensus.TimeoutCommit = 5 * time.Second c.Tendermint.Config.Instrumentation.Prometheus = true c.Tendermint.Config.Instrumentation.PrometheusListenAddr = "0.0.0.0:26660" - c.Tendermint.Config.TxIndex.IndexAllTags = true + c.Tendermint.Config.TxIndex.IndexAllKeys = true c.Cosmos.RelativePath = "cosmos" c.Cosmos.MinGasPrices = "1.0atto" From 28a71be0a14d9f8dee2dc47229902f5ad0497b45 Mon Sep 17 00:00:00 2001 From: krhubert Date: Thu, 6 Feb 2020 14:55:57 +0100 Subject: [PATCH 09/87] Fix lint --- cosmos/keybase.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cosmos/keybase.go b/cosmos/keybase.go index 120b6d275..548195135 100644 --- a/cosmos/keybase.go +++ b/cosmos/keybase.go @@ -13,8 +13,10 @@ import ( ) const ( + // DefaultAlgo for create account. + DefaultAlgo = keys.Secp256k1 + mnemonicEntropySize = 256 - DefaultAlgo = keys.Secp256k1 ) // Keybase is a standard cosmos keybase. From edbd32a352c17c59989ee568682dcc036a8eeee9 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 6 Feb 2020 11:29:24 +0700 Subject: [PATCH 10/87] Generate app and cmds file cosmos scaffold tool --- app/app.go | 340 ++++++++++++++++++++++++++++++++++++++++ app/export.go | 164 +++++++++++++++++++ cmd/mesg-cli/main.go | 148 +++++++++++++++++ cmd/mesg-deamon/main.go | 102 ++++++++++++ x/.gitkeep | 0 5 files changed, 754 insertions(+) create mode 100644 app/app.go create mode 100644 app/export.go create mode 100644 cmd/mesg-cli/main.go create mode 100644 cmd/mesg-deamon/main.go create mode 100644 x/.gitkeep diff --git a/app/app.go b/app/app.go new file mode 100644 index 000000000..1f323fa33 --- /dev/null +++ b/app/app.go @@ -0,0 +1,340 @@ +package app + +import ( + "encoding/json" + "io" + "os" + + "github.com/mesg-foundation/engine/cosmos" + abci "github.com/tendermint/tendermint/abci/types" + cmn "github.com/tendermint/tendermint/libs/common" + "github.com/tendermint/tendermint/libs/log" + dbm "github.com/tendermint/tm-db" + + bam "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/cosmos/cosmos-sdk/version" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" + distr "github.com/cosmos/cosmos-sdk/x/distribution" + "github.com/cosmos/cosmos-sdk/x/genaccounts" + "github.com/cosmos/cosmos-sdk/x/genutil" + "github.com/cosmos/cosmos-sdk/x/params" + "github.com/cosmos/cosmos-sdk/x/slashing" + "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/x/supply" + mesgcodec "github.com/mesg-foundation/engine/codec" +) + +const appName = "engine" + +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") + + // ModuleBasics NewBasicManager is in charge of setting up basic module elemnets + ModuleBasics = module.NewBasicManager( + genaccounts.AppModuleBasic{}, + genutil.AppModuleBasic{}, + auth.AppModuleBasic{}, + bank.AppModuleBasic{}, + staking.AppModuleBasic{}, + distr.AppModuleBasic{}, + params.AppModuleBasic{}, + slashing.AppModuleBasic{}, + supply.AppModuleBasic{}, + + // Engine's AppModuleBasic + cosmos.NewAppModuleBasic(ownershipsdk.ModuleName), + cosmos.NewAppModuleBasic(servicesdk.ModuleName), + cosmos.NewAppModuleBasic(instancesdk.ModuleName), + cosmos.NewAppModuleBasic(runnersdk.ModuleName), + cosmos.NewAppModuleBasic(executionsdk.ModuleName), + cosmos.NewAppModuleBasic(processsdk.ModuleName), + ) + // account permissions + maccPerms = map[string][]string{ + auth.FeeCollectorName: nil, + distr.ModuleName: nil, + staking.BondedPoolName: {supply.Burner, supply.Staking}, + staking.NotBondedPoolName: {supply.Burner, supply.Staking}, + } +) + +// MakeCodec generates the necessary codecs for Amino +func MakeCodec() *codec.Codec { + // TODO: let's use the cosmos template way + // var cdc = codec.New() + // ModuleBasics.RegisterCodec(cdc) + // sdk.RegisterCodec(cdc) + // codec.RegisterCrypto(cdc) + // return cdc + return mesgcodec.Codec +} + +type newApp struct { + *bam.BaseApp + cdc *codec.Codec + + // keys to access the substores + keys map[string]*sdk.KVStoreKey + tkeys map[string]*sdk.TransientStoreKey + + // Keepers + accountKeeper auth.AccountKeeper + bankKeeper bank.Keeper + stakingKeeper staking.Keeper + slashingKeeper slashing.Keeper + distrKeeper distr.Keeper + supplyKeeper supply.Keeper + paramsKeeper params.Keeper + + // Engine's keepers + ownershipKeeper *ownershipsdk.Keeper + serviceKeeper *servicesdk.Keeper + instanceKeeper *instancesdk.Keeper + runnerKeeper *runnersdk.Keeper + processKeeper *processsdk.Keeper + executionKeeper *executionsdk.Keeper + + // Module Manager + mm *module.Manager +} + +// NewInitApp is a constructor function for nameServiceApp +func NewInitApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, + invCheckPeriod uint, baseAppOptions ...func(*bam.BaseApp)) *newApp { + + // First define the top level codec that will be shared by the different modules + cdc := MakeCodec() + + // BaseApp handles interactions with Tendermint through the ABCI protocol + bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...) + + bApp.SetAppVersion(version.Version) + + keys := sdk.NewKVStoreKeys( + bam.MainStoreKey, + auth.StoreKey, + staking.StoreKey, + supply.StoreKey, + distr.StoreKey, + slashing.StoreKey, + params.StoreKey, + + // Engine's module keys + ownershipsdk.ModuleName, + servicesdk.ModuleName, + instancesdk.ModuleName, + runnersdk.ModuleName, + executionsdk.ModuleName, + processsdk.ModuleName, + ) + + tkeys := sdk.NewTransientStoreKeys(staking.TStoreKey, params.TStoreKey) + + // Here you initialize your application with the store keys it requires + var app = &newApp{ + BaseApp: bApp, + cdc: cdc, + keys: keys, + tkeys: tkeys, + } + + // The ParamsKeeper handles parameter storage for the application + app.paramsKeeper = params.NewKeeper(app.cdc, keys[params.StoreKey], tkeys[params.TStoreKey], params.DefaultCodespace) + // Set specific supspaces + authSubspace := app.paramsKeeper.Subspace(auth.DefaultParamspace) + bankSupspace := app.paramsKeeper.Subspace(bank.DefaultParamspace) + stakingSubspace := app.paramsKeeper.Subspace(staking.DefaultParamspace) + distrSubspace := app.paramsKeeper.Subspace(distr.DefaultParamspace) + slashingSubspace := app.paramsKeeper.Subspace(slashing.DefaultParamspace) + + // The AccountKeeper handles address -> account lookups + app.accountKeeper = auth.NewAccountKeeper( + app.cdc, + keys[auth.StoreKey], + authSubspace, + auth.ProtoBaseAccount, + ) + + // The BankKeeper allows you perform sdk.Coins interactions + app.bankKeeper = bank.NewBaseKeeper( + app.accountKeeper, + bankSupspace, + bank.DefaultCodespace, + app.ModuleAccountAddrs(), + ) + + // The SupplyKeeper collects transaction fees and renders them to the fee distribution module + app.supplyKeeper = supply.NewKeeper( + app.cdc, + keys[supply.StoreKey], + app.accountKeeper, + app.bankKeeper, + maccPerms, + ) + + // The staking keeper + stakingKeeper := staking.NewKeeper( + app.cdc, + keys[staking.StoreKey], + tkeys[staking.TStoreKey], + app.supplyKeeper, + stakingSubspace, + staking.DefaultCodespace, + ) + + app.distrKeeper = distr.NewKeeper( + app.cdc, + keys[distr.StoreKey], + distrSubspace, + &stakingKeeper, + app.supplyKeeper, + distr.DefaultCodespace, + auth.FeeCollectorName, + app.ModuleAccountAddrs(), + ) + + app.slashingKeeper = slashing.NewKeeper( + app.cdc, + keys[slashing.StoreKey], + &stakingKeeper, + slashingSubspace, + slashing.DefaultCodespace, + ) + + // register the staking hooks + // NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks + app.stakingKeeper = *stakingKeeper.SetHooks( + staking.NewMultiStakingHooks( + app.distrKeeper.Hooks(), + app.slashingKeeper.Hooks()), + ) + + // Engine's module keepers + ownershipKeeper := ownershipsdk.NewKeeper(ownershipStoreKey) + serviceKeeper := servicesdk.NewKeeper(serviceStoreKey, ownershipKeeper) + instanceKeeper := instancesdk.NewKeeper(instanceStoreKey) + runnerKeeper := runnersdk.NewKeeper(runnerStoreKey, instanceKeeper) + processKeeper := processsdk.NewKeeper(processStoreKey, ownershipKeeper, instanceKeeper) + executionKeeper := executionsdk.NewKeeper(executionStoreKey, serviceKeeper, instanceKeeper, runnerKeeper, processKeeper) + + app.mm = module.NewManager( + genaccounts.NewAppModule(app.accountKeeper), + genutil.NewAppModule(app.accountKeeper, app.stakingKeeper, app.BaseApp.DeliverTx), + auth.NewAppModule(app.accountKeeper), + bank.NewAppModule(app.bankKeeper, app.accountKeeper), + supply.NewAppModule(app.supplyKeeper, app.accountKeeper), + distr.NewAppModule(app.distrKeeper, app.supplyKeeper), + + // Engine's modules + ownershipsdk.NewModule(ownershipKeeper), + servicesdk.NewModule(serviceKeeper), + instancesdk.NewModule(instanceKeeper), + runnersdk.NewModule(runnerKeeper), + executionsdk.NewModule(executionKeeper), + processsdk.NewModule(processKeeper), + + slashing.NewAppModule(app.slashingKeeper, app.stakingKeeper), + staking.NewAppModule(app.stakingKeeper, app.distrKeeper, app.accountKeeper, app.supplyKeeper), + ) + + app.mm.SetOrderBeginBlockers(distr.ModuleName, slashing.ModuleName) + app.mm.SetOrderEndBlockers(staking.ModuleName) + + // Sets the order of Genesis - Order matters, genutil is to always come last + // NOTE: The genutils moodule must occur after staking so that pools are + // properly initialized with tokens from genesis accounts. + app.mm.SetOrderInitGenesis( + genaccounts.ModuleName, + distr.ModuleName, + staking.ModuleName, + auth.ModuleName, + bank.ModuleName, + slashing.ModuleName, + + // Engine's modules + ownershipsdk.ModuleName, + servicesdk.ModuleName, + instancesdk.ModuleName, + runnersdk.ModuleName, + executionsdk.ModuleName, + processsdk.ModuleName, + + supply.ModuleName, + genutil.ModuleName, + ) + + // register all module routes and module queriers + app.mm.RegisterRoutes(app.Router(), app.QueryRouter()) + + // The initChainer handles translating the genesis.json file into initial state for the network + app.SetInitChainer(app.InitChainer) + app.SetBeginBlocker(app.BeginBlocker) + app.SetEndBlocker(app.EndBlocker) + + // The AnteHandler handles signature verification and transaction pre-processing + app.SetAnteHandler( + auth.NewAnteHandler( + app.accountKeeper, + app.supplyKeeper, + auth.DefaultSigVerificationGasConsumer, + ), + ) + + // initialize stores + app.MountKVStores(keys) + app.MountTransientStores(tkeys) + + err := app.LoadLatestVersion(app.keys[bam.MainStoreKey]) + if err != nil { + cmn.Exit(err.Error()) + } + + return app +} + +// GenesisState represents chain state at the start of the chain. Any initial state (account balances) are stored here. +type GenesisState map[string]json.RawMessage + +// NewDefaultGenesisState returns a the default GensisState +func NewDefaultGenesisState() GenesisState { + return ModuleBasics.DefaultGenesis() +} + +func (app *newApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain { + var genesisState GenesisState + + err := app.cdc.UnmarshalJSON(req.AppStateBytes, &genesisState) + if err != nil { + panic(err) + } + + return app.mm.InitGenesis(ctx, genesisState) +} + +func (app *newApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock { + return app.mm.BeginBlock(ctx, req) +} +func (app *newApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock { + return app.mm.EndBlock(ctx, req) +} +func (app *newApp) LoadHeight(height int64) error { + return app.LoadVersion(height, app.keys[bam.MainStoreKey]) +} + +// ModuleAccountAddrs returns all the app's module account addresses. +func (app *newApp) ModuleAccountAddrs() map[string]bool { + modAccAddrs := make(map[string]bool) + for acc := range maccPerms { + modAccAddrs[supply.NewModuleAddress(acc).String()] = true + } + + return modAccAddrs +} diff --git a/app/export.go b/app/export.go new file mode 100644 index 000000000..8f0a7c05a --- /dev/null +++ b/app/export.go @@ -0,0 +1,164 @@ +package app + +import ( + "encoding/json" + "log" + + abci "github.com/tendermint/tendermint/abci/types" + tmtypes "github.com/tendermint/tendermint/types" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/slashing" + "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/x/staking/exported" +) + +// ExportAppStateAndValidators exports the state of the application for a genesis +// file. +func (app *newApp) ExportAppStateAndValidators( + forZeroHeight bool, jailWhiteList []string, +) (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) { + + // as if they could withdraw from the start of the next block + ctx := app.NewContext(true, abci.Header{Height: app.LastBlockHeight()}) + + if forZeroHeight { + app.prepForZeroHeightGenesis(ctx, jailWhiteList) + } + + genState := app.mm.ExportGenesis(ctx) + appState, err = codec.MarshalJSONIndent(app.cdc, genState) + if err != nil { + return nil, nil, err + } + + validators = staking.WriteValidators(ctx, app.stakingKeeper) + return appState, validators, nil +} + +// prepare for fresh start at zero height +// NOTE zero height genesis is a temporary feature which will be deprecated +// in favour of export at a block height +func (app *newApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []string) { + applyWhiteList := false + + //Check if there is a whitelist + if len(jailWhiteList) > 0 { + applyWhiteList = true + } + + whiteListMap := make(map[string]bool) + + for _, addr := range jailWhiteList { + _, err := sdk.ValAddressFromBech32(addr) + if err != nil { + log.Fatal(err) + } + whiteListMap[addr] = true + } + + /* Handle fee distribution state. */ + + // withdraw all validator commission + app.stakingKeeper.IterateValidators(ctx, func(_ int64, val exported.ValidatorI) (stop bool) { + _, _ = app.distrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator()) + return false + }) + + // withdraw all delegator rewards + dels := app.stakingKeeper.GetAllDelegations(ctx) + for _, delegation := range dels { + _, _ = app.distrKeeper.WithdrawDelegationRewards(ctx, delegation.DelegatorAddress, delegation.ValidatorAddress) + } + + // clear validator slash events + app.distrKeeper.DeleteAllValidatorSlashEvents(ctx) + + // clear validator historical rewards + app.distrKeeper.DeleteAllValidatorHistoricalRewards(ctx) + + // set context height to zero + height := ctx.BlockHeight() + ctx = ctx.WithBlockHeight(0) + + // reinitialize all validators + app.stakingKeeper.IterateValidators(ctx, func(_ int64, val exported.ValidatorI) (stop bool) { + + // donate any unwithdrawn outstanding reward fraction tokens to the community pool + scraps := app.distrKeeper.GetValidatorOutstandingRewards(ctx, val.GetOperator()) + feePool := app.distrKeeper.GetFeePool(ctx) + feePool.CommunityPool = feePool.CommunityPool.Add(scraps) + app.distrKeeper.SetFeePool(ctx, feePool) + + app.distrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator()) + return false + }) + + // reinitialize all delegations + for _, del := range dels { + app.distrKeeper.Hooks().BeforeDelegationCreated(ctx, del.DelegatorAddress, del.ValidatorAddress) + app.distrKeeper.Hooks().AfterDelegationModified(ctx, del.DelegatorAddress, del.ValidatorAddress) + } + + // reset context height + ctx = ctx.WithBlockHeight(height) + + /* Handle staking state. */ + + // iterate through redelegations, reset creation height + app.stakingKeeper.IterateRedelegations(ctx, func(_ int64, red staking.Redelegation) (stop bool) { + for i := range red.Entries { + red.Entries[i].CreationHeight = 0 + } + app.stakingKeeper.SetRedelegation(ctx, red) + return false + }) + + // iterate through unbonding delegations, reset creation height + app.stakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd staking.UnbondingDelegation) (stop bool) { + for i := range ubd.Entries { + ubd.Entries[i].CreationHeight = 0 + } + app.stakingKeeper.SetUnbondingDelegation(ctx, ubd) + return false + }) + + // Iterate through validators by power descending, reset bond heights, and + // update bond intra-tx counters. + store := ctx.KVStore(app.keys[staking.StoreKey]) + iter := sdk.KVStoreReversePrefixIterator(store, staking.ValidatorsKey) + counter := int16(0) + + for ; iter.Valid(); iter.Next() { + addr := sdk.ValAddress(iter.Key()[1:]) + validator, found := app.stakingKeeper.GetValidator(ctx, addr) + if !found { + panic("expected validator, not found") + } + + validator.UnbondingHeight = 0 + if applyWhiteList && !whiteListMap[addr.String()] { + validator.Jailed = true + } + + app.stakingKeeper.SetValidator(ctx, validator) + counter++ + } + + iter.Close() + + _ = app.stakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) + + /* Handle slashing state. */ + + // reset start height on signing infos + app.slashingKeeper.IterateValidatorSigningInfos( + ctx, + func(addr sdk.ConsAddress, info slashing.ValidatorSigningInfo) (stop bool) { + info.StartHeight = 0 + app.slashingKeeper.SetValidatorSigningInfo(ctx, addr, info) + return false + }, + ) +} diff --git a/cmd/mesg-cli/main.go b/cmd/mesg-cli/main.go new file mode 100644 index 000000000..db5d508e4 --- /dev/null +++ b/cmd/mesg-cli/main.go @@ -0,0 +1,148 @@ +package main + +import ( + "fmt" + "os" + "path" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/keys" + "github.com/cosmos/cosmos-sdk/client/lcd" + "github.com/cosmos/cosmos-sdk/client/rpc" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/version" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" + "github.com/mesg-foundation/engine/app" + mesgconfig "github.com/mesg-foundation/engine/config" + "github.com/spf13/cobra" + "github.com/spf13/viper" + amino "github.com/tendermint/go-amino" + "github.com/tendermint/tendermint/libs/cli" + "github.com/mesg-foundation/engine/cosmos" +) + +func main() { + cobra.EnableCommandSorting = false + + cdc := app.MakeCodec() + + // Read in the configuration file for the sdk + // config := sdk.GetConfig() + // config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) + // config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) + // config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub) + // config.Seal() + cfg, err := mesgconfig.New() + if err != nil { + panic(err) + } + cosmos.CustomizeConfig(cfg) + + rootCmd := &cobra.Command{ + Use: "mesg-cli", + Short: "mesg app Client", + } + + // Add --chain-id to persistent flags and mark it required + rootCmd.PersistentFlags().String(client.FlagChainID, "", "Chain ID of tendermint node") + rootCmd.PersistentPreRunE = func(_ *cobra.Command, _ []string) error { + return initConfig(rootCmd) + } + + // Construct Root Command + rootCmd.AddCommand( + rpc.StatusCommand(), + client.ConfigCmd(app.DefaultCLIHome), + queryCmd(cdc), + txCmd(cdc), + client.LineBreak, + lcd.ServeCommand(cdc, registerRoutes), + client.LineBreak, + keys.Commands(), + client.LineBreak, + version.Cmd, + client.NewCompletionCmd(rootCmd, true), + ) + + executor := cli.PrepareMainCmd(rootCmd, "MESG", app.DefaultCLIHome) + err := executor.Execute() + if err != nil { + fmt.Printf("Failed executing CLI command: %s, exiting...\n", err) + os.Exit(1) + } +} + +func registerRoutes(rs *lcd.RestServer) { + client.RegisterRoutes(rs.CliCtx, rs.Mux) + app.ModuleBasics.RegisterRESTRoutes(rs.CliCtx, rs.Mux) +} + +func queryCmd(cdc *amino.Codec) *cobra.Command { + queryCmd := &cobra.Command{ + Use: "query", + Aliases: []string{"q"}, + Short: "Querying subcommands", + } + + queryCmd.AddCommand( + authcmd.GetAccountCmd(cdc), + client.LineBreak, + rpc.ValidatorCommand(cdc), + rpc.BlockCommand(), + authcmd.QueryTxsByEventsCmd(cdc), + authcmd.QueryTxCmd(cdc), + client.LineBreak, + ) + + // add modules' query commands + app.ModuleBasics.AddQueryCommands(queryCmd, cdc) + + return queryCmd +} + +func txCmd(cdc *amino.Codec) *cobra.Command { + txCmd := &cobra.Command{ + Use: "tx", + Short: "Transactions subcommands", + } + + txCmd.AddCommand( + bankcmd.SendTxCmd(cdc), + client.LineBreak, + authcmd.GetSignCommand(cdc), + authcmd.GetMultiSignCommand(cdc), + client.LineBreak, + authcmd.GetBroadcastCommand(cdc), + authcmd.GetEncodeCommand(cdc), + client.LineBreak, + ) + + // add modules' tx commands + app.ModuleBasics.AddTxCommands(txCmd, cdc) + + return txCmd +} + +func initConfig(cmd *cobra.Command) error { + home, err := cmd.PersistentFlags().GetString(cli.HomeFlag) + if err != nil { + return err + } + + cfgFile := path.Join(home, "config", "config.toml") + if _, err := os.Stat(cfgFile); err == nil { + viper.SetConfigFile(cfgFile) + + if err := viper.ReadInConfig(); err != nil { + return err + } + } + if err := viper.BindPFlag(client.FlagChainID, cmd.PersistentFlags().Lookup(client.FlagChainID)); err != nil { + return err + } + if err := viper.BindPFlag(cli.EncodingFlag, cmd.PersistentFlags().Lookup(cli.EncodingFlag)); err != nil { + return err + } + return viper.BindPFlag(cli.OutputFlag, cmd.PersistentFlags().Lookup(cli.OutputFlag)) +} diff --git a/cmd/mesg-deamon/main.go b/cmd/mesg-deamon/main.go new file mode 100644 index 000000000..48ea1425a --- /dev/null +++ b/cmd/mesg-deamon/main.go @@ -0,0 +1,102 @@ +package main + +import ( + "encoding/json" + "io" + + "github.com/cosmos/cosmos-sdk/server" + "github.com/cosmos/cosmos-sdk/x/genaccounts" + genaccscli "github.com/cosmos/cosmos-sdk/x/genaccounts/client/cli" + "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/tendermint/tendermint/libs/cli" + "github.com/tendermint/tendermint/libs/log" + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/store" + sdk "github.com/cosmos/cosmos-sdk/types" + genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" + app "github.com/mesg-foundation/engine/app" + abci "github.com/tendermint/tendermint/abci/types" + tmtypes "github.com/tendermint/tendermint/types" + dbm "github.com/tendermint/tm-db" + mesgconfig "github.com/mesg-foundation/engine/config" + "github.com/mesg-foundation/engine/cosmos" +) + +const flagInvCheckPeriod = "inv-check-period" + +var invCheckPeriod uint + +func main() { + cdc := app.MakeCodec() + + // config := sdk.GetConfig() + // config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) + // config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) + // config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub) + // config.Seal() + cfg, err := mesgconfig.New() + if err != nil { + panic(err) + } + cosmos.CustomizeConfig(cfg) + + ctx := server.NewDefaultContext() + cobra.EnableCommandSorting = false + rootCmd := &cobra.Command{ + Use: "mesg-deamon", + Short: "mesg app Daemon (server)", + PersistentPreRunE: server.PersistentPreRunEFn(ctx), + } + + rootCmd.AddCommand( + genutilcli.InitCmd(ctx, cdc, app.ModuleBasics, app.DefaultNodeHome), + genutilcli.CollectGenTxsCmd(ctx, cdc, genaccounts.AppModuleBasic{}, app.DefaultNodeHome), + genutilcli.GenTxCmd( + ctx, cdc, app.ModuleBasics, staking.AppModuleBasic{}, + genaccounts.AppModuleBasic{}, app.DefaultNodeHome, app.DefaultCLIHome, + ), + genutilcli.ValidateGenesisCmd(ctx, cdc, app.ModuleBasics), + // AddGenesisAccountCmd allows users to add accounts to the genesis file + genaccscli.AddGenesisAccountCmd(ctx, cdc, app.DefaultNodeHome, app.DefaultCLIHome), + ) + + server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators) + + // prepare and add flags + executor := cli.PrepareBaseCmd(rootCmd, "AU", app.DefaultNodeHome) + rootCmd.PersistentFlags().UintVar(&invCheckPeriod, flagInvCheckPeriod, + 0, "Assert registered invariants every N blocks") + err := executor.Execute() + if err != nil { + panic(err) + } +} + +func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer) abci.Application { + return app.NewInitApp( + logger, db, traceStore, true, invCheckPeriod, + baseapp.SetPruning(store.NewPruningOptionsFromString(viper.GetString("pruning"))), + baseapp.SetMinGasPrices(viper.GetString(server.FlagMinGasPrices)), + baseapp.SetHaltHeight(uint64(viper.GetInt(server.FlagHaltHeight))), + ) +} + +func exportAppStateAndTMValidators( + logger log.Logger, db dbm.DB, traceStore io.Writer, height int64, forZeroHeight bool, jailWhiteList []string, +) (json.RawMessage, []tmtypes.GenesisValidator, error) { + + if height != -1 { + aApp := app.NewInitApp(logger, db, traceStore, true, uint(1)) + err := aApp.LoadHeight(height) + if err != nil { + return nil, nil, err + } + return aApp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList) + } + + aApp := app.NewInitApp(logger, db, traceStore, true, uint(1)) + + return aApp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList) +} diff --git a/x/.gitkeep b/x/.gitkeep new file mode 100644 index 000000000..e69de29bb From 395c2c2d14fd6851d68db48e482b73d4077f09a5 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 6 Feb 2020 12:32:05 +0700 Subject: [PATCH 11/87] Update generated files to latest cosmos --- app/app.go | 197 ++++++++++++++--------- app/export.go | 21 ++- cmd/mesg-cli/main.go | 77 ++++++--- cmd/mesg-daemon/genaccounts.go | 153 ++++++++++++++++++ cmd/{mesg-deamon => mesg-daemon}/main.go | 56 ++++--- core/main.go | 24 +-- 6 files changed, 392 insertions(+), 136 deletions(-) create mode 100644 cmd/mesg-daemon/genaccounts.go rename cmd/{mesg-deamon => mesg-daemon}/main.go (66%) diff --git a/app/app.go b/app/app.go index 1f323fa33..256c1f36d 100644 --- a/app/app.go +++ b/app/app.go @@ -5,27 +5,34 @@ import ( "io" "os" - "github.com/mesg-foundation/engine/cosmos" abci "github.com/tendermint/tendermint/abci/types" - cmn "github.com/tendermint/tendermint/libs/common" "github.com/tendermint/tendermint/libs/log" + tmos "github.com/tendermint/tendermint/libs/os" dbm "github.com/tendermint/tm-db" bam "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth/vesting" "github.com/cosmos/cosmos-sdk/x/bank" distr "github.com/cosmos/cosmos-sdk/x/distribution" - "github.com/cosmos/cosmos-sdk/x/genaccounts" "github.com/cosmos/cosmos-sdk/x/genutil" "github.com/cosmos/cosmos-sdk/x/params" "github.com/cosmos/cosmos-sdk/x/slashing" "github.com/cosmos/cosmos-sdk/x/staking" "github.com/cosmos/cosmos-sdk/x/supply" mesgcodec "github.com/mesg-foundation/engine/codec" + "github.com/mesg-foundation/engine/cosmos" + executionsdk "github.com/mesg-foundation/engine/sdk/execution" + instancesdk "github.com/mesg-foundation/engine/sdk/instance" + ownershipsdk "github.com/mesg-foundation/engine/sdk/ownership" + processsdk "github.com/mesg-foundation/engine/sdk/process" + runnersdk "github.com/mesg-foundation/engine/sdk/runner" + servicesdk "github.com/mesg-foundation/engine/sdk/service" ) const appName = "engine" @@ -37,9 +44,10 @@ var ( // DefaultNodeHome sets the folder where the applcation data and configuration will be stored DefaultNodeHome = os.ExpandEnv("$HOME/.mesg/tendermint") - // ModuleBasics NewBasicManager is in charge of setting up basic module elemnets + // ModuleBasics The module BasicManager is in charge of setting up basic, + // non-dependant module elements, such as codec registration + // and genesis verification. ModuleBasics = module.NewBasicManager( - genaccounts.AppModuleBasic{}, genutil.AppModuleBasic{}, auth.AppModuleBasic{}, bank.AppModuleBasic{}, @@ -57,7 +65,8 @@ var ( cosmos.NewAppModuleBasic(executionsdk.ModuleName), cosmos.NewAppModuleBasic(processsdk.ModuleName), ) - // account permissions + + // module account permissions maccPerms = map[string][]string{ auth.FeeCollectorName: nil, distr.ModuleName: nil, @@ -66,26 +75,36 @@ var ( } ) -// MakeCodec generates the necessary codecs for Amino +// MakeCodec creates the application codec. The codec is sealed before it is +// returned. func MakeCodec() *codec.Codec { // TODO: let's use the cosmos template way // var cdc = codec.New() - // ModuleBasics.RegisterCodec(cdc) + + ModuleBasics.RegisterCodec(mesgcodec.Codec) + vesting.RegisterCodec(mesgcodec.Codec) // sdk.RegisterCodec(cdc) // codec.RegisterCrypto(cdc) - // return cdc + + // return cdc.Seal() return mesgcodec.Codec } -type newApp struct { +// NewApp extended ABCI application +type NewApp struct { *bam.BaseApp cdc *codec.Codec + invCheckPeriod uint + // keys to access the substores keys map[string]*sdk.KVStoreKey - tkeys map[string]*sdk.TransientStoreKey + tKeys map[string]*sdk.TransientStoreKey + + // subspaces + subspaces map[string]params.Subspace - // Keepers + // keepers accountKeeper auth.AccountKeeper bankKeeper bank.Keeper stakingKeeper staking.Keeper @@ -104,20 +123,28 @@ type newApp struct { // Module Manager mm *module.Manager + + // simulation manager + sm *module.SimulationManager } -// NewInitApp is a constructor function for nameServiceApp -func NewInitApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, - invCheckPeriod uint, baseAppOptions ...func(*bam.BaseApp)) *newApp { +// verify app interface at compile time +var _ simapp.App = (*NewApp)(nil) +// NewengineApp is a constructor function for engineApp +func NewInitApp( + logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, + invCheckPeriod uint, baseAppOptions ...func(*bam.BaseApp), +) *NewApp { // First define the top level codec that will be shared by the different modules cdc := MakeCodec() // BaseApp handles interactions with Tendermint through the ABCI protocol bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...) - + bApp.SetCommitMultiStoreTracer(traceStore) bApp.SetAppVersion(version.Version) + // TODO: Add the keys that module requires keys := sdk.NewKVStoreKeys( bam.MainStoreKey, auth.StoreKey, @@ -136,38 +163,39 @@ func NewInitApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b processsdk.ModuleName, ) - tkeys := sdk.NewTransientStoreKeys(staking.TStoreKey, params.TStoreKey) + tKeys := sdk.NewTransientStoreKeys(staking.TStoreKey, params.TStoreKey) // Here you initialize your application with the store keys it requires - var app = &newApp{ - BaseApp: bApp, - cdc: cdc, - keys: keys, - tkeys: tkeys, + var app = &NewApp{ + BaseApp: bApp, + cdc: cdc, + invCheckPeriod: invCheckPeriod, + keys: keys, + tKeys: tKeys, + subspaces: make(map[string]params.Subspace), } // The ParamsKeeper handles parameter storage for the application - app.paramsKeeper = params.NewKeeper(app.cdc, keys[params.StoreKey], tkeys[params.TStoreKey], params.DefaultCodespace) + app.paramsKeeper = params.NewKeeper(app.cdc, keys[params.StoreKey], tKeys[params.TStoreKey]) // Set specific supspaces - authSubspace := app.paramsKeeper.Subspace(auth.DefaultParamspace) - bankSupspace := app.paramsKeeper.Subspace(bank.DefaultParamspace) - stakingSubspace := app.paramsKeeper.Subspace(staking.DefaultParamspace) - distrSubspace := app.paramsKeeper.Subspace(distr.DefaultParamspace) - slashingSubspace := app.paramsKeeper.Subspace(slashing.DefaultParamspace) + app.subspaces[auth.ModuleName] = app.paramsKeeper.Subspace(auth.DefaultParamspace) + app.subspaces[bank.ModuleName] = app.paramsKeeper.Subspace(bank.DefaultParamspace) + app.subspaces[staking.ModuleName] = app.paramsKeeper.Subspace(staking.DefaultParamspace) + app.subspaces[distr.ModuleName] = app.paramsKeeper.Subspace(distr.DefaultParamspace) + app.subspaces[slashing.ModuleName] = app.paramsKeeper.Subspace(slashing.DefaultParamspace) // The AccountKeeper handles address -> account lookups app.accountKeeper = auth.NewAccountKeeper( app.cdc, keys[auth.StoreKey], - authSubspace, + app.subspaces[auth.ModuleName], auth.ProtoBaseAccount, ) // The BankKeeper allows you perform sdk.Coins interactions app.bankKeeper = bank.NewBaseKeeper( app.accountKeeper, - bankSupspace, - bank.DefaultCodespace, + app.subspaces[bank.ModuleName], app.ModuleAccountAddrs(), ) @@ -184,19 +212,16 @@ func NewInitApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b stakingKeeper := staking.NewKeeper( app.cdc, keys[staking.StoreKey], - tkeys[staking.TStoreKey], app.supplyKeeper, - stakingSubspace, - staking.DefaultCodespace, + app.subspaces[staking.ModuleName], ) app.distrKeeper = distr.NewKeeper( app.cdc, keys[distr.StoreKey], - distrSubspace, + app.subspaces[distr.ModuleName], &stakingKeeper, app.supplyKeeper, - distr.DefaultCodespace, auth.FeeCollectorName, app.ModuleAccountAddrs(), ) @@ -205,8 +230,7 @@ func NewInitApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b app.cdc, keys[slashing.StoreKey], &stakingKeeper, - slashingSubspace, - slashing.DefaultCodespace, + app.subspaces[slashing.ModuleName], ) // register the staking hooks @@ -218,41 +242,44 @@ func NewInitApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b ) // Engine's module keepers - ownershipKeeper := ownershipsdk.NewKeeper(ownershipStoreKey) - serviceKeeper := servicesdk.NewKeeper(serviceStoreKey, ownershipKeeper) - instanceKeeper := instancesdk.NewKeeper(instanceStoreKey) - runnerKeeper := runnersdk.NewKeeper(runnerStoreKey, instanceKeeper) - processKeeper := processsdk.NewKeeper(processStoreKey, ownershipKeeper, instanceKeeper) - executionKeeper := executionsdk.NewKeeper(executionStoreKey, serviceKeeper, instanceKeeper, runnerKeeper, processKeeper) - + app.ownershipKeeper = ownershipsdk.NewKeeper(keys[ownershipsdk.ModuleName]) + app.serviceKeeper = servicesdk.NewKeeper(keys[servicesdk.ModuleName], app.ownershipKeeper) + app.instanceKeeper = instancesdk.NewKeeper(keys[instancesdk.ModuleName]) + app.runnerKeeper = runnersdk.NewKeeper(keys[runnersdk.ModuleName], app.instanceKeeper) + app.processKeeper = processsdk.NewKeeper(keys[processsdk.ModuleName], app.ownershipKeeper, app.instanceKeeper) + app.executionKeeper = executionsdk.NewKeeper(keys[executionsdk.ModuleName], app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper) + + // NOTE: Any module instantiated in the module manager that is later modified + // must be passed by reference here. app.mm = module.NewManager( - genaccounts.NewAppModule(app.accountKeeper), genutil.NewAppModule(app.accountKeeper, app.stakingKeeper, app.BaseApp.DeliverTx), auth.NewAppModule(app.accountKeeper), bank.NewAppModule(app.bankKeeper, app.accountKeeper), supply.NewAppModule(app.supplyKeeper, app.accountKeeper), - distr.NewAppModule(app.distrKeeper, app.supplyKeeper), + distr.NewAppModule(app.distrKeeper, app.accountKeeper, app.supplyKeeper, app.stakingKeeper), + slashing.NewAppModule(app.slashingKeeper, app.accountKeeper, app.stakingKeeper), // Engine's modules - ownershipsdk.NewModule(ownershipKeeper), - servicesdk.NewModule(serviceKeeper), - instancesdk.NewModule(instanceKeeper), - runnersdk.NewModule(runnerKeeper), - executionsdk.NewModule(executionKeeper), - processsdk.NewModule(processKeeper), - - slashing.NewAppModule(app.slashingKeeper, app.stakingKeeper), - staking.NewAppModule(app.stakingKeeper, app.distrKeeper, app.accountKeeper, app.supplyKeeper), + ownershipsdk.NewModule(app.ownershipKeeper), + servicesdk.NewModule(app.serviceKeeper), + instancesdk.NewModule(app.instanceKeeper), + runnersdk.NewModule(app.runnerKeeper), + executionsdk.NewModule(app.executionKeeper), + processsdk.NewModule(app.processKeeper), + + staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper), ) + // During begin block slashing happens after distr.BeginBlocker so that + // there is nothing left over in the validator fee pool, so as to keep the + // CanWithdrawInvariant invariant. app.mm.SetOrderBeginBlockers(distr.ModuleName, slashing.ModuleName) app.mm.SetOrderEndBlockers(staking.ModuleName) // Sets the order of Genesis - Order matters, genutil is to always come last - // NOTE: The genutils moodule must occur after staking so that pools are + // NOTE: The genutils module must occur after staking so that pools are // properly initialized with tokens from genesis accounts. app.mm.SetOrderInitGenesis( - genaccounts.ModuleName, distr.ModuleName, staking.ModuleName, auth.ModuleName, @@ -290,11 +317,13 @@ func NewInitApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b // initialize stores app.MountKVStores(keys) - app.MountTransientStores(tkeys) + app.MountTransientStores(tKeys) - err := app.LoadLatestVersion(app.keys[bam.MainStoreKey]) - if err != nil { - cmn.Exit(err.Error()) + if loadLatest { + err := app.LoadLatestVersion(app.keys[bam.MainStoreKey]) + if err != nil { + tmos.Exit(err.Error()) + } } return app @@ -303,34 +332,37 @@ func NewInitApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b // GenesisState represents chain state at the start of the chain. Any initial state (account balances) are stored here. type GenesisState map[string]json.RawMessage -// NewDefaultGenesisState returns a the default GensisState +// NewDefaultGenesisState generates the default state for the application. func NewDefaultGenesisState() GenesisState { return ModuleBasics.DefaultGenesis() } -func (app *newApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain { - var genesisState GenesisState +// InitChainer application update at chain initialization +func (app *NewApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain { + var genesisState simapp.GenesisState - err := app.cdc.UnmarshalJSON(req.AppStateBytes, &genesisState) - if err != nil { - panic(err) - } + app.cdc.MustUnmarshalJSON(req.AppStateBytes, &genesisState) return app.mm.InitGenesis(ctx, genesisState) } -func (app *newApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock { +// BeginBlocker application updates every begin block +func (app *NewApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock { return app.mm.BeginBlock(ctx, req) } -func (app *newApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock { + +// EndBlocker application updates every end block +func (app *NewApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock { return app.mm.EndBlock(ctx, req) } -func (app *newApp) LoadHeight(height int64) error { + +// LoadHeight loads a particular height +func (app *NewApp) LoadHeight(height int64) error { return app.LoadVersion(height, app.keys[bam.MainStoreKey]) } // ModuleAccountAddrs returns all the app's module account addresses. -func (app *newApp) ModuleAccountAddrs() map[string]bool { +func (app *NewApp) ModuleAccountAddrs() map[string]bool { modAccAddrs := make(map[string]bool) for acc := range maccPerms { modAccAddrs[supply.NewModuleAddress(acc).String()] = true @@ -338,3 +370,22 @@ func (app *newApp) ModuleAccountAddrs() map[string]bool { return modAccAddrs } + +// Codec returns the application's sealed codec. +func (app *NewApp) Codec() *codec.Codec { + return app.cdc +} + +// SimulationManager implements the SimulationApp interface +func (app *NewApp) SimulationManager() *module.SimulationManager { + return app.sm +} + +// GetMaccPerms returns a mapping of the application's module account permissions. +func GetMaccPerms() map[string][]string { + modAccPerms := make(map[string][]string) + for k, v := range maccPerms { + modAccPerms[k] = v + } + return modAccPerms +} diff --git a/app/export.go b/app/export.go index 8f0a7c05a..ad76bd0ef 100644 --- a/app/export.go +++ b/app/export.go @@ -11,12 +11,11 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing" "github.com/cosmos/cosmos-sdk/x/staking" - "github.com/cosmos/cosmos-sdk/x/staking/exported" ) // ExportAppStateAndValidators exports the state of the application for a genesis // file. -func (app *newApp) ExportAppStateAndValidators( +func (app *NewApp) ExportAppStateAndValidators( forZeroHeight bool, jailWhiteList []string, ) (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) { @@ -40,7 +39,7 @@ func (app *newApp) ExportAppStateAndValidators( // prepare for fresh start at zero height // NOTE zero height genesis is a temporary feature which will be deprecated // in favour of export at a block height -func (app *newApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []string) { +func (app *NewApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []string) { applyWhiteList := false //Check if there is a whitelist @@ -61,15 +60,21 @@ func (app *newApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []str /* Handle fee distribution state. */ // withdraw all validator commission - app.stakingKeeper.IterateValidators(ctx, func(_ int64, val exported.ValidatorI) (stop bool) { - _, _ = app.distrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator()) + app.stakingKeeper.IterateValidators(ctx, func(_ int64, val staking.ValidatorI) (stop bool) { + _, err := app.distrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator()) + if err != nil { + log.Fatal(err) + } return false }) // withdraw all delegator rewards dels := app.stakingKeeper.GetAllDelegations(ctx) for _, delegation := range dels { - _, _ = app.distrKeeper.WithdrawDelegationRewards(ctx, delegation.DelegatorAddress, delegation.ValidatorAddress) + _, err := app.distrKeeper.WithdrawDelegationRewards(ctx, delegation.DelegatorAddress, delegation.ValidatorAddress) + if err != nil { + log.Fatal(err) + } } // clear validator slash events @@ -83,12 +88,12 @@ func (app *newApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []str ctx = ctx.WithBlockHeight(0) // reinitialize all validators - app.stakingKeeper.IterateValidators(ctx, func(_ int64, val exported.ValidatorI) (stop bool) { + app.stakingKeeper.IterateValidators(ctx, func(_ int64, val staking.ValidatorI) (stop bool) { // donate any unwithdrawn outstanding reward fraction tokens to the community pool scraps := app.distrKeeper.GetValidatorOutstandingRewards(ctx, val.GetOperator()) feePool := app.distrKeeper.GetFeePool(ctx) - feePool.CommunityPool = feePool.CommunityPool.Add(scraps) + feePool.CommunityPool = feePool.CommunityPool.Add(scraps...) app.distrKeeper.SetFeePool(ctx, feePool) app.distrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator()) diff --git a/cmd/mesg-cli/main.go b/cmd/mesg-cli/main.go index db5d508e4..57e9a6b81 100644 --- a/cmd/mesg-cli/main.go +++ b/cmd/mesg-cli/main.go @@ -6,25 +6,34 @@ import ( "path" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/client/lcd" "github.com/cosmos/cosmos-sdk/client/rpc" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/version" + "github.com/cosmos/cosmos-sdk/x/auth" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest" + "github.com/cosmos/cosmos-sdk/x/bank" bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" - "github.com/mesg-foundation/engine/app" - mesgconfig "github.com/mesg-foundation/engine/config" + "github.com/spf13/cobra" "github.com/spf13/viper" - amino "github.com/tendermint/go-amino" + + "github.com/tendermint/go-amino" "github.com/tendermint/tendermint/libs/cli" + + "github.com/mesg-foundation/engine/app" + "github.com/mesg-foundation/engine/config" "github.com/mesg-foundation/engine/cosmos" ) func main() { + // Configure cobra to sort commands cobra.EnableCommandSorting = false + // Instantiate the codec for the command line application cdc := app.MakeCodec() // Read in the configuration file for the sdk @@ -33,19 +42,23 @@ func main() { // config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) // config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub) // config.Seal() - cfg, err := mesgconfig.New() + cfg, err := config.New() if err != nil { panic(err) } cosmos.CustomizeConfig(cfg) + // TODO: setup keybase, viper object, etc. to be passed into + // the below functions and eliminate global vars, like we do + // with the cdc + rootCmd := &cobra.Command{ - Use: "mesg-cli", - Short: "mesg app Client", + Use: "acli", + Short: "Command line interface for interacting with appd", } // Add --chain-id to persistent flags and mark it required - rootCmd.PersistentFlags().String(client.FlagChainID, "", "Chain ID of tendermint node") + rootCmd.PersistentFlags().String(flags.FlagChainID, "", "Chain ID of tendermint node") rootCmd.PersistentPreRunE = func(_ *cobra.Command, _ []string) error { return initConfig(rootCmd) } @@ -56,16 +69,18 @@ func main() { client.ConfigCmd(app.DefaultCLIHome), queryCmd(cdc), txCmd(cdc), - client.LineBreak, + flags.LineBreak, lcd.ServeCommand(cdc, registerRoutes), - client.LineBreak, + flags.LineBreak, keys.Commands(), - client.LineBreak, + flags.LineBreak, version.Cmd, - client.NewCompletionCmd(rootCmd, true), + flags.NewCompletionCmd(rootCmd, true), ) - executor := cli.PrepareMainCmd(rootCmd, "MESG", app.DefaultCLIHome) + // Add flags and prefix all env exposed with AA + executor := cli.PrepareMainCmd(rootCmd, "AA", app.DefaultCLIHome) + err := executor.Execute() if err != nil { fmt.Printf("Failed executing CLI command: %s, exiting...\n", err) @@ -73,11 +88,6 @@ func main() { } } -func registerRoutes(rs *lcd.RestServer) { - client.RegisterRoutes(rs.CliCtx, rs.Mux) - app.ModuleBasics.RegisterRESTRoutes(rs.CliCtx, rs.Mux) -} - func queryCmd(cdc *amino.Codec) *cobra.Command { queryCmd := &cobra.Command{ Use: "query", @@ -87,12 +97,12 @@ func queryCmd(cdc *amino.Codec) *cobra.Command { queryCmd.AddCommand( authcmd.GetAccountCmd(cdc), - client.LineBreak, + flags.LineBreak, rpc.ValidatorCommand(cdc), rpc.BlockCommand(), authcmd.QueryTxsByEventsCmd(cdc), authcmd.QueryTxCmd(cdc), - client.LineBreak, + flags.LineBreak, ) // add modules' query commands @@ -109,21 +119,42 @@ func txCmd(cdc *amino.Codec) *cobra.Command { txCmd.AddCommand( bankcmd.SendTxCmd(cdc), - client.LineBreak, + flags.LineBreak, authcmd.GetSignCommand(cdc), authcmd.GetMultiSignCommand(cdc), - client.LineBreak, + flags.LineBreak, authcmd.GetBroadcastCommand(cdc), authcmd.GetEncodeCommand(cdc), - client.LineBreak, + authcmd.GetDecodeCommand(cdc), + flags.LineBreak, ) // add modules' tx commands app.ModuleBasics.AddTxCommands(txCmd, cdc) + // remove auth and bank commands as they're mounted under the root tx command + var cmdsToRemove []*cobra.Command + + for _, cmd := range txCmd.Commands() { + if cmd.Use == auth.ModuleName || cmd.Use == bank.ModuleName { + cmdsToRemove = append(cmdsToRemove, cmd) + } + } + + txCmd.RemoveCommand(cmdsToRemove...) + return txCmd } +// registerRoutes registers the routes from the different modules for the LCD. +// NOTE: details on the routes added for each module are in the module documentation +// NOTE: If making updates here you also need to update the test helper in client/lcd/test_helper.go +func registerRoutes(rs *lcd.RestServer) { + client.RegisterRoutes(rs.CliCtx, rs.Mux) + authrest.RegisterTxRoutes(rs.CliCtx, rs.Mux) + app.ModuleBasics.RegisterRESTRoutes(rs.CliCtx, rs.Mux) +} + func initConfig(cmd *cobra.Command) error { home, err := cmd.PersistentFlags().GetString(cli.HomeFlag) if err != nil { @@ -138,7 +169,7 @@ func initConfig(cmd *cobra.Command) error { return err } } - if err := viper.BindPFlag(client.FlagChainID, cmd.PersistentFlags().Lookup(client.FlagChainID)); err != nil { + if err := viper.BindPFlag(flags.FlagChainID, cmd.PersistentFlags().Lookup(flags.FlagChainID)); err != nil { return err } if err := viper.BindPFlag(cli.EncodingFlag, cmd.PersistentFlags().Lookup(cli.EncodingFlag)); err != nil { diff --git a/cmd/mesg-daemon/genaccounts.go b/cmd/mesg-daemon/genaccounts.go new file mode 100644 index 000000000..90df73f4c --- /dev/null +++ b/cmd/mesg-daemon/genaccounts.go @@ -0,0 +1,153 @@ +package main + +import ( + "bufio" + "errors" + "fmt" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + + "github.com/tendermint/tendermint/libs/cli" + + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/crypto/keys" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/server" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" + authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting" + "github.com/cosmos/cosmos-sdk/x/genutil" +) + +const ( + flagClientHome = "home-client" + flagVestingStart = "vesting-start-time" + flagVestingEnd = "vesting-end-time" + flagVestingAmt = "vesting-amount" +) + +// AddGenesisAccountCmd returns add-genesis-account cobra Command. +func AddGenesisAccountCmd( + ctx *server.Context, cdc *codec.Codec, defaultNodeHome, defaultClientHome string, +) *cobra.Command { + + cmd := &cobra.Command{ + Use: "add-genesis-account [address_or_key_name] [coin][,[coin]]", + Short: "Add a genesis account to genesis.json", + Long: `Add a genesis account to genesis.json. The provided account must specify +the account address or key name and a list of initial coins. If a key name is given, +the address will be looked up in the local Keybase. The list of initial tokens must +contain valid denominations. Accounts may optionally be supplied with vesting parameters. +`, + Args: cobra.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) error { + config := ctx.Config + config.SetRoot(viper.GetString(cli.HomeFlag)) + + addr, err := sdk.AccAddressFromBech32(args[0]) + inBuf := bufio.NewReader(cmd.InOrStdin()) + if err != nil { + // attempt to lookup address from Keybase if no address was provided + kb, err := keys.NewKeyring( + sdk.KeyringServiceName(), + viper.GetString(flags.FlagKeyringBackend), + viper.GetString(flagClientHome), + inBuf, + ) + if err != nil { + return err + } + + info, err := kb.Get(args[0]) + if err != nil { + return fmt.Errorf("failed to get address from Keybase: %w", err) + } + + addr = info.GetAddress() + } + + coins, err := sdk.ParseCoins(args[1]) + if err != nil { + return fmt.Errorf("failed to parse coins: %w", err) + } + + vestingStart := viper.GetInt64(flagVestingStart) + vestingEnd := viper.GetInt64(flagVestingEnd) + vestingAmt, err := sdk.ParseCoins(viper.GetString(flagVestingAmt)) + if err != nil { + return fmt.Errorf("failed to parse vesting amount: %w", err) + } + + // create concrete account type based on input parameters + var genAccount authexported.GenesisAccount + + baseAccount := auth.NewBaseAccount(addr, coins.Sort(), nil, 0, 0) + if !vestingAmt.IsZero() { + baseVestingAccount, err := authvesting.NewBaseVestingAccount(baseAccount, vestingAmt.Sort(), vestingEnd) + if err != nil { + return fmt.Errorf("failed to create base vesting account: %w", err) + } + + switch { + case vestingStart != 0 && vestingEnd != 0: + genAccount = authvesting.NewContinuousVestingAccountRaw(baseVestingAccount, vestingStart) + + case vestingEnd != 0: + genAccount = authvesting.NewDelayedVestingAccountRaw(baseVestingAccount) + + default: + return errors.New("invalid vesting parameters; must supply start and end time or end time") + } + } else { + genAccount = baseAccount + } + + if err := genAccount.Validate(); err != nil { + return fmt.Errorf("failed to validate new genesis account: %w", err) + } + + genFile := config.GenesisFile() + appState, genDoc, err := genutil.GenesisStateFromGenFile(cdc, genFile) + if err != nil { + return fmt.Errorf("failed to unmarshal genesis state: %w", err) + } + + authGenState := auth.GetGenesisStateFromAppState(cdc, appState) + + if authGenState.Accounts.Contains(addr) { + return fmt.Errorf("cannot add account at existing address %s", addr) + } + + // Add the new account to the set of genesis accounts and sanitize the + // accounts afterwards. + authGenState.Accounts = append(authGenState.Accounts, genAccount) + authGenState.Accounts = auth.SanitizeGenesisAccounts(authGenState.Accounts) + + authGenStateBz, err := cdc.MarshalJSON(authGenState) + if err != nil { + return fmt.Errorf("failed to marshal auth genesis state: %w", err) + } + + appState[auth.ModuleName] = authGenStateBz + + appStateJSON, err := cdc.MarshalJSON(appState) + if err != nil { + return fmt.Errorf("failed to marshal application genesis state: %w", err) + } + + genDoc.AppState = appStateJSON + return genutil.ExportGenesisFile(genDoc, genFile) + }, + } + + cmd.Flags().String(cli.HomeFlag, defaultNodeHome, "node's home directory") + cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|test)") + cmd.Flags().String(flagClientHome, defaultClientHome, "client's home directory") + cmd.Flags().String(flagVestingAmt, "", "amount of coins for vesting accounts") + cmd.Flags().Uint64(flagVestingStart, 0, "schedule start time (unix epoch) for vesting accounts") + cmd.Flags().Uint64(flagVestingEnd, 0, "schedule end time (unix epoch) for vesting accounts") + + return cmd +} diff --git a/cmd/mesg-deamon/main.go b/cmd/mesg-daemon/main.go similarity index 66% rename from cmd/mesg-deamon/main.go rename to cmd/mesg-daemon/main.go index 48ea1425a..6fffdcdf2 100644 --- a/cmd/mesg-deamon/main.go +++ b/cmd/mesg-daemon/main.go @@ -4,24 +4,28 @@ import ( "encoding/json" "io" - "github.com/cosmos/cosmos-sdk/server" - "github.com/cosmos/cosmos-sdk/x/genaccounts" - genaccscli "github.com/cosmos/cosmos-sdk/x/genaccounts/client/cli" - "github.com/cosmos/cosmos-sdk/x/staking" "github.com/spf13/cobra" "github.com/spf13/viper" + + abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/cli" "github.com/tendermint/tendermint/libs/log" + tmtypes "github.com/tendermint/tendermint/types" + dbm "github.com/tendermint/tm-db" + + "github.com/mesg-foundation/engine/app" + "github.com/mesg-foundation/engine/config" + "github.com/mesg-foundation/engine/cosmos" + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/client/debug" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/store" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" - app "github.com/mesg-foundation/engine/app" - abci "github.com/tendermint/tendermint/abci/types" - tmtypes "github.com/tendermint/tendermint/types" - dbm "github.com/tendermint/tm-db" - mesgconfig "github.com/mesg-foundation/engine/config" - "github.com/mesg-foundation/engine/cosmos" + "github.com/cosmos/cosmos-sdk/x/staking" ) const flagInvCheckPeriod = "inv-check-period" @@ -36,7 +40,7 @@ func main() { // config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) // config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub) // config.Seal() - cfg, err := mesgconfig.New() + cfg, err := config.New() if err != nil { panic(err) } @@ -45,22 +49,24 @@ func main() { ctx := server.NewDefaultContext() cobra.EnableCommandSorting = false rootCmd := &cobra.Command{ - Use: "mesg-deamon", - Short: "mesg app Daemon (server)", + Use: "aud", + Short: "app Daemon (server)", PersistentPreRunE: server.PersistentPreRunEFn(ctx), } + rootCmd.AddCommand(genutilcli.InitCmd(ctx, cdc, app.ModuleBasics, app.DefaultNodeHome)) + rootCmd.AddCommand(genutilcli.CollectGenTxsCmd(ctx, cdc, auth.GenesisAccountIterator{}, app.DefaultNodeHome)) + rootCmd.AddCommand(genutilcli.MigrateGenesisCmd(ctx, cdc)) rootCmd.AddCommand( - genutilcli.InitCmd(ctx, cdc, app.ModuleBasics, app.DefaultNodeHome), - genutilcli.CollectGenTxsCmd(ctx, cdc, genaccounts.AppModuleBasic{}, app.DefaultNodeHome), genutilcli.GenTxCmd( ctx, cdc, app.ModuleBasics, staking.AppModuleBasic{}, - genaccounts.AppModuleBasic{}, app.DefaultNodeHome, app.DefaultCLIHome, + auth.GenesisAccountIterator{}, app.DefaultNodeHome, app.DefaultCLIHome, ), - genutilcli.ValidateGenesisCmd(ctx, cdc, app.ModuleBasics), - // AddGenesisAccountCmd allows users to add accounts to the genesis file - genaccscli.AddGenesisAccountCmd(ctx, cdc, app.DefaultNodeHome, app.DefaultCLIHome), ) + rootCmd.AddCommand(genutilcli.ValidateGenesisCmd(ctx, cdc, app.ModuleBasics)) + rootCmd.AddCommand(AddGenesisAccountCmd(ctx, cdc, app.DefaultNodeHome, app.DefaultCLIHome)) + rootCmd.AddCommand(flags.NewCompletionCmd(rootCmd, true)) + rootCmd.AddCommand(debug.Cmd(cdc)) server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators) @@ -75,11 +81,19 @@ func main() { } func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer) abci.Application { + var cache sdk.MultiStorePersistentCache + + if viper.GetBool(server.FlagInterBlockCache) { + cache = store.NewCommitKVStoreCacheManager() + } + return app.NewInitApp( logger, db, traceStore, true, invCheckPeriod, baseapp.SetPruning(store.NewPruningOptionsFromString(viper.GetString("pruning"))), baseapp.SetMinGasPrices(viper.GetString(server.FlagMinGasPrices)), - baseapp.SetHaltHeight(uint64(viper.GetInt(server.FlagHaltHeight))), + baseapp.SetHaltHeight(viper.GetUint64(server.FlagHaltHeight)), + baseapp.SetHaltTime(viper.GetUint64(server.FlagHaltTime)), + baseapp.SetInterBlockCache(cache), ) } @@ -88,7 +102,7 @@ func exportAppStateAndTMValidators( ) (json.RawMessage, []tmtypes.GenesisValidator, error) { if height != -1 { - aApp := app.NewInitApp(logger, db, traceStore, true, uint(1)) + aApp := app.NewInitApp(logger, db, traceStore, false, uint(1)) err := aApp.LoadHeight(height) if err != nil { return nil, nil, err diff --git a/core/main.go b/core/main.go index 23a98d9b7..8c63aa66b 100644 --- a/core/main.go +++ b/core/main.go @@ -6,12 +6,13 @@ import ( "strconv" "sync" + bam "github.com/cosmos/cosmos-sdk/baseapp" cosmosclient "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/crypto/keys" - "github.com/cosmos/cosmos-sdk/types/module" authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest" "github.com/gorilla/mux" + "github.com/mesg-foundation/engine/app" "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/config" "github.com/mesg-foundation/engine/container" @@ -92,7 +93,7 @@ func loadOrGenConfigAccount(kb *cosmos.Keybase, cfg *config.Config) (keys.Info, return kb.CreateAccount(cfg.Account.Name, mnemonic, "", cfg.Account.Password, keys.CreateHDPath(cfg.Account.Number, cfg.Account.Index).String(), cosmos.DefaultAlgo) } -func loadOrGenDevGenesis(basicManager *module.BasicManager, kb *cosmos.Keybase, cfg *config.Config) (*tmtypes.GenesisDoc, error) { +func loadOrGenDevGenesis(kb *cosmos.Keybase, cfg *config.Config) (*tmtypes.GenesisDoc, error) { if cosmos.GenesisExist(cfg.Tendermint.Config.GenesisFile()) { return cosmos.LoadGenesis(cfg.Tendermint.Config.GenesisFile()) } @@ -112,7 +113,7 @@ func loadOrGenDevGenesis(basicManager *module.BasicManager, kb *cosmos.Keybase, "nodeID": validator.NodeID, "peer": fmt.Sprintf("%s@%s:26656", validator.NodeID, validator.Name), }).Warnln("Validator") - return cosmos.GenGenesis(kb, basicManager.DefaultGenesis(), cfg.DevGenesis.ChainID, cfg.DevGenesis.InitialBalances, cfg.DevGenesis.ValidatorDelegationCoin, cfg.Tendermint.Config.GenesisFile(), []cosmos.GenesisValidator{validator}) + return cosmos.GenGenesis(kb, app.NewDefaultGenesisState(), cfg.DevGenesis.ChainID, cfg.DevGenesis.InitialBalances, cfg.DevGenesis.ValidatorDelegationCoin, cfg.Tendermint.Config.GenesisFile(), []cosmos.GenesisValidator{validator}) } func main() { @@ -128,7 +129,7 @@ func main() { logger.Init(cfg.Log.Format, cfg.Log.Level, cfg.Log.ForceColors) // init basicManager - basicManager := enginesdk.NewBasicManager() + // basicManager := enginesdk.NewBasicManager() // init tendermint logger tendermintLogger := logger.TendermintLogger() @@ -148,10 +149,11 @@ func main() { } // init app - app, err := enginesdk.NewApp(tendermintLogger, db, cfg.Cosmos.MinGasPrices) - if err != nil { - logrus.WithField("module", "main").Fatalln(err) - } + // app, err := enginesdk.NewApp(tendermintLogger, db, cfg.Cosmos.MinGasPrices) + initApp := app.NewInitApp(tendermintLogger, db, nil, true, 0, bam.SetMinGasPrices(cfg.Cosmos.MinGasPrices)) + // if err != nil { + // logrus.WithField("module", "main").Fatalln(err) + // } // init key manager kb, err := cosmos.NewKeybase(filepath.Join(cfg.Path, cfg.Cosmos.RelativePath)) @@ -167,13 +169,13 @@ func main() { logrus.WithField("address", acc.GetAddress().String()).Info("engine account") // load or gen genesis - genesis, err := loadOrGenDevGenesis(basicManager, kb, cfg) + genesis, err := loadOrGenDevGenesis(kb, cfg) if err != nil { logrus.WithField("module", "main").Fatalln(err) } // create cosmos node - node, err := cosmos.NewNode(app, cfg.Tendermint.Config, genesis) + node, err := cosmos.NewNode(initApp.BaseApp, cfg.Tendermint.Config, genesis) if err != nil { logrus.WithField("module", "main").Fatalln(err) } @@ -231,7 +233,7 @@ func main() { mux := mux.NewRouter() cosmosclient.RegisterRoutes(cliCtx, mux) authrest.RegisterTxRoutes(cliCtx, mux) - basicManager.RegisterRESTRoutes(cliCtx, mux) + app.ModuleBasics.RegisterRESTRoutes(cliCtx, mux) go func() { if err := rpcserver.StartHTTPServer(lcdServer, mux, tendermintLogger, cfgLcd); err != nil { logrus.WithField("module", "main").Warnln(err) // not a fatal because closing the connection return an error here From 55d3f6175c6a0f36ff37de024804f969ae2ce48c Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 6 Feb 2020 12:36:06 +0700 Subject: [PATCH 12/87] Remove now useless sdk/modules.go file and previous cosmos cmds --- Makefile | 6 +- cmd/mesg-cli/main.go | 113 ++++++++++++++- cmd/mesg-cosmos-daemon/main.go | 101 -------------- cmd/mesg-cosmos/main.go | 246 --------------------------------- cmd/mesg-daemon/main.go | 24 +++- sdk/modules.go | 244 -------------------------------- 6 files changed, 137 insertions(+), 597 deletions(-) delete mode 100644 cmd/mesg-cosmos-daemon/main.go delete mode 100644 cmd/mesg-cosmos/main.go delete mode 100644 sdk/modules.go diff --git a/Makefile b/Makefile index 0c21d80b3..f83e06987 100644 --- a/Makefile +++ b/Makefile @@ -54,9 +54,9 @@ dep: build: check-version dep go build -mod=readonly -o ./bin/engine -ldflags="-X 'github.com/mesg-foundation/engine/version.Version=$(version)'" core/main.go -build-cmd-cosmos: dep - go build -mod=readonly -o ./bin/mesg-cosmos ./cmd/mesg-cosmos/main.go - go build -mod=readonly -o ./bin/mesg-cosmos-daemon ./cmd/mesg-cosmos-daemon/main.go +build-cmd: dep + go build -mod=readonly -o ./bin/mesg-cli ./cmd/mesg-cli/ + go build -mod=readonly -o ./bin/mesg-daemon ./cmd/mesg-daemon/ e2e: docker-dev ./scripts/run-e2e.sh diff --git a/cmd/mesg-cli/main.go b/cmd/mesg-cli/main.go index 57e9a6b81..e5f0e0369 100644 --- a/cmd/mesg-cli/main.go +++ b/cmd/mesg-cli/main.go @@ -10,7 +10,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/client/lcd" "github.com/cosmos/cosmos-sdk/client/rpc" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/x/auth" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" @@ -81,7 +80,7 @@ func main() { // Add flags and prefix all env exposed with AA executor := cli.PrepareMainCmd(rootCmd, "AA", app.DefaultCLIHome) - err := executor.Execute() + err = executor.Execute() if err != nil { fmt.Printf("Failed executing CLI command: %s, exiting...\n", err) os.Exit(1) @@ -177,3 +176,113 @@ func initConfig(cmd *cobra.Command) error { } return viper.BindPFlag(cli.OutputFlag, cmd.PersistentFlags().Lookup(cli.OutputFlag)) } + +/* +List of commands that could be used in this cli + +distribution +func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command { + func GetCmdWithdrawRewards(cdc *codec.Codec) *cobra.Command { + func GetCmdWithdrawAllRewards(cdc *codec.Codec, queryRoute string) *cobra.Command { + func GetCmdSetWithdrawAddr(cdc *codec.Codec) *cobra.Command { +func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryValidatorOutstandingRewards(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryValidatorCommission(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryValidatorSlashes(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryDelegatorRewards(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryCommunityPool(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetCmdSubmitProposal(cdc *codec.Codec) *cobra.Command { + +gov +func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryProposal(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryProposals(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryVote(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryVotes(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryDeposit(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryDeposits(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryTally(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryParam(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryProposer(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetTxCmd(storeKey string, cdc *codec.Codec, pcmds []*cobra.Command) *cobra.Command { + func GetCmdSubmitProposal(cdc *codec.Codec) *cobra.Command { + func GetCmdDeposit(cdc *codec.Codec) *cobra.Command { + func GetCmdVote(cdc *codec.Codec) *cobra.Command { + +bank +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + func SendTxCmd(cdc *codec.Codec) *cobra.Command { + +params +func GetCmdSubmitProposal(cdc *codec.Codec) *cobra.Command { + +auth +func GetQueryCmd(cdc *codec.Codec) *cobra.Command { + func GetAccountCmd(cdc *codec.Codec) *cobra.Command { +func QueryTxsByEventsCmd(cdc *codec.Codec) *cobra.Command { +func QueryTxCmd(cdc *codec.Codec) *cobra.Command { +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + func GetSignCommand(codec *codec.Codec) *cobra.Command { + func GetMultiSignCommand(cdc *codec.Codec) *cobra.Command { +func GetBroadcastCommand(cdc *codec.Codec) *cobra.Command { +func GetEncodeCommand(cdc *codec.Codec) *cobra.Command { + +staking +func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryValidator(storeName string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryValidators(storeName string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryValidatorUnbondingDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryValidatorRedelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryDelegation(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryValidatorDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryUnbondingDelegation(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryUnbondingDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryRedelegation(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryRedelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryPool(storeName string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryParams(storeName string, cdc *codec.Codec) *cobra.Command { +func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command { + func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command { + func GetCmdEditValidator(cdc *codec.Codec) *cobra.Command { + func GetCmdDelegate(cdc *codec.Codec) *cobra.Command { + func GetCmdRedelegate(storeName string, cdc *codec.Codec) *cobra.Command { + func GetCmdUnbond(storeName string, cdc *codec.Codec) *cobra.Command { + +crisis +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + func GetCmdInvariantBroken(cdc *codec.Codec) *cobra.Command { + +supply +func GetQueryCmd(cdc *codec.Codec) *cobra.Command { + func GetCmdQueryTotalSupply(cdc *codec.Codec) *cobra.Command { + +slashing +func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { + func GetCmdQuerySigningInfo(storeName string, cdc *codec.Codec) *cobra.Command { + func GetCmdQueryParams(cdc *codec.Codec) *cobra.Command { +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + func GetCmdUnjail(cdc *codec.Codec) *cobra.Command { + +mint +func GetQueryCmd(cdc *codec.Codec) *cobra.Command { + func GetCmdQueryParams(cdc *codec.Codec) *cobra.Command { + func GetCmdQueryInflation(cdc *codec.Codec) *cobra.Command { + func GetCmdQueryAnnualProvisions(cdc *codec.Codec) *cobra.Command { + +client +func ConfigCmd(defaultCLIHome string) *cobra.Command { + +client/lcd +func ServeCommand(cdc *codec.Codec, registerRoutesFn func(*RestServer)) *cobra.Command { + +client/keys +func Commands() *cobra.Command { + +client/rpc +func ValidatorCommand(cdc *codec.Codec) *cobra.Command { +func StatusCommand() *cobra.Command { +func BlockCommand() *cobra.Command { +*/ diff --git a/cmd/mesg-cosmos-daemon/main.go b/cmd/mesg-cosmos-daemon/main.go deleted file mode 100644 index c671604ac..000000000 --- a/cmd/mesg-cosmos-daemon/main.go +++ /dev/null @@ -1,101 +0,0 @@ -package main - -import ( - "encoding/json" - "io" - "os" - - "github.com/cosmos/cosmos-sdk/server" - "github.com/cosmos/cosmos-sdk/types/module" - "github.com/cosmos/cosmos-sdk/x/auth" - genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" - "github.com/cosmos/cosmos-sdk/x/staking" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/config" - "github.com/mesg-foundation/engine/cosmos" - enginesdk "github.com/mesg-foundation/engine/sdk" - "github.com/spf13/cobra" - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/libs/cli" - "github.com/tendermint/tendermint/libs/log" - tmtypes "github.com/tendermint/tendermint/types" - db "github.com/tendermint/tm-db" -) - -var ( - defaultCLIHome = os.ExpandEnv("$HOME/.mesg-cosmos-cli") - basicManager module.BasicManager -) - -func main() { - cobra.EnableCommandSorting = false - - // init app and codec - cfg, err := config.New() - if err != nil { - panic(err) - } - cosmos.CustomizeConfig(cfg) - - basicManager = *enginesdk.NewBasicManager() - cdc := codec.Codec - - ctx := server.NewDefaultContext() - - rootCmd := &cobra.Command{ - Use: "mesg-cosmos-daemon", - Short: "ClI Daemon (server)", - PersistentPreRunE: server.PersistentPreRunEFn(ctx), - } - // CLI commands to initialize the chain - rootCmd.AddCommand( - genutilcli.InitCmd(ctx, cdc, basicManager, cfg.Tendermint.Config.RootDir), - genutilcli.CollectGenTxsCmd(ctx, cdc, auth.GenesisAccountIterator{}, cfg.Tendermint.Config.RootDir), - genutilcli.GenTxCmd( - ctx, cdc, basicManager, staking.AppModuleBasic{}, - auth.GenesisAccountIterator{}, cfg.Tendermint.Config.RootDir, defaultCLIHome, - ), - genutilcli.ValidateGenesisCmd(ctx, cdc, basicManager), - ) - - server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators) - - // prepare and add flags - executor := cli.PrepareBaseCmd(rootCmd, "MESG", cfg.Tendermint.Config.RootDir) - err = executor.Execute() - if err != nil { - panic(err) - } -} - -func newApp(logger log.Logger, db db.DB, traceStore io.Writer) abci.Application { - // TODO: could start the engine here - return nil -} - -func exportAppStateAndTMValidators(logger log.Logger, db db.DB, traceStore io.Writer, height int64, forZeroHeight bool, jailWhiteList []string, -) (json.RawMessage, []tmtypes.GenesisValidator, error) { - return nil, nil, nil -} - -/* -genaccounts -func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec, defaultNodeHome, defaultClientHome string) *cobra.Command { - -genutil -func CollectGenTxsCmd(ctx *server.Context, cdc *codec.Codec, genAccIterator types.GenesisAccountsIterator, defaultNodeHome string) *cobra.Command { -func GenTxCmd(ctx *server.Context, cdc *codec.Codec, mbm module.BasicManager, smbh StakingMsgBuildingHelpers, genAccIterator types.GenesisAccountsIterator, defaultNodeHome, defaultCLIHome string) *cobra.Command { -func InitCmd(ctx *server.Context, cdc *codec.Codec, mbm module.BasicManager, defaultNodeHome string) *cobra.Command { -func MigrateGenesisCmd(_ *server.Context, cdc *codec.Codec) *cobra.Command { -func ValidateGenesisCmd(ctx *server.Context, cdc *codec.Codec, mbm module.BasicManager) *cobra.Command { - -server -func AddCommands(ctx *Context, cdc *codec.Codec, rootCmd *cobra.Command, appCreator AppCreator, appExport AppExporter) { - func StartCmd(ctx *Context, appCreator AppCreator) *cobra.Command { - func ExportCmd(ctx *Context, cdc *codec.Codec, appExporter AppExporter) *cobra.Command { - func ShowNodeIDCmd(ctx *Context) *cobra.Command { - func ShowValidatorCmd(ctx *Context) *cobra.Command { - func ShowAddressCmd(ctx *Context) *cobra.Command { - func VersionCmd(ctx *Context) *cobra.Command { - func UnsafeResetAllCmd(ctx *Context) *cobra.Command { -*/ diff --git a/cmd/mesg-cosmos/main.go b/cmd/mesg-cosmos/main.go deleted file mode 100644 index c6babdfe8..000000000 --- a/cmd/mesg-cosmos/main.go +++ /dev/null @@ -1,246 +0,0 @@ -package main - -import ( - "os" - "path" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/client/keys" - "github.com/cosmos/cosmos-sdk/client/lcd" - "github.com/cosmos/cosmos-sdk/client/rpc" - "github.com/cosmos/cosmos-sdk/types/module" - "github.com/cosmos/cosmos-sdk/version" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" - authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/config" - "github.com/mesg-foundation/engine/cosmos" - enginesdk "github.com/mesg-foundation/engine/sdk" - "github.com/spf13/cobra" - "github.com/spf13/viper" - amino "github.com/tendermint/go-amino" - "github.com/tendermint/tendermint/libs/cli" -) - -var ( - defaultCLIHome = os.ExpandEnv("$HOME/.mesg-cosmos-cli") - basicManager module.BasicManager -) - -func main() { - // init app and codec - cfg, err := config.New() - if err != nil { - panic(err) - } - cosmos.CustomizeConfig(cfg) - - basicManager = *enginesdk.NewBasicManager() - cdc := codec.Codec - - rootCmd := &cobra.Command{ - Use: "mesg-cosmos", - Short: "Cosmos Client", - } - - // Add --chain-id to persistent flags and mark it required - rootCmd.PersistentFlags().String(flags.FlagChainID, "", "Chain ID of tendermint node") - rootCmd.PersistentPreRunE = func(_ *cobra.Command, _ []string) error { - return initConfig(rootCmd) - } - - // Construct Root Command - rootCmd.AddCommand( - rpc.StatusCommand(), - client.ConfigCmd(defaultCLIHome), - queryCmd(cdc), - txCmd(cdc), - lcd.ServeCommand(cdc, registerRoutes), - keys.Commands(), - version.Cmd, - flags.NewCompletionCmd(rootCmd, true), - ) - - executor := cli.PrepareMainCmd(rootCmd, "MESG", defaultCLIHome) - err = executor.Execute() - if err != nil { - panic(err) - } -} - -func registerRoutes(rs *lcd.RestServer) { - client.RegisterRoutes(rs.CliCtx, rs.Mux) - authrest.RegisterTxRoutes(rs.CliCtx, rs.Mux) - basicManager.RegisterRESTRoutes(rs.CliCtx, rs.Mux) -} - -func queryCmd(cdc *amino.Codec) *cobra.Command { - queryCmd := &cobra.Command{ - Use: "query", - Aliases: []string{"q"}, - Short: "Querying subcommands", - } - - queryCmd.AddCommand( - rpc.ValidatorCommand(cdc), - rpc.BlockCommand(), - authcmd.QueryTxsByEventsCmd(cdc), - authcmd.QueryTxCmd(cdc), - ) - - // add modules' query commands - basicManager.AddQueryCommands(queryCmd, cdc) - - return queryCmd -} - -func txCmd(cdc *amino.Codec) *cobra.Command { - txCmd := &cobra.Command{ - Use: "tx", - Short: "Transactions subcommands", - } - - txCmd.AddCommand( - authcmd.GetBroadcastCommand(cdc), - authcmd.GetEncodeCommand(cdc), - ) - - // add modules' tx commands - basicManager.AddTxCommands(txCmd, cdc) - - return txCmd -} - -func initConfig(cmd *cobra.Command) error { - home, err := cmd.PersistentFlags().GetString(cli.HomeFlag) - if err != nil { - return err - } - - cfgFile := path.Join(home, "config", "config.toml") - if _, err := os.Stat(cfgFile); err == nil { - viper.SetConfigFile(cfgFile) - - if err := viper.ReadInConfig(); err != nil { - return err - } - } - if err := viper.BindPFlag(flags.FlagChainID, cmd.PersistentFlags().Lookup(flags.FlagChainID)); err != nil { - return err - } - if err := viper.BindPFlag(cli.EncodingFlag, cmd.PersistentFlags().Lookup(cli.EncodingFlag)); err != nil { - return err - } - return viper.BindPFlag(cli.OutputFlag, cmd.PersistentFlags().Lookup(cli.OutputFlag)) -} - -/* -List of commands that could be used in this cli - -distribution -func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command { - func GetCmdWithdrawRewards(cdc *codec.Codec) *cobra.Command { - func GetCmdWithdrawAllRewards(cdc *codec.Codec, queryRoute string) *cobra.Command { - func GetCmdSetWithdrawAddr(cdc *codec.Codec) *cobra.Command { -func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryValidatorOutstandingRewards(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryValidatorCommission(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryValidatorSlashes(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryDelegatorRewards(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryCommunityPool(queryRoute string, cdc *codec.Codec) *cobra.Command { -func GetCmdSubmitProposal(cdc *codec.Codec) *cobra.Command { - -gov -func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryProposal(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryProposals(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryVote(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryVotes(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryDeposit(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryDeposits(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryTally(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryParam(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryProposer(queryRoute string, cdc *codec.Codec) *cobra.Command { -func GetTxCmd(storeKey string, cdc *codec.Codec, pcmds []*cobra.Command) *cobra.Command { - func GetCmdSubmitProposal(cdc *codec.Codec) *cobra.Command { - func GetCmdDeposit(cdc *codec.Codec) *cobra.Command { - func GetCmdVote(cdc *codec.Codec) *cobra.Command { - -bank -func GetTxCmd(cdc *codec.Codec) *cobra.Command { - func SendTxCmd(cdc *codec.Codec) *cobra.Command { - -params -func GetCmdSubmitProposal(cdc *codec.Codec) *cobra.Command { - -auth -func GetQueryCmd(cdc *codec.Codec) *cobra.Command { - func GetAccountCmd(cdc *codec.Codec) *cobra.Command { -func QueryTxsByEventsCmd(cdc *codec.Codec) *cobra.Command { -func QueryTxCmd(cdc *codec.Codec) *cobra.Command { -func GetTxCmd(cdc *codec.Codec) *cobra.Command { - func GetSignCommand(codec *codec.Codec) *cobra.Command { - func GetMultiSignCommand(cdc *codec.Codec) *cobra.Command { -func GetBroadcastCommand(cdc *codec.Codec) *cobra.Command { -func GetEncodeCommand(cdc *codec.Codec) *cobra.Command { - -staking -func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryValidator(storeName string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryValidators(storeName string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryValidatorUnbondingDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryValidatorRedelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryDelegation(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryValidatorDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryUnbondingDelegation(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryUnbondingDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryRedelegation(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryRedelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryPool(storeName string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryParams(storeName string, cdc *codec.Codec) *cobra.Command { -func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command { - func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command { - func GetCmdEditValidator(cdc *codec.Codec) *cobra.Command { - func GetCmdDelegate(cdc *codec.Codec) *cobra.Command { - func GetCmdRedelegate(storeName string, cdc *codec.Codec) *cobra.Command { - func GetCmdUnbond(storeName string, cdc *codec.Codec) *cobra.Command { - -crisis -func GetTxCmd(cdc *codec.Codec) *cobra.Command { - func GetCmdInvariantBroken(cdc *codec.Codec) *cobra.Command { - -supply -func GetQueryCmd(cdc *codec.Codec) *cobra.Command { - func GetCmdQueryTotalSupply(cdc *codec.Codec) *cobra.Command { - -slashing -func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { - func GetCmdQuerySigningInfo(storeName string, cdc *codec.Codec) *cobra.Command { - func GetCmdQueryParams(cdc *codec.Codec) *cobra.Command { -func GetTxCmd(cdc *codec.Codec) *cobra.Command { - func GetCmdUnjail(cdc *codec.Codec) *cobra.Command { - -mint -func GetQueryCmd(cdc *codec.Codec) *cobra.Command { - func GetCmdQueryParams(cdc *codec.Codec) *cobra.Command { - func GetCmdQueryInflation(cdc *codec.Codec) *cobra.Command { - func GetCmdQueryAnnualProvisions(cdc *codec.Codec) *cobra.Command { - -client -func ConfigCmd(defaultCLIHome string) *cobra.Command { - -client/lcd -func ServeCommand(cdc *codec.Codec, registerRoutesFn func(*RestServer)) *cobra.Command { - -client/keys -func Commands() *cobra.Command { - -client/rpc -func ValidatorCommand(cdc *codec.Codec) *cobra.Command { -func StatusCommand() *cobra.Command { -func BlockCommand() *cobra.Command { -*/ diff --git a/cmd/mesg-daemon/main.go b/cmd/mesg-daemon/main.go index 6fffdcdf2..82bb908af 100644 --- a/cmd/mesg-daemon/main.go +++ b/cmd/mesg-daemon/main.go @@ -74,7 +74,7 @@ func main() { executor := cli.PrepareBaseCmd(rootCmd, "AU", app.DefaultNodeHome) rootCmd.PersistentFlags().UintVar(&invCheckPeriod, flagInvCheckPeriod, 0, "Assert registered invariants every N blocks") - err := executor.Execute() + err = executor.Execute() if err != nil { panic(err) } @@ -114,3 +114,25 @@ func exportAppStateAndTMValidators( return aApp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList) } + +/* +genaccounts +func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec, defaultNodeHome, defaultClientHome string) *cobra.Command { + +genutil +func CollectGenTxsCmd(ctx *server.Context, cdc *codec.Codec, genAccIterator types.GenesisAccountsIterator, defaultNodeHome string) *cobra.Command { +func GenTxCmd(ctx *server.Context, cdc *codec.Codec, mbm module.BasicManager, smbh StakingMsgBuildingHelpers, genAccIterator types.GenesisAccountsIterator, defaultNodeHome, defaultCLIHome string) *cobra.Command { +func InitCmd(ctx *server.Context, cdc *codec.Codec, mbm module.BasicManager, defaultNodeHome string) *cobra.Command { +func MigrateGenesisCmd(_ *server.Context, cdc *codec.Codec) *cobra.Command { +func ValidateGenesisCmd(ctx *server.Context, cdc *codec.Codec, mbm module.BasicManager) *cobra.Command { + +server +func AddCommands(ctx *Context, cdc *codec.Codec, rootCmd *cobra.Command, appCreator AppCreator, appExport AppExporter) { + func StartCmd(ctx *Context, appCreator AppCreator) *cobra.Command { + func ExportCmd(ctx *Context, cdc *codec.Codec, appExporter AppExporter) *cobra.Command { + func ShowNodeIDCmd(ctx *Context) *cobra.Command { + func ShowValidatorCmd(ctx *Context) *cobra.Command { + func ShowAddressCmd(ctx *Context) *cobra.Command { + func VersionCmd(ctx *Context) *cobra.Command { + func UnsafeResetAllCmd(ctx *Context) *cobra.Command { +*/ diff --git a/sdk/modules.go b/sdk/modules.go deleted file mode 100644 index 0f37303e2..000000000 --- a/sdk/modules.go +++ /dev/null @@ -1,244 +0,0 @@ -package sdk - -import ( - bam "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/simapp" - cosmostypes "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/module" - "github.com/cosmos/cosmos-sdk/version" - "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/auth/ante" - "github.com/cosmos/cosmos-sdk/x/auth/vesting" - "github.com/cosmos/cosmos-sdk/x/bank" - "github.com/cosmos/cosmos-sdk/x/distribution" - "github.com/cosmos/cosmos-sdk/x/genutil" - "github.com/cosmos/cosmos-sdk/x/params" - "github.com/cosmos/cosmos-sdk/x/slashing" - "github.com/cosmos/cosmos-sdk/x/staking" - "github.com/cosmos/cosmos-sdk/x/supply" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/cosmos" - executionsdk "github.com/mesg-foundation/engine/sdk/execution" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" - ownershipsdk "github.com/mesg-foundation/engine/sdk/ownership" - processsdk "github.com/mesg-foundation/engine/sdk/process" - runnersdk "github.com/mesg-foundation/engine/sdk/runner" - servicesdk "github.com/mesg-foundation/engine/sdk/service" - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/libs/log" - dbm "github.com/tendermint/tm-db" -) - -// NewBasicManager returns a basic manager with all the app's modules. -func NewBasicManager() *module.BasicManager { - basicManager := module.NewBasicManager( - params.AppModuleBasic{}, - genutil.AppModuleBasic{}, - auth.AppModuleBasic{}, - bank.AppModuleBasic{}, - supply.AppModuleBasic{}, - distribution.AppModuleBasic{}, - slashing.AppModuleBasic{}, - staking.AppModuleBasic{}, - - cosmos.NewAppModuleBasic(ownershipsdk.ModuleName), - cosmos.NewAppModuleBasic(servicesdk.ModuleName), - cosmos.NewAppModuleBasic(instancesdk.ModuleName), - cosmos.NewAppModuleBasic(runnersdk.ModuleName), - cosmos.NewAppModuleBasic(executionsdk.ModuleName), - cosmos.NewAppModuleBasic(processsdk.ModuleName), - ) - vesting.RegisterCodec(codec.Codec) - basicManager.RegisterCodec(codec.Codec) - return &basicManager -} - -// NewApp returns a initialized and loaded cosmos app. -func NewApp(logger log.Logger, db dbm.DB, minGasPrices string) (*bam.BaseApp, error) { - // init cosmos stores - mainStoreKey := cosmostypes.NewKVStoreKey(bam.MainStoreKey) - paramsStoreKey := cosmostypes.NewKVStoreKey(params.StoreKey) - paramsTStoreKey := cosmostypes.NewTransientStoreKey(params.TStoreKey) - supplyStoreKey := cosmostypes.NewKVStoreKey(supply.StoreKey) - stakingStoreKey := cosmostypes.NewKVStoreKey(staking.StoreKey) - stakingTStoreKey := cosmostypes.NewTransientStoreKey(staking.TStoreKey) - distrStoreKey := cosmostypes.NewKVStoreKey(distribution.StoreKey) - slashingStoreKey := cosmostypes.NewKVStoreKey(slashing.StoreKey) - - ownershipStoreKey := cosmostypes.NewKVStoreKey(ownershipsdk.ModuleName) - serviceStoreKey := cosmostypes.NewKVStoreKey(servicesdk.ModuleName) - instanceStoreKey := cosmostypes.NewKVStoreKey(instancesdk.ModuleName) - runnerStoreKey := cosmostypes.NewKVStoreKey(runnersdk.ModuleName) - executionStoreKey := cosmostypes.NewKVStoreKey(executionsdk.ModuleName) - processStoreKey := cosmostypes.NewKVStoreKey(processsdk.ModuleName) - - // account permissions - maccPerms := map[string][]string{ - auth.FeeCollectorName: nil, - distribution.ModuleName: nil, - staking.BondedPoolName: {supply.Burner, supply.Staking}, - staking.NotBondedPoolName: {supply.Burner, supply.Staking}, - } - // Module Accounts Addresses - modAccAddrs := make(map[string]bool) - for acc := range maccPerms { - modAccAddrs[supply.NewModuleAddress(acc).String()] = true - } - - // init cosmos keepers - paramsKeeper := params.NewKeeper( - codec.Codec, - paramsStoreKey, - paramsTStoreKey, - ) - - accountKeeper := auth.NewAccountKeeper( - codec.Codec, - paramsStoreKey, - paramsKeeper.Subspace(auth.DefaultParamspace), - auth.ProtoBaseAccount, - ) - bankKeeper := bank.NewBaseKeeper( - accountKeeper, - paramsKeeper.Subspace(bank.DefaultParamspace), - modAccAddrs, - ) - supplyKeeper := supply.NewKeeper( - codec.Codec, - supplyStoreKey, - accountKeeper, - bankKeeper, - maccPerms, - ) - stakingKeeper := staking.NewKeeper( - codec.Codec, - stakingStoreKey, - supplyKeeper, - paramsKeeper.Subspace(staking.DefaultParamspace), - ) - distrKeeper := distribution.NewKeeper( - codec.Codec, - distrStoreKey, - paramsKeeper.Subspace(distribution.DefaultParamspace), - &stakingKeeper, - supplyKeeper, - auth.FeeCollectorName, - modAccAddrs, - ) - slashingKeeper := slashing.NewKeeper( - codec.Codec, - slashingStoreKey, - &stakingKeeper, - paramsKeeper.Subspace(slashing.DefaultParamspace), - ) - stakingKeeper = *stakingKeeper.SetHooks( - staking.NewMultiStakingHooks( - distrKeeper.Hooks(), - slashingKeeper.Hooks(), - ), - ) - - ownershipKeeper := ownershipsdk.NewKeeper(ownershipStoreKey) - serviceKeeper := servicesdk.NewKeeper(serviceStoreKey, ownershipKeeper) - instanceKeeper := instancesdk.NewKeeper(instanceStoreKey) - runnerKeeper := runnersdk.NewKeeper(runnerStoreKey, instanceKeeper) - processKeeper := processsdk.NewKeeper(processStoreKey, ownershipKeeper, instanceKeeper) - executionKeeper := executionsdk.NewKeeper(executionStoreKey, serviceKeeper, instanceKeeper, runnerKeeper, processKeeper) - - // init app - // TODO: engine should be in config - app := bam.NewBaseApp("engine", logger, db, auth.DefaultTxDecoder(codec.Codec), bam.SetMinGasPrices(minGasPrices)) - app.SetAppVersion(version.Version) - - // init module manager - manager := module.NewManager( - genutil.NewAppModule(accountKeeper, stakingKeeper, app.DeliverTx), - auth.NewAppModule(accountKeeper), - bank.NewAppModule(bankKeeper, accountKeeper), - supply.NewAppModule(supplyKeeper, accountKeeper), - distribution.NewAppModule(distrKeeper, accountKeeper, supplyKeeper, stakingKeeper), - slashing.NewAppModule(slashingKeeper, accountKeeper, stakingKeeper), - - ownershipsdk.NewModule(ownershipKeeper), - servicesdk.NewModule(serviceKeeper), - instancesdk.NewModule(instanceKeeper), - runnersdk.NewModule(runnerKeeper), - executionsdk.NewModule(executionKeeper), - processsdk.NewModule(processKeeper), - - staking.NewAppModule(stakingKeeper, accountKeeper, supplyKeeper), - ) - manager.SetOrderBeginBlockers(distribution.ModuleName, slashing.ModuleName) - manager.SetOrderEndBlockers(staking.ModuleName) - manager.SetOrderInitGenesis( - distribution.ModuleName, - staking.ModuleName, - auth.ModuleName, - bank.ModuleName, - slashing.ModuleName, - - // app module - ownershipsdk.ModuleName, - servicesdk.ModuleName, - instancesdk.ModuleName, - runnersdk.ModuleName, - executionsdk.ModuleName, - processsdk.ModuleName, - - // genutil should be last module - supply.ModuleName, - genutil.ModuleName, - ) - - // register app to manager - manager.RegisterRoutes(app.Router(), app.QueryRouter()) - - app.SetInitChainer(func(ctx cosmostypes.Context, req abci.RequestInitChain) abci.ResponseInitChain { - var genesisState simapp.GenesisState - codec.Codec.MustUnmarshalJSON(req.AppStateBytes, &genesisState) - return manager.InitGenesis(ctx, genesisState) - }) - app.SetBeginBlocker(manager.BeginBlock) - app.SetEndBlocker(manager.EndBlock) - - app.MountStores( - mainStoreKey, - paramsStoreKey, - paramsTStoreKey, - supplyStoreKey, - stakingStoreKey, - stakingTStoreKey, - distrStoreKey, - slashingStoreKey, - - ownershipStoreKey, - serviceStoreKey, - instanceStoreKey, - runnerStoreKey, - executionStoreKey, - processStoreKey, - ) - - app.SetAnteHandler( - cosmostypes.ChainAnteDecorators( - ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first - ante.NewMempoolFeeDecorator(), - ante.NewValidateBasicDecorator(), - ante.NewValidateMemoDecorator(accountKeeper), - ante.NewConsumeGasForTxSizeDecorator(accountKeeper), - ante.NewSetPubKeyDecorator(accountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators - ante.NewValidateSigCountDecorator(accountKeeper), - ante.NewDeductFeeDecorator(accountKeeper, supplyKeeper), - ante.NewSigGasConsumeDecorator(accountKeeper, auth.DefaultSigVerificationGasConsumer), - ante.NewSigVerificationDecorator(accountKeeper), - cosmos.NewForceCheckTxProxyDecorator(ante.NewIncrementSequenceDecorator(accountKeeper)), // innermost AnteDecorator - ), - // Previous implementation: - // auth.NewAnteHandler(accountKeeper, supplyKeeper, auth.DefaultSigVerificationGasConsumer) - ) - if err := app.LoadLatestVersion(mainStoreKey); err != nil { - return nil, err - } - - return app, nil -} From dd6d4cffaf1698efc493fbb77cf916781d46191c Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 6 Feb 2020 14:08:36 +0700 Subject: [PATCH 13/87] fix comment --- app/app.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/app.go b/app/app.go index 256c1f36d..3d3a21a5e 100644 --- a/app/app.go +++ b/app/app.go @@ -131,7 +131,7 @@ type NewApp struct { // verify app interface at compile time var _ simapp.App = (*NewApp)(nil) -// NewengineApp is a constructor function for engineApp +// NewInitApp is a constructor function for engineApp func NewInitApp( logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, invCheckPeriod uint, baseAppOptions ...func(*bam.BaseApp), @@ -144,7 +144,6 @@ func NewInitApp( bApp.SetCommitMultiStoreTracer(traceStore) bApp.SetAppVersion(version.Version) - // TODO: Add the keys that module requires keys := sdk.NewKVStoreKeys( bam.MainStoreKey, auth.StoreKey, From db5899a901dd07300d515b361d1d5b1f8326e590 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 6 Feb 2020 18:12:07 +0700 Subject: [PATCH 14/87] Add new decorator to force context.IsChectTx to false in NewIncrementSequenceDecorator to allow multiple tx from same account in one block --- app/app.go | 23 +++++++++++++++++++---- app/decorator.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 app/decorator.go diff --git a/app/app.go b/app/app.go index 3d3a21a5e..96d6d6ca1 100644 --- a/app/app.go +++ b/app/app.go @@ -17,6 +17,7 @@ import ( "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth/ante" "github.com/cosmos/cosmos-sdk/x/auth/vesting" "github.com/cosmos/cosmos-sdk/x/bank" distr "github.com/cosmos/cosmos-sdk/x/distribution" @@ -307,11 +308,25 @@ func NewInitApp( // The AnteHandler handles signature verification and transaction pre-processing app.SetAnteHandler( - auth.NewAnteHandler( - app.accountKeeper, - app.supplyKeeper, - auth.DefaultSigVerificationGasConsumer, + sdk.ChainAnteDecorators( + ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first + ante.NewMempoolFeeDecorator(), + ante.NewValidateBasicDecorator(), + ante.NewValidateMemoDecorator(app.accountKeeper), + ante.NewConsumeGasForTxSizeDecorator(app.accountKeeper), + ante.NewSetPubKeyDecorator(app.accountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators + ante.NewValidateSigCountDecorator(app.accountKeeper), + ante.NewDeductFeeDecorator(app.accountKeeper, app.supplyKeeper), + ante.NewSigGasConsumeDecorator(app.accountKeeper, auth.DefaultSigVerificationGasConsumer), + ante.NewSigVerificationDecorator(app.accountKeeper), + newForceCheckTxProxyDecorator(ante.NewIncrementSequenceDecorator(app.accountKeeper)), // innermost AnteDecorator ), + // Previous implementation: + // auth.NewAnteHandler( + // app.accountKeeper, + // app.supplyKeeper, + // auth.DefaultSigVerificationGasConsumer, + // ), ) // initialize stores diff --git a/app/decorator.go b/app/decorator.go new file mode 100644 index 000000000..f457df5c3 --- /dev/null +++ b/app/decorator.go @@ -0,0 +1,29 @@ +package app + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type forceCheckTxProxyDecorator struct { + d sdk.AnteDecorator +} + +func newForceCheckTxProxyDecorator(d sdk.AnteDecorator) forceCheckTxProxyDecorator { + return forceCheckTxProxyDecorator{ + d: d, + } +} + +func (d forceCheckTxProxyDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { + ctx = ctx.WithIsCheckTx(false).WithValue("mesgHackIsCheckTx", ctx.IsCheckTx()) + n := func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) { + checkTx, ok := ctx.Value("mesgHackIsCheckTx").(bool) + if !ok { + return ctx, fmt.Errorf("mesgHackIsCheckTx is not a bool") + } + return next(ctx.WithIsCheckTx(checkTx), tx, simulate) + } + return d.d.AnteHandle(ctx, tx, simulate, n) +} From 16ee943f65db2375af137217318e20cfcef43ca4 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 6 Feb 2020 18:19:08 +0700 Subject: [PATCH 15/87] fix lint and comment --- app/app.go | 2 +- app/export.go | 4 +--- cmd/mesg-daemon/genaccounts.go | 11 ++++------- cmd/mesg-daemon/main.go | 23 ++++++++++------------- 4 files changed, 16 insertions(+), 24 deletions(-) diff --git a/app/app.go b/app/app.go index 96d6d6ca1..b441bf99c 100644 --- a/app/app.go +++ b/app/app.go @@ -79,7 +79,7 @@ var ( // MakeCodec creates the application codec. The codec is sealed before it is // returned. func MakeCodec() *codec.Codec { - // TODO: let's use the cosmos template way + // TODO: to change to the cosmos template way // var cdc = codec.New() ModuleBasics.RegisterCodec(mesgcodec.Codec) diff --git a/app/export.go b/app/export.go index ad76bd0ef..3846f3c06 100644 --- a/app/export.go +++ b/app/export.go @@ -18,7 +18,6 @@ import ( func (app *NewApp) ExportAppStateAndValidators( forZeroHeight bool, jailWhiteList []string, ) (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) { - // as if they could withdraw from the start of the next block ctx := app.NewContext(true, abci.Header{Height: app.LastBlockHeight()}) @@ -38,7 +37,7 @@ func (app *NewApp) ExportAppStateAndValidators( // prepare for fresh start at zero height // NOTE zero height genesis is a temporary feature which will be deprecated -// in favour of export at a block height +// in favor of export at a block height func (app *NewApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []string) { applyWhiteList := false @@ -89,7 +88,6 @@ func (app *NewApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []str // reinitialize all validators app.stakingKeeper.IterateValidators(ctx, func(_ int64, val staking.ValidatorI) (stop bool) { - // donate any unwithdrawn outstanding reward fraction tokens to the community pool scraps := app.distrKeeper.GetValidatorOutstandingRewards(ctx, val.GetOperator()) feePool := app.distrKeeper.GetFeePool(ctx) diff --git a/cmd/mesg-daemon/genaccounts.go b/cmd/mesg-daemon/genaccounts.go index 90df73f4c..99b207eb0 100644 --- a/cmd/mesg-daemon/genaccounts.go +++ b/cmd/mesg-daemon/genaccounts.go @@ -5,20 +5,18 @@ import ( "errors" "fmt" - "github.com/spf13/cobra" - "github.com/spf13/viper" - - "github.com/tendermint/tendermint/libs/cli" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/crypto/keys" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/crypto/keys" "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting" "github.com/cosmos/cosmos-sdk/x/genutil" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/tendermint/tendermint/libs/cli" ) const ( @@ -32,7 +30,6 @@ const ( func AddGenesisAccountCmd( ctx *server.Context, cdc *codec.Codec, defaultNodeHome, defaultClientHome string, ) *cobra.Command { - cmd := &cobra.Command{ Use: "add-genesis-account [address_or_key_name] [coin][,[coin]]", Short: "Add a genesis account to genesis.json", diff --git a/cmd/mesg-daemon/main.go b/cmd/mesg-daemon/main.go index 82bb908af..601488026 100644 --- a/cmd/mesg-daemon/main.go +++ b/cmd/mesg-daemon/main.go @@ -4,19 +4,6 @@ import ( "encoding/json" "io" - "github.com/spf13/cobra" - "github.com/spf13/viper" - - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/libs/cli" - "github.com/tendermint/tendermint/libs/log" - tmtypes "github.com/tendermint/tendermint/types" - dbm "github.com/tendermint/tm-db" - - "github.com/mesg-foundation/engine/app" - "github.com/mesg-foundation/engine/config" - "github.com/mesg-foundation/engine/cosmos" - "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client/debug" "github.com/cosmos/cosmos-sdk/client/flags" @@ -26,6 +13,16 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/mesg-foundation/engine/app" + "github.com/mesg-foundation/engine/config" + "github.com/mesg-foundation/engine/cosmos" + "github.com/spf13/cobra" + "github.com/spf13/viper" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/libs/cli" + "github.com/tendermint/tendermint/libs/log" + tmtypes "github.com/tendermint/tendermint/types" + dbm "github.com/tendermint/tm-db" ) const flagInvCheckPeriod = "inv-check-period" From 6aaf56e8d0e9913b694963bfafc1eec69199abf0 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Fri, 7 Feb 2020 05:44:35 +0000 Subject: [PATCH 16/87] Bump github.com/prometheus/client_golang from 1.4.0 to 1.4.1 Bumps [github.com/prometheus/client_golang](https://github.com/prometheus/client_golang) from 1.4.0 to 1.4.1. - [Release notes](https://github.com/prometheus/client_golang/releases) - [Changelog](https://github.com/prometheus/client_golang/blob/master/CHANGELOG.md) - [Commits](https://github.com/prometheus/client_golang/compare/v1.4.0...v1.4.1) Signed-off-by: dependabot-preview[bot] --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index b9bf883d9..ae75b3d0d 100644 --- a/go.mod +++ b/go.mod @@ -43,7 +43,7 @@ require ( github.com/opencontainers/go-digest v1.0.0-rc1 // indirect github.com/opencontainers/image-spec v1.0.1 // indirect github.com/opencontainers/runc v0.1.1 // indirect - github.com/prometheus/client_golang v1.4.0 + github.com/prometheus/client_golang v1.4.1 github.com/pseudomuto/protoc-gen-doc v1.3.0 github.com/pseudomuto/protokit v0.2.0 // indirect github.com/rcrowley/go-metrics v0.0.0-20190706150252-9beb055b7962 // indirect diff --git a/go.sum b/go.sum index e96024f1c..11ff00f15 100644 --- a/go.sum +++ b/go.sum @@ -290,6 +290,8 @@ github.com/prometheus/client_golang v1.0.0 h1:vrDKnkGzuGvhNAL56c7DBz29ZL+KxnoR0x github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= github.com/prometheus/client_golang v1.4.0 h1:YVIb/fVcOTMSqtqZWSKnHpSLBxu8DKgxq8z6RuBZwqI= github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU= +github.com/prometheus/client_golang v1.4.1 h1:FFSuS004yOQEtDdTq+TAOLP5xUq63KqAFYyOi8zA+Y8= +github.com/prometheus/client_golang v1.4.1/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= From 30544adfae7e1776ad2cd6f04e6ebd9cba0554dd Mon Sep 17 00:00:00 2001 From: krhubert Date: Fri, 7 Feb 2020 16:26:59 +0100 Subject: [PATCH 17/87] Lint --- cmd/mesg-cli/main.go | 20 +++++++------------- cmd/mesg-daemon/main.go | 4 ++-- core/main.go | 5 ----- cosmos/decorator.go | 32 -------------------------------- 4 files changed, 9 insertions(+), 52 deletions(-) delete mode 100644 cosmos/decorator.go diff --git a/cmd/mesg-cli/main.go b/cmd/mesg-cli/main.go index e5f0e0369..84c888dee 100644 --- a/cmd/mesg-cli/main.go +++ b/cmd/mesg-cli/main.go @@ -16,16 +16,13 @@ import ( authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest" "github.com/cosmos/cosmos-sdk/x/bank" bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" - + "github.com/mesg-foundation/engine/app" + "github.com/mesg-foundation/engine/config" + "github.com/mesg-foundation/engine/cosmos" "github.com/spf13/cobra" "github.com/spf13/viper" - "github.com/tendermint/go-amino" "github.com/tendermint/tendermint/libs/cli" - - "github.com/mesg-foundation/engine/app" - "github.com/mesg-foundation/engine/config" - "github.com/mesg-foundation/engine/cosmos" ) func main() { @@ -36,6 +33,7 @@ func main() { cdc := app.MakeCodec() // Read in the configuration file for the sdk + // TODO: change back when more refactor is done // config := sdk.GetConfig() // config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) // config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) @@ -47,12 +45,8 @@ func main() { } cosmos.CustomizeConfig(cfg) - // TODO: setup keybase, viper object, etc. to be passed into - // the below functions and eliminate global vars, like we do - // with the cdc - rootCmd := &cobra.Command{ - Use: "acli", + Use: "mesg-cli", Short: "Command line interface for interacting with appd", } @@ -77,8 +71,8 @@ func main() { flags.NewCompletionCmd(rootCmd, true), ) - // Add flags and prefix all env exposed with AA - executor := cli.PrepareMainCmd(rootCmd, "AA", app.DefaultCLIHome) + // Add flags and prefix all env exposed with MESG + executor := cli.PrepareMainCmd(rootCmd, "MESG", app.DefaultCLIHome) err = executor.Execute() if err != nil { diff --git a/cmd/mesg-daemon/main.go b/cmd/mesg-daemon/main.go index 601488026..a90378768 100644 --- a/cmd/mesg-daemon/main.go +++ b/cmd/mesg-daemon/main.go @@ -32,6 +32,7 @@ var invCheckPeriod uint func main() { cdc := app.MakeCodec() + // TODO: put back when more refactor is done // config := sdk.GetConfig() // config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) // config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) @@ -46,7 +47,7 @@ func main() { ctx := server.NewDefaultContext() cobra.EnableCommandSorting = false rootCmd := &cobra.Command{ - Use: "aud", + Use: "mesg-daemon", Short: "app Daemon (server)", PersistentPreRunE: server.PersistentPreRunEFn(ctx), } @@ -97,7 +98,6 @@ func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer) abci.Application func exportAppStateAndTMValidators( logger log.Logger, db dbm.DB, traceStore io.Writer, height int64, forZeroHeight bool, jailWhiteList []string, ) (json.RawMessage, []tmtypes.GenesisValidator, error) { - if height != -1 { aApp := app.NewInitApp(logger, db, traceStore, false, uint(1)) err := aApp.LoadHeight(height) diff --git a/core/main.go b/core/main.go index 8c63aa66b..f1a459822 100644 --- a/core/main.go +++ b/core/main.go @@ -148,12 +148,7 @@ func main() { logrus.WithField("module", "main").Fatalln(err) } - // init app - // app, err := enginesdk.NewApp(tendermintLogger, db, cfg.Cosmos.MinGasPrices) initApp := app.NewInitApp(tendermintLogger, db, nil, true, 0, bam.SetMinGasPrices(cfg.Cosmos.MinGasPrices)) - // if err != nil { - // logrus.WithField("module", "main").Fatalln(err) - // } // init key manager kb, err := cosmos.NewKeybase(filepath.Join(cfg.Path, cfg.Cosmos.RelativePath)) diff --git a/cosmos/decorator.go b/cosmos/decorator.go deleted file mode 100644 index 75f8c78b5..000000000 --- a/cosmos/decorator.go +++ /dev/null @@ -1,32 +0,0 @@ -package cosmos - -import ( - "fmt" - - sdk "github.com/cosmos/cosmos-sdk/types" -) - -// ForceCheckTxProxyDecorator defines a proxy decorator that force the ctx.IsCheckTx to false. -type ForceCheckTxProxyDecorator struct { - d sdk.AnteDecorator -} - -// NewForceCheckTxProxyDecorator returns a proxy decorator that force the ctx.IsCheckTx to false. -func NewForceCheckTxProxyDecorator(d sdk.AnteDecorator) ForceCheckTxProxyDecorator { - return ForceCheckTxProxyDecorator{ - d: d, - } -} - -// AnteHandle creates a proxy on top of the passed AnteDecorator AnteHandle. -func (d ForceCheckTxProxyDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { - ctx = ctx.WithIsCheckTx(false).WithValue("mesgHackIsCheckTx", ctx.IsCheckTx()) - n := func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) { - checkTx, ok := ctx.Value("mesgHackIsCheckTx").(bool) - if !ok { - return ctx, fmt.Errorf("mesgHackIsCheckTx is not a bool") - } - return next(ctx.WithIsCheckTx(checkTx), tx, simulate) - } - return d.d.AnteHandle(ctx, tx, simulate, n) -} From b33989b2e38e103af776f1604b32c114fd282459 Mon Sep 17 00:00:00 2001 From: krhubert Date: Mon, 10 Feb 2020 18:57:35 +0100 Subject: [PATCH 18/87] Change x package to ext --- .golangci.yml | 2 +- core/main.go | 8 ++++---- cosmos/client.go | 4 ++-- e2e/testdata/test-complex-service/main.go | 2 +- {x => ext}/xerrors/errors.go | 0 {x => ext}/xerrors/errors_test.go | 0 {x => ext}/xnet/dnsclient.go | 0 {x => ext}/xnet/ip_test.go | 0 {x => ext}/xnet/ipsock.go | 0 {x => ext}/xos/env.go | 0 {x => ext}/xos/env_test.go | 0 {x => ext}/xrand/seed.go | 0 {x => ext}/xreflect/xreflect.go | 0 {x => ext}/xreflect/xreflect_test.go | 0 {x => ext}/xsignal/signal.go | 0 {x => ext}/xsignal/signal_test.go | 0 {x => ext}/xstrings/strings.go | 0 {x => ext}/xstrings/strings_test.go | 0 {x => ext}/xvalidator/validator.go | 2 +- protobuf/api/execution.go | 2 +- sdk/execution/msgs.go | 2 +- sdk/process/msgs.go | 2 +- sdk/runner/container.go | 4 ++-- sdk/runner/msgs.go | 2 +- sdk/runner/sdk.go | 2 +- sdk/service/msgs.go | 2 +- server/grpc/server.go | 2 +- service/service_parameter.go | 2 +- service/validator/validator.go | 4 ++-- x/.gitkeep | 0 30 files changed, 21 insertions(+), 21 deletions(-) rename {x => ext}/xerrors/errors.go (100%) rename {x => ext}/xerrors/errors_test.go (100%) rename {x => ext}/xnet/dnsclient.go (100%) rename {x => ext}/xnet/ip_test.go (100%) rename {x => ext}/xnet/ipsock.go (100%) rename {x => ext}/xos/env.go (100%) rename {x => ext}/xos/env_test.go (100%) rename {x => ext}/xrand/seed.go (100%) rename {x => ext}/xreflect/xreflect.go (100%) rename {x => ext}/xreflect/xreflect_test.go (100%) rename {x => ext}/xsignal/signal.go (100%) rename {x => ext}/xsignal/signal_test.go (100%) rename {x => ext}/xstrings/strings.go (100%) rename {x => ext}/xstrings/strings_test.go (100%) rename {x => ext}/xvalidator/validator.go (99%) delete mode 100644 x/.gitkeep diff --git a/.golangci.yml b/.golangci.yml index 127c9a1ba..8fad85dc2 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -20,7 +20,7 @@ issues: - "ineffective break statement. Did you mean to break out of the outer loop" exclude-rules: - - path: x/xvalidator/validator.go + - path: ext/xvalidator/validator.go linters: - golint text: "exported var Translator should have its own declaration" diff --git a/core/main.go b/core/main.go index f1a459822..afdcc7928 100644 --- a/core/main.go +++ b/core/main.go @@ -17,6 +17,10 @@ import ( "github.com/mesg-foundation/engine/config" "github.com/mesg-foundation/engine/container" "github.com/mesg-foundation/engine/cosmos" + "github.com/mesg-foundation/engine/ext/xerrors" + "github.com/mesg-foundation/engine/ext/xnet" + "github.com/mesg-foundation/engine/ext/xrand" + "github.com/mesg-foundation/engine/ext/xsignal" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/logger" "github.com/mesg-foundation/engine/orchestrator" @@ -25,10 +29,6 @@ import ( runnersdk "github.com/mesg-foundation/engine/sdk/runner" "github.com/mesg-foundation/engine/server/grpc" "github.com/mesg-foundation/engine/version" - "github.com/mesg-foundation/engine/x/xerrors" - "github.com/mesg-foundation/engine/x/xnet" - "github.com/mesg-foundation/engine/x/xrand" - "github.com/mesg-foundation/engine/x/xsignal" "github.com/sirupsen/logrus" rpcserver "github.com/tendermint/tendermint/rpc/lib/server" tmtypes "github.com/tendermint/tendermint/types" diff --git a/cosmos/client.go b/cosmos/client.go index 9e7015929..ab8a9bff9 100644 --- a/cosmos/client.go +++ b/cosmos/client.go @@ -16,8 +16,8 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/x/xreflect" - "github.com/mesg-foundation/engine/x/xstrings" + "github.com/mesg-foundation/engine/ext/xreflect" + "github.com/mesg-foundation/engine/ext/xstrings" abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/node" rpcclient "github.com/tendermint/tendermint/rpc/client" diff --git a/e2e/testdata/test-complex-service/main.go b/e2e/testdata/test-complex-service/main.go index 04c23e7e2..8b56e5c6e 100644 --- a/e2e/testdata/test-complex-service/main.go +++ b/e2e/testdata/test-complex-service/main.go @@ -9,9 +9,9 @@ import ( "os" "time" + "github.com/mesg-foundation/engine/ext/xsignal" "github.com/mesg-foundation/engine/hash" pb "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/x/xsignal" "google.golang.org/grpc" "google.golang.org/grpc/keepalive" ) diff --git a/x/xerrors/errors.go b/ext/xerrors/errors.go similarity index 100% rename from x/xerrors/errors.go rename to ext/xerrors/errors.go diff --git a/x/xerrors/errors_test.go b/ext/xerrors/errors_test.go similarity index 100% rename from x/xerrors/errors_test.go rename to ext/xerrors/errors_test.go diff --git a/x/xnet/dnsclient.go b/ext/xnet/dnsclient.go similarity index 100% rename from x/xnet/dnsclient.go rename to ext/xnet/dnsclient.go diff --git a/x/xnet/ip_test.go b/ext/xnet/ip_test.go similarity index 100% rename from x/xnet/ip_test.go rename to ext/xnet/ip_test.go diff --git a/x/xnet/ipsock.go b/ext/xnet/ipsock.go similarity index 100% rename from x/xnet/ipsock.go rename to ext/xnet/ipsock.go diff --git a/x/xos/env.go b/ext/xos/env.go similarity index 100% rename from x/xos/env.go rename to ext/xos/env.go diff --git a/x/xos/env_test.go b/ext/xos/env_test.go similarity index 100% rename from x/xos/env_test.go rename to ext/xos/env_test.go diff --git a/x/xrand/seed.go b/ext/xrand/seed.go similarity index 100% rename from x/xrand/seed.go rename to ext/xrand/seed.go diff --git a/x/xreflect/xreflect.go b/ext/xreflect/xreflect.go similarity index 100% rename from x/xreflect/xreflect.go rename to ext/xreflect/xreflect.go diff --git a/x/xreflect/xreflect_test.go b/ext/xreflect/xreflect_test.go similarity index 100% rename from x/xreflect/xreflect_test.go rename to ext/xreflect/xreflect_test.go diff --git a/x/xsignal/signal.go b/ext/xsignal/signal.go similarity index 100% rename from x/xsignal/signal.go rename to ext/xsignal/signal.go diff --git a/x/xsignal/signal_test.go b/ext/xsignal/signal_test.go similarity index 100% rename from x/xsignal/signal_test.go rename to ext/xsignal/signal_test.go diff --git a/x/xstrings/strings.go b/ext/xstrings/strings.go similarity index 100% rename from x/xstrings/strings.go rename to ext/xstrings/strings.go diff --git a/x/xstrings/strings_test.go b/ext/xstrings/strings_test.go similarity index 100% rename from x/xstrings/strings_test.go rename to ext/xstrings/strings_test.go diff --git a/x/xvalidator/validator.go b/ext/xvalidator/validator.go similarity index 99% rename from x/xvalidator/validator.go rename to ext/xvalidator/validator.go index 05e4a9ad3..2fd77f74b 100644 --- a/x/xvalidator/validator.go +++ b/ext/xvalidator/validator.go @@ -9,8 +9,8 @@ import ( cosmostypes "github.com/cosmos/cosmos-sdk/types" "github.com/go-playground/locales/en" ut "github.com/go-playground/universal-translator" + "github.com/mesg-foundation/engine/ext/xnet" "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/x/xnet" validator "gopkg.in/go-playground/validator.v9" en_translations "gopkg.in/go-playground/validator.v9/translations/en" ) diff --git a/protobuf/api/execution.go b/protobuf/api/execution.go index d4e321387..dc9efa840 100644 --- a/protobuf/api/execution.go +++ b/protobuf/api/execution.go @@ -4,7 +4,7 @@ import ( fmt "fmt" execution "github.com/mesg-foundation/engine/execution" - "github.com/mesg-foundation/engine/x/xstrings" + "github.com/mesg-foundation/engine/ext/xstrings" ) // Validate checks if given filter is valid and returns error. diff --git a/sdk/execution/msgs.go b/sdk/execution/msgs.go index 8a72432f1..df1f3bd33 100644 --- a/sdk/execution/msgs.go +++ b/sdk/execution/msgs.go @@ -5,8 +5,8 @@ import ( cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/cosmos" + "github.com/mesg-foundation/engine/ext/xvalidator" "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/x/xvalidator" ) // msgCreateExecution defines a state transition to create a execution. diff --git a/sdk/process/msgs.go b/sdk/process/msgs.go index 73c053236..7eb457a37 100644 --- a/sdk/process/msgs.go +++ b/sdk/process/msgs.go @@ -4,10 +4,10 @@ import ( cosmostypes "github.com/cosmos/cosmos-sdk/types" cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/mesg-foundation/engine/codec" + "github.com/mesg-foundation/engine/ext/xvalidator" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/process" "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/x/xvalidator" ) // msgCreateProcess defines a state transition to create a service. diff --git a/sdk/runner/container.go b/sdk/runner/container.go index 92fcaf1cd..2fa2a5f79 100644 --- a/sdk/runner/container.go +++ b/sdk/runner/container.go @@ -13,10 +13,10 @@ import ( "github.com/docker/docker/client" "github.com/docker/docker/pkg/archive" "github.com/mesg-foundation/engine/container" + "github.com/mesg-foundation/engine/ext/xerrors" + "github.com/mesg-foundation/engine/ext/xos" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/service" - "github.com/mesg-foundation/engine/x/xerrors" - "github.com/mesg-foundation/engine/x/xos" ) // Build the imge of the container diff --git a/sdk/runner/msgs.go b/sdk/runner/msgs.go index 9e9be3f43..4eca8f97c 100644 --- a/sdk/runner/msgs.go +++ b/sdk/runner/msgs.go @@ -5,8 +5,8 @@ import ( cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/cosmos" + "github.com/mesg-foundation/engine/ext/xvalidator" "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/x/xvalidator" ) // msgCreateRunner defines a state transition to create a runner. diff --git a/sdk/runner/sdk.go b/sdk/runner/sdk.go index 65c55034b..35dcd4e91 100644 --- a/sdk/runner/sdk.go +++ b/sdk/runner/sdk.go @@ -6,13 +6,13 @@ import ( "github.com/mesg-foundation/engine/container" "github.com/mesg-foundation/engine/cosmos" + "github.com/mesg-foundation/engine/ext/xos" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/instance" "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/runner" instancesdk "github.com/mesg-foundation/engine/sdk/instance" servicesdk "github.com/mesg-foundation/engine/sdk/service" - "github.com/mesg-foundation/engine/x/xos" ) // SDK is the runner sdk. diff --git a/sdk/service/msgs.go b/sdk/service/msgs.go index 2749b75d4..df8aa0227 100644 --- a/sdk/service/msgs.go +++ b/sdk/service/msgs.go @@ -5,7 +5,7 @@ import ( cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/x/xvalidator" + "github.com/mesg-foundation/engine/ext/xvalidator" ) // msgCreateService defines a state transition to create a service. diff --git a/server/grpc/server.go b/server/grpc/server.go index a9729ba29..7ca0fc9ba 100644 --- a/server/grpc/server.go +++ b/server/grpc/server.go @@ -9,10 +9,10 @@ import ( grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus" grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" "github.com/mesg-foundation/engine/config" + "github.com/mesg-foundation/engine/ext/xvalidator" protobuf_api "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/sdk" "github.com/mesg-foundation/engine/server/grpc/api" - "github.com/mesg-foundation/engine/x/xvalidator" "github.com/sirupsen/logrus" "google.golang.org/grpc" "google.golang.org/grpc/keepalive" diff --git a/service/service_parameter.go b/service/service_parameter.go index 07a09b2b9..db8c9cffa 100644 --- a/service/service_parameter.go +++ b/service/service_parameter.go @@ -3,8 +3,8 @@ package service import ( "fmt" + "github.com/mesg-foundation/engine/ext/xerrors" "github.com/mesg-foundation/engine/protobuf/types" - "github.com/mesg-foundation/engine/x/xerrors" ) // validateServiceParameters validates data to see if it matches with parameters schema. diff --git a/service/validator/validator.go b/service/validator/validator.go index f6b510540..c9ad122f6 100644 --- a/service/validator/validator.go +++ b/service/validator/validator.go @@ -4,9 +4,9 @@ import ( "fmt" "strings" + "github.com/mesg-foundation/engine/ext/xerrors" + "github.com/mesg-foundation/engine/ext/xvalidator" "github.com/mesg-foundation/engine/service" - "github.com/mesg-foundation/engine/x/xerrors" - "github.com/mesg-foundation/engine/x/xvalidator" validator "gopkg.in/go-playground/validator.v9" ) diff --git a/x/.gitkeep b/x/.gitkeep deleted file mode 100644 index e69de29bb..000000000 From 10a69efcc9f666494ddb0ff8a148b40bd2c6fa70 Mon Sep 17 00:00:00 2001 From: krhubert Date: Thu, 6 Feb 2020 19:47:32 +0100 Subject: [PATCH 19/87] Use template to move ownership cosmos module to x/ dir --- app/app.go | 15 +- core/main.go | 2 +- cosmos/client.go | 20 ++- sdk/ownership/keeper.go | 97 ------------ sdk/ownership/module.go | 37 ----- sdk/ownership/sdk.go | 28 ---- sdk/process/keeper.go | 10 +- sdk/sdk.go | 4 - sdk/service/keeper.go | 16 +- sdk/service/msgs.go | 2 +- server/grpc/api/ownership.go | 18 ++- server/grpc/server.go | 12 +- server/grpc/server_test.go | 2 +- x/ownership/abci.go | 13 ++ x/ownership/alias.go | 35 +++++ x/ownership/client/cli/query.go | 51 +++++++ x/ownership/client/cli/tx.go | 26 ++++ x/ownership/client/rest/query.go | 39 +++++ x/ownership/client/rest/rest.go | 13 ++ x/ownership/client/rest/tx.go | 9 ++ x/ownership/genesis.go | 18 +++ x/ownership/handler.go | 17 +++ x/ownership/internal/keeper/keeper.go | 104 +++++++++++++ x/ownership/internal/keeper/querier.go | 33 +++++ x/ownership/internal/types/codec.go | 18 +++ .../internal/types/expected_keepers.go | 14 ++ x/ownership/internal/types/genesis.go | 19 +++ x/ownership/internal/types/key.go | 15 ++ x/ownership/internal/types/params.go | 38 +++++ x/ownership/internal/types/querier.go | 6 + x/ownership/module.go | 139 ++++++++++++++++++ 31 files changed, 671 insertions(+), 199 deletions(-) delete mode 100644 sdk/ownership/keeper.go delete mode 100644 sdk/ownership/module.go delete mode 100644 sdk/ownership/sdk.go create mode 100644 x/ownership/abci.go create mode 100644 x/ownership/alias.go create mode 100644 x/ownership/client/cli/query.go create mode 100644 x/ownership/client/cli/tx.go create mode 100644 x/ownership/client/rest/query.go create mode 100644 x/ownership/client/rest/rest.go create mode 100644 x/ownership/client/rest/tx.go create mode 100644 x/ownership/genesis.go create mode 100644 x/ownership/handler.go create mode 100644 x/ownership/internal/keeper/keeper.go create mode 100644 x/ownership/internal/keeper/querier.go create mode 100644 x/ownership/internal/types/codec.go create mode 100644 x/ownership/internal/types/expected_keepers.go create mode 100644 x/ownership/internal/types/genesis.go create mode 100644 x/ownership/internal/types/key.go create mode 100644 x/ownership/internal/types/params.go create mode 100644 x/ownership/internal/types/querier.go create mode 100644 x/ownership/module.go diff --git a/app/app.go b/app/app.go index b441bf99c..0e4c916dc 100644 --- a/app/app.go +++ b/app/app.go @@ -30,10 +30,10 @@ import ( "github.com/mesg-foundation/engine/cosmos" executionsdk "github.com/mesg-foundation/engine/sdk/execution" instancesdk "github.com/mesg-foundation/engine/sdk/instance" - ownershipsdk "github.com/mesg-foundation/engine/sdk/ownership" processsdk "github.com/mesg-foundation/engine/sdk/process" runnersdk "github.com/mesg-foundation/engine/sdk/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" + "github.com/mesg-foundation/engine/x/ownership" ) const appName = "engine" @@ -59,7 +59,7 @@ var ( supply.AppModuleBasic{}, // Engine's AppModuleBasic - cosmos.NewAppModuleBasic(ownershipsdk.ModuleName), + cosmos.NewAppModuleBasic(ownership.ModuleName), cosmos.NewAppModuleBasic(servicesdk.ModuleName), cosmos.NewAppModuleBasic(instancesdk.ModuleName), cosmos.NewAppModuleBasic(runnersdk.ModuleName), @@ -84,6 +84,7 @@ func MakeCodec() *codec.Codec { ModuleBasics.RegisterCodec(mesgcodec.Codec) vesting.RegisterCodec(mesgcodec.Codec) + ownership.RegisterCodec(mesgcodec.Codec) // sdk.RegisterCodec(cdc) // codec.RegisterCrypto(cdc) @@ -115,7 +116,7 @@ type NewApp struct { paramsKeeper params.Keeper // Engine's keepers - ownershipKeeper *ownershipsdk.Keeper + ownershipKeeper ownership.Keeper serviceKeeper *servicesdk.Keeper instanceKeeper *instancesdk.Keeper runnerKeeper *runnersdk.Keeper @@ -155,7 +156,7 @@ func NewInitApp( params.StoreKey, // Engine's module keys - ownershipsdk.ModuleName, + ownership.ModuleName, servicesdk.ModuleName, instancesdk.ModuleName, runnersdk.ModuleName, @@ -242,7 +243,7 @@ func NewInitApp( ) // Engine's module keepers - app.ownershipKeeper = ownershipsdk.NewKeeper(keys[ownershipsdk.ModuleName]) + app.ownershipKeeper = ownership.NewKeeper(app.cdc, keys[ownership.ModuleName]) app.serviceKeeper = servicesdk.NewKeeper(keys[servicesdk.ModuleName], app.ownershipKeeper) app.instanceKeeper = instancesdk.NewKeeper(keys[instancesdk.ModuleName]) app.runnerKeeper = runnersdk.NewKeeper(keys[runnersdk.ModuleName], app.instanceKeeper) @@ -260,7 +261,7 @@ func NewInitApp( slashing.NewAppModule(app.slashingKeeper, app.accountKeeper, app.stakingKeeper), // Engine's modules - ownershipsdk.NewModule(app.ownershipKeeper), + ownership.NewAppModule(app.ownershipKeeper), servicesdk.NewModule(app.serviceKeeper), instancesdk.NewModule(app.instanceKeeper), runnersdk.NewModule(app.runnerKeeper), @@ -287,7 +288,7 @@ func NewInitApp( slashing.ModuleName, // Engine's modules - ownershipsdk.ModuleName, + ownership.ModuleName, servicesdk.ModuleName, instancesdk.ModuleName, runnersdk.ModuleName, diff --git a/core/main.go b/core/main.go index afdcc7928..3b6da53be 100644 --- a/core/main.go +++ b/core/main.go @@ -191,7 +191,7 @@ func main() { } // init gRPC server. - server := grpc.New(sdk, cfg) + server := grpc.New(sdk, cfg, client) logrus.WithField("module", "main").Infof("starting MESG Engine version %s", version.Version) diff --git a/cosmos/client.go b/cosmos/client.go index ab8a9bff9..38c67ca19 100644 --- a/cosmos/client.go +++ b/cosmos/client.go @@ -15,9 +15,9 @@ import ( authExported "github.com/cosmos/cosmos-sdk/x/auth/exported" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/ext/xreflect" "github.com/mesg-foundation/engine/ext/xstrings" + "github.com/mesg-foundation/engine/hash" abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/node" rpcclient "github.com/tendermint/tendermint/rpc/client" @@ -55,6 +55,24 @@ func NewClient(node *node.Node, kb keys.Keybase, chainID, accName, accPassword, }, nil } +// QueryJSON is abci.query wrapper with errors check and decode data. +func (c *Client) QueryJSON(path string, qdata, ptr interface{}) error { + var data []byte + if !xreflect.IsNil(qdata) { + b, err := codec.MarshalJSON(qdata) + if err != nil { + return err + } + data = b + } + + result, _, err := c.QueryWithData(path, data) + if err != nil { + return err + } + return codec.UnmarshalJSON(result, ptr) +} + // Query is abci.query wrapper with errors check and decode data. func (c *Client) Query(path string, qdata, ptr interface{}) error { var data []byte diff --git a/sdk/ownership/keeper.go b/sdk/ownership/keeper.go deleted file mode 100644 index 31cda1ad3..000000000 --- a/sdk/ownership/keeper.go +++ /dev/null @@ -1,97 +0,0 @@ -package ownershipsdk - -import ( - "fmt" - - cosmostypes "github.com/cosmos/cosmos-sdk/types" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/ownership" -) - -// Keeper holds the logic to read and write data. -type Keeper struct { - storeKey *cosmostypes.KVStoreKey -} - -// NewKeeper initialize a new keeper. -func NewKeeper(storeKey *cosmostypes.KVStoreKey) *Keeper { - return &Keeper{ - storeKey: storeKey, - } -} - -// Create creates a new ownership. -func (k *Keeper) Create(req cosmostypes.Request, owner cosmostypes.AccAddress, resourceHash hash.Hash, resource ownership.Ownership_Resource) (*ownership.Ownership, error) { - store := req.KVStore(k.storeKey) - hashes := findOwnerships(store, "", resourceHash) - if len(hashes) > 0 { - return nil, fmt.Errorf("resource %s:%q already has an owner", resource, resourceHash) - } - ownership := &ownership.Ownership{ - Owner: owner.String(), - Resource: resource, - ResourceHash: resourceHash, - } - ownership.Hash = hash.Dump(ownership) - - value, err := codec.MarshalBinaryBare(ownership) - if err != nil { - return nil, err - } - store.Set(ownership.Hash, value) - return ownership, nil -} - -// Delete deletes a ownership. -func (k *Keeper) Delete(req cosmostypes.Request, owner cosmostypes.AccAddress, resourceHash hash.Hash) error { - store := req.KVStore(k.storeKey) - hashes := findOwnerships(store, owner.String(), resourceHash) - if len(hashes) == 0 { - return fmt.Errorf("resource %q do not have any ownership", resourceHash) - } - - for _, hash := range hashes { - store.Delete(hash) - } - return nil -} - -// List returns all ownerships. -func (k *Keeper) List(req cosmostypes.Request) ([]*ownership.Ownership, error) { - var ( - ownerships []*ownership.Ownership - iter = req.KVStore(k.storeKey).Iterator(nil, nil) - ) - defer iter.Close() - - for iter.Valid() { - var o *ownership.Ownership - if err := codec.UnmarshalBinaryBare(iter.Value(), &o); err != nil { - return nil, err - } - ownerships = append(ownerships, o) - iter.Next() - } - return ownerships, nil -} - -// hasOwner checks if given resource has owner. Returns all ownership hash and true if has one -// nil and false otherwise. -func findOwnerships(store cosmostypes.KVStore, owner string, resourceHash hash.Hash) []hash.Hash { - var ownerships []hash.Hash - iter := store.Iterator(nil, nil) - - for iter.Valid() { - var o *ownership.Ownership - if err := codec.UnmarshalBinaryBare(iter.Value(), &o); err == nil { - if (owner == "" || o.Owner == owner) && o.ResourceHash.Equal(resourceHash) { - ownerships = append(ownerships, o.Hash) - } - } - iter.Next() - } - - iter.Close() - return ownerships -} diff --git a/sdk/ownership/module.go b/sdk/ownership/module.go deleted file mode 100644 index 5ed26b8e8..000000000 --- a/sdk/ownership/module.go +++ /dev/null @@ -1,37 +0,0 @@ -package ownershipsdk - -import ( - "fmt" - - cosmostypes "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/cosmos-sdk/types/module" - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/hash" - abci "github.com/tendermint/tendermint/abci/types" -) - -// ModuleName is the name of this module. -const ModuleName = "ownership" - -// NewModule returns the module of this sdk. -func NewModule(k *Keeper) module.AppModule { - return cosmos.NewAppModule(cosmos.NewAppModuleBasic(ModuleName), handler(k), querier(k)) -} - -func handler(k *Keeper) cosmos.Handler { - return func(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { - return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "Unrecognized ownership Msg type: %v", msg.Type()) - } -} - -func querier(k *Keeper) cosmos.Querier { - return func(request cosmostypes.Request, path []string, req abci.RequestQuery) (res interface{}, err error) { - switch path[0] { - case "list": - return k.List(request) - default: - return nil, fmt.Errorf("unknown ownership query endpoint %s", path[0]) - } - } -} diff --git a/sdk/ownership/sdk.go b/sdk/ownership/sdk.go deleted file mode 100644 index 9c7717d73..000000000 --- a/sdk/ownership/sdk.go +++ /dev/null @@ -1,28 +0,0 @@ -package ownershipsdk - -import ( - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/ownership" -) - -// SDK is the ownership sdk. -type SDK struct { - client *cosmos.Client -} - -// New returns the ownership sdk. -func New(client *cosmos.Client) *SDK { - sdk := &SDK{ - client: client, - } - return sdk -} - -// List returns all ownerships. -func (s *SDK) List() ([]*ownership.Ownership, error) { - var ownerships []*ownership.Ownership - if err := s.client.Query("custom/"+ModuleName+"/list", nil, &ownerships); err != nil { - return nil, err - } - return ownerships, nil -} diff --git a/sdk/process/keeper.go b/sdk/process/keeper.go index 9e8fab7d4..c4a51aa19 100644 --- a/sdk/process/keeper.go +++ b/sdk/process/keeper.go @@ -6,21 +6,21 @@ import ( cosmostypes "github.com/cosmos/cosmos-sdk/types" "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/ownership" + ownershippb "github.com/mesg-foundation/engine/ownership" "github.com/mesg-foundation/engine/process" instancesdk "github.com/mesg-foundation/engine/sdk/instance" - ownershipsdk "github.com/mesg-foundation/engine/sdk/ownership" + "github.com/mesg-foundation/engine/x/ownership" ) // Keeper is the service keeper. type Keeper struct { storeKey *cosmostypes.KVStoreKey - ownershipKeeper *ownershipsdk.Keeper + ownershipKeeper ownership.Keeper instanceKeeper *instancesdk.Keeper } // NewKeeper returns the keeper of the service sdk. -func NewKeeper(storeKey *cosmostypes.KVStoreKey, ownershipKeeper *ownershipsdk.Keeper, instanceKeeper *instancesdk.Keeper) *Keeper { +func NewKeeper(storeKey *cosmostypes.KVStoreKey, ownershipKeeper ownership.Keeper, instanceKeeper *instancesdk.Keeper) *Keeper { return &Keeper{ storeKey: storeKey, ownershipKeeper: ownershipKeeper, @@ -63,7 +63,7 @@ func (k *Keeper) Create(req cosmostypes.Request, msg *msgCreateProcess) (*proces return nil, err } - if _, err := k.ownershipKeeper.Create(req, msg.Owner, p.Hash, ownership.Ownership_Process); err != nil { + if _, err := k.ownershipKeeper.Set(req, msg.Owner, p.Hash, ownershippb.Ownership_Process); err != nil { return nil, err } diff --git a/sdk/sdk.go b/sdk/sdk.go index 3d6867230..c1f511d83 100644 --- a/sdk/sdk.go +++ b/sdk/sdk.go @@ -7,7 +7,6 @@ import ( eventsdk "github.com/mesg-foundation/engine/sdk/event" executionsdk "github.com/mesg-foundation/engine/sdk/execution" instancesdk "github.com/mesg-foundation/engine/sdk/instance" - ownershipsdk "github.com/mesg-foundation/engine/sdk/ownership" processesdk "github.com/mesg-foundation/engine/sdk/process" runnersdk "github.com/mesg-foundation/engine/sdk/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" @@ -20,7 +19,6 @@ type SDK struct { Execution *executionsdk.SDK Event *eventsdk.Event Process *processesdk.SDK - Ownership *ownershipsdk.SDK Runner *runnersdk.SDK } @@ -28,7 +26,6 @@ type SDK struct { func New(client *cosmos.Client, kb *cosmos.Keybase, container container.Container, engineName, port, ipfsEndpoint string) *SDK { ps := pubsub.New(0) serviceSDK := servicesdk.New(client) - ownershipSDK := ownershipsdk.New(client) instanceSDK := instancesdk.New(client) runnerSDK := runnersdk.New(client, serviceSDK, instanceSDK, container, engineName, port, ipfsEndpoint) processSDK := processesdk.New(client) @@ -40,7 +37,6 @@ func New(client *cosmos.Client, kb *cosmos.Keybase, container container.Containe Execution: executionSDK, Event: eventSDK, Process: processSDK, - Ownership: ownershipSDK, Runner: runnerSDK, } } diff --git a/sdk/service/keeper.go b/sdk/service/keeper.go index 7896dc888..90a356a07 100644 --- a/sdk/service/keeper.go +++ b/sdk/service/keeper.go @@ -6,24 +6,24 @@ import ( cosmostypes "github.com/cosmos/cosmos-sdk/types" "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/ownership" + ownershippb "github.com/mesg-foundation/engine/ownership" "github.com/mesg-foundation/engine/protobuf/api" - ownershipsdk "github.com/mesg-foundation/engine/sdk/ownership" "github.com/mesg-foundation/engine/service" "github.com/mesg-foundation/engine/service/validator" + "github.com/mesg-foundation/engine/x/ownership" ) // Keeper holds the logic to read and write data. type Keeper struct { - storeKey *cosmostypes.KVStoreKey - ownerships *ownershipsdk.Keeper + storeKey *cosmostypes.KVStoreKey + ownershipKeeper ownership.Keeper } // NewKeeper initialize a new keeper. -func NewKeeper(storeKey *cosmostypes.KVStoreKey, ownerships *ownershipsdk.Keeper) *Keeper { +func NewKeeper(storeKey *cosmostypes.KVStoreKey, ownershipKeeper ownership.Keeper) *Keeper { return &Keeper{ - storeKey: storeKey, - ownerships: ownerships, + storeKey: storeKey, + ownershipKeeper: ownershipKeeper, } } @@ -48,7 +48,7 @@ func (k *Keeper) Create(request cosmostypes.Request, msg *msgCreateService) (*se return nil, err } - if _, err := k.ownerships.Create(request, msg.Owner, srv.Hash, ownership.Ownership_Service); err != nil { + if _, err := k.ownershipKeeper.Set(request, msg.Owner, srv.Hash, ownershippb.Ownership_Service); err != nil { return nil, err } diff --git a/sdk/service/msgs.go b/sdk/service/msgs.go index df8aa0227..6f1c903f2 100644 --- a/sdk/service/msgs.go +++ b/sdk/service/msgs.go @@ -4,8 +4,8 @@ import ( cosmostypes "github.com/cosmos/cosmos-sdk/types" cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/ext/xvalidator" + "github.com/mesg-foundation/engine/protobuf/api" ) // msgCreateService defines a state transition to create a service. diff --git a/server/grpc/api/ownership.go b/server/grpc/api/ownership.go index 0e5024d84..529916307 100644 --- a/server/grpc/api/ownership.go +++ b/server/grpc/api/ownership.go @@ -3,26 +3,32 @@ package api import ( "context" + "github.com/mesg-foundation/engine/cosmos" + ownershippb "github.com/mesg-foundation/engine/ownership" protobuf_api "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/sdk" + "github.com/mesg-foundation/engine/x/ownership" ) // OwnershipServer is the type to aggregate all Ownership APIs. type OwnershipServer struct { - sdk *sdk.SDK + sdk *sdk.SDK + client *cosmos.Client } // NewOwnershipServer creates a new OwnershipServer. -func NewOwnershipServer(sdk *sdk.SDK) *OwnershipServer { - return &OwnershipServer{sdk: sdk} +func NewOwnershipServer(sdk *sdk.SDK, client *cosmos.Client) *OwnershipServer { + return &OwnershipServer{ + sdk: sdk, + client: client, + } } // List returns all ownerships. func (s *OwnershipServer) List(ctx context.Context, req *protobuf_api.ListOwnershipRequest) (*protobuf_api.ListOwnershipResponse, error) { - ownerships, err := s.sdk.Ownership.List() - if err != nil { + var ownerships []*ownershippb.Ownership + if err := s.client.QueryJSON("custom/"+ownership.ModuleName+"/"+ownership.QueryListOwnerships, nil, &ownerships); err != nil { return nil, err } - return &protobuf_api.ListOwnershipResponse{Ownerships: ownerships}, nil } diff --git a/server/grpc/server.go b/server/grpc/server.go index 7ca0fc9ba..847d79db2 100644 --- a/server/grpc/server.go +++ b/server/grpc/server.go @@ -9,6 +9,7 @@ import ( grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus" grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" "github.com/mesg-foundation/engine/config" + "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/ext/xvalidator" protobuf_api "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/sdk" @@ -24,11 +25,16 @@ type Server struct { instance *grpc.Server sdk *sdk.SDK cfg *config.Config + client *cosmos.Client } // New returns a new gRPC server. -func New(sdk *sdk.SDK, cfg *config.Config) *Server { - return &Server{sdk: sdk, cfg: cfg} +func New(sdk *sdk.SDK, cfg *config.Config, client *cosmos.Client) *Server { + return &Server{ + sdk: sdk, + cfg: cfg, + client: client, + } } // Serve listens for connections. @@ -80,7 +86,7 @@ func (s *Server) register() { protobuf_api.RegisterInstanceServer(s.instance, api.NewInstanceServer(s.sdk)) protobuf_api.RegisterServiceServer(s.instance, api.NewServiceServer(s.sdk)) protobuf_api.RegisterProcessServer(s.instance, api.NewProcessServer(s.sdk)) - protobuf_api.RegisterOwnershipServer(s.instance, api.NewOwnershipServer(s.sdk)) + protobuf_api.RegisterOwnershipServer(s.instance, api.NewOwnershipServer(s.sdk, s.client)) protobuf_api.RegisterRunnerServer(s.instance, api.NewRunnerServer(s.sdk)) reflection.Register(s.instance) diff --git a/server/grpc/server_test.go b/server/grpc/server_test.go index cee473192..207295145 100644 --- a/server/grpc/server_test.go +++ b/server/grpc/server_test.go @@ -8,7 +8,7 @@ import ( ) func TestServerServe(t *testing.T) { - s := New(nil, nil) + s := New(nil, nil, nil) go func() { time.Sleep(500 * time.Millisecond) s.Close() diff --git a/x/ownership/abci.go b/x/ownership/abci.go new file mode 100644 index 000000000..c7ec36a3d --- /dev/null +++ b/x/ownership/abci.go @@ -0,0 +1,13 @@ +package ownership + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// BeginBlocker check for infraction evidence or downtime of validators +// on every begin block +func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k Keeper) {} + +// EndBlocker called every block, process inflation, update validator set. +func EndBlocker(ctx sdk.Context, k Keeper) {} diff --git a/x/ownership/alias.go b/x/ownership/alias.go new file mode 100644 index 000000000..c4f00e3a2 --- /dev/null +++ b/x/ownership/alias.go @@ -0,0 +1,35 @@ +package ownership + +import ( + "github.com/mesg-foundation/engine/x/ownership/internal/keeper" + "github.com/mesg-foundation/engine/x/ownership/internal/types" +) + +// const aliases +const ( + ModuleName = types.ModuleName + RouterKey = types.RouterKey + StoreKey = types.StoreKey + DefaultParamspace = types.DefaultParamspace + QuerierRoute = types.QuerierRoute +) + +// functions and variable aliases +var ( + NewKeeper = keeper.NewKeeper + NewQuerier = keeper.NewQuerier + RegisterCodec = types.RegisterCodec + NewGenesisState = types.NewGenesisState + DefaultGenesisState = types.DefaultGenesisState + ValidateGenesis = types.ValidateGenesis + + ModuleCdc = types.ModuleCdc + QueryListOwnerships = types.QueryListOwnerships +) + +// module types +type ( + Keeper = keeper.Keeper + GenesisState = types.GenesisState + Params = types.Params +) diff --git a/x/ownership/client/cli/query.go b/x/ownership/client/cli/query.go new file mode 100644 index 000000000..8c5c04724 --- /dev/null +++ b/x/ownership/client/cli/query.go @@ -0,0 +1,51 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/mesg-foundation/engine/ownership" + "github.com/mesg-foundation/engine/x/ownership/internal/types" + "github.com/spf13/cobra" +) + +// GetQueryCmd returns the cli query commands for this module +func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { + // Group ownership queries under a subcommand + ownershipQueryCmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + ownershipQueryCmd.AddCommand( + flags.GetCommands()..., + ) + + return ownershipQueryCmd +} + +// GetCmdListOwnerships returns command listing ownerships +func GetCmdListOwnerships(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "list", + Short: "list", + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/"+types.QueryListOwnerships, queryRoute), nil) + if err != nil { + fmt.Printf("could not get scavenges\n%s\n", err.Error()) + return nil + } + + var out []*ownership.Ownership + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} diff --git a/x/ownership/client/cli/tx.go b/x/ownership/client/cli/tx.go new file mode 100644 index 000000000..67ad67c77 --- /dev/null +++ b/x/ownership/client/cli/tx.go @@ -0,0 +1,26 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/mesg-foundation/engine/x/ownership/internal/types" + "github.com/spf13/cobra" +) + +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + ownershipTxCmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + ownershipTxCmd.AddCommand(flags.PostCommands()...) + + return ownershipTxCmd +} diff --git a/x/ownership/client/rest/query.go b/x/ownership/client/rest/query.go new file mode 100644 index 000000000..62598ed89 --- /dev/null +++ b/x/ownership/client/rest/query.go @@ -0,0 +1,39 @@ +package rest + +import ( + "fmt" + "net/http" + + "github.com/gorilla/mux" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/types/rest" + "github.com/mesg-foundation/engine/x/ownership/internal/types" +) + +func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { + r.HandleFunc( + "/ownership/parameters", + queryParamsHandlerFn(cliCtx), + ).Methods("GET") +} + +func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/parameters", types.QuerierRoute) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} diff --git a/x/ownership/client/rest/rest.go b/x/ownership/client/rest/rest.go new file mode 100644 index 000000000..ca9a5c63d --- /dev/null +++ b/x/ownership/client/rest/rest.go @@ -0,0 +1,13 @@ +package rest + +import ( + "github.com/gorilla/mux" + + "github.com/cosmos/cosmos-sdk/client/context" +) + +// RegisterRoutes registers ownership-related REST handlers to a router +func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) { + registerQueryRoutes(cliCtx, r) + registerTxRoutes(cliCtx, r) +} diff --git a/x/ownership/client/rest/tx.go b/x/ownership/client/rest/tx.go new file mode 100644 index 000000000..ae173af7c --- /dev/null +++ b/x/ownership/client/rest/tx.go @@ -0,0 +1,9 @@ +package rest + +import ( + "github.com/gorilla/mux" + + "github.com/cosmos/cosmos-sdk/client/context" +) + +func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {} diff --git a/x/ownership/genesis.go b/x/ownership/genesis.go new file mode 100644 index 000000000..2fac591fe --- /dev/null +++ b/x/ownership/genesis.go @@ -0,0 +1,18 @@ +package ownership + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/mesg-foundation/engine/x/ownership/internal/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// InitGenesis initialize default parameters and the keeper's address to pubkey map. +func InitGenesis(ctx sdk.Context, k Keeper, data types.GenesisState) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} + +// ExportGenesis writes the current store values // to a genesis file, +// which can be imported again with InitGenesis. +func ExportGenesis(ctx sdk.Context, k Keeper) types.GenesisState { + return types.NewGenesisState() +} diff --git a/x/ownership/handler.go b/x/ownership/handler.go new file mode 100644 index 000000000..322eb7fc0 --- /dev/null +++ b/x/ownership/handler.go @@ -0,0 +1,17 @@ +package ownership + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/x/ownership/internal/types" +) + +// NewHandler creates an sdk.Handler for all the instance type messages +func NewHandler(k Keeper) sdk.Handler { + return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg) + return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg) + } +} diff --git a/x/ownership/internal/keeper/keeper.go b/x/ownership/internal/keeper/keeper.go new file mode 100644 index 000000000..969b02c4b --- /dev/null +++ b/x/ownership/internal/keeper/keeper.go @@ -0,0 +1,104 @@ +package keeper + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/ownership" + "github.com/mesg-foundation/engine/x/ownership/internal/types" + "github.com/tendermint/tendermint/libs/log" +) + +// Keeper of the ownership store +type Keeper struct { + storeKey sdk.StoreKey + cdc *codec.Codec +} + +// NewKeeper creates a ownership keeper +func NewKeeper(cdc *codec.Codec, key sdk.StoreKey) Keeper { + keeper := Keeper{ + storeKey: key, + cdc: cdc, + } + return keeper +} + +// Logger returns a module-specific logger. +func (k Keeper) Logger(ctx sdk.Context) log.Logger { + return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) +} + +// List returns all ownerships. +func (k *Keeper) List(ctx sdk.Context) ([]*ownership.Ownership, error) { + var ( + ownerships []*ownership.Ownership + iter = ctx.KVStore(k.storeKey).Iterator(nil, nil) + ) + defer iter.Close() + + for iter.Valid() { + var o *ownership.Ownership + if err := k.cdc.UnmarshalBinaryLengthPrefixed(iter.Value(), &o); err != nil { + return nil, err + } + ownerships = append(ownerships, o) + iter.Next() + } + return ownerships, nil +} + +// Set creates a new ownership. +func (k Keeper) Set(ctx sdk.Context, owner sdk.AccAddress, resourceHash hash.Hash, resource ownership.Ownership_Resource) (*ownership.Ownership, error) { + store := ctx.KVStore(k.storeKey) + hashes := k.findOwnerships(store, "", resourceHash) + if len(hashes) > 0 { + return nil, fmt.Errorf("resource %s:%q already has an owner", resource, resourceHash) + } + + ownership := &ownership.Ownership{ + Owner: owner.String(), + Resource: resource, + ResourceHash: resourceHash, + } + ownership.Hash = hash.Dump(ownership) + + store.Set(ownership.Hash, k.cdc.MustMarshalBinaryLengthPrefixed(ownership)) + return ownership, nil +} + +// Delete deletes an ownership. +func (k Keeper) Delete(ctx sdk.Context, owner sdk.AccAddress, resourceHash hash.Hash) error { + store := ctx.KVStore(k.storeKey) + hashes := k.findOwnerships(store, owner.String(), resourceHash) + if len(hashes) == 0 { + return fmt.Errorf("resource %q do not have any ownership", resourceHash) + } + + for _, hash := range hashes { + store.Delete(hash) + } + return nil +} + +// hasOwner checks if given resource has owner. Returns all ownership hash and true if has one +// nil and false otherwise. +func (k Keeper) findOwnerships(store sdk.KVStore, owner string, resourceHash hash.Hash) []hash.Hash { + var ownerships []hash.Hash + iter := store.Iterator(nil, nil) + + for iter.Valid() { + var o *ownership.Ownership + if err := k.cdc.UnmarshalBinaryBare(iter.Value(), &o); err == nil { + if (owner == "" || o.Owner == owner) && o.ResourceHash.Equal(resourceHash) { + ownerships = append(ownerships, o.Hash) + } + } + iter.Next() + } + + iter.Close() + return ownerships +} diff --git a/x/ownership/internal/keeper/querier.go b/x/ownership/internal/keeper/querier.go new file mode 100644 index 000000000..9dd8b3117 --- /dev/null +++ b/x/ownership/internal/keeper/querier.go @@ -0,0 +1,33 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/x/ownership/internal/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// NewQuerier creates a new querier for ownership clients. +func NewQuerier(k Keeper) sdk.Querier { + return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) { + switch path[0] { + case types.QueryListOwnerships: + return listOwnerships(ctx, k) + default: + return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown ownership query endpoint") + } + } +} + +func listOwnerships(ctx sdk.Context, k Keeper) ([]byte, error) { + ownerships, err := k.List(ctx) + if err != nil { + return nil, err + } + + res, err := types.ModuleCdc.MarshalJSON(ownerships) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return res, nil +} diff --git a/x/ownership/internal/types/codec.go b/x/ownership/internal/types/codec.go new file mode 100644 index 000000000..b243782e1 --- /dev/null +++ b/x/ownership/internal/types/codec.go @@ -0,0 +1,18 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" +) + +// RegisterCodec registers concrete types on codec. +func RegisterCodec(cdc *codec.Codec) {} + +// ModuleCdc defines the module codec. +var ModuleCdc *codec.Codec + +func init() { + ModuleCdc = codec.New() + RegisterCodec(ModuleCdc) + codec.RegisterCrypto(ModuleCdc) + ModuleCdc.Seal() +} diff --git a/x/ownership/internal/types/expected_keepers.go b/x/ownership/internal/types/expected_keepers.go new file mode 100644 index 000000000..23128499d --- /dev/null +++ b/x/ownership/internal/types/expected_keepers.go @@ -0,0 +1,14 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/params" +) + +// ParamSubspace defines the expected Subspace interfacace +type ParamSubspace interface { + WithKeyTable(table params.KeyTable) params.Subspace + Get(ctx sdk.Context, key []byte, ptr interface{}) + GetParamSet(ctx sdk.Context, ps params.ParamSet) + SetParamSet(ctx sdk.Context, ps params.ParamSet) +} diff --git a/x/ownership/internal/types/genesis.go b/x/ownership/internal/types/genesis.go new file mode 100644 index 000000000..82fa33c65 --- /dev/null +++ b/x/ownership/internal/types/genesis.go @@ -0,0 +1,19 @@ +package types + +// GenesisState - all ownership state that must be provided at genesis +type GenesisState struct{} + +// NewGenesisState creates a new GenesisState object +func NewGenesisState() GenesisState { + return GenesisState{} +} + +// DefaultGenesisState - default GenesisState used by Cosmos Hub +func DefaultGenesisState() GenesisState { + return GenesisState{} +} + +// ValidateGenesis validates the ownership genesis parameters +func ValidateGenesis(data GenesisState) error { + return nil +} diff --git a/x/ownership/internal/types/key.go b/x/ownership/internal/types/key.go new file mode 100644 index 000000000..9bd525545 --- /dev/null +++ b/x/ownership/internal/types/key.go @@ -0,0 +1,15 @@ +package types + +const ( + // ModuleName is the name of the module + ModuleName = "ownership" + + // StoreKey to be used when creating the KVStore + StoreKey = ModuleName + + // RouterKey to be used for routing msgs + RouterKey = ModuleName + + // QuerierRoute to be used for routing + QuerierRoute = ModuleName +) diff --git a/x/ownership/internal/types/params.go b/x/ownership/internal/types/params.go new file mode 100644 index 000000000..4940992b5 --- /dev/null +++ b/x/ownership/internal/types/params.go @@ -0,0 +1,38 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/x/params" +) + +// Default parameter namespace +const ( + DefaultParamspace = ModuleName +) + +// ParamKeyTable for instance module +func ParamKeyTable() params.KeyTable { + return params.NewKeyTable().RegisterParamSet(&Params{}) +} + +// Params - used for initializing default parameter for instance at genesis +type Params struct{} + +// NewParams creates a new Params object +func NewParams() Params { + return Params{} +} + +// String implements the stringer interface for Params +func (p Params) String() string { + return "" +} + +// ParamSetPairs - Implements params.ParamSet +func (p *Params) ParamSetPairs() params.ParamSetPairs { + return params.ParamSetPairs{} +} + +// DefaultParams defines the parameters for this module +func DefaultParams() Params { + return NewParams() +} diff --git a/x/ownership/internal/types/querier.go b/x/ownership/internal/types/querier.go new file mode 100644 index 000000000..96ab75048 --- /dev/null +++ b/x/ownership/internal/types/querier.go @@ -0,0 +1,6 @@ +package types + +// Query endpoints supported by the ownership querier +const ( + QueryListOwnerships = "list" +) diff --git a/x/ownership/module.go b/x/ownership/module.go new file mode 100644 index 000000000..2d1be1575 --- /dev/null +++ b/x/ownership/module.go @@ -0,0 +1,139 @@ +package ownership + +import ( + "encoding/json" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/gorilla/mux" + "github.com/mesg-foundation/engine/x/ownership/client/cli" + "github.com/mesg-foundation/engine/x/ownership/client/rest" + "github.com/mesg-foundation/engine/x/ownership/internal/types" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" +) + +var ( + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} +) + +// AppModuleBasic defines the basic application module used by the ownership module. +type AppModuleBasic struct{} + +var _ module.AppModuleBasic = AppModuleBasic{} + +// Name returns the ownership module's name. +func (AppModuleBasic) Name() string { + return types.ModuleName +} + +// RegisterCodec registers the ownership module's types for the given codec. +func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { + RegisterCodec(cdc) +} + +// DefaultGenesis returns default genesis state as raw bytes for the ownership +// module. +func (AppModuleBasic) DefaultGenesis() json.RawMessage { + return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) +} + +// ValidateGenesis performs genesis state validation for the ownership module. +func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { + var data GenesisState + err := ModuleCdc.UnmarshalJSON(bz, &data) + if err != nil { + return err + } + return ValidateGenesis(data) +} + +// RegisterRESTRoutes registers the REST routes for the ownership module. +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { + rest.RegisterRoutes(ctx, rtr) +} + +// GetTxCmd returns the root tx command for the ownership module. +func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { + return cli.GetTxCmd(cdc) +} + +// GetQueryCmd returns no root query command for the ownership module. +func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { + return cli.GetQueryCmd(StoreKey, cdc) +} + +//____________________________________________________________________________ + +// AppModule implements an application module for the ownership module. +type AppModule struct { + AppModuleBasic + + keeper Keeper +} + +// NewAppModule creates a new AppModule object +func NewAppModule(k Keeper) AppModule { + return AppModule{ + AppModuleBasic: AppModuleBasic{}, + keeper: k, + } +} + +// Name returns the ownership module's name. +func (AppModule) Name() string { + return ModuleName +} + +// RegisterInvariants registers the ownership module invariants. +func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} + +// Route returns the message routing key for the ownership module. +func (AppModule) Route() string { + return RouterKey +} + +// NewHandler returns an sdk.Handler for the ownership module. +func (am AppModule) NewHandler() sdk.Handler { + return NewHandler(am.keeper) +} + +// QuerierRoute returns the ownership module's querier route name. +func (AppModule) QuerierRoute() string { + return QuerierRoute +} + +// NewQuerierHandler returns the ownership module sdk.Querier. +func (am AppModule) NewQuerierHandler() sdk.Querier { + return NewQuerier(am.keeper) +} + +// InitGenesis performs genesis initialization for the ownership module. It returns +// no validator updates. +func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { + var genesisState GenesisState + ModuleCdc.MustUnmarshalJSON(data, &genesisState) + InitGenesis(ctx, am.keeper, genesisState) + return []abci.ValidatorUpdate{} +} + +// ExportGenesis returns the exported genesis state as raw bytes for the ownership +// module. +func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { + gs := ExportGenesis(ctx, am.keeper) + return ModuleCdc.MustMarshalJSON(gs) +} + +// BeginBlock returns the begin blocker for the ownership module. +func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { + BeginBlocker(ctx, req, am.keeper) +} + +// EndBlock returns the end blocker for the ownership module. It returns no validator +// updates. +func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} From e980d858c34cefde09ef7802674b358fcd28b529 Mon Sep 17 00:00:00 2001 From: krhubert Date: Tue, 11 Feb 2020 11:08:45 +0100 Subject: [PATCH 20/87] Fix error + lint issues --- app/app.go | 5 ++--- server/grpc/api/ownership.go | 2 +- x/ownership/client/cli/query.go | 4 ++-- x/ownership/internal/keeper/keeper.go | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/app/app.go b/app/app.go index 0e4c916dc..1c74b7fcc 100644 --- a/app/app.go +++ b/app/app.go @@ -59,7 +59,7 @@ var ( supply.AppModuleBasic{}, // Engine's AppModuleBasic - cosmos.NewAppModuleBasic(ownership.ModuleName), + ownership.AppModuleBasic{}, cosmos.NewAppModuleBasic(servicesdk.ModuleName), cosmos.NewAppModuleBasic(instancesdk.ModuleName), cosmos.NewAppModuleBasic(runnersdk.ModuleName), @@ -84,7 +84,6 @@ func MakeCodec() *codec.Codec { ModuleBasics.RegisterCodec(mesgcodec.Codec) vesting.RegisterCodec(mesgcodec.Codec) - ownership.RegisterCodec(mesgcodec.Codec) // sdk.RegisterCodec(cdc) // codec.RegisterCrypto(cdc) @@ -243,7 +242,7 @@ func NewInitApp( ) // Engine's module keepers - app.ownershipKeeper = ownership.NewKeeper(app.cdc, keys[ownership.ModuleName]) + app.ownershipKeeper = ownership.NewKeeper(app.cdc, keys[ownership.StoreKey]) app.serviceKeeper = servicesdk.NewKeeper(keys[servicesdk.ModuleName], app.ownershipKeeper) app.instanceKeeper = instancesdk.NewKeeper(keys[instancesdk.ModuleName]) app.runnerKeeper = runnersdk.NewKeeper(keys[runnersdk.ModuleName], app.instanceKeeper) diff --git a/server/grpc/api/ownership.go b/server/grpc/api/ownership.go index 529916307..be338c5d6 100644 --- a/server/grpc/api/ownership.go +++ b/server/grpc/api/ownership.go @@ -27,7 +27,7 @@ func NewOwnershipServer(sdk *sdk.SDK, client *cosmos.Client) *OwnershipServer { // List returns all ownerships. func (s *OwnershipServer) List(ctx context.Context, req *protobuf_api.ListOwnershipRequest) (*protobuf_api.ListOwnershipResponse, error) { var ownerships []*ownershippb.Ownership - if err := s.client.QueryJSON("custom/"+ownership.ModuleName+"/"+ownership.QueryListOwnerships, nil, &ownerships); err != nil { + if err := s.client.QueryJSON("custom/"+ownership.QuerierRoute+"/"+ownership.QueryListOwnerships, nil, &ownerships); err != nil { return nil, err } return &protobuf_api.ListOwnershipResponse{Ownerships: ownerships}, nil diff --git a/x/ownership/client/cli/query.go b/x/ownership/client/cli/query.go index 8c5c04724..5ec7d713b 100644 --- a/x/ownership/client/cli/query.go +++ b/x/ownership/client/cli/query.go @@ -37,9 +37,9 @@ func GetCmdListOwnerships(queryRoute string, cdc *codec.Codec) *cobra.Command { Short: "list", RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) - res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/"+types.QueryListOwnerships, queryRoute), nil) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryListOwnerships), nil) if err != nil { - fmt.Printf("could not get scavenges\n%s\n", err.Error()) + fmt.Printf("could not get ownerships\n%s\n", err.Error()) return nil } diff --git a/x/ownership/internal/keeper/keeper.go b/x/ownership/internal/keeper/keeper.go index 969b02c4b..7a6a3a05e 100644 --- a/x/ownership/internal/keeper/keeper.go +++ b/x/ownership/internal/keeper/keeper.go @@ -91,7 +91,7 @@ func (k Keeper) findOwnerships(store sdk.KVStore, owner string, resourceHash has for iter.Valid() { var o *ownership.Ownership - if err := k.cdc.UnmarshalBinaryBare(iter.Value(), &o); err == nil { + if err := k.cdc.UnmarshalBinaryLengthPrefixed(iter.Value(), &o); err == nil { if (owner == "" || o.Owner == owner) && o.ResourceHash.Equal(resourceHash) { ownerships = append(ownerships, o.Hash) } From adea005bb7fdf97edf6bd96f82665bcc239b9ef3 Mon Sep 17 00:00:00 2001 From: krhubert Date: Tue, 11 Feb 2020 11:31:39 +0100 Subject: [PATCH 21/87] Implement list rest query for ownership --- x/ownership/client/rest/query.go | 29 ++++++++++++++++++++++++++--- x/ownership/client/rest/rest.go | 3 +-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/x/ownership/client/rest/query.go b/x/ownership/client/rest/query.go index 62598ed89..38c349524 100644 --- a/x/ownership/client/rest/query.go +++ b/x/ownership/client/rest/query.go @@ -4,18 +4,41 @@ import ( "fmt" "net/http" - "github.com/gorilla/mux" - "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/types/rest" + "github.com/gorilla/mux" "github.com/mesg-foundation/engine/x/ownership/internal/types" ) func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { + r.HandleFunc( + "/ownership/list", + queryListHandlerFn(cliCtx), + ).Methods(http.MethodGet) r.HandleFunc( "/ownership/parameters", queryParamsHandlerFn(cliCtx), - ).Methods("GET") + ).Methods(http.MethodGet) +} + +func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/list", types.QuerierRoute) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } } func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { diff --git a/x/ownership/client/rest/rest.go b/x/ownership/client/rest/rest.go index ca9a5c63d..19c6ebc84 100644 --- a/x/ownership/client/rest/rest.go +++ b/x/ownership/client/rest/rest.go @@ -1,9 +1,8 @@ package rest import ( - "github.com/gorilla/mux" - "github.com/cosmos/cosmos-sdk/client/context" + "github.com/gorilla/mux" ) // RegisterRoutes registers ownership-related REST handlers to a router From c506d4517718b639f6a06639ce12d0cde0347a16 Mon Sep 17 00:00:00 2001 From: krhubert Date: Tue, 11 Feb 2020 12:04:19 +0100 Subject: [PATCH 22/87] Fix module ownership client --- x/ownership/client/cli/query.go | 4 +++- x/ownership/client/rest/query.go | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/x/ownership/client/cli/query.go b/x/ownership/client/cli/query.go index 5ec7d713b..2172df13e 100644 --- a/x/ownership/client/cli/query.go +++ b/x/ownership/client/cli/query.go @@ -24,7 +24,9 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { } ownershipQueryCmd.AddCommand( - flags.GetCommands()..., + flags.GetCommands( + GetCmdListOwnerships(queryRoute, cdc), + )..., ) return ownershipQueryCmd diff --git a/x/ownership/client/rest/query.go b/x/ownership/client/rest/query.go index 38c349524..5c2165d05 100644 --- a/x/ownership/client/rest/query.go +++ b/x/ownership/client/rest/query.go @@ -28,7 +28,7 @@ func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return } - route := fmt.Sprintf("custom/%s/list", types.QuerierRoute) + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryListOwnerships) res, height, err := cliCtx.QueryWithData(route, nil) if err != nil { From 1fa7dc0f5b8d4b894fcbc08d8d52a814ffe6be4b Mon Sep 17 00:00:00 2001 From: krhubert Date: Tue, 11 Feb 2020 11:31:39 +0100 Subject: [PATCH 23/87] Implement list rest query for ownership --- x/ownership/client/rest/query.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/ownership/client/rest/query.go b/x/ownership/client/rest/query.go index 5c2165d05..351acd8b7 100644 --- a/x/ownership/client/rest/query.go +++ b/x/ownership/client/rest/query.go @@ -29,7 +29,6 @@ func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryListOwnerships) - res, height, err := cliCtx.QueryWithData(route, nil) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) From e42ed83800fa91735fe5de405a0fe20472d72b8f Mon Sep 17 00:00:00 2001 From: krhubert Date: Tue, 11 Feb 2020 18:26:34 +0100 Subject: [PATCH 24/87] Use template to move instance cosmos module to x/ dir --- app/app.go | 14 +- sdk/event/event.go | 26 ++-- sdk/execution/keeper.go | 6 +- sdk/execution/sdk.go | 15 +- sdk/instance/keeper.go | 80 ---------- sdk/instance/module.go | 56 ------- sdk/instance/sdk.go | 48 ------ sdk/process/keeper.go | 6 +- sdk/runner/keeper.go | 6 +- sdk/runner/sdk.go | 16 +- sdk/sdk.go | 10 +- server/grpc/api/instance.go | 30 +++- server/grpc/server.go | 2 +- x/instance/abci.go | 13 ++ x/instance/alias.go | 36 +++++ x/instance/client/cli/query.go | 72 +++++++++ x/instance/client/cli/tx.go | 25 ++++ x/instance/client/rest/query.go | 46 ++++++ x/instance/client/rest/rest.go | 12 ++ x/instance/client/rest/tx.go | 8 + x/instance/genesis.go | 18 +++ x/instance/handler.go | 17 +++ x/instance/internal/keeper/keeper.go | 88 +++++++++++ x/instance/internal/keeper/querier.go | 62 ++++++++ x/instance/internal/types/codec.go | 18 +++ x/instance/internal/types/expected_keepers.go | 14 ++ x/instance/internal/types/genesis.go | 19 +++ x/instance/internal/types/key.go | 15 ++ x/instance/internal/types/params.go | 38 +++++ x/instance/internal/types/querier.go | 8 + x/instance/module.go | 139 ++++++++++++++++++ 31 files changed, 718 insertions(+), 245 deletions(-) delete mode 100644 sdk/instance/keeper.go delete mode 100644 sdk/instance/module.go delete mode 100644 sdk/instance/sdk.go create mode 100644 x/instance/abci.go create mode 100644 x/instance/alias.go create mode 100644 x/instance/client/cli/query.go create mode 100644 x/instance/client/cli/tx.go create mode 100644 x/instance/client/rest/query.go create mode 100644 x/instance/client/rest/rest.go create mode 100644 x/instance/client/rest/tx.go create mode 100644 x/instance/genesis.go create mode 100644 x/instance/handler.go create mode 100644 x/instance/internal/keeper/keeper.go create mode 100644 x/instance/internal/keeper/querier.go create mode 100644 x/instance/internal/types/codec.go create mode 100644 x/instance/internal/types/expected_keepers.go create mode 100644 x/instance/internal/types/genesis.go create mode 100644 x/instance/internal/types/key.go create mode 100644 x/instance/internal/types/params.go create mode 100644 x/instance/internal/types/querier.go create mode 100644 x/instance/module.go diff --git a/app/app.go b/app/app.go index 1c74b7fcc..81e739819 100644 --- a/app/app.go +++ b/app/app.go @@ -29,10 +29,10 @@ import ( mesgcodec "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/cosmos" executionsdk "github.com/mesg-foundation/engine/sdk/execution" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" processsdk "github.com/mesg-foundation/engine/sdk/process" runnersdk "github.com/mesg-foundation/engine/sdk/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" + "github.com/mesg-foundation/engine/x/instance" "github.com/mesg-foundation/engine/x/ownership" ) @@ -60,8 +60,8 @@ var ( // Engine's AppModuleBasic ownership.AppModuleBasic{}, + instance.AppModuleBasic{}, cosmos.NewAppModuleBasic(servicesdk.ModuleName), - cosmos.NewAppModuleBasic(instancesdk.ModuleName), cosmos.NewAppModuleBasic(runnersdk.ModuleName), cosmos.NewAppModuleBasic(executionsdk.ModuleName), cosmos.NewAppModuleBasic(processsdk.ModuleName), @@ -116,8 +116,8 @@ type NewApp struct { // Engine's keepers ownershipKeeper ownership.Keeper + instanceKeeper instance.Keeper serviceKeeper *servicesdk.Keeper - instanceKeeper *instancesdk.Keeper runnerKeeper *runnersdk.Keeper processKeeper *processsdk.Keeper executionKeeper *executionsdk.Keeper @@ -156,8 +156,8 @@ func NewInitApp( // Engine's module keys ownership.ModuleName, + instance.ModuleName, servicesdk.ModuleName, - instancesdk.ModuleName, runnersdk.ModuleName, executionsdk.ModuleName, processsdk.ModuleName, @@ -243,8 +243,8 @@ func NewInitApp( // Engine's module keepers app.ownershipKeeper = ownership.NewKeeper(app.cdc, keys[ownership.StoreKey]) + app.instanceKeeper = instance.NewKeeper(app.cdc, keys[instance.StoreKey]) app.serviceKeeper = servicesdk.NewKeeper(keys[servicesdk.ModuleName], app.ownershipKeeper) - app.instanceKeeper = instancesdk.NewKeeper(keys[instancesdk.ModuleName]) app.runnerKeeper = runnersdk.NewKeeper(keys[runnersdk.ModuleName], app.instanceKeeper) app.processKeeper = processsdk.NewKeeper(keys[processsdk.ModuleName], app.ownershipKeeper, app.instanceKeeper) app.executionKeeper = executionsdk.NewKeeper(keys[executionsdk.ModuleName], app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper) @@ -261,8 +261,8 @@ func NewInitApp( // Engine's modules ownership.NewAppModule(app.ownershipKeeper), + instance.NewAppModule(app.instanceKeeper), servicesdk.NewModule(app.serviceKeeper), - instancesdk.NewModule(app.instanceKeeper), runnersdk.NewModule(app.runnerKeeper), executionsdk.NewModule(app.executionKeeper), processsdk.NewModule(app.processKeeper), @@ -288,8 +288,8 @@ func NewInitApp( // Engine's modules ownership.ModuleName, + instance.ModuleName, servicesdk.ModuleName, - instancesdk.ModuleName, runnersdk.ModuleName, executionsdk.ModuleName, processsdk.ModuleName, diff --git a/sdk/event/event.go b/sdk/event/event.go index 87319697a..17862fad3 100644 --- a/sdk/event/event.go +++ b/sdk/event/event.go @@ -1,12 +1,16 @@ package eventsdk import ( + "fmt" + "github.com/cskr/pubsub" + "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/event" "github.com/mesg-foundation/engine/hash" + instancepb "github.com/mesg-foundation/engine/instance" "github.com/mesg-foundation/engine/protobuf/types" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" servicesdk "github.com/mesg-foundation/engine/sdk/service" + "github.com/mesg-foundation/engine/x/instance" ) const ( @@ -16,17 +20,17 @@ const ( // Event exposes event APIs of MESG. type Event struct { - ps *pubsub.PubSub - instance *instancesdk.SDK - service *servicesdk.SDK + ps *pubsub.PubSub + client *cosmos.Client + service *servicesdk.SDK } // New creates a new Event SDK with given options. -func New(ps *pubsub.PubSub, service *servicesdk.SDK, instance *instancesdk.SDK) *Event { +func New(ps *pubsub.PubSub, service *servicesdk.SDK, client *cosmos.Client) *Event { return &Event{ - ps: ps, - service: service, - instance: instance, + ps: ps, + client: client, + service: service, } } @@ -34,12 +38,12 @@ func New(ps *pubsub.PubSub, service *servicesdk.SDK, instance *instancesdk.SDK) func (e *Event) Create(instanceHash hash.Hash, eventKey string, eventData *types.Struct) (*event.Event, error) { event := event.Create(instanceHash, eventKey, eventData) - instance, err := e.instance.Get(event.InstanceHash) - if err != nil { + var inst instancepb.Instance + if err := e.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", instance.QuerierRoute, instance.QueryGetInstance, event.InstanceHash), nil, &inst); err != nil { return nil, err } - service, err := e.service.Get(instance.ServiceHash) + service, err := e.service.Get(inst.ServiceHash) if err != nil { return nil, err } diff --git a/sdk/execution/keeper.go b/sdk/execution/keeper.go index f27b9f986..feba6d8ee 100644 --- a/sdk/execution/keeper.go +++ b/sdk/execution/keeper.go @@ -10,23 +10,23 @@ import ( "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/protobuf/types" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" processsdk "github.com/mesg-foundation/engine/sdk/process" runnersdk "github.com/mesg-foundation/engine/sdk/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" + "github.com/mesg-foundation/engine/x/instance" ) // Keeper holds the logic to read and write data. type Keeper struct { storeKey *cosmostypes.KVStoreKey serviceKeeper *servicesdk.Keeper - instanceKeeper *instancesdk.Keeper + instanceKeeper instance.Keeper runnerKeeper *runnersdk.Keeper processKeeper *processsdk.Keeper } // NewKeeper initialize a new keeper. -func NewKeeper(storeKey *cosmostypes.KVStoreKey, serviceKeeper *servicesdk.Keeper, instanceKeeper *instancesdk.Keeper, runnerKeeper *runnersdk.Keeper, processKeeper *processsdk.Keeper) *Keeper { +func NewKeeper(storeKey *cosmostypes.KVStoreKey, serviceKeeper *servicesdk.Keeper, instanceKeeper instance.Keeper, runnerKeeper *runnersdk.Keeper, processKeeper *processsdk.Keeper) *Keeper { return &Keeper{ storeKey: storeKey, serviceKeeper: serviceKeeper, diff --git a/sdk/execution/sdk.go b/sdk/execution/sdk.go index 111f76930..6d657ee28 100644 --- a/sdk/execution/sdk.go +++ b/sdk/execution/sdk.go @@ -7,7 +7,6 @@ import ( "github.com/mesg-foundation/engine/execution" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/protobuf/api" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" runnersdk "github.com/mesg-foundation/engine/sdk/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" ) @@ -16,18 +15,16 @@ import ( type SDK struct { client *cosmos.Client - serviceSDK *servicesdk.SDK - instanceSDK *instancesdk.SDK - runnerSDK *runnersdk.SDK + serviceSDK *servicesdk.SDK + runnerSDK *runnersdk.SDK } // New returns the execution sdk. -func New(client *cosmos.Client, serviceSDK *servicesdk.SDK, instanceSDK *instancesdk.SDK, runnerSDK *runnersdk.SDK) *SDK { +func New(client *cosmos.Client, serviceSDK *servicesdk.SDK, runnerSDK *runnersdk.SDK) *SDK { sdk := &SDK{ - client: client, - serviceSDK: serviceSDK, - instanceSDK: instanceSDK, - runnerSDK: runnerSDK, + client: client, + serviceSDK: serviceSDK, + runnerSDK: runnerSDK, } return sdk } diff --git a/sdk/instance/keeper.go b/sdk/instance/keeper.go deleted file mode 100644 index d59c8573a..000000000 --- a/sdk/instance/keeper.go +++ /dev/null @@ -1,80 +0,0 @@ -package instancesdk - -import ( - "fmt" - - cosmostypes "github.com/cosmos/cosmos-sdk/types" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/instance" - "github.com/mesg-foundation/engine/protobuf/api" -) - -// Keeper holds the logic to read and write data. -type Keeper struct { - storeKey *cosmostypes.KVStoreKey -} - -// NewKeeper initialize a new keeper. -func NewKeeper(storeKey *cosmostypes.KVStoreKey) *Keeper { - return &Keeper{ - storeKey: storeKey, - } -} - -// FetchOrCreate creates a new instance if needed. -func (k *Keeper) FetchOrCreate(request cosmostypes.Request, serviceHash hash.Hash, envHash hash.Hash) (*instance.Instance, error) { - inst := &instance.Instance{ - ServiceHash: serviceHash, - EnvHash: envHash, - } - inst.Hash = hash.Dump(inst) - - if store := request.KVStore(k.storeKey); !store.Has(inst.Hash) { - value, err := codec.MarshalBinaryBare(inst) - if err != nil { - return nil, err - } - store.Set(inst.Hash, value) - } - - return inst, nil -} - -// Get returns the instance that matches given hash. -func (k *Keeper) Get(request cosmostypes.Request, hash hash.Hash) (*instance.Instance, error) { - var i *instance.Instance - store := request.KVStore(k.storeKey) - if !store.Has(hash) { - return nil, fmt.Errorf("instance %q not found", hash) - } - value := store.Get(hash) - return i, codec.UnmarshalBinaryBare(value, &i) -} - -// Exists returns true if a specific set of data exists in the database, false otherwise -func (k *Keeper) Exists(request cosmostypes.Request, hash hash.Hash) (bool, error) { - return request.KVStore(k.storeKey).Has(hash), nil -} - -// List returns all instances. -func (k *Keeper) List(request cosmostypes.Request, f *api.ListInstanceRequest_Filter) ([]*instance.Instance, error) { - var ( - instances []*instance.Instance - iter = request.KVStore(k.storeKey).Iterator(nil, nil) - ) - - // filter results - for iter.Valid() { - var i *instance.Instance - if err := codec.UnmarshalBinaryBare(iter.Value(), &i); err != nil { - return nil, err - } - if f == nil || f.ServiceHash.IsZero() || i.ServiceHash.Equal(f.ServiceHash) { - instances = append(instances, i) - } - iter.Next() - } - iter.Close() - return instances, nil -} diff --git a/sdk/instance/module.go b/sdk/instance/module.go deleted file mode 100644 index db691c72f..000000000 --- a/sdk/instance/module.go +++ /dev/null @@ -1,56 +0,0 @@ -package instancesdk - -import ( - "fmt" - - cosmostypes "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/cosmos-sdk/types/module" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/protobuf/api" - abci "github.com/tendermint/tendermint/abci/types" -) - -// ModuleName is the name of this module. -const ModuleName = "instance" - -// NewModule returns the module of this sdk. -func NewModule(k *Keeper) module.AppModule { - return cosmos.NewAppModule(cosmos.NewAppModuleBasic(ModuleName), handler(k), querier(k)) -} - -func handler(k *Keeper) cosmos.Handler { - return func(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { - errmsg := fmt.Sprintf("Unrecognized instance Msg type: %v", msg.Type()) - return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errmsg) - } -} - -func querier(k *Keeper) cosmos.Querier { - return func(request cosmostypes.Request, path []string, req abci.RequestQuery) (res interface{}, err error) { - switch path[0] { - case "get": - hash, err := hash.Decode(path[1]) - if err != nil { - return nil, err - } - return k.Get(request, hash) - case "list": - var f api.ListInstanceRequest_Filter - if err := codec.UnmarshalBinaryBare(req.Data, &f); err != nil { - return nil, err - } - return k.List(request, &f) - case "exists": - hash, err := hash.Decode(path[1]) - if err != nil { - return nil, err - } - return k.Exists(request, hash) - default: - return nil, fmt.Errorf("unknown instance query endpoint %s", path[0]) - } - } -} diff --git a/sdk/instance/sdk.go b/sdk/instance/sdk.go deleted file mode 100644 index 9400e518d..000000000 --- a/sdk/instance/sdk.go +++ /dev/null @@ -1,48 +0,0 @@ -package instancesdk - -import ( - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/instance" - "github.com/mesg-foundation/engine/protobuf/api" -) - -// SDK is the instance sdk. -type SDK struct { - client *cosmos.Client -} - -// New returns the instance sdk. -func New(client *cosmos.Client) *SDK { - sdk := &SDK{ - client: client, - } - return sdk -} - -// Get returns the instance that matches given hash. -func (s *SDK) Get(hash hash.Hash) (*instance.Instance, error) { - var instance instance.Instance - if err := s.client.Query("custom/"+ModuleName+"/get/"+hash.String(), nil, &instance); err != nil { - return nil, err - } - return &instance, nil -} - -// List returns all instances. -func (s *SDK) List(f *api.ListInstanceRequest_Filter) ([]*instance.Instance, error) { - var instances []*instance.Instance - if err := s.client.Query("custom/"+ModuleName+"/list", f, &instances); err != nil { - return nil, err - } - return instances, nil -} - -// Exists returns if a instance already exists. -func (s *SDK) Exists(hash hash.Hash) (bool, error) { - var exists bool - if err := s.client.Query("custom/"+ModuleName+"/exists/"+hash.String(), nil, &exists); err != nil { - return false, err - } - return exists, nil -} diff --git a/sdk/process/keeper.go b/sdk/process/keeper.go index c4a51aa19..726ef201b 100644 --- a/sdk/process/keeper.go +++ b/sdk/process/keeper.go @@ -8,7 +8,7 @@ import ( "github.com/mesg-foundation/engine/hash" ownershippb "github.com/mesg-foundation/engine/ownership" "github.com/mesg-foundation/engine/process" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" + "github.com/mesg-foundation/engine/x/instance" "github.com/mesg-foundation/engine/x/ownership" ) @@ -16,11 +16,11 @@ import ( type Keeper struct { storeKey *cosmostypes.KVStoreKey ownershipKeeper ownership.Keeper - instanceKeeper *instancesdk.Keeper + instanceKeeper instance.Keeper } // NewKeeper returns the keeper of the service sdk. -func NewKeeper(storeKey *cosmostypes.KVStoreKey, ownershipKeeper ownership.Keeper, instanceKeeper *instancesdk.Keeper) *Keeper { +func NewKeeper(storeKey *cosmostypes.KVStoreKey, ownershipKeeper ownership.Keeper, instanceKeeper instance.Keeper) *Keeper { return &Keeper{ storeKey: storeKey, ownershipKeeper: ownershipKeeper, diff --git a/sdk/runner/keeper.go b/sdk/runner/keeper.go index 4ba932e32..21a2d5be3 100644 --- a/sdk/runner/keeper.go +++ b/sdk/runner/keeper.go @@ -8,17 +8,17 @@ import ( "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/runner" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" + "github.com/mesg-foundation/engine/x/instance" ) // Keeper holds the logic to read and write data. type Keeper struct { storeKey *cosmostypes.KVStoreKey - instanceKeeper *instancesdk.Keeper + instanceKeeper instance.Keeper } // NewKeeper initialize a new keeper. -func NewKeeper(storeKey *cosmostypes.KVStoreKey, instanceKeeper *instancesdk.Keeper) *Keeper { +func NewKeeper(storeKey *cosmostypes.KVStoreKey, instanceKeeper instance.Keeper) *Keeper { return &Keeper{ storeKey: storeKey, instanceKeeper: instanceKeeper, diff --git a/sdk/runner/sdk.go b/sdk/runner/sdk.go index 35dcd4e91..fe301699b 100644 --- a/sdk/runner/sdk.go +++ b/sdk/runner/sdk.go @@ -8,17 +8,16 @@ import ( "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/ext/xos" "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/instance" + instancepb "github.com/mesg-foundation/engine/instance" "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/runner" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" servicesdk "github.com/mesg-foundation/engine/sdk/service" + "github.com/mesg-foundation/engine/x/instance" ) // SDK is the runner sdk. type SDK struct { serviceSDK *servicesdk.SDK - instanceSDK *instancesdk.SDK client *cosmos.Client container container.Container port string @@ -33,11 +32,10 @@ type Filter struct { } // New returns the runner sdk. -func New(client *cosmos.Client, serviceSDK *servicesdk.SDK, instanceSDK *instancesdk.SDK, container container.Container, engineName, port, ipfsEndpoint string) *SDK { +func New(client *cosmos.Client, serviceSDK *servicesdk.SDK, container container.Container, engineName, port, ipfsEndpoint string) *SDK { sdk := &SDK{ container: container, serviceSDK: serviceSDK, - instanceSDK: instanceSDK, client: client, port: port, engineName: engineName, @@ -57,7 +55,7 @@ func (s *SDK) Create(req *api.CreateRunnerRequest) (*runner.Runner, error) { instanceEnv := xos.EnvMergeSlices(srv.Configuration.Env, req.Env) envHash := hash.Dump(instanceEnv) // TODO: should be done by instance or runner - instanceHash := hash.Dump(&instance.Instance{ + instanceHash := hash.Dump(&instancepb.Instance{ ServiceHash: srv.Hash, EnvHash: envHash, }) @@ -122,11 +120,11 @@ func (s *SDK) Delete(req *api.DeleteRunnerRequest) error { return err } - // get instance and service - inst, err := s.instanceSDK.Get(runner.InstanceHash) - if err != nil { + var inst instancepb.Instance + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", instance.QuerierRoute, instance.QueryGetInstance, runner.InstanceHash), nil, &inst); err != nil { return err } + srv, err := s.serviceSDK.Get(inst.ServiceHash) if err != nil { return err diff --git a/sdk/sdk.go b/sdk/sdk.go index c1f511d83..5ddbdbd4d 100644 --- a/sdk/sdk.go +++ b/sdk/sdk.go @@ -6,7 +6,6 @@ import ( "github.com/mesg-foundation/engine/cosmos" eventsdk "github.com/mesg-foundation/engine/sdk/event" executionsdk "github.com/mesg-foundation/engine/sdk/execution" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" processesdk "github.com/mesg-foundation/engine/sdk/process" runnersdk "github.com/mesg-foundation/engine/sdk/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" @@ -15,7 +14,6 @@ import ( // SDK exposes all functionalities of MESG Engine. type SDK struct { Service *servicesdk.SDK - Instance *instancesdk.SDK Execution *executionsdk.SDK Event *eventsdk.Event Process *processesdk.SDK @@ -26,14 +24,12 @@ type SDK struct { func New(client *cosmos.Client, kb *cosmos.Keybase, container container.Container, engineName, port, ipfsEndpoint string) *SDK { ps := pubsub.New(0) serviceSDK := servicesdk.New(client) - instanceSDK := instancesdk.New(client) - runnerSDK := runnersdk.New(client, serviceSDK, instanceSDK, container, engineName, port, ipfsEndpoint) + runnerSDK := runnersdk.New(client, serviceSDK, container, engineName, port, ipfsEndpoint) processSDK := processesdk.New(client) - executionSDK := executionsdk.New(client, serviceSDK, instanceSDK, runnerSDK) - eventSDK := eventsdk.New(ps, serviceSDK, instanceSDK) + executionSDK := executionsdk.New(client, serviceSDK, runnerSDK) + eventSDK := eventsdk.New(ps, serviceSDK, client) return &SDK{ Service: serviceSDK, - Instance: instanceSDK, Execution: executionSDK, Event: eventSDK, Process: processSDK, diff --git a/server/grpc/api/instance.go b/server/grpc/api/instance.go index 5c833070d..645e6d6a5 100644 --- a/server/grpc/api/instance.go +++ b/server/grpc/api/instance.go @@ -2,32 +2,46 @@ package api import ( "context" + "fmt" - "github.com/mesg-foundation/engine/instance" + "github.com/mesg-foundation/engine/cosmos" + instancepb "github.com/mesg-foundation/engine/instance" protobuf_api "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/sdk" + "github.com/mesg-foundation/engine/x/instance" ) // InstanceServer is the type to aggregate all Instance APIs. type InstanceServer struct { - sdk *sdk.SDK + sdk *sdk.SDK + client *cosmos.Client } // NewInstanceServer creates a new ServiceServer. -func NewInstanceServer(sdk *sdk.SDK) *InstanceServer { - return &InstanceServer{sdk: sdk} +func NewInstanceServer(sdk *sdk.SDK, client *cosmos.Client) *InstanceServer { + return &InstanceServer{ + sdk: sdk, + client: client, + } } // List instances. func (s *InstanceServer) List(ctx context.Context, request *protobuf_api.ListInstanceRequest) (*protobuf_api.ListInstanceResponse, error) { - instances, err := s.sdk.Instance.List(request.Filter) - if err != nil { + var instances []*instancepb.Instance + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s", instance.QuerierRoute, instance.QueryListInstances), request.Filter, &instances); err != nil { return nil, err } return &protobuf_api.ListInstanceResponse{Instances: instances}, nil } // Get retrives instance. -func (s *InstanceServer) Get(ctx context.Context, request *protobuf_api.GetInstanceRequest) (*instance.Instance, error) { - return s.sdk.Instance.Get(request.Hash) +func (s *InstanceServer) Get(ctx context.Context, request *protobuf_api.GetInstanceRequest) (*instancepb.Instance, error) { + var inst instancepb.Instance + err := s.client.QueryJSON( + fmt.Sprintf("custom/%s/%s/%s", instance.QuerierRoute, instance.QueryGetInstance, request.Hash), + nil, &inst) + if err != nil { + return nil, err + } + return &inst, nil } diff --git a/server/grpc/server.go b/server/grpc/server.go index 847d79db2..d43ab13f6 100644 --- a/server/grpc/server.go +++ b/server/grpc/server.go @@ -83,7 +83,7 @@ func validateInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryS func (s *Server) register() { protobuf_api.RegisterEventServer(s.instance, api.NewEventServer(s.sdk)) protobuf_api.RegisterExecutionServer(s.instance, api.NewExecutionServer(s.sdk)) - protobuf_api.RegisterInstanceServer(s.instance, api.NewInstanceServer(s.sdk)) + protobuf_api.RegisterInstanceServer(s.instance, api.NewInstanceServer(s.sdk, s.client)) protobuf_api.RegisterServiceServer(s.instance, api.NewServiceServer(s.sdk)) protobuf_api.RegisterProcessServer(s.instance, api.NewProcessServer(s.sdk)) protobuf_api.RegisterOwnershipServer(s.instance, api.NewOwnershipServer(s.sdk, s.client)) diff --git a/x/instance/abci.go b/x/instance/abci.go new file mode 100644 index 000000000..5b75cbec2 --- /dev/null +++ b/x/instance/abci.go @@ -0,0 +1,13 @@ +package instance + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// BeginBlocker check for infraction evidence or downtime of validators +// on every begin block +func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k Keeper) {} + +// EndBlocker called every block, process inflation, update validator set. +func EndBlocker(ctx sdk.Context, k Keeper) {} diff --git a/x/instance/alias.go b/x/instance/alias.go new file mode 100644 index 000000000..558da0ee9 --- /dev/null +++ b/x/instance/alias.go @@ -0,0 +1,36 @@ +package instance + +import ( + "github.com/mesg-foundation/engine/x/instance/internal/keeper" + "github.com/mesg-foundation/engine/x/instance/internal/types" +) + +// const aliases +const ( + ModuleName = types.ModuleName + RouterKey = types.RouterKey + StoreKey = types.StoreKey + DefaultParamspace = types.DefaultParamspace + QuerierRoute = types.QuerierRoute +) + +// functions and variable aliases +var ( + NewKeeper = keeper.NewKeeper + NewQuerier = keeper.NewQuerier + RegisterCodec = types.RegisterCodec + NewGenesisState = types.NewGenesisState + DefaultGenesisState = types.DefaultGenesisState + ValidateGenesis = types.ValidateGenesis + + ModuleCdc = types.ModuleCdc + QueryGetInstance = types.QueryGetInstance + QueryListInstances = types.QueryListInstances +) + +// module types +type ( + Keeper = keeper.Keeper + GenesisState = types.GenesisState + Params = types.Params +) diff --git a/x/instance/client/cli/query.go b/x/instance/client/cli/query.go new file mode 100644 index 000000000..507a8f152 --- /dev/null +++ b/x/instance/client/cli/query.go @@ -0,0 +1,72 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/mesg-foundation/engine/instance" + "github.com/mesg-foundation/engine/x/instance/internal/types" + "github.com/spf13/cobra" +) + +// GetQueryCmd returns the cli query commands for this module +func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { + // Group instance queries under a subcommand + instanceQueryCmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + instanceQueryCmd.AddCommand( + flags.GetCommands( + GetCmdGetInstance(queryRoute, cdc), + GetCmdListInstances(queryRoute, cdc), + )..., + ) + + return instanceQueryCmd +} + +func GetCmdGetInstance(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "get", + Short: "get", + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetInstance), nil) + if err != nil { + fmt.Printf("could not get instance\n%s\n", err.Error()) + return nil + } + + var out *instance.Instance + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} + +func GetCmdListInstances(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "list", + Short: "list", + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryListInstances), nil) + if err != nil { + fmt.Printf("could not list instances\n%s\n", err.Error()) + return nil + } + + var out []*instance.Instance + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} diff --git a/x/instance/client/cli/tx.go b/x/instance/client/cli/tx.go new file mode 100644 index 000000000..43d50b641 --- /dev/null +++ b/x/instance/client/cli/tx.go @@ -0,0 +1,25 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/mesg-foundation/engine/x/instance/internal/types" + "github.com/spf13/cobra" +) + +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + ownershipTxCmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + ownershipTxCmd.AddCommand(flags.PostCommands()...) + return ownershipTxCmd +} diff --git a/x/instance/client/rest/query.go b/x/instance/client/rest/query.go new file mode 100644 index 000000000..8090daa9b --- /dev/null +++ b/x/instance/client/rest/query.go @@ -0,0 +1,46 @@ +package rest + +import ( + "fmt" + "net/http" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/types/rest" + "github.com/gorilla/mux" + "github.com/mesg-foundation/engine/x/instance/internal/types" +) + +func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { + r.HandleFunc( + "/ownership/get", + queryHandlerFn(cliCtx, types.QueryGetInstance), + ).Methods(http.MethodGet) + r.HandleFunc( + "/ownership/list", + queryHandlerFn(cliCtx, types.QueryListInstances), + ).Methods(http.MethodGet) + r.HandleFunc( + "/instance/parameters", + queryHandlerFn(cliCtx, types.QuerierRoute), + ).Methods("GET") +} + +func queryHandlerFn(cliCtx context.CLIContext, path string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, path) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} diff --git a/x/instance/client/rest/rest.go b/x/instance/client/rest/rest.go new file mode 100644 index 000000000..5a4ce220e --- /dev/null +++ b/x/instance/client/rest/rest.go @@ -0,0 +1,12 @@ +package rest + +import ( + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/gorilla/mux" +) + +// RegisterRoutes registers instance-related REST handlers to a router +func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) { + registerQueryRoutes(cliCtx, r) + registerTxRoutes(cliCtx, r) +} diff --git a/x/instance/client/rest/tx.go b/x/instance/client/rest/tx.go new file mode 100644 index 000000000..14207f21a --- /dev/null +++ b/x/instance/client/rest/tx.go @@ -0,0 +1,8 @@ +package rest + +import ( + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/gorilla/mux" +) + +func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {} diff --git a/x/instance/genesis.go b/x/instance/genesis.go new file mode 100644 index 000000000..798a6b22a --- /dev/null +++ b/x/instance/genesis.go @@ -0,0 +1,18 @@ +package instance + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/mesg-foundation/engine/x/instance/internal/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// InitGenesis initialize default parameters and the keeper's address to pubkey map. +func InitGenesis(ctx sdk.Context, k Keeper, data types.GenesisState) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} + +// ExportGenesis writes the current store values // to a genesis file, +// which can be imported again with InitGenesis. +func ExportGenesis(ctx sdk.Context, k Keeper) types.GenesisState { + return types.NewGenesisState() +} diff --git a/x/instance/handler.go b/x/instance/handler.go new file mode 100644 index 000000000..227f262e3 --- /dev/null +++ b/x/instance/handler.go @@ -0,0 +1,17 @@ +package instance + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/x/instance/internal/types" +) + +// NewHandler creates an sdk.Handler for all the instance type messages +func NewHandler(k Keeper) sdk.Handler { + return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg) + return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg) + } +} diff --git a/x/instance/internal/keeper/keeper.go b/x/instance/internal/keeper/keeper.go new file mode 100644 index 000000000..751a89042 --- /dev/null +++ b/x/instance/internal/keeper/keeper.go @@ -0,0 +1,88 @@ +package keeper + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/instance" + "github.com/mesg-foundation/engine/protobuf/api" + "github.com/mesg-foundation/engine/x/instance/internal/types" + "github.com/tendermint/tendermint/libs/log" +) + +// Keeper of the instance store +type Keeper struct { + storeKey sdk.StoreKey + cdc *codec.Codec +} + +// NewKeeper creates a instance keeper +func NewKeeper(cdc *codec.Codec, key sdk.StoreKey) Keeper { + keeper := Keeper{ + storeKey: key, + cdc: cdc, + } + return keeper +} + +// Logger returns a module-specific logger. +func (k Keeper) Logger(ctx sdk.Context) log.Logger { + return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) +} + +// FetchOrCreate creates a new instance if needed. +func (k Keeper) FetchOrCreate(ctx sdk.Context, serviceHash hash.Hash, envHash hash.Hash) (*instance.Instance, error) { + store := ctx.KVStore(k.storeKey) + + inst := &instance.Instance{ + ServiceHash: serviceHash, + EnvHash: envHash, + } + inst.Hash = hash.Dump(inst) + + if !store.Has(inst.Hash) { + value, err := k.cdc.MarshalBinaryLengthPrefixed(inst) + if err != nil { + return nil, err + } + store.Set(inst.Hash, value) + } + + return inst, nil +} + +// Get returns the instance from the keeper. +func (k Keeper) Get(ctx sdk.Context, instanceHash hash.Hash) (*instance.Instance, error) { + store := ctx.KVStore(k.storeKey) + if !store.Has(instanceHash) { + return nil, fmt.Errorf("instance %q not found", instanceHash) + } + + var item *instance.Instance + if err := k.cdc.UnmarshalBinaryLengthPrefixed(store.Get(instanceHash), &item); err != nil { + return nil, err + } + return item, nil +} + +// List returns instances from the keeper. +func (k Keeper) List(ctx sdk.Context, f *api.ListInstanceRequest_Filter) ([]*instance.Instance, error) { + store := ctx.KVStore(k.storeKey) + iter := store.Iterator(nil, nil) + var items []*instance.Instance + + for iter.Valid() { + var item *instance.Instance + if err := k.cdc.UnmarshalBinaryLengthPrefixed(iter.Value(), &item); err != nil { + return nil, err + } + if f == nil || f.ServiceHash.IsZero() || item.ServiceHash.Equal(f.ServiceHash) { + items = append(items, item) + } + iter.Next() + } + iter.Close() + return items, nil +} diff --git a/x/instance/internal/keeper/querier.go b/x/instance/internal/keeper/querier.go new file mode 100644 index 000000000..ace4b7df2 --- /dev/null +++ b/x/instance/internal/keeper/querier.go @@ -0,0 +1,62 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/protobuf/api" + "github.com/mesg-foundation/engine/x/instance/internal/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// NewQuerier creates a new querier for instance clients. +func NewQuerier(k Keeper) sdk.Querier { + return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) { + switch path[0] { + case types.QueryGetInstance: + return getInstance(ctx, path[1:], k) + case types.QueryListInstances: + return listInstance(ctx, req, k) + default: + return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown instance query endpoint") + } + } +} + +func getInstance(ctx sdk.Context, path []string, k Keeper) ([]byte, error) { + hash, err := hash.Decode(path[0]) + if err != nil { + return nil, err + } + + instance, err := k.Get(ctx, hash) + if err != nil { + return nil, err + } + + res, err := types.ModuleCdc.MarshalJSON(instance) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return res, nil +} + +func listInstance(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) { + var f *api.ListInstanceRequest_Filter + if len(req.Data) > 0 { + if err := types.ModuleCdc.UnmarshalJSON(req.Data, &f); err != nil { + return nil, err + } + } + + instances, err := k.List(ctx, f) + if err != nil { + return nil, err + } + + res, err := types.ModuleCdc.MarshalJSON(instances) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return res, nil +} diff --git a/x/instance/internal/types/codec.go b/x/instance/internal/types/codec.go new file mode 100644 index 000000000..4b5e77ba8 --- /dev/null +++ b/x/instance/internal/types/codec.go @@ -0,0 +1,18 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" +) + +// RegisterCodec registers concrete types on codec +func RegisterCodec(cdc *codec.Codec) {} + +// ModuleCdc defines the module codec +var ModuleCdc *codec.Codec + +func init() { + ModuleCdc = codec.New() + RegisterCodec(ModuleCdc) + codec.RegisterCrypto(ModuleCdc) + ModuleCdc.Seal() +} diff --git a/x/instance/internal/types/expected_keepers.go b/x/instance/internal/types/expected_keepers.go new file mode 100644 index 000000000..23128499d --- /dev/null +++ b/x/instance/internal/types/expected_keepers.go @@ -0,0 +1,14 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/params" +) + +// ParamSubspace defines the expected Subspace interfacace +type ParamSubspace interface { + WithKeyTable(table params.KeyTable) params.Subspace + Get(ctx sdk.Context, key []byte, ptr interface{}) + GetParamSet(ctx sdk.Context, ps params.ParamSet) + SetParamSet(ctx sdk.Context, ps params.ParamSet) +} diff --git a/x/instance/internal/types/genesis.go b/x/instance/internal/types/genesis.go new file mode 100644 index 000000000..bebeec0dc --- /dev/null +++ b/x/instance/internal/types/genesis.go @@ -0,0 +1,19 @@ +package types + +// GenesisState - all instance state that must be provided at genesis +type GenesisState struct{} + +// NewGenesisState creates a new GenesisState object +func NewGenesisState() GenesisState { + return GenesisState{} +} + +// DefaultGenesisState - default GenesisState used by Cosmos Hub +func DefaultGenesisState() GenesisState { + return GenesisState{} +} + +// ValidateGenesis validates the instance genesis parameters +func ValidateGenesis(data GenesisState) error { + return nil +} diff --git a/x/instance/internal/types/key.go b/x/instance/internal/types/key.go new file mode 100644 index 000000000..023017612 --- /dev/null +++ b/x/instance/internal/types/key.go @@ -0,0 +1,15 @@ +package types + +const ( + // ModuleName is the name of the module + ModuleName = "instance" + + // StoreKey to be used when creating the KVStore + StoreKey = ModuleName + + // RouterKey to be used for routing msgs + RouterKey = ModuleName + + // QuerierRoute to be used for routing + QuerierRoute = ModuleName +) diff --git a/x/instance/internal/types/params.go b/x/instance/internal/types/params.go new file mode 100644 index 000000000..4940992b5 --- /dev/null +++ b/x/instance/internal/types/params.go @@ -0,0 +1,38 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/x/params" +) + +// Default parameter namespace +const ( + DefaultParamspace = ModuleName +) + +// ParamKeyTable for instance module +func ParamKeyTable() params.KeyTable { + return params.NewKeyTable().RegisterParamSet(&Params{}) +} + +// Params - used for initializing default parameter for instance at genesis +type Params struct{} + +// NewParams creates a new Params object +func NewParams() Params { + return Params{} +} + +// String implements the stringer interface for Params +func (p Params) String() string { + return "" +} + +// ParamSetPairs - Implements params.ParamSet +func (p *Params) ParamSetPairs() params.ParamSetPairs { + return params.ParamSetPairs{} +} + +// DefaultParams defines the parameters for this module +func DefaultParams() Params { + return NewParams() +} diff --git a/x/instance/internal/types/querier.go b/x/instance/internal/types/querier.go new file mode 100644 index 000000000..88d4c3574 --- /dev/null +++ b/x/instance/internal/types/querier.go @@ -0,0 +1,8 @@ +package types + +// Query endpoints supported by the instance querier +const ( + QueryGetInstance = "get" + QueryExistInstance = "exist" + QueryListInstances = "list" +) diff --git a/x/instance/module.go b/x/instance/module.go new file mode 100644 index 000000000..d371a647a --- /dev/null +++ b/x/instance/module.go @@ -0,0 +1,139 @@ +package instance + +import ( + "encoding/json" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/gorilla/mux" + "github.com/mesg-foundation/engine/x/instance/client/cli" + "github.com/mesg-foundation/engine/x/instance/client/rest" + "github.com/mesg-foundation/engine/x/instance/internal/types" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" +) + +var ( + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} +) + +// AppModuleBasic defines the basic application module used by the instance module. +type AppModuleBasic struct{} + +var _ module.AppModuleBasic = AppModuleBasic{} + +// Name returns the instance module's name. +func (AppModuleBasic) Name() string { + return types.ModuleName +} + +// RegisterCodec registers the instance module's types for the given codec. +func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { + RegisterCodec(cdc) +} + +// DefaultGenesis returns default genesis state as raw bytes for the instance +// module. +func (AppModuleBasic) DefaultGenesis() json.RawMessage { + return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) +} + +// ValidateGenesis performs genesis state validation for the instance module. +func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { + var data GenesisState + err := ModuleCdc.UnmarshalJSON(bz, &data) + if err != nil { + return err + } + return ValidateGenesis(data) +} + +// RegisterRESTRoutes registers the REST routes for the instance module. +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { + rest.RegisterRoutes(ctx, rtr) +} + +// GetTxCmd returns the root tx command for the instance module. +func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { + return nil +} + +// GetQueryCmd returns no root query command for the instance module. +func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { + return cli.GetQueryCmd(StoreKey, cdc) +} + +//____________________________________________________________________________ + +// AppModule implements an application module for the instance module. +type AppModule struct { + AppModuleBasic + + keeper Keeper +} + +// NewAppModule creates a new AppModule object +func NewAppModule(k Keeper) AppModule { + return AppModule{ + AppModuleBasic: AppModuleBasic{}, + keeper: k, + } +} + +// Name returns the instance module's name. +func (AppModule) Name() string { + return ModuleName +} + +// RegisterInvariants registers the instance module invariants. +func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} + +// Route returns the message routing key for the instance module. +func (AppModule) Route() string { + return RouterKey +} + +// NewHandler returns an sdk.Handler for the instance module. +func (am AppModule) NewHandler() sdk.Handler { + return NewHandler(am.keeper) +} + +// QuerierRoute returns the instance module's querier route name. +func (AppModule) QuerierRoute() string { + return QuerierRoute +} + +// NewQuerierHandler returns the instance module sdk.Querier. +func (am AppModule) NewQuerierHandler() sdk.Querier { + return NewQuerier(am.keeper) +} + +// InitGenesis performs genesis initialization for the instance module. It returns +// no validator updates. +func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { + var genesisState GenesisState + ModuleCdc.MustUnmarshalJSON(data, &genesisState) + InitGenesis(ctx, am.keeper, genesisState) + return []abci.ValidatorUpdate{} +} + +// ExportGenesis returns the exported genesis state as raw bytes for the instance +// module. +func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { + gs := ExportGenesis(ctx, am.keeper) + return ModuleCdc.MustMarshalJSON(gs) +} + +// BeginBlock returns the begin blocker for the instance module. +func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { + BeginBlocker(ctx, req, am.keeper) +} + +// EndBlock returns the end blocker for the instance module. It returns no validator +// updates. +func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} From 0ab1e08813b23266bcd981dec75a53bab182cfd8 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 12 Feb 2020 11:04:32 +0700 Subject: [PATCH 25/87] add e2e lcd test for ownerships --- e2e/api_test.go | 18 ++++++++++++++++++ e2e/process_test.go | 42 +++++++++++++++++++++++++++++++----------- e2e/service_test.go | 22 ++++++++++++++++------ 3 files changed, 65 insertions(+), 17 deletions(-) diff --git a/e2e/api_test.go b/e2e/api_test.go index e7348047c..a96ec3a65 100644 --- a/e2e/api_test.go +++ b/e2e/api_test.go @@ -2,8 +2,12 @@ package main import ( "context" + "io/ioutil" + "net/http" "testing" + "github.com/cosmos/cosmos-sdk/types/rest" + "github.com/mesg-foundation/engine/app" pb "github.com/mesg-foundation/engine/protobuf/api" "github.com/stretchr/testify/require" "google.golang.org/grpc" @@ -21,6 +25,20 @@ type apiclient struct { var client apiclient +const lcdEndpoint = "http://127.0.0.1:1317/" + +func lcdGet(t *testing.T, path string, ptr interface{}) { + resp, err := http.Get(lcdEndpoint + path) + require.NoError(t, err) + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + require.NoError(t, err) + cdc := app.MakeCodec() + cosResp := rest.ResponseWithHeight{} + require.NoError(t, cdc.UnmarshalJSON(body, &cosResp)) + require.NoError(t, cdc.UnmarshalJSON(cosResp.Result, ptr)) +} + func TestAPI(t *testing.T) { if testing.Short() { t.Skip() diff --git a/e2e/process_test.go b/e2e/process_test.go index 23ea4d869..8a309b642 100644 --- a/e2e/process_test.go +++ b/e2e/process_test.go @@ -66,15 +66,28 @@ func testProcess(t *testing.T) { }) t.Run("check ownership creation", func(t *testing.T) { - ownerships, err := client.OwnershipClient.List(context.Background(), &pb.ListOwnershipRequest{}) - require.NoError(t, err) - owners := make([]*ownership.Ownership, 0) - for _, o := range ownerships.Ownerships { - if o.ResourceHash.Equal(processHash) && o.Resource == ownership.Ownership_Process && o.Owner != "" { - owners = append(owners, o) + t.Run("lcd", func(t *testing.T) { + ownerships := make([]*ownership.Ownership, 0) + lcdGet(t, "ownership/list", &ownerships) + owners := make([]*ownership.Ownership, 0) + for _, o := range ownerships { + if o.ResourceHash.Equal(processHash) && o.Resource == ownership.Ownership_Process && o.Owner != "" { + owners = append(owners, o) + } } - } - require.Len(t, owners, 1) + require.Len(t, owners, 1) + }) + t.Run("grpc", func(t *testing.T) { + ownerships, err := client.OwnershipClient.List(context.Background(), &pb.ListOwnershipRequest{}) + require.NoError(t, err) + owners := make([]*ownership.Ownership, 0) + for _, o := range ownerships.Ownerships { + if o.ResourceHash.Equal(processHash) && o.Resource == ownership.Ownership_Process && o.Owner != "" { + owners = append(owners, o) + } + } + require.Len(t, owners, 1) + }) }) t.Run("list", func(t *testing.T) { @@ -89,8 +102,15 @@ func testProcess(t *testing.T) { }) t.Run("check ownership deletion", func(t *testing.T) { - ownerships, err := client.OwnershipClient.List(context.Background(), &pb.ListOwnershipRequest{}) - require.NoError(t, err) - require.Len(t, ownerships.Ownerships, 1) + t.Run("lcd", func(t *testing.T) { + ownerships := make([]*ownership.Ownership, 0) + lcdGet(t, "ownership/list", &ownerships) + require.Len(t, ownerships, 1) + }) + t.Run("grpc", func(t *testing.T) { + ownerships, err := client.OwnershipClient.List(context.Background(), &pb.ListOwnershipRequest{}) + require.NoError(t, err) + require.Len(t, ownerships.Ownerships, 1) + }) }) } diff --git a/e2e/service_test.go b/e2e/service_test.go index e2583c02f..0c5740224 100644 --- a/e2e/service_test.go +++ b/e2e/service_test.go @@ -50,11 +50,21 @@ func testService(t *testing.T) { }) t.Run("check ownership creation", func(t *testing.T) { - ownerships, err := client.OwnershipClient.List(context.Background(), &pb.ListOwnershipRequest{}) - require.NoError(t, err) - require.Len(t, ownerships.Ownerships, 1) - require.NotEmpty(t, ownerships.Ownerships[0].Owner) - require.Equal(t, ownership.Ownership_Service, ownerships.Ownerships[0].Resource) - require.Equal(t, testServiceHash, ownerships.Ownerships[0].ResourceHash) + t.Run("lcd", func(t *testing.T) { + ownerships, err := client.OwnershipClient.List(context.Background(), &pb.ListOwnershipRequest{}) + require.NoError(t, err) + require.Len(t, ownerships.Ownerships, 1) + require.NotEmpty(t, ownerships.Ownerships[0].Owner) + require.Equal(t, ownership.Ownership_Service, ownerships.Ownerships[0].Resource) + require.Equal(t, testServiceHash, ownerships.Ownerships[0].ResourceHash) + }) + t.Run("grpc", func(t *testing.T) { + ownerships := make([]*ownership.Ownership, 0) + lcdGet(t, "ownership/list", &ownerships) + require.Len(t, ownerships, 1) + require.NotEmpty(t, ownerships[0].Owner) + require.Equal(t, ownership.Ownership_Service, ownerships[0].Resource) + require.Equal(t, testServiceHash, ownerships[0].ResourceHash) + }) }) } From 0dbd1f7539f17e969a0f91bde5d3fd1462e25cfa Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 12 Feb 2020 11:29:57 +0700 Subject: [PATCH 26/87] init only once the codec in e2e tests --- e2e/api_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/e2e/api_test.go b/e2e/api_test.go index a96ec3a65..0137dca7d 100644 --- a/e2e/api_test.go +++ b/e2e/api_test.go @@ -23,7 +23,10 @@ type apiclient struct { pb.RunnerClient } -var client apiclient +var ( + client apiclient + cdc = app.MakeCodec() +) const lcdEndpoint = "http://127.0.0.1:1317/" @@ -33,7 +36,6 @@ func lcdGet(t *testing.T, path string, ptr interface{}) { defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) require.NoError(t, err) - cdc := app.MakeCodec() cosResp := rest.ResponseWithHeight{} require.NoError(t, cdc.UnmarshalJSON(body, &cosResp)) require.NoError(t, cdc.UnmarshalJSON(cosResp.Result, ptr)) From ac8e61a840db6cb0c7f0115b5c4600cfa0dfd799 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Wed, 12 Feb 2020 05:47:09 +0000 Subject: [PATCH 27/87] Bump github.com/cosmos/cosmos-sdk from 0.38.0 to 0.38.1 Bumps [github.com/cosmos/cosmos-sdk](https://github.com/cosmos/cosmos-sdk) from 0.38.0 to 0.38.1. - [Release notes](https://github.com/cosmos/cosmos-sdk/releases) - [Changelog](https://github.com/cosmos/cosmos-sdk/blob/master/CHANGELOG.md) - [Commits](https://github.com/cosmos/cosmos-sdk/compare/v0.38.0...v0.38.1) Signed-off-by: dependabot-preview[bot] --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index a6826e31d..e8b95ae85 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/btcsuite/btcd v0.0.0-20190807005414-4063feeff79a // indirect github.com/containerd/containerd v1.3.0 // indirect github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc // indirect - github.com/cosmos/cosmos-sdk v0.38.0 + github.com/cosmos/cosmos-sdk v0.38.1 github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d github.com/cskr/pubsub v1.0.2 github.com/docker/cli v0.0.0-20191011045415-5d85cdacd257 diff --git a/go.sum b/go.sum index b82fc1c1e..3368af69a 100644 --- a/go.sum +++ b/go.sum @@ -69,6 +69,8 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7 github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cosmos/cosmos-sdk v0.38.0 h1:BrflLMrECI2ZfftRAq2iAlxlyk+W/4iKVCNCC3a+RPc= github.com/cosmos/cosmos-sdk v0.38.0/go.mod h1:9ZZex0GKpyNCvilvVAPBoB+0n3A/aO1+/UhPVEaiCy4= +github.com/cosmos/cosmos-sdk v0.38.1 h1:DTuxIJeMpB//ydq+ObAjQgsaiwYBZ8T7NDzXjyiL1Kg= +github.com/cosmos/cosmos-sdk v0.38.1/go.mod h1:9ZZex0GKpyNCvilvVAPBoB+0n3A/aO1+/UhPVEaiCy4= github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d h1:49RLWk1j44Xu4fjHb6JFYmeUnDORVwHNkDxaQ0ctCVU= github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y= github.com/cosmos/ledger-cosmos-go v0.11.1 h1:9JIYsGnXP613pb2vPjFeMMjBI5lEDsEaF6oYorTy6J4= From 92ff3ac20486c7fdd1f906593a03c797078042e5 Mon Sep 17 00:00:00 2001 From: krhubert Date: Wed, 12 Feb 2020 09:07:59 +0100 Subject: [PATCH 28/87] Fix rest and cli client for instance moduel --- x/instance/client/cli/query.go | 3 +- x/instance/client/rest/query.go | 56 ++++++++++++++++++++++++---- x/instance/internal/keeper/keeper.go | 4 +- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/x/instance/client/cli/query.go b/x/instance/client/cli/query.go index 507a8f152..2152c26e9 100644 --- a/x/instance/client/cli/query.go +++ b/x/instance/client/cli/query.go @@ -37,9 +37,10 @@ func GetCmdGetInstance(queryRoute string, cdc *codec.Codec) *cobra.Command { return &cobra.Command{ Use: "get", Short: "get", + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) - res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetInstance), nil) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", queryRoute, types.QueryGetInstance, args[0]), nil) if err != nil { fmt.Printf("could not get instance\n%s\n", err.Error()) return nil diff --git a/x/instance/client/rest/query.go b/x/instance/client/rest/query.go index 8090daa9b..43437bdf9 100644 --- a/x/instance/client/rest/query.go +++ b/x/instance/client/rest/query.go @@ -12,27 +12,69 @@ import ( func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { r.HandleFunc( - "/ownership/get", - queryHandlerFn(cliCtx, types.QueryGetInstance), + "/instance/get/{hash}", + queryGetHandlerFn(cliCtx), ).Methods(http.MethodGet) r.HandleFunc( - "/ownership/list", - queryHandlerFn(cliCtx, types.QueryListInstances), + "/instance/list", + queryListHandlerFn(cliCtx), ).Methods(http.MethodGet) r.HandleFunc( "/instance/parameters", - queryHandlerFn(cliCtx, types.QuerierRoute), + queryParametersHandlerFn(cliCtx), ).Methods("GET") } -func queryHandlerFn(cliCtx context.CLIContext, path string) http.HandlerFunc { +func queryGetHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryGetInstance, vars["hash"]) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + +func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryListInstances) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + +func queryParametersHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) if !ok { return } - route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, path) + route := fmt.Sprintf("custom/%s/parameters", types.QuerierRoute) res, height, err := cliCtx.QueryWithData(route, nil) if err != nil { diff --git a/x/instance/internal/keeper/keeper.go b/x/instance/internal/keeper/keeper.go index 751a89042..849b1c87b 100644 --- a/x/instance/internal/keeper/keeper.go +++ b/x/instance/internal/keeper/keeper.go @@ -74,12 +74,12 @@ func (k Keeper) List(ctx sdk.Context, f *api.ListInstanceRequest_Filter) ([]*ins var items []*instance.Instance for iter.Valid() { - var item *instance.Instance + var item instance.Instance if err := k.cdc.UnmarshalBinaryLengthPrefixed(iter.Value(), &item); err != nil { return nil, err } if f == nil || f.ServiceHash.IsZero() || item.ServiceHash.Equal(f.ServiceHash) { - items = append(items, item) + items = append(items, &item) } iter.Next() } From 6b84f84aab811e11bd75e244d762a0456ba216b5 Mon Sep 17 00:00:00 2001 From: krhubert Date: Wed, 12 Feb 2020 09:08:50 +0100 Subject: [PATCH 29/87] Remove unused insntace module query type --- x/instance/internal/types/querier.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/instance/internal/types/querier.go b/x/instance/internal/types/querier.go index 88d4c3574..ec91f8b77 100644 --- a/x/instance/internal/types/querier.go +++ b/x/instance/internal/types/querier.go @@ -3,6 +3,5 @@ package types // Query endpoints supported by the instance querier const ( QueryGetInstance = "get" - QueryExistInstance = "exist" QueryListInstances = "list" ) From 0564530244ff4c1de200a493a87024a08a598d7d Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 12 Feb 2020 18:18:58 +0700 Subject: [PATCH 30/87] Put back pointer on instance unmarshal in instance keeper --- x/instance/internal/keeper/keeper.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/instance/internal/keeper/keeper.go b/x/instance/internal/keeper/keeper.go index 849b1c87b..751a89042 100644 --- a/x/instance/internal/keeper/keeper.go +++ b/x/instance/internal/keeper/keeper.go @@ -74,12 +74,12 @@ func (k Keeper) List(ctx sdk.Context, f *api.ListInstanceRequest_Filter) ([]*ins var items []*instance.Instance for iter.Valid() { - var item instance.Instance + var item *instance.Instance if err := k.cdc.UnmarshalBinaryLengthPrefixed(iter.Value(), &item); err != nil { return nil, err } if f == nil || f.ServiceHash.IsZero() || item.ServiceHash.Equal(f.ServiceHash) { - items = append(items, &item) + items = append(items, item) } iter.Next() } From 95d064439476bb460626c0d84d57efd3e73f774d Mon Sep 17 00:00:00 2001 From: krhubert Date: Wed, 12 Feb 2020 16:17:39 +0100 Subject: [PATCH 31/87] Use template to move runner cosmos module to x/ dir --- app/app.go | 14 +- sdk/execution/keeper.go | 6 +- sdk/runner/codec.go | 8 -- sdk/runner/keeper.go | 94 ------------- sdk/runner/module.go | 57 -------- sdk/runner/msgs.go | 111 --------------- sdk/runner/sdk.go | 41 +++--- x/runner/abci.go | 13 ++ x/runner/alias.go | 43 ++++++ x/runner/client/cli/query.go | 73 ++++++++++ x/runner/client/cli/tx.go | 53 ++++++++ x/runner/client/rest/query.go | 89 ++++++++++++ x/runner/client/rest/rest.go | 13 ++ x/runner/client/rest/tx.go | 10 ++ x/runner/genesis.go | 18 +++ x/runner/handler.go | 64 +++++++++ x/runner/internal/keeper/keeper.go | 108 +++++++++++++++ x/runner/internal/keeper/querier.go | 54 ++++++++ x/runner/internal/types/codec.go | 21 +++ x/runner/internal/types/events.go | 11 ++ x/runner/internal/types/expected_keepers.go | 21 +++ x/runner/internal/types/genesis.go | 19 +++ x/runner/internal/types/key.go | 15 +++ x/runner/internal/types/msg.go | 110 +++++++++++++++ x/runner/internal/types/params.go | 38 ++++++ x/runner/internal/types/querier.go | 7 + x/runner/module.go | 142 ++++++++++++++++++++ 27 files changed, 953 insertions(+), 300 deletions(-) delete mode 100644 sdk/runner/codec.go delete mode 100644 sdk/runner/keeper.go delete mode 100644 sdk/runner/module.go delete mode 100644 sdk/runner/msgs.go create mode 100644 x/runner/abci.go create mode 100644 x/runner/alias.go create mode 100644 x/runner/client/cli/query.go create mode 100644 x/runner/client/cli/tx.go create mode 100644 x/runner/client/rest/query.go create mode 100644 x/runner/client/rest/rest.go create mode 100644 x/runner/client/rest/tx.go create mode 100644 x/runner/genesis.go create mode 100644 x/runner/handler.go create mode 100644 x/runner/internal/keeper/keeper.go create mode 100644 x/runner/internal/keeper/querier.go create mode 100644 x/runner/internal/types/codec.go create mode 100644 x/runner/internal/types/events.go create mode 100644 x/runner/internal/types/expected_keepers.go create mode 100644 x/runner/internal/types/genesis.go create mode 100644 x/runner/internal/types/key.go create mode 100644 x/runner/internal/types/msg.go create mode 100644 x/runner/internal/types/params.go create mode 100644 x/runner/internal/types/querier.go create mode 100644 x/runner/module.go diff --git a/app/app.go b/app/app.go index 81e739819..c73b4a5c9 100644 --- a/app/app.go +++ b/app/app.go @@ -30,10 +30,10 @@ import ( "github.com/mesg-foundation/engine/cosmos" executionsdk "github.com/mesg-foundation/engine/sdk/execution" processsdk "github.com/mesg-foundation/engine/sdk/process" - runnersdk "github.com/mesg-foundation/engine/sdk/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" "github.com/mesg-foundation/engine/x/instance" "github.com/mesg-foundation/engine/x/ownership" + "github.com/mesg-foundation/engine/x/runner" ) const appName = "engine" @@ -61,8 +61,8 @@ var ( // Engine's AppModuleBasic ownership.AppModuleBasic{}, instance.AppModuleBasic{}, + runner.AppModuleBasic{}, cosmos.NewAppModuleBasic(servicesdk.ModuleName), - cosmos.NewAppModuleBasic(runnersdk.ModuleName), cosmos.NewAppModuleBasic(executionsdk.ModuleName), cosmos.NewAppModuleBasic(processsdk.ModuleName), ) @@ -117,8 +117,8 @@ type NewApp struct { // Engine's keepers ownershipKeeper ownership.Keeper instanceKeeper instance.Keeper + runnerKeeper runner.Keeper serviceKeeper *servicesdk.Keeper - runnerKeeper *runnersdk.Keeper processKeeper *processsdk.Keeper executionKeeper *executionsdk.Keeper @@ -157,8 +157,8 @@ func NewInitApp( // Engine's module keys ownership.ModuleName, instance.ModuleName, + runner.ModuleName, servicesdk.ModuleName, - runnersdk.ModuleName, executionsdk.ModuleName, processsdk.ModuleName, ) @@ -244,8 +244,8 @@ func NewInitApp( // Engine's module keepers app.ownershipKeeper = ownership.NewKeeper(app.cdc, keys[ownership.StoreKey]) app.instanceKeeper = instance.NewKeeper(app.cdc, keys[instance.StoreKey]) + app.runnerKeeper = runner.NewKeeper(app.cdc, keys[runner.ModuleName], app.instanceKeeper) app.serviceKeeper = servicesdk.NewKeeper(keys[servicesdk.ModuleName], app.ownershipKeeper) - app.runnerKeeper = runnersdk.NewKeeper(keys[runnersdk.ModuleName], app.instanceKeeper) app.processKeeper = processsdk.NewKeeper(keys[processsdk.ModuleName], app.ownershipKeeper, app.instanceKeeper) app.executionKeeper = executionsdk.NewKeeper(keys[executionsdk.ModuleName], app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper) @@ -262,8 +262,8 @@ func NewInitApp( // Engine's modules ownership.NewAppModule(app.ownershipKeeper), instance.NewAppModule(app.instanceKeeper), + runner.NewAppModule(app.runnerKeeper, app.instanceKeeper), servicesdk.NewModule(app.serviceKeeper), - runnersdk.NewModule(app.runnerKeeper), executionsdk.NewModule(app.executionKeeper), processsdk.NewModule(app.processKeeper), @@ -289,8 +289,8 @@ func NewInitApp( // Engine's modules ownership.ModuleName, instance.ModuleName, + runner.ModuleName, servicesdk.ModuleName, - runnersdk.ModuleName, executionsdk.ModuleName, processsdk.ModuleName, diff --git a/sdk/execution/keeper.go b/sdk/execution/keeper.go index feba6d8ee..645877f4f 100644 --- a/sdk/execution/keeper.go +++ b/sdk/execution/keeper.go @@ -11,9 +11,9 @@ import ( "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/protobuf/types" processsdk "github.com/mesg-foundation/engine/sdk/process" - runnersdk "github.com/mesg-foundation/engine/sdk/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" "github.com/mesg-foundation/engine/x/instance" + "github.com/mesg-foundation/engine/x/runner" ) // Keeper holds the logic to read and write data. @@ -21,12 +21,12 @@ type Keeper struct { storeKey *cosmostypes.KVStoreKey serviceKeeper *servicesdk.Keeper instanceKeeper instance.Keeper - runnerKeeper *runnersdk.Keeper + runnerKeeper runner.Keeper processKeeper *processsdk.Keeper } // NewKeeper initialize a new keeper. -func NewKeeper(storeKey *cosmostypes.KVStoreKey, serviceKeeper *servicesdk.Keeper, instanceKeeper instance.Keeper, runnerKeeper *runnersdk.Keeper, processKeeper *processsdk.Keeper) *Keeper { +func NewKeeper(storeKey *cosmostypes.KVStoreKey, serviceKeeper *servicesdk.Keeper, instanceKeeper instance.Keeper, runnerKeeper runner.Keeper, processKeeper *processsdk.Keeper) *Keeper { return &Keeper{ storeKey: storeKey, serviceKeeper: serviceKeeper, diff --git a/sdk/runner/codec.go b/sdk/runner/codec.go deleted file mode 100644 index 2bde90484..000000000 --- a/sdk/runner/codec.go +++ /dev/null @@ -1,8 +0,0 @@ -package runnersdk - -import "github.com/mesg-foundation/engine/codec" - -func init() { - codec.RegisterConcrete(msgCreateRunner{}, "runner/create", nil) - codec.RegisterConcrete(msgDeleteRunner{}, "runner/delete", nil) -} diff --git a/sdk/runner/keeper.go b/sdk/runner/keeper.go deleted file mode 100644 index 21a2d5be3..000000000 --- a/sdk/runner/keeper.go +++ /dev/null @@ -1,94 +0,0 @@ -package runnersdk - -import ( - "errors" - "fmt" - - cosmostypes "github.com/cosmos/cosmos-sdk/types" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/runner" - "github.com/mesg-foundation/engine/x/instance" -) - -// Keeper holds the logic to read and write data. -type Keeper struct { - storeKey *cosmostypes.KVStoreKey - instanceKeeper instance.Keeper -} - -// NewKeeper initialize a new keeper. -func NewKeeper(storeKey *cosmostypes.KVStoreKey, instanceKeeper instance.Keeper) *Keeper { - return &Keeper{ - storeKey: storeKey, - instanceKeeper: instanceKeeper, - } -} - -// Create creates a new runner. -func (k *Keeper) Create(request cosmostypes.Request, msg *msgCreateRunner) (*runner.Runner, error) { - store := request.KVStore(k.storeKey) - inst, err := k.instanceKeeper.FetchOrCreate(request, msg.ServiceHash, msg.EnvHash) - if err != nil { - return nil, err - } - run := &runner.Runner{ - Address: msg.Address.String(), - InstanceHash: inst.Hash, - } - run.Hash = hash.Dump(run) - if store.Has(run.Hash) { - return nil, fmt.Errorf("runner %q already exists", run.Hash) - } - value, err := codec.MarshalBinaryBare(run) - if err != nil { - return nil, err - } - store.Set(run.Hash, value) - return run, nil -} - -// Delete deletes a runner. -func (k *Keeper) Delete(request cosmostypes.Request, msg *msgDeleteRunner) error { - store := request.KVStore(k.storeKey) - run := runner.Runner{} - value := store.Get(msg.RunnerHash) - if err := codec.UnmarshalBinaryBare(value, &run); err != nil { - return err - } - if run.Address != msg.Address.String() { - return errors.New("only the runner owner can remove itself") - } - store.Delete(msg.RunnerHash) - return nil -} - -// Get returns the runner that matches given hash. -func (k *Keeper) Get(request cosmostypes.Request, hash hash.Hash) (*runner.Runner, error) { - store := request.KVStore(k.storeKey) - if !store.Has(hash) { - return nil, fmt.Errorf("runner %q not found", hash) - } - value := store.Get(hash) - var run *runner.Runner - return run, codec.UnmarshalBinaryBare(value, &run) -} - -// List returns all runners. -func (k *Keeper) List(request cosmostypes.Request) ([]*runner.Runner, error) { - var ( - runners []*runner.Runner - iter = request.KVStore(k.storeKey).Iterator(nil, nil) - ) - for iter.Valid() { - var run *runner.Runner - value := iter.Value() - if err := codec.UnmarshalBinaryBare(value, &run); err != nil { - return nil, err - } - runners = append(runners, run) - iter.Next() - } - iter.Close() - return runners, nil -} diff --git a/sdk/runner/module.go b/sdk/runner/module.go deleted file mode 100644 index d4ba60573..000000000 --- a/sdk/runner/module.go +++ /dev/null @@ -1,57 +0,0 @@ -package runnersdk - -import ( - "fmt" - - cosmostypes "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/cosmos-sdk/types/module" - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/hash" - abci "github.com/tendermint/tendermint/abci/types" -) - -// ModuleName is the name of this module. -const ModuleName = "runner" - -// NewModule returns the module of this sdk. -func NewModule(k *Keeper) module.AppModule { - return cosmos.NewAppModule(cosmos.NewAppModuleBasic(ModuleName), handler(k), querier(k)) -} - -func handler(k *Keeper) cosmos.Handler { - return func(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { - switch msg := msg.(type) { - case msgCreateRunner: - run, err := k.Create(request, &msg) - if err != nil { - return nil, cosmos.NewMesgWrapError(cosmos.CodeInternal, err) - } - return run.Hash, nil - case msgDeleteRunner: - if err := k.Delete(request, &msg); err != nil { - return nil, cosmos.NewMesgWrapError(cosmos.CodeInternal, err) - } - return nil, nil - default: - return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, fmt.Sprintf("Unrecognized runner Msg type: %v", msg.Type())) - } - } -} - -func querier(k *Keeper) cosmos.Querier { - return func(request cosmostypes.Request, path []string, req abci.RequestQuery) (res interface{}, err error) { - switch path[0] { - case "get": - hash, err := hash.Decode(path[1]) - if err != nil { - return nil, err - } - return k.Get(request, hash) - case "list": - return k.List(request) - default: - return nil, fmt.Errorf("unknown runner query endpoint %s", path[0]) - } - } -} diff --git a/sdk/runner/msgs.go b/sdk/runner/msgs.go deleted file mode 100644 index 4eca8f97c..000000000 --- a/sdk/runner/msgs.go +++ /dev/null @@ -1,111 +0,0 @@ -package runnersdk - -import ( - cosmostypes "github.com/cosmos/cosmos-sdk/types" - cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/ext/xvalidator" - "github.com/mesg-foundation/engine/hash" -) - -// msgCreateRunner defines a state transition to create a runner. -type msgCreateRunner struct { - Address cosmostypes.AccAddress `json:"address" validate:"required,accaddress"` - ServiceHash hash.Hash `json:"serviceHash" validate:"required,hash"` - EnvHash hash.Hash `json:"envHash" validate:"omitempty,hash"` -} - -// newMsgCreateRunner is a constructor function for msgCreateRunner. -func newMsgCreateRunner(address cosmostypes.AccAddress, serviceHash hash.Hash, envHash hash.Hash) *msgCreateRunner { - return &msgCreateRunner{ - Address: address, - ServiceHash: serviceHash, - EnvHash: envHash, - } -} - -// Route should return the name of the module. -func (msg msgCreateRunner) Route() string { - return ModuleName -} - -// Type returns the action. -func (msg msgCreateRunner) Type() string { - return "create_runner" -} - -// ValidateBasic runs stateless checks on the message. -func (msg msgCreateRunner) ValidateBasic() error { - if err := xvalidator.Validate.Struct(msg); err != nil { - return err - } - if msg.ServiceHash.IsZero() { - return cosmoserrors.Wrap(cosmos.ErrValidation, "serviceHash is missing") - } - if msg.EnvHash.IsZero() { - return cosmoserrors.Wrap(cosmos.ErrValidation, "envHash is missing") - } - if msg.Address.Empty() { - return cosmoserrors.Wrap(cosmoserrors.ErrInvalidAddress, "address is missing") - } - return nil -} - -// GetSignBytes encodes the message for signing. -func (msg msgCreateRunner) GetSignBytes() []byte { - return cosmostypes.MustSortJSON(codec.MustMarshalJSON(msg)) -} - -// GetSigners defines whose signature is required. -func (msg msgCreateRunner) GetSigners() []cosmostypes.AccAddress { - return []cosmostypes.AccAddress{msg.Address} -} - -// msgDeleteRunner defines a state transition to delete a runner. -type msgDeleteRunner struct { - Address cosmostypes.AccAddress `json:"address" validate:"required,accaddress"` - RunnerHash hash.Hash `json:"runnerHash" validate:"required,hash"` -} - -// newMsgDeleteRunner is a constructor function for msgDeleteRunner. -func newMsgDeleteRunner(address cosmostypes.AccAddress, runnerHash hash.Hash) *msgDeleteRunner { - return &msgDeleteRunner{ - Address: address, - RunnerHash: runnerHash, - } -} - -// Route should return the name of the module. -func (msg msgDeleteRunner) Route() string { - return ModuleName -} - -// Type returns the action. -func (msg msgDeleteRunner) Type() string { - return "delete_runner" -} - -// ValidateBasic runs stateless checks on the message. -func (msg msgDeleteRunner) ValidateBasic() error { - if err := xvalidator.Validate.Struct(msg); err != nil { - return err - } - if msg.RunnerHash.IsZero() { - return cosmoserrors.Wrap(cosmos.ErrValidation, "runnerHash is missing") - } - if msg.Address.Empty() { - return cosmoserrors.Wrap(cosmoserrors.ErrInvalidAddress, "address is missing") - } - return nil -} - -// GetSignBytes encodes the message for signing. -func (msg msgDeleteRunner) GetSignBytes() []byte { - return cosmostypes.MustSortJSON(codec.MustMarshalJSON(msg)) -} - -// GetSigners defines whose signature is required. -func (msg msgDeleteRunner) GetSigners() []cosmostypes.AccAddress { - return []cosmostypes.AccAddress{msg.Address} -} diff --git a/sdk/runner/sdk.go b/sdk/runner/sdk.go index fe301699b..e71fb1816 100644 --- a/sdk/runner/sdk.go +++ b/sdk/runner/sdk.go @@ -10,9 +10,10 @@ import ( "github.com/mesg-foundation/engine/hash" instancepb "github.com/mesg-foundation/engine/instance" "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/runner" + runnerpb "github.com/mesg-foundation/engine/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" "github.com/mesg-foundation/engine/x/instance" + "github.com/mesg-foundation/engine/x/runner" ) // SDK is the runner sdk. @@ -45,7 +46,7 @@ func New(client *cosmos.Client, serviceSDK *servicesdk.SDK, container container. } // Create creates a new runner. -func (s *SDK) Create(req *api.CreateRunnerRequest) (*runner.Runner, error) { +func (s *SDK) Create(req *api.CreateRunnerRequest) (*runnerpb.Runner, error) { // calculate instance's hash. // TODO: this should be merged with the same logic currently in instance sdk srv, err := s.serviceSDK.Get(req.ServiceHash) @@ -63,7 +64,7 @@ func (s *SDK) Create(req *api.CreateRunnerRequest) (*runner.Runner, error) { if err != nil { return nil, err } - expRunnerHash := hash.Dump(&runner.Runner{ + expRunnerHash := hash.Dump(&runnerpb.Runner{ Address: acc.GetAddress().String(), InstanceHash: instanceHash, }) @@ -85,7 +86,7 @@ func (s *SDK) Create(req *api.CreateRunnerRequest) (*runner.Runner, error) { stop(s.container, expRunnerHash, srv.Dependencies) } - msg := newMsgCreateRunner(acc.GetAddress(), req.ServiceHash, envHash) + msg := runner.NewMsgCreateRunner(acc.GetAddress(), req.ServiceHash, envHash) tx, err := s.client.BuildAndBroadcastMsg(msg) if err != nil { onError() @@ -106,7 +107,7 @@ func (s *SDK) Create(req *api.CreateRunnerRequest) (*runner.Runner, error) { // Delete deletes an existing runner. func (s *SDK) Delete(req *api.DeleteRunnerRequest) error { // get runner before deleting it - runner, err := s.Get(req.Hash) + r, err := s.Get(req.Hash) if err != nil { return err } @@ -114,14 +115,14 @@ func (s *SDK) Delete(req *api.DeleteRunnerRequest) error { if err != nil { return err } - msg := newMsgDeleteRunner(acc.GetAddress(), req.Hash) + msg := runner.NewMsgDeleteRunner(acc.GetAddress(), req.Hash) _, err = s.client.BuildAndBroadcastMsg(msg) if err != nil { return err } var inst instancepb.Instance - if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", instance.QuerierRoute, instance.QueryGetInstance, runner.InstanceHash), nil, &inst); err != nil { + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", instance.QuerierRoute, instance.QueryGetInstance, r.InstanceHash), nil, &inst); err != nil { return err } @@ -131,7 +132,7 @@ func (s *SDK) Delete(req *api.DeleteRunnerRequest) error { } // stop the local running service - if err := stop(s.container, runner.Hash, srv.Dependencies); err != nil { + if err := stop(s.container, r.Hash, srv.Dependencies); err != nil { return err } @@ -146,18 +147,18 @@ func (s *SDK) Delete(req *api.DeleteRunnerRequest) error { } // Get returns the runner that matches given hash. -func (s *SDK) Get(hash hash.Hash) (*runner.Runner, error) { - var runner runner.Runner - if err := s.client.Query("custom/"+ModuleName+"/get/"+hash.String(), nil, &runner); err != nil { +func (s *SDK) Get(hash hash.Hash) (*runnerpb.Runner, error) { + var r runnerpb.Runner + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", runner.ModuleName, runner.QueryGetRunner, hash), nil, &r); err != nil { return nil, err } - return &runner, nil + return &r, nil } // List returns all runners. -func (s *SDK) List(f *Filter) ([]*runner.Runner, error) { - var runners []*runner.Runner - if err := s.client.Query("custom/"+ModuleName+"/list", nil, &runners); err != nil { +func (s *SDK) List(f *Filter) ([]*runnerpb.Runner, error) { + var runners []*runnerpb.Runner + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s", runner.ModuleName, runner.QueryListRunners), nil, &runners); err != nil { return nil, err } // no filter, returns @@ -165,11 +166,11 @@ func (s *SDK) List(f *Filter) ([]*runner.Runner, error) { return runners, nil } // filter results - ret := make([]*runner.Runner, 0) - for _, runner := range runners { - if (f.Address == "" || runner.Address == f.Address) && - (f.InstanceHash.IsZero() || runner.InstanceHash.Equal(f.InstanceHash)) { - ret = append(ret, runner) + ret := make([]*runnerpb.Runner, 0) + for _, r := range runners { + if (f.Address == "" || r.Address == f.Address) && + (f.InstanceHash.IsZero() || r.InstanceHash.Equal(f.InstanceHash)) { + ret = append(ret, r) } } return ret, nil diff --git a/x/runner/abci.go b/x/runner/abci.go new file mode 100644 index 000000000..8d5ad54dd --- /dev/null +++ b/x/runner/abci.go @@ -0,0 +1,13 @@ +package runner + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// BeginBlocker check for infraction evidence or downtime of validators +// on every begin block +func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k Keeper) {} + +// EndBlocker called every block, process inflation, update validator set. +func EndBlocker(ctx sdk.Context, k Keeper) {} diff --git a/x/runner/alias.go b/x/runner/alias.go new file mode 100644 index 000000000..7f9a24901 --- /dev/null +++ b/x/runner/alias.go @@ -0,0 +1,43 @@ +package runner + +import ( + "github.com/mesg-foundation/engine/x/runner/internal/keeper" + "github.com/mesg-foundation/engine/x/runner/internal/types" +) + +// const aliases +const ( + ModuleName = types.ModuleName + RouterKey = types.RouterKey + StoreKey = types.StoreKey + DefaultParamspace = types.DefaultParamspace + QuerierRoute = types.QuerierRoute +) + +// functions and variable aliases +var ( + NewKeeper = keeper.NewKeeper + NewQuerier = keeper.NewQuerier + RegisterCodec = types.RegisterCodec + NewGenesisState = types.NewGenesisState + DefaultGenesisState = types.DefaultGenesisState + ValidateGenesis = types.ValidateGenesis + + ModuleCdc = types.ModuleCdc + + QueryGetRunner = types.QueryGetRunner + QueryListRunners = types.QueryListRunners + + NewMsgCreateRunner = types.NewMsgCreateRunner + NewMsgDeleteRunner = types.NewMsgDeleteRunner +) + +// module types +type ( + Keeper = keeper.Keeper + GenesisState = types.GenesisState + Params = types.Params + + MsgCreateRunner = types.MsgCreateRunner + MsgDeleteRunner = types.MsgDeleteRunner +) diff --git a/x/runner/client/cli/query.go b/x/runner/client/cli/query.go new file mode 100644 index 000000000..55051811e --- /dev/null +++ b/x/runner/client/cli/query.go @@ -0,0 +1,73 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/mesg-foundation/engine/runner" + "github.com/mesg-foundation/engine/x/runner/internal/types" + "github.com/spf13/cobra" +) + +// GetQueryCmd returns the cli query commands for this module +func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { + // Group runner queries under a subcommand + runnerQueryCmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + runnerQueryCmd.AddCommand( + flags.GetCommands( + GetCmdGetRunner(queryRoute, cdc), + GetCmdListRunners(queryRoute, cdc), + )..., + ) + + return runnerQueryCmd +} + +func GetCmdGetRunner(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "get", + Short: "get", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", queryRoute, types.QueryGetRunner, args[0]), nil) + if err != nil { + fmt.Printf("could not get runner\n%s\n", err.Error()) + return nil + } + + var out *runner.Runner + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} + +func GetCmdListRunners(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "list", + Short: "list", + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryListRunners), nil) + if err != nil { + fmt.Printf("could not list runners\n%s\n", err.Error()) + return nil + } + + var out []*runner.Runner + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} diff --git a/x/runner/client/cli/tx.go b/x/runner/client/cli/tx.go new file mode 100644 index 000000000..812bbd521 --- /dev/null +++ b/x/runner/client/cli/tx.go @@ -0,0 +1,53 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/mesg-foundation/engine/x/runner/internal/types" + "github.com/spf13/cobra" +) + +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + runnerTxCmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + runnerTxCmd.AddCommand(flags.PostCommands( + GetCmdCreateRunner(cdc), + GetCmdDeleteRunner(cdc), + )...) + + return runnerTxCmd +} + +func GetCmdCreateRunner(cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "createRunner [serviceHash] [key=val]...", + Short: "Creates a new runner", + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + // TODO: + return nil + }, + } +} + +func GetCmdDeleteRunner(cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "createRunner [serviceHash] [key=val]...", + Short: "Creates a new runner", + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + // TODO: + return nil + }, + } +} diff --git a/x/runner/client/rest/query.go b/x/runner/client/rest/query.go new file mode 100644 index 000000000..3f48580f0 --- /dev/null +++ b/x/runner/client/rest/query.go @@ -0,0 +1,89 @@ +package rest + +import ( + "fmt" + "net/http" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/types/rest" + "github.com/gorilla/mux" + "github.com/mesg-foundation/engine/x/runner/internal/types" +) + +func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { + r.HandleFunc( + "/runner/get/{hash}", + queryGetHandlerFn(cliCtx), + ).Methods(http.MethodGet) + r.HandleFunc( + "/runner/list", + queryListHandlerFn(cliCtx), + ).Methods(http.MethodGet) + + r.HandleFunc( + "/runner/parameters", + queryParamsHandlerFn(cliCtx), + ).Methods("GET") +} + +func queryGetHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryGetRunner, vars["hash"]) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + +func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryListRunners) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + +func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/parameters", types.QuerierRoute) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} diff --git a/x/runner/client/rest/rest.go b/x/runner/client/rest/rest.go new file mode 100644 index 000000000..2688f7265 --- /dev/null +++ b/x/runner/client/rest/rest.go @@ -0,0 +1,13 @@ +package rest + +import ( + "github.com/gorilla/mux" + + "github.com/cosmos/cosmos-sdk/client/context" +) + +// RegisterRoutes registers runner-related REST handlers to a router +func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) { + registerQueryRoutes(cliCtx, r) + registerTxRoutes(cliCtx, r) +} diff --git a/x/runner/client/rest/tx.go b/x/runner/client/rest/tx.go new file mode 100644 index 000000000..048e7d9e9 --- /dev/null +++ b/x/runner/client/rest/tx.go @@ -0,0 +1,10 @@ +package rest + +import ( + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/gorilla/mux" +) + +func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) { + // TODO: +} diff --git a/x/runner/genesis.go b/x/runner/genesis.go new file mode 100644 index 000000000..9841bdd34 --- /dev/null +++ b/x/runner/genesis.go @@ -0,0 +1,18 @@ +package runner + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/mesg-foundation/engine/x/runner/internal/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// InitGenesis initialize default parameters and the keeper's address to pubkey map. +func InitGenesis(ctx sdk.Context, k Keeper, data types.GenesisState) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} + +// ExportGenesis writes the current store values // to a genesis file, +// which can be imported again with InitGenesis. +func ExportGenesis(ctx sdk.Context, k Keeper) types.GenesisState { + return types.NewGenesisState() +} diff --git a/x/runner/handler.go b/x/runner/handler.go new file mode 100644 index 000000000..9810142cb --- /dev/null +++ b/x/runner/handler.go @@ -0,0 +1,64 @@ +package runner + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/x/runner/internal/types" +) + +// NewHandler creates an sdk.Handler for all the runner type messages +func NewHandler(k Keeper) sdk.Handler { + return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + ctx = ctx.WithEventManager(sdk.NewEventManager()) + switch msg := msg.(type) { + case MsgCreateRunner: + return handleMsgCreateRunner(ctx, k, &msg) + case MsgDeleteRunner: + return handleMsgDeleteRunner(ctx, k, &msg) + default: + errMsg := fmt.Sprintf("unrecognized %s message type: %T", ModuleName, msg) + return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg) + } + } +} + +// handleMsgCreateRunner creates a new runner. +func handleMsgCreateRunner(ctx sdk.Context, k Keeper, msg *MsgCreateRunner) (*sdk.Result, error) { + runner, err := k.Create(ctx, msg) + if err != nil { + return nil, err + } + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeyAction, types.EventTypeCreateRunner), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Address.String()), + sdk.NewAttribute(types.AttributeHash, runner.Hash.String()), + ), + ) + + return &sdk.Result{Events: ctx.EventManager().Events()}, nil +} + +// handleMsgDeleteRunner deletes a runner. +func handleMsgDeleteRunner(ctx sdk.Context, k Keeper, msg *MsgDeleteRunner) (*sdk.Result, error) { + if err := k.Delete(ctx, msg); err != nil { + return nil, err + } + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeyAction, types.EventTypeDeleteRunner), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Address.String()), + sdk.NewAttribute(types.AttributeHash, msg.RunnerHash.String()), + ), + ) + + return &sdk.Result{Events: ctx.EventManager().Events()}, nil +} diff --git a/x/runner/internal/keeper/keeper.go b/x/runner/internal/keeper/keeper.go new file mode 100644 index 000000000..2dac6fbfa --- /dev/null +++ b/x/runner/internal/keeper/keeper.go @@ -0,0 +1,108 @@ +package keeper + +import ( + "errors" + "fmt" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/runner" + "github.com/mesg-foundation/engine/x/runner/internal/types" + "github.com/tendermint/tendermint/libs/log" +) + +// Keeper of the runner store +type Keeper struct { + storeKey sdk.StoreKey + cdc *codec.Codec + instanceKeeper types.InstaceKeeper +} + +// NewKeeper creates a runner keeper +func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, instanceKeeper types.InstaceKeeper) Keeper { + keeper := Keeper{ + storeKey: key, + cdc: cdc, + instanceKeeper: instanceKeeper, + } + return keeper +} + +// Logger returns a module-specific logger. +func (k Keeper) Logger(ctx sdk.Context) log.Logger { + return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) +} + +// Create creates a new runner. +func (k *Keeper) Create(ctx sdk.Context, msg *types.MsgCreateRunner) (*runner.Runner, error) { + store := ctx.KVStore(k.storeKey) + inst, err := k.instanceKeeper.FetchOrCreate(ctx, msg.ServiceHash, msg.EnvHash) + if err != nil { + return nil, err + } + + r := &runner.Runner{ + Address: msg.Address.String(), + InstanceHash: inst.Hash, + } + r.Hash = hash.Dump(r) + if store.Has(r.Hash) { + return nil, fmt.Errorf("runner %q already exists", r.Hash) + } + + value, err := k.cdc.MarshalBinaryLengthPrefixed(r) + if err != nil { + return nil, err + } + store.Set(r.Hash, value) + return r, nil +} + +// Delete deletes a runner. +func (k *Keeper) Delete(ctx sdk.Context, msg *types.MsgDeleteRunner) error { + store := ctx.KVStore(k.storeKey) + if !store.Has(msg.RunnerHash) { + return fmt.Errorf("runner %q not found", msg.RunnerHash) + } + + value := store.Get(msg.RunnerHash) + var r *runner.Runner + if err := k.cdc.UnmarshalBinaryLengthPrefixed(value, &r); err != nil { + return fmt.Errorf("unmarshal error: %w", err) + } + if r.Address != msg.Address.String() { + return errors.New("only the runner owner can remove itself") + } + store.Delete(msg.RunnerHash) + return nil +} + +// Get returns the runner that matches given hash. +func (k *Keeper) Get(ctx sdk.Context, hash hash.Hash) (*runner.Runner, error) { + store := ctx.KVStore(k.storeKey) + if !store.Has(hash) { + return nil, fmt.Errorf("runner %q not found", hash) + } + value := store.Get(hash) + var r *runner.Runner + return r, k.cdc.UnmarshalBinaryLengthPrefixed(value, &r) +} + +// List returns all runners. +func (k *Keeper) List(ctx sdk.Context) ([]*runner.Runner, error) { + var ( + runners []*runner.Runner + iter = ctx.KVStore(k.storeKey).Iterator(nil, nil) + ) + for iter.Valid() { + var r *runner.Runner + if err := k.cdc.UnmarshalBinaryLengthPrefixed(iter.Value(), &r); err != nil { + return nil, err + } + runners = append(runners, r) + iter.Next() + } + iter.Close() + return runners, nil +} diff --git a/x/runner/internal/keeper/querier.go b/x/runner/internal/keeper/querier.go new file mode 100644 index 000000000..430b7d33e --- /dev/null +++ b/x/runner/internal/keeper/querier.go @@ -0,0 +1,54 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/x/runner/internal/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// NewQuerier creates a new querier for instance clients. +func NewQuerier(k Keeper) sdk.Querier { + return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) { + switch path[0] { + case types.QueryGetRunner: + return getRunner(ctx, path[1:], k) + case types.QueryListRunners: + return listRunner(ctx, k) + default: + return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown runner query endpoint") + } + } +} + +func getRunner(ctx sdk.Context, path []string, k Keeper) ([]byte, error) { + hash, err := hash.Decode(path[0]) + if err != nil { + return nil, err + } + + instance, err := k.Get(ctx, hash) + if err != nil { + return nil, err + } + + res, err := types.ModuleCdc.MarshalJSON(instance) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return res, nil +} + +func listRunner(ctx sdk.Context, k Keeper) ([]byte, error) { + instances, err := k.List(ctx) + if err != nil { + return nil, err + } + + res, err := types.ModuleCdc.MarshalJSON(instances) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return res, nil +} diff --git a/x/runner/internal/types/codec.go b/x/runner/internal/types/codec.go new file mode 100644 index 000000000..ad4d320ff --- /dev/null +++ b/x/runner/internal/types/codec.go @@ -0,0 +1,21 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" +) + +// RegisterCodec registers concrete types on codec +func RegisterCodec(cdc *codec.Codec) { + cdc.RegisterConcrete(MsgCreateRunner{}, "runner/CreateRunner", nil) + cdc.RegisterConcrete(MsgDeleteRunner{}, "runner/DeleteRunner", nil) +} + +// ModuleCdc defines the module codec +var ModuleCdc *codec.Codec + +func init() { + ModuleCdc = codec.New() + RegisterCodec(ModuleCdc) + codec.RegisterCrypto(ModuleCdc) + ModuleCdc.Seal() +} diff --git a/x/runner/internal/types/events.go b/x/runner/internal/types/events.go new file mode 100644 index 000000000..0795732ff --- /dev/null +++ b/x/runner/internal/types/events.go @@ -0,0 +1,11 @@ +package types + +// module event types +const ( + EventTypeCreateRunner = "CreateRunner" + EventTypeDeleteRunner = "DeleteRunner" + + AttributeHash = "hash" + + AttributeValueCategory = ModuleName +) diff --git a/x/runner/internal/types/expected_keepers.go b/x/runner/internal/types/expected_keepers.go new file mode 100644 index 000000000..8fc28fa3c --- /dev/null +++ b/x/runner/internal/types/expected_keepers.go @@ -0,0 +1,21 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/params" + "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/instance" +) + +// ParamSubspace defines the expected Subspace interfacace +type ParamSubspace interface { + WithKeyTable(table params.KeyTable) params.Subspace + Get(ctx sdk.Context, key []byte, ptr interface{}) + GetParamSet(ctx sdk.Context, ps params.ParamSet) + SetParamSet(ctx sdk.Context, ps params.ParamSet) +} + +// InstanceKeeper module interface. +type InstaceKeeper interface { + FetchOrCreate(ctx sdk.Context, serviceHash hash.Hash, envHash hash.Hash) (*instance.Instance, error) +} diff --git a/x/runner/internal/types/genesis.go b/x/runner/internal/types/genesis.go new file mode 100644 index 000000000..bebeec0dc --- /dev/null +++ b/x/runner/internal/types/genesis.go @@ -0,0 +1,19 @@ +package types + +// GenesisState - all instance state that must be provided at genesis +type GenesisState struct{} + +// NewGenesisState creates a new GenesisState object +func NewGenesisState() GenesisState { + return GenesisState{} +} + +// DefaultGenesisState - default GenesisState used by Cosmos Hub +func DefaultGenesisState() GenesisState { + return GenesisState{} +} + +// ValidateGenesis validates the instance genesis parameters +func ValidateGenesis(data GenesisState) error { + return nil +} diff --git a/x/runner/internal/types/key.go b/x/runner/internal/types/key.go new file mode 100644 index 000000000..b3d0166d4 --- /dev/null +++ b/x/runner/internal/types/key.go @@ -0,0 +1,15 @@ +package types + +const ( + // ModuleName is the name of the module + ModuleName = "runner" + + // StoreKey to be used when creating the KVStore + StoreKey = ModuleName + + // RouterKey to be used for routing msgs + RouterKey = ModuleName + + // QuerierRoute to be used for routing + QuerierRoute = ModuleName +) diff --git a/x/runner/internal/types/msg.go b/x/runner/internal/types/msg.go new file mode 100644 index 000000000..58aa081e1 --- /dev/null +++ b/x/runner/internal/types/msg.go @@ -0,0 +1,110 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/cosmos" + "github.com/mesg-foundation/engine/ext/xvalidator" + "github.com/mesg-foundation/engine/hash" +) + +// MsgCreateRunner defines a state transition to create a runner. +type MsgCreateRunner struct { + Address sdk.AccAddress `json:"address" validate:"required,accaddress"` + ServiceHash hash.Hash `json:"serviceHash" validate:"required,hash"` + EnvHash hash.Hash `json:"envHash" validate:"omitempty,hash"` +} + +// NewMsgCreateRunner is a constructor function for MsgCreateRunner. +func NewMsgCreateRunner(address sdk.AccAddress, serviceHash hash.Hash, envHash hash.Hash) *MsgCreateRunner { + return &MsgCreateRunner{ + Address: address, + ServiceHash: serviceHash, + EnvHash: envHash, + } +} + +// Route should return the name of the module route. +func (msg MsgCreateRunner) Route() string { + return RouterKey +} + +// Type returns the action. +func (msg MsgCreateRunner) Type() string { + return "CreateRunner" +} + +// ValidateBasic runs stateless checks on the message. +func (msg MsgCreateRunner) ValidateBasic() error { + if err := xvalidator.Validate.Struct(msg); err != nil { + return err + } + if msg.ServiceHash.IsZero() { + return sdkerrors.Wrap(cosmos.ErrValidation, "serviceHash is missing") + } + if msg.EnvHash.IsZero() { + return sdkerrors.Wrap(cosmos.ErrValidation, "envHash is missing") + } + if msg.Address.Empty() { + return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "address is missing") + } + return nil +} + +// GetSignBytes encodes the message for signing. +func (msg MsgCreateRunner) GetSignBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg)) +} + +// GetSigners defines whose signature is required. +func (msg MsgCreateRunner) GetSigners() []sdk.AccAddress { + return []sdk.AccAddress{msg.Address} +} + +// MsgDeleteRunner defines a state transition to delete a runner. +type MsgDeleteRunner struct { + Address sdk.AccAddress `json:"address" validate:"required,accaddress"` + RunnerHash hash.Hash `json:"runnerHash" validate:"required,hash"` +} + +// NewMsgDeleteRunner is a constructor function for MsgDeleteRunner. +func NewMsgDeleteRunner(address sdk.AccAddress, runnerHash hash.Hash) *MsgDeleteRunner { + return &MsgDeleteRunner{ + Address: address, + RunnerHash: runnerHash, + } +} + +// Route should return the name of the module. +func (msg MsgDeleteRunner) Route() string { + return ModuleName +} + +// Type returns the action. +func (msg MsgDeleteRunner) Type() string { + return "DeleteRunner" +} + +// ValidateBasic runs stateless checks on the message. +func (msg MsgDeleteRunner) ValidateBasic() error { + if err := xvalidator.Validate.Struct(msg); err != nil { + return sdkerrors.Wrap(cosmos.ErrValidation, err.Error()) + } + if msg.RunnerHash.IsZero() { + return sdkerrors.Wrap(cosmos.ErrValidation, "runnerHash is missing") + } + if msg.Address.Empty() { + return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "address is missing") + } + return nil +} + +// GetSignBytes encodes the message for signing. +func (msg MsgDeleteRunner) GetSignBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg)) +} + +// GetSigners defines whose signature is required. +func (msg MsgDeleteRunner) GetSigners() []sdk.AccAddress { + return []sdk.AccAddress{msg.Address} +} diff --git a/x/runner/internal/types/params.go b/x/runner/internal/types/params.go new file mode 100644 index 000000000..4940992b5 --- /dev/null +++ b/x/runner/internal/types/params.go @@ -0,0 +1,38 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/x/params" +) + +// Default parameter namespace +const ( + DefaultParamspace = ModuleName +) + +// ParamKeyTable for instance module +func ParamKeyTable() params.KeyTable { + return params.NewKeyTable().RegisterParamSet(&Params{}) +} + +// Params - used for initializing default parameter for instance at genesis +type Params struct{} + +// NewParams creates a new Params object +func NewParams() Params { + return Params{} +} + +// String implements the stringer interface for Params +func (p Params) String() string { + return "" +} + +// ParamSetPairs - Implements params.ParamSet +func (p *Params) ParamSetPairs() params.ParamSetPairs { + return params.ParamSetPairs{} +} + +// DefaultParams defines the parameters for this module +func DefaultParams() Params { + return NewParams() +} diff --git a/x/runner/internal/types/querier.go b/x/runner/internal/types/querier.go new file mode 100644 index 000000000..687e9326c --- /dev/null +++ b/x/runner/internal/types/querier.go @@ -0,0 +1,7 @@ +package types + +// Query endpoints supported by the runner querier +const ( + QueryGetRunner = "get" + QueryListRunners = "list" +) diff --git a/x/runner/module.go b/x/runner/module.go new file mode 100644 index 000000000..f75326f11 --- /dev/null +++ b/x/runner/module.go @@ -0,0 +1,142 @@ +package runner + +import ( + "encoding/json" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/gorilla/mux" + "github.com/mesg-foundation/engine/x/instance" + "github.com/mesg-foundation/engine/x/runner/client/cli" + "github.com/mesg-foundation/engine/x/runner/client/rest" + "github.com/mesg-foundation/engine/x/runner/internal/types" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" +) + +var ( + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} +) + +// AppModuleBasic defines the basic application module used by the runner module. +type AppModuleBasic struct{} + +var _ module.AppModuleBasic = AppModuleBasic{} + +// Name returns the runner module's name. +func (AppModuleBasic) Name() string { + return types.ModuleName +} + +// RegisterCodec registers the runner module's types for the given codec. +func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { + RegisterCodec(cdc) +} + +// DefaultGenesis returns default genesis state as raw bytes for the runner +// module. +func (AppModuleBasic) DefaultGenesis() json.RawMessage { + return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) +} + +// ValidateGenesis performs genesis state validation for the runner module. +func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { + var data GenesisState + err := ModuleCdc.UnmarshalJSON(bz, &data) + if err != nil { + return err + } + return ValidateGenesis(data) +} + +// RegisterRESTRoutes registers the REST routes for the runner module. +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { + rest.RegisterRoutes(ctx, rtr) +} + +// GetTxCmd returns the root tx command for the runner module. +func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { + return cli.GetTxCmd(cdc) +} + +// GetQueryCmd returns no root query command for the runner module. +func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { + return cli.GetQueryCmd(StoreKey, cdc) +} + +//____________________________________________________________________________ + +// AppModule implements an application module for the runner module. +type AppModule struct { + AppModuleBasic + + keeper Keeper + instanceKeeper instance.Keeper +} + +// NewAppModule creates a new AppModule object +func NewAppModule(k Keeper, instanceKeeper instance.Keeper) AppModule { + return AppModule{ + AppModuleBasic: AppModuleBasic{}, + keeper: k, + instanceKeeper: instanceKeeper, + } +} + +// Name returns the runner module's name. +func (AppModule) Name() string { + return ModuleName +} + +// RegisterInvariants registers the runner module invariants. +func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} + +// Route returns the message routing key for the runner module. +func (AppModule) Route() string { + return RouterKey +} + +// NewHandler returns an sdk.Handler for the runner module. +func (am AppModule) NewHandler() sdk.Handler { + return NewHandler(am.keeper) +} + +// QuerierRoute returns the runner module's querier route name. +func (AppModule) QuerierRoute() string { + return QuerierRoute +} + +// NewQuerierHandler returns the runner module sdk.Querier. +func (am AppModule) NewQuerierHandler() sdk.Querier { + return NewQuerier(am.keeper) +} + +// InitGenesis performs genesis initialization for the runner module. It returns +// no validator updates. +func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { + var genesisState GenesisState + ModuleCdc.MustUnmarshalJSON(data, &genesisState) + InitGenesis(ctx, am.keeper, genesisState) + return []abci.ValidatorUpdate{} +} + +// ExportGenesis returns the exported genesis state as raw bytes for the runner +// module. +func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { + gs := ExportGenesis(ctx, am.keeper) + return ModuleCdc.MustMarshalJSON(gs) +} + +// BeginBlock returns the begin blocker for the runner module. +func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { + BeginBlocker(ctx, req, am.keeper) +} + +// EndBlock returns the end blocker for the runner module. It returns no validator +// updates. +func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} From 8a64b590ef5d065c490f66928e894c36b306b5da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Mah=C3=A9?= Date: Thu, 13 Feb 2020 17:12:33 +0700 Subject: [PATCH 32/87] Fix runner handler returned data --- x/runner/handler.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/x/runner/handler.go b/x/runner/handler.go index 9810142cb..25b322e83 100644 --- a/x/runner/handler.go +++ b/x/runner/handler.go @@ -41,7 +41,10 @@ func handleMsgCreateRunner(ctx sdk.Context, k Keeper, msg *MsgCreateRunner) (*sd ), ) - return &sdk.Result{Events: ctx.EventManager().Events()}, nil + return &sdk.Result{ + Data: runner.Hash, + Events: ctx.EventManager().Events(), + }, nil } // handleMsgDeleteRunner deletes a runner. From 9055e3d9709058a98925d4544416063d9648abcc Mon Sep 17 00:00:00 2001 From: krhubert Date: Thu, 13 Feb 2020 11:25:37 +0100 Subject: [PATCH 33/87] Fixes --- app/app.go | 2 +- sdk/runner/sdk.go | 4 ++-- x/runner/internal/keeper/keeper.go | 4 ++-- x/runner/internal/types/expected_keepers.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/app.go b/app/app.go index c73b4a5c9..131812d67 100644 --- a/app/app.go +++ b/app/app.go @@ -244,7 +244,7 @@ func NewInitApp( // Engine's module keepers app.ownershipKeeper = ownership.NewKeeper(app.cdc, keys[ownership.StoreKey]) app.instanceKeeper = instance.NewKeeper(app.cdc, keys[instance.StoreKey]) - app.runnerKeeper = runner.NewKeeper(app.cdc, keys[runner.ModuleName], app.instanceKeeper) + app.runnerKeeper = runner.NewKeeper(app.cdc, keys[runner.StoreKey], app.instanceKeeper) app.serviceKeeper = servicesdk.NewKeeper(keys[servicesdk.ModuleName], app.ownershipKeeper) app.processKeeper = processsdk.NewKeeper(keys[processsdk.ModuleName], app.ownershipKeeper, app.instanceKeeper) app.executionKeeper = executionsdk.NewKeeper(keys[executionsdk.ModuleName], app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper) diff --git a/sdk/runner/sdk.go b/sdk/runner/sdk.go index e71fb1816..2dc897fd0 100644 --- a/sdk/runner/sdk.go +++ b/sdk/runner/sdk.go @@ -149,7 +149,7 @@ func (s *SDK) Delete(req *api.DeleteRunnerRequest) error { // Get returns the runner that matches given hash. func (s *SDK) Get(hash hash.Hash) (*runnerpb.Runner, error) { var r runnerpb.Runner - if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", runner.ModuleName, runner.QueryGetRunner, hash), nil, &r); err != nil { + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", runner.QuerierRoute, runner.QueryGetRunner, hash), nil, &r); err != nil { return nil, err } return &r, nil @@ -158,7 +158,7 @@ func (s *SDK) Get(hash hash.Hash) (*runnerpb.Runner, error) { // List returns all runners. func (s *SDK) List(f *Filter) ([]*runnerpb.Runner, error) { var runners []*runnerpb.Runner - if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s", runner.ModuleName, runner.QueryListRunners), nil, &runners); err != nil { + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s", runner.QuerierRoute, runner.QueryListRunners), nil, &runners); err != nil { return nil, err } // no filter, returns diff --git a/x/runner/internal/keeper/keeper.go b/x/runner/internal/keeper/keeper.go index 2dac6fbfa..ee3189bc2 100644 --- a/x/runner/internal/keeper/keeper.go +++ b/x/runner/internal/keeper/keeper.go @@ -16,11 +16,11 @@ import ( type Keeper struct { storeKey sdk.StoreKey cdc *codec.Codec - instanceKeeper types.InstaceKeeper + instanceKeeper types.InstanceKeeper } // NewKeeper creates a runner keeper -func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, instanceKeeper types.InstaceKeeper) Keeper { +func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, instanceKeeper types.InstanceKeeper) Keeper { keeper := Keeper{ storeKey: key, cdc: cdc, diff --git a/x/runner/internal/types/expected_keepers.go b/x/runner/internal/types/expected_keepers.go index 8fc28fa3c..5b0fd210b 100644 --- a/x/runner/internal/types/expected_keepers.go +++ b/x/runner/internal/types/expected_keepers.go @@ -16,6 +16,6 @@ type ParamSubspace interface { } // InstanceKeeper module interface. -type InstaceKeeper interface { +type InstanceKeeper interface { FetchOrCreate(ctx sdk.Context, serviceHash hash.Hash, envHash hash.Hash) (*instance.Instance, error) } From f7dade884b27b0a974196f1fd1fdc0cff73d3fb4 Mon Sep 17 00:00:00 2001 From: krhubert Date: Wed, 12 Feb 2020 19:56:13 +0100 Subject: [PATCH 34/87] Use template to move process cosmos module to x/ dir --- app/app.go | 14 +- process/codec.go | 54 +++---- sdk/execution/keeper.go | 6 +- sdk/process/codec.go | 10 -- sdk/process/keeper.go | 110 --------------- sdk/process/module.go | 54 ------- sdk/process/msgs.go | 110 --------------- sdk/process/sdk.go | 25 ++-- x/process/abci.go | 13 ++ x/process/alias.go | 43 ++++++ x/process/client/cli/query.go | 73 ++++++++++ x/process/client/cli/tx.go | 53 +++++++ x/process/client/rest/query.go | 89 ++++++++++++ x/process/client/rest/rest.go | 13 ++ x/process/client/rest/tx.go | 10 ++ x/process/genesis.go | 18 +++ x/process/handler.go | 67 +++++++++ x/process/internal/keeper/keeper.go | 119 ++++++++++++++++ x/process/internal/keeper/querier.go | 54 +++++++ x/process/internal/types/codec.go | 23 +++ x/process/internal/types/events.go | 11 ++ x/process/internal/types/expected_keepers.go | 28 ++++ x/process/internal/types/genesis.go | 19 +++ x/process/internal/types/key.go | 15 ++ x/process/internal/types/msg.go | 110 +++++++++++++++ x/process/internal/types/params.go | 38 +++++ x/process/internal/types/querier.go | 7 + x/process/module.go | 139 +++++++++++++++++++ 28 files changed, 993 insertions(+), 332 deletions(-) delete mode 100644 sdk/process/codec.go delete mode 100644 sdk/process/keeper.go delete mode 100644 sdk/process/module.go delete mode 100644 sdk/process/msgs.go create mode 100644 x/process/abci.go create mode 100644 x/process/alias.go create mode 100644 x/process/client/cli/query.go create mode 100644 x/process/client/cli/tx.go create mode 100644 x/process/client/rest/query.go create mode 100644 x/process/client/rest/rest.go create mode 100644 x/process/client/rest/tx.go create mode 100644 x/process/genesis.go create mode 100644 x/process/handler.go create mode 100644 x/process/internal/keeper/keeper.go create mode 100644 x/process/internal/keeper/querier.go create mode 100644 x/process/internal/types/codec.go create mode 100644 x/process/internal/types/events.go create mode 100644 x/process/internal/types/expected_keepers.go create mode 100644 x/process/internal/types/genesis.go create mode 100644 x/process/internal/types/key.go create mode 100644 x/process/internal/types/msg.go create mode 100644 x/process/internal/types/params.go create mode 100644 x/process/internal/types/querier.go create mode 100644 x/process/module.go diff --git a/app/app.go b/app/app.go index 81e739819..e97d66bd3 100644 --- a/app/app.go +++ b/app/app.go @@ -29,11 +29,11 @@ import ( mesgcodec "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/cosmos" executionsdk "github.com/mesg-foundation/engine/sdk/execution" - processsdk "github.com/mesg-foundation/engine/sdk/process" runnersdk "github.com/mesg-foundation/engine/sdk/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" "github.com/mesg-foundation/engine/x/instance" "github.com/mesg-foundation/engine/x/ownership" + "github.com/mesg-foundation/engine/x/process" ) const appName = "engine" @@ -61,10 +61,10 @@ var ( // Engine's AppModuleBasic ownership.AppModuleBasic{}, instance.AppModuleBasic{}, + process.AppModuleBasic{}, cosmos.NewAppModuleBasic(servicesdk.ModuleName), cosmos.NewAppModuleBasic(runnersdk.ModuleName), cosmos.NewAppModuleBasic(executionsdk.ModuleName), - cosmos.NewAppModuleBasic(processsdk.ModuleName), ) // module account permissions @@ -117,9 +117,9 @@ type NewApp struct { // Engine's keepers ownershipKeeper ownership.Keeper instanceKeeper instance.Keeper + processKeeper process.Keeper serviceKeeper *servicesdk.Keeper runnerKeeper *runnersdk.Keeper - processKeeper *processsdk.Keeper executionKeeper *executionsdk.Keeper // Module Manager @@ -157,10 +157,10 @@ func NewInitApp( // Engine's module keys ownership.ModuleName, instance.ModuleName, + process.ModuleName, servicesdk.ModuleName, runnersdk.ModuleName, executionsdk.ModuleName, - processsdk.ModuleName, ) tKeys := sdk.NewTransientStoreKeys(staking.TStoreKey, params.TStoreKey) @@ -244,9 +244,9 @@ func NewInitApp( // Engine's module keepers app.ownershipKeeper = ownership.NewKeeper(app.cdc, keys[ownership.StoreKey]) app.instanceKeeper = instance.NewKeeper(app.cdc, keys[instance.StoreKey]) + app.processKeeper = process.NewKeeper(app.cdc, keys[process.StoreKey], app.instanceKeeper, app.ownershipKeeper) app.serviceKeeper = servicesdk.NewKeeper(keys[servicesdk.ModuleName], app.ownershipKeeper) app.runnerKeeper = runnersdk.NewKeeper(keys[runnersdk.ModuleName], app.instanceKeeper) - app.processKeeper = processsdk.NewKeeper(keys[processsdk.ModuleName], app.ownershipKeeper, app.instanceKeeper) app.executionKeeper = executionsdk.NewKeeper(keys[executionsdk.ModuleName], app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper) // NOTE: Any module instantiated in the module manager that is later modified @@ -262,10 +262,10 @@ func NewInitApp( // Engine's modules ownership.NewAppModule(app.ownershipKeeper), instance.NewAppModule(app.instanceKeeper), + process.NewAppModule(app.processKeeper), servicesdk.NewModule(app.serviceKeeper), runnersdk.NewModule(app.runnerKeeper), executionsdk.NewModule(app.executionKeeper), - processsdk.NewModule(app.processKeeper), staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper), ) @@ -289,10 +289,10 @@ func NewInitApp( // Engine's modules ownership.ModuleName, instance.ModuleName, + process.ModuleName, servicesdk.ModuleName, runnersdk.ModuleName, executionsdk.ModuleName, - processsdk.ModuleName, supply.ModuleName, genutil.ModuleName, diff --git a/process/codec.go b/process/codec.go index 915374596..39f345304 100644 --- a/process/codec.go +++ b/process/codec.go @@ -3,35 +3,35 @@ package process import ( "sort" - "github.com/mesg-foundation/engine/codec" + "github.com/cosmos/cosmos-sdk/codec" ) -func init() { - codec.RegisterInterface((*isProcess_Node_Map_Output_Value)(nil), nil) - codec.RegisterConcrete(&Process_Node_Map_Output_Ref{}, "mesg.types.Process_Node_Map_Output_Ref", nil) - codec.RegisterConcrete(&Process_Node_Map_Output_Reference{}, "mesg.types.Process_Node_Map_Output_Reference", nil) - codec.RegisterConcrete(&Process_Node_Map_Output_Null_{}, "mesg.types.Process_Node_Map_Output_Null_", nil) - codec.RegisterConcrete(&Process_Node_Map_Output_StringConst{}, "mesg.types.Process_Node_Map_Output_StringConst", nil) - codec.RegisterConcrete(&Process_Node_Map_Output_DoubleConst{}, "mesg.types.Process_Node_Map_Output_DoubleConst", nil) - codec.RegisterConcrete(&Process_Node_Map_Output_BoolConst{}, "mesg.types.Process_Node_Map_Output_BoolConst", nil) - codec.RegisterConcrete(&Process_Node_Map_Output_List_{}, "mesg.types.Process_Node_Map_Output_List_", nil) - codec.RegisterConcrete(&Process_Node_Map_Output_List{}, "mesg.types.Process_Node_Map_Output_List", nil) - codec.RegisterConcrete(&Process_Node_Map_Output_Map_{}, "mesg.types.Process_Node_Map_Output_Map_", nil) - codec.RegisterConcrete(&Process_Node_Map_Output_Map{}, "mesg.types.Process_Node_Map_Output_Map", nil) - codec.RegisterInterface((*isProcess_Node_Type)(nil), nil) - codec.RegisterConcrete(&Process_Node_Result_{}, "mesg.types.Process_Node_Result_", nil) - codec.RegisterConcrete(&Process_Node_Result{}, "mesg.types.Process_Node_Result", nil) - codec.RegisterConcrete(&Process_Node_Event_{}, "mesg.types.Process_Node_Event_", nil) - codec.RegisterConcrete(&Process_Node_Event{}, "mesg.types.Process_Node_Event", nil) - codec.RegisterConcrete(&Process_Node_Task_{}, "mesg.types.Process_Node_Task_", nil) - codec.RegisterConcrete(&Process_Node_Task{}, "mesg.types.Process_Node_Task", nil) - codec.RegisterConcrete(&Process_Node_Map_{}, "mesg.types.Process_Node_Map_", nil) - codec.RegisterConcrete(&Process_Node_Map{}, "mesg.types.Process_Node_Map", nil) - codec.RegisterConcrete(&Process_Node_Filter_{}, "mesg.types.Process_Node_Filter_", nil) - codec.RegisterConcrete(&Process_Node_Filter{}, "mesg.types.Process_Node_Filter", nil) - codec.RegisterInterface((*isProcess_Node_Map_Output_Reference_Path_Selector)(nil), nil) - codec.RegisterConcrete(&Process_Node_Map_Output_Reference_Path_Key{}, "mesg.types.Process_Node_Map_Output_Reference_Path_Key", nil) - codec.RegisterConcrete(&Process_Node_Map_Output_Reference_Path_Index{}, "mesg.types.Process_Node_Map_Output_Reference_Path_Index", nil) +func RegisterCodec(cdc *codec.Codec) { + cdc.RegisterInterface((*isProcess_Node_Map_Output_Value)(nil), nil) + cdc.RegisterConcrete(&Process_Node_Map_Output_Ref{}, "mesg.types.Process_Node_Map_Output_Ref", nil) + cdc.RegisterConcrete(&Process_Node_Map_Output_Reference{}, "mesg.types.Process_Node_Map_Output_Reference", nil) + cdc.RegisterConcrete(&Process_Node_Map_Output_Null_{}, "mesg.types.Process_Node_Map_Output_Null_", nil) + cdc.RegisterConcrete(&Process_Node_Map_Output_StringConst{}, "mesg.types.Process_Node_Map_Output_StringConst", nil) + cdc.RegisterConcrete(&Process_Node_Map_Output_DoubleConst{}, "mesg.types.Process_Node_Map_Output_DoubleConst", nil) + cdc.RegisterConcrete(&Process_Node_Map_Output_BoolConst{}, "mesg.types.Process_Node_Map_Output_BoolConst", nil) + cdc.RegisterConcrete(&Process_Node_Map_Output_List_{}, "mesg.types.Process_Node_Map_Output_List_", nil) + cdc.RegisterConcrete(&Process_Node_Map_Output_List{}, "mesg.types.Process_Node_Map_Output_List", nil) + cdc.RegisterConcrete(&Process_Node_Map_Output_Map_{}, "mesg.types.Process_Node_Map_Output_Map_", nil) + cdc.RegisterConcrete(&Process_Node_Map_Output_Map{}, "mesg.types.Process_Node_Map_Output_Map", nil) + cdc.RegisterInterface((*isProcess_Node_Type)(nil), nil) + cdc.RegisterConcrete(&Process_Node_Result_{}, "mesg.types.Process_Node_Result_", nil) + cdc.RegisterConcrete(&Process_Node_Result{}, "mesg.types.Process_Node_Result", nil) + cdc.RegisterConcrete(&Process_Node_Event_{}, "mesg.types.Process_Node_Event_", nil) + cdc.RegisterConcrete(&Process_Node_Event{}, "mesg.types.Process_Node_Event", nil) + cdc.RegisterConcrete(&Process_Node_Task_{}, "mesg.types.Process_Node_Task_", nil) + cdc.RegisterConcrete(&Process_Node_Task{}, "mesg.types.Process_Node_Task", nil) + cdc.RegisterConcrete(&Process_Node_Map_{}, "mesg.types.Process_Node_Map_", nil) + cdc.RegisterConcrete(&Process_Node_Map{}, "mesg.types.Process_Node_Map", nil) + cdc.RegisterConcrete(&Process_Node_Filter_{}, "mesg.types.Process_Node_Filter_", nil) + cdc.RegisterConcrete(&Process_Node_Filter{}, "mesg.types.Process_Node_Filter", nil) + cdc.RegisterInterface((*isProcess_Node_Map_Output_Reference_Path_Selector)(nil), nil) + cdc.RegisterConcrete(&Process_Node_Map_Output_Reference_Path_Key{}, "mesg.types.Process_Node_Map_Output_Reference_Path_Key", nil) + cdc.RegisterConcrete(&Process_Node_Map_Output_Reference_Path_Index{}, "mesg.types.Process_Node_Map_Output_Reference_Path_Index", nil) } // KeyOutput is a simple key/value representation of one output of a Process_Node_Map. diff --git a/sdk/execution/keeper.go b/sdk/execution/keeper.go index feba6d8ee..2f18bab35 100644 --- a/sdk/execution/keeper.go +++ b/sdk/execution/keeper.go @@ -10,10 +10,10 @@ import ( "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/protobuf/types" - processsdk "github.com/mesg-foundation/engine/sdk/process" runnersdk "github.com/mesg-foundation/engine/sdk/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" "github.com/mesg-foundation/engine/x/instance" + "github.com/mesg-foundation/engine/x/process" ) // Keeper holds the logic to read and write data. @@ -22,11 +22,11 @@ type Keeper struct { serviceKeeper *servicesdk.Keeper instanceKeeper instance.Keeper runnerKeeper *runnersdk.Keeper - processKeeper *processsdk.Keeper + processKeeper process.Keeper } // NewKeeper initialize a new keeper. -func NewKeeper(storeKey *cosmostypes.KVStoreKey, serviceKeeper *servicesdk.Keeper, instanceKeeper instance.Keeper, runnerKeeper *runnersdk.Keeper, processKeeper *processsdk.Keeper) *Keeper { +func NewKeeper(storeKey *cosmostypes.KVStoreKey, serviceKeeper *servicesdk.Keeper, instanceKeeper instance.Keeper, runnerKeeper *runnersdk.Keeper, processKeeper process.Keeper) *Keeper { return &Keeper{ storeKey: storeKey, serviceKeeper: serviceKeeper, diff --git a/sdk/process/codec.go b/sdk/process/codec.go deleted file mode 100644 index 3e7233d60..000000000 --- a/sdk/process/codec.go +++ /dev/null @@ -1,10 +0,0 @@ -package processsdk - -import ( - "github.com/mesg-foundation/engine/codec" -) - -func init() { - codec.RegisterConcrete(msgCreateProcess{}, "process/create", nil) - codec.RegisterConcrete(msgDeleteProcess{}, "process/delete", nil) -} diff --git a/sdk/process/keeper.go b/sdk/process/keeper.go deleted file mode 100644 index 726ef201b..000000000 --- a/sdk/process/keeper.go +++ /dev/null @@ -1,110 +0,0 @@ -package processsdk - -import ( - "fmt" - - cosmostypes "github.com/cosmos/cosmos-sdk/types" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/hash" - ownershippb "github.com/mesg-foundation/engine/ownership" - "github.com/mesg-foundation/engine/process" - "github.com/mesg-foundation/engine/x/instance" - "github.com/mesg-foundation/engine/x/ownership" -) - -// Keeper is the service keeper. -type Keeper struct { - storeKey *cosmostypes.KVStoreKey - ownershipKeeper ownership.Keeper - instanceKeeper instance.Keeper -} - -// NewKeeper returns the keeper of the service sdk. -func NewKeeper(storeKey *cosmostypes.KVStoreKey, ownershipKeeper ownership.Keeper, instanceKeeper instance.Keeper) *Keeper { - return &Keeper{ - storeKey: storeKey, - ownershipKeeper: ownershipKeeper, - instanceKeeper: instanceKeeper, - } -} - -// Create creates a new process. -func (k *Keeper) Create(req cosmostypes.Request, msg *msgCreateProcess) (*process.Process, error) { - store := req.KVStore(k.storeKey) - p := &process.Process{ - Name: msg.Request.Name, - Nodes: msg.Request.Nodes, - Edges: msg.Request.Edges, - } - p.Hash = hash.Dump(p) - if store.Has(p.Hash) { - return nil, fmt.Errorf("process %q already exists", p.Hash) - } - - for _, node := range p.Nodes { - switch n := node.Type.(type) { - case *process.Process_Node_Result_: - if _, err := k.instanceKeeper.Get(req, n.Result.InstanceHash); err != nil { - return nil, err - } - case *process.Process_Node_Event_: - if _, err := k.instanceKeeper.Get(req, n.Event.InstanceHash); err != nil { - return nil, err - } - case *process.Process_Node_Task_: - if _, err := k.instanceKeeper.Get(req, n.Task.InstanceHash); err != nil { - return nil, err - } - } - } - - value, err := codec.MarshalBinaryBare(p) - if err != nil { - return nil, err - } - - if _, err := k.ownershipKeeper.Set(req, msg.Owner, p.Hash, ownershippb.Ownership_Process); err != nil { - return nil, err - } - - store.Set(p.Hash, value) - return p, nil -} - -// Delete deletes a process and realated ownership. -func (k *Keeper) Delete(req cosmostypes.Request, msg *msgDeleteProcess) error { - if err := k.ownershipKeeper.Delete(req, msg.Owner, msg.Request.Hash); err != nil { - return err - } - req.KVStore(k.storeKey).Delete(msg.Request.Hash) - return nil -} - -// Get returns the service that matches given hash. -func (k *Keeper) Get(req cosmostypes.Request, hash hash.Hash) (*process.Process, error) { - store := req.KVStore(k.storeKey) - if !store.Has(hash) { - return nil, fmt.Errorf("process %q not found", hash) - } - - var p *process.Process - return p, codec.UnmarshalBinaryBare(store.Get(hash), &p) -} - -// List returns all services. -func (k *Keeper) List(req cosmostypes.Request) ([]*process.Process, error) { - var ( - processes []*process.Process - iter = req.KVStore(k.storeKey).Iterator(nil, nil) - ) - for iter.Valid() { - var p *process.Process - if err := codec.UnmarshalBinaryBare(iter.Value(), &p); err != nil { - return nil, err - } - processes = append(processes, p) - iter.Next() - } - iter.Close() - return processes, nil -} diff --git a/sdk/process/module.go b/sdk/process/module.go deleted file mode 100644 index 3d2cb92bf..000000000 --- a/sdk/process/module.go +++ /dev/null @@ -1,54 +0,0 @@ -package processsdk - -import ( - "fmt" - - cosmostypes "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/cosmos-sdk/types/module" - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/hash" - abci "github.com/tendermint/tendermint/abci/types" -) - -// ModuleName is the name of this module. -const ModuleName = "process" - -// NewModule returns the module of this sdk. -func NewModule(k *Keeper) module.AppModule { - return cosmos.NewAppModule(cosmos.NewAppModuleBasic(ModuleName), handler(k), querier(k)) -} - -func handler(k *Keeper) cosmos.Handler { - return func(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { - switch msg := msg.(type) { - case msgCreateProcess: - p, err := k.Create(request, &msg) - if err != nil { - return nil, err - } - return p.Hash, nil - case msgDeleteProcess: - return nil, k.Delete(request, &msg) - default: - return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized process msg type: %v", msg.Type()) - } - } -} - -func querier(k *Keeper) cosmos.Querier { - return func(request cosmostypes.Request, path []string, req abci.RequestQuery) (res interface{}, err error) { - switch path[0] { - case "get": - hash, err := hash.Decode(path[1]) - if err != nil { - return nil, err - } - return k.Get(request, hash) - case "list": - return k.List(request) - default: - return nil, fmt.Errorf("unknown service query endpoint %s", path[0]) - } - } -} diff --git a/sdk/process/msgs.go b/sdk/process/msgs.go deleted file mode 100644 index 7eb457a37..000000000 --- a/sdk/process/msgs.go +++ /dev/null @@ -1,110 +0,0 @@ -package processsdk - -import ( - cosmostypes "github.com/cosmos/cosmos-sdk/types" - cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/ext/xvalidator" - "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/process" - "github.com/mesg-foundation/engine/protobuf/api" -) - -// msgCreateProcess defines a state transition to create a service. -type msgCreateProcess struct { - Owner cosmostypes.AccAddress `json:"owner" validate:"required,accaddress"` - Request *api.CreateProcessRequest `json:"request" validate:"required"` -} - -// newMsgCreateProcess is a constructor function for msgCreateProcess. -func newMsgCreateProcess(owner cosmostypes.AccAddress, req *api.CreateProcessRequest) *msgCreateProcess { - return &msgCreateProcess{ - Request: req, - Owner: owner, - } -} - -// Route should return the name of the module. -func (msg msgCreateProcess) Route() string { - return ModuleName -} - -// Type returns the action. -func (msg msgCreateProcess) Type() string { - return "create_process" -} - -// ValidateBasic runs stateless checks on the message. -func (msg msgCreateProcess) ValidateBasic() error { - if msg.Owner.Empty() { - return cosmoserrors.Wrap(cosmoserrors.ErrInvalidAddress, "owner is missing") - } - if err := xvalidator.Validate.Struct(msg); err != nil { - return err - } - p := &process.Process{ - Name: msg.Request.Name, - Nodes: msg.Request.Nodes, - Edges: msg.Request.Edges, - } - p.Hash = hash.Dump(p) - if err := p.Validate(); err != nil { - return err - } - return nil -} - -// GetSignBytes encodes the message for signing. -func (msg msgCreateProcess) GetSignBytes() []byte { - return cosmostypes.MustSortJSON(codec.MustMarshalJSON(msg)) -} - -// GetSigners defines whose signature is required. -func (msg msgCreateProcess) GetSigners() []cosmostypes.AccAddress { - return []cosmostypes.AccAddress{msg.Owner} -} - -// msgDeleteProcess defines a state transition to create a service. -type msgDeleteProcess struct { - Owner cosmostypes.AccAddress `json:"owner" validate:"required,accaddress"` - Request *api.DeleteProcessRequest `json:"request" validate:"required"` -} - -// newMsgDeleteProcess is a constructor function for msgDeleteProcess. -func newMsgDeleteProcess(owner cosmostypes.AccAddress, request *api.DeleteProcessRequest) *msgDeleteProcess { - return &msgDeleteProcess{ - Owner: owner, - Request: request, - } -} - -// Route should return the name of the module. -func (msg msgDeleteProcess) Route() string { - return ModuleName -} - -// Type returns the action. -func (msg msgDeleteProcess) Type() string { - return "delete_process" -} - -// ValidateBasic runs stateless checks on the message. -func (msg msgDeleteProcess) ValidateBasic() error { - if msg.Owner.Empty() { - return cosmoserrors.Wrap(cosmoserrors.ErrInvalidAddress, "owner is missing") - } - if err := xvalidator.Validate.Struct(msg); err != nil { - return err - } - return nil -} - -// GetSignBytes encodes the message for signing. -func (msg msgDeleteProcess) GetSignBytes() []byte { - return cosmostypes.MustSortJSON(codec.MustMarshalJSON(msg)) -} - -// GetSigners defines whose signature is required. -func (msg msgDeleteProcess) GetSigners() []cosmostypes.AccAddress { - return []cosmostypes.AccAddress{msg.Owner} -} diff --git a/sdk/process/sdk.go b/sdk/process/sdk.go index cc99d987b..35a110f33 100644 --- a/sdk/process/sdk.go +++ b/sdk/process/sdk.go @@ -1,10 +1,13 @@ package processsdk import ( + "fmt" + "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/process" + processpb "github.com/mesg-foundation/engine/process" "github.com/mesg-foundation/engine/protobuf/api" + "github.com/mesg-foundation/engine/x/process" ) // SDK is the process sdk. @@ -20,12 +23,12 @@ func New(client *cosmos.Client) *SDK { } // Create creates a new process. -func (s *SDK) Create(req *api.CreateProcessRequest) (*process.Process, error) { +func (s *SDK) Create(req *api.CreateProcessRequest) (*processpb.Process, error) { acc, err := s.client.GetAccount() if err != nil { return nil, err } - msg := newMsgCreateProcess(acc.GetAddress(), req) + msg := process.NewMsgCreateProcess(acc.GetAddress(), req) tx, err := s.client.BuildAndBroadcastMsg(msg) if err != nil { return nil, err @@ -39,24 +42,24 @@ func (s *SDK) Delete(req *api.DeleteProcessRequest) error { if err != nil { return err } - msg := newMsgDeleteProcess(acc.GetAddress(), req) + msg := process.NewMsgDeleteProcess(acc.GetAddress(), req) _, err = s.client.BuildAndBroadcastMsg(msg) return err } // Get returns the process that matches given hash. -func (s *SDK) Get(hash hash.Hash) (*process.Process, error) { - var process process.Process - if err := s.client.Query("custom/"+ModuleName+"/get/"+hash.String(), nil, &process); err != nil { +func (s *SDK) Get(hash hash.Hash) (*processpb.Process, error) { + var p processpb.Process + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", process.QuerierRoute, process.QueryGetProcess, hash.String()), nil, &p); err != nil { return nil, err } - return &process, nil + return &p, nil } // List returns all processes. -func (s *SDK) List() ([]*process.Process, error) { - var processes []*process.Process - if err := s.client.Query("custom/"+ModuleName+"/list", nil, &processes); err != nil { +func (s *SDK) List() ([]*processpb.Process, error) { + var processes []*processpb.Process + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s", process.QuerierRoute, process.QueryListProcesses), nil, &processes); err != nil { return nil, err } return processes, nil diff --git a/x/process/abci.go b/x/process/abci.go new file mode 100644 index 000000000..580d7d7b2 --- /dev/null +++ b/x/process/abci.go @@ -0,0 +1,13 @@ +package process + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// BeginBlocker check for infraction evidence or downtime of validators +// on every begin block +func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k Keeper) {} + +// EndBlocker called every block, process inflation, update validator set. +func EndBlocker(ctx sdk.Context, k Keeper) {} diff --git a/x/process/alias.go b/x/process/alias.go new file mode 100644 index 000000000..83d4bc449 --- /dev/null +++ b/x/process/alias.go @@ -0,0 +1,43 @@ +package process + +import ( + "github.com/mesg-foundation/engine/x/process/internal/keeper" + "github.com/mesg-foundation/engine/x/process/internal/types" +) + +// const aliases +const ( + ModuleName = types.ModuleName + RouterKey = types.RouterKey + StoreKey = types.StoreKey + DefaultParamspace = types.DefaultParamspace + QuerierRoute = types.QuerierRoute +) + +// functions and variable aliases +var ( + NewKeeper = keeper.NewKeeper + NewQuerier = keeper.NewQuerier + RegisterCodec = types.RegisterCodec + NewGenesisState = types.NewGenesisState + DefaultGenesisState = types.DefaultGenesisState + ValidateGenesis = types.ValidateGenesis + + ModuleCdc = types.ModuleCdc + + QueryGetProcess = types.QueryGetProcess + QueryListProcesses = types.QueryListProcesses + + NewMsgCreateProcess = types.NewMsgCreateProcess + NewMsgDeleteProcess = types.NewMsgDeleteProcess +) + +// module types +type ( + Keeper = keeper.Keeper + GenesisState = types.GenesisState + Params = types.Params + + MsgCreateProcess = types.MsgCreateProcess + MsgDeleteProcess = types.MsgDeleteProcess +) diff --git a/x/process/client/cli/query.go b/x/process/client/cli/query.go new file mode 100644 index 000000000..26ba27146 --- /dev/null +++ b/x/process/client/cli/query.go @@ -0,0 +1,73 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/mesg-foundation/engine/process" + "github.com/mesg-foundation/engine/x/process/internal/types" + "github.com/spf13/cobra" +) + +// GetQueryCmd returns the cli query commands for this module +func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { + // Group process queries under a subcommand + processQueryCmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + processQueryCmd.AddCommand( + flags.GetCommands( + GetCmdGetProcess(queryRoute, cdc), + GetCmdListProcesses(queryRoute, cdc), + )..., + ) + + return processQueryCmd +} + +func GetCmdGetProcess(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "get", + Short: "get", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", queryRoute, types.QueryGetProcess, args[0]), nil) + if err != nil { + fmt.Printf("could not get process\n%s\n", err.Error()) + return nil + } + + var out *process.Process + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} + +func GetCmdListProcesses(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "list", + Short: "list", + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryListProcesses), nil) + if err != nil { + fmt.Printf("could not list processes\n%s\n", err.Error()) + return nil + } + + var out []*process.Process + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} diff --git a/x/process/client/cli/tx.go b/x/process/client/cli/tx.go new file mode 100644 index 000000000..d6e4faa7b --- /dev/null +++ b/x/process/client/cli/tx.go @@ -0,0 +1,53 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/mesg-foundation/engine/x/process/internal/types" + "github.com/spf13/cobra" +) + +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + processTxCmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + processTxCmd.AddCommand(flags.PostCommands( + GetCmdCreateProcess(cdc), + GetCmdDeleteProcess(cdc), + )...) + + return processTxCmd +} + +func GetCmdCreateProcess(cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "createProcess [serviceHash] [key=val]...", + Short: "Creates a new process", + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + // TODO: + return nil + }, + } +} + +func GetCmdDeleteProcess(cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "createProcess [serviceHash] [key=val]...", + Short: "Creates a new process", + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + // TODO: + return nil + }, + } +} diff --git a/x/process/client/rest/query.go b/x/process/client/rest/query.go new file mode 100644 index 000000000..05b6e2c1d --- /dev/null +++ b/x/process/client/rest/query.go @@ -0,0 +1,89 @@ +package rest + +import ( + "fmt" + "net/http" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/types/rest" + "github.com/gorilla/mux" + "github.com/mesg-foundation/engine/x/process/internal/types" +) + +func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { + r.HandleFunc( + "/process/get/{hash}", + queryGetHandlerFn(cliCtx), + ).Methods(http.MethodGet) + r.HandleFunc( + "/process/list", + queryListHandlerFn(cliCtx), + ).Methods(http.MethodGet) + + r.HandleFunc( + "/process/parameters", + queryParamsHandlerFn(cliCtx), + ).Methods("GET") +} + +func queryGetHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryGetProcess, vars["hash"]) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + +func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryListProcesses) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + +func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/parameters", types.QuerierRoute) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} diff --git a/x/process/client/rest/rest.go b/x/process/client/rest/rest.go new file mode 100644 index 000000000..b73197e76 --- /dev/null +++ b/x/process/client/rest/rest.go @@ -0,0 +1,13 @@ +package rest + +import ( + "github.com/gorilla/mux" + + "github.com/cosmos/cosmos-sdk/client/context" +) + +// RegisterRoutes registers process-related REST handlers to a router +func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) { + registerQueryRoutes(cliCtx, r) + registerTxRoutes(cliCtx, r) +} diff --git a/x/process/client/rest/tx.go b/x/process/client/rest/tx.go new file mode 100644 index 000000000..048e7d9e9 --- /dev/null +++ b/x/process/client/rest/tx.go @@ -0,0 +1,10 @@ +package rest + +import ( + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/gorilla/mux" +) + +func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) { + // TODO: +} diff --git a/x/process/genesis.go b/x/process/genesis.go new file mode 100644 index 000000000..8a79f93fb --- /dev/null +++ b/x/process/genesis.go @@ -0,0 +1,18 @@ +package process + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/mesg-foundation/engine/x/process/internal/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// InitGenesis initialize default parameters and the keeper's address to pubkey map. +func InitGenesis(ctx sdk.Context, k Keeper, data types.GenesisState) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} + +// ExportGenesis writes the current store values // to a genesis file, +// which can be imported again with InitGenesis. +func ExportGenesis(ctx sdk.Context, k Keeper) types.GenesisState { + return types.NewGenesisState() +} diff --git a/x/process/handler.go b/x/process/handler.go new file mode 100644 index 000000000..9c4690256 --- /dev/null +++ b/x/process/handler.go @@ -0,0 +1,67 @@ +package process + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/x/process/internal/types" +) + +// NewHandler creates an sdk.Handler for all the process type messages +func NewHandler(k Keeper) sdk.Handler { + return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + ctx = ctx.WithEventManager(sdk.NewEventManager()) + switch msg := msg.(type) { + case MsgCreateProcess: + return handleMsgCreateProcess(ctx, k, &msg) + case MsgDeleteProcess: + return handleMsgDeleteProcess(ctx, k, &msg) + default: + errMsg := fmt.Sprintf("unrecognized %s message type: %T", ModuleName, msg) + return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg) + } + } +} + +// handleMsgCreateProcess creates a new process. +func handleMsgCreateProcess(ctx sdk.Context, k Keeper, msg *MsgCreateProcess) (*sdk.Result, error) { + p, err := k.Create(ctx, msg) + if err != nil { + return nil, err + } + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeyAction, types.EventTypeCreateProcess), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Owner.String()), + sdk.NewAttribute(types.AttributeHash, p.Hash.String()), + ), + ) + + return &sdk.Result{ + Data: p.Hash, + Events: ctx.EventManager().Events(), + }, nil +} + +// handleMsgDeleteProcess deletes a process. +func handleMsgDeleteProcess(ctx sdk.Context, k Keeper, msg *MsgDeleteProcess) (*sdk.Result, error) { + if err := k.Delete(ctx, msg); err != nil { + return nil, err + } + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeyAction, types.EventTypeDeleteProcess), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Owner.String()), + sdk.NewAttribute(types.AttributeHash, msg.Request.Hash.String()), + ), + ) + + return &sdk.Result{Events: ctx.EventManager().Events()}, nil +} diff --git a/x/process/internal/keeper/keeper.go b/x/process/internal/keeper/keeper.go new file mode 100644 index 000000000..3c17e3497 --- /dev/null +++ b/x/process/internal/keeper/keeper.go @@ -0,0 +1,119 @@ +package keeper + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/mesg-foundation/engine/hash" + ownershippb "github.com/mesg-foundation/engine/ownership" + "github.com/mesg-foundation/engine/process" + processpb "github.com/mesg-foundation/engine/process" + "github.com/mesg-foundation/engine/x/process/internal/types" + "github.com/tendermint/tendermint/libs/log" +) + +// Keeper of the process store +type Keeper struct { + storeKey sdk.StoreKey + cdc *codec.Codec + ownershipKeeper types.OwnershipKeeper + instanceKeeper types.InstanceKeeper +} + +// NewKeeper creates a process keeper +func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, instanceKeeper types.InstanceKeeper, ownershipKeeper types.OwnershipKeeper) Keeper { + keeper := Keeper{ + storeKey: key, + cdc: cdc, + instanceKeeper: instanceKeeper, + ownershipKeeper: ownershipKeeper, + } + return keeper +} + +// Logger returns a module-specific logger. +func (k Keeper) Logger(ctx sdk.Context) log.Logger { + return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) +} + +// Create creates a new process. +func (k *Keeper) Create(ctx sdk.Context, msg *types.MsgCreateProcess) (*processpb.Process, error) { + store := ctx.KVStore(k.storeKey) + p := &process.Process{ + Name: msg.Request.Name, + Nodes: msg.Request.Nodes, + Edges: msg.Request.Edges, + } + p.Hash = hash.Dump(p) + if store.Has(p.Hash) { + return nil, fmt.Errorf("process %q already exists", p.Hash) + } + + for _, node := range p.Nodes { + switch n := node.Type.(type) { + case *processpb.Process_Node_Result_: + if _, err := k.instanceKeeper.Get(ctx, n.Result.InstanceHash); err != nil { + return nil, err + } + case *processpb.Process_Node_Event_: + if _, err := k.instanceKeeper.Get(ctx, n.Event.InstanceHash); err != nil { + return nil, err + } + case *processpb.Process_Node_Task_: + if _, err := k.instanceKeeper.Get(ctx, n.Task.InstanceHash); err != nil { + return nil, err + } + } + } + + value, err := k.cdc.MarshalBinaryLengthPrefixed(p) + if err != nil { + return nil, err + } + + if _, err := k.ownershipKeeper.Set(ctx, msg.Owner, p.Hash, ownershippb.Ownership_Process); err != nil { + return nil, err + } + + store.Set(p.Hash, value) + return p, nil +} + +// Delete deletes a process. +func (k *Keeper) Delete(ctx sdk.Context, msg *types.MsgDeleteProcess) error { + if err := k.ownershipKeeper.Delete(ctx, msg.Owner, msg.Request.Hash); err != nil { + return err + } + ctx.KVStore(k.storeKey).Delete(msg.Request.Hash) + return nil +} + +// Get returns the process that matches given hash. +func (k *Keeper) Get(ctx sdk.Context, hash hash.Hash) (*processpb.Process, error) { + store := ctx.KVStore(k.storeKey) + if !store.Has(hash) { + return nil, fmt.Errorf("process %q not found", hash) + } + value := store.Get(hash) + var p *processpb.Process + return p, k.cdc.UnmarshalBinaryLengthPrefixed(value, &p) +} + +// List returns all processes. +func (k *Keeper) List(ctx sdk.Context) ([]*processpb.Process, error) { + var ( + processes []*processpb.Process + iter = ctx.KVStore(k.storeKey).Iterator(nil, nil) + ) + for iter.Valid() { + var p *processpb.Process + if err := k.cdc.UnmarshalBinaryLengthPrefixed(iter.Value(), &p); err != nil { + return nil, err + } + processes = append(processes, p) + iter.Next() + } + iter.Close() + return processes, nil +} diff --git a/x/process/internal/keeper/querier.go b/x/process/internal/keeper/querier.go new file mode 100644 index 000000000..54e735917 --- /dev/null +++ b/x/process/internal/keeper/querier.go @@ -0,0 +1,54 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/x/process/internal/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// NewQuerier creates a new querier for instance clients. +func NewQuerier(k Keeper) sdk.Querier { + return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) { + switch path[0] { + case types.QueryGetProcess: + return getProcess(ctx, path[1:], k) + case types.QueryListProcesses: + return listProcess(ctx, k) + default: + return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown process query endpoint") + } + } +} + +func getProcess(ctx sdk.Context, path []string, k Keeper) ([]byte, error) { + hash, err := hash.Decode(path[0]) + if err != nil { + return nil, err + } + + instance, err := k.Get(ctx, hash) + if err != nil { + return nil, err + } + + res, err := types.ModuleCdc.MarshalJSON(instance) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return res, nil +} + +func listProcess(ctx sdk.Context, k Keeper) ([]byte, error) { + instances, err := k.List(ctx) + if err != nil { + return nil, err + } + + res, err := types.ModuleCdc.MarshalJSON(instances) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return res, nil +} diff --git a/x/process/internal/types/codec.go b/x/process/internal/types/codec.go new file mode 100644 index 000000000..e2ba8cb35 --- /dev/null +++ b/x/process/internal/types/codec.go @@ -0,0 +1,23 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" + processpb "github.com/mesg-foundation/engine/process" +) + +// RegisterCodec registers concrete types on codec +func RegisterCodec(cdc *codec.Codec) { + processpb.RegisterCodec(cdc) + cdc.RegisterConcrete(MsgCreateProcess{}, "process/CreateProcess", nil) + cdc.RegisterConcrete(MsgDeleteProcess{}, "process/DeleteProcess", nil) +} + +// ModuleCdc defines the module codec +var ModuleCdc *codec.Codec + +func init() { + ModuleCdc = codec.New() + RegisterCodec(ModuleCdc) + codec.RegisterCrypto(ModuleCdc) + ModuleCdc.Seal() +} diff --git a/x/process/internal/types/events.go b/x/process/internal/types/events.go new file mode 100644 index 000000000..57e107415 --- /dev/null +++ b/x/process/internal/types/events.go @@ -0,0 +1,11 @@ +package types + +// module event types +const ( + EventTypeCreateProcess = "CreateProcess" + EventTypeDeleteProcess = "DeleteProcess" + + AttributeHash = "hash" + + AttributeValueCategory = ModuleName +) diff --git a/x/process/internal/types/expected_keepers.go b/x/process/internal/types/expected_keepers.go new file mode 100644 index 000000000..fc0ec8169 --- /dev/null +++ b/x/process/internal/types/expected_keepers.go @@ -0,0 +1,28 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/params" + "github.com/mesg-foundation/engine/hash" + instancepb "github.com/mesg-foundation/engine/instance" + ownershippb "github.com/mesg-foundation/engine/ownership" +) + +// ParamSubspace defines the expected Subspace interfacace +type ParamSubspace interface { + WithKeyTable(table params.KeyTable) params.Subspace + Get(ctx sdk.Context, key []byte, ptr interface{}) + GetParamSet(ctx sdk.Context, ps params.ParamSet) + SetParamSet(ctx sdk.Context, ps params.ParamSet) +} + +// InstanceKeeper module interface. +type InstanceKeeper interface { + Get(ctx sdk.Context, instanceHash hash.Hash) (*instancepb.Instance, error) +} + +// OwnershipKeeper module interface. +type OwnershipKeeper interface { + Delete(ctx sdk.Context, owner sdk.AccAddress, resourceHash hash.Hash) error + Set(ctx sdk.Context, owner sdk.AccAddress, resourceHash hash.Hash, resource ownershippb.Ownership_Resource) (*ownershippb.Ownership, error) +} diff --git a/x/process/internal/types/genesis.go b/x/process/internal/types/genesis.go new file mode 100644 index 000000000..bebeec0dc --- /dev/null +++ b/x/process/internal/types/genesis.go @@ -0,0 +1,19 @@ +package types + +// GenesisState - all instance state that must be provided at genesis +type GenesisState struct{} + +// NewGenesisState creates a new GenesisState object +func NewGenesisState() GenesisState { + return GenesisState{} +} + +// DefaultGenesisState - default GenesisState used by Cosmos Hub +func DefaultGenesisState() GenesisState { + return GenesisState{} +} + +// ValidateGenesis validates the instance genesis parameters +func ValidateGenesis(data GenesisState) error { + return nil +} diff --git a/x/process/internal/types/key.go b/x/process/internal/types/key.go new file mode 100644 index 000000000..1b8762a08 --- /dev/null +++ b/x/process/internal/types/key.go @@ -0,0 +1,15 @@ +package types + +const ( + // ModuleName is the name of the module + ModuleName = "process" + + // StoreKey to be used when creating the KVStore + StoreKey = ModuleName + + // RouterKey to be used for routing msgs + RouterKey = ModuleName + + // QuerierRoute to be used for routing + QuerierRoute = ModuleName +) diff --git a/x/process/internal/types/msg.go b/x/process/internal/types/msg.go new file mode 100644 index 000000000..13ec6859f --- /dev/null +++ b/x/process/internal/types/msg.go @@ -0,0 +1,110 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/ext/xvalidator" + "github.com/mesg-foundation/engine/hash" + processpb "github.com/mesg-foundation/engine/process" + "github.com/mesg-foundation/engine/protobuf/api" +) + +// MsgCreateProcess defines a state transition to create a process. +type MsgCreateProcess struct { + Owner sdk.AccAddress `json:"address" validate:"required,accaddress"` + Request *api.CreateProcessRequest `json:"request" validate:"required"` +} + +// NewMsgCreateProcess is a constructor function for MsgCreateProcess. +func NewMsgCreateProcess(owner sdk.AccAddress, request *api.CreateProcessRequest) *MsgCreateProcess { + return &MsgCreateProcess{ + Owner: owner, + Request: request, + } +} + +// Route should return the name of the module route. +func (msg MsgCreateProcess) Route() string { + return RouterKey +} + +// Type returns the action. +func (msg MsgCreateProcess) Type() string { + return "CreateProcess" +} + +// ValidateBasic runs stateless checks on the message. +func (msg MsgCreateProcess) ValidateBasic() error { + if msg.Owner.Empty() { + return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "owner is missing") + } + if err := xvalidator.Validate.Struct(msg); err != nil { + return err + } + p := &processpb.Process{ + Name: msg.Request.Name, + Nodes: msg.Request.Nodes, + Edges: msg.Request.Edges, + } + p.Hash = hash.Dump(p) + if err := p.Validate(); err != nil { + return err + } + + return nil +} + +// GetSignBytes encodes the message for signing. +func (msg MsgCreateProcess) GetSignBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg)) +} + +// GetSigners defines whose signature is required. +func (msg MsgCreateProcess) GetSigners() []sdk.AccAddress { + return []sdk.AccAddress{msg.Owner} +} + +// MsgDeleteProcess defines a state transition to delete a process. +type MsgDeleteProcess struct { + Owner sdk.AccAddress `json:"address" validate:"required,accaddress"` + Request *api.DeleteProcessRequest `json:"request" validate:"required"` +} + +// NewMsgDeleteProcess is a constructor function for MsgDeleteProcess. +func NewMsgDeleteProcess(owner sdk.AccAddress, request *api.DeleteProcessRequest) *MsgDeleteProcess { + return &MsgDeleteProcess{ + Owner: owner, + Request: request, + } +} + +// Route should return the name of the module. +func (msg MsgDeleteProcess) Route() string { + return ModuleName +} + +// Type returns the action. +func (msg MsgDeleteProcess) Type() string { + return "DeleteProcess" +} + +// ValidateBasic runs stateless checks on the message. +func (msg MsgDeleteProcess) ValidateBasic() error { + if msg.Owner.Empty() { + return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "owner is missing") + } + if err := xvalidator.Validate.Struct(msg); err != nil { + return err + } + return nil +} + +// GetSignBytes encodes the message for signing. +func (msg MsgDeleteProcess) GetSignBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg)) +} + +// GetSigners defines whose signature is required. +func (msg MsgDeleteProcess) GetSigners() []sdk.AccAddress { + return []sdk.AccAddress{msg.Owner} +} diff --git a/x/process/internal/types/params.go b/x/process/internal/types/params.go new file mode 100644 index 000000000..4940992b5 --- /dev/null +++ b/x/process/internal/types/params.go @@ -0,0 +1,38 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/x/params" +) + +// Default parameter namespace +const ( + DefaultParamspace = ModuleName +) + +// ParamKeyTable for instance module +func ParamKeyTable() params.KeyTable { + return params.NewKeyTable().RegisterParamSet(&Params{}) +} + +// Params - used for initializing default parameter for instance at genesis +type Params struct{} + +// NewParams creates a new Params object +func NewParams() Params { + return Params{} +} + +// String implements the stringer interface for Params +func (p Params) String() string { + return "" +} + +// ParamSetPairs - Implements params.ParamSet +func (p *Params) ParamSetPairs() params.ParamSetPairs { + return params.ParamSetPairs{} +} + +// DefaultParams defines the parameters for this module +func DefaultParams() Params { + return NewParams() +} diff --git a/x/process/internal/types/querier.go b/x/process/internal/types/querier.go new file mode 100644 index 000000000..c8c2a911b --- /dev/null +++ b/x/process/internal/types/querier.go @@ -0,0 +1,7 @@ +package types + +// Query endpoints supported by the process querier +const ( + QueryGetProcess = "get" + QueryListProcesses = "list" +) diff --git a/x/process/module.go b/x/process/module.go new file mode 100644 index 000000000..ccf907beb --- /dev/null +++ b/x/process/module.go @@ -0,0 +1,139 @@ +package process + +import ( + "encoding/json" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/gorilla/mux" + "github.com/mesg-foundation/engine/x/process/client/cli" + "github.com/mesg-foundation/engine/x/process/client/rest" + "github.com/mesg-foundation/engine/x/process/internal/types" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" +) + +var ( + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} +) + +// AppModuleBasic defines the basic application module used by the process module. +type AppModuleBasic struct{} + +var _ module.AppModuleBasic = AppModuleBasic{} + +// Name returns the process module's name. +func (AppModuleBasic) Name() string { + return types.ModuleName +} + +// RegisterCodec registers the process module's types for the given codec. +func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { + RegisterCodec(cdc) +} + +// DefaultGenesis returns default genesis state as raw bytes for the process +// module. +func (AppModuleBasic) DefaultGenesis() json.RawMessage { + return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) +} + +// ValidateGenesis performs genesis state validation for the process module. +func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { + var data GenesisState + err := ModuleCdc.UnmarshalJSON(bz, &data) + if err != nil { + return err + } + return ValidateGenesis(data) +} + +// RegisterRESTRoutes registers the REST routes for the process module. +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { + rest.RegisterRoutes(ctx, rtr) +} + +// GetTxCmd returns the root tx command for the process module. +func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { + return cli.GetTxCmd(cdc) +} + +// GetQueryCmd returns no root query command for the process module. +func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { + return cli.GetQueryCmd(StoreKey, cdc) +} + +//____________________________________________________________________________ + +// AppModule implements an application module for the process module. +type AppModule struct { + AppModuleBasic + + keeper Keeper +} + +// NewAppModule creates a new AppModule object +func NewAppModule(k Keeper) AppModule { + return AppModule{ + AppModuleBasic: AppModuleBasic{}, + keeper: k, + } +} + +// Name returns the process module's name. +func (AppModule) Name() string { + return ModuleName +} + +// RegisterInvariants registers the process module invariants. +func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} + +// Route returns the message routing key for the process module. +func (AppModule) Route() string { + return RouterKey +} + +// NewHandler returns an sdk.Handler for the process module. +func (am AppModule) NewHandler() sdk.Handler { + return NewHandler(am.keeper) +} + +// QuerierRoute returns the process module's querier route name. +func (AppModule) QuerierRoute() string { + return QuerierRoute +} + +// NewQuerierHandler returns the process module sdk.Querier. +func (am AppModule) NewQuerierHandler() sdk.Querier { + return NewQuerier(am.keeper) +} + +// InitGenesis performs genesis initialization for the process module. It returns +// no validator updates. +func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { + var genesisState GenesisState + ModuleCdc.MustUnmarshalJSON(data, &genesisState) + InitGenesis(ctx, am.keeper, genesisState) + return []abci.ValidatorUpdate{} +} + +// ExportGenesis returns the exported genesis state as raw bytes for the process +// module. +func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { + gs := ExportGenesis(ctx, am.keeper) + return ModuleCdc.MustMarshalJSON(gs) +} + +// BeginBlock returns the begin blocker for the process module. +func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { + BeginBlocker(ctx, req, am.keeper) +} + +// EndBlock returns the end blocker for the process module. It returns no validator +// updates. +func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} From 055aaaf9d52e3eb907246ef3dca28a2fd5a5adc5 Mon Sep 17 00:00:00 2001 From: krhubert Date: Thu, 13 Feb 2020 19:39:39 +0100 Subject: [PATCH 35/87] Use template to move service cosmos module to x/ dir --- app/app.go | 14 +- sdk/execution/keeper.go | 6 +- sdk/service/codec.go | 9 -- sdk/service/keeper.go | 119 ---------------- sdk/service/module.go | 67 --------- sdk/service/msgs.go | 54 ------- sdk/service/sdk.go | 32 ++--- x/service/abci.go | 13 ++ x/service/alias.go | 43 ++++++ x/service/client/cli/query.go | 116 +++++++++++++++ x/service/client/cli/tx.go | 26 ++++ x/service/client/rest/query.go | 122 ++++++++++++++++ x/service/client/rest/rest.go | 12 ++ x/service/client/rest/tx.go | 10 ++ x/service/genesis.go | 18 +++ x/service/handler.go | 46 ++++++ x/service/internal/keeper/keeper.go | 127 +++++++++++++++++ x/service/internal/keeper/querier.go | 92 ++++++++++++ x/service/internal/types/codec.go | 20 +++ x/service/internal/types/events.go | 10 ++ x/service/internal/types/expected_keepers.go | 21 +++ x/service/internal/types/genesis.go | 19 +++ x/service/internal/types/key.go | 15 ++ x/service/internal/types/msg.go | 53 +++++++ x/service/internal/types/params.go | 38 +++++ x/service/internal/types/querier.go | 9 ++ x/service/module.go | 141 +++++++++++++++++++ 27 files changed, 977 insertions(+), 275 deletions(-) delete mode 100644 sdk/service/codec.go delete mode 100644 sdk/service/keeper.go delete mode 100644 sdk/service/module.go delete mode 100644 sdk/service/msgs.go create mode 100644 x/service/abci.go create mode 100644 x/service/alias.go create mode 100644 x/service/client/cli/query.go create mode 100644 x/service/client/cli/tx.go create mode 100644 x/service/client/rest/query.go create mode 100644 x/service/client/rest/rest.go create mode 100644 x/service/client/rest/tx.go create mode 100644 x/service/genesis.go create mode 100644 x/service/handler.go create mode 100644 x/service/internal/keeper/keeper.go create mode 100644 x/service/internal/keeper/querier.go create mode 100644 x/service/internal/types/codec.go create mode 100644 x/service/internal/types/events.go create mode 100644 x/service/internal/types/expected_keepers.go create mode 100644 x/service/internal/types/genesis.go create mode 100644 x/service/internal/types/key.go create mode 100644 x/service/internal/types/msg.go create mode 100644 x/service/internal/types/params.go create mode 100644 x/service/internal/types/querier.go create mode 100644 x/service/module.go diff --git a/app/app.go b/app/app.go index 543ce2c9d..c9669c1f9 100644 --- a/app/app.go +++ b/app/app.go @@ -29,11 +29,11 @@ import ( mesgcodec "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/cosmos" executionsdk "github.com/mesg-foundation/engine/sdk/execution" - servicesdk "github.com/mesg-foundation/engine/sdk/service" "github.com/mesg-foundation/engine/x/instance" "github.com/mesg-foundation/engine/x/ownership" "github.com/mesg-foundation/engine/x/process" "github.com/mesg-foundation/engine/x/runner" + "github.com/mesg-foundation/engine/x/service" ) const appName = "engine" @@ -63,7 +63,7 @@ var ( instance.AppModuleBasic{}, process.AppModuleBasic{}, runner.AppModuleBasic{}, - cosmos.NewAppModuleBasic(servicesdk.ModuleName), + service.AppModuleBasic{}, cosmos.NewAppModuleBasic(executionsdk.ModuleName), ) @@ -119,7 +119,7 @@ type NewApp struct { instanceKeeper instance.Keeper processKeeper process.Keeper runnerKeeper runner.Keeper - serviceKeeper *servicesdk.Keeper + serviceKeeper service.Keeper executionKeeper *executionsdk.Keeper // Module Manager @@ -159,7 +159,7 @@ func NewInitApp( instance.ModuleName, process.ModuleName, runner.ModuleName, - servicesdk.ModuleName, + service.ModuleName, executionsdk.ModuleName, ) @@ -245,7 +245,7 @@ func NewInitApp( app.ownershipKeeper = ownership.NewKeeper(app.cdc, keys[ownership.StoreKey]) app.instanceKeeper = instance.NewKeeper(app.cdc, keys[instance.StoreKey]) app.processKeeper = process.NewKeeper(app.cdc, keys[process.StoreKey], app.instanceKeeper, app.ownershipKeeper) - app.serviceKeeper = servicesdk.NewKeeper(keys[servicesdk.ModuleName], app.ownershipKeeper) + app.serviceKeeper = service.NewKeeper(app.cdc, keys[service.StoreKey], app.ownershipKeeper) app.runnerKeeper = runner.NewKeeper(app.cdc, keys[runner.StoreKey], app.instanceKeeper) app.executionKeeper = executionsdk.NewKeeper(keys[executionsdk.ModuleName], app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper) @@ -264,7 +264,7 @@ func NewInitApp( instance.NewAppModule(app.instanceKeeper), process.NewAppModule(app.processKeeper), runner.NewAppModule(app.runnerKeeper, app.instanceKeeper), - servicesdk.NewModule(app.serviceKeeper), + service.NewAppModule(app.serviceKeeper), executionsdk.NewModule(app.executionKeeper), staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper), @@ -291,7 +291,7 @@ func NewInitApp( instance.ModuleName, process.ModuleName, runner.ModuleName, - servicesdk.ModuleName, + service.ModuleName, executionsdk.ModuleName, supply.ModuleName, diff --git a/sdk/execution/keeper.go b/sdk/execution/keeper.go index 896756579..776d9014e 100644 --- a/sdk/execution/keeper.go +++ b/sdk/execution/keeper.go @@ -10,23 +10,23 @@ import ( "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/protobuf/types" - servicesdk "github.com/mesg-foundation/engine/sdk/service" "github.com/mesg-foundation/engine/x/instance" "github.com/mesg-foundation/engine/x/process" "github.com/mesg-foundation/engine/x/runner" + "github.com/mesg-foundation/engine/x/service" ) // Keeper holds the logic to read and write data. type Keeper struct { storeKey *cosmostypes.KVStoreKey - serviceKeeper *servicesdk.Keeper + serviceKeeper service.Keeper instanceKeeper instance.Keeper runnerKeeper runner.Keeper processKeeper process.Keeper } // NewKeeper initialize a new keeper. -func NewKeeper(storeKey *cosmostypes.KVStoreKey, serviceKeeper *servicesdk.Keeper, instanceKeeper instance.Keeper, runnerKeeper runner.Keeper, processKeeper process.Keeper) *Keeper { +func NewKeeper(storeKey *cosmostypes.KVStoreKey, serviceKeeper service.Keeper, instanceKeeper instance.Keeper, runnerKeeper runner.Keeper, processKeeper process.Keeper) *Keeper { return &Keeper{ storeKey: storeKey, serviceKeeper: serviceKeeper, diff --git a/sdk/service/codec.go b/sdk/service/codec.go deleted file mode 100644 index 90ecf078c..000000000 --- a/sdk/service/codec.go +++ /dev/null @@ -1,9 +0,0 @@ -package servicesdk - -import ( - "github.com/mesg-foundation/engine/codec" -) - -func init() { - codec.RegisterConcrete(msgCreateService{}, "service/create", nil) -} diff --git a/sdk/service/keeper.go b/sdk/service/keeper.go deleted file mode 100644 index 90a356a07..000000000 --- a/sdk/service/keeper.go +++ /dev/null @@ -1,119 +0,0 @@ -package servicesdk - -import ( - "fmt" - - cosmostypes "github.com/cosmos/cosmos-sdk/types" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/hash" - ownershippb "github.com/mesg-foundation/engine/ownership" - "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/service" - "github.com/mesg-foundation/engine/service/validator" - "github.com/mesg-foundation/engine/x/ownership" -) - -// Keeper holds the logic to read and write data. -type Keeper struct { - storeKey *cosmostypes.KVStoreKey - ownershipKeeper ownership.Keeper -} - -// NewKeeper initialize a new keeper. -func NewKeeper(storeKey *cosmostypes.KVStoreKey, ownershipKeeper ownership.Keeper) *Keeper { - return &Keeper{ - storeKey: storeKey, - ownershipKeeper: ownershipKeeper, - } -} - -// Create creates a new service. -func (k *Keeper) Create(request cosmostypes.Request, msg *msgCreateService) (*service.Service, error) { - store := request.KVStore(k.storeKey) - // create service - srv := initializeService(msg.Request) - - // check if service already exists. - if store.Has(srv.Hash) { - return nil, fmt.Errorf("service %q already exists", srv.Hash) - } - - // TODO: the following test should be moved in New function - if srv.Sid == "" { - // make sure that sid doesn't have the same length with id. - srv.Sid = "_" + srv.Hash.String() - } - - if err := validator.ValidateService(srv); err != nil { - return nil, err - } - - if _, err := k.ownershipKeeper.Set(request, msg.Owner, srv.Hash, ownershippb.Ownership_Service); err != nil { - return nil, err - } - - value, err := codec.MarshalBinaryBare(srv) - if err != nil { - return nil, err - } - store.Set(srv.Hash, value) - return srv, nil -} - -// Get returns the service that matches given hash. -func (k *Keeper) Get(request cosmostypes.Request, hash hash.Hash) (*service.Service, error) { - var sv *service.Service - store := request.KVStore(k.storeKey) - if !store.Has(hash) { - return nil, fmt.Errorf("service %q not found", hash) - } - value := store.Get(hash) - return sv, codec.UnmarshalBinaryBare(value, &sv) -} - -// Exists returns true if a specific set of data exists in the database, false otherwise -func (k *Keeper) Exists(request cosmostypes.Request, hash hash.Hash) (bool, error) { - return request.KVStore(k.storeKey).Has(hash), nil -} - -// Hash returns the hash of a service request. -func (k *Keeper) Hash(serviceRequest *api.CreateServiceRequest) hash.Hash { - return initializeService(serviceRequest).Hash -} - -// List returns all services. -func (k *Keeper) List(request cosmostypes.Request) ([]*service.Service, error) { - var ( - services []*service.Service - iter = request.KVStore(k.storeKey).Iterator(nil, nil) - ) - for iter.Valid() { - var sv *service.Service - if err := codec.UnmarshalBinaryBare(iter.Value(), &sv); err != nil { - return nil, err - } - services = append(services, sv) - iter.Next() - } - iter.Close() - return services, nil -} - -func initializeService(req *api.CreateServiceRequest) *service.Service { - // create service - srv := &service.Service{ - Sid: req.Sid, - Name: req.Name, - Description: req.Description, - Configuration: req.Configuration, - Tasks: req.Tasks, - Events: req.Events, - Dependencies: req.Dependencies, - Repository: req.Repository, - Source: req.Source, - } - - // calculate and apply hash to service. - srv.Hash = hash.Dump(srv) - return srv -} diff --git a/sdk/service/module.go b/sdk/service/module.go deleted file mode 100644 index aac85aaf8..000000000 --- a/sdk/service/module.go +++ /dev/null @@ -1,67 +0,0 @@ -package servicesdk - -import ( - "fmt" - - cosmostypes "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/cosmos-sdk/types/module" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/protobuf/api" - abci "github.com/tendermint/tendermint/abci/types" -) - -// ModuleName is the name of this module. -const ModuleName = "service" - -// NewModule returns the module of this sdk. -func NewModule(k *Keeper) module.AppModule { - return cosmos.NewAppModule(cosmos.NewAppModuleBasic(ModuleName), handler(k), querier(k)) -} - -func handler(k *Keeper) cosmos.Handler { - return func(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { - switch msg := msg.(type) { - case msgCreateService: - srv, err := k.Create(request, &msg) - if err != nil { - return nil, cosmos.NewMesgWrapError(cosmos.CodeInternal, err) - } - return srv.Hash, nil - default: - return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "Unrecognized service Msg type: %v", msg.Type()) - } - } -} - -func querier(k *Keeper) cosmos.Querier { - return func(request cosmostypes.Request, path []string, req abci.RequestQuery) (res interface{}, err error) { - switch path[0] { - case "get": - hash, err := hash.Decode(path[1]) - if err != nil { - return nil, err - } - return k.Get(request, hash) - case "list": - return k.List(request) - case "hash": - var createServiceRequest api.CreateServiceRequest - if err := codec.UnmarshalBinaryBare(req.Data, &createServiceRequest); err != nil { - return nil, err - } - return k.Hash(&createServiceRequest), nil - case "exists": - hash, err := hash.Decode(path[1]) - if err != nil { - return nil, err - } - return k.Exists(request, hash) - - default: - return nil, fmt.Errorf("unknown service query endpoint %s", path[0]) - } - } -} diff --git a/sdk/service/msgs.go b/sdk/service/msgs.go deleted file mode 100644 index 6f1c903f2..000000000 --- a/sdk/service/msgs.go +++ /dev/null @@ -1,54 +0,0 @@ -package servicesdk - -import ( - cosmostypes "github.com/cosmos/cosmos-sdk/types" - cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/ext/xvalidator" - "github.com/mesg-foundation/engine/protobuf/api" -) - -// msgCreateService defines a state transition to create a service. -type msgCreateService struct { - Request *api.CreateServiceRequest `json:"request" validate:"required"` - Owner cosmostypes.AccAddress `json:"owner" validate:"required,accaddress"` -} - -// newMsgCreateService is a constructor function for msgCreateService. -func newMsgCreateService(req *api.CreateServiceRequest, owner cosmostypes.AccAddress) *msgCreateService { - return &msgCreateService{ - Request: req, - Owner: owner, - } -} - -// Route should return the name of the module. -func (msg msgCreateService) Route() string { - return ModuleName -} - -// Type returns the action. -func (msg msgCreateService) Type() string { - return "create_service" -} - -// ValidateBasic runs stateless checks on the message. -func (msg msgCreateService) ValidateBasic() error { - if err := xvalidator.Validate.Struct(msg); err != nil { - return err - } - if msg.Owner.Empty() { - return cosmoserrors.Wrap(cosmoserrors.ErrInvalidAddress, "owner is missing") - } - return nil -} - -// GetSignBytes encodes the message for signing. -func (msg msgCreateService) GetSignBytes() []byte { - return cosmostypes.MustSortJSON(codec.MustMarshalJSON(msg)) -} - -// GetSigners defines whose signature is required. -func (msg msgCreateService) GetSigners() []cosmostypes.AccAddress { - return []cosmostypes.AccAddress{msg.Owner} -} diff --git a/sdk/service/sdk.go b/sdk/service/sdk.go index 3ea7d308c..8e2ac18c7 100644 --- a/sdk/service/sdk.go +++ b/sdk/service/sdk.go @@ -1,10 +1,13 @@ package servicesdk import ( + "fmt" + "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/service" + servicepb "github.com/mesg-foundation/engine/service" + "github.com/mesg-foundation/engine/x/service" ) // SDK is the service sdk. @@ -14,19 +17,16 @@ type SDK struct { // New returns the service sdk. func New(client *cosmos.Client) *SDK { - sdk := &SDK{ - client: client, - } - return sdk + return &SDK{client: client} } // Create creates a new service from definition. -func (s *SDK) Create(req *api.CreateServiceRequest) (*service.Service, error) { +func (s *SDK) Create(req *api.CreateServiceRequest) (*servicepb.Service, error) { acc, err := s.client.GetAccount() if err != nil { return nil, err } - msg := newMsgCreateService(req, acc.GetAddress()) + msg := service.NewMsgCreateService(acc.GetAddress(), req) tx, err := s.client.BuildAndBroadcastMsg(msg) if err != nil { return nil, err @@ -35,18 +35,18 @@ func (s *SDK) Create(req *api.CreateServiceRequest) (*service.Service, error) { } // Get returns the service that matches given hash. -func (s *SDK) Get(hash hash.Hash) (*service.Service, error) { - var service service.Service - if err := s.client.Query("custom/"+ModuleName+"/get/"+hash.String(), nil, &service); err != nil { +func (s *SDK) Get(hash hash.Hash) (*servicepb.Service, error) { + var se servicepb.Service + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", service.QuerierRoute, service.QueryGetService, hash), nil, &se); err != nil { return nil, err } - return &service, nil + return &se, nil } // List returns all services. -func (s *SDK) List() ([]*service.Service, error) { - var services []*service.Service - if err := s.client.Query("custom/"+ModuleName+"/list", nil, &services); err != nil { +func (s *SDK) List() ([]*servicepb.Service, error) { + var services []*servicepb.Service + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s", service.QuerierRoute, service.QueryListService), nil, &services); err != nil { return nil, err } return services, nil @@ -55,7 +55,7 @@ func (s *SDK) List() ([]*service.Service, error) { // Exists returns if a service already exists. func (s *SDK) Exists(hash hash.Hash) (bool, error) { var exists bool - if err := s.client.Query("custom/"+ModuleName+"/exists/"+hash.String(), nil, &exists); err != nil { + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", service.QuerierRoute, service.QueryExistService, hash), nil, &exists); err != nil { return false, err } return exists, nil @@ -64,7 +64,7 @@ func (s *SDK) Exists(hash hash.Hash) (bool, error) { // Hash returns the calculate hash of a service. func (s *SDK) Hash(req *api.CreateServiceRequest) (hash.Hash, error) { var h hash.Hash - if err := s.client.Query("custom/"+ModuleName+"/hash", req, &h); err != nil { + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s", service.QuerierRoute, service.QueryHashService), req, &h); err != nil { return nil, err } return h, nil diff --git a/x/service/abci.go b/x/service/abci.go new file mode 100644 index 000000000..f799d1db7 --- /dev/null +++ b/x/service/abci.go @@ -0,0 +1,13 @@ +package service + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// BeginBlocker check for infraction evidence or downtime of validators +// on every begin block +func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k Keeper) {} + +// EndBlocker called every block, process inflation, update validator set. +func EndBlocker(ctx sdk.Context, k Keeper) {} diff --git a/x/service/alias.go b/x/service/alias.go new file mode 100644 index 000000000..24f54257f --- /dev/null +++ b/x/service/alias.go @@ -0,0 +1,43 @@ +package service + +import ( + "github.com/mesg-foundation/engine/x/service/internal/keeper" + "github.com/mesg-foundation/engine/x/service/internal/types" +) + +// const aliases +const ( + ModuleName = types.ModuleName + RouterKey = types.RouterKey + StoreKey = types.StoreKey + DefaultParamspace = types.DefaultParamspace + QuerierRoute = types.QuerierRoute +) + +// functions and variable aliases +var ( + NewKeeper = keeper.NewKeeper + NewQuerier = keeper.NewQuerier + RegisterCodec = types.RegisterCodec + NewGenesisState = types.NewGenesisState + DefaultGenesisState = types.DefaultGenesisState + ValidateGenesis = types.ValidateGenesis + + ModuleCdc = types.ModuleCdc + + QueryGetService = types.QueryGetService + QueryListService = types.QueryListService + QueryHashService = types.QueryHashService + QueryExistService = types.QueryExistService + + NewMsgCreateService = types.NewMsgCreateService +) + +// module types +type ( + Keeper = keeper.Keeper + GenesisState = types.GenesisState + Params = types.Params + + MsgCreateService = types.MsgCreateService +) diff --git a/x/service/client/cli/query.go b/x/service/client/cli/query.go new file mode 100644 index 000000000..bea3e9ac3 --- /dev/null +++ b/x/service/client/cli/query.go @@ -0,0 +1,116 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/service" + "github.com/mesg-foundation/engine/x/service/internal/types" + "github.com/spf13/cobra" +) + +// GetQueryCmd returns the cli query commands for this module +func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { + // Group service queries under a subcommand + serviceQueryCmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + serviceQueryCmd.AddCommand( + flags.GetCommands( + GetCmdGetService(queryRoute, cdc), + GetCmdListService(queryRoute, cdc), + GetCmdHashService(queryRoute, cdc), + GetCmdExistService(queryRoute, cdc), + )..., + ) + + return serviceQueryCmd +} + +func GetCmdGetService(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "get [hash]", + Short: "get", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", queryRoute, types.QueryGetService, args[0]), nil) + if err != nil { + fmt.Printf("could not get service\n%s\n", err.Error()) + return nil + } + + var out *service.Service + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} + +func GetCmdListService(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "list", + Short: "list", + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryListService), nil) + if err != nil { + fmt.Printf("could not list services\n%s\n", err.Error()) + return nil + } + + var out []*service.Service + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} + +func GetCmdHashService(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "hash [definition]", + Short: "hash", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryHashService), []byte(args[0])) + if err != nil { + fmt.Printf("could not check service\n%s\n", err.Error()) + return nil + } + + var out hash.Hash + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} + +func GetCmdExistService(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "exist [hash]", + Short: "exist", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", queryRoute, types.QueryExistService, args[0]), nil) + if err != nil { + fmt.Printf("could not check service\n%s\n", err.Error()) + return nil + } + + var out bool + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} diff --git a/x/service/client/cli/tx.go b/x/service/client/cli/tx.go new file mode 100644 index 000000000..dc3ce6ce7 --- /dev/null +++ b/x/service/client/cli/tx.go @@ -0,0 +1,26 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/mesg-foundation/engine/x/service/internal/types" + "github.com/spf13/cobra" +) + +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + serviceTxCmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + serviceTxCmd.AddCommand(flags.PostCommands()...) + + return serviceTxCmd +} diff --git a/x/service/client/rest/query.go b/x/service/client/rest/query.go new file mode 100644 index 000000000..48d36666c --- /dev/null +++ b/x/service/client/rest/query.go @@ -0,0 +1,122 @@ +package rest + +import ( + "fmt" + "io/ioutil" + "net/http" + + "github.com/gorilla/mux" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/types/rest" + "github.com/mesg-foundation/engine/x/service/internal/types" +) + +func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { + r.HandleFunc( + "/service/get/{hash}", + queryGetHandlerFn(cliCtx), + ).Methods(http.MethodGet) + r.HandleFunc( + "/service/list", + queryListHandlerFn(cliCtx), + ).Methods(http.MethodGet) + r.HandleFunc( + "/service/hash", + queryHashHandlerFn(cliCtx), + ).Methods(http.MethodPost) + r.HandleFunc( + "/service/exist/{hash}", + queryExistHandlerFn(cliCtx), + ).Methods(http.MethodGet) +} + +func queryGetHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryGetService, vars["hash"]) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + +func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryListService) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + +func queryHashHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + data, err := ioutil.ReadAll(r.Body) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryHashService) + + res, height, err := cliCtx.QueryWithData(route, data) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + +func queryExistHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryExistService, vars["hash"]) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} diff --git a/x/service/client/rest/rest.go b/x/service/client/rest/rest.go new file mode 100644 index 000000000..400fdfb2c --- /dev/null +++ b/x/service/client/rest/rest.go @@ -0,0 +1,12 @@ +package rest + +import ( + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/gorilla/mux" +) + +// RegisterRoutes registers service-related REST handlers to a router +func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) { + registerQueryRoutes(cliCtx, r) + registerTxRoutes(cliCtx, r) +} diff --git a/x/service/client/rest/tx.go b/x/service/client/rest/tx.go new file mode 100644 index 000000000..048e7d9e9 --- /dev/null +++ b/x/service/client/rest/tx.go @@ -0,0 +1,10 @@ +package rest + +import ( + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/gorilla/mux" +) + +func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) { + // TODO: +} diff --git a/x/service/genesis.go b/x/service/genesis.go new file mode 100644 index 000000000..87c204348 --- /dev/null +++ b/x/service/genesis.go @@ -0,0 +1,18 @@ +package service + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/mesg-foundation/engine/x/service/internal/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// InitGenesis initialize default parameters and the keeper's address to pubkey map. +func InitGenesis(ctx sdk.Context, k Keeper, data types.GenesisState) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} + +// ExportGenesis writes the current store values // to a genesis file, +// which can be imported again with InitGenesis. +func ExportGenesis(ctx sdk.Context, k Keeper) types.GenesisState { + return types.NewGenesisState() +} diff --git a/x/service/handler.go b/x/service/handler.go new file mode 100644 index 000000000..7cd577b12 --- /dev/null +++ b/x/service/handler.go @@ -0,0 +1,46 @@ +package service + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/x/service/internal/types" +) + +// NewHandler creates an sdk.Handler for all the service type messages +func NewHandler(k Keeper) sdk.Handler { + return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + ctx = ctx.WithEventManager(sdk.NewEventManager()) + switch msg := msg.(type) { + case MsgCreateService: + return handleMsgCreateService(ctx, k, &msg) + default: + errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg) + return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg) + } + } +} + +// handleMsgCreateService creates a new process. +func handleMsgCreateService(ctx sdk.Context, k Keeper, msg *MsgCreateService) (*sdk.Result, error) { + s, err := k.Create(ctx, msg) + if err != nil { + return nil, err + } + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeyAction, types.EventTypeCreateService), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Owner.String()), + sdk.NewAttribute(types.AttributeHash, s.Hash.String()), + ), + ) + + return &sdk.Result{ + Data: s.Hash, + Events: ctx.EventManager().Events(), + }, nil +} diff --git a/x/service/internal/keeper/keeper.go b/x/service/internal/keeper/keeper.go new file mode 100644 index 000000000..0e12d4f40 --- /dev/null +++ b/x/service/internal/keeper/keeper.go @@ -0,0 +1,127 @@ +package keeper + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/mesg-foundation/engine/hash" + ownershippb "github.com/mesg-foundation/engine/ownership" + "github.com/mesg-foundation/engine/protobuf/api" + servicepb "github.com/mesg-foundation/engine/service" + "github.com/mesg-foundation/engine/service/validator" + "github.com/mesg-foundation/engine/x/service/internal/types" + "github.com/tendermint/tendermint/libs/log" +) + +// Keeper of the service store +type Keeper struct { + storeKey sdk.StoreKey + cdc *codec.Codec + ownershipKeeper types.OwnershipKeeper +} + +// NewKeeper creates a service keeper +func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, ownershipKeeper types.OwnershipKeeper) Keeper { + keeper := Keeper{ + storeKey: key, + cdc: cdc, + ownershipKeeper: ownershipKeeper, + } + return keeper +} + +// Logger returns a module-specific logger. +func (k Keeper) Logger(ctx sdk.Context) log.Logger { + return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) +} + +// Create creates a new service. +func (k *Keeper) Create(ctx sdk.Context, msg *types.MsgCreateService) (*servicepb.Service, error) { + store := ctx.KVStore(k.storeKey) + // create service + srv := initializeService(msg.Request) + + // check if service already exists. + if store.Has(srv.Hash) { + return nil, fmt.Errorf("service %q already exists", srv.Hash) + } + + // TODO: the following test should be moved in New function + if srv.Sid == "" { + // make sure that sid doesn't have the same length with id. + srv.Sid = "_" + srv.Hash.String() + } + + if err := validator.ValidateService(srv); err != nil { + return nil, err + } + + if _, err := k.ownershipKeeper.Set(ctx, msg.Owner, srv.Hash, ownershippb.Ownership_Service); err != nil { + return nil, err + } + + value, err := k.cdc.MarshalBinaryLengthPrefixed(srv) + if err != nil { + return nil, err + } + store.Set(srv.Hash, value) + return srv, nil +} + +// Get returns the service that matches given hash. +func (k *Keeper) Get(ctx sdk.Context, hash hash.Hash) (*servicepb.Service, error) { + store := ctx.KVStore(k.storeKey) + if !store.Has(hash) { + return nil, fmt.Errorf("service %q not found", hash) + } + var sv *servicepb.Service + return sv, k.cdc.UnmarshalBinaryLengthPrefixed(store.Get(hash), &sv) +} + +// Exists returns true if a specific set of data exists in the database, false otherwise +func (k *Keeper) Exists(ctx sdk.Context, hash hash.Hash) (bool, error) { + return ctx.KVStore(k.storeKey).Has(hash), nil +} + +// Hash returns the hash of a service request. +func (k *Keeper) Hash(_ sdk.Context, serviceRequest *api.CreateServiceRequest) hash.Hash { + return initializeService(serviceRequest).Hash +} + +// List returns all services. +func (k *Keeper) List(ctx sdk.Context) ([]*servicepb.Service, error) { + var ( + services []*servicepb.Service + iter = ctx.KVStore(k.storeKey).Iterator(nil, nil) + ) + for iter.Valid() { + var sv *servicepb.Service + if err := k.cdc.UnmarshalBinaryLengthPrefixed(iter.Value(), &sv); err != nil { + return nil, err + } + services = append(services, sv) + iter.Next() + } + iter.Close() + return services, nil +} + +func initializeService(req *api.CreateServiceRequest) *servicepb.Service { + // create service + srv := &servicepb.Service{ + Sid: req.Sid, + Name: req.Name, + Description: req.Description, + Configuration: req.Configuration, + Tasks: req.Tasks, + Events: req.Events, + Dependencies: req.Dependencies, + Repository: req.Repository, + Source: req.Source, + } + + // calculate and apply hash to service. + srv.Hash = hash.Dump(srv) + return srv +} diff --git a/x/service/internal/keeper/querier.go b/x/service/internal/keeper/querier.go new file mode 100644 index 000000000..efe59cf7e --- /dev/null +++ b/x/service/internal/keeper/querier.go @@ -0,0 +1,92 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/protobuf/api" + "github.com/mesg-foundation/engine/x/service/internal/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// NewQuerier creates a new querier for service clients. +func NewQuerier(k Keeper) sdk.Querier { + return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) { + switch path[0] { + case types.QueryGetService: + return getService(ctx, k, path[1:]) + case types.QueryListService: + return listService(ctx, k) + case types.QueryHashService: + return hashService(ctx, k, req) + case types.QueryExistService: + return existService(ctx, k, path[1:]) + default: + return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown service query endpoint") + } + } +} + +func getService(ctx sdk.Context, k Keeper, path []string) ([]byte, error) { + hash, err := hash.Decode(path[0]) + if err != nil { + return nil, err + } + + servcie, err := k.Get(ctx, hash) + if err != nil { + return nil, err + } + + res, err := types.ModuleCdc.MarshalJSON(servcie) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return res, nil +} + +func hashService(ctx sdk.Context, k Keeper, req abci.RequestQuery) ([]byte, error) { + var createServiceRequest api.CreateServiceRequest + if err := k.cdc.UnmarshalJSON(req.Data, &createServiceRequest); err != nil { + return nil, err + } + + hash := k.Hash(ctx, &createServiceRequest) + + res, err := types.ModuleCdc.MarshalJSON(hash) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return res, nil +} + +func listService(ctx sdk.Context, k Keeper) ([]byte, error) { + servcies, err := k.List(ctx) + if err != nil { + return nil, err + } + + res, err := types.ModuleCdc.MarshalJSON(servcies) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return res, nil +} + +func existService(ctx sdk.Context, k Keeper, path []string) ([]byte, error) { + hash, err := hash.Decode(path[0]) + if err != nil { + return nil, err + } + + servcie, err := k.Exists(ctx, hash) + if err != nil { + return nil, err + } + + res, err := types.ModuleCdc.MarshalJSON(servcie) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return res, nil +} diff --git a/x/service/internal/types/codec.go b/x/service/internal/types/codec.go new file mode 100644 index 000000000..f660358cb --- /dev/null +++ b/x/service/internal/types/codec.go @@ -0,0 +1,20 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" +) + +// RegisterCodec registers concrete types on codec +func RegisterCodec(cdc *codec.Codec) { + cdc.RegisterConcrete(MsgCreateService{}, "service/CreateService", nil) +} + +// ModuleCdc defines the module codec +var ModuleCdc *codec.Codec + +func init() { + ModuleCdc = codec.New() + RegisterCodec(ModuleCdc) + codec.RegisterCrypto(ModuleCdc) + ModuleCdc.Seal() +} diff --git a/x/service/internal/types/events.go b/x/service/internal/types/events.go new file mode 100644 index 000000000..49046f01c --- /dev/null +++ b/x/service/internal/types/events.go @@ -0,0 +1,10 @@ +package types + +// service module event types +const ( + EventTypeCreateService = "CreateService" + + AttributeHash = "hash" + + AttributeValueCategory = ModuleName +) diff --git a/x/service/internal/types/expected_keepers.go b/x/service/internal/types/expected_keepers.go new file mode 100644 index 000000000..d3e4e4729 --- /dev/null +++ b/x/service/internal/types/expected_keepers.go @@ -0,0 +1,21 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/params" + "github.com/mesg-foundation/engine/hash" + ownershippb "github.com/mesg-foundation/engine/ownership" +) + +// ParamSubspace defines the expected Subspace interfacace +type ParamSubspace interface { + WithKeyTable(table params.KeyTable) params.Subspace + Get(ctx sdk.Context, key []byte, ptr interface{}) + GetParamSet(ctx sdk.Context, ps params.ParamSet) + SetParamSet(ctx sdk.Context, ps params.ParamSet) +} + +// OwnershipKeeper module interface. +type OwnershipKeeper interface { + Set(ctx sdk.Context, owner sdk.AccAddress, resourceHash hash.Hash, resource ownershippb.Ownership_Resource) (*ownershippb.Ownership, error) +} diff --git a/x/service/internal/types/genesis.go b/x/service/internal/types/genesis.go new file mode 100644 index 000000000..bebeec0dc --- /dev/null +++ b/x/service/internal/types/genesis.go @@ -0,0 +1,19 @@ +package types + +// GenesisState - all instance state that must be provided at genesis +type GenesisState struct{} + +// NewGenesisState creates a new GenesisState object +func NewGenesisState() GenesisState { + return GenesisState{} +} + +// DefaultGenesisState - default GenesisState used by Cosmos Hub +func DefaultGenesisState() GenesisState { + return GenesisState{} +} + +// ValidateGenesis validates the instance genesis parameters +func ValidateGenesis(data GenesisState) error { + return nil +} diff --git a/x/service/internal/types/key.go b/x/service/internal/types/key.go new file mode 100644 index 000000000..40bfea37f --- /dev/null +++ b/x/service/internal/types/key.go @@ -0,0 +1,15 @@ +package types + +const ( + // ModuleName is the name of the module + ModuleName = "service" + + // StoreKey to be used when creating the KVStore + StoreKey = ModuleName + + // RouterKey to be used for routing msgs + RouterKey = ModuleName + + // QuerierRoute to be used for routing + QuerierRoute = ModuleName +) diff --git a/x/service/internal/types/msg.go b/x/service/internal/types/msg.go new file mode 100644 index 000000000..4e89520d2 --- /dev/null +++ b/x/service/internal/types/msg.go @@ -0,0 +1,53 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/ext/xvalidator" + "github.com/mesg-foundation/engine/protobuf/api" +) + +// MsgCreateService defines a state transition to create a service. +type MsgCreateService struct { + Owner sdk.AccAddress `json:"owner" validate:"required,accaddress"` + Request *api.CreateServiceRequest `json:"request" validate:"required"` +} + +// NewMsgCreateService is a constructor function for MsgCreateService. +func NewMsgCreateService(owner sdk.AccAddress, request *api.CreateServiceRequest) *MsgCreateService { + return &MsgCreateService{ + Owner: owner, + Request: request, + } +} + +// Route should return the name of the module route. +func (msg MsgCreateService) Route() string { + return RouterKey +} + +// Type returns the action. +func (msg MsgCreateService) Type() string { + return "CreateService" +} + +// ValidateBasic runs stateless checks on the message. +func (msg MsgCreateService) ValidateBasic() error { + if msg.Owner.Empty() { + return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "owner is missing") + } + if err := xvalidator.Validate.Struct(msg); err != nil { + return err + } + return nil +} + +// GetSignBytes encodes the message for signing. +func (msg MsgCreateService) GetSignBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg)) +} + +// GetSigners defines whose signature is required. +func (msg MsgCreateService) GetSigners() []sdk.AccAddress { + return []sdk.AccAddress{msg.Owner} +} diff --git a/x/service/internal/types/params.go b/x/service/internal/types/params.go new file mode 100644 index 000000000..4940992b5 --- /dev/null +++ b/x/service/internal/types/params.go @@ -0,0 +1,38 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/x/params" +) + +// Default parameter namespace +const ( + DefaultParamspace = ModuleName +) + +// ParamKeyTable for instance module +func ParamKeyTable() params.KeyTable { + return params.NewKeyTable().RegisterParamSet(&Params{}) +} + +// Params - used for initializing default parameter for instance at genesis +type Params struct{} + +// NewParams creates a new Params object +func NewParams() Params { + return Params{} +} + +// String implements the stringer interface for Params +func (p Params) String() string { + return "" +} + +// ParamSetPairs - Implements params.ParamSet +func (p *Params) ParamSetPairs() params.ParamSetPairs { + return params.ParamSetPairs{} +} + +// DefaultParams defines the parameters for this module +func DefaultParams() Params { + return NewParams() +} diff --git a/x/service/internal/types/querier.go b/x/service/internal/types/querier.go new file mode 100644 index 000000000..474039158 --- /dev/null +++ b/x/service/internal/types/querier.go @@ -0,0 +1,9 @@ +package types + +// Query endpoints supported by the service querier +const ( + QueryGetService = "get" + QueryListService = "list" + QueryHashService = "hash" + QueryExistService = "exist" +) diff --git a/x/service/module.go b/x/service/module.go new file mode 100644 index 000000000..0504f2978 --- /dev/null +++ b/x/service/module.go @@ -0,0 +1,141 @@ +package service + +import ( + "encoding/json" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/gorilla/mux" + "github.com/mesg-foundation/engine/x/service/client/cli" + "github.com/mesg-foundation/engine/x/service/client/rest" + "github.com/mesg-foundation/engine/x/service/internal/types" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" +) + +var ( + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} +) + +// AppModuleBasic defines the basic application module used by the service module. +type AppModuleBasic struct{} + +var _ module.AppModuleBasic = AppModuleBasic{} + +// Name returns the service module's name. +func (AppModuleBasic) Name() string { + return types.ModuleName +} + +// RegisterCodec registers the service module's types for the given codec. +func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { + RegisterCodec(cdc) +} + +// DefaultGenesis returns default genesis state as raw bytes for the service +// module. +func (AppModuleBasic) DefaultGenesis() json.RawMessage { + return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) +} + +// ValidateGenesis performs genesis state validation for the service module. +func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { + var data GenesisState + err := ModuleCdc.UnmarshalJSON(bz, &data) + if err != nil { + return err + } + return ValidateGenesis(data) +} + +// RegisterRESTRoutes registers the REST routes for the service module. +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { + rest.RegisterRoutes(ctx, rtr) +} + +// GetTxCmd returns the root tx command for the service module. +func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { + return cli.GetTxCmd(cdc) +} + +// GetQueryCmd returns no root query command for the service module. +func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { + return cli.GetQueryCmd(StoreKey, cdc) +} + +//____________________________________________________________________________ + +// AppModule implements an application module for the service module. +type AppModule struct { + AppModuleBasic + + keeper Keeper + // TODO: Add keepers that your application depends on +} + +// NewAppModule creates a new AppModule object +func NewAppModule(k Keeper /*TODO: Add Keepers that your application depends on*/) AppModule { + return AppModule{ + AppModuleBasic: AppModuleBasic{}, + keeper: k, + // TODO: Add keepers that your application depends on + } +} + +// Name returns the service module's name. +func (AppModule) Name() string { + return ModuleName +} + +// RegisterInvariants registers the service module invariants. +func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} + +// Route returns the message routing key for the service module. +func (AppModule) Route() string { + return RouterKey +} + +// NewHandler returns an sdk.Handler for the service module. +func (am AppModule) NewHandler() sdk.Handler { + return NewHandler(am.keeper) +} + +// QuerierRoute returns the service module's querier route name. +func (AppModule) QuerierRoute() string { + return QuerierRoute +} + +// NewQuerierHandler returns the service module sdk.Querier. +func (am AppModule) NewQuerierHandler() sdk.Querier { + return NewQuerier(am.keeper) +} + +// InitGenesis performs genesis initialization for the service module. It returns +// no validator updates. +func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { + var genesisState GenesisState + ModuleCdc.MustUnmarshalJSON(data, &genesisState) + InitGenesis(ctx, am.keeper, genesisState) + return []abci.ValidatorUpdate{} +} + +// ExportGenesis returns the exported genesis state as raw bytes for the service +// module. +func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { + gs := ExportGenesis(ctx, am.keeper) + return ModuleCdc.MustMarshalJSON(gs) +} + +// BeginBlock returns the begin blocker for the service module. +func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { + BeginBlocker(ctx, req, am.keeper) +} + +// EndBlock returns the end blocker for the service module. It returns no validator +// updates. +func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} From 022e3f2d14af612d01eb62dfb1ba43b4d88c9e01 Mon Sep 17 00:00:00 2001 From: krhubert Date: Thu, 13 Feb 2020 22:12:57 +0100 Subject: [PATCH 36/87] Use template to move execution cosmos module to x/ dir --- app/app.go | 34 ++-- codec/codec.go | 50 ----- core/main.go | 13 +- cosmos/client.go | 18 +- cosmos/genesis.go | 20 +- cosmos/genesis_test.go | 16 +- cosmos/module.go | 169 ----------------- protobuf/api/codec.go | 11 +- protobuf/types/codec.go | 19 +- protobuf/types/struct_test.go | 12 +- sdk/execution/codec.go | 8 - sdk/execution/keeper.go | 166 ----------------- sdk/execution/module.go | 58 ------ sdk/execution/msgs.go | 106 ----------- sdk/execution/sdk.go | 39 ++-- x/execution/abci.go | 13 ++ x/execution/alias.go | 45 +++++ x/execution/client/cli/query.go | 73 ++++++++ x/execution/client/cli/tx.go | 25 +++ x/execution/client/rest/query.go | 64 +++++++ x/execution/client/rest/rest.go | 12 ++ x/execution/client/rest/tx.go | 8 + x/execution/genesis.go | 20 ++ x/execution/handler.go | 71 ++++++++ x/execution/internal/keeper/keeper.go | 172 ++++++++++++++++++ .../execution/internal/keeper}/metrics.go | 17 +- x/execution/internal/keeper/querier.go | 54 ++++++ x/execution/internal/types/codec.go | 25 +++ x/execution/internal/types/events.go | 11 ++ .../internal/types/expected_keepers.go | 39 ++++ x/execution/internal/types/genesis.go | 19 ++ x/execution/internal/types/key.go | 15 ++ x/execution/internal/types/msg.go | 102 +++++++++++ x/execution/internal/types/params.go | 38 ++++ x/execution/internal/types/querier.go | 6 + x/execution/module.go | 139 ++++++++++++++ x/process/internal/keeper/keeper.go | 8 +- x/runner/internal/keeper/keeper.go | 8 +- x/service/internal/keeper/keeper.go | 10 +- 39 files changed, 1070 insertions(+), 663 deletions(-) delete mode 100644 codec/codec.go delete mode 100644 cosmos/module.go delete mode 100644 sdk/execution/codec.go delete mode 100644 sdk/execution/keeper.go delete mode 100644 sdk/execution/module.go delete mode 100644 sdk/execution/msgs.go create mode 100644 x/execution/abci.go create mode 100644 x/execution/alias.go create mode 100644 x/execution/client/cli/query.go create mode 100644 x/execution/client/cli/tx.go create mode 100644 x/execution/client/rest/query.go create mode 100644 x/execution/client/rest/rest.go create mode 100644 x/execution/client/rest/tx.go create mode 100644 x/execution/genesis.go create mode 100644 x/execution/handler.go create mode 100644 x/execution/internal/keeper/keeper.go rename {sdk/execution => x/execution/internal/keeper}/metrics.go (82%) create mode 100644 x/execution/internal/keeper/querier.go create mode 100644 x/execution/internal/types/codec.go create mode 100644 x/execution/internal/types/events.go create mode 100644 x/execution/internal/types/expected_keepers.go create mode 100644 x/execution/internal/types/genesis.go create mode 100644 x/execution/internal/types/key.go create mode 100644 x/execution/internal/types/msg.go create mode 100644 x/execution/internal/types/params.go create mode 100644 x/execution/internal/types/querier.go create mode 100644 x/execution/module.go diff --git a/app/app.go b/app/app.go index c9669c1f9..ecf28968d 100644 --- a/app/app.go +++ b/app/app.go @@ -26,9 +26,9 @@ import ( "github.com/cosmos/cosmos-sdk/x/slashing" "github.com/cosmos/cosmos-sdk/x/staking" "github.com/cosmos/cosmos-sdk/x/supply" - mesgcodec "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/cosmos" - executionsdk "github.com/mesg-foundation/engine/sdk/execution" + "github.com/mesg-foundation/engine/protobuf/api" + "github.com/mesg-foundation/engine/protobuf/types" + "github.com/mesg-foundation/engine/x/execution" "github.com/mesg-foundation/engine/x/instance" "github.com/mesg-foundation/engine/x/ownership" "github.com/mesg-foundation/engine/x/process" @@ -64,7 +64,7 @@ var ( process.AppModuleBasic{}, runner.AppModuleBasic{}, service.AppModuleBasic{}, - cosmos.NewAppModuleBasic(executionsdk.ModuleName), + execution.AppModuleBasic{}, ) // module account permissions @@ -79,16 +79,16 @@ var ( // MakeCodec creates the application codec. The codec is sealed before it is // returned. func MakeCodec() *codec.Codec { - // TODO: to change to the cosmos template way - // var cdc = codec.New() + var cdc = codec.New() - ModuleBasics.RegisterCodec(mesgcodec.Codec) - vesting.RegisterCodec(mesgcodec.Codec) - // sdk.RegisterCodec(cdc) - // codec.RegisterCrypto(cdc) + ModuleBasics.RegisterCodec(cdc) + vesting.RegisterCodec(cdc) + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) + types.RegisterCodec(cdc) + api.RegisterCodec(cdc) - // return cdc.Seal() - return mesgcodec.Codec + return cdc.Seal() } // NewApp extended ABCI application @@ -120,7 +120,7 @@ type NewApp struct { processKeeper process.Keeper runnerKeeper runner.Keeper serviceKeeper service.Keeper - executionKeeper *executionsdk.Keeper + executionKeeper execution.Keeper // Module Manager mm *module.Manager @@ -160,7 +160,7 @@ func NewInitApp( process.ModuleName, runner.ModuleName, service.ModuleName, - executionsdk.ModuleName, + execution.ModuleName, ) tKeys := sdk.NewTransientStoreKeys(staking.TStoreKey, params.TStoreKey) @@ -247,7 +247,7 @@ func NewInitApp( app.processKeeper = process.NewKeeper(app.cdc, keys[process.StoreKey], app.instanceKeeper, app.ownershipKeeper) app.serviceKeeper = service.NewKeeper(app.cdc, keys[service.StoreKey], app.ownershipKeeper) app.runnerKeeper = runner.NewKeeper(app.cdc, keys[runner.StoreKey], app.instanceKeeper) - app.executionKeeper = executionsdk.NewKeeper(keys[executionsdk.ModuleName], app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper) + app.executionKeeper = execution.NewKeeper(app.cdc, keys[execution.StoreKey], app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper) // NOTE: Any module instantiated in the module manager that is later modified // must be passed by reference here. @@ -265,7 +265,7 @@ func NewInitApp( process.NewAppModule(app.processKeeper), runner.NewAppModule(app.runnerKeeper, app.instanceKeeper), service.NewAppModule(app.serviceKeeper), - executionsdk.NewModule(app.executionKeeper), + execution.NewAppModule(app.executionKeeper), staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper), ) @@ -292,7 +292,7 @@ func NewInitApp( process.ModuleName, runner.ModuleName, service.ModuleName, - executionsdk.ModuleName, + execution.ModuleName, supply.ModuleName, genutil.ModuleName, diff --git a/codec/codec.go b/codec/codec.go deleted file mode 100644 index 6e0dcb624..000000000 --- a/codec/codec.go +++ /dev/null @@ -1,50 +0,0 @@ -package codec - -import ( - cosmoscodec "github.com/cosmos/cosmos-sdk/codec" - cosmostypes "github.com/cosmos/cosmos-sdk/types" - "github.com/tendermint/go-amino" -) - -// Codec is a general codec where every data structs needs to register to -var Codec = amino.NewCodec() - -func init() { - cosmostypes.RegisterCodec(Codec) - cosmoscodec.RegisterCrypto(Codec) -} - -// RegisterConcrete https://godoc.org/github.com/tendermint/go-amino#Codec.RegisterConcrete -func RegisterConcrete(o interface{}, name string, copts *amino.ConcreteOptions) { - Codec.RegisterConcrete(o, name, copts) -} - -// RegisterInterface https://godoc.org/github.com/tendermint/go-amino#Codec.RegisterInterface -func RegisterInterface(ptr interface{}, iopts *amino.InterfaceOptions) { - Codec.RegisterInterface(ptr, iopts) -} - -// MustMarshalJSON https://godoc.org/github.com/tendermint/go-amino#Codec.MustMarshalJSON -func MustMarshalJSON(o interface{}) []byte { - return Codec.MustMarshalJSON(o) -} - -// MarshalJSON https://godoc.org/github.com/tendermint/go-amino#Codec.MarshalJSON -func MarshalJSON(o interface{}) ([]byte, error) { - return Codec.MarshalJSON(o) -} - -// UnmarshalJSON https://godoc.org/github.com/tendermint/go-amino#Codec.UnmarshalJSON -func UnmarshalJSON(bz []byte, ptr interface{}) error { - return Codec.UnmarshalJSON(bz, ptr) -} - -// UnmarshalBinaryBare https://godoc.org/github.com/tendermint/go-amino#Codec.UnmarshalBinaryBare -func UnmarshalBinaryBare(bz []byte, ptr interface{}) error { - return Codec.UnmarshalBinaryBare(bz, ptr) -} - -// MarshalBinaryBare https://godoc.org/github.com/tendermint/go-amino#Codec.MarshalBinaryBare -func MarshalBinaryBare(o interface{}) ([]byte, error) { - return Codec.MarshalBinaryBare(o) -} diff --git a/core/main.go b/core/main.go index 3b6da53be..6556f4ca4 100644 --- a/core/main.go +++ b/core/main.go @@ -9,11 +9,11 @@ import ( bam "github.com/cosmos/cosmos-sdk/baseapp" cosmosclient "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keys" authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest" "github.com/gorilla/mux" "github.com/mesg-foundation/engine/app" - "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/config" "github.com/mesg-foundation/engine/container" "github.com/mesg-foundation/engine/cosmos" @@ -93,7 +93,7 @@ func loadOrGenConfigAccount(kb *cosmos.Keybase, cfg *config.Config) (keys.Info, return kb.CreateAccount(cfg.Account.Name, mnemonic, "", cfg.Account.Password, keys.CreateHDPath(cfg.Account.Number, cfg.Account.Index).String(), cosmos.DefaultAlgo) } -func loadOrGenDevGenesis(kb *cosmos.Keybase, cfg *config.Config) (*tmtypes.GenesisDoc, error) { +func loadOrGenDevGenesis(cdc *codec.Codec, kb *cosmos.Keybase, cfg *config.Config) (*tmtypes.GenesisDoc, error) { if cosmos.GenesisExist(cfg.Tendermint.Config.GenesisFile()) { return cosmos.LoadGenesis(cfg.Tendermint.Config.GenesisFile()) } @@ -113,7 +113,7 @@ func loadOrGenDevGenesis(kb *cosmos.Keybase, cfg *config.Config) (*tmtypes.Genes "nodeID": validator.NodeID, "peer": fmt.Sprintf("%s@%s:26656", validator.NodeID, validator.Name), }).Warnln("Validator") - return cosmos.GenGenesis(kb, app.NewDefaultGenesisState(), cfg.DevGenesis.ChainID, cfg.DevGenesis.InitialBalances, cfg.DevGenesis.ValidatorDelegationCoin, cfg.Tendermint.Config.GenesisFile(), []cosmos.GenesisValidator{validator}) + return cosmos.GenGenesis(cdc, kb, app.NewDefaultGenesisState(), cfg.DevGenesis.ChainID, cfg.DevGenesis.InitialBalances, cfg.DevGenesis.ValidatorDelegationCoin, cfg.Tendermint.Config.GenesisFile(), []cosmos.GenesisValidator{validator}) } func main() { @@ -149,6 +149,7 @@ func main() { } initApp := app.NewInitApp(tendermintLogger, db, nil, true, 0, bam.SetMinGasPrices(cfg.Cosmos.MinGasPrices)) + cdc := initApp.Codec() // init key manager kb, err := cosmos.NewKeybase(filepath.Join(cfg.Path, cfg.Cosmos.RelativePath)) @@ -164,7 +165,7 @@ func main() { logrus.WithField("address", acc.GetAddress().String()).Info("engine account") // load or gen genesis - genesis, err := loadOrGenDevGenesis(kb, cfg) + genesis, err := loadOrGenDevGenesis(cdc, kb, cfg) if err != nil { logrus.WithField("module", "main").Fatalln(err) } @@ -176,7 +177,7 @@ func main() { } // create cosmos client - client, err := cosmos.NewClient(node, kb, genesis.ChainID, cfg.Account.Name, cfg.Account.Password, cfg.Cosmos.MinGasPrices) + client, err := cosmos.NewClient(node, cdc, kb, genesis.ChainID, cfg.Account.Name, cfg.Account.Password, cfg.Cosmos.MinGasPrices) if err != nil { logrus.WithField("module", "main").Fatalln(err) } @@ -222,7 +223,7 @@ func main() { } cliCtx := context.NewCLIContext(). - WithCodec(codec.Codec). + WithCodec(cdc). WithClient(client). WithTrustNode(true) mux := mux.NewRouter() diff --git a/cosmos/client.go b/cosmos/client.go index 38c67ca19..0bcfc4802 100644 --- a/cosmos/client.go +++ b/cosmos/client.go @@ -8,13 +8,13 @@ import ( "time" "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keys" sdktypes "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" authutils "github.com/cosmos/cosmos-sdk/x/auth/client/utils" authExported "github.com/cosmos/cosmos-sdk/x/auth/exported" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/ext/xreflect" "github.com/mesg-foundation/engine/ext/xstrings" "github.com/mesg-foundation/engine/hash" @@ -27,6 +27,7 @@ import ( // Client is a tendermint client with helper functions. type Client struct { *rpcclient.Local + cdc *codec.Codec kb keys.Keybase chainID string accName string @@ -40,13 +41,14 @@ type Client struct { } // NewClient returns a rpc tendermint client. -func NewClient(node *node.Node, kb keys.Keybase, chainID, accName, accPassword, minGasPrices string) (*Client, error) { +func NewClient(node *node.Node, cdc *codec.Codec, kb keys.Keybase, chainID, accName, accPassword, minGasPrices string) (*Client, error) { minGasPricesDecoded, err := sdktypes.ParseDecCoins(minGasPrices) if err != nil { return nil, err } return &Client{ Local: rpcclient.NewLocal(node), + cdc: cdc, kb: kb, chainID: chainID, accName: accName, @@ -59,7 +61,7 @@ func NewClient(node *node.Node, kb keys.Keybase, chainID, accName, accPassword, func (c *Client) QueryJSON(path string, qdata, ptr interface{}) error { var data []byte if !xreflect.IsNil(qdata) { - b, err := codec.MarshalJSON(qdata) + b, err := c.cdc.MarshalJSON(qdata) if err != nil { return err } @@ -70,14 +72,14 @@ func (c *Client) QueryJSON(path string, qdata, ptr interface{}) error { if err != nil { return err } - return codec.UnmarshalJSON(result, ptr) + return c.cdc.UnmarshalJSON(result, ptr) } // Query is abci.query wrapper with errors check and decode data. func (c *Client) Query(path string, qdata, ptr interface{}) error { var data []byte if !xreflect.IsNil(qdata) { - b, err := codec.MarshalBinaryBare(qdata) + b, err := c.cdc.MarshalBinaryBare(qdata) if err != nil { return err } @@ -87,7 +89,7 @@ func (c *Client) Query(path string, qdata, ptr interface{}) error { if err != nil { return err } - return codec.UnmarshalBinaryBare(result, ptr) + return c.cdc.UnmarshalBinaryBare(result, ptr) } // QueryWithData performs a query to a Tendermint node with the provided path @@ -230,7 +232,7 @@ func (c *Client) createAndSignTx(msgs []sdktypes.Msg) (tenderminttypes.Tx, error // Create TxBuilder txBuilder := authtypes.NewTxBuilder( - authutils.GetTxEncoder(codec.Codec), + authutils.GetTxEncoder(c.cdc), accR.GetAccountNumber(), sequence, flags.DefaultGasLimit, @@ -248,7 +250,7 @@ func (c *Client) createAndSignTx(msgs []sdktypes.Msg) (tenderminttypes.Tx, error if err != nil { return nil, err } - _, adjusted, err := authutils.CalculateGas(c.QueryWithData, codec.Codec, txBytes, txBuilder.GasAdjustment()) + _, adjusted, err := authutils.CalculateGas(c.QueryWithData, c.cdc, txBytes, txBuilder.GasAdjustment()) if err != nil { return nil, err } diff --git a/cosmos/genesis.go b/cosmos/genesis.go index d2fe43be4..5cdc091f1 100644 --- a/cosmos/genesis.go +++ b/cosmos/genesis.go @@ -5,13 +5,13 @@ import ( "os" "time" + "github.com/cosmos/cosmos-sdk/codec" sdktypes "github.com/cosmos/cosmos-sdk/types" authutils "github.com/cosmos/cosmos-sdk/x/auth/client/utils" authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/genutil" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/mesg-foundation/engine/codec" "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/privval" @@ -54,7 +54,7 @@ func LoadGenesis(genesisFile string) (*tmtypes.GenesisDoc, error) { } // GenGenesis generates a new genesis and save it. -func GenGenesis(kb *Keybase, defaultGenesisŚtate map[string]json.RawMessage, chainID, initialBalances, validatorDelegationCoin, genesisFile string, validators []GenesisValidator) (*tmtypes.GenesisDoc, error) { +func GenGenesis(cdc *codec.Codec, kb *Keybase, defaultGenesisŚtate map[string]json.RawMessage, chainID, initialBalances, validatorDelegationCoin, genesisFile string, validators []GenesisValidator) (*tmtypes.GenesisDoc, error) { valDelCoin, err := sdktypes.ParseCoin(validatorDelegationCoin) if err != nil { return nil, err @@ -73,7 +73,7 @@ func GenGenesis(kb *Keybase, defaultGenesisŚtate map[string]json.RawMessage, ch accNumber := uint64(0) sequence := uint64(0) b := authtypes.NewTxBuilder( - authutils.GetTxEncoder(codec.Codec), + authutils.GetTxEncoder(cdc), accNumber, sequence, 0, @@ -96,11 +96,11 @@ func GenGenesis(kb *Keybase, defaultGenesisŚtate map[string]json.RawMessage, ch } } // generate genesis - appState, err := genGenesisAppState(defaultGenesisŚtate, validatorTx, initialBalances) + appState, err := genGenesisAppState(cdc, defaultGenesisŚtate, validatorTx, initialBalances) if err != nil { return nil, err } - genesis, err := genGenesisDoc(appState, chainID, time.Now()) + genesis, err := genGenesisDoc(cdc, appState, chainID, time.Now()) if err != nil { return nil, err } @@ -111,8 +111,8 @@ func GenGenesis(kb *Keybase, defaultGenesisŚtate map[string]json.RawMessage, ch return genesis, nil } -func genGenesisDoc(appState map[string]json.RawMessage, chainID string, genesisTime time.Time) (*tmtypes.GenesisDoc, error) { - appStateEncoded, err := codec.MarshalJSON(appState) +func genGenesisDoc(cdc *codec.Codec, appState map[string]json.RawMessage, chainID string, genesisTime time.Time) (*tmtypes.GenesisDoc, error) { + appStateEncoded, err := cdc.MarshalJSON(appState) if err != nil { return nil, err } @@ -125,7 +125,7 @@ func genGenesisDoc(appState map[string]json.RawMessage, chainID string, genesisT return genesis, genesis.ValidateAndComplete() } -func genGenesisAppState(defaultGenesisŚtate map[string]json.RawMessage, signedStdTx authtypes.StdTx, initialBalances string) (map[string]json.RawMessage, error) { +func genGenesisAppState(cdc *codec.Codec, defaultGenesisŚtate map[string]json.RawMessage, signedStdTx authtypes.StdTx, initialBalances string) (map[string]json.RawMessage, error) { genAccs := authexported.GenesisAccounts{} pubkeys := signedStdTx.GetPubKeys() for i, signer := range signedStdTx.GetSigners() { @@ -139,12 +139,12 @@ func genGenesisAppState(defaultGenesisŚtate map[string]json.RawMessage, signedS } genAccs = append(genAccs, genAcc) } - genstate, err := codec.MarshalJSON(authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)) + genstate, err := cdc.MarshalJSON(authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)) if err != nil { return nil, err } defaultGenesisŚtate[authtypes.ModuleName] = genstate - return genutil.SetGenTxsInAppGenesisState(codec.Codec, defaultGenesisŚtate, []authtypes.StdTx{signedStdTx}) + return genutil.SetGenTxsInAppGenesisState(cdc, defaultGenesisŚtate, []authtypes.StdTx{signedStdTx}) } func genCreateValidatorMsg(accAddress sdktypes.AccAddress, accName string, validatorDelegationCoin sdktypes.Coin, valPubKey crypto.PubKey) stakingtypes.MsgCreateValidator { diff --git a/cosmos/genesis_test.go b/cosmos/genesis_test.go index dceb6b04e..c1f4afb36 100644 --- a/cosmos/genesis_test.go +++ b/cosmos/genesis_test.go @@ -7,17 +7,21 @@ import ( "path/filepath" "testing" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keys" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/mesg-foundation/engine/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/staking" "github.com/stretchr/testify/require" ) func TestGenesis(t *testing.T) { // codec - authtypes.RegisterCodec(codec.Codec) - stakingtypes.RegisterCodec(codec.Codec) + cdc := codec.New() + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) + auth.RegisterCodec(cdc) + staking.RegisterCodec(cdc) // path path, _ := ioutil.TempDir("", "TestGenesis") @@ -59,7 +63,7 @@ func TestGenesis(t *testing.T) { require.False(t, GenesisExist(genesisPath)) }) t.Run("generate genesis", func(t *testing.T) { - genesis, err := GenGenesis(kb, defaultGenesisState, chainID, initialBalances, validatorDelegationCoin, genesisPath, validators) + genesis, err := GenGenesis(cdc, kb, defaultGenesisState, chainID, initialBalances, validatorDelegationCoin, genesisPath, validators) require.NoError(t, err) require.NotEmpty(t, genesis) }) diff --git a/cosmos/module.go b/cosmos/module.go deleted file mode 100644 index 81cd8d77a..000000000 --- a/cosmos/module.go +++ /dev/null @@ -1,169 +0,0 @@ -package cosmos - -import ( - "encoding/json" - - "github.com/cosmos/cosmos-sdk/client/context" - cosmoscodec "github.com/cosmos/cosmos-sdk/codec" - cosmostypes "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/cosmos-sdk/types/module" - "github.com/gorilla/mux" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/hash" - "github.com/spf13/cobra" - abci "github.com/tendermint/tendermint/abci/types" -) - -// type check to ensure the interface is properly implemented -var ( - _ module.AppModule = &AppModule{} - _ module.AppModuleBasic = &AppModuleBasic{} -) - -// ---------------------------------------------- -// AppModuleBasic -// ---------------------------------------------- - -// AppModuleBasic is a basic element of an cosmos app. -type AppModuleBasic struct { - name string -} - -// NewAppModuleBasic inits an AppModuleBasic using a name. -func NewAppModuleBasic(name string) *AppModuleBasic { - return &AppModuleBasic{ - name: name, - } -} - -// Name returns the name of the module. -func (m *AppModuleBasic) Name() string { - return m.name -} - -// RegisterCodec registers the module's structs in the codec. -func (*AppModuleBasic) RegisterCodec(cdc *cosmoscodec.Codec) { -} - -// DefaultGenesis returns the default genesis of the module. -func (*AppModuleBasic) DefaultGenesis() json.RawMessage { - return []byte("{}") -} - -// ValidateGenesis checks a Genesis. -func (*AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { - return nil -} - -// RegisterRESTRoutes registers rest routes -func (*AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {} - -// GetQueryCmd returns the root query command of this module -func (*AppModuleBasic) GetQueryCmd(cdc *cosmoscodec.Codec) *cobra.Command { - return nil -} - -// GetTxCmd returns the root tx command of this module -func (*AppModuleBasic) GetTxCmd(cdc *cosmoscodec.Codec) *cobra.Command { - return nil -} - -// ---------------------------------------------- -// AppModule -// ---------------------------------------------- - -// Handler defines the core of the state transition function of an application. -type Handler func(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) - -// Querier is responsible to answer to ABCI queries. -type Querier func(request cosmostypes.Request, path []string, req abci.RequestQuery) (res interface{}, err error) - -// AppModule is a main element of an cosmos app. -type AppModule struct { - *AppModuleBasic - handler Handler - querier Querier -} - -// NewAppModule inits an AppModule using an AppModuleBasic, Handler and Querier. -func NewAppModule(moduleBasic *AppModuleBasic, handler Handler, querier Querier) *AppModule { - return &AppModule{ - AppModuleBasic: moduleBasic, - handler: handler, - querier: querier, - } -} - -// RegisterInvariants registers invariants to the registry. -func (m *AppModule) RegisterInvariants(ir cosmostypes.InvariantRegistry) {} - -// Route returns the route prefix for transaction of the module. -func (m *AppModule) Route() string { - return m.name -} - -// NewHandler returns the handler used to apply transactions. -func (m AppModule) NewHandler() cosmostypes.Handler { - return func(request cosmostypes.Request, msg cosmostypes.Msg) (*cosmostypes.Result, error) { - request = request.WithEventManager(cosmostypes.NewEventManager()) - hash, err := m.handler(request, msg) - if err != nil { - return nil, err - } - - event := cosmostypes.NewEvent( - cosmostypes.EventTypeMessage, - cosmostypes.NewAttribute(cosmostypes.AttributeKeyModule, m.name), - ) - if hash != nil { - event = event.AppendAttributes( - cosmostypes.NewAttribute(AttributeKeyHash, hash.String()), - ) - } - request.EventManager().EmitEvent(event) - - return &cosmostypes.Result{ - Data: hash, - Events: request.EventManager().Events(), - }, nil - } -} - -// QuerierRoute the route prefix for query of the module. -func (m *AppModule) QuerierRoute() string { - return m.name -} - -// NewQuerierHandler returns the handler used to reply ABCI query. -func (m AppModule) NewQuerierHandler() cosmostypes.Querier { - return func(request cosmostypes.Request, path []string, req abci.RequestQuery) ([]byte, error) { - data, err := m.querier(request, path, req) - if err != nil { - return nil, err - } - res, err := codec.MarshalBinaryBare(data) - if err != nil { - return nil, sdkerrors.Wrap(ErrMarshal, err.Error()) - } - return res, nil - } -} - -// BeginBlock is called at the beginning of the process of a new block. -func (m *AppModule) BeginBlock(_ cosmostypes.Request, _ abci.RequestBeginBlock) {} - -// EndBlock is called at the end of the process of a new block. -func (m *AppModule) EndBlock(cosmostypes.Request, abci.RequestEndBlock) []abci.ValidatorUpdate { - return []abci.ValidatorUpdate{} -} - -// InitGenesis initializes the genesis from a request and data. -func (m *AppModule) InitGenesis(request cosmostypes.Request, data json.RawMessage) []abci.ValidatorUpdate { - return []abci.ValidatorUpdate{} -} - -// ExportGenesis exports the current state of the app. -func (m *AppModule) ExportGenesis(request cosmostypes.Request) json.RawMessage { - return []byte("{}") -} diff --git a/protobuf/api/codec.go b/protobuf/api/codec.go index eaf536293..44269984b 100644 --- a/protobuf/api/codec.go +++ b/protobuf/api/codec.go @@ -1,11 +1,12 @@ package api import ( - "github.com/mesg-foundation/engine/codec" + "github.com/cosmos/cosmos-sdk/codec" ) -func init() { - codec.RegisterInterface((*isUpdateExecutionRequest_Result)(nil), nil) - codec.RegisterConcrete(&UpdateExecutionRequest_Outputs{}, "mesg.api.UpdateExecutionRequest_Outputs", nil) - codec.RegisterConcrete(&UpdateExecutionRequest_Error{}, "mesg.api.UpdateExecutionRequest_Error", nil) +// RegisterCodec registers interface for error. +func RegisterCodec(cdc *codec.Codec) { + cdc.RegisterInterface((*isUpdateExecutionRequest_Result)(nil), nil) + cdc.RegisterConcrete(&UpdateExecutionRequest_Outputs{}, "mesg.api.UpdateExecutionRequest_Outputs", nil) + cdc.RegisterConcrete(&UpdateExecutionRequest_Error{}, "mesg.api.UpdateExecutionRequest_Error", nil) } diff --git a/protobuf/types/codec.go b/protobuf/types/codec.go index 5b35cf4ae..f3e37d74f 100644 --- a/protobuf/types/codec.go +++ b/protobuf/types/codec.go @@ -1,15 +1,16 @@ package types import ( - "github.com/mesg-foundation/engine/codec" + "github.com/cosmos/cosmos-sdk/codec" ) -func init() { - codec.RegisterInterface((*isValue_Kind)(nil), nil) - codec.RegisterConcrete(&Value_NullValue{}, "mesg.types.Value_NullValue", nil) - codec.RegisterConcrete(&Value_NumberValue{}, "mesg.types.Value_NumberValue", nil) - codec.RegisterConcrete(&Value_StringValue{}, "mesg.types.Value_StringValue", nil) - codec.RegisterConcrete(&Value_BoolValue{}, "mesg.types.Value_BoolValue", nil) - codec.RegisterConcrete(&Value_ListValue{}, "mesg.types.Value_ListValue", nil) - codec.RegisterConcrete(&Value_StructValue{}, "mesg.types.Value_StructValue", nil) +// RegisterCodec register struct type in cdc. +func RegisterCodec(cdc *codec.Codec) { + cdc.RegisterInterface((*isValue_Kind)(nil), nil) + cdc.RegisterConcrete(&Value_NullValue{}, "mesg.types.Value_NullValue", nil) + cdc.RegisterConcrete(&Value_NumberValue{}, "mesg.types.Value_NumberValue", nil) + cdc.RegisterConcrete(&Value_StringValue{}, "mesg.types.Value_StringValue", nil) + cdc.RegisterConcrete(&Value_BoolValue{}, "mesg.types.Value_BoolValue", nil) + cdc.RegisterConcrete(&Value_ListValue{}, "mesg.types.Value_ListValue", nil) + cdc.RegisterConcrete(&Value_StructValue{}, "mesg.types.Value_StructValue", nil) } diff --git a/protobuf/types/struct_test.go b/protobuf/types/struct_test.go index 7c6e8d055..0637e1380 100644 --- a/protobuf/types/struct_test.go +++ b/protobuf/types/struct_test.go @@ -3,7 +3,7 @@ package types import ( "testing" - "github.com/mesg-foundation/engine/codec" + "github.com/cosmos/cosmos-sdk/codec" "github.com/mesg-foundation/engine/hash" "github.com/stretchr/testify/require" ) @@ -26,6 +26,8 @@ func TestStructHash(t *testing.T) { } func TestStructMarshal(t *testing.T) { + cdc := codec.New() + RegisterCodec(cdc) var ( structSort1 = &Struct{ Fields: map[string]*Value{ @@ -84,16 +86,16 @@ func TestStructMarshal(t *testing.T) { structUnm2 *Struct ) t.Run("Marshal", func(t *testing.T) { - structValueSort1, err = codec.MarshalBinaryBare(structSort1) + structValueSort1, err = cdc.MarshalBinaryLengthPrefixed(structSort1) require.NoError(t, err) - structValueSort2, err = codec.MarshalBinaryBare(structSort2) + structValueSort2, err = cdc.MarshalBinaryLengthPrefixed(structSort2) require.NoError(t, err) require.True(t, hash.Dump(structValueSort1).Equal(hash.Dump(structValueSort2))) }) t.Run("Unmarshal", func(t *testing.T) { - require.NoError(t, codec.UnmarshalBinaryBare(structValueSort1, &structUnm1)) + require.NoError(t, cdc.UnmarshalBinaryLengthPrefixed(structValueSort1, &structUnm1)) require.True(t, structSort1.Equal(structUnm1)) - require.NoError(t, codec.UnmarshalBinaryBare(structValueSort2, &structUnm2)) + require.NoError(t, cdc.UnmarshalBinaryLengthPrefixed(structValueSort2, &structUnm2)) require.True(t, structSort2.Equal(structUnm2)) require.True(t, structUnm1.Equal(structUnm2)) }) diff --git a/sdk/execution/codec.go b/sdk/execution/codec.go deleted file mode 100644 index 6039f7a40..000000000 --- a/sdk/execution/codec.go +++ /dev/null @@ -1,8 +0,0 @@ -package executionsdk - -import "github.com/mesg-foundation/engine/codec" - -func init() { - codec.RegisterConcrete(msgCreateExecution{}, "execution/create", nil) - codec.RegisterConcrete(msgUpdateExecution{}, "execution/update", nil) -} diff --git a/sdk/execution/keeper.go b/sdk/execution/keeper.go deleted file mode 100644 index 776d9014e..000000000 --- a/sdk/execution/keeper.go +++ /dev/null @@ -1,166 +0,0 @@ -package executionsdk - -import ( - "errors" - "fmt" - - cosmostypes "github.com/cosmos/cosmos-sdk/types" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/execution" - "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/protobuf/types" - "github.com/mesg-foundation/engine/x/instance" - "github.com/mesg-foundation/engine/x/process" - "github.com/mesg-foundation/engine/x/runner" - "github.com/mesg-foundation/engine/x/service" -) - -// Keeper holds the logic to read and write data. -type Keeper struct { - storeKey *cosmostypes.KVStoreKey - serviceKeeper service.Keeper - instanceKeeper instance.Keeper - runnerKeeper runner.Keeper - processKeeper process.Keeper -} - -// NewKeeper initialize a new keeper. -func NewKeeper(storeKey *cosmostypes.KVStoreKey, serviceKeeper service.Keeper, instanceKeeper instance.Keeper, runnerKeeper runner.Keeper, processKeeper process.Keeper) *Keeper { - return &Keeper{ - storeKey: storeKey, - serviceKeeper: serviceKeeper, - instanceKeeper: instanceKeeper, - runnerKeeper: runnerKeeper, - processKeeper: processKeeper, - } -} - -// Create creates a new execution from definition. -func (k *Keeper) Create(request cosmostypes.Request, msg msgCreateExecution) (*execution.Execution, error) { - run, err := k.runnerKeeper.Get(request, msg.Request.ExecutorHash) - if err != nil { - return nil, err - } - inst, err := k.instanceKeeper.Get(request, run.InstanceHash) - if err != nil { - return nil, err - } - srv, err := k.serviceKeeper.Get(request, inst.ServiceHash) - if err != nil { - return nil, err - } - if !msg.Request.ProcessHash.IsZero() { - if _, err := k.processKeeper.Get(request, msg.Request.ProcessHash); err != nil { - return nil, err - } - } - if err := srv.RequireTaskInputs(msg.Request.TaskKey, msg.Request.Inputs); err != nil { - return nil, err - } - exec := execution.New( - msg.Request.ProcessHash, - run.InstanceHash, - msg.Request.ParentHash, - msg.Request.EventHash, - msg.Request.NodeKey, - msg.Request.TaskKey, - msg.Request.Inputs, - msg.Request.Tags, - msg.Request.ExecutorHash, - ) - store := request.KVStore(k.storeKey) - if store.Has(exec.Hash) { - return nil, fmt.Errorf("execution %q already exists", exec.Hash) - } - if err := exec.Execute(); err != nil { - return nil, err - } - value, err := codec.MarshalBinaryBare(exec) - if err != nil { - return nil, err - } - if !request.IsCheckTx() { - m.InProgress.Add(1) - } - store.Set(exec.Hash, value) - return exec, nil -} - -// Update updates a new execution from definition. -func (k *Keeper) Update(request cosmostypes.Request, msg msgUpdateExecution) (*execution.Execution, error) { - store := request.KVStore(k.storeKey) - if !store.Has(msg.Request.Hash) { - return nil, fmt.Errorf("execution %q doesn't exist", msg.Request.Hash) - } - var exec *execution.Execution - if err := codec.UnmarshalBinaryBare(store.Get(msg.Request.Hash), &exec); err != nil { - return nil, err - } - switch res := msg.Request.Result.(type) { - case *api.UpdateExecutionRequest_Outputs: - if err := k.validateExecutionOutput(request, exec.InstanceHash, exec.TaskKey, res.Outputs); err != nil { - if err1 := exec.Failed(err); err1 != nil { - return nil, err1 - } - } else if err := exec.Complete(res.Outputs); err != nil { - return nil, err - } - case *api.UpdateExecutionRequest_Error: - if err := exec.Failed(errors.New(res.Error)); err != nil { - return nil, err - } - default: - return nil, errors.New("no execution result supplied") - } - value, err := codec.MarshalBinaryBare(exec) - if err != nil { - return nil, err - } - if !request.IsCheckTx() { - m.Completed.Add(1) - } - store.Set(exec.Hash, value) - return exec, nil -} - -func (k *Keeper) validateExecutionOutput(request cosmostypes.Request, instanceHash hash.Hash, taskKey string, outputs *types.Struct) error { - inst, err := k.instanceKeeper.Get(request, instanceHash) - if err != nil { - return err - } - srv, err := k.serviceKeeper.Get(request, inst.ServiceHash) - if err != nil { - return err - } - return srv.RequireTaskOutputs(taskKey, outputs) -} - -// Get returns the execution that matches given hash. -func (k *Keeper) Get(request cosmostypes.Request, hash hash.Hash) (*execution.Execution, error) { - var exec *execution.Execution - store := request.KVStore(k.storeKey) - if !store.Has(hash) { - return nil, fmt.Errorf("execution %q not found", hash) - } - return exec, codec.UnmarshalBinaryBare(store.Get(hash), &exec) -} - -// List returns all executions. -func (k *Keeper) List(request cosmostypes.Request) ([]*execution.Execution, error) { - var ( - execs []*execution.Execution - iter = request.KVStore(k.storeKey).Iterator(nil, nil) - ) - for iter.Valid() { - var exec *execution.Execution - value := iter.Value() - if err := codec.UnmarshalBinaryBare(value, &exec); err != nil { - return nil, err - } - execs = append(execs, exec) - iter.Next() - } - iter.Close() - return execs, nil -} diff --git a/sdk/execution/module.go b/sdk/execution/module.go deleted file mode 100644 index af8821c07..000000000 --- a/sdk/execution/module.go +++ /dev/null @@ -1,58 +0,0 @@ -package executionsdk - -import ( - "fmt" - - cosmostypes "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/cosmos-sdk/types/module" - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/hash" - abci "github.com/tendermint/tendermint/abci/types" -) - -// ModuleName is the name of this module. -const ModuleName = "execution" - -// NewModule returns the module of this sdk. -func NewModule(k *Keeper) module.AppModule { - return cosmos.NewAppModule(cosmos.NewAppModuleBasic(ModuleName), handler(k), querier(k)) -} - -func handler(k *Keeper) cosmos.Handler { - return func(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { - switch msg := msg.(type) { - case msgCreateExecution: - exec, err := k.Create(request, msg) - if err != nil { - return nil, err - } - return exec.Hash, nil - case msgUpdateExecution: - exec, err := k.Update(request, msg) - if err != nil { - return nil, err - } - return exec.Hash, nil - default: - return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, fmt.Sprintf("Unrecognized execution Msg type: %v", msg.Type())) - } - } -} - -func querier(k *Keeper) cosmos.Querier { - return func(request cosmostypes.Request, path []string, req abci.RequestQuery) (res interface{}, err error) { - switch path[0] { - case "get": - hash, err := hash.Decode(path[1]) - if err != nil { - return nil, err - } - return k.Get(request, hash) - case "list": - return k.List(request) - default: - return nil, fmt.Errorf("unknown execution query endpoint %s", path[0]) - } - } -} diff --git a/sdk/execution/msgs.go b/sdk/execution/msgs.go deleted file mode 100644 index df1f3bd33..000000000 --- a/sdk/execution/msgs.go +++ /dev/null @@ -1,106 +0,0 @@ -package executionsdk - -import ( - cosmostypes "github.com/cosmos/cosmos-sdk/types" - cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/ext/xvalidator" - "github.com/mesg-foundation/engine/protobuf/api" -) - -// msgCreateExecution defines a state transition to create a execution. -type msgCreateExecution struct { - Request *api.CreateExecutionRequest `json:"request"` - Signer cosmostypes.AccAddress `json:"signer"` -} - -const msgCreateExecutionType = "create_execution" -const msgUpdateExecutionType = "update_execution" - -// newMsgCreateExecution is a constructor function for msgCreateExecution. -func newMsgCreateExecution(req *api.CreateExecutionRequest, signer cosmostypes.AccAddress) *msgCreateExecution { - return &msgCreateExecution{ - Request: req, - Signer: signer, - } -} - -// Route should return the name of the module. -func (msg msgCreateExecution) Route() string { - return ModuleName -} - -// Type returns the action. -func (msg msgCreateExecution) Type() string { - return msgCreateExecutionType -} - -// ValidateBasic runs stateless checks on the message. -func (msg msgCreateExecution) ValidateBasic() error { - if err := xvalidator.Validate.Struct(msg); err != nil { - return err - } - if !msg.Request.ParentHash.IsZero() && !msg.Request.EventHash.IsZero() { - return cosmoserrors.Wrap(cosmos.ErrValidation, "cannot have both parent and event hash") - } - if msg.Request.ParentHash.IsZero() && msg.Request.EventHash.IsZero() { - return cosmoserrors.Wrap(cosmos.ErrValidation, "should have at least an event hash or parent hash") - } - if msg.Request.ExecutorHash.IsZero() { - return cosmoserrors.Wrap(cosmos.ErrValidation, "should have a executor hash") - } - return nil -} - -// GetSignBytes encodes the message for signing. -func (msg msgCreateExecution) GetSignBytes() []byte { - return cosmostypes.MustSortJSON(codec.MustMarshalJSON(msg)) -} - -// GetSigners defines whose signature is required. -func (msg msgCreateExecution) GetSigners() []cosmostypes.AccAddress { - return []cosmostypes.AccAddress{msg.Signer} -} - -// msgUpdateExecution defines a state transition to update a execution. -type msgUpdateExecution struct { - Request *api.UpdateExecutionRequest `json:"request"` - Executor cosmostypes.AccAddress `json:"executor"` -} - -// newMsgUpdateExecution is a constructor function for msgUpdateExecution. -func newMsgUpdateExecution(req *api.UpdateExecutionRequest, executor cosmostypes.AccAddress) *msgUpdateExecution { - return &msgUpdateExecution{ - Request: req, - Executor: executor, - } -} - -// Route should return the name of the module. -func (msg msgUpdateExecution) Route() string { - return ModuleName -} - -// Type returns the action. -func (msg msgUpdateExecution) Type() string { - return msgUpdateExecutionType -} - -// ValidateBasic runs stateless checks on the message. -func (msg msgUpdateExecution) ValidateBasic() error { - if msg.Executor.Empty() { - return cosmoserrors.Wrap(cosmoserrors.ErrInvalidAddress, "executor is missing") - } - return nil -} - -// GetSignBytes encodes the message for signing. -func (msg msgUpdateExecution) GetSignBytes() []byte { - return cosmostypes.MustSortJSON(codec.MustMarshalJSON(msg)) -} - -// GetSigners defines whose signature is required. -func (msg msgUpdateExecution) GetSigners() []cosmostypes.AccAddress { - return []cosmostypes.AccAddress{msg.Executor} -} diff --git a/sdk/execution/sdk.go b/sdk/execution/sdk.go index 6d657ee28..56271a9fb 100644 --- a/sdk/execution/sdk.go +++ b/sdk/execution/sdk.go @@ -2,13 +2,15 @@ package executionsdk import ( "context" + "fmt" "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/execution" + executionpb "github.com/mesg-foundation/engine/execution" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/protobuf/api" runnersdk "github.com/mesg-foundation/engine/sdk/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" + "github.com/mesg-foundation/engine/x/execution" ) // SDK is the execution sdk. @@ -30,13 +32,13 @@ func New(client *cosmos.Client, serviceSDK *servicesdk.SDK, runnerSDK *runnersdk } // Create creates a new execution. -func (s *SDK) Create(req *api.CreateExecutionRequest) (*execution.Execution, error) { - m.Created.Add(1) +func (s *SDK) Create(req *api.CreateExecutionRequest) (*executionpb.Execution, error) { + execution.M.Created.Add(1) acc, err := s.client.GetAccount() if err != nil { return nil, err } - msg := newMsgCreateExecution(req, acc.GetAddress()) + msg := execution.NewMsgCreateExecution(req, acc.GetAddress()) tx, err := s.client.BuildAndBroadcastMsg(msg) if err != nil { return nil, err @@ -45,13 +47,13 @@ func (s *SDK) Create(req *api.CreateExecutionRequest) (*execution.Execution, err } // Update updates a execution. -func (s *SDK) Update(req *api.UpdateExecutionRequest) (*execution.Execution, error) { - m.Updated.Add(1) +func (s *SDK) Update(req *api.UpdateExecutionRequest) (*executionpb.Execution, error) { + execution.M.Updated.Add(1) acc, err := s.client.GetAccount() if err != nil { return nil, err } - msg := newMsgUpdateExecution(req, acc.GetAddress()) + msg := execution.NewMsgUpdateExecution(req, acc.GetAddress()) tx, err := s.client.BuildAndBroadcastMsg(msg) if err != nil { return nil, err @@ -60,35 +62,35 @@ func (s *SDK) Update(req *api.UpdateExecutionRequest) (*execution.Execution, err } // Get returns the execution that matches given hash. -func (s *SDK) Get(hash hash.Hash) (*execution.Execution, error) { - var execution execution.Execution - if err := s.client.Query("custom/"+ModuleName+"/get/"+hash.String(), nil, &execution); err != nil { +func (s *SDK) Get(hash hash.Hash) (*executionpb.Execution, error) { + var e executionpb.Execution + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", execution.QuerierRoute, execution.QueryGetExecution, hash.String()), nil, &e); err != nil { return nil, err } - return &execution, nil + return &e, nil } // List returns all executions. -func (s *SDK) List() ([]*execution.Execution, error) { - var executions []*execution.Execution - if err := s.client.Query("custom/"+ModuleName+"/list", nil, &executions); err != nil { +func (s *SDK) List() ([]*executionpb.Execution, error) { + var es []*executionpb.Execution + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s", execution.QuerierRoute, execution.QueryListExecution), nil, &es); err != nil { return nil, err } - return executions, nil + return es, nil } // Stream returns execution that matches given hash. -func (s *SDK) Stream(ctx context.Context, req *api.StreamExecutionRequest) (chan *execution.Execution, chan error, error) { +func (s *SDK) Stream(ctx context.Context, req *api.StreamExecutionRequest) (chan *executionpb.Execution, chan error, error) { if err := req.Filter.Validate(); err != nil { return nil, nil, err } - stream, serrC, err := s.client.Stream(ctx, cosmos.EventModuleQuery(ModuleName)) + stream, serrC, err := s.client.Stream(ctx, cosmos.EventModuleQuery(execution.ModuleName)) if err != nil { return nil, nil, err } - execC := make(chan *execution.Execution) + execC := make(chan *executionpb.Execution) errC := make(chan error) go func() { loop: @@ -98,6 +100,7 @@ func (s *SDK) Stream(ctx context.Context, req *api.StreamExecutionRequest) (chan exec, err := s.Get(hash) if err != nil { errC <- err + break } if req.Filter.Match(exec) { execC <- exec diff --git a/x/execution/abci.go b/x/execution/abci.go new file mode 100644 index 000000000..5604a39ce --- /dev/null +++ b/x/execution/abci.go @@ -0,0 +1,13 @@ +package execution + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// BeginBlocker check for infraction evidence or downtime of validators +// on every begin block +func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k Keeper) {} + +// EndBlocker called every block, process inflation, update validator set. +func EndBlocker(ctx sdk.Context, k Keeper) {} diff --git a/x/execution/alias.go b/x/execution/alias.go new file mode 100644 index 000000000..4b7dd5249 --- /dev/null +++ b/x/execution/alias.go @@ -0,0 +1,45 @@ +package execution + +import ( + "github.com/mesg-foundation/engine/x/execution/internal/keeper" + "github.com/mesg-foundation/engine/x/execution/internal/types" +) + +// const aliases +const ( + ModuleName = types.ModuleName + RouterKey = types.RouterKey + StoreKey = types.StoreKey + DefaultParamspace = types.DefaultParamspace + QuerierRoute = types.QuerierRoute +) + +// functions and variable aliases +var ( + NewKeeper = keeper.NewKeeper + NewQuerier = keeper.NewQuerier + RegisterCodec = types.RegisterCodec + NewGenesisState = types.NewGenesisState + DefaultGenesisState = types.DefaultGenesisState + ValidateGenesis = types.ValidateGenesis + + ModuleCdc = types.ModuleCdc + + QueryGetExecution = types.QueryGetExecution + QueryListExecution = types.QueryListExecution + + NewMsgCreateExecution = types.NewMsgCreateExecution + NewMsgUpdateExecution = types.NewMsgUpdateExecution + + M = keeper.M +) + +// module types +type ( + Keeper = keeper.Keeper + GenesisState = types.GenesisState + Params = types.Params + + MsgCreateExecution = types.MsgCreateExecution + MsgUpdateExecution = types.MsgUpdateExecution +) diff --git a/x/execution/client/cli/query.go b/x/execution/client/cli/query.go new file mode 100644 index 000000000..68af79c92 --- /dev/null +++ b/x/execution/client/cli/query.go @@ -0,0 +1,73 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/mesg-foundation/engine/execution" + "github.com/mesg-foundation/engine/x/execution/internal/types" + "github.com/spf13/cobra" +) + +// GetQueryCmd returns the cli query commands for this module +func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { + // Group execution queries under a subcommand + executionQueryCmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + executionQueryCmd.AddCommand( + flags.GetCommands( + GetCmdGetExecution(queryRoute, cdc), + GetCmdListExecution(queryRoute, cdc), + )..., + ) + + return executionQueryCmd +} + +func GetCmdGetExecution(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "get [hash]", + Short: "get", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", queryRoute, types.QueryGetExecution, args[0]), nil) + if err != nil { + fmt.Printf("could not get execution\n%s\n", err.Error()) + return nil + } + + var out *execution.Execution + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} + +func GetCmdListExecution(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "list", + Short: "list", + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryListExecution), nil) + if err != nil { + fmt.Printf("could not list services\n%s\n", err.Error()) + return nil + } + + var out []*execution.Execution + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} diff --git a/x/execution/client/cli/tx.go b/x/execution/client/cli/tx.go new file mode 100644 index 000000000..acb49edce --- /dev/null +++ b/x/execution/client/cli/tx.go @@ -0,0 +1,25 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/mesg-foundation/engine/x/execution/internal/types" + "github.com/spf13/cobra" +) + +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + executionTxCmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + executionTxCmd.AddCommand(flags.PostCommands()...) + return executionTxCmd +} diff --git a/x/execution/client/rest/query.go b/x/execution/client/rest/query.go new file mode 100644 index 000000000..2f6603861 --- /dev/null +++ b/x/execution/client/rest/query.go @@ -0,0 +1,64 @@ +package rest + +import ( + "fmt" + "net/http" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/types/rest" + "github.com/gorilla/mux" + "github.com/mesg-foundation/engine/x/execution/internal/types" +) + +func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { + r.HandleFunc( + "/service/get/{hash}", + queryGetHandlerFn(cliCtx), + ).Methods(http.MethodGet) + r.HandleFunc( + "/service/list", + queryListHandlerFn(cliCtx), + ).Methods(http.MethodGet) +} + +func queryGetHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryGetExecution, vars["hash"]) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + +func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryListExecution) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} diff --git a/x/execution/client/rest/rest.go b/x/execution/client/rest/rest.go new file mode 100644 index 000000000..4b7629486 --- /dev/null +++ b/x/execution/client/rest/rest.go @@ -0,0 +1,12 @@ +package rest + +import ( + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/gorilla/mux" +) + +// RegisterRoutes registers execution-related REST handlers to a router +func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) { + registerQueryRoutes(cliCtx, r) + registerTxRoutes(cliCtx, r) +} diff --git a/x/execution/client/rest/tx.go b/x/execution/client/rest/tx.go new file mode 100644 index 000000000..14207f21a --- /dev/null +++ b/x/execution/client/rest/tx.go @@ -0,0 +1,8 @@ +package rest + +import ( + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/gorilla/mux" +) + +func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {} diff --git a/x/execution/genesis.go b/x/execution/genesis.go new file mode 100644 index 000000000..6ec641d75 --- /dev/null +++ b/x/execution/genesis.go @@ -0,0 +1,20 @@ +package execution + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/mesg-foundation/engine/x/execution/internal/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// InitGenesis initialize default parameters +// and the keeper's address to pubkey map +func InitGenesis(ctx sdk.Context, k Keeper, data types.GenesisState) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} + +// ExportGenesis writes the current store values +// to a genesis file, which can be imported again +// with InitGenesis +func ExportGenesis(ctx sdk.Context, k Keeper) (data types.GenesisState) { + return types.NewGenesisState() +} diff --git a/x/execution/handler.go b/x/execution/handler.go new file mode 100644 index 000000000..b90aa3dd9 --- /dev/null +++ b/x/execution/handler.go @@ -0,0 +1,71 @@ +package execution + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/x/execution/internal/types" +) + +// NewHandler creates an sdk.Handler for all the execution type messages +func NewHandler(k Keeper) sdk.Handler { + return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + ctx = ctx.WithEventManager(sdk.NewEventManager()) + switch msg := msg.(type) { + case MsgCreateExecution: + return handleMsgCreateExecution(ctx, k, msg) + case MsgUpdateExecution: + return handleMsgUpdateExecution(ctx, k, msg) + default: + errMsg := fmt.Sprintf("unrecognized %s message type: %T", ModuleName, msg) + return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg) + } + } +} + +// handleMsgCreateExecution creates a new process. +func handleMsgCreateExecution(ctx sdk.Context, k Keeper, msg MsgCreateExecution) (*sdk.Result, error) { + s, err := k.Create(ctx, msg) + if err != nil { + return nil, err + } + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeyAction, types.EventTypeCreateExecution), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Signer.String()), + sdk.NewAttribute(types.AttributeHash, s.Hash.String()), + ), + ) + + return &sdk.Result{ + Data: s.Hash, + Events: ctx.EventManager().Events(), + }, nil +} + +// handleMsgUpdateExecution updates an execution. +func handleMsgUpdateExecution(ctx sdk.Context, k Keeper, msg MsgUpdateExecution) (*sdk.Result, error) { + s, err := k.Update(ctx, msg) + if err != nil { + return nil, err + } + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeyAction, types.EventTypeUpdateExecution), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Executor.String()), + sdk.NewAttribute(types.AttributeHash, s.Hash.String()), + ), + ) + + return &sdk.Result{ + Data: s.Hash, + Events: ctx.EventManager().Events(), + }, nil +} diff --git a/x/execution/internal/keeper/keeper.go b/x/execution/internal/keeper/keeper.go new file mode 100644 index 000000000..68fb19540 --- /dev/null +++ b/x/execution/internal/keeper/keeper.go @@ -0,0 +1,172 @@ +package keeper + +import ( + "errors" + "fmt" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + executionpb "github.com/mesg-foundation/engine/execution" + "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/protobuf/api" + typespb "github.com/mesg-foundation/engine/protobuf/types" + "github.com/mesg-foundation/engine/x/execution/internal/types" + "github.com/tendermint/tendermint/libs/log" +) + +// Keeper of the execution store +type Keeper struct { + storeKey sdk.StoreKey + cdc *codec.Codec + serviceKeeper types.ServiceKeeper + instanceKeeper types.InstanceKeeper + runnerKeeper types.RunnerKeeper + processKeeper types.ProcessKeeper +} + +// NewKeeper creates a execution keeper +func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, serviceKeeper types.ServiceKeeper, instanceKeeper types.InstanceKeeper, runnerKeeper types.RunnerKeeper, processKeeper types.ProcessKeeper) Keeper { + keeper := Keeper{ + storeKey: key, + cdc: cdc, + serviceKeeper: serviceKeeper, + instanceKeeper: instanceKeeper, + runnerKeeper: runnerKeeper, + processKeeper: processKeeper, + } + return keeper +} + +// Logger returns a module-specific logger. +func (k Keeper) Logger(ctx sdk.Context) log.Logger { + return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) +} + +// Create creates a new execution from definition. +func (k *Keeper) Create(ctx sdk.Context, msg types.MsgCreateExecution) (*executionpb.Execution, error) { + run, err := k.runnerKeeper.Get(ctx, msg.Request.ExecutorHash) + if err != nil { + return nil, err + } + inst, err := k.instanceKeeper.Get(ctx, run.InstanceHash) + if err != nil { + return nil, err + } + srv, err := k.serviceKeeper.Get(ctx, inst.ServiceHash) + if err != nil { + return nil, err + } + if !msg.Request.ProcessHash.IsZero() { + if _, err := k.processKeeper.Get(ctx, msg.Request.ProcessHash); err != nil { + return nil, err + } + } + if err := srv.RequireTaskInputs(msg.Request.TaskKey, msg.Request.Inputs); err != nil { + return nil, err + } + exec := executionpb.New( + msg.Request.ProcessHash, + run.InstanceHash, + msg.Request.ParentHash, + msg.Request.EventHash, + msg.Request.NodeKey, + msg.Request.TaskKey, + msg.Request.Inputs, + msg.Request.Tags, + msg.Request.ExecutorHash, + ) + store := ctx.KVStore(k.storeKey) + if store.Has(exec.Hash) { + return nil, fmt.Errorf("execution %q already exists", exec.Hash) + } + if err := exec.Execute(); err != nil { + return nil, err + } + value, err := k.cdc.MarshalBinaryLengthPrefixed(exec) + if err != nil { + return nil, err + } + if !ctx.IsCheckTx() { + M.InProgress.Add(1) + } + store.Set(exec.Hash, value) + return exec, nil +} + +// Update updates a new execution from definition. +func (k *Keeper) Update(ctx sdk.Context, msg types.MsgUpdateExecution) (*executionpb.Execution, error) { + store := ctx.KVStore(k.storeKey) + if !store.Has(msg.Request.Hash) { + return nil, fmt.Errorf("execution %q doesn't exist", msg.Request.Hash) + } + var exec *executionpb.Execution + if err := k.cdc.UnmarshalBinaryLengthPrefixed(store.Get(msg.Request.Hash), &exec); err != nil { + return nil, err + } + switch res := msg.Request.Result.(type) { + case *api.UpdateExecutionRequest_Outputs: + if err := k.validateExecutionOutput(ctx, exec.InstanceHash, exec.TaskKey, res.Outputs); err != nil { + if err1 := exec.Failed(err); err1 != nil { + return nil, err1 + } + } else if err := exec.Complete(res.Outputs); err != nil { + return nil, err + } + case *api.UpdateExecutionRequest_Error: + if err := exec.Failed(errors.New(res.Error)); err != nil { + return nil, err + } + default: + return nil, errors.New("no execution result supplied") + } + value, err := k.cdc.MarshalBinaryLengthPrefixed(exec) + if err != nil { + return nil, err + } + if !ctx.IsCheckTx() { + M.Completed.Add(1) + } + store.Set(exec.Hash, value) + return exec, nil +} + +func (k *Keeper) validateExecutionOutput(ctx sdk.Context, instanceHash hash.Hash, taskKey string, outputs *typespb.Struct) error { + inst, err := k.instanceKeeper.Get(ctx, instanceHash) + if err != nil { + return err + } + srv, err := k.serviceKeeper.Get(ctx, inst.ServiceHash) + if err != nil { + return err + } + return srv.RequireTaskOutputs(taskKey, outputs) +} + +// Get returns the execution that matches given hash. +func (k *Keeper) Get(ctx sdk.Context, hash hash.Hash) (*executionpb.Execution, error) { + var exec *executionpb.Execution + store := ctx.KVStore(k.storeKey) + if !store.Has(hash) { + return nil, fmt.Errorf("execution %q not found", hash) + } + return exec, k.cdc.UnmarshalBinaryLengthPrefixed(store.Get(hash), &exec) +} + +// List returns all executions. +func (k *Keeper) List(ctx sdk.Context) ([]*executionpb.Execution, error) { + var ( + execs []*executionpb.Execution + iter = ctx.KVStore(k.storeKey).Iterator(nil, nil) + ) + for iter.Valid() { + var exec *executionpb.Execution + value := iter.Value() + if err := k.cdc.UnmarshalBinaryLengthPrefixed(value, &exec); err != nil { + return nil, err + } + execs = append(execs, exec) + iter.Next() + } + iter.Close() + return execs, nil +} diff --git a/sdk/execution/metrics.go b/x/execution/internal/keeper/metrics.go similarity index 82% rename from sdk/execution/metrics.go rename to x/execution/internal/keeper/metrics.go index f763d09e8..db15d030d 100644 --- a/sdk/execution/metrics.go +++ b/x/execution/internal/keeper/metrics.go @@ -1,4 +1,4 @@ -package executionsdk +package keeper import ( "github.com/go-kit/kit/metrics" @@ -6,17 +6,20 @@ import ( stdprometheus "github.com/prometheus/client_golang/prometheus" ) -var m *metric +// M is global metric variable. +var M = NewMetric() -type metric struct { +// Metric is struct to keep counters for execution statuses. +type Metric struct { Created metrics.Counter InProgress metrics.Counter Updated metrics.Counter Completed metrics.Counter } -func newMetric() *metric { - return &metric{ +// NewMetric creates a counters metric. +func NewMetric() *Metric { + return &Metric{ Created: prometheus.NewCounterFrom(stdprometheus.CounterOpts{ Namespace: "mesg", Subsystem: "execution", @@ -43,7 +46,3 @@ func newMetric() *metric { }, []string{}), } } - -func init() { - m = newMetric() -} diff --git a/x/execution/internal/keeper/querier.go b/x/execution/internal/keeper/querier.go new file mode 100644 index 000000000..570515ae8 --- /dev/null +++ b/x/execution/internal/keeper/querier.go @@ -0,0 +1,54 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/x/execution/internal/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// NewQuerier creates a new querier for execution clients. +func NewQuerier(k Keeper) sdk.Querier { + return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) { + switch path[0] { + case types.QueryGetExecution: + return getExecution(ctx, k, path[1:]) + case types.QueryListExecution: + return listExecution(ctx, k) + default: + return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown execution query endpoint") + } + } +} + +func getExecution(ctx sdk.Context, k Keeper, path []string) ([]byte, error) { + hash, err := hash.Decode(path[0]) + if err != nil { + return nil, err + } + + e, err := k.Get(ctx, hash) + if err != nil { + return nil, err + } + + res, err := types.ModuleCdc.MarshalJSON(e) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return res, nil +} + +func listExecution(ctx sdk.Context, k Keeper) ([]byte, error) { + es, err := k.List(ctx) + if err != nil { + return nil, err + } + + res, err := types.ModuleCdc.MarshalJSON(es) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return res, nil +} diff --git a/x/execution/internal/types/codec.go b/x/execution/internal/types/codec.go new file mode 100644 index 000000000..f573c4922 --- /dev/null +++ b/x/execution/internal/types/codec.go @@ -0,0 +1,25 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" + "github.com/mesg-foundation/engine/protobuf/api" + "github.com/mesg-foundation/engine/protobuf/types" +) + +// RegisterCodec registers concrete types on codec +func RegisterCodec(cdc *codec.Codec) { + cdc.RegisterConcrete(MsgCreateExecution{}, "execution/CreateExecution", nil) + cdc.RegisterConcrete(MsgUpdateExecution{}, "execution/UpdateExecution", nil) +} + +// ModuleCdc defines the module codec +var ModuleCdc *codec.Codec + +func init() { + ModuleCdc = codec.New() + RegisterCodec(ModuleCdc) + types.RegisterCodec(ModuleCdc) + api.RegisterCodec(ModuleCdc) + codec.RegisterCrypto(ModuleCdc) + ModuleCdc.Seal() +} diff --git a/x/execution/internal/types/events.go b/x/execution/internal/types/events.go new file mode 100644 index 000000000..fd6e654f3 --- /dev/null +++ b/x/execution/internal/types/events.go @@ -0,0 +1,11 @@ +package types + +// execution module event types +const ( + EventTypeCreateExecution = "CreateExeuction" + EventTypeUpdateExecution = "UpdateExeuction" + + AttributeHash = "hash" + + AttributeValueCategory = ModuleName +) diff --git a/x/execution/internal/types/expected_keepers.go b/x/execution/internal/types/expected_keepers.go new file mode 100644 index 000000000..74acb81f9 --- /dev/null +++ b/x/execution/internal/types/expected_keepers.go @@ -0,0 +1,39 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/params" + "github.com/mesg-foundation/engine/hash" + instancepb "github.com/mesg-foundation/engine/instance" + processpb "github.com/mesg-foundation/engine/process" + runnerpb "github.com/mesg-foundation/engine/runner" + servicepb "github.com/mesg-foundation/engine/service" +) + +// ParamSubspace defines the expected Subspace interfacace +type ParamSubspace interface { + WithKeyTable(table params.KeyTable) params.Subspace + Get(ctx sdk.Context, key []byte, ptr interface{}) + GetParamSet(ctx sdk.Context, ps params.ParamSet) + SetParamSet(ctx sdk.Context, ps params.ParamSet) +} + +// ServiceKeeper module interface. +type ServiceKeeper interface { + Get(ctx sdk.Context, hash hash.Hash) (*servicepb.Service, error) +} + +// InstanceKeeper module interface. +type InstanceKeeper interface { + Get(ctx sdk.Context, instanceHash hash.Hash) (*instancepb.Instance, error) +} + +// RunnerKeeper module interface. +type RunnerKeeper interface { + Get(ctx sdk.Context, hash hash.Hash) (*runnerpb.Runner, error) +} + +// ProcessKeeper module interface. +type ProcessKeeper interface { + Get(ctx sdk.Context, hash hash.Hash) (*processpb.Process, error) +} diff --git a/x/execution/internal/types/genesis.go b/x/execution/internal/types/genesis.go new file mode 100644 index 000000000..bebeec0dc --- /dev/null +++ b/x/execution/internal/types/genesis.go @@ -0,0 +1,19 @@ +package types + +// GenesisState - all instance state that must be provided at genesis +type GenesisState struct{} + +// NewGenesisState creates a new GenesisState object +func NewGenesisState() GenesisState { + return GenesisState{} +} + +// DefaultGenesisState - default GenesisState used by Cosmos Hub +func DefaultGenesisState() GenesisState { + return GenesisState{} +} + +// ValidateGenesis validates the instance genesis parameters +func ValidateGenesis(data GenesisState) error { + return nil +} diff --git a/x/execution/internal/types/key.go b/x/execution/internal/types/key.go new file mode 100644 index 000000000..c7a14f938 --- /dev/null +++ b/x/execution/internal/types/key.go @@ -0,0 +1,15 @@ +package types + +const ( + // ModuleName is the name of the module + ModuleName = "execution" + + // StoreKey to be used when creating the KVStore + StoreKey = ModuleName + + // RouterKey to be used for routing msgs + RouterKey = ModuleName + + // QuerierRoute to be used for routing + QuerierRoute = ModuleName +) diff --git a/x/execution/internal/types/msg.go b/x/execution/internal/types/msg.go new file mode 100644 index 000000000..304de2bb4 --- /dev/null +++ b/x/execution/internal/types/msg.go @@ -0,0 +1,102 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/cosmos" + "github.com/mesg-foundation/engine/ext/xvalidator" + "github.com/mesg-foundation/engine/protobuf/api" +) + +// MsgCreateExecution defines a state transition to create a execution. +type MsgCreateExecution struct { + Request *api.CreateExecutionRequest `json:"request"` + Signer sdk.AccAddress `json:"signer"` +} + +// NewMsgCreateExecution is a constructor function for MsgCreateExecution. +func NewMsgCreateExecution(req *api.CreateExecutionRequest, signer sdk.AccAddress) *MsgCreateExecution { + return &MsgCreateExecution{ + Request: req, + Signer: signer, + } +} + +// Route should return the name of the module. +func (msg MsgCreateExecution) Route() string { + return ModuleName +} + +// Type returns the action. +func (msg MsgCreateExecution) Type() string { + return "CreateExecution" +} + +// ValidateBasic runs stateless checks on the message. +func (msg MsgCreateExecution) ValidateBasic() error { + if err := xvalidator.Validate.Struct(msg); err != nil { + return err + } + if !msg.Request.ParentHash.IsZero() && !msg.Request.EventHash.IsZero() { + return sdkerrors.Wrap(cosmos.ErrValidation, "cannot have both parent and event hash") + } + if msg.Request.ParentHash.IsZero() && msg.Request.EventHash.IsZero() { + return sdkerrors.Wrap(cosmos.ErrValidation, "should have at least an event hash or parent hash") + } + if msg.Request.ExecutorHash.IsZero() { + return sdkerrors.Wrap(cosmos.ErrValidation, "should have a executor hash") + } + return nil +} + +// GetSignBytes encodes the message for signing. +func (msg MsgCreateExecution) GetSignBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg)) +} + +// GetSigners defines whose signature is required. +func (msg MsgCreateExecution) GetSigners() []sdk.AccAddress { + return []sdk.AccAddress{msg.Signer} +} + +// MsgUpdateExecution defines a state transition to update a execution. +type MsgUpdateExecution struct { + Request *api.UpdateExecutionRequest `json:"request"` + Executor sdk.AccAddress `json:"executor"` +} + +// NewMsgUpdateExecution is a constructor function for MsgUpdateExecution. +func NewMsgUpdateExecution(req *api.UpdateExecutionRequest, executor sdk.AccAddress) *MsgUpdateExecution { + return &MsgUpdateExecution{ + Request: req, + Executor: executor, + } +} + +// Route should return the name of the module. +func (msg MsgUpdateExecution) Route() string { + return ModuleName +} + +// Type returns the action. +func (msg MsgUpdateExecution) Type() string { + return "UpdateExecution" +} + +// ValidateBasic runs stateless checks on the message. +func (msg MsgUpdateExecution) ValidateBasic() error { + if msg.Executor.Empty() { + return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "executor is missing") + } + return nil +} + +// GetSignBytes encodes the message for signing. +func (msg MsgUpdateExecution) GetSignBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg)) +} + +// GetSigners defines whose signature is required. +func (msg MsgUpdateExecution) GetSigners() []sdk.AccAddress { + return []sdk.AccAddress{msg.Executor} +} diff --git a/x/execution/internal/types/params.go b/x/execution/internal/types/params.go new file mode 100644 index 000000000..4940992b5 --- /dev/null +++ b/x/execution/internal/types/params.go @@ -0,0 +1,38 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/x/params" +) + +// Default parameter namespace +const ( + DefaultParamspace = ModuleName +) + +// ParamKeyTable for instance module +func ParamKeyTable() params.KeyTable { + return params.NewKeyTable().RegisterParamSet(&Params{}) +} + +// Params - used for initializing default parameter for instance at genesis +type Params struct{} + +// NewParams creates a new Params object +func NewParams() Params { + return Params{} +} + +// String implements the stringer interface for Params +func (p Params) String() string { + return "" +} + +// ParamSetPairs - Implements params.ParamSet +func (p *Params) ParamSetPairs() params.ParamSetPairs { + return params.ParamSetPairs{} +} + +// DefaultParams defines the parameters for this module +func DefaultParams() Params { + return NewParams() +} diff --git a/x/execution/internal/types/querier.go b/x/execution/internal/types/querier.go new file mode 100644 index 000000000..cd2a8920f --- /dev/null +++ b/x/execution/internal/types/querier.go @@ -0,0 +1,6 @@ +package types + +const ( + QueryGetExecution = "get" + QueryListExecution = "list" +) diff --git a/x/execution/module.go b/x/execution/module.go new file mode 100644 index 000000000..48476076b --- /dev/null +++ b/x/execution/module.go @@ -0,0 +1,139 @@ +package execution + +import ( + "encoding/json" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/gorilla/mux" + "github.com/mesg-foundation/engine/x/execution/client/cli" + "github.com/mesg-foundation/engine/x/execution/client/rest" + "github.com/mesg-foundation/engine/x/execution/internal/types" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" +) + +var ( + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} +) + +// AppModuleBasic defines the basic application module used by the execution module. +type AppModuleBasic struct{} + +var _ module.AppModuleBasic = AppModuleBasic{} + +// Name returns the execution module's name. +func (AppModuleBasic) Name() string { + return types.ModuleName +} + +// RegisterCodec registers the execution module's types for the given codec. +func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { + RegisterCodec(cdc) +} + +// DefaultGenesis returns default genesis state as raw bytes for the execution +// module. +func (AppModuleBasic) DefaultGenesis() json.RawMessage { + return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) +} + +// ValidateGenesis performs genesis state validation for the execution module. +func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { + var data GenesisState + err := ModuleCdc.UnmarshalJSON(bz, &data) + if err != nil { + return err + } + return ValidateGenesis(data) +} + +// RegisterRESTRoutes registers the REST routes for the execution module. +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { + rest.RegisterRoutes(ctx, rtr) +} + +// GetTxCmd returns the root tx command for the execution module. +func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { + return cli.GetTxCmd(cdc) +} + +// GetQueryCmd returns no root query command for the execution module. +func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { + return cli.GetQueryCmd(StoreKey, cdc) +} + +//____________________________________________________________________________ + +// AppModule implements an application module for the execution module. +type AppModule struct { + AppModuleBasic + + keeper Keeper +} + +// NewAppModule creates a new AppModule object +func NewAppModule(k Keeper) AppModule { + return AppModule{ + AppModuleBasic: AppModuleBasic{}, + keeper: k, + } +} + +// Name returns the execution module's name. +func (AppModule) Name() string { + return ModuleName +} + +// RegisterInvariants registers the execution module invariants. +func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} + +// Route returns the message routing key for the execution module. +func (AppModule) Route() string { + return RouterKey +} + +// NewHandler returns an sdk.Handler for the execution module. +func (am AppModule) NewHandler() sdk.Handler { + return NewHandler(am.keeper) +} + +// QuerierRoute returns the execution module's querier route name. +func (AppModule) QuerierRoute() string { + return QuerierRoute +} + +// NewQuerierHandler returns the execution module sdk.Querier. +func (am AppModule) NewQuerierHandler() sdk.Querier { + return NewQuerier(am.keeper) +} + +// InitGenesis performs genesis initialization for the execution module. It returns +// no validator updates. +func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { + var genesisState GenesisState + ModuleCdc.MustUnmarshalJSON(data, &genesisState) + InitGenesis(ctx, am.keeper, genesisState) + return []abci.ValidatorUpdate{} +} + +// ExportGenesis returns the exported genesis state as raw bytes for the execution +// module. +func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { + gs := ExportGenesis(ctx, am.keeper) + return ModuleCdc.MustMarshalJSON(gs) +} + +// BeginBlock returns the begin blocker for the execution module. +func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { + BeginBlocker(ctx, req, am.keeper) +} + +// EndBlock returns the end blocker for the execution module. It returns no validator +// updates. +func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} diff --git a/x/process/internal/keeper/keeper.go b/x/process/internal/keeper/keeper.go index 3c17e3497..8d2ce7dbd 100644 --- a/x/process/internal/keeper/keeper.go +++ b/x/process/internal/keeper/keeper.go @@ -38,7 +38,7 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { } // Create creates a new process. -func (k *Keeper) Create(ctx sdk.Context, msg *types.MsgCreateProcess) (*processpb.Process, error) { +func (k Keeper) Create(ctx sdk.Context, msg *types.MsgCreateProcess) (*processpb.Process, error) { store := ctx.KVStore(k.storeKey) p := &process.Process{ Name: msg.Request.Name, @@ -81,7 +81,7 @@ func (k *Keeper) Create(ctx sdk.Context, msg *types.MsgCreateProcess) (*processp } // Delete deletes a process. -func (k *Keeper) Delete(ctx sdk.Context, msg *types.MsgDeleteProcess) error { +func (k Keeper) Delete(ctx sdk.Context, msg *types.MsgDeleteProcess) error { if err := k.ownershipKeeper.Delete(ctx, msg.Owner, msg.Request.Hash); err != nil { return err } @@ -90,7 +90,7 @@ func (k *Keeper) Delete(ctx sdk.Context, msg *types.MsgDeleteProcess) error { } // Get returns the process that matches given hash. -func (k *Keeper) Get(ctx sdk.Context, hash hash.Hash) (*processpb.Process, error) { +func (k Keeper) Get(ctx sdk.Context, hash hash.Hash) (*processpb.Process, error) { store := ctx.KVStore(k.storeKey) if !store.Has(hash) { return nil, fmt.Errorf("process %q not found", hash) @@ -101,7 +101,7 @@ func (k *Keeper) Get(ctx sdk.Context, hash hash.Hash) (*processpb.Process, error } // List returns all processes. -func (k *Keeper) List(ctx sdk.Context) ([]*processpb.Process, error) { +func (k Keeper) List(ctx sdk.Context) ([]*processpb.Process, error) { var ( processes []*processpb.Process iter = ctx.KVStore(k.storeKey).Iterator(nil, nil) diff --git a/x/runner/internal/keeper/keeper.go b/x/runner/internal/keeper/keeper.go index ee3189bc2..dc9bf1908 100644 --- a/x/runner/internal/keeper/keeper.go +++ b/x/runner/internal/keeper/keeper.go @@ -35,7 +35,7 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { } // Create creates a new runner. -func (k *Keeper) Create(ctx sdk.Context, msg *types.MsgCreateRunner) (*runner.Runner, error) { +func (k Keeper) Create(ctx sdk.Context, msg *types.MsgCreateRunner) (*runner.Runner, error) { store := ctx.KVStore(k.storeKey) inst, err := k.instanceKeeper.FetchOrCreate(ctx, msg.ServiceHash, msg.EnvHash) if err != nil { @@ -60,7 +60,7 @@ func (k *Keeper) Create(ctx sdk.Context, msg *types.MsgCreateRunner) (*runner.Ru } // Delete deletes a runner. -func (k *Keeper) Delete(ctx sdk.Context, msg *types.MsgDeleteRunner) error { +func (k Keeper) Delete(ctx sdk.Context, msg *types.MsgDeleteRunner) error { store := ctx.KVStore(k.storeKey) if !store.Has(msg.RunnerHash) { return fmt.Errorf("runner %q not found", msg.RunnerHash) @@ -79,7 +79,7 @@ func (k *Keeper) Delete(ctx sdk.Context, msg *types.MsgDeleteRunner) error { } // Get returns the runner that matches given hash. -func (k *Keeper) Get(ctx sdk.Context, hash hash.Hash) (*runner.Runner, error) { +func (k Keeper) Get(ctx sdk.Context, hash hash.Hash) (*runner.Runner, error) { store := ctx.KVStore(k.storeKey) if !store.Has(hash) { return nil, fmt.Errorf("runner %q not found", hash) @@ -90,7 +90,7 @@ func (k *Keeper) Get(ctx sdk.Context, hash hash.Hash) (*runner.Runner, error) { } // List returns all runners. -func (k *Keeper) List(ctx sdk.Context) ([]*runner.Runner, error) { +func (k Keeper) List(ctx sdk.Context) ([]*runner.Runner, error) { var ( runners []*runner.Runner iter = ctx.KVStore(k.storeKey).Iterator(nil, nil) diff --git a/x/service/internal/keeper/keeper.go b/x/service/internal/keeper/keeper.go index 0e12d4f40..679689db5 100644 --- a/x/service/internal/keeper/keeper.go +++ b/x/service/internal/keeper/keeper.go @@ -37,7 +37,7 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { } // Create creates a new service. -func (k *Keeper) Create(ctx sdk.Context, msg *types.MsgCreateService) (*servicepb.Service, error) { +func (k Keeper) Create(ctx sdk.Context, msg *types.MsgCreateService) (*servicepb.Service, error) { store := ctx.KVStore(k.storeKey) // create service srv := initializeService(msg.Request) @@ -70,7 +70,7 @@ func (k *Keeper) Create(ctx sdk.Context, msg *types.MsgCreateService) (*servicep } // Get returns the service that matches given hash. -func (k *Keeper) Get(ctx sdk.Context, hash hash.Hash) (*servicepb.Service, error) { +func (k Keeper) Get(ctx sdk.Context, hash hash.Hash) (*servicepb.Service, error) { store := ctx.KVStore(k.storeKey) if !store.Has(hash) { return nil, fmt.Errorf("service %q not found", hash) @@ -80,17 +80,17 @@ func (k *Keeper) Get(ctx sdk.Context, hash hash.Hash) (*servicepb.Service, error } // Exists returns true if a specific set of data exists in the database, false otherwise -func (k *Keeper) Exists(ctx sdk.Context, hash hash.Hash) (bool, error) { +func (k Keeper) Exists(ctx sdk.Context, hash hash.Hash) (bool, error) { return ctx.KVStore(k.storeKey).Has(hash), nil } // Hash returns the hash of a service request. -func (k *Keeper) Hash(_ sdk.Context, serviceRequest *api.CreateServiceRequest) hash.Hash { +func (k Keeper) Hash(_ sdk.Context, serviceRequest *api.CreateServiceRequest) hash.Hash { return initializeService(serviceRequest).Hash } // List returns all services. -func (k *Keeper) List(ctx sdk.Context) ([]*servicepb.Service, error) { +func (k Keeper) List(ctx sdk.Context) ([]*servicepb.Service, error) { var ( services []*servicepb.Service iter = ctx.KVStore(k.storeKey).Iterator(nil, nil) From 8c11b2128fe6a0a91b8a4f6573228e6e4cfd1451 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Sun, 16 Feb 2020 16:11:57 +0700 Subject: [PATCH 37/87] use forked go-amino version --- go.mod | 2 ++ go.sum | 11 ++--------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index e8b95ae85..a369bcf18 100644 --- a/go.mod +++ b/go.mod @@ -66,4 +66,6 @@ require ( gotest.tools v2.2.0+incompatible // indirect ) +replace github.com/tendermint/go-amino => github.com/mesg-foundation/go-amino v0.15.1-0.20200216090907-4e9dce4edc74 + go 1.13 diff --git a/go.sum b/go.sum index 3368af69a..bc3fd9cff 100644 --- a/go.sum +++ b/go.sum @@ -67,8 +67,6 @@ github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8Nz github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= -github.com/cosmos/cosmos-sdk v0.38.0 h1:BrflLMrECI2ZfftRAq2iAlxlyk+W/4iKVCNCC3a+RPc= -github.com/cosmos/cosmos-sdk v0.38.0/go.mod h1:9ZZex0GKpyNCvilvVAPBoB+0n3A/aO1+/UhPVEaiCy4= github.com/cosmos/cosmos-sdk v0.38.1 h1:DTuxIJeMpB//ydq+ObAjQgsaiwYBZ8T7NDzXjyiL1Kg= github.com/cosmos/cosmos-sdk v0.38.1/go.mod h1:9ZZex0GKpyNCvilvVAPBoB+0n3A/aO1+/UhPVEaiCy4= github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d h1:49RLWk1j44Xu4fjHb6JFYmeUnDORVwHNkDxaQ0ctCVU= @@ -243,6 +241,8 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/mesg-foundation/go-amino v0.15.1-0.20200216090907-4e9dce4edc74 h1:TJ0nzi4nEPVGtrRPcqHWACb3898aHvY5JfBLwgvAh/o= +github.com/mesg-foundation/go-amino v0.15.1-0.20200216090907-4e9dce4edc74/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME= github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 h1:hLDRPB66XQT/8+wG9WsDpiCvZf1yKO7sz7scAjSlBa0= github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= @@ -290,8 +290,6 @@ github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXP github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= github.com/prometheus/client_golang v1.0.0 h1:vrDKnkGzuGvhNAL56c7DBz29ZL+KxnoR0x7enabFceM= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= -github.com/prometheus/client_golang v1.4.0 h1:YVIb/fVcOTMSqtqZWSKnHpSLBxu8DKgxq8z6RuBZwqI= -github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU= github.com/prometheus/client_golang v1.4.1 h1:FFSuS004yOQEtDdTq+TAOLP5xUq63KqAFYyOi8zA+Y8= github.com/prometheus/client_golang v1.4.1/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= @@ -381,9 +379,6 @@ github.com/tendermint/btcd v0.1.1 h1:0VcxPfflS2zZ3RiOAHkBiFUcPvbtRj5O7zHmcJWHV7s github.com/tendermint/btcd v0.1.1/go.mod h1:DC6/m53jtQzr/NFmMNEu0rxf18/ktVoVtMrnDD5pN+U= github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 h1:hqAk8riJvK4RMWx1aInLzndwxKalgi5rTqgfXxOxbEI= github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15/go.mod h1:z4YtwM70uOnk8h0pjJYlj3zdYwi9l03By6iAIF5j/Pk= -github.com/tendermint/go-amino v0.14.1/go.mod h1:i/UKE5Uocn+argJJBb12qTZsCDBcAYMbR92AaJVmKso= -github.com/tendermint/go-amino v0.15.1 h1:D2uk35eT4iTsvJd9jWIetzthE5C0/k2QmMFkCN+4JgQ= -github.com/tendermint/go-amino v0.15.1/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME= github.com/tendermint/iavl v0.13.0 h1:r2sINvNFlJsLlLhGoqlqPlREWYkuK26BvMfkBt+XQnA= github.com/tendermint/iavl v0.13.0/go.mod h1:7nSUPdrsHEZ2nNZa+9gaIrcJciWd1jCQZXtcyARU82k= github.com/tendermint/tendermint v0.33.0 h1:TW1g9sQs3YSqKM8o1+opL3/VmBy4Ke/VKV9MxYpqNbI= @@ -496,8 +491,6 @@ google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZi google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= -google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk= google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= From 1762998b1c98b7c394b8e4117c0d552e2fc22c01 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Sun, 16 Feb 2020 16:12:16 +0700 Subject: [PATCH 38/87] add json test to protobuf/types/struct --- protobuf/types/struct_test.go | 61 +++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/protobuf/types/struct_test.go b/protobuf/types/struct_test.go index 0637e1380..2a0637127 100644 --- a/protobuf/types/struct_test.go +++ b/protobuf/types/struct_test.go @@ -79,24 +79,51 @@ func TestStructMarshal(t *testing.T) { }}}}, }, } - structValueSort1 []byte - structValueSort2 []byte - err error - structUnm1 *Struct - structUnm2 *Struct ) - t.Run("Marshal", func(t *testing.T) { - structValueSort1, err = cdc.MarshalBinaryLengthPrefixed(structSort1) - require.NoError(t, err) - structValueSort2, err = cdc.MarshalBinaryLengthPrefixed(structSort2) - require.NoError(t, err) - require.True(t, hash.Dump(structValueSort1).Equal(hash.Dump(structValueSort2))) + t.Run("BinaryLengthPrefixed", func(t *testing.T) { + var ( + structValueSort1 []byte + structValueSort2 []byte + err error + structUnm1 *Struct + structUnm2 *Struct + ) + t.Run("Marshal", func(t *testing.T) { + structValueSort1, err = cdc.MarshalBinaryLengthPrefixed(structSort1) + require.NoError(t, err) + structValueSort2, err = cdc.MarshalBinaryLengthPrefixed(structSort2) + require.NoError(t, err) + require.True(t, hash.Dump(structValueSort1).Equal(hash.Dump(structValueSort2))) + }) + t.Run("Unmarshal", func(t *testing.T) { + require.NoError(t, cdc.UnmarshalBinaryLengthPrefixed(structValueSort1, &structUnm1)) + require.True(t, structSort1.Equal(structUnm1)) + require.NoError(t, cdc.UnmarshalBinaryLengthPrefixed(structValueSort2, &structUnm2)) + require.True(t, structSort2.Equal(structUnm2)) + require.True(t, structUnm1.Equal(structUnm2)) + }) }) - t.Run("Unmarshal", func(t *testing.T) { - require.NoError(t, cdc.UnmarshalBinaryLengthPrefixed(structValueSort1, &structUnm1)) - require.True(t, structSort1.Equal(structUnm1)) - require.NoError(t, cdc.UnmarshalBinaryLengthPrefixed(structValueSort2, &structUnm2)) - require.True(t, structSort2.Equal(structUnm2)) - require.True(t, structUnm1.Equal(structUnm2)) + t.Run("JSON", func(t *testing.T) { + var ( + structValueSort1 []byte + structValueSort2 []byte + err error + structUnm1 *Struct + structUnm2 *Struct + ) + t.Run("Marshal", func(t *testing.T) { + structValueSort1, err = cdc.MarshalJSON(structSort1) + require.NoError(t, err) + structValueSort2, err = cdc.MarshalJSON(structSort2) + require.NoError(t, err) + require.True(t, hash.Dump(structValueSort1).Equal(hash.Dump(structValueSort2))) + }) + t.Run("Unmarshal", func(t *testing.T) { + require.NoError(t, cdc.UnmarshalJSON(structValueSort1, &structUnm1)) + require.True(t, structSort1.Equal(structUnm1)) + require.NoError(t, cdc.UnmarshalJSON(structValueSort2, &structUnm2)) + require.True(t, structSort2.Equal(structUnm2)) + require.True(t, structUnm1.Equal(structUnm2)) + }) }) } From 4120eb285cc3884be39417f25ed9a4f34d2823df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Mah=C3=A9?= Date: Sun, 16 Feb 2020 16:19:09 +0700 Subject: [PATCH 39/87] Fix typos --- x/execution/client/cli/query.go | 2 +- x/execution/client/rest/query.go | 4 ++-- x/execution/internal/types/events.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/x/execution/client/cli/query.go b/x/execution/client/cli/query.go index 68af79c92..106af38bf 100644 --- a/x/execution/client/cli/query.go +++ b/x/execution/client/cli/query.go @@ -61,7 +61,7 @@ func GetCmdListExecution(queryRoute string, cdc *codec.Codec) *cobra.Command { cliCtx := context.NewCLIContext().WithCodec(cdc) res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryListExecution), nil) if err != nil { - fmt.Printf("could not list services\n%s\n", err.Error()) + fmt.Printf("could not list executions\n%s\n", err.Error()) return nil } diff --git a/x/execution/client/rest/query.go b/x/execution/client/rest/query.go index 2f6603861..9c4bc4127 100644 --- a/x/execution/client/rest/query.go +++ b/x/execution/client/rest/query.go @@ -12,11 +12,11 @@ import ( func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { r.HandleFunc( - "/service/get/{hash}", + "/execution/get/{hash}", queryGetHandlerFn(cliCtx), ).Methods(http.MethodGet) r.HandleFunc( - "/service/list", + "/execution/list", queryListHandlerFn(cliCtx), ).Methods(http.MethodGet) } diff --git a/x/execution/internal/types/events.go b/x/execution/internal/types/events.go index fd6e654f3..5013f6037 100644 --- a/x/execution/internal/types/events.go +++ b/x/execution/internal/types/events.go @@ -2,8 +2,8 @@ package types // execution module event types const ( - EventTypeCreateExecution = "CreateExeuction" - EventTypeUpdateExecution = "UpdateExeuction" + EventTypeCreateExecution = "CreateExecution" + EventTypeUpdateExecution = "UpdateExecution" AttributeHash = "hash" From c01ce8e20f2db65b3ff8aea8826c316e7fa48515 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Sun, 16 Feb 2020 17:19:42 +0700 Subject: [PATCH 40/87] add a bunch of e2e test using the lcd --- e2e/api_test.go | 25 +++++++++++++++-- e2e/execution_test.go | 13 +++++++-- e2e/instance_test.go | 33 ++++++++++++++++------ e2e/process_test.go | 43 ++++++++++++++++++++-------- e2e/runner_test.go | 48 ++++++++++++++++++++++++-------- e2e/service_test.go | 65 ++++++++++++++++++++++++++++++++----------- 6 files changed, 173 insertions(+), 54 deletions(-) diff --git a/e2e/api_test.go b/e2e/api_test.go index 0137dca7d..436f542ea 100644 --- a/e2e/api_test.go +++ b/e2e/api_test.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "context" "io/ioutil" "net/http" @@ -28,7 +29,10 @@ var ( cdc = app.MakeCodec() ) -const lcdEndpoint = "http://127.0.0.1:1317/" +const ( + lcdEndpoint = "http://127.0.0.1:1317/" + lcdPostContentType = "application/json" +) func lcdGet(t *testing.T, path string, ptr interface{}) { resp, err := http.Get(lcdEndpoint + path) @@ -38,7 +42,24 @@ func lcdGet(t *testing.T, path string, ptr interface{}) { require.NoError(t, err) cosResp := rest.ResponseWithHeight{} require.NoError(t, cdc.UnmarshalJSON(body, &cosResp)) - require.NoError(t, cdc.UnmarshalJSON(cosResp.Result, ptr)) + if len(cosResp.Result) > 0 { + require.NoError(t, cdc.UnmarshalJSON(cosResp.Result, ptr)) + } +} + +func lcdPost(t *testing.T, path string, req interface{}, ptr interface{}) { + reqBody, err := cdc.MarshalJSON(req) + require.NoError(t, err) + resp, err := http.Post(lcdEndpoint+path, lcdPostContentType, bytes.NewReader(reqBody)) + require.NoError(t, err) + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + require.NoError(t, err) + cosResp := rest.ResponseWithHeight{} + require.NoError(t, cdc.UnmarshalJSON(body, &cosResp)) + if len(cosResp.Result) > 0 { + require.NoError(t, cdc.UnmarshalJSON(cosResp.Result, ptr)) + } } func TestAPI(t *testing.T) { diff --git a/e2e/execution_test.go b/e2e/execution_test.go index b44ad5b74..40d283ca5 100644 --- a/e2e/execution_test.go +++ b/e2e/execution_test.go @@ -177,9 +177,16 @@ func testExecution(t *testing.T) { }) t.Run("list", func(t *testing.T) { - resp, err := client.ExecutionClient.List(context.Background(), &pb.ListExecutionRequest{}) - require.NoError(t, err) - require.Len(t, resp.Executions, 2) + t.Run("grpc", func(t *testing.T) { + resp, err := client.ExecutionClient.List(context.Background(), &pb.ListExecutionRequest{}) + require.NoError(t, err) + require.Len(t, resp.Executions, 2) + }) + t.Run("lcd", func(t *testing.T) { + execs := make([]*execution.Execution, 0) + lcdGet(t, "execution/list", &execs) + require.Len(t, execs, 2) + }) }) t.Run("many executions in parallel", func(t *testing.T) { diff --git a/e2e/instance_test.go b/e2e/instance_test.go index 50b570024..a804842cf 100644 --- a/e2e/instance_test.go +++ b/e2e/instance_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/instance" pb "github.com/mesg-foundation/engine/protobuf/api" "github.com/stretchr/testify/require" ) @@ -13,18 +14,34 @@ var testInstanceHash hash.Hash func testInstance(t *testing.T) { t.Run("get", func(t *testing.T) { - resp, err := client.InstanceClient.Get(context.Background(), &pb.GetInstanceRequest{Hash: testInstanceHash}) - require.NoError(t, err) - require.Equal(t, testInstanceHash, resp.Hash) - require.Equal(t, testServiceHash, resp.ServiceHash) - require.Equal(t, hash.Dump([]string{"BAR=3", "FOO=1", "REQUIRED=4"}), resp.EnvHash) + t.Run("lcd", func(t *testing.T) { + resp, err := client.InstanceClient.Get(context.Background(), &pb.GetInstanceRequest{Hash: testInstanceHash}) + require.NoError(t, err) + require.Equal(t, testInstanceHash, resp.Hash) + require.Equal(t, testServiceHash, resp.ServiceHash) + require.Equal(t, hash.Dump([]string{"BAR=3", "FOO=1", "REQUIRED=4"}), resp.EnvHash) + }) + t.Run("lcd", func(t *testing.T) { + var inst *instance.Instance + lcdGet(t, "instance/get/"+testInstanceHash.String(), &inst) + require.Equal(t, testInstanceHash, inst.Hash) + require.Equal(t, testServiceHash, inst.ServiceHash) + require.Equal(t, hash.Dump([]string{"BAR=3", "FOO=1", "REQUIRED=4"}), inst.EnvHash) + }) }) t.Run("list", func(t *testing.T) { t.Run("with nil filter", func(t *testing.T) { - resp, err := client.InstanceClient.List(context.Background(), &pb.ListInstanceRequest{}) - require.NoError(t, err) - require.Len(t, resp.Instances, 1) + t.Run("lcd", func(t *testing.T) { + resp, err := client.InstanceClient.List(context.Background(), &pb.ListInstanceRequest{}) + require.NoError(t, err) + require.Len(t, resp.Instances, 1) + }) + t.Run("lcd", func(t *testing.T) { + insts := make([]*instance.Instance, 0) + lcdGet(t, "instance/list", &insts) + require.Len(t, insts, 1) + }) }) t.Run("do not match service", func(t *testing.T) { resp, err := client.InstanceClient.List(context.Background(), &pb.ListInstanceRequest{ diff --git a/e2e/process_test.go b/e2e/process_test.go index 8a309b642..1d33288bf 100644 --- a/e2e/process_test.go +++ b/e2e/process_test.go @@ -54,15 +54,27 @@ func testProcess(t *testing.T) { }) t.Run("get", func(t *testing.T) { - p, err := client.ProcessClient.Get(context.Background(), &pb.GetProcessRequest{Hash: testProcessHash}) - require.NoError(t, err) - require.True(t, p.Equal(&process.Process{ - Hash: p.Hash, - Name: req.Name, - Nodes: req.Nodes, - Edges: req.Edges, - })) - processHash = p.Hash + t.Run("grpc", func(t *testing.T) { + p, err := client.ProcessClient.Get(context.Background(), &pb.GetProcessRequest{Hash: testProcessHash}) + require.NoError(t, err) + require.True(t, p.Equal(&process.Process{ + Hash: p.Hash, + Name: req.Name, + Nodes: req.Nodes, + Edges: req.Edges, + })) + processHash = p.Hash + }) + t.Run("lcd", func(t *testing.T) { + var p *process.Process + lcdGet(t, "process/get/"+testProcessHash.String(), &p) + require.True(t, p.Equal(&process.Process{ + Hash: p.Hash, + Name: req.Name, + Nodes: req.Nodes, + Edges: req.Edges, + })) + }) }) t.Run("check ownership creation", func(t *testing.T) { @@ -91,9 +103,16 @@ func testProcess(t *testing.T) { }) t.Run("list", func(t *testing.T) { - ps, err := client.ProcessClient.List(context.Background(), &pb.ListProcessRequest{}) - require.NoError(t, err) - require.Len(t, ps.Processes, 1) + t.Run("lcd", func(t *testing.T) { + ps, err := client.ProcessClient.List(context.Background(), &pb.ListProcessRequest{}) + require.NoError(t, err) + require.Len(t, ps.Processes, 1) + }) + t.Run("lcd", func(t *testing.T) { + ps := make([]*process.Process, 0) + lcdGet(t, "process/list", &ps) + require.Len(t, ps, 1) + }) }) t.Run("delete", func(t *testing.T) { diff --git a/e2e/runner_test.go b/e2e/runner_test.go index 31eee7df2..bfd6650b4 100644 --- a/e2e/runner_test.go +++ b/e2e/runner_test.go @@ -7,6 +7,7 @@ import ( "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/protobuf/acknowledgement" pb "github.com/mesg-foundation/engine/protobuf/api" + "github.com/mesg-foundation/engine/runner" "github.com/stretchr/testify/require" ) @@ -35,19 +36,35 @@ func testRunner(t *testing.T) { }) t.Run("get", func(t *testing.T) { - resp, err := client.RunnerClient.Get(context.Background(), &pb.GetRunnerRequest{Hash: testRunnerHash}) - require.NoError(t, err) - require.Equal(t, testRunnerHash, resp.Hash) - testInstanceHash = resp.InstanceHash + t.Run("grpc", func(t *testing.T) { + resp, err := client.RunnerClient.Get(context.Background(), &pb.GetRunnerRequest{Hash: testRunnerHash}) + require.NoError(t, err) + require.Equal(t, testRunnerHash, resp.Hash) + testInstanceHash = resp.InstanceHash + }) + t.Run("lcd", func(t *testing.T) { + var r *runner.Runner + lcdGet(t, "runner/get/"+testRunnerHash.String(), &r) + require.Equal(t, testRunnerHash, r.Hash) + }) }) // TODO: need to test the filters t.Run("list", func(t *testing.T) { - resp, err := client.RunnerClient.List(context.Background(), &pb.ListRunnerRequest{}) - require.NoError(t, err) - require.Len(t, resp.Runners, 1) - require.Equal(t, testInstanceHash, resp.Runners[0].InstanceHash) - require.Equal(t, testRunnerHash, resp.Runners[0].Hash) + t.Run("grpc", func(t *testing.T) { + resp, err := client.RunnerClient.List(context.Background(), &pb.ListRunnerRequest{}) + require.NoError(t, err) + require.Len(t, resp.Runners, 1) + require.Equal(t, testInstanceHash, resp.Runners[0].InstanceHash) + require.Equal(t, testRunnerHash, resp.Runners[0].Hash) + }) + t.Run("lcd", func(t *testing.T) { + rs := make([]*runner.Runner, 0) + lcdGet(t, "runner/list", &rs) + require.Len(t, rs, 1) + require.Equal(t, testInstanceHash, rs[0].InstanceHash) + require.Equal(t, testRunnerHash, rs[0].Hash) + }) }) } @@ -55,7 +72,14 @@ func testDeleteRunner(t *testing.T) { _, err := client.RunnerClient.Delete(context.Background(), &pb.DeleteRunnerRequest{Hash: testRunnerHash}) require.NoError(t, err) - resp, err := client.RunnerClient.List(context.Background(), &pb.ListRunnerRequest{}) - require.NoError(t, err) - require.Len(t, resp.Runners, 0) + t.Run("grpc", func(t *testing.T) { + resp, err := client.RunnerClient.List(context.Background(), &pb.ListRunnerRequest{}) + require.NoError(t, err) + require.Len(t, resp.Runners, 0) + }) + t.Run("lcd", func(t *testing.T) { + rs := make([]*runner.Runner, 0) + lcdGet(t, "runner/list", &rs) + require.Len(t, rs, 0) + }) } diff --git a/e2e/service_test.go b/e2e/service_test.go index 0c5740224..efb26cac2 100644 --- a/e2e/service_test.go +++ b/e2e/service_test.go @@ -7,6 +7,7 @@ import ( "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/ownership" pb "github.com/mesg-foundation/engine/protobuf/api" + "github.com/mesg-foundation/engine/service" "github.com/stretchr/testify/require" ) @@ -22,35 +23,65 @@ func testService(t *testing.T) { }) t.Run("get", func(t *testing.T) { - service, err := client.ServiceClient.Get(context.Background(), &pb.GetServiceRequest{Hash: testServiceHash}) - require.NoError(t, err) - require.Equal(t, testServiceHash, service.Hash) + t.Run("grpc", func(t *testing.T) { + service, err := client.ServiceClient.Get(context.Background(), &pb.GetServiceRequest{Hash: testServiceHash}) + require.NoError(t, err) + require.Equal(t, testServiceHash, service.Hash) + }) + t.Run("lcd", func(t *testing.T) { + var s *service.Service + lcdGet(t, "service/get/"+testServiceHash.String(), &s) + require.Equal(t, testServiceHash, s.Hash) + }) }) t.Run("list", func(t *testing.T) { - resp, err := client.ServiceClient.List(context.Background(), &pb.ListServiceRequest{}) - require.NoError(t, err) - require.Len(t, resp.Services, 1) + t.Run("grpc", func(t *testing.T) { + resp, err := client.ServiceClient.List(context.Background(), &pb.ListServiceRequest{}) + require.NoError(t, err) + require.Len(t, resp.Services, 1) + }) + t.Run("lcd", func(t *testing.T) { + ss := make([]*service.Service, 0) + lcdGet(t, "service/list", &ss) + require.Len(t, ss, 1) + }) }) t.Run("exists", func(t *testing.T) { - resp, err := client.ServiceClient.Exists(context.Background(), &pb.ExistsServiceRequest{Hash: testServiceHash}) - require.NoError(t, err) - require.True(t, resp.Exists) + t.Run("grpc", func(t *testing.T) { + resp, err := client.ServiceClient.Exists(context.Background(), &pb.ExistsServiceRequest{Hash: testServiceHash}) + require.NoError(t, err) + require.True(t, resp.Exists) - resp, err = client.ServiceClient.Exists(context.Background(), &pb.ExistsServiceRequest{Hash: hash.Int(1)}) - require.NoError(t, err) - require.False(t, resp.Exists) + resp, err = client.ServiceClient.Exists(context.Background(), &pb.ExistsServiceRequest{Hash: hash.Int(1)}) + require.NoError(t, err) + require.False(t, resp.Exists) + }) + t.Run("lcd", func(t *testing.T) { + var exist bool + lcdGet(t, "service/exist/"+testServiceHash.String(), &exist) + require.True(t, exist) + lcdGet(t, "service/exist/"+hash.Int(1).String(), &exist) + require.False(t, exist) + }) }) t.Run("hash", func(t *testing.T) { - resp, err := client.ServiceClient.Hash(context.Background(), req) - require.NoError(t, err) - require.Equal(t, testServiceHash, resp.Hash) + t.Run("grpc", func(t *testing.T) { + resp, err := client.ServiceClient.Hash(context.Background(), req) + require.NoError(t, err) + require.Equal(t, testServiceHash, resp.Hash) + }) + t.Run("lcd", func(t *testing.T) { + var hash hash.Hash + lcdPost(t, "service/hash", req, &hash) + require.Equal(t, testServiceHash, hash) + }) }) t.Run("check ownership creation", func(t *testing.T) { - t.Run("lcd", func(t *testing.T) { + t.Run("grpc", func(t *testing.T) { ownerships, err := client.OwnershipClient.List(context.Background(), &pb.ListOwnershipRequest{}) require.NoError(t, err) require.Len(t, ownerships.Ownerships, 1) @@ -58,7 +89,7 @@ func testService(t *testing.T) { require.Equal(t, ownership.Ownership_Service, ownerships.Ownerships[0].Resource) require.Equal(t, testServiceHash, ownerships.Ownerships[0].ResourceHash) }) - t.Run("grpc", func(t *testing.T) { + t.Run("lcd", func(t *testing.T) { ownerships := make([]*ownership.Ownership, 0) lcdGet(t, "ownership/list", &ownerships) require.Len(t, ownerships, 1) From c5a851325ebcede5aee18fdf31c8cc70086fd2ba Mon Sep 17 00:00:00 2001 From: Anthony Date: Mon, 17 Feb 2020 10:45:24 +0700 Subject: [PATCH 41/87] Apply suggestions from code review --- e2e/instance_test.go | 4 ++-- e2e/process_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/e2e/instance_test.go b/e2e/instance_test.go index a804842cf..3917f5da9 100644 --- a/e2e/instance_test.go +++ b/e2e/instance_test.go @@ -14,7 +14,7 @@ var testInstanceHash hash.Hash func testInstance(t *testing.T) { t.Run("get", func(t *testing.T) { - t.Run("lcd", func(t *testing.T) { + t.Run("grpc", func(t *testing.T) { resp, err := client.InstanceClient.Get(context.Background(), &pb.GetInstanceRequest{Hash: testInstanceHash}) require.NoError(t, err) require.Equal(t, testInstanceHash, resp.Hash) @@ -32,7 +32,7 @@ func testInstance(t *testing.T) { t.Run("list", func(t *testing.T) { t.Run("with nil filter", func(t *testing.T) { - t.Run("lcd", func(t *testing.T) { + t.Run("grpc", func(t *testing.T) { resp, err := client.InstanceClient.List(context.Background(), &pb.ListInstanceRequest{}) require.NoError(t, err) require.Len(t, resp.Instances, 1) diff --git a/e2e/process_test.go b/e2e/process_test.go index 1d33288bf..9856955db 100644 --- a/e2e/process_test.go +++ b/e2e/process_test.go @@ -103,7 +103,7 @@ func testProcess(t *testing.T) { }) t.Run("list", func(t *testing.T) { - t.Run("lcd", func(t *testing.T) { + t.Run("grpc", func(t *testing.T) { ps, err := client.ProcessClient.List(context.Background(), &pb.ListProcessRequest{}) require.NoError(t, err) require.Len(t, ps.Processes, 1) From 97bb8f8f0d00cd7d967af4a1d1f01eabdaeecc45 Mon Sep 17 00:00:00 2001 From: krhubert Date: Sat, 15 Feb 2020 19:00:03 +0100 Subject: [PATCH 42/87] Move cosmos errors to it's one pkg --- cosmos/{ => errors}/errors.go | 2 +- x/execution/internal/types/msg.go | 8 ++++---- x/runner/internal/types/msg.go | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) rename cosmos/{ => errors}/errors.go (98%) diff --git a/cosmos/errors.go b/cosmos/errors/errors.go similarity index 98% rename from cosmos/errors.go rename to cosmos/errors/errors.go index 2b98ae1d3..18fd5d793 100644 --- a/cosmos/errors.go +++ b/cosmos/errors/errors.go @@ -1,4 +1,4 @@ -package cosmos +package errors import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" diff --git a/x/execution/internal/types/msg.go b/x/execution/internal/types/msg.go index 304de2bb4..f51860211 100644 --- a/x/execution/internal/types/msg.go +++ b/x/execution/internal/types/msg.go @@ -3,7 +3,7 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/mesg-foundation/engine/cosmos" + "github.com/mesg-foundation/engine/cosmos/errors" "github.com/mesg-foundation/engine/ext/xvalidator" "github.com/mesg-foundation/engine/protobuf/api" ) @@ -38,13 +38,13 @@ func (msg MsgCreateExecution) ValidateBasic() error { return err } if !msg.Request.ParentHash.IsZero() && !msg.Request.EventHash.IsZero() { - return sdkerrors.Wrap(cosmos.ErrValidation, "cannot have both parent and event hash") + return sdkerrors.Wrap(errors.ErrValidation, "cannot have both parent and event hash") } if msg.Request.ParentHash.IsZero() && msg.Request.EventHash.IsZero() { - return sdkerrors.Wrap(cosmos.ErrValidation, "should have at least an event hash or parent hash") + return sdkerrors.Wrap(errors.ErrValidation, "should have at least an event hash or parent hash") } if msg.Request.ExecutorHash.IsZero() { - return sdkerrors.Wrap(cosmos.ErrValidation, "should have a executor hash") + return sdkerrors.Wrap(errors.ErrValidation, "should have a executor hash") } return nil } diff --git a/x/runner/internal/types/msg.go b/x/runner/internal/types/msg.go index 58aa081e1..b4e64a664 100644 --- a/x/runner/internal/types/msg.go +++ b/x/runner/internal/types/msg.go @@ -3,7 +3,7 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/mesg-foundation/engine/cosmos" + "github.com/mesg-foundation/engine/cosmos/errors" "github.com/mesg-foundation/engine/ext/xvalidator" "github.com/mesg-foundation/engine/hash" ) @@ -40,10 +40,10 @@ func (msg MsgCreateRunner) ValidateBasic() error { return err } if msg.ServiceHash.IsZero() { - return sdkerrors.Wrap(cosmos.ErrValidation, "serviceHash is missing") + return sdkerrors.Wrap(errors.ErrValidation, "serviceHash is missing") } if msg.EnvHash.IsZero() { - return sdkerrors.Wrap(cosmos.ErrValidation, "envHash is missing") + return sdkerrors.Wrap(errors.ErrValidation, "envHash is missing") } if msg.Address.Empty() { return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "address is missing") @@ -88,10 +88,10 @@ func (msg MsgDeleteRunner) Type() string { // ValidateBasic runs stateless checks on the message. func (msg MsgDeleteRunner) ValidateBasic() error { if err := xvalidator.Validate.Struct(msg); err != nil { - return sdkerrors.Wrap(cosmos.ErrValidation, err.Error()) + return sdkerrors.Wrap(errors.ErrValidation, err.Error()) } if msg.RunnerHash.IsZero() { - return sdkerrors.Wrap(cosmos.ErrValidation, "runnerHash is missing") + return sdkerrors.Wrap(errors.ErrValidation, "runnerHash is missing") } if msg.Address.Empty() { return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "address is missing") From b9288beac174f21a4c5964d5c52190a5abb20290 Mon Sep 17 00:00:00 2001 From: krhubert Date: Sat, 15 Feb 2020 20:33:33 +0100 Subject: [PATCH 43/87] Refactor cosmos module - create cosmos module client - removed sdk packages - create runner/builder and event/publisher packages - remove orchestrator tests & mocks --- Dockerfile.tools | 1 - Makefile | 5 +- core/main.go | 28 +- cosmos/client.go | 17 -- cosmos/module_client.go | 284 ++++++++++++++++++++ event/event.go | 4 +- {sdk/event => event}/event_listener.go | 11 +- {sdk/event => event}/event_listener_test.go | 25 +- event/publisher/publisher.go | 56 ++++ go.mod | 2 - go.sum | 9 - internal/tools/tools.go | 1 - orchestrator/mocks/EventSDK.go | 27 -- orchestrator/mocks/ExecutionSDK.go | 92 ------- orchestrator/mocks/ProcessSDK.go | 35 --- orchestrator/mocks/RunnerSDK.go | 36 --- orchestrator/orchestrator.go | 25 +- orchestrator/orchestrator_test.go | 271 ------------------- orchestrator/type.go | 43 +-- runner/builder/builder.go | 124 +++++++++ {sdk/runner => runner/builder}/container.go | 2 +- scripts/build-mocks.sh | 7 - sdk/event/event.go | 64 ----- sdk/execution/sdk.go | 118 -------- sdk/process/sdk.go | 66 ----- sdk/runner/sdk.go | 177 ------------ sdk/sdk.go | 38 --- sdk/service/sdk.go | 71 ----- server/grpc/api/event.go | 18 +- server/grpc/api/execution.go | 23 +- server/grpc/api/instance.go | 32 +-- server/grpc/api/ownership.go | 23 +- server/grpc/api/process.go | 20 +- server/grpc/api/runner.go | 42 +-- server/grpc/api/service.go | 26 +- server/grpc/server.go | 32 +-- x/instance/internal/keeper/querier.go | 4 +- 37 files changed, 615 insertions(+), 1244 deletions(-) create mode 100644 cosmos/module_client.go rename {sdk/event => event}/event_listener.go (85%) rename {sdk/event => event}/event_listener_test.go (70%) create mode 100644 event/publisher/publisher.go delete mode 100644 orchestrator/mocks/EventSDK.go delete mode 100644 orchestrator/mocks/ExecutionSDK.go delete mode 100644 orchestrator/mocks/ProcessSDK.go delete mode 100644 orchestrator/mocks/RunnerSDK.go delete mode 100644 orchestrator/orchestrator_test.go create mode 100644 runner/builder/builder.go rename {sdk/runner => runner/builder}/container.go (99%) delete mode 100755 scripts/build-mocks.sh delete mode 100644 sdk/event/event.go delete mode 100644 sdk/execution/sdk.go delete mode 100644 sdk/process/sdk.go delete mode 100644 sdk/runner/sdk.go delete mode 100644 sdk/sdk.go delete mode 100644 sdk/service/sdk.go diff --git a/Dockerfile.tools b/Dockerfile.tools index 40f76a47f..a31455ec6 100644 --- a/Dockerfile.tools +++ b/Dockerfile.tools @@ -38,7 +38,6 @@ RUN go mod download RUN go install github.com/golang/protobuf/protoc-gen-go RUN go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc -RUN go install github.com/vektra/mockery/.../ # verify that mesg-dev container is being used. ENV MESG_DEV true diff --git a/Makefile b/Makefile index f83e06987..246632d32 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: all build build-cmd-cosmos changelog check-version clean clean-build clean-docker dep dev dev-mon dev-start dev-stop docker-build docker-dev docker-publish docker-publish-dev docker-tools genesis lint mock protobuf test +.PHONY: all build build-cmd-cosmos changelog check-version clean clean-build clean-docker dep dev dev-mon dev-start dev-stop docker-build docker-dev docker-publish docker-publish-dev docker-tools genesis lint protobuf test MAJOR_VERSION := $(shell echo $(version) | cut -d . -f 1) MINOR_VERSION := $(shell echo $(version) | cut -d . -f 1-2) @@ -67,9 +67,6 @@ test: dep lint: golangci-lint run -mock: docker-tools - docker run --rm -v $(PWD):/project mesg/tools:local ./scripts/build-mocks.sh - protobuf: docker-tools docker run --rm -v $(PWD):/project mesg/tools:local ./scripts/build-proto.sh diff --git a/core/main.go b/core/main.go index 6556f4ca4..8d1e439b7 100644 --- a/core/main.go +++ b/core/main.go @@ -17,6 +17,7 @@ import ( "github.com/mesg-foundation/engine/config" "github.com/mesg-foundation/engine/container" "github.com/mesg-foundation/engine/cosmos" + "github.com/mesg-foundation/engine/event/publisher" "github.com/mesg-foundation/engine/ext/xerrors" "github.com/mesg-foundation/engine/ext/xnet" "github.com/mesg-foundation/engine/ext/xrand" @@ -25,8 +26,7 @@ import ( "github.com/mesg-foundation/engine/logger" "github.com/mesg-foundation/engine/orchestrator" "github.com/mesg-foundation/engine/protobuf/api" - enginesdk "github.com/mesg-foundation/engine/sdk" - runnersdk "github.com/mesg-foundation/engine/sdk/runner" + "github.com/mesg-foundation/engine/runner/builder" "github.com/mesg-foundation/engine/server/grpc" "github.com/mesg-foundation/engine/version" "github.com/sirupsen/logrus" @@ -35,8 +35,8 @@ import ( db "github.com/tendermint/tm-db" ) -func stopRunningServices(sdk *enginesdk.SDK, address string) error { - runners, err := sdk.Runner.List(&runnersdk.Filter{Address: address}) +func stopRunningServices(mc *cosmos.ModuleClient, b *builder.Builder, address string) error { + runners, err := mc.ListRunner(&cosmos.FilterRunner{Address: address}) if err != nil { return err } @@ -49,7 +49,7 @@ func stopRunningServices(sdk *enginesdk.SDK, address string) error { for _, instance := range runners { go func(hash hash.Hash) { defer wg.Done() - err := sdk.Runner.Delete(&api.DeleteRunnerRequest{ + err := b.Delete(&api.DeleteRunnerRequest{ Hash: hash, DeleteData: false, }) @@ -128,9 +128,6 @@ func main() { // init logger. logger.Init(cfg.Log.Format, cfg.Log.Level, cfg.Log.ForceColors) - // init basicManager - // basicManager := enginesdk.NewBasicManager() - // init tendermint logger tendermintLogger := logger.TendermintLogger() @@ -182,8 +179,13 @@ func main() { logrus.WithField("module", "main").Fatalln(err) } - // init sdk - sdk := enginesdk.New(client, kb, container, cfg.Name, strconv.Itoa(port), cfg.IpfsEndpoint) + mc := cosmos.NewModuleClient(client) + + // init runner builder + b := builder.New(mc, container, cfg.Name, strconv.Itoa(port), cfg.IpfsEndpoint) + + // init event publiserh + ep := publisher.New(mc) // start tendermint node logrus.WithField("module", "main").WithField("seeds", cfg.Tendermint.Config.P2P.Seeds).Info("starting tendermint node") @@ -192,7 +194,7 @@ func main() { } // init gRPC server. - server := grpc.New(sdk, cfg, client) + server := grpc.New(mc, ep, b) logrus.WithField("module", "main").Infof("starting MESG Engine version %s", version.Version) @@ -203,7 +205,7 @@ func main() { }() logrus.WithField("module", "main").Info("starting process engine") - s := orchestrator.New(sdk.Event, sdk.Execution, sdk.Process, sdk.Runner) + s := orchestrator.New(mc, ep) go func() { if err := s.Start(); err != nil { logrus.WithField("module", "main").Fatalln(err) @@ -244,7 +246,7 @@ func main() { } logrus.WithField("module", "main").Info("stopping running services") - if err := stopRunningServices(sdk, acc.GetAddress().String()); err != nil { + if err := stopRunningServices(mc, b, acc.GetAddress().String()); err != nil { logrus.WithField("module", "main").Fatalln(err) } diff --git a/cosmos/client.go b/cosmos/client.go index 0bcfc4802..e040852db 100644 --- a/cosmos/client.go +++ b/cosmos/client.go @@ -75,23 +75,6 @@ func (c *Client) QueryJSON(path string, qdata, ptr interface{}) error { return c.cdc.UnmarshalJSON(result, ptr) } -// Query is abci.query wrapper with errors check and decode data. -func (c *Client) Query(path string, qdata, ptr interface{}) error { - var data []byte - if !xreflect.IsNil(qdata) { - b, err := c.cdc.MarshalBinaryBare(qdata) - if err != nil { - return err - } - data = b - } - result, _, err := c.QueryWithData(path, data) - if err != nil { - return err - } - return c.cdc.UnmarshalBinaryBare(result, ptr) -} - // QueryWithData performs a query to a Tendermint node with the provided path // and a data payload. It returns the result and height of the query upon success // or an error if the query fails. diff --git a/cosmos/module_client.go b/cosmos/module_client.go new file mode 100644 index 000000000..2bb8d0e5f --- /dev/null +++ b/cosmos/module_client.go @@ -0,0 +1,284 @@ +package cosmos + +import ( + "context" + "fmt" + + executionpb "github.com/mesg-foundation/engine/execution" + "github.com/mesg-foundation/engine/ext/xos" + "github.com/mesg-foundation/engine/hash" + instancepb "github.com/mesg-foundation/engine/instance" + ownershippb "github.com/mesg-foundation/engine/ownership" + processpb "github.com/mesg-foundation/engine/process" + "github.com/mesg-foundation/engine/protobuf/api" + runnerpb "github.com/mesg-foundation/engine/runner" + servicepb "github.com/mesg-foundation/engine/service" + "github.com/mesg-foundation/engine/x/execution" + "github.com/mesg-foundation/engine/x/instance" + "github.com/mesg-foundation/engine/x/ownership" + "github.com/mesg-foundation/engine/x/process" + "github.com/mesg-foundation/engine/x/runner" + "github.com/mesg-foundation/engine/x/service" +) + +type ModuleClient struct { + *Client +} + +func NewModuleClient(c *Client) *ModuleClient { + return &ModuleClient{Client: c} +} + +func sroutef(format string, args ...interface{}) string { + return fmt.Sprintf("custom/"+format, args...) +} + +// Create creates a new service from definition. +func (mc *ModuleClient) CreateService(req *api.CreateServiceRequest) (*servicepb.Service, error) { + acc, err := mc.GetAccount() + if err != nil { + return nil, err + } + msg := service.NewMsgCreateService(acc.GetAddress(), req) + tx, err := mc.BuildAndBroadcastMsg(msg) + if err != nil { + return nil, err + } + return mc.GetService(tx.Data) +} + +// GetService returns the service that matches given hash. +func (mc *ModuleClient) GetService(hash hash.Hash) (*servicepb.Service, error) { + var out *servicepb.Service + route := sroutef("%s/%s/%s", service.QuerierRoute, service.QueryGetService, hash) + return out, mc.QueryJSON(route, nil, &out) +} + +// ListService returns all services. +func (mc *ModuleClient) ListService() ([]*servicepb.Service, error) { + var out []*servicepb.Service + route := sroutef("%s/%s", service.QuerierRoute, service.QueryListService) + return out, mc.QueryJSON(route, nil, &out) +} + +// ExistService returns if a service already exists. +func (mc *ModuleClient) ExistService(hash hash.Hash) (bool, error) { + var out bool + route := sroutef("%s/%s/%s", service.QuerierRoute, service.QueryExistService, hash) + return out, mc.QueryJSON(route, nil, &out) +} + +// HashService returns the calculate hash of a service. +func (mc *ModuleClient) HashService(req *api.CreateServiceRequest) (hash.Hash, error) { + var out hash.Hash + route := sroutef("%s/%s", service.QuerierRoute, service.QueryHashService) + return out, mc.QueryJSON(route, req, &out) +} + +// CreateProcess creates a new process. +func (mc *ModuleClient) CreateProcess(req *api.CreateProcessRequest) (*processpb.Process, error) { + acc, err := mc.GetAccount() + if err != nil { + return nil, err + } + msg := process.NewMsgCreateProcess(acc.GetAddress(), req) + tx, err := mc.BuildAndBroadcastMsg(msg) + if err != nil { + return nil, err + } + return mc.GetProcess(tx.Data) +} + +// GetInstance returns the instance that matches given hash. +func (mc *ModuleClient) GetInstance(hash hash.Hash) (*instancepb.Instance, error) { + var out *instancepb.Instance + route := sroutef("%s/%s/%s", instance.QuerierRoute, instance.QueryGetInstance, hash) + return out, mc.QueryJSON(route, nil, &out) +} + +// ListInstance returns all instances. +func (mc *ModuleClient) ListInstance(req *api.ListInstanceRequest) ([]*instancepb.Instance, error) { + var out []*instancepb.Instance + route := sroutef("%s/%s", instance.QuerierRoute, instance.QueryListInstances) + return out, mc.QueryJSON(route, req, &out) +} + +// ListOwnership returns all ownerships. +func (mc *ModuleClient) ListOwnership() ([]*ownershippb.Ownership, error) { + var out []*ownershippb.Ownership + route := sroutef("%s/%s", ownership.QuerierRoute, ownership.QueryListOwnerships) + return out, mc.QueryJSON(route, nil, &out) +} + +// DeleteProcess deletes the process by hash. +func (mc *ModuleClient) DeleteProcess(req *api.DeleteProcessRequest) error { + acc, err := mc.GetAccount() + if err != nil { + return err + } + msg := process.NewMsgDeleteProcess(acc.GetAddress(), req) + _, err = mc.BuildAndBroadcastMsg(msg) + return err +} + +// GetProcess returns the process that matches given hash. +func (mc *ModuleClient) GetProcess(hash hash.Hash) (*processpb.Process, error) { + var out *processpb.Process + route := sroutef("%s/%s/%s", process.QuerierRoute, process.QueryGetProcess, hash.String()) + return out, mc.QueryJSON(route, nil, &out) +} + +// ListProcess returns all processes. +func (mc *ModuleClient) ListProcess() ([]*processpb.Process, error) { + var out []*processpb.Process + route := sroutef("%s/%s", process.QuerierRoute, process.QueryListProcesses) + return out, mc.QueryJSON(route, nil, &out) +} + +// CreateExecution creates a new execution. +func (mc *ModuleClient) CreateExecution(req *api.CreateExecutionRequest) (*executionpb.Execution, error) { + acc, err := mc.GetAccount() + if err != nil { + return nil, err + } + msg := execution.NewMsgCreateExecution(req, acc.GetAddress()) + tx, err := mc.BuildAndBroadcastMsg(msg) + if err != nil { + return nil, err + } + return mc.GetExecution(tx.Data) +} + +// UpdateExecution updates a execution. +func (mc *ModuleClient) UpdateExecution(req *api.UpdateExecutionRequest) (*executionpb.Execution, error) { + acc, err := mc.GetAccount() + if err != nil { + return nil, err + } + msg := execution.NewMsgUpdateExecution(req, acc.GetAddress()) + tx, err := mc.BuildAndBroadcastMsg(msg) + if err != nil { + return nil, err + } + return mc.GetExecution(tx.Data) +} + +// GetExecution returns the execution that matches given hash. +func (mc *ModuleClient) GetExecution(hash hash.Hash) (*executionpb.Execution, error) { + var out *executionpb.Execution + route := sroutef("%s/%s/%s", execution.QuerierRoute, execution.QueryGetExecution, hash) + return out, mc.QueryJSON(route, nil, &out) +} + +// ListExecution returns all executions. +func (mc *ModuleClient) ListExecution() ([]*executionpb.Execution, error) { + var out []*executionpb.Execution + route := sroutef("%s/%s", execution.QuerierRoute, execution.QueryListExecution) + return out, mc.QueryJSON(route, nil, &out) +} + +// StreamExecution returns execution that matches given hash. +func (mc *ModuleClient) StreamExecution(ctx context.Context, req *api.StreamExecutionRequest) (chan *executionpb.Execution, chan error, error) { + if err := req.Filter.Validate(); err != nil { + return nil, nil, err + } + + stream, serrC, err := mc.Stream(ctx, EventModuleQuery(execution.ModuleName)) + if err != nil { + return nil, nil, err + } + + execC := make(chan *executionpb.Execution) + errC := make(chan error) + go func() { + loop: + for { + select { + case hash := <-stream: + exec, err := mc.GetExecution(hash) + if err != nil { + errC <- err + break + } + if req.Filter.Match(exec) { + execC <- exec + } + case err := <-serrC: + errC <- err + case <-ctx.Done(): + break loop + } + } + close(errC) + close(execC) + }() + return execC, errC, nil +} + +// CreateRunner creates a new runner. +func (mc *ModuleClient) CreateRunner(req *api.CreateRunnerRequest) (*runnerpb.Runner, error) { + s, err := mc.GetService(req.ServiceHash) + if err != nil { + return nil, err + } + envHash := hash.Dump(xos.EnvMergeSlices(s.Configuration.Env, req.Env)) + acc, err := mc.GetAccount() + if err != nil { + return nil, err + } + + msg := runner.NewMsgCreateRunner(acc.GetAddress(), req.ServiceHash, envHash) + tx, err := mc.BuildAndBroadcastMsg(msg) + if err != nil { + return nil, err + } + return mc.GetRunner(tx.Data) +} + +// DeleteRunner deletes an existing runner. +func (mc *ModuleClient) DeleteRunner(req *api.DeleteRunnerRequest) error { + acc, err := mc.GetAccount() + if err != nil { + return err + } + msg := runner.NewMsgDeleteRunner(acc.GetAddress(), req.Hash) + _, err = mc.BuildAndBroadcastMsg(msg) + return err +} + +// GetRunner returns the runner that matches given hash. +func (mc *ModuleClient) GetRunner(hash hash.Hash) (*runnerpb.Runner, error) { + var out *runnerpb.Runner + route := sroutef("%s/%s/%s", runner.QuerierRoute, runner.QueryGetRunner, hash) + return out, mc.QueryJSON(route, nil, &out) +} + +// FilterRunner to apply while listing runners. +type FilterRunner struct { + Address string + InstanceHash hash.Hash +} + +// ListRunner returns all runners. +func (mc *ModuleClient) ListRunner(f *FilterRunner) ([]*runnerpb.Runner, error) { + var rs []*runnerpb.Runner + route := sroutef("%s/%s", runner.QuerierRoute, runner.QueryListRunners) + if err := mc.QueryJSON(route, nil, &rs); err != nil { + return nil, err + } + + // no filter, returns + if f == nil { + return rs, nil + } + + // filter results + out := make([]*runnerpb.Runner, 0) + for _, r := range rs { + if (f.Address == "" || r.Address == f.Address) && + (f.InstanceHash.IsZero() || r.InstanceHash.Equal(f.InstanceHash)) { + out = append(out, r) + } + } + return out, nil +} diff --git a/event/event.go b/event/event.go index 7bf22f3ac..42ec2f972 100644 --- a/event/event.go +++ b/event/event.go @@ -5,8 +5,8 @@ import ( "github.com/mesg-foundation/engine/protobuf/types" ) -// Create creates an event eventKey with eventData for service s. -func Create(instanceHash hash.Hash, eventKey string, eventData *types.Struct) *Event { +// New creates an event eventKey with eventData for service s. +func New(instanceHash hash.Hash, eventKey string, eventData *types.Struct) *Event { e := &Event{ InstanceHash: instanceHash, Key: eventKey, diff --git a/sdk/event/event_listener.go b/event/event_listener.go similarity index 85% rename from sdk/event/event_listener.go rename to event/event_listener.go index 6a720292a..e0e131488 100644 --- a/sdk/event/event_listener.go +++ b/event/event_listener.go @@ -1,8 +1,7 @@ -package eventsdk +package event import ( "github.com/cskr/pubsub" - "github.com/mesg-foundation/engine/event" "github.com/mesg-foundation/engine/hash" ) @@ -14,7 +13,7 @@ type Filter struct { } // Match matches event. -func (f *Filter) Match(e *event.Event) bool { +func (f *Filter) Match(e *Event) bool { if f == nil { return true } @@ -41,7 +40,7 @@ func (f *Filter) HasKey() bool { // Listener provides functionalities to listen MESG events. type Listener struct { - C chan *event.Event + C chan *Event ps *pubsub.PubSub topic string @@ -53,7 +52,7 @@ type Listener struct { // NewListener creates a new Listener with given sdk and filters. func NewListener(ps *pubsub.PubSub, topic string, f *Filter) *Listener { return &Listener{ - C: make(chan *event.Event, 1), + C: make(chan *Event, 1), ps: ps, topic: topic, c: ps.Sub(topic), @@ -72,7 +71,7 @@ func (l *Listener) Close() { // Listen listens events that match filter. func (l *Listener) Listen() { for v := range l.c { - if e := v.(*event.Event); l.filter.Match(e) { + if e := v.(*Event); l.filter.Match(e) { l.C <- e } } diff --git a/sdk/event/event_listener_test.go b/event/event_listener_test.go similarity index 70% rename from sdk/event/event_listener_test.go rename to event/event_listener_test.go index 38fa23765..521c757ef 100644 --- a/sdk/event/event_listener_test.go +++ b/event/event_listener_test.go @@ -1,10 +1,9 @@ -package eventsdk +package event import ( "testing" "github.com/cskr/pubsub" - "github.com/mesg-foundation/engine/event" "github.com/mesg-foundation/engine/hash" "github.com/stretchr/testify/assert" ) @@ -12,7 +11,7 @@ import ( func TestFilter(t *testing.T) { var tests = []struct { f *Filter - e *event.Event + e *Event match bool }{ { @@ -22,42 +21,42 @@ func TestFilter(t *testing.T) { }, { &Filter{}, - &event.Event{}, + &Event{}, true, }, { &Filter{Hash: hash.Int(1)}, - &event.Event{Hash: hash.Int(1)}, + &Event{Hash: hash.Int(1)}, true, }, { &Filter{Hash: hash.Int(1)}, - &event.Event{Hash: hash.Int(2)}, + &Event{Hash: hash.Int(2)}, false, }, { &Filter{InstanceHash: hash.Int(1)}, - &event.Event{InstanceHash: hash.Int(1)}, + &Event{InstanceHash: hash.Int(1)}, true, }, { &Filter{InstanceHash: hash.Int(1)}, - &event.Event{InstanceHash: hash.Int(1)}, + &Event{InstanceHash: hash.Int(1)}, true, }, { &Filter{Key: "0"}, - &event.Event{Key: "0"}, + &Event{Key: "0"}, true, }, { &Filter{Key: "*"}, - &event.Event{Key: "0"}, + &Event{Key: "0"}, true, }, { &Filter{Key: "0"}, - &event.Event{Key: "1"}, + &Event{Key: "1"}, false, }, } @@ -69,12 +68,12 @@ func TestFilter(t *testing.T) { func TestEventListener(t *testing.T) { topic := "test-topic" - testEvent := &event.Event{Key: "0"} + testEvent := &Event{Key: "0"} ps := pubsub.New(0) el := NewListener(ps, topic, &Filter{Key: "0"}) go func() { - ps.Pub(&event.Event{Key: "1"}, topic) + ps.Pub(&Event{Key: "1"}, topic) ps.Pub(testEvent, topic) }() go el.Listen() diff --git a/event/publisher/publisher.go b/event/publisher/publisher.go new file mode 100644 index 000000000..e8631a2f0 --- /dev/null +++ b/event/publisher/publisher.go @@ -0,0 +1,56 @@ +package publisher + +import ( + "github.com/cskr/pubsub" + "github.com/mesg-foundation/engine/cosmos" + "github.com/mesg-foundation/engine/event" + "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/protobuf/types" +) + +const ( + // streamTopic is topic used to broadcast events. + streamTopic = "event-stream" +) + +// EventPublisher exposes event APIs of MESG. +type EventPublisher struct { + ps *pubsub.PubSub + mc *cosmos.ModuleClient +} + +// New creates a new Event SDK with given options. +func New(mc *cosmos.ModuleClient) *EventPublisher { + return &EventPublisher{ + ps: pubsub.New(0), + mc: mc, + } +} + +// Publish a MESG event eventKey with eventData for service token. +func (ep *EventPublisher) Publish(instanceHash hash.Hash, eventKey string, eventData *types.Struct) (*event.Event, error) { + i, err := ep.mc.GetInstance(instanceHash) + if err != nil { + return nil, err + } + + s, err := ep.mc.GetService(i.ServiceHash) + if err != nil { + return nil, err + } + + if err := s.RequireEventData(eventKey, eventData); err != nil { + return nil, err + } + + e := event.New(instanceHash, eventKey, eventData) + go ep.ps.Pub(e, streamTopic) + return e, nil +} + +// GetStream broadcasts all events. +func (ep *EventPublisher) GetStream(f *event.Filter) *event.Listener { + l := event.NewListener(ep.ps, streamTopic, f) + go l.Listen() + return l +} diff --git a/go.mod b/go.mod index a369bcf18..99d3518a8 100644 --- a/go.mod +++ b/go.mod @@ -55,10 +55,8 @@ require ( github.com/tendermint/go-amino v0.15.1 github.com/tendermint/tendermint v0.33.0 github.com/tendermint/tm-db v0.4.0 - github.com/vektra/mockery v0.0.0-20181123154057-e78b021dcbb5 golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413 golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 // indirect - golang.org/x/tools v0.0.0-20190813142322-97f12d73768f // indirect google.golang.org/grpc v1.27.1 gopkg.in/go-playground/assert.v1 v1.2.1 // indirect gopkg.in/go-playground/validator.v9 v9.31.0 diff --git a/go.sum b/go.sum index bc3fd9cff..a25deda1e 100644 --- a/go.sum +++ b/go.sum @@ -388,8 +388,6 @@ github.com/tendermint/tm-db v0.4.0/go.mod h1:+Cwhgowrf7NBGXmsqFMbwEtbo80XmyrlY5J github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= -github.com/vektra/mockery v0.0.0-20181123154057-e78b021dcbb5 h1:Xim2mBRFdXzXmKRO8DJg/FJtn/8Fj9NOEpO6+WuMPmk= -github.com/vektra/mockery v0.0.0-20181123154057-e78b021dcbb5/go.mod h1:ppEjwdhyy7Y31EnHRDm1JkChoC7LXIJ7Ex0VYLWtZtQ= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/zondax/hid v0.9.0 h1:eiT3P6vNxAEVxXMw66eZUAAnU2zD33JBkfG/EnfAKl8= @@ -423,8 +421,6 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190628185345-da137c7871d7 h1:rTIdg5QFRR7XCaK4LCjBiPbx8j4DQRpdYMnGn/bJUEU= golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 h1:fHDIZ2oxGnUZRN6WgWFCbYBjH9uqVPRCUVUDhs0wnbA= @@ -466,8 +462,6 @@ golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20181112210238-4b1f3b6b1646 h1:JEEoTsNEpPwxsebhPLC6P2jNr+6RFZLY4elUBVcMb+I= -golang.org/x/tools v0.0.0-20181112210238-4b1f3b6b1646/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c h1:vamGzbGri8IKo20MQncCuljcQ5uAO6kaCeawQPVblAI= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= @@ -475,9 +469,6 @@ golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3 golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/tools v0.0.0-20190813142322-97f12d73768f h1:nQv5Lx4ucsmk8T4jkEQKJu7YLkYXy/PLoZgTpnIrkuI= -golang.org/x/tools v0.0.0-20190813142322-97f12d73768f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= diff --git a/internal/tools/tools.go b/internal/tools/tools.go index c02b0930a..a185d6aeb 100644 --- a/internal/tools/tools.go +++ b/internal/tools/tools.go @@ -8,5 +8,4 @@ import ( _ "github.com/go-bindata/go-bindata/go-bindata" _ "github.com/golang/protobuf/protoc-gen-go" _ "github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc" - _ "github.com/vektra/mockery/cmd/mockery" ) diff --git a/orchestrator/mocks/EventSDK.go b/orchestrator/mocks/EventSDK.go deleted file mode 100644 index 888406ca4..000000000 --- a/orchestrator/mocks/EventSDK.go +++ /dev/null @@ -1,27 +0,0 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. - -package mocks - -import eventsdk "github.com/mesg-foundation/engine/sdk/event" -import mock "github.com/stretchr/testify/mock" - -// EventSDK is an autogenerated mock type for the EventSDK type -type EventSDK struct { - mock.Mock -} - -// GetStream provides a mock function with given fields: f -func (_m *EventSDK) GetStream(f *eventsdk.Filter) *eventsdk.Listener { - ret := _m.Called(f) - - var r0 *eventsdk.Listener - if rf, ok := ret.Get(0).(func(*eventsdk.Filter) *eventsdk.Listener); ok { - r0 = rf(f) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*eventsdk.Listener) - } - } - - return r0 -} diff --git a/orchestrator/mocks/ExecutionSDK.go b/orchestrator/mocks/ExecutionSDK.go deleted file mode 100644 index 824f9600a..000000000 --- a/orchestrator/mocks/ExecutionSDK.go +++ /dev/null @@ -1,92 +0,0 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. - -package mocks - -import api "github.com/mesg-foundation/engine/protobuf/api" -import context "context" -import execution "github.com/mesg-foundation/engine/execution" -import hash "github.com/mesg-foundation/engine/hash" -import mock "github.com/stretchr/testify/mock" - -// ExecutionSDK is an autogenerated mock type for the ExecutionSDK type -type ExecutionSDK struct { - mock.Mock -} - -// Create provides a mock function with given fields: req -func (_m *ExecutionSDK) Create(req *api.CreateExecutionRequest) (*execution.Execution, error) { - ret := _m.Called(req) - - var r0 *execution.Execution - if rf, ok := ret.Get(0).(func(*api.CreateExecutionRequest) *execution.Execution); ok { - r0 = rf(req) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*execution.Execution) - } - } - - var r1 error - if rf, ok := ret.Get(1).(func(*api.CreateExecutionRequest) error); ok { - r1 = rf(req) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// Get provides a mock function with given fields: _a0 -func (_m *ExecutionSDK) Get(_a0 hash.Hash) (*execution.Execution, error) { - ret := _m.Called(_a0) - - var r0 *execution.Execution - if rf, ok := ret.Get(0).(func(hash.Hash) *execution.Execution); ok { - r0 = rf(_a0) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*execution.Execution) - } - } - - var r1 error - if rf, ok := ret.Get(1).(func(hash.Hash) error); ok { - r1 = rf(_a0) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// Stream provides a mock function with given fields: ctx, req -func (_m *ExecutionSDK) Stream(ctx context.Context, req *api.StreamExecutionRequest) (chan *execution.Execution, chan error, error) { - ret := _m.Called(ctx, req) - - var r0 chan *execution.Execution - if rf, ok := ret.Get(0).(func(context.Context, *api.StreamExecutionRequest) chan *execution.Execution); ok { - r0 = rf(ctx, req) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(chan *execution.Execution) - } - } - - var r1 chan error - if rf, ok := ret.Get(1).(func(context.Context, *api.StreamExecutionRequest) chan error); ok { - r1 = rf(ctx, req) - } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(chan error) - } - } - - var r2 error - if rf, ok := ret.Get(2).(func(context.Context, *api.StreamExecutionRequest) error); ok { - r2 = rf(ctx, req) - } else { - r2 = ret.Error(2) - } - - return r0, r1, r2 -} diff --git a/orchestrator/mocks/ProcessSDK.go b/orchestrator/mocks/ProcessSDK.go deleted file mode 100644 index aa15b5a72..000000000 --- a/orchestrator/mocks/ProcessSDK.go +++ /dev/null @@ -1,35 +0,0 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. - -package mocks - -import mock "github.com/stretchr/testify/mock" - -import process "github.com/mesg-foundation/engine/process" - -// ProcessSDK is an autogenerated mock type for the ProcessSDK type -type ProcessSDK struct { - mock.Mock -} - -// List provides a mock function with given fields: -func (_m *ProcessSDK) List() ([]*process.Process, error) { - ret := _m.Called() - - var r0 []*process.Process - if rf, ok := ret.Get(0).(func() []*process.Process); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]*process.Process) - } - } - - var r1 error - if rf, ok := ret.Get(1).(func() error); ok { - r1 = rf() - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} diff --git a/orchestrator/mocks/RunnerSDK.go b/orchestrator/mocks/RunnerSDK.go deleted file mode 100644 index 53d2031f1..000000000 --- a/orchestrator/mocks/RunnerSDK.go +++ /dev/null @@ -1,36 +0,0 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. - -package mocks - -import mock "github.com/stretchr/testify/mock" - -import runner "github.com/mesg-foundation/engine/runner" -import runnersdk "github.com/mesg-foundation/engine/sdk/runner" - -// RunnerSDK is an autogenerated mock type for the RunnerSDK type -type RunnerSDK struct { - mock.Mock -} - -// List provides a mock function with given fields: f -func (_m *RunnerSDK) List(f *runnersdk.Filter) ([]*runner.Runner, error) { - ret := _m.Called(f) - - var r0 []*runner.Runner - if rf, ok := ret.Get(0).(func(*runnersdk.Filter) []*runner.Runner); ok { - r0 = rf(f) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]*runner.Runner) - } - } - - var r1 error - if rf, ok := ret.Get(1).(func(*runnersdk.Filter) error); ok { - r1 = rf(f) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} diff --git a/orchestrator/orchestrator.go b/orchestrator/orchestrator.go index 1fb371514..902fc30c8 100644 --- a/orchestrator/orchestrator.go +++ b/orchestrator/orchestrator.go @@ -6,24 +6,23 @@ import ( "fmt" "math/rand" + "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/event" + "github.com/mesg-foundation/engine/event/publisher" "github.com/mesg-foundation/engine/execution" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/process" "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/protobuf/types" - runnersdk "github.com/mesg-foundation/engine/sdk/runner" "github.com/sirupsen/logrus" ) // New creates a new Process instance -func New(event EventSDK, execution ExecutionSDK, process ProcessSDK, runner RunnerSDK) *Orchestrator { +func New(mc *cosmos.ModuleClient, ep *publisher.EventPublisher) *Orchestrator { return &Orchestrator{ - event: event, - execution: execution, - process: process, - runner: runner, - ErrC: make(chan error), + mc: mc, + ep: ep, + ErrC: make(chan error), } } @@ -33,12 +32,12 @@ func (s *Orchestrator) Start() error { return fmt.Errorf("process orchestrator already running") } - s.eventStream = s.event.GetStream(nil) + s.eventStream = s.ep.GetStream(nil) ctx, cancel := context.WithCancel(context.Background()) defer cancel() - executionStream, errC, err := s.execution.Stream(ctx, &api.StreamExecutionRequest{ + executionStream, errC, err := s.mc.StreamExecution(ctx, &api.StreamExecutionRequest{ Filter: &api.StreamExecutionRequest_Filter{ Statuses: []execution.Status{execution.Status_Completed}, }, @@ -105,7 +104,7 @@ func (s *Orchestrator) findNodes(wf *process.Process, filter func(wf *process.Pr } func (s *Orchestrator) execute(filter func(wf *process.Process, node *process.Process_Node) (bool, error), exec *execution.Execution, event *event.Event, data *types.Struct) { - processes, err := s.process.List() + processes, err := s.mc.ListProcess() if err != nil { s.ErrC <- err return @@ -231,7 +230,7 @@ func (s *Orchestrator) resolveInput(wfHash hash.Hash, exec *execution.Execution, return nil, fmt.Errorf("reference's nodeKey not found") } if exec.NodeKey != nodeKey { - parent, err := s.execution.Get(exec.ParentHash) + parent, err := s.mc.GetExecution(exec.ParentHash) if err != nil { return nil, err } @@ -248,7 +247,7 @@ func (s *Orchestrator) processTask(nodeKey string, task *process.Process_Node_Ta if exec != nil { execHash = exec.Hash } - executors, err := s.runner.List(&runnersdk.Filter{ + executors, err := s.mc.ListRunner(&cosmos.FilterRunner{ InstanceHash: task.InstanceHash, }) if err != nil { @@ -258,7 +257,7 @@ func (s *Orchestrator) processTask(nodeKey string, task *process.Process_Node_Ta return fmt.Errorf("no runner is running instance %q", task.InstanceHash) } executor := executors[rand.Intn(len(executors))] - _, err = s.execution.Create(&api.CreateExecutionRequest{ + _, err = s.mc.CreateExecution(&api.CreateExecutionRequest{ ProcessHash: wf.Hash, EventHash: eventHash, ParentHash: execHash, diff --git a/orchestrator/orchestrator_test.go b/orchestrator/orchestrator_test.go deleted file mode 100644 index ac10aecd8..000000000 --- a/orchestrator/orchestrator_test.go +++ /dev/null @@ -1,271 +0,0 @@ -package orchestrator - -import ( - "fmt" - "testing" - - "github.com/mesg-foundation/engine/event" - "github.com/mesg-foundation/engine/execution" - "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/orchestrator/mocks" - "github.com/mesg-foundation/engine/process" - "github.com/mesg-foundation/engine/protobuf/types" - "github.com/mesg-foundation/engine/runner" - "github.com/stretchr/testify/mock" - "github.com/stretchr/testify/require" -) - -func TestFilter(t *testing.T) { - o := New(&mocks.EventSDK{}, &mocks.ExecutionSDK{}, &mocks.ProcessSDK{}, &mocks.RunnerSDK{}) - p := process.Process{ - Hash: hash.Int(1), - Nodes: []*process.Process_Node{ - { - Key: "1", - Type: &process.Process_Node_Event_{ - Event: &process.Process_Node_Event{ - InstanceHash: hash.Int(1), - EventKey: "1", - }, - }, - }, - { - Key: "2", - Type: &process.Process_Node_Task_{Task: &process.Process_Node_Task{ - InstanceHash: hash.Int(2), - TaskKey: "2", - }}}, - { - Key: "3", - Type: &process.Process_Node_Task_{Task: &process.Process_Node_Task{ - InstanceHash: hash.Int(3), - TaskKey: "3", - }}}, - }, - Edges: []*process.Process_Edge{ - {Src: "1", Dst: "2"}, - {Src: "2", Dst: "3"}, - }, - } - var tests = []struct { - filter func(wf *process.Process, node *process.Process_Node) (bool, error) - p *process.Process - n *process.Process_Node - res bool - err error - }{ - { - filter: o.eventFilter(&event.Event{InstanceHash: hash.Int(1), Key: "1"}), - n: p.Nodes[0], - res: true, - err: nil, - }, - { - filter: o.eventFilter(&event.Event{InstanceHash: hash.Int(1), Key: "2"}), - n: p.Nodes[0], - res: false, - err: nil, - }, - { - filter: o.eventFilter(&event.Event{InstanceHash: hash.Int(2), Key: "1"}), - n: p.Nodes[0], - res: false, - err: nil, - }, - { - filter: o.eventFilter(&event.Event{InstanceHash: hash.Int(2), Key: "1"}), - n: p.Nodes[1], - res: false, - err: nil, - }, - { - filter: o.resultFilter(&execution.Execution{InstanceHash: hash.Int(1), TaskKey: "1"}), - n: &process.Process_Node{Type: &process.Process_Node_Result_{Result: &process.Process_Node_Result{ - InstanceHash: hash.Int(1), - TaskKey: "1", - }}}, - res: true, - err: nil, - }, - { - filter: o.resultFilter(&execution.Execution{InstanceHash: hash.Int(1), TaskKey: "1"}), - n: &process.Process_Node{Type: &process.Process_Node_Result_{Result: &process.Process_Node_Result{ - InstanceHash: hash.Int(1), - TaskKey: "2", - }}}, - res: false, - err: nil, - }, - { - filter: o.resultFilter(&execution.Execution{InstanceHash: hash.Int(1), TaskKey: "1"}), - n: &process.Process_Node{Type: &process.Process_Node_Result_{Result: &process.Process_Node_Result{ - InstanceHash: hash.Int(2), - TaskKey: "1", - }}}, - res: false, - err: nil, - }, - { - filter: o.resultFilter(&execution.Execution{InstanceHash: hash.Int(1), TaskKey: "1"}), - n: p.Nodes[0], - res: false, - err: nil, - }, - { - filter: o.dependencyFilter(&execution.Execution{InstanceHash: hash.Int(3), TaskKey: "2", ProcessHash: hash.Int(1), NodeKey: "2"}), - p: &p, - n: p.Nodes[2], - res: true, - err: nil, - }, - { - filter: o.dependencyFilter(&execution.Execution{InstanceHash: hash.Int(3), TaskKey: "2", ProcessHash: hash.Int(2), NodeKey: "2"}), - p: &p, - n: p.Nodes[2], - res: false, - err: nil, - }, - { - filter: o.dependencyFilter(&execution.Execution{InstanceHash: hash.Int(3), TaskKey: "2", ProcessHash: hash.Int(1), NodeKey: "1"}), - p: &p, - n: p.Nodes[2], - res: false, - err: nil, - }, - { - filter: o.dependencyFilter(&execution.Execution{InstanceHash: hash.Int(3), TaskKey: "2", ProcessHash: hash.Int(1), NodeKey: "2"}), - p: &p, - n: p.Nodes[0], - res: false, - err: nil, - }, - } - for _, test := range tests { - ok, err := test.filter(test.p, test.n) - if test.err != nil { - require.Equal(t, test.err, err) - } else { - require.Equal(t, ok, test.res) - } - } -} - -func TestFindNode(t *testing.T) { - o := New(&mocks.EventSDK{}, &mocks.ExecutionSDK{}, &mocks.ProcessSDK{}, &mocks.RunnerSDK{}) - data := &process.Process{ - Hash: hash.Int(1), - Nodes: []*process.Process_Node{ - { - Key: "1", - Type: &process.Process_Node_Event_{ - Event: &process.Process_Node_Event{}, - }, - }, - }, - } - require.Len(t, o.findNodes(data, func(p *process.Process, n *process.Process_Node) (bool, error) { - return true, nil - }), 1) - require.Len(t, o.findNodes(data, func(p *process.Process, n *process.Process_Node) (bool, error) { - return n.Key == "1", nil - }), 1) - require.Len(t, o.findNodes(data, func(p *process.Process, n *process.Process_Node) (bool, error) { - return n.Key == "2", nil - }), 0) -} - -// func TestProcessMap(t *testing.T) { -// e := &mocks.ExecutionSDK{} -// o := New(&mocks.EventSDK{}, e, &mocks.ProcessSDK{}) -// exec := &execution.Execution{ -// ProcessHash: hash.Int(1), -// NodeKey: "1", -// ParentHash: hash.Int(2), -// Outputs: &types.Struct{ -// Fields: map[string]*types.Value{ -// "xxx": &types.Value{ -// Kind: &types.Value_StringValue{StringValue: "str"}, -// }, -// }, -// }, -// } -// o.processMap(&process.Process_Node_Map{ -// Outputs: []*process.Process_Node_Map_Output{}, -// }, ) -// } - -func TestResolveInput(t *testing.T) { - e := &mocks.ExecutionSDK{} - o := New(&mocks.EventSDK{}, e, &mocks.ProcessSDK{}, &mocks.RunnerSDK{}) - exec := &execution.Execution{ - ProcessHash: hash.Int(2), - NodeKey: "2", - ParentHash: hash.Int(3), - Outputs: &types.Struct{ - Fields: map[string]*types.Value{ - "xxx": { - Kind: &types.Value_StringValue{StringValue: "str"}, - }, - }, - }, - } - // Different processes - _, err := o.resolveInput(hash.Int(1), exec, "2", &process.Process_Node_Map_Output_Reference_Path{Selector: &process.Process_Node_Map_Output_Reference_Path_Key{Key: "xxx"}}) - require.Error(t, err) - // Different steps, should return the value of the data - val, err := o.resolveInput(hash.Int(2), exec, "2", &process.Process_Node_Map_Output_Reference_Path{Selector: &process.Process_Node_Map_Output_Reference_Path_Key{Key: "xxx"}}) - require.NoError(t, err) - require.Equal(t, val, exec.Outputs.Fields["xxx"]) - // Invalid execution parent hash - e.On("Get", mock.Anything).Once().Return(nil, fmt.Errorf("err")) - _, err = o.resolveInput(hash.Int(2), exec, "-", &process.Process_Node_Map_Output_Reference_Path{Selector: &process.Process_Node_Map_Output_Reference_Path_Key{Key: "xxx"}}) - require.Error(t, err) - // Output from a previous exec - execMock := &execution.Execution{ - NodeKey: "3", - ProcessHash: hash.Int(2), - Outputs: &types.Struct{ - Fields: map[string]*types.Value{ - "yyy": { - Kind: &types.Value_StringValue{StringValue: "str2"}, - }, - }, - }, - } - e.On("Get", mock.Anything).Once().Return(execMock, nil) - val, err = o.resolveInput(hash.Int(2), exec, "3", &process.Process_Node_Map_Output_Reference_Path{Selector: &process.Process_Node_Map_Output_Reference_Path_Key{Key: "yyy"}}) - require.NoError(t, err) - require.Equal(t, val, execMock.Outputs.Fields["yyy"]) -} - -func TestProcessTask(t *testing.T) { - e := &mocks.ExecutionSDK{} - e.On("Create", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Once().Return(nil, nil) - r := &mocks.RunnerSDK{} - r.On("List", mock.Anything).Once().Return([]*runner.Runner{{Hash: hash.Int(1)}}, nil) - o := New(&mocks.EventSDK{}, e, &mocks.ProcessSDK{}, r) - err := o.processTask("-", &process.Process_Node_Task{ - InstanceHash: hash.Int(1), - TaskKey: "-", - }, &process.Process{ - Hash: hash.Int(2), - }, &execution.Execution{ - Hash: hash.Int(3), - }, nil, &types.Struct{ - Fields: map[string]*types.Value{}, - }) - require.NoError(t, err) - e.On("Create", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Once().Return(nil, fmt.Errorf("error")) - r.On("List", mock.Anything).Once().Return([]*runner.Runner{{Hash: hash.Int(1)}}, nil) - err = o.processTask("-", &process.Process_Node_Task{ - InstanceHash: hash.Int(1), - TaskKey: "-", - }, &process.Process{ - Hash: hash.Int(2), - }, nil, &event.Event{ - Hash: hash.Int(3), - }, &types.Struct{ - Fields: map[string]*types.Value{}, - }) - require.Error(t, err) -} diff --git a/orchestrator/type.go b/orchestrator/type.go index edfa8fe34..cf19adc26 100644 --- a/orchestrator/type.go +++ b/orchestrator/type.go @@ -1,50 +1,19 @@ package orchestrator import ( - "context" - + "github.com/mesg-foundation/engine/cosmos" + "github.com/mesg-foundation/engine/event" + "github.com/mesg-foundation/engine/event/publisher" "github.com/mesg-foundation/engine/execution" - "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/process" - "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/runner" - eventsdk "github.com/mesg-foundation/engine/sdk/event" - runnersdk "github.com/mesg-foundation/engine/sdk/runner" ) -// ExecutionSDK execution interface needed for the orchestrator -type ExecutionSDK interface { - Stream(ctx context.Context, req *api.StreamExecutionRequest) (chan *execution.Execution, chan error, error) - Get(hash hash.Hash) (*execution.Execution, error) - Create(req *api.CreateExecutionRequest) (*execution.Execution, error) -} - -// EventSDK event interface needed for the orchestrator -type EventSDK interface { - GetStream(f *eventsdk.Filter) *eventsdk.Listener -} - -// ProcessSDK process interface needed for the orchestrator -type ProcessSDK interface { - List() ([]*process.Process, error) -} - -// RunnerSDK is the interface of the runner sdk needed for the orchestrator -type RunnerSDK interface { - List(f *runnersdk.Filter) ([]*runner.Runner, error) -} - // Orchestrator manages the executions based on the definition of the processes type Orchestrator struct { - event EventSDK - eventStream *eventsdk.Listener + mc *cosmos.ModuleClient + ep *publisher.EventPublisher - execution ExecutionSDK + eventStream *event.Listener executionStream <-chan *execution.Execution - process ProcessSDK - - runner RunnerSDK - ErrC chan error } diff --git a/runner/builder/builder.go b/runner/builder/builder.go new file mode 100644 index 000000000..b5022f147 --- /dev/null +++ b/runner/builder/builder.go @@ -0,0 +1,124 @@ +package builder + +import ( + "errors" + "fmt" + + "github.com/mesg-foundation/engine/container" + "github.com/mesg-foundation/engine/cosmos" + "github.com/mesg-foundation/engine/ext/xos" + "github.com/mesg-foundation/engine/hash" + instancepb "github.com/mesg-foundation/engine/instance" + "github.com/mesg-foundation/engine/protobuf/api" + runnerpb "github.com/mesg-foundation/engine/runner" +) + +// Builder is the runner builder. +type Builder struct { + mc *cosmos.ModuleClient + container container.Container + engineName string + port string + ipfsEndpoint string +} + +// New returns the runner sdk. +func New(mc *cosmos.ModuleClient, container container.Container, engineName, port, ipfsEndpoint string) *Builder { + sdk := &Builder{ + container: container, + mc: mc, + engineName: engineName, + port: port, + ipfsEndpoint: ipfsEndpoint, + } + return sdk +} + +// Create creates a new runner. +func (b *Builder) Create(req *api.CreateRunnerRequest) (*runnerpb.Runner, error) { + // calculate instance's hash. + // TODO: this should be merged with the same logic currently in instance sdk + srv, err := b.mc.GetService(req.ServiceHash) + if err != nil { + return nil, err + } + + instanceEnv := xos.EnvMergeSlices(srv.Configuration.Env, req.Env) + envHash := hash.Dump(instanceEnv) + // TODO: should be done by instance or runner + instanceHash := hash.Dump(&instancepb.Instance{ + ServiceHash: srv.Hash, + EnvHash: envHash, + }) + acc, err := b.mc.GetAccount() + if err != nil { + return nil, err + } + expRunnerHash := hash.Dump(&runnerpb.Runner{ + Address: acc.GetAddress().String(), + InstanceHash: instanceHash, + }) + + if runExisting, _ := b.mc.GetRunner(expRunnerHash); runExisting != nil { + return nil, fmt.Errorf("runner %q already exists", runExisting.Hash) + } + + // start the container + imageHash, err := build(b.container, srv, b.ipfsEndpoint) + if err != nil { + return nil, err + } + _, err = start(b.container, srv, instanceHash, expRunnerHash, imageHash, instanceEnv, b.engineName, b.port) + if err != nil { + return nil, err + } + + run, err := b.mc.CreateRunner(req) + if err != nil { + stop(b.container, expRunnerHash, srv.Dependencies) + return nil, err + } + + if !run.Hash.Equal(expRunnerHash) { + stop(b.container, expRunnerHash, srv.Dependencies) + return nil, errors.New("calculated runner hash is not the same") + } + return run, nil +} + +// Delete deletes an existing runner. +func (b *Builder) Delete(req *api.DeleteRunnerRequest) error { + // get runner before deleting it + r, err := b.mc.GetRunner(req.Hash) + if err != nil { + return err + } + + if err := b.mc.DeleteRunner(req); err != nil { + return err + } + + inst, err := b.mc.GetInstance(r.InstanceHash) + if err != nil { + return err + } + + srv, err := b.mc.GetService(inst.ServiceHash) + if err != nil { + return err + } + + // stop the local running service + if err := stop(b.container, r.Hash, srv.Dependencies); err != nil { + return err + } + + // remove local volume + if req.DeleteData { + if err := deleteData(b.container, srv); err != nil { + return err + } + } + + return nil +} diff --git a/sdk/runner/container.go b/runner/builder/container.go similarity index 99% rename from sdk/runner/container.go rename to runner/builder/container.go index 2fa2a5f79..459339a24 100644 --- a/sdk/runner/container.go +++ b/runner/builder/container.go @@ -1,4 +1,4 @@ -package runnersdk +package builder import ( "errors" diff --git a/scripts/build-mocks.sh b/scripts/build-mocks.sh deleted file mode 100755 index 5dbd5bed8..000000000 --- a/scripts/build-mocks.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -e - -# generate mocks -mockery -name ExecutionSDK -dir ./orchestrator -output ./orchestrator/mocks -mockery -name EventSDK -dir ./orchestrator -output ./orchestrator/mocks -mockery -name ProcessSDK -dir ./orchestrator -output ./orchestrator/mocks -mockery -name RunnerSDK -dir ./orchestrator -output ./orchestrator/mocks diff --git a/sdk/event/event.go b/sdk/event/event.go deleted file mode 100644 index 17862fad3..000000000 --- a/sdk/event/event.go +++ /dev/null @@ -1,64 +0,0 @@ -package eventsdk - -import ( - "fmt" - - "github.com/cskr/pubsub" - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/event" - "github.com/mesg-foundation/engine/hash" - instancepb "github.com/mesg-foundation/engine/instance" - "github.com/mesg-foundation/engine/protobuf/types" - servicesdk "github.com/mesg-foundation/engine/sdk/service" - "github.com/mesg-foundation/engine/x/instance" -) - -const ( - // streamTopic is topic used to broadcast events. - streamTopic = "event-stream" -) - -// Event exposes event APIs of MESG. -type Event struct { - ps *pubsub.PubSub - client *cosmos.Client - service *servicesdk.SDK -} - -// New creates a new Event SDK with given options. -func New(ps *pubsub.PubSub, service *servicesdk.SDK, client *cosmos.Client) *Event { - return &Event{ - ps: ps, - client: client, - service: service, - } -} - -// Create a MESG event eventKey with eventData for service token. -func (e *Event) Create(instanceHash hash.Hash, eventKey string, eventData *types.Struct) (*event.Event, error) { - event := event.Create(instanceHash, eventKey, eventData) - - var inst instancepb.Instance - if err := e.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", instance.QuerierRoute, instance.QueryGetInstance, event.InstanceHash), nil, &inst); err != nil { - return nil, err - } - - service, err := e.service.Get(inst.ServiceHash) - if err != nil { - return nil, err - } - - if err := service.RequireEventData(event.Key, event.Data); err != nil { - return nil, err - } - - go e.ps.Pub(event, streamTopic) - return event, nil -} - -// GetStream broadcasts all events. -func (e *Event) GetStream(f *Filter) *Listener { - l := NewListener(e.ps, streamTopic, f) - go l.Listen() - return l -} diff --git a/sdk/execution/sdk.go b/sdk/execution/sdk.go deleted file mode 100644 index 56271a9fb..000000000 --- a/sdk/execution/sdk.go +++ /dev/null @@ -1,118 +0,0 @@ -package executionsdk - -import ( - "context" - "fmt" - - "github.com/mesg-foundation/engine/cosmos" - executionpb "github.com/mesg-foundation/engine/execution" - "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/protobuf/api" - runnersdk "github.com/mesg-foundation/engine/sdk/runner" - servicesdk "github.com/mesg-foundation/engine/sdk/service" - "github.com/mesg-foundation/engine/x/execution" -) - -// SDK is the execution sdk. -type SDK struct { - client *cosmos.Client - - serviceSDK *servicesdk.SDK - runnerSDK *runnersdk.SDK -} - -// New returns the execution sdk. -func New(client *cosmos.Client, serviceSDK *servicesdk.SDK, runnerSDK *runnersdk.SDK) *SDK { - sdk := &SDK{ - client: client, - serviceSDK: serviceSDK, - runnerSDK: runnerSDK, - } - return sdk -} - -// Create creates a new execution. -func (s *SDK) Create(req *api.CreateExecutionRequest) (*executionpb.Execution, error) { - execution.M.Created.Add(1) - acc, err := s.client.GetAccount() - if err != nil { - return nil, err - } - msg := execution.NewMsgCreateExecution(req, acc.GetAddress()) - tx, err := s.client.BuildAndBroadcastMsg(msg) - if err != nil { - return nil, err - } - return s.Get(tx.Data) -} - -// Update updates a execution. -func (s *SDK) Update(req *api.UpdateExecutionRequest) (*executionpb.Execution, error) { - execution.M.Updated.Add(1) - acc, err := s.client.GetAccount() - if err != nil { - return nil, err - } - msg := execution.NewMsgUpdateExecution(req, acc.GetAddress()) - tx, err := s.client.BuildAndBroadcastMsg(msg) - if err != nil { - return nil, err - } - return s.Get(tx.Data) -} - -// Get returns the execution that matches given hash. -func (s *SDK) Get(hash hash.Hash) (*executionpb.Execution, error) { - var e executionpb.Execution - if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", execution.QuerierRoute, execution.QueryGetExecution, hash.String()), nil, &e); err != nil { - return nil, err - } - return &e, nil -} - -// List returns all executions. -func (s *SDK) List() ([]*executionpb.Execution, error) { - var es []*executionpb.Execution - if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s", execution.QuerierRoute, execution.QueryListExecution), nil, &es); err != nil { - return nil, err - } - return es, nil -} - -// Stream returns execution that matches given hash. -func (s *SDK) Stream(ctx context.Context, req *api.StreamExecutionRequest) (chan *executionpb.Execution, chan error, error) { - if err := req.Filter.Validate(); err != nil { - return nil, nil, err - } - - stream, serrC, err := s.client.Stream(ctx, cosmos.EventModuleQuery(execution.ModuleName)) - if err != nil { - return nil, nil, err - } - - execC := make(chan *executionpb.Execution) - errC := make(chan error) - go func() { - loop: - for { - select { - case hash := <-stream: - exec, err := s.Get(hash) - if err != nil { - errC <- err - break - } - if req.Filter.Match(exec) { - execC <- exec - } - case err := <-serrC: - errC <- err - case <-ctx.Done(): - break loop - } - } - close(errC) - close(execC) - }() - return execC, errC, nil -} diff --git a/sdk/process/sdk.go b/sdk/process/sdk.go deleted file mode 100644 index 35a110f33..000000000 --- a/sdk/process/sdk.go +++ /dev/null @@ -1,66 +0,0 @@ -package processsdk - -import ( - "fmt" - - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/hash" - processpb "github.com/mesg-foundation/engine/process" - "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/x/process" -) - -// SDK is the process sdk. -type SDK struct { - client *cosmos.Client -} - -// New creates a new Process SDK with given options. -func New(client *cosmos.Client) *SDK { - return &SDK{ - client: client, - } -} - -// Create creates a new process. -func (s *SDK) Create(req *api.CreateProcessRequest) (*processpb.Process, error) { - acc, err := s.client.GetAccount() - if err != nil { - return nil, err - } - msg := process.NewMsgCreateProcess(acc.GetAddress(), req) - tx, err := s.client.BuildAndBroadcastMsg(msg) - if err != nil { - return nil, err - } - return s.Get(tx.Data) -} - -// Delete deletes the process by hash. -func (s *SDK) Delete(req *api.DeleteProcessRequest) error { - acc, err := s.client.GetAccount() - if err != nil { - return err - } - msg := process.NewMsgDeleteProcess(acc.GetAddress(), req) - _, err = s.client.BuildAndBroadcastMsg(msg) - return err -} - -// Get returns the process that matches given hash. -func (s *SDK) Get(hash hash.Hash) (*processpb.Process, error) { - var p processpb.Process - if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", process.QuerierRoute, process.QueryGetProcess, hash.String()), nil, &p); err != nil { - return nil, err - } - return &p, nil -} - -// List returns all processes. -func (s *SDK) List() ([]*processpb.Process, error) { - var processes []*processpb.Process - if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s", process.QuerierRoute, process.QueryListProcesses), nil, &processes); err != nil { - return nil, err - } - return processes, nil -} diff --git a/sdk/runner/sdk.go b/sdk/runner/sdk.go deleted file mode 100644 index 2dc897fd0..000000000 --- a/sdk/runner/sdk.go +++ /dev/null @@ -1,177 +0,0 @@ -package runnersdk - -import ( - "errors" - "fmt" - - "github.com/mesg-foundation/engine/container" - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/ext/xos" - "github.com/mesg-foundation/engine/hash" - instancepb "github.com/mesg-foundation/engine/instance" - "github.com/mesg-foundation/engine/protobuf/api" - runnerpb "github.com/mesg-foundation/engine/runner" - servicesdk "github.com/mesg-foundation/engine/sdk/service" - "github.com/mesg-foundation/engine/x/instance" - "github.com/mesg-foundation/engine/x/runner" -) - -// SDK is the runner sdk. -type SDK struct { - serviceSDK *servicesdk.SDK - client *cosmos.Client - container container.Container - port string - engineName string - ipfsEndpoint string -} - -// Filter to apply while listing runners. -type Filter struct { - Address string - InstanceHash hash.Hash -} - -// New returns the runner sdk. -func New(client *cosmos.Client, serviceSDK *servicesdk.SDK, container container.Container, engineName, port, ipfsEndpoint string) *SDK { - sdk := &SDK{ - container: container, - serviceSDK: serviceSDK, - client: client, - port: port, - engineName: engineName, - ipfsEndpoint: ipfsEndpoint, - } - return sdk -} - -// Create creates a new runner. -func (s *SDK) Create(req *api.CreateRunnerRequest) (*runnerpb.Runner, error) { - // calculate instance's hash. - // TODO: this should be merged with the same logic currently in instance sdk - srv, err := s.serviceSDK.Get(req.ServiceHash) - if err != nil { - return nil, err - } - instanceEnv := xos.EnvMergeSlices(srv.Configuration.Env, req.Env) - envHash := hash.Dump(instanceEnv) - // TODO: should be done by instance or runner - instanceHash := hash.Dump(&instancepb.Instance{ - ServiceHash: srv.Hash, - EnvHash: envHash, - }) - acc, err := s.client.GetAccount() - if err != nil { - return nil, err - } - expRunnerHash := hash.Dump(&runnerpb.Runner{ - Address: acc.GetAddress().String(), - InstanceHash: instanceHash, - }) - - if runExisting, _ := s.Get(expRunnerHash); runExisting != nil { - return nil, fmt.Errorf("runner %q already exists", runExisting.Hash) - } - - // start the container - imageHash, err := build(s.container, srv, s.ipfsEndpoint) - if err != nil { - return nil, err - } - _, err = start(s.container, srv, instanceHash, expRunnerHash, imageHash, instanceEnv, s.engineName, s.port) - if err != nil { - return nil, err - } - onError := func() { - stop(s.container, expRunnerHash, srv.Dependencies) - } - - msg := runner.NewMsgCreateRunner(acc.GetAddress(), req.ServiceHash, envHash) - tx, err := s.client.BuildAndBroadcastMsg(msg) - if err != nil { - onError() - return nil, err - } - run, err := s.Get(tx.Data) - if err != nil { - onError() - return nil, err - } - if !run.Hash.Equal(expRunnerHash) { - onError() - return nil, errors.New("calculated runner hash is not the same") - } - return run, nil -} - -// Delete deletes an existing runner. -func (s *SDK) Delete(req *api.DeleteRunnerRequest) error { - // get runner before deleting it - r, err := s.Get(req.Hash) - if err != nil { - return err - } - acc, err := s.client.GetAccount() - if err != nil { - return err - } - msg := runner.NewMsgDeleteRunner(acc.GetAddress(), req.Hash) - _, err = s.client.BuildAndBroadcastMsg(msg) - if err != nil { - return err - } - - var inst instancepb.Instance - if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", instance.QuerierRoute, instance.QueryGetInstance, r.InstanceHash), nil, &inst); err != nil { - return err - } - - srv, err := s.serviceSDK.Get(inst.ServiceHash) - if err != nil { - return err - } - - // stop the local running service - if err := stop(s.container, r.Hash, srv.Dependencies); err != nil { - return err - } - - // remove local volume - if req.DeleteData { - if err := deleteData(s.container, srv); err != nil { - return err - } - } - - return nil -} - -// Get returns the runner that matches given hash. -func (s *SDK) Get(hash hash.Hash) (*runnerpb.Runner, error) { - var r runnerpb.Runner - if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", runner.QuerierRoute, runner.QueryGetRunner, hash), nil, &r); err != nil { - return nil, err - } - return &r, nil -} - -// List returns all runners. -func (s *SDK) List(f *Filter) ([]*runnerpb.Runner, error) { - var runners []*runnerpb.Runner - if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s", runner.QuerierRoute, runner.QueryListRunners), nil, &runners); err != nil { - return nil, err - } - // no filter, returns - if f == nil { - return runners, nil - } - // filter results - ret := make([]*runnerpb.Runner, 0) - for _, r := range runners { - if (f.Address == "" || r.Address == f.Address) && - (f.InstanceHash.IsZero() || r.InstanceHash.Equal(f.InstanceHash)) { - ret = append(ret, r) - } - } - return ret, nil -} diff --git a/sdk/sdk.go b/sdk/sdk.go deleted file mode 100644 index 5ddbdbd4d..000000000 --- a/sdk/sdk.go +++ /dev/null @@ -1,38 +0,0 @@ -package sdk - -import ( - "github.com/cskr/pubsub" - "github.com/mesg-foundation/engine/container" - "github.com/mesg-foundation/engine/cosmos" - eventsdk "github.com/mesg-foundation/engine/sdk/event" - executionsdk "github.com/mesg-foundation/engine/sdk/execution" - processesdk "github.com/mesg-foundation/engine/sdk/process" - runnersdk "github.com/mesg-foundation/engine/sdk/runner" - servicesdk "github.com/mesg-foundation/engine/sdk/service" -) - -// SDK exposes all functionalities of MESG Engine. -type SDK struct { - Service *servicesdk.SDK - Execution *executionsdk.SDK - Event *eventsdk.Event - Process *processesdk.SDK - Runner *runnersdk.SDK -} - -// New creates a new SDK with given options. -func New(client *cosmos.Client, kb *cosmos.Keybase, container container.Container, engineName, port, ipfsEndpoint string) *SDK { - ps := pubsub.New(0) - serviceSDK := servicesdk.New(client) - runnerSDK := runnersdk.New(client, serviceSDK, container, engineName, port, ipfsEndpoint) - processSDK := processesdk.New(client) - executionSDK := executionsdk.New(client, serviceSDK, runnerSDK) - eventSDK := eventsdk.New(ps, serviceSDK, client) - return &SDK{ - Service: serviceSDK, - Execution: executionSDK, - Event: eventSDK, - Process: processSDK, - Runner: runnerSDK, - } -} diff --git a/sdk/service/sdk.go b/sdk/service/sdk.go deleted file mode 100644 index 8e2ac18c7..000000000 --- a/sdk/service/sdk.go +++ /dev/null @@ -1,71 +0,0 @@ -package servicesdk - -import ( - "fmt" - - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/protobuf/api" - servicepb "github.com/mesg-foundation/engine/service" - "github.com/mesg-foundation/engine/x/service" -) - -// SDK is the service sdk. -type SDK struct { - client *cosmos.Client -} - -// New returns the service sdk. -func New(client *cosmos.Client) *SDK { - return &SDK{client: client} -} - -// Create creates a new service from definition. -func (s *SDK) Create(req *api.CreateServiceRequest) (*servicepb.Service, error) { - acc, err := s.client.GetAccount() - if err != nil { - return nil, err - } - msg := service.NewMsgCreateService(acc.GetAddress(), req) - tx, err := s.client.BuildAndBroadcastMsg(msg) - if err != nil { - return nil, err - } - return s.Get(tx.Data) -} - -// Get returns the service that matches given hash. -func (s *SDK) Get(hash hash.Hash) (*servicepb.Service, error) { - var se servicepb.Service - if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", service.QuerierRoute, service.QueryGetService, hash), nil, &se); err != nil { - return nil, err - } - return &se, nil -} - -// List returns all services. -func (s *SDK) List() ([]*servicepb.Service, error) { - var services []*servicepb.Service - if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s", service.QuerierRoute, service.QueryListService), nil, &services); err != nil { - return nil, err - } - return services, nil -} - -// Exists returns if a service already exists. -func (s *SDK) Exists(hash hash.Hash) (bool, error) { - var exists bool - if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", service.QuerierRoute, service.QueryExistService, hash), nil, &exists); err != nil { - return false, err - } - return exists, nil -} - -// Hash returns the calculate hash of a service. -func (s *SDK) Hash(req *api.CreateServiceRequest) (hash.Hash, error) { - var h hash.Hash - if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s", service.QuerierRoute, service.QueryHashService), req, &h); err != nil { - return nil, err - } - return h, nil -} diff --git a/server/grpc/api/event.go b/server/grpc/api/event.go index fe9629a72..6c918fb32 100644 --- a/server/grpc/api/event.go +++ b/server/grpc/api/event.go @@ -5,20 +5,20 @@ import ( "errors" "fmt" + "github.com/mesg-foundation/engine/event" + "github.com/mesg-foundation/engine/event/publisher" "github.com/mesg-foundation/engine/protobuf/acknowledgement" "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/sdk" - eventsdk "github.com/mesg-foundation/engine/sdk/event" ) // EventServer serve event functions. type EventServer struct { - sdk *sdk.SDK + ep *publisher.EventPublisher } // NewEventServer creates a new EventServer. -func NewEventServer(sdk *sdk.SDK) *EventServer { - return &EventServer{sdk: sdk} +func NewEventServer(ep *publisher.EventPublisher) *EventServer { + return &EventServer{ep: ep} } // Create creates a new event. @@ -27,7 +27,7 @@ func (s *EventServer) Create(ctx context.Context, req *api.CreateEventRequest) ( return nil, errors.New("create event: key missing") } - event, err := s.sdk.Event.Create(req.InstanceHash, req.Key, req.Data) + event, err := s.ep.Publish(req.InstanceHash, req.Key, req.Data) if err != nil { return nil, fmt.Errorf("create event: data %s", err) } @@ -37,15 +37,15 @@ func (s *EventServer) Create(ctx context.Context, req *api.CreateEventRequest) ( // Stream returns stream of events. func (s *EventServer) Stream(req *api.StreamEventRequest, resp api.Event_StreamServer) error { - var f *eventsdk.Filter + var f *event.Filter if req.Filter != nil { - f = &eventsdk.Filter{ + f = &event.Filter{ Hash: req.Filter.Hash, InstanceHash: req.Filter.InstanceHash, Key: req.Filter.Key, } } - stream := s.sdk.Event.GetStream(f) + stream := s.ep.GetStream(f) defer stream.Close() // send header to notify client that the stream is ready. diff --git a/server/grpc/api/execution.go b/server/grpc/api/execution.go index 2ba492db9..438d3a8dd 100644 --- a/server/grpc/api/execution.go +++ b/server/grpc/api/execution.go @@ -3,37 +3,34 @@ package api import ( "context" + "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/execution" "github.com/mesg-foundation/engine/protobuf/acknowledgement" "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/sdk" ) // ExecutionServer serve execution functions. type ExecutionServer struct { - sdk *sdk.SDK + mc *cosmos.ModuleClient } // NewExecutionServer creates a new ExecutionServer. -func NewExecutionServer(sdk *sdk.SDK) *ExecutionServer { - return &ExecutionServer{sdk: sdk} +func NewExecutionServer(mc *cosmos.ModuleClient) *ExecutionServer { + return &ExecutionServer{mc: mc} } // Create creates an execution. func (s *ExecutionServer) Create(ctx context.Context, req *api.CreateExecutionRequest) (*api.CreateExecutionResponse, error) { - exec, err := s.sdk.Execution.Create(req) + exec, err := s.mc.CreateExecution(req) if err != nil { return nil, err } - - return &api.CreateExecutionResponse{ - Hash: exec.Hash, - }, nil + return &api.CreateExecutionResponse{Hash: exec.Hash}, nil } // Get returns execution from given hash. func (s *ExecutionServer) Get(ctx context.Context, req *api.GetExecutionRequest) (*execution.Execution, error) { - return s.sdk.Execution.Get(req.Hash) + return s.mc.GetExecution(req.Hash) } // Stream returns stream of executions. @@ -41,7 +38,7 @@ func (s *ExecutionServer) Stream(req *api.StreamExecutionRequest, resp api.Execu ctx, cancel := context.WithCancel(context.Background()) defer cancel() - stream, errC, err := s.sdk.Execution.Stream(ctx, req) + stream, errC, err := s.mc.StreamExecution(ctx, req) if err != nil { return err } @@ -66,7 +63,7 @@ func (s *ExecutionServer) Stream(req *api.StreamExecutionRequest, resp api.Execu // Update updates execution from given hash. func (s *ExecutionServer) Update(ctx context.Context, req *api.UpdateExecutionRequest) (*api.UpdateExecutionResponse, error) { - if _, err := s.sdk.Execution.Update(req); err != nil { + if _, err := s.mc.UpdateExecution(req); err != nil { return nil, err } return &api.UpdateExecutionResponse{}, nil @@ -74,7 +71,7 @@ func (s *ExecutionServer) Update(ctx context.Context, req *api.UpdateExecutionRe // List returns all executions. func (s *ExecutionServer) List(ctx context.Context, req *api.ListExecutionRequest) (*api.ListExecutionResponse, error) { - executions, err := s.sdk.Execution.List() + executions, err := s.mc.ListExecution() if err != nil { return nil, err } diff --git a/server/grpc/api/instance.go b/server/grpc/api/instance.go index 645e6d6a5..14146239d 100644 --- a/server/grpc/api/instance.go +++ b/server/grpc/api/instance.go @@ -2,46 +2,32 @@ package api import ( "context" - "fmt" "github.com/mesg-foundation/engine/cosmos" instancepb "github.com/mesg-foundation/engine/instance" protobuf_api "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/sdk" - "github.com/mesg-foundation/engine/x/instance" ) // InstanceServer is the type to aggregate all Instance APIs. type InstanceServer struct { - sdk *sdk.SDK - client *cosmos.Client + mc *cosmos.ModuleClient } // NewInstanceServer creates a new ServiceServer. -func NewInstanceServer(sdk *sdk.SDK, client *cosmos.Client) *InstanceServer { - return &InstanceServer{ - sdk: sdk, - client: client, - } +func NewInstanceServer(mc *cosmos.ModuleClient) *InstanceServer { + return &InstanceServer{mc: mc} } // List instances. -func (s *InstanceServer) List(ctx context.Context, request *protobuf_api.ListInstanceRequest) (*protobuf_api.ListInstanceResponse, error) { - var instances []*instancepb.Instance - if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s", instance.QuerierRoute, instance.QueryListInstances), request.Filter, &instances); err != nil { +func (s *InstanceServer) List(ctx context.Context, req *protobuf_api.ListInstanceRequest) (*protobuf_api.ListInstanceResponse, error) { + out, err := s.mc.ListInstance(req) + if err != nil { return nil, err } - return &protobuf_api.ListInstanceResponse{Instances: instances}, nil + return &protobuf_api.ListInstanceResponse{Instances: out}, nil } // Get retrives instance. -func (s *InstanceServer) Get(ctx context.Context, request *protobuf_api.GetInstanceRequest) (*instancepb.Instance, error) { - var inst instancepb.Instance - err := s.client.QueryJSON( - fmt.Sprintf("custom/%s/%s/%s", instance.QuerierRoute, instance.QueryGetInstance, request.Hash), - nil, &inst) - if err != nil { - return nil, err - } - return &inst, nil +func (s *InstanceServer) Get(ctx context.Context, req *protobuf_api.GetInstanceRequest) (*instancepb.Instance, error) { + return s.mc.GetInstance(req.Hash) } diff --git a/server/grpc/api/ownership.go b/server/grpc/api/ownership.go index be338c5d6..9a0803de0 100644 --- a/server/grpc/api/ownership.go +++ b/server/grpc/api/ownership.go @@ -4,31 +4,24 @@ import ( "context" "github.com/mesg-foundation/engine/cosmos" - ownershippb "github.com/mesg-foundation/engine/ownership" - protobuf_api "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/sdk" - "github.com/mesg-foundation/engine/x/ownership" + "github.com/mesg-foundation/engine/protobuf/api" ) // OwnershipServer is the type to aggregate all Ownership APIs. type OwnershipServer struct { - sdk *sdk.SDK - client *cosmos.Client + mc *cosmos.ModuleClient } // NewOwnershipServer creates a new OwnershipServer. -func NewOwnershipServer(sdk *sdk.SDK, client *cosmos.Client) *OwnershipServer { - return &OwnershipServer{ - sdk: sdk, - client: client, - } +func NewOwnershipServer(mc *cosmos.ModuleClient) *OwnershipServer { + return &OwnershipServer{mc: mc} } // List returns all ownerships. -func (s *OwnershipServer) List(ctx context.Context, req *protobuf_api.ListOwnershipRequest) (*protobuf_api.ListOwnershipResponse, error) { - var ownerships []*ownershippb.Ownership - if err := s.client.QueryJSON("custom/"+ownership.QuerierRoute+"/"+ownership.QueryListOwnerships, nil, &ownerships); err != nil { +func (s *OwnershipServer) List(ctx context.Context, req *api.ListOwnershipRequest) (*api.ListOwnershipResponse, error) { + out, err := s.mc.ListOwnership() + if err != nil { return nil, err } - return &protobuf_api.ListOwnershipResponse{Ownerships: ownerships}, nil + return &api.ListOwnershipResponse{Ownerships: out}, nil } diff --git a/server/grpc/api/process.go b/server/grpc/api/process.go index 6a8598369..6434b39c0 100644 --- a/server/grpc/api/process.go +++ b/server/grpc/api/process.go @@ -3,24 +3,24 @@ package api import ( "context" + "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/process" "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/sdk" ) // ProcessServer is the type to aggregate all Service APIs. type ProcessServer struct { - sdk *sdk.SDK + mc *cosmos.ModuleClient } // NewProcessServer creates a new ProcessServer. -func NewProcessServer(sdk *sdk.SDK) *ProcessServer { - return &ProcessServer{sdk: sdk} +func NewProcessServer(mc *cosmos.ModuleClient) *ProcessServer { + return &ProcessServer{mc: mc} } // Create creates a new process. func (s *ProcessServer) Create(ctx context.Context, req *api.CreateProcessRequest) (*api.CreateProcessResponse, error) { - wf, err := s.sdk.Process.Create(req) + wf, err := s.mc.CreateProcess(req) if err != nil { return nil, err } @@ -29,21 +29,19 @@ func (s *ProcessServer) Create(ctx context.Context, req *api.CreateProcessReques // Delete deletes process by hash or sid. func (s *ProcessServer) Delete(ctx context.Context, req *api.DeleteProcessRequest) (*api.DeleteProcessResponse, error) { - return &api.DeleteProcessResponse{}, s.sdk.Process.Delete(req) + return &api.DeleteProcessResponse{}, s.mc.DeleteProcess(req) } // Get returns process from given hash. func (s *ProcessServer) Get(ctx context.Context, req *api.GetProcessRequest) (*process.Process, error) { - return s.sdk.Process.Get(req.Hash) + return s.mc.GetProcess(req.Hash) } // List returns all processes. func (s *ProcessServer) List(ctx context.Context, req *api.ListProcessRequest) (*api.ListProcessResponse, error) { - processes, err := s.sdk.Process.List() + processes, err := s.mc.ListProcess() if err != nil { return nil, err } - return &api.ListProcessResponse{ - Processes: processes, - }, nil + return &api.ListProcessResponse{Processes: processes}, nil } diff --git a/server/grpc/api/runner.go b/server/grpc/api/runner.go index 28ef58001..ebd48f545 100644 --- a/server/grpc/api/runner.go +++ b/server/grpc/api/runner.go @@ -3,57 +3,61 @@ package api import ( "context" - protobuf_api "github.com/mesg-foundation/engine/protobuf/api" + "github.com/mesg-foundation/engine/cosmos" + "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/runner" - "github.com/mesg-foundation/engine/sdk" - runnersdk "github.com/mesg-foundation/engine/sdk/runner" + "github.com/mesg-foundation/engine/runner/builder" ) // RunnerServer is the type to aggregate all Runner APIs. type RunnerServer struct { - sdk *sdk.SDK + mc *cosmos.ModuleClient + b *builder.Builder } // NewRunnerServer creates a new RunnerServer. -func NewRunnerServer(sdk *sdk.SDK) *RunnerServer { - return &RunnerServer{sdk: sdk} +func NewRunnerServer(mc *cosmos.ModuleClient, b *builder.Builder) *RunnerServer { + return &RunnerServer{ + mc: mc, + b: b, + } } // Create creates a new runner. -func (s *RunnerServer) Create(ctx context.Context, req *protobuf_api.CreateRunnerRequest) (*protobuf_api.CreateRunnerResponse, error) { - srv, err := s.sdk.Runner.Create(req) +func (s *RunnerServer) Create(ctx context.Context, req *api.CreateRunnerRequest) (*api.CreateRunnerResponse, error) { + r, err := s.b.Create(req) if err != nil { return nil, err } - return &protobuf_api.CreateRunnerResponse{Hash: srv.Hash}, nil + return &api.CreateRunnerResponse{Hash: r.Hash}, nil } // Delete deletes a runner. -func (s *RunnerServer) Delete(ctx context.Context, req *protobuf_api.DeleteRunnerRequest) (*protobuf_api.DeleteRunnerResponse, error) { - if err := s.sdk.Runner.Delete(req); err != nil { +func (s *RunnerServer) Delete(ctx context.Context, req *api.DeleteRunnerRequest) (*api.DeleteRunnerResponse, error) { + if err := s.b.Delete(req); err != nil { return nil, err } - return &protobuf_api.DeleteRunnerResponse{}, nil + return &api.DeleteRunnerResponse{}, nil } // Get returns runner from given hash. -func (s *RunnerServer) Get(ctx context.Context, req *protobuf_api.GetRunnerRequest) (*runner.Runner, error) { - return s.sdk.Runner.Get(req.Hash) +func (s *RunnerServer) Get(ctx context.Context, req *api.GetRunnerRequest) (*runner.Runner, error) { + return s.mc.GetRunner(req.Hash) } // List returns all runners. -func (s *RunnerServer) List(ctx context.Context, req *protobuf_api.ListRunnerRequest) (*protobuf_api.ListRunnerResponse, error) { - var filter *runnersdk.Filter +func (s *RunnerServer) List(ctx context.Context, req *api.ListRunnerRequest) (*api.ListRunnerResponse, error) { + var f *cosmos.FilterRunner if req.Filter != nil { - filter = &runnersdk.Filter{ + f = &cosmos.FilterRunner{ Address: req.Filter.Address, InstanceHash: req.Filter.InstanceHash, } } - runners, err := s.sdk.Runner.List(filter) + runners, err := s.mc.ListRunner(f) if err != nil { return nil, err } - return &protobuf_api.ListRunnerResponse{Runners: runners}, nil + return &api.ListRunnerResponse{Runners: runners}, nil } diff --git a/server/grpc/api/service.go b/server/grpc/api/service.go index bd15b1261..63cf2970c 100644 --- a/server/grpc/api/service.go +++ b/server/grpc/api/service.go @@ -3,24 +3,24 @@ package api import ( "context" + "github.com/mesg-foundation/engine/cosmos" protobuf_api "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/sdk" "github.com/mesg-foundation/engine/service" ) // ServiceServer is the type to aggregate all Service APIs. type ServiceServer struct { - sdk *sdk.SDK + mc *cosmos.ModuleClient } // NewServiceServer creates a new ServiceServer. -func NewServiceServer(sdk *sdk.SDK) *ServiceServer { - return &ServiceServer{sdk: sdk} +func NewServiceServer(mc *cosmos.ModuleClient) *ServiceServer { + return &ServiceServer{mc: mc} } // Create creates a new service from definition. func (s *ServiceServer) Create(ctx context.Context, req *protobuf_api.CreateServiceRequest) (*protobuf_api.CreateServiceResponse, error) { - srv, err := s.sdk.Service.Create(req) + srv, err := s.mc.CreateService(req) if err != nil { return nil, err } @@ -29,12 +29,12 @@ func (s *ServiceServer) Create(ctx context.Context, req *protobuf_api.CreateServ // Get returns service from given hash. func (s *ServiceServer) Get(ctx context.Context, req *protobuf_api.GetServiceRequest) (*service.Service, error) { - return s.sdk.Service.Get(req.Hash) + return s.mc.GetService(req.Hash) } // List returns all services. func (s *ServiceServer) List(ctx context.Context, req *protobuf_api.ListServiceRequest) (*protobuf_api.ListServiceResponse, error) { - services, err := s.sdk.Service.List() + services, err := s.mc.ListService() if err != nil { return nil, err } @@ -44,22 +44,18 @@ func (s *ServiceServer) List(ctx context.Context, req *protobuf_api.ListServiceR // Exists returns if a service already exists. func (s *ServiceServer) Exists(ctx context.Context, req *protobuf_api.ExistsServiceRequest) (*protobuf_api.ExistsServiceResponse, error) { - exists, err := s.sdk.Service.Exists(req.Hash) + exist, err := s.mc.ExistService(req.Hash) if err != nil { return nil, err } - return &protobuf_api.ExistsServiceResponse{ - Exists: exists, - }, nil + return &protobuf_api.ExistsServiceResponse{Exists: exist}, nil } // Hash returns the calculated hash of a service request. func (s *ServiceServer) Hash(ctx context.Context, req *protobuf_api.CreateServiceRequest) (*protobuf_api.HashServiceResponse, error) { - h, err := s.sdk.Service.Hash(req) + h, err := s.mc.HashService(req) if err != nil { return nil, err } - return &protobuf_api.HashServiceResponse{ - Hash: h, - }, nil + return &protobuf_api.HashServiceResponse{Hash: h}, nil } diff --git a/server/grpc/server.go b/server/grpc/server.go index d43ab13f6..aca365dd5 100644 --- a/server/grpc/server.go +++ b/server/grpc/server.go @@ -8,11 +8,11 @@ import ( grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus" grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" - "github.com/mesg-foundation/engine/config" "github.com/mesg-foundation/engine/cosmos" + "github.com/mesg-foundation/engine/event/publisher" "github.com/mesg-foundation/engine/ext/xvalidator" protobuf_api "github.com/mesg-foundation/engine/protobuf/api" - "github.com/mesg-foundation/engine/sdk" + "github.com/mesg-foundation/engine/runner/builder" "github.com/mesg-foundation/engine/server/grpc/api" "github.com/sirupsen/logrus" "google.golang.org/grpc" @@ -23,17 +23,17 @@ import ( // Server contains the server config. type Server struct { instance *grpc.Server - sdk *sdk.SDK - cfg *config.Config - client *cosmos.Client + mc *cosmos.ModuleClient + ep *publisher.EventPublisher + b *builder.Builder } // New returns a new gRPC server. -func New(sdk *sdk.SDK, cfg *config.Config, client *cosmos.Client) *Server { +func New(mc *cosmos.ModuleClient, ep *publisher.EventPublisher, b *builder.Builder) *Server { return &Server{ - sdk: sdk, - cfg: cfg, - client: client, + mc: mc, + ep: ep, + b: b, } } @@ -81,13 +81,13 @@ func validateInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryS // register all server func (s *Server) register() { - protobuf_api.RegisterEventServer(s.instance, api.NewEventServer(s.sdk)) - protobuf_api.RegisterExecutionServer(s.instance, api.NewExecutionServer(s.sdk)) - protobuf_api.RegisterInstanceServer(s.instance, api.NewInstanceServer(s.sdk, s.client)) - protobuf_api.RegisterServiceServer(s.instance, api.NewServiceServer(s.sdk)) - protobuf_api.RegisterProcessServer(s.instance, api.NewProcessServer(s.sdk)) - protobuf_api.RegisterOwnershipServer(s.instance, api.NewOwnershipServer(s.sdk, s.client)) - protobuf_api.RegisterRunnerServer(s.instance, api.NewRunnerServer(s.sdk)) + protobuf_api.RegisterEventServer(s.instance, api.NewEventServer(s.ep)) + protobuf_api.RegisterExecutionServer(s.instance, api.NewExecutionServer(s.mc)) + protobuf_api.RegisterInstanceServer(s.instance, api.NewInstanceServer(s.mc)) + protobuf_api.RegisterServiceServer(s.instance, api.NewServiceServer(s.mc)) + protobuf_api.RegisterProcessServer(s.instance, api.NewProcessServer(s.mc)) + protobuf_api.RegisterOwnershipServer(s.instance, api.NewOwnershipServer(s.mc)) + protobuf_api.RegisterRunnerServer(s.instance, api.NewRunnerServer(s.mc, s.b)) reflection.Register(s.instance) } diff --git a/x/instance/internal/keeper/querier.go b/x/instance/internal/keeper/querier.go index ace4b7df2..f4ae39726 100644 --- a/x/instance/internal/keeper/querier.go +++ b/x/instance/internal/keeper/querier.go @@ -42,14 +42,14 @@ func getInstance(ctx sdk.Context, path []string, k Keeper) ([]byte, error) { } func listInstance(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) { - var f *api.ListInstanceRequest_Filter + var f *api.ListInstanceRequest if len(req.Data) > 0 { if err := types.ModuleCdc.UnmarshalJSON(req.Data, &f); err != nil { return nil, err } } - instances, err := k.List(ctx, f) + instances, err := k.List(ctx, f.Filter) if err != nil { return nil, err } From 59e735255b9feff1fb566f682e5f1c254a42a45e Mon Sep 17 00:00:00 2001 From: krhubert Date: Mon, 17 Feb 2020 08:14:34 +0100 Subject: [PATCH 44/87] Validate hash and unmarshal in all queriers --- cosmos/errors/errors.go | 8 +++----- x/execution/internal/keeper/querier.go | 6 +++++- x/instance/internal/keeper/querier.go | 8 ++++++-- x/process/internal/keeper/querier.go | 6 +++++- x/runner/internal/keeper/querier.go | 6 +++++- x/service/internal/keeper/querier.go | 11 +++++++++-- 6 files changed, 33 insertions(+), 12 deletions(-) diff --git a/cosmos/errors/errors.go b/cosmos/errors/errors.go index 18fd5d793..473fabbac 100644 --- a/cosmos/errors/errors.go +++ b/cosmos/errors/errors.go @@ -11,17 +11,15 @@ const ( // Base mesg codes. const ( - CodeInternal uint32 = 1000 CodeValidation uint32 = 2000 - CodeMarshal uint32 = 2001 - CodeUnmarshal uint32 = 2002 + MissingHash uint32 = 2001 ) // mesg errors var ( + ErrMissingHash = sdkerrors.Register(CodespaceMesg, MissingHash, "bad request: missing hash") + ErrValidation = sdkerrors.Register(CodespaceMesg, CodeValidation, "validation failed") - ErrMarshal = sdkerrors.Register(CodespaceMesg, CodeMarshal, "failed to marshal") // TODO: to replace by cosmoserrors.ErrJSONMarshal if it makes sense - ErrUnmarshal = sdkerrors.Register(CodespaceMesg, CodeUnmarshal, "failed to unmarshal") // TODO: to replace by cosmoserrors.ErrJSONUnmarshal if it makes sense ) // NewMesgWrapError creates error with given code type and mesg codespace. diff --git a/x/execution/internal/keeper/querier.go b/x/execution/internal/keeper/querier.go index 570515ae8..ca529a928 100644 --- a/x/execution/internal/keeper/querier.go +++ b/x/execution/internal/keeper/querier.go @@ -3,6 +3,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/cosmos/errors" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/x/execution/internal/types" abci "github.com/tendermint/tendermint/abci/types" @@ -23,9 +24,12 @@ func NewQuerier(k Keeper) sdk.Querier { } func getExecution(ctx sdk.Context, k Keeper, path []string) ([]byte, error) { + if len(path) == 0 { + return nil, errors.ErrMissingHash + } hash, err := hash.Decode(path[0]) if err != nil { - return nil, err + return nil, sdkerrors.Wrap(errors.ErrValidation, err.Error()) } e, err := k.Get(ctx, hash) diff --git a/x/instance/internal/keeper/querier.go b/x/instance/internal/keeper/querier.go index f4ae39726..8c7106341 100644 --- a/x/instance/internal/keeper/querier.go +++ b/x/instance/internal/keeper/querier.go @@ -3,6 +3,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/cosmos/errors" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/x/instance/internal/types" @@ -24,9 +25,12 @@ func NewQuerier(k Keeper) sdk.Querier { } func getInstance(ctx sdk.Context, path []string, k Keeper) ([]byte, error) { + if len(path) == 0 { + return nil, errors.ErrMissingHash + } hash, err := hash.Decode(path[0]) if err != nil { - return nil, err + return nil, sdkerrors.Wrap(errors.ErrValidation, err.Error()) } instance, err := k.Get(ctx, hash) @@ -45,7 +49,7 @@ func listInstance(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, err var f *api.ListInstanceRequest if len(req.Data) > 0 { if err := types.ModuleCdc.UnmarshalJSON(req.Data, &f); err != nil { - return nil, err + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error()) } } diff --git a/x/process/internal/keeper/querier.go b/x/process/internal/keeper/querier.go index 54e735917..b86d2f6cd 100644 --- a/x/process/internal/keeper/querier.go +++ b/x/process/internal/keeper/querier.go @@ -3,6 +3,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/cosmos/errors" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/x/process/internal/types" abci "github.com/tendermint/tendermint/abci/types" @@ -23,9 +24,12 @@ func NewQuerier(k Keeper) sdk.Querier { } func getProcess(ctx sdk.Context, path []string, k Keeper) ([]byte, error) { + if len(path) == 0 { + return nil, errors.ErrMissingHash + } hash, err := hash.Decode(path[0]) if err != nil { - return nil, err + return nil, sdkerrors.Wrap(errors.ErrValidation, err.Error()) } instance, err := k.Get(ctx, hash) diff --git a/x/runner/internal/keeper/querier.go b/x/runner/internal/keeper/querier.go index 430b7d33e..29599be8e 100644 --- a/x/runner/internal/keeper/querier.go +++ b/x/runner/internal/keeper/querier.go @@ -3,6 +3,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/cosmos/errors" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/x/runner/internal/types" abci "github.com/tendermint/tendermint/abci/types" @@ -23,9 +24,12 @@ func NewQuerier(k Keeper) sdk.Querier { } func getRunner(ctx sdk.Context, path []string, k Keeper) ([]byte, error) { + if len(path) == 0 { + return nil, errors.ErrMissingHash + } hash, err := hash.Decode(path[0]) if err != nil { - return nil, err + return nil, sdkerrors.Wrap(errors.ErrValidation, err.Error()) } instance, err := k.Get(ctx, hash) diff --git a/x/service/internal/keeper/querier.go b/x/service/internal/keeper/querier.go index efe59cf7e..e3b069eb0 100644 --- a/x/service/internal/keeper/querier.go +++ b/x/service/internal/keeper/querier.go @@ -3,6 +3,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/cosmos/errors" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/x/service/internal/types" @@ -28,9 +29,12 @@ func NewQuerier(k Keeper) sdk.Querier { } func getService(ctx sdk.Context, k Keeper, path []string) ([]byte, error) { + if len(path) == 0 { + return nil, errors.ErrMissingHash + } hash, err := hash.Decode(path[0]) if err != nil { - return nil, err + return nil, sdkerrors.Wrap(errors.ErrValidation, err.Error()) } servcie, err := k.Get(ctx, hash) @@ -48,7 +52,7 @@ func getService(ctx sdk.Context, k Keeper, path []string) ([]byte, error) { func hashService(ctx sdk.Context, k Keeper, req abci.RequestQuery) ([]byte, error) { var createServiceRequest api.CreateServiceRequest if err := k.cdc.UnmarshalJSON(req.Data, &createServiceRequest); err != nil { - return nil, err + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error()) } hash := k.Hash(ctx, &createServiceRequest) @@ -74,6 +78,9 @@ func listService(ctx sdk.Context, k Keeper) ([]byte, error) { } func existService(ctx sdk.Context, k Keeper, path []string) ([]byte, error) { + if len(path) == 0 { + return nil, errors.ErrMissingHash + } hash, err := hash.Decode(path[0]) if err != nil { return nil, err From 347219a168e71dc7215415f2b54d6664d42d5998 Mon Sep 17 00:00:00 2001 From: krhubert Date: Mon, 17 Feb 2020 08:59:16 +0100 Subject: [PATCH 45/87] Add missing comments --- cosmos/module_client.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cosmos/module_client.go b/cosmos/module_client.go index 2bb8d0e5f..a0b23c788 100644 --- a/cosmos/module_client.go +++ b/cosmos/module_client.go @@ -21,10 +21,12 @@ import ( "github.com/mesg-foundation/engine/x/service" ) +// ModuleClient handles all communication with every module. type ModuleClient struct { *Client } +// NewModuleClient creates new module client. func NewModuleClient(c *Client) *ModuleClient { return &ModuleClient{Client: c} } @@ -33,7 +35,7 @@ func sroutef(format string, args ...interface{}) string { return fmt.Sprintf("custom/"+format, args...) } -// Create creates a new service from definition. +// CreateService creates a new service from definition. func (mc *ModuleClient) CreateService(req *api.CreateServiceRequest) (*servicepb.Service, error) { acc, err := mc.GetAccount() if err != nil { From d7cd80276407d5f2157aa8c25661243746bc70d8 Mon Sep 17 00:00:00 2001 From: krhubert Date: Mon, 17 Feb 2020 09:35:57 +0100 Subject: [PATCH 46/87] Add back commented orchestrator_test.go --- orchestrator/orchestrator_test.go | 260 ++++++++++++++++++++++++++++++ 1 file changed, 260 insertions(+) create mode 100644 orchestrator/orchestrator_test.go diff --git a/orchestrator/orchestrator_test.go b/orchestrator/orchestrator_test.go new file mode 100644 index 000000000..e3649bd49 --- /dev/null +++ b/orchestrator/orchestrator_test.go @@ -0,0 +1,260 @@ +package orchestrator + +// XXX: comment test because they were using sdk mocks. +// we don't have sdk now so for now keeping it commented +// TODO: add them later. + +// func TestFilter(t *testing.T) { +// o := New(&mocks.EventSDK{}, &mocks.ExecutionSDK{}, &mocks.ProcessSDK{}, &mocks.RunnerSDK{}) +// p := process.Process{ +// Hash: hash.Int(1), +// Nodes: []*process.Process_Node{ +// { +// Key: "1", +// Type: &process.Process_Node_Event_{ +// Event: &process.Process_Node_Event{ +// InstanceHash: hash.Int(1), +// EventKey: "1", +// }, +// }, +// }, +// { +// Key: "2", +// Type: &process.Process_Node_Task_{Task: &process.Process_Node_Task{ +// InstanceHash: hash.Int(2), +// TaskKey: "2", +// }}}, +// { +// Key: "3", +// Type: &process.Process_Node_Task_{Task: &process.Process_Node_Task{ +// InstanceHash: hash.Int(3), +// TaskKey: "3", +// }}}, +// }, +// Edges: []*process.Process_Edge{ +// {Src: "1", Dst: "2"}, +// {Src: "2", Dst: "3"}, +// }, +// } +// var tests = []struct { +// filter func(wf *process.Process, node *process.Process_Node) (bool, error) +// p *process.Process +// n *process.Process_Node +// res bool +// err error +// }{ +// { +// filter: o.eventFilter(&event.Event{InstanceHash: hash.Int(1), Key: "1"}), +// n: p.Nodes[0], +// res: true, +// err: nil, +// }, +// { +// filter: o.eventFilter(&event.Event{InstanceHash: hash.Int(1), Key: "2"}), +// n: p.Nodes[0], +// res: false, +// err: nil, +// }, +// { +// filter: o.eventFilter(&event.Event{InstanceHash: hash.Int(2), Key: "1"}), +// n: p.Nodes[0], +// res: false, +// err: nil, +// }, +// { +// filter: o.eventFilter(&event.Event{InstanceHash: hash.Int(2), Key: "1"}), +// n: p.Nodes[1], +// res: false, +// err: nil, +// }, +// { +// filter: o.resultFilter(&execution.Execution{InstanceHash: hash.Int(1), TaskKey: "1"}), +// n: &process.Process_Node{Type: &process.Process_Node_Result_{Result: &process.Process_Node_Result{ +// InstanceHash: hash.Int(1), +// TaskKey: "1", +// }}}, +// res: true, +// err: nil, +// }, +// { +// filter: o.resultFilter(&execution.Execution{InstanceHash: hash.Int(1), TaskKey: "1"}), +// n: &process.Process_Node{Type: &process.Process_Node_Result_{Result: &process.Process_Node_Result{ +// InstanceHash: hash.Int(1), +// TaskKey: "2", +// }}}, +// res: false, +// err: nil, +// }, +// { +// filter: o.resultFilter(&execution.Execution{InstanceHash: hash.Int(1), TaskKey: "1"}), +// n: &process.Process_Node{Type: &process.Process_Node_Result_{Result: &process.Process_Node_Result{ +// InstanceHash: hash.Int(2), +// TaskKey: "1", +// }}}, +// res: false, +// err: nil, +// }, +// { +// filter: o.resultFilter(&execution.Execution{InstanceHash: hash.Int(1), TaskKey: "1"}), +// n: p.Nodes[0], +// res: false, +// err: nil, +// }, +// { +// filter: o.dependencyFilter(&execution.Execution{InstanceHash: hash.Int(3), TaskKey: "2", ProcessHash: hash.Int(1), NodeKey: "2"}), +// p: &p, +// n: p.Nodes[2], +// res: true, +// err: nil, +// }, +// { +// filter: o.dependencyFilter(&execution.Execution{InstanceHash: hash.Int(3), TaskKey: "2", ProcessHash: hash.Int(2), NodeKey: "2"}), +// p: &p, +// n: p.Nodes[2], +// res: false, +// err: nil, +// }, +// { +// filter: o.dependencyFilter(&execution.Execution{InstanceHash: hash.Int(3), TaskKey: "2", ProcessHash: hash.Int(1), NodeKey: "1"}), +// p: &p, +// n: p.Nodes[2], +// res: false, +// err: nil, +// }, +// { +// filter: o.dependencyFilter(&execution.Execution{InstanceHash: hash.Int(3), TaskKey: "2", ProcessHash: hash.Int(1), NodeKey: "2"}), +// p: &p, +// n: p.Nodes[0], +// res: false, +// err: nil, +// }, +// } +// for _, test := range tests { +// ok, err := test.filter(test.p, test.n) +// if test.err != nil { +// require.Equal(t, test.err, err) +// } else { +// require.Equal(t, ok, test.res) +// } +// } +// } + +// func TestFindNode(t *testing.T) { +// o := New(&mocks.EventSDK{}, &mocks.ExecutionSDK{}, &mocks.ProcessSDK{}, &mocks.RunnerSDK{}) +// data := &process.Process{ +// Hash: hash.Int(1), +// Nodes: []*process.Process_Node{ +// { +// Key: "1", +// Type: &process.Process_Node_Event_{ +// Event: &process.Process_Node_Event{}, +// }, +// }, +// }, +// } +// require.Len(t, o.findNodes(data, func(p *process.Process, n *process.Process_Node) (bool, error) { +// return true, nil +// }), 1) +// require.Len(t, o.findNodes(data, func(p *process.Process, n *process.Process_Node) (bool, error) { +// return n.Key == "1", nil +// }), 1) +// require.Len(t, o.findNodes(data, func(p *process.Process, n *process.Process_Node) (bool, error) { +// return n.Key == "2", nil +// }), 0) +// } + +// // func TestProcessMap(t *testing.T) { +// // e := &mocks.ExecutionSDK{} +// // o := New(&mocks.EventSDK{}, e, &mocks.ProcessSDK{}) +// // exec := &execution.Execution{ +// // ProcessHash: hash.Int(1), +// // NodeKey: "1", +// // ParentHash: hash.Int(2), +// // Outputs: &types.Struct{ +// // Fields: map[string]*types.Value{ +// // "xxx": &types.Value{ +// // Kind: &types.Value_StringValue{StringValue: "str"}, +// // }, +// // }, +// // }, +// // } +// // o.processMap(&process.Process_Node_Map{ +// // Outputs: []*process.Process_Node_Map_Output{}, +// // }, ) +// // } + +// func TestResolveInput(t *testing.T) { +// e := &mocks.ExecutionSDK{} +// o := New(&mocks.EventSDK{}, e, &mocks.ProcessSDK{}, &mocks.RunnerSDK{}) +// exec := &execution.Execution{ +// ProcessHash: hash.Int(2), +// NodeKey: "2", +// ParentHash: hash.Int(3), +// Outputs: &types.Struct{ +// Fields: map[string]*types.Value{ +// "xxx": { +// Kind: &types.Value_StringValue{StringValue: "str"}, +// }, +// }, +// }, +// } +// // Different processes +// _, err := o.resolveInput(hash.Int(1), exec, "2", &process.Process_Node_Map_Output_Reference_Path{Selector: &process.Process_Node_Map_Output_Reference_Path_Key{Key: "xxx"}}) +// require.Error(t, err) +// // Different steps, should return the value of the data +// val, err := o.resolveInput(hash.Int(2), exec, "2", &process.Process_Node_Map_Output_Reference_Path{Selector: &process.Process_Node_Map_Output_Reference_Path_Key{Key: "xxx"}}) +// require.NoError(t, err) +// require.Equal(t, val, exec.Outputs.Fields["xxx"]) +// // Invalid execution parent hash +// e.On("Get", mock.Anything).Once().Return(nil, fmt.Errorf("err")) +// _, err = o.resolveInput(hash.Int(2), exec, "-", &process.Process_Node_Map_Output_Reference_Path{Selector: &process.Process_Node_Map_Output_Reference_Path_Key{Key: "xxx"}}) +// require.Error(t, err) +// // Output from a previous exec +// execMock := &execution.Execution{ +// NodeKey: "3", +// ProcessHash: hash.Int(2), +// Outputs: &types.Struct{ +// Fields: map[string]*types.Value{ +// "yyy": { +// Kind: &types.Value_StringValue{StringValue: "str2"}, +// }, +// }, +// }, +// } +// e.On("Get", mock.Anything).Once().Return(execMock, nil) +// val, err = o.resolveInput(hash.Int(2), exec, "3", &process.Process_Node_Map_Output_Reference_Path{Selector: &process.Process_Node_Map_Output_Reference_Path_Key{Key: "yyy"}}) +// require.NoError(t, err) +// require.Equal(t, val, execMock.Outputs.Fields["yyy"]) +// } + +// func TestProcessTask(t *testing.T) { +// e := &mocks.ExecutionSDK{} +// e.On("Create", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Once().Return(nil, nil) +// r := &mocks.RunnerSDK{} +// r.On("List", mock.Anything).Once().Return([]*runner.Runner{{Hash: hash.Int(1)}}, nil) +// o := New(&mocks.EventSDK{}, e, &mocks.ProcessSDK{}, r) +// err := o.processTask("-", &process.Process_Node_Task{ +// InstanceHash: hash.Int(1), +// TaskKey: "-", +// }, &process.Process{ +// Hash: hash.Int(2), +// }, &execution.Execution{ +// Hash: hash.Int(3), +// }, nil, &types.Struct{ +// Fields: map[string]*types.Value{}, +// }) +// require.NoError(t, err) +// e.On("Create", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Once().Return(nil, fmt.Errorf("error")) +// r.On("List", mock.Anything).Once().Return([]*runner.Runner{{Hash: hash.Int(1)}}, nil) +// err = o.processTask("-", &process.Process_Node_Task{ +// InstanceHash: hash.Int(1), +// TaskKey: "-", +// }, &process.Process{ +// Hash: hash.Int(2), +// }, nil, &event.Event{ +// Hash: hash.Int(3), +// }, &types.Struct{ +// Fields: map[string]*types.Value{}, +// }) +// require.Error(t, err) +// } From f43338d4ad8ed0bec488745ba8aaaaa664910a54 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Mon, 17 Feb 2020 16:31:19 +0700 Subject: [PATCH 47/87] fix typo --- core/main.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/main.go b/core/main.go index 8d1e439b7..1efa6742c 100644 --- a/core/main.go +++ b/core/main.go @@ -184,7 +184,7 @@ func main() { // init runner builder b := builder.New(mc, container, cfg.Name, strconv.Itoa(port), cfg.IpfsEndpoint) - // init event publiserh + // init event publisher ep := publisher.New(mc) // start tendermint node @@ -195,7 +195,6 @@ func main() { // init gRPC server. server := grpc.New(mc, ep, b) - logrus.WithField("module", "main").Infof("starting MESG Engine version %s", version.Version) go func() { From 3a6028dd2b5587d319d3017e95405107d4b6dafa Mon Sep 17 00:00:00 2001 From: krhubert Date: Mon, 17 Feb 2020 13:14:34 +0100 Subject: [PATCH 48/87] Fix panic on nil request in list instance --- x/instance/internal/keeper/querier.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/x/instance/internal/keeper/querier.go b/x/instance/internal/keeper/querier.go index 8c7106341..987df8666 100644 --- a/x/instance/internal/keeper/querier.go +++ b/x/instance/internal/keeper/querier.go @@ -46,14 +46,16 @@ func getInstance(ctx sdk.Context, path []string, k Keeper) ([]byte, error) { } func listInstance(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) { - var f *api.ListInstanceRequest + var f *api.ListInstanceRequest_Filter if len(req.Data) > 0 { - if err := types.ModuleCdc.UnmarshalJSON(req.Data, &f); err != nil { + var r *api.ListInstanceRequest + if err := types.ModuleCdc.UnmarshalJSON(req.Data, &r); err != nil { return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error()) } + f = r.Filter } - instances, err := k.List(ctx, f.Filter) + instances, err := k.List(ctx, f) if err != nil { return nil, err } From 1f2ec033f9e33bf10c9500c9a5ed189312ae83e2 Mon Sep 17 00:00:00 2001 From: krhubert Date: Tue, 18 Feb 2020 19:13:13 +0100 Subject: [PATCH 49/87] Use disable-all instead of enable-all in golangci config --- .golangci.yml | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 8fad85dc2..87d809bf8 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -26,20 +26,28 @@ issues: text: "exported var Translator should have its own declaration" linters: - enable-all: true - disable: - - depguard - - dupl - - errcheck - - funlen - - gochecknoglobals - - gocognit - - goconst - - godox - - gosec - - interfacer - - lll - - prealloc - - stylecheck - - wsl - - gochecknoinits + disable-all: true + enable: + - bodyclose + - deadcode + - dogsled + - gocritic + - gocyclo + - gofmt + - goimports + - golint + - gosimple + - govet + - ineffassign + - maligned + - misspell + - nakedret + - scopelint + - staticcheck + - structcheck + - typecheck + - unconvert + - unparam + - unused + - varcheck + - whitespace From fcc19f34c6409866ca4f7bc1841376886215926d Mon Sep 17 00:00:00 2001 From: krhubert Date: Wed, 19 Feb 2020 17:23:51 +0100 Subject: [PATCH 50/87] Add IsCoins validator --- ext/xvalidator/validator.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/ext/xvalidator/validator.go b/ext/xvalidator/validator.go index 2fd77f74b..e869a3a47 100644 --- a/ext/xvalidator/validator.go +++ b/ext/xvalidator/validator.go @@ -6,7 +6,7 @@ import ( "strconv" "strings" - cosmostypes "github.com/cosmos/cosmos-sdk/types" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/go-playground/locales/en" ut "github.com/go-playground/universal-translator" "github.com/mesg-foundation/engine/ext/xnet" @@ -67,7 +67,15 @@ func NewWithPrefix(prefix string) (*validator.Validate, ut.Translator) { validate.RegisterValidation("accaddress", IsAccAddress) validate.RegisterTranslation("accaddress", trans, func(ut ut.Translator) error { - return ut.Add("hash", "{0} must be a valid address", false) + return ut.Add("address", "{0} must be a valid address", false) + }, func(ut ut.Translator, fe validator.FieldError) string { + t, _ := ut.T(fe.Tag(), fe.Field(), prefix) + return t + }) + + validate.RegisterValidation("coins", IsCoins) + validate.RegisterTranslation("cosin", trans, func(ut ut.Translator) error { + return ut.Add("coins", "{0} must be a valid coins", false) }, func(ut ut.Translator, fe validator.FieldError) string { t, _ := ut.T(fe.Tag(), fe.Field(), prefix) return t @@ -104,18 +112,24 @@ func IsHash(fl validator.FieldLevel) bool { func IsAccAddress(fl validator.FieldLevel) bool { switch v := fl.Field(); v.Kind() { case reflect.String: - _, err := cosmostypes.AccAddressFromBech32(fl.Field().String()) + _, err := sdk.AccAddressFromBech32(fl.Field().String()) return err == nil case reflect.Slice: if v.Type().Elem().Kind() != reflect.Uint8 { // if it's not slice of bytes then break break } - return cosmostypes.VerifyAddressFormat(v.Bytes()) == nil + return sdk.VerifyAddressFormat(v.Bytes()) == nil } return false } +// IsCoins validates if given field is valid cosmos coins. +func IsCoins(fl validator.FieldLevel) bool { + _, err := sdk.ParseCoins(fl.Field().String()) + return err == nil +} + // IsDomainName validates if given field is valid domain name. func IsDomainName(fl validator.FieldLevel) bool { return xnet.IsDomainName(fl.Field().String()) From a3c11022c653075d1b66db2150bd4c9f410152b7 Mon Sep 17 00:00:00 2001 From: krhubert Date: Wed, 19 Feb 2020 17:33:47 +0100 Subject: [PATCH 51/87] Add Price field to proto Execution type --- execution/execution.go | 3 +- execution/execution.pb.go | 87 ++++++++++++----------- execution/execution_test.go | 10 +-- protobuf/api/execution.pb.go | 122 ++++++++++++++++++--------------- protobuf/api/execution.proto | 5 ++ protobuf/types/execution.proto | 5 ++ 6 files changed, 130 insertions(+), 102 deletions(-) diff --git a/execution/execution.go b/execution/execution.go index 54cafe10a..77f016200 100644 --- a/execution/execution.go +++ b/execution/execution.go @@ -6,7 +6,7 @@ import ( ) // New returns a new execution. It returns an error if inputs are invalid. -func New(processHash, instanceHash, parentHash, eventHash hash.Hash, nodeKey, taskKey string, inputs *types.Struct, tags []string, executorHash hash.Hash) *Execution { +func New(processHash, instanceHash, parentHash, eventHash hash.Hash, nodeKey, taskKey, price string, inputs *types.Struct, tags []string, executorHash hash.Hash) *Execution { exec := &Execution{ ProcessHash: processHash, EventHash: eventHash, @@ -16,6 +16,7 @@ func New(processHash, instanceHash, parentHash, eventHash hash.Hash, nodeKey, ta TaskKey: taskKey, NodeKey: nodeKey, Tags: tags, + Price: price, Status: Status_Created, ExecutorHash: executorHash, } diff --git a/execution/execution.pb.go b/execution/execution.pb.go index 57b0c7cf6..1c53e792c 100644 --- a/execution/execution.pb.go +++ b/execution/execution.pb.go @@ -95,10 +95,12 @@ type Execution struct { // node key of the process. NodeKey string `protobuf:"bytes,12,opt,name=nodeKey,proto3" json:"nodeKey,omitempty" hash:"name:12"` // runner that should execute this execution. - ExecutorHash github_com_mesg_foundation_engine_hash.Hash `protobuf:"bytes,13,opt,name=executorHash,proto3,customtype=github.com/mesg-foundation/engine/hash.Hash" json:"executorHash" hash:"name:13"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + ExecutorHash github_com_mesg_foundation_engine_hash.Hash `protobuf:"bytes,13,opt,name=executorHash,proto3,customtype=github.com/mesg-foundation/engine/hash.Hash" json:"executorHash" hash:"name:13"` + // price of running the exeuction. + Price string `protobuf:"bytes,14,opt,name=price,proto3" json:"price,omitempty" hash:"name:14" validate:"coins"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *Execution) Reset() { *m = Execution{} } @@ -133,42 +135,44 @@ func init() { func init() { proto.RegisterFile("execution.proto", fileDescriptor_776e2c5022e94aef) } var fileDescriptor_776e2c5022e94aef = []byte{ - // 549 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x94, 0xd1, 0x6e, 0xd3, 0x30, - 0x14, 0x86, 0x97, 0xad, 0x4b, 0x96, 0xd3, 0x6e, 0x14, 0x4b, 0x48, 0xd1, 0x90, 0xd6, 0xc8, 0x57, - 0x85, 0xad, 0x09, 0x6b, 0x35, 0x40, 0x95, 0x10, 0xd0, 0x31, 0x04, 0x42, 0x48, 0x28, 0x15, 0x37, - 0xdc, 0xb9, 0x89, 0x97, 0x5a, 0x6b, 0xed, 0xc8, 0x76, 0x0a, 0x7b, 0x23, 0x1e, 0x65, 0xcf, 0xc0, - 0x45, 0x25, 0x90, 0x78, 0x81, 0x3d, 0x01, 0x8a, 0xb3, 0x6c, 0xed, 0x90, 0x10, 0x9a, 0x7a, 0x97, - 0x93, 0x73, 0xfe, 0xef, 0xfc, 0x3e, 0x39, 0x31, 0xdc, 0xa3, 0xdf, 0x68, 0x9c, 0x6b, 0x26, 0x78, - 0x90, 0x49, 0xa1, 0x05, 0x82, 0x29, 0x55, 0x69, 0xa0, 0xcf, 0x33, 0xaa, 0x76, 0x71, 0x2a, 0x52, - 0x11, 0x9a, 0xf7, 0xa3, 0xfc, 0x34, 0x2c, 0x22, 0x13, 0x98, 0xa7, 0xb2, 0x7e, 0xf7, 0xe1, 0x75, - 0xda, 0x68, 0x42, 0xa5, 0x65, 0x1e, 0xeb, 0x32, 0x89, 0x7f, 0x3b, 0xe0, 0x9e, 0x54, 0x0d, 0xd0, - 0x10, 0x6a, 0x63, 0xa2, 0xc6, 0x9e, 0xe5, 0x5b, 0xed, 0xc6, 0xe0, 0xe5, 0xc5, 0xbc, 0xb5, 0xf6, - 0x63, 0xde, 0xda, 0x4f, 0x99, 0x1e, 0xe7, 0xa3, 0x20, 0x16, 0xd3, 0xb0, 0xe8, 0xdd, 0x39, 0x15, - 0x39, 0x4f, 0x48, 0xa1, 0x08, 0x29, 0x4f, 0x19, 0xa7, 0x61, 0xa1, 0x0a, 0xde, 0x11, 0x35, 0xbe, - 0x9c, 0xb7, 0xb6, 0x8a, 0xa0, 0x8f, 0x3b, 0x38, 0x32, 0x30, 0x94, 0x00, 0x64, 0x44, 0x52, 0xae, - 0x8b, 0xbc, 0xb7, 0x6e, 0xd0, 0x6f, 0xee, 0x86, 0xde, 0x2e, 0xd1, 0x9c, 0x4c, 0x69, 0xbf, 0x8b, - 0xa3, 0x05, 0x2e, 0x1a, 0x81, 0x4b, 0x67, 0x55, 0x93, 0x8d, 0x55, 0x35, 0xe9, 0xe1, 0xe8, 0x06, - 0x8b, 0x9e, 0x83, 0xad, 0x34, 0xd1, 0xb9, 0xf2, 0x6a, 0xbe, 0xd5, 0xde, 0xe9, 0xa2, 0xe0, 0xe6, - 0x53, 0x04, 0x43, 0x93, 0x19, 0x34, 0x96, 0x26, 0x70, 0x55, 0x8f, 0xc6, 0xd0, 0x60, 0x5c, 0x69, - 0xc2, 0x63, 0x6a, 0x0c, 0x6e, 0xae, 0xca, 0xe0, 0x11, 0x8e, 0x96, 0xc8, 0x68, 0x1f, 0x1c, 0x4d, - 0xd4, 0xd9, 0x07, 0x7a, 0xee, 0xd9, 0xbe, 0xd5, 0x76, 0x07, 0xf7, 0x6f, 0x29, 0x9e, 0xe2, 0xa8, - 0xaa, 0x40, 0xaf, 0xc0, 0x66, 0x3c, 0xcb, 0xb5, 0xf2, 0x1c, 0xdf, 0x6a, 0xd7, 0xbb, 0x0f, 0xca, - 0x03, 0x55, 0x0b, 0x13, 0x0c, 0xcd, 0xaa, 0xfc, 0x85, 0x78, 0x86, 0xa3, 0x2b, 0x1d, 0x7a, 0x01, - 0x8e, 0xc8, 0xb5, 0x41, 0x6c, 0xfd, 0x0b, 0xb1, 0x3c, 0x96, 0x4a, 0x83, 0x30, 0x6c, 0x52, 0x29, - 0x85, 0xf4, 0x5c, 0xe3, 0x75, 0xb9, 0xaa, 0x4c, 0xa1, 0xd7, 0x50, 0xd3, 0x24, 0x55, 0x1e, 0xf8, - 0x1b, 0x6d, 0x77, 0xd0, 0xb9, 0x9c, 0xb7, 0x1e, 0x2d, 0x78, 0x39, 0x7c, 0x82, 0xfd, 0x19, 0x99, - 0xb0, 0x84, 0x68, 0xda, 0xc7, 0x09, 0x9b, 0xd1, 0x83, 0x4c, 0x32, 0xae, 0x89, 0x8a, 0x19, 0xc3, - 0x91, 0x91, 0xa2, 0x14, 0xea, 0x99, 0x14, 0x31, 0x55, 0xca, 0x4c, 0xbf, 0x6e, 0xa6, 0x7f, 0x72, - 0xb7, 0xe9, 0xef, 0x2c, 0x36, 0x3f, 0xc4, 0xd1, 0x22, 0x19, 0x1d, 0x80, 0xc3, 0x45, 0x42, 0x8b, - 0xe9, 0x37, 0xcc, 0x89, 0xd0, 0x6d, 0x45, 0x17, 0x47, 0x55, 0x09, 0x62, 0xd0, 0x28, 0x7f, 0x6e, - 0x21, 0x8d, 0xaf, 0xed, 0x95, 0xf9, 0xea, 0xe1, 0x68, 0x09, 0xfd, 0xf8, 0x23, 0xd8, 0xe5, 0x82, - 0xa2, 0x3a, 0x38, 0x9f, 0xf9, 0x19, 0x17, 0x5f, 0x79, 0x73, 0xad, 0x08, 0x8e, 0x25, 0x25, 0x9a, - 0x26, 0x4d, 0x0b, 0xed, 0x00, 0xbc, 0xe7, 0x9f, 0xa4, 0x48, 0x25, 0x55, 0xaa, 0xb9, 0x8e, 0xb6, - 0xc1, 0x3d, 0x16, 0xd3, 0x6c, 0x42, 0x8b, 0xf4, 0x06, 0x02, 0xb0, 0xdf, 0x12, 0x36, 0xa1, 0x49, - 0xb3, 0x36, 0x38, 0xba, 0xf8, 0xb9, 0xb7, 0xf6, 0xfd, 0xd7, 0x9e, 0xf5, 0xe5, 0x3f, 0x5c, 0x5e, - 0x5f, 0x60, 0x23, 0xdb, 0xac, 0x45, 0xef, 0x4f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xe4, 0x92, 0x42, - 0xae, 0xd4, 0x04, 0x00, 0x00, + // 577 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x54, 0xcd, 0x6e, 0xd3, 0x4c, + 0x14, 0xad, 0xdb, 0xd4, 0xa9, 0x6f, 0xd2, 0x7c, 0xf9, 0x46, 0x42, 0xb2, 0x8a, 0x54, 0x5b, 0xc3, + 0x26, 0xd0, 0xd6, 0xa6, 0x29, 0xe5, 0xa7, 0x12, 0x02, 0x52, 0x8a, 0x40, 0x08, 0x09, 0xb9, 0x62, + 0xc3, 0x6e, 0x62, 0x4f, 0x9d, 0x51, 0x9b, 0x19, 0x6b, 0x66, 0x1c, 0xe8, 0xdb, 0xb0, 0xe4, 0x51, + 0xfa, 0x0c, 0x2c, 0x22, 0xc1, 0x23, 0xe4, 0x09, 0x90, 0xc7, 0x75, 0xeb, 0x14, 0x09, 0xa1, 0x2a, + 0xbb, 0xdc, 0x9c, 0x7b, 0xce, 0x3d, 0xf7, 0xf8, 0xda, 0xf0, 0x1f, 0xfd, 0x4a, 0xe3, 0x5c, 0x33, + 0xc1, 0x83, 0x4c, 0x0a, 0x2d, 0x10, 0x8c, 0xa9, 0x4a, 0x03, 0x7d, 0x9e, 0x51, 0xb5, 0x81, 0x53, + 0x91, 0x8a, 0xd0, 0xfc, 0x3f, 0xcc, 0x4f, 0xc2, 0xa2, 0x32, 0x85, 0xf9, 0x55, 0xf6, 0x6f, 0xdc, + 0xbd, 0x82, 0x0d, 0x27, 0x54, 0x5a, 0xe6, 0xb1, 0x2e, 0x41, 0xfc, 0x6d, 0x0d, 0x9c, 0xa3, 0x6a, + 0x00, 0x3a, 0x86, 0xc6, 0x88, 0xa8, 0x91, 0x6b, 0xf9, 0x56, 0xaf, 0x3d, 0x78, 0x71, 0x31, 0xf5, + 0x96, 0x7e, 0x4c, 0xbd, 0xad, 0x94, 0xe9, 0x51, 0x3e, 0x0c, 0x62, 0x31, 0x0e, 0x8b, 0xd9, 0x3b, + 0x27, 0x22, 0xe7, 0x09, 0x29, 0x18, 0x21, 0xe5, 0x29, 0xe3, 0x34, 0x2c, 0x58, 0xc1, 0x5b, 0xa2, + 0x46, 0xb3, 0xa9, 0xb7, 0x56, 0x14, 0x07, 0x78, 0x07, 0x47, 0x46, 0x0c, 0x25, 0x00, 0x19, 0x91, + 0x94, 0xeb, 0x02, 0x77, 0x97, 0x8d, 0xf4, 0xeb, 0xdb, 0x49, 0xaf, 0x97, 0xd2, 0x9c, 0x8c, 0xe9, + 0x41, 0x1f, 0x47, 0x35, 0x5d, 0x34, 0x04, 0x87, 0x4e, 0xaa, 0x21, 0x2b, 0x8b, 0x1a, 0xb2, 0x87, + 0xa3, 0x6b, 0x59, 0xf4, 0x14, 0x6c, 0xa5, 0x89, 0xce, 0x95, 0xdb, 0xf0, 0xad, 0x5e, 0xa7, 0x8f, + 0x82, 0xeb, 0x47, 0x11, 0x1c, 0x1b, 0x64, 0xd0, 0x9e, 0x4b, 0xe0, 0xb2, 0x1f, 0x8d, 0xa0, 0xcd, + 0xb8, 0xd2, 0x84, 0xc7, 0xd4, 0x18, 0x5c, 0x5d, 0x94, 0xc1, 0x7d, 0x1c, 0xcd, 0x29, 0xa3, 0x2d, + 0x68, 0x6a, 0xa2, 0x4e, 0xdf, 0xd3, 0x73, 0xd7, 0xf6, 0xad, 0x9e, 0x33, 0xf8, 0xff, 0x06, 0xe3, + 0x31, 0x8e, 0xaa, 0x0e, 0xf4, 0x12, 0x6c, 0xc6, 0xb3, 0x5c, 0x2b, 0xb7, 0xe9, 0x5b, 0xbd, 0x56, + 0xff, 0x4e, 0xb9, 0x50, 0x75, 0x30, 0xc1, 0xb1, 0x39, 0x95, 0x3f, 0x24, 0x9e, 0xe0, 0xe8, 0x92, + 0x87, 0x9e, 0x43, 0x53, 0xe4, 0xda, 0x48, 0xac, 0xfd, 0x4d, 0x62, 0x3e, 0x96, 0x8a, 0x83, 0x30, + 0xac, 0x52, 0x29, 0x85, 0x74, 0x1d, 0xe3, 0x75, 0xbe, 0xab, 0x84, 0xd0, 0x2b, 0x68, 0x68, 0x92, + 0x2a, 0x17, 0xfc, 0x95, 0x9e, 0x33, 0xd8, 0x99, 0x4d, 0xbd, 0xfb, 0x35, 0x2f, 0xbb, 0x0f, 0xb1, + 0x3f, 0x21, 0x67, 0x2c, 0x21, 0x9a, 0x1e, 0xe0, 0x84, 0x4d, 0xe8, 0x76, 0x26, 0x19, 0xd7, 0x44, + 0xc5, 0x8c, 0xe1, 0xc8, 0x50, 0x51, 0x0a, 0xad, 0x4c, 0x8a, 0x98, 0x2a, 0x65, 0xd2, 0x6f, 0x99, + 0xf4, 0x8f, 0x6e, 0x97, 0x7e, 0xa7, 0x3e, 0x7c, 0x17, 0x47, 0x75, 0x65, 0xb4, 0x0d, 0x4d, 0x2e, + 0x12, 0x5a, 0xa4, 0xdf, 0x36, 0x1b, 0xa1, 0x9b, 0x8c, 0x3e, 0x8e, 0xaa, 0x16, 0xc4, 0xa0, 0x5d, + 0xbe, 0xdc, 0x42, 0x1a, 0x5f, 0xeb, 0x0b, 0xf3, 0xb5, 0x87, 0xa3, 0x39, 0x69, 0xf4, 0x0c, 0x56, + 0x33, 0xc9, 0x62, 0xea, 0x76, 0x8c, 0xad, 0x7b, 0xb3, 0xa9, 0xe7, 0xd5, 0x09, 0x8f, 0xea, 0x29, + 0xc6, 0x82, 0x71, 0x85, 0xa3, 0x92, 0xf1, 0xe0, 0x03, 0xd8, 0xe5, 0x6d, 0xa3, 0x16, 0x34, 0x3f, + 0xf1, 0x53, 0x2e, 0xbe, 0xf0, 0xee, 0x52, 0x51, 0x1c, 0x4a, 0x4a, 0x34, 0x4d, 0xba, 0x16, 0xea, + 0x00, 0xbc, 0xe3, 0x1f, 0xa5, 0x48, 0x25, 0x55, 0xaa, 0xbb, 0x8c, 0xd6, 0xc1, 0x39, 0x14, 0xe3, + 0xec, 0x8c, 0x16, 0xf0, 0x0a, 0x02, 0xb0, 0xdf, 0x10, 0x76, 0x46, 0x93, 0x6e, 0x63, 0xb0, 0x7f, + 0xf1, 0x73, 0x73, 0xe9, 0xfb, 0xaf, 0x4d, 0xeb, 0xf3, 0x3f, 0x2c, 0x78, 0xf5, 0xed, 0x1b, 0xda, + 0xe6, 0xa2, 0xf6, 0x7e, 0x07, 0x00, 0x00, 0xff, 0xff, 0x9f, 0x6c, 0x7b, 0xe0, 0x0f, 0x05, 0x00, + 0x00, } func (this *Execution) Equal(that interface{}) bool { @@ -234,6 +238,9 @@ func (this *Execution) Equal(that interface{}) bool { if !this.ExecutorHash.Equal(that1.ExecutorHash) { return false } + if this.Price != that1.Price { + return false + } if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) { return false } diff --git a/execution/execution_test.go b/execution/execution_test.go index 20e6e9d6b..ac5f6c891 100644 --- a/execution/execution_test.go +++ b/execution/execution_test.go @@ -19,7 +19,7 @@ func TestNewFromService(t *testing.T) { tags = []string{"tag"} ) - execution := New(nil, hash, parentHash, eventHash, "", taskKey, nil, tags, nil) + execution := New(nil, hash, parentHash, eventHash, "", taskKey, "", nil, tags, nil) require.NotNil(t, execution) require.Equal(t, hash, execution.InstanceHash) require.Equal(t, parentHash, execution.ParentHash) @@ -31,7 +31,7 @@ func TestNewFromService(t *testing.T) { } func TestExecute(t *testing.T) { - e := New(nil, nil, nil, nil, "", "", nil, nil, nil) + e := New(nil, nil, nil, nil, "", "", "", nil, nil, nil) require.NoError(t, e.Execute()) require.Equal(t, Status_InProgress, e.Status) require.Error(t, e.Execute()) @@ -39,7 +39,7 @@ func TestExecute(t *testing.T) { func TestComplete(t *testing.T) { var output types.Struct - e := New(nil, nil, nil, nil, "", "", nil, nil, nil) + e := New(nil, nil, nil, nil, "", "", "", nil, nil, nil) e.Execute() require.NoError(t, e.Complete(&output)) require.Equal(t, Status_Completed, e.Status) @@ -49,7 +49,7 @@ func TestComplete(t *testing.T) { func TestFailed(t *testing.T) { err := errors.New("test") - e := New(nil, nil, nil, nil, "", "", nil, nil, nil) + e := New(nil, nil, nil, nil, "", "", "", nil, nil, nil) e.Execute() require.NoError(t, e.Failed(err)) require.Equal(t, Status_Failed, e.Status) @@ -61,7 +61,7 @@ func TestExecutionHash(t *testing.T) { ids := make(map[string]bool) f := func(instanceHash, parentHash, eventID []byte, taskKey string, tags []string) bool { - e := New(nil, instanceHash, parentHash, eventID, "", taskKey, nil, tags, nil) + e := New(nil, instanceHash, parentHash, eventID, "", taskKey, "", nil, tags, nil) if ids[string(e.Hash)] { return false } diff --git a/protobuf/api/execution.pb.go b/protobuf/api/execution.pb.go index 9fb852ad0..5c6a542c1 100644 --- a/protobuf/api/execution.pb.go +++ b/protobuf/api/execution.pb.go @@ -34,15 +34,17 @@ type CreateExecutionRequest struct { TaskKey string `protobuf:"bytes,2,opt,name=taskKey,proto3" json:"taskKey,omitempty" validate:"required,printascii"` Inputs *types.Struct `protobuf:"bytes,3,opt,name=inputs,proto3" json:"inputs,omitempty"` // tags the execution. - Tags []string `protobuf:"bytes,4,rep,name=tags,proto3" json:"tags,omitempty" validate:"dive,printascii"` - ParentHash github_com_mesg_foundation_engine_hash.Hash `protobuf:"bytes,5,opt,name=parentHash,proto3,customtype=github.com/mesg-foundation/engine/hash.Hash" json:"parentHash" validate:"omitempty,hash"` - EventHash github_com_mesg_foundation_engine_hash.Hash `protobuf:"bytes,6,opt,name=eventHash,proto3,customtype=github.com/mesg-foundation/engine/hash.Hash" json:"eventHash" validate:"omitempty,hash"` - ProcessHash github_com_mesg_foundation_engine_hash.Hash `protobuf:"bytes,7,opt,name=processHash,proto3,customtype=github.com/mesg-foundation/engine/hash.Hash" json:"processHash" validate:"omitempty,hash"` - NodeKey string `protobuf:"bytes,8,opt,name=nodeKey,proto3" json:"nodeKey,omitempty"` - ExecutorHash github_com_mesg_foundation_engine_hash.Hash `protobuf:"bytes,9,opt,name=executorHash,proto3,customtype=github.com/mesg-foundation/engine/hash.Hash" json:"executorHash" validate:"omitempty,hash"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + Tags []string `protobuf:"bytes,4,rep,name=tags,proto3" json:"tags,omitempty" validate:"dive,printascii"` + ParentHash github_com_mesg_foundation_engine_hash.Hash `protobuf:"bytes,5,opt,name=parentHash,proto3,customtype=github.com/mesg-foundation/engine/hash.Hash" json:"parentHash" validate:"omitempty,hash"` + EventHash github_com_mesg_foundation_engine_hash.Hash `protobuf:"bytes,6,opt,name=eventHash,proto3,customtype=github.com/mesg-foundation/engine/hash.Hash" json:"eventHash" validate:"omitempty,hash"` + ProcessHash github_com_mesg_foundation_engine_hash.Hash `protobuf:"bytes,7,opt,name=processHash,proto3,customtype=github.com/mesg-foundation/engine/hash.Hash" json:"processHash" validate:"omitempty,hash"` + NodeKey string `protobuf:"bytes,8,opt,name=nodeKey,proto3" json:"nodeKey,omitempty"` + ExecutorHash github_com_mesg_foundation_engine_hash.Hash `protobuf:"bytes,9,opt,name=executorHash,proto3,customtype=github.com/mesg-foundation/engine/hash.Hash" json:"executorHash" validate:"omitempty,hash"` + // price of running the execution. + Price string `protobuf:"bytes,10,opt,name=price,proto3" json:"price,omitempty" validate:"coins"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *CreateExecutionRequest) Reset() { *m = CreateExecutionRequest{} } @@ -97,6 +99,13 @@ func (m *CreateExecutionRequest) GetNodeKey() string { return "" } +func (m *CreateExecutionRequest) GetPrice() string { + if m != nil { + return m.Price + } + return "" +} + // CreateExecutionResponse defines response for execution creation. type CreateExecutionResponse struct { // Execution's hash. @@ -464,53 +473,54 @@ func init() { func init() { proto.RegisterFile("protobuf/api/execution.proto", fileDescriptor_96e2c86581f82f05) } var fileDescriptor_96e2c86581f82f05 = []byte{ - // 725 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xbc, 0x55, 0xc1, 0x72, 0x12, 0x4d, - 0x10, 0x06, 0x16, 0x16, 0xe8, 0xa4, 0xfe, 0xc3, 0xfc, 0x7f, 0xc8, 0x86, 0x5f, 0x01, 0xf7, 0x22, - 0x55, 0x26, 0x8b, 0x92, 0xf2, 0x62, 0x69, 0x59, 0x85, 0x15, 0x13, 0x4b, 0xcb, 0x58, 0x9b, 0xf2, - 0xe2, 0x6d, 0x80, 0x0e, 0x4c, 0x19, 0x76, 0x36, 0x33, 0xb3, 0x29, 0xe3, 0x3b, 0xf8, 0x0c, 0xbe, - 0x8e, 0x67, 0x8f, 0x1e, 0xb8, 0x7b, 0xe5, 0x01, 0x2c, 0x6b, 0x67, 0x80, 0x25, 0xb0, 0xc9, 0x21, - 0x55, 0xe4, 0xc6, 0x6c, 0x7f, 0x3d, 0xdf, 0xd7, 0xd3, 0xdd, 0x1f, 0x70, 0x2f, 0x14, 0x5c, 0xf1, - 0x6e, 0x74, 0xda, 0xa2, 0x21, 0x6b, 0xe1, 0x17, 0xec, 0x45, 0x8a, 0xf1, 0xc0, 0xd3, 0x9f, 0x49, - 0x69, 0x84, 0x72, 0xe0, 0xd1, 0x90, 0x55, 0xdd, 0x01, 0x1f, 0xf0, 0xd6, 0x1c, 0x1c, 0x9f, 0xf4, - 0x41, 0xff, 0x32, 0xe8, 0xea, 0xff, 0xf3, 0xb0, 0xba, 0x0c, 0x51, 0xb6, 0xa4, 0x12, 0x51, 0x4f, - 0x4d, 0x83, 0xb5, 0xa5, 0xe0, 0x12, 0x95, 0xfb, 0xbd, 0x00, 0x95, 0x57, 0x02, 0xa9, 0xc2, 0x83, - 0x59, 0xc4, 0xc7, 0xf3, 0x08, 0xa5, 0x22, 0xcf, 0xa1, 0xa8, 0xa8, 0xfc, 0xfc, 0x16, 0x2f, 0x9d, - 0x5c, 0x23, 0xdb, 0x2c, 0x77, 0xdc, 0xc9, 0xb8, 0x5e, 0xbb, 0xa0, 0x67, 0xac, 0x4f, 0x15, 0x3e, - 0x73, 0x05, 0x9e, 0x47, 0x4c, 0x60, 0x7f, 0x37, 0x14, 0x2c, 0x50, 0x54, 0xf6, 0x18, 0x73, 0xfd, - 0x59, 0x0a, 0xd9, 0x03, 0x9b, 0x05, 0x61, 0xa4, 0xa4, 0x63, 0x35, 0xb2, 0xcd, 0x8d, 0xf6, 0x96, - 0xa7, 0x8b, 0x9a, 0xc9, 0xf1, 0x4e, 0xb4, 0x4a, 0x7f, 0x0a, 0x22, 0x6d, 0xc8, 0x2b, 0x3a, 0x90, - 0x4e, 0xbe, 0x61, 0x35, 0xcb, 0x9d, 0xda, 0x64, 0x5c, 0xaf, 0x26, 0x4c, 0x7d, 0x76, 0x81, 0x57, - 0x58, 0x34, 0x96, 0x84, 0x00, 0x21, 0x15, 0x18, 0xa8, 0x23, 0x2a, 0x87, 0x4e, 0xa1, 0x91, 0x6d, - 0x6e, 0x76, 0x3e, 0xfc, 0x18, 0xd7, 0x33, 0xbf, 0xc6, 0xf5, 0x47, 0x03, 0xa6, 0x86, 0x51, 0xd7, - 0xeb, 0xf1, 0x51, 0x2b, 0x26, 0xde, 0x3b, 0xe5, 0x51, 0xd0, 0xa7, 0x71, 0x7d, 0x2d, 0x0c, 0x06, - 0x2c, 0xc0, 0xd6, 0x90, 0xca, 0xa1, 0x17, 0xa7, 0x4e, 0xc6, 0xf5, 0x9d, 0x84, 0x8c, 0x8f, 0x98, - 0xc2, 0x51, 0xa8, 0x2e, 0x77, 0x63, 0x80, 0xeb, 0x2f, 0x70, 0x90, 0x00, 0xca, 0x78, 0x31, 0x23, - 0xb4, 0xd7, 0x44, 0x98, 0x50, 0x10, 0x01, 0x1b, 0xa1, 0xe0, 0x3d, 0x94, 0x52, 0x33, 0x16, 0xd7, - 0xc4, 0xb8, 0x48, 0x42, 0x1c, 0x28, 0x06, 0xbc, 0x8f, 0x71, 0xdb, 0x4b, 0x71, 0xdb, 0xfd, 0xd9, - 0x91, 0x28, 0xd8, 0x34, 0xe3, 0xc3, 0x85, 0x96, 0x53, 0x5e, 0x93, 0x9c, 0x2b, 0x2c, 0x6e, 0x17, - 0xb6, 0x57, 0x06, 0x54, 0x86, 0x3c, 0x90, 0x48, 0x0e, 0x21, 0x1f, 0x67, 0x38, 0x59, 0x2d, 0x64, - 0xff, 0x16, 0x42, 0x7c, 0x7d, 0x81, 0xfb, 0x15, 0xfe, 0x3d, 0x44, 0xb5, 0xb2, 0x01, 0xbd, 0x2b, - 0xf7, 0x1f, 0xdf, 0xae, 0x50, 0x27, 0x65, 0x63, 0x4c, 0x9d, 0x86, 0xfb, 0xb7, 0x05, 0x95, 0x13, - 0x25, 0x90, 0x8e, 0x56, 0xf8, 0x5f, 0x82, 0x7d, 0xca, 0xce, 0x14, 0x0a, 0xad, 0x60, 0xa3, 0xfd, - 0xd0, 0x9b, 0x19, 0x83, 0x97, 0x9e, 0xe1, 0xbd, 0xd6, 0x70, 0x7f, 0x9a, 0x56, 0xfd, 0x66, 0x81, - 0x6d, 0x3e, 0x11, 0x0f, 0x4a, 0x52, 0x51, 0x15, 0x49, 0x94, 0x4e, 0xb6, 0x61, 0x35, 0xff, 0x69, - 0x13, 0x73, 0x9b, 0xf6, 0x05, 0xef, 0x44, 0xc7, 0xfc, 0x39, 0x26, 0x6e, 0x36, 0x0b, 0xa4, 0xa2, - 0x41, 0x0f, 0x75, 0xb3, 0x73, 0xeb, 0x6a, 0xf6, 0x22, 0x0b, 0xd9, 0x4f, 0x3c, 0xc7, 0xd2, 0x9e, - 0xb3, 0x33, 0x19, 0xd7, 0xb7, 0x92, 0xec, 0x54, 0xab, 0xb9, 0x8d, 0x77, 0x2c, 0xcf, 0x72, 0xe1, - 0x4e, 0x66, 0xf9, 0x67, 0x16, 0x2a, 0x1f, 0xc3, 0x7e, 0x9a, 0xdb, 0xde, 0xc5, 0xac, 0x91, 0x27, - 0x50, 0xe4, 0x91, 0xd2, 0xae, 0x9c, 0xbb, 0xc1, 0x95, 0x8f, 0x32, 0xfe, 0x0c, 0x47, 0x2a, 0x50, - 0x40, 0x21, 0xb8, 0x30, 0xfd, 0x38, 0xca, 0xf8, 0xe6, 0xd8, 0x29, 0x81, 0x2d, 0x50, 0x46, 0x67, - 0xca, 0xdd, 0x81, 0xed, 0x95, 0x9a, 0xcc, 0x82, 0xba, 0x15, 0xf8, 0xef, 0x1d, 0x93, 0x2b, 0x8b, - 0xe5, 0xbe, 0x87, 0xad, 0xa5, 0xef, 0xd3, 0x8d, 0x7e, 0x0a, 0x30, 0xff, 0x87, 0x32, 0x73, 0x3a, - 0xd7, 0x68, 0xe6, 0x34, 0x49, 0x59, 0x00, 0xb6, 0xff, 0xe4, 0xa0, 0x3c, 0x8f, 0x90, 0x63, 0xb0, - 0x8d, 0x63, 0x90, 0x46, 0xb2, 0x30, 0xe9, 0x7f, 0x72, 0xd5, 0x07, 0x37, 0x20, 0xa6, 0x45, 0x64, - 0xc8, 0x0b, 0xb0, 0x0e, 0x51, 0x91, 0xfb, 0x09, 0x36, 0xc5, 0x2d, 0xaa, 0xe9, 0x3a, 0xdd, 0x0c, - 0x79, 0x03, 0xf9, 0xb8, 0x5a, 0x52, 0x4b, 0xf2, 0xd3, 0x5e, 0xa5, 0x5a, 0xbf, 0x36, 0x3e, 0x57, - 0x72, 0x00, 0xb6, 0xd9, 0xfc, 0xc5, 0xd2, 0xd2, 0xbd, 0xe0, 0x5a, 0x3d, 0x8f, 0xb3, 0xf1, 0x0b, - 0x99, 0x96, 0x2d, 0x5e, 0x93, 0x3e, 0x98, 0x8b, 0x2f, 0x74, 0x5d, 0x9b, 0x33, 0x9d, 0xc2, 0x27, - 0x8b, 0x86, 0xac, 0x6b, 0xeb, 0x41, 0xda, 0xff, 0x1b, 0x00, 0x00, 0xff, 0xff, 0x83, 0x16, 0xca, - 0xdf, 0xdf, 0x08, 0x00, 0x00, + // 751 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xbc, 0x55, 0x41, 0x6f, 0xda, 0x48, + 0x14, 0x06, 0x0c, 0x06, 0x5e, 0xa2, 0xd5, 0x6a, 0x36, 0x21, 0x0e, 0xbb, 0x8b, 0x59, 0x5f, 0x16, + 0xed, 0x26, 0xa6, 0x25, 0xea, 0xa5, 0x6a, 0x55, 0x89, 0x2a, 0x4d, 0xaa, 0x56, 0x4d, 0xe5, 0xa8, + 0x97, 0xde, 0x06, 0xf3, 0x02, 0xa3, 0x06, 0xdb, 0x99, 0x19, 0x47, 0x4d, 0xff, 0x43, 0xff, 0x57, + 0xa5, 0xde, 0x7a, 0xec, 0x81, 0x7b, 0xaf, 0xfc, 0x80, 0xaa, 0xf2, 0x0c, 0x60, 0x02, 0x4e, 0x0e, + 0x91, 0xc8, 0xcd, 0x33, 0xef, 0x7b, 0xf3, 0xbd, 0x37, 0xf3, 0xbd, 0xcf, 0xf0, 0x57, 0xc4, 0x43, + 0x19, 0xf6, 0xe2, 0xb3, 0x36, 0x8d, 0x58, 0x1b, 0x3f, 0xa2, 0x1f, 0x4b, 0x16, 0x06, 0xae, 0xda, + 0x26, 0x95, 0x11, 0x8a, 0x81, 0x4b, 0x23, 0x56, 0x77, 0x06, 0xe1, 0x20, 0x6c, 0xcf, 0xc1, 0xc9, + 0x4a, 0x2d, 0xd4, 0x97, 0x46, 0xd7, 0xff, 0x9c, 0x87, 0xe5, 0x55, 0x84, 0xa2, 0x2d, 0x24, 0x8f, + 0x7d, 0x39, 0x0d, 0x36, 0x96, 0x82, 0x4b, 0x54, 0xce, 0xd7, 0x12, 0xd4, 0x9e, 0x73, 0xa4, 0x12, + 0x0f, 0x67, 0x11, 0x0f, 0x2f, 0x62, 0x14, 0x92, 0x3c, 0x81, 0xb2, 0xa4, 0xe2, 0xc3, 0x2b, 0xbc, + 0xb2, 0x0a, 0xcd, 0x7c, 0xab, 0xda, 0x75, 0x26, 0x63, 0xbb, 0x71, 0x49, 0xcf, 0x59, 0x9f, 0x4a, + 0x7c, 0xec, 0x70, 0xbc, 0x88, 0x19, 0xc7, 0xfe, 0x5e, 0xc4, 0x59, 0x20, 0xa9, 0xf0, 0x19, 0x73, + 0xbc, 0x59, 0x0a, 0xd9, 0x07, 0x93, 0x05, 0x51, 0x2c, 0x85, 0x65, 0x34, 0xf3, 0xad, 0x8d, 0xce, + 0xb6, 0xab, 0x9a, 0x9a, 0x95, 0xe3, 0x9e, 0xaa, 0x2a, 0xbd, 0x29, 0x88, 0x74, 0xa0, 0x28, 0xe9, + 0x40, 0x58, 0xc5, 0xa6, 0xd1, 0xaa, 0x76, 0x1b, 0x93, 0xb1, 0x5d, 0x4f, 0x99, 0xfa, 0xec, 0x12, + 0xaf, 0xb1, 0x28, 0x2c, 0x89, 0x00, 0x22, 0xca, 0x31, 0x90, 0xc7, 0x54, 0x0c, 0xad, 0x52, 0x33, + 0xdf, 0xda, 0xec, 0xbe, 0xfd, 0x32, 0xb6, 0x73, 0xdf, 0xc7, 0xf6, 0xff, 0x03, 0x26, 0x87, 0x71, + 0xcf, 0xf5, 0xc3, 0x51, 0x3b, 0x21, 0xde, 0x3f, 0x0b, 0xe3, 0xa0, 0x4f, 0x93, 0xfe, 0xda, 0x18, + 0x0c, 0x58, 0x80, 0xed, 0x21, 0x15, 0x43, 0x37, 0x49, 0x9d, 0x8c, 0xed, 0xdd, 0x94, 0x2c, 0x1c, + 0x31, 0x89, 0xa3, 0x48, 0x5e, 0xed, 0x25, 0x00, 0xc7, 0x5b, 0xe0, 0x20, 0x01, 0x54, 0xf1, 0x72, + 0x46, 0x68, 0xae, 0x89, 0x30, 0xa5, 0x20, 0x1c, 0x36, 0x22, 0x1e, 0xfa, 0x28, 0x84, 0x62, 0x2c, + 0xaf, 0x89, 0x71, 0x91, 0x84, 0x58, 0x50, 0x0e, 0xc2, 0x3e, 0x26, 0xcf, 0x5e, 0x49, 0x9e, 0xdd, + 0x9b, 0x2d, 0x89, 0x84, 0x4d, 0x2d, 0x9f, 0x90, 0xab, 0x72, 0xaa, 0x6b, 0x2a, 0xe7, 0x1a, 0x0b, + 0xf9, 0x0f, 0x4a, 0x11, 0x67, 0x3e, 0x5a, 0xa0, 0x44, 0xb8, 0x35, 0x19, 0xdb, 0xbf, 0xa7, 0xb9, + 0x7e, 0xc8, 0x02, 0xe1, 0x78, 0x1a, 0xe2, 0xf4, 0x60, 0x67, 0x45, 0xcc, 0x22, 0x0a, 0x03, 0x81, + 0xe4, 0x08, 0x8a, 0xc9, 0xe9, 0x56, 0x5e, 0x15, 0x7d, 0x70, 0x87, 0xa2, 0x3d, 0x75, 0x80, 0xf3, + 0x09, 0xfe, 0x38, 0x42, 0xb9, 0x32, 0x2d, 0xfe, 0xb5, 0xf3, 0x4f, 0xee, 0x76, 0x29, 0x56, 0xc6, + 0x74, 0xe9, 0x3b, 0xd1, 0xdc, 0x3f, 0x0c, 0xa8, 0x9d, 0x4a, 0x8e, 0x74, 0xb4, 0xc2, 0xff, 0x0c, + 0xcc, 0x33, 0x76, 0x2e, 0x91, 0xab, 0x0a, 0x36, 0x3a, 0xff, 0xba, 0x33, 0x13, 0x71, 0xb3, 0x33, + 0xdc, 0x17, 0x0a, 0xee, 0x4d, 0xd3, 0xea, 0x9f, 0x0d, 0x30, 0xf5, 0x16, 0x71, 0xa1, 0x22, 0x24, + 0x95, 0xb1, 0x40, 0x61, 0xe5, 0x9b, 0x46, 0xeb, 0xb7, 0x0e, 0xd1, 0xa7, 0x29, 0x0f, 0x71, 0x4f, + 0x55, 0xcc, 0x9b, 0x63, 0x12, 0x61, 0xb0, 0x40, 0x48, 0x1a, 0xf8, 0xa8, 0x84, 0x51, 0x58, 0x97, + 0x30, 0x16, 0x59, 0xc8, 0x41, 0xea, 0x4f, 0x86, 0x92, 0xc6, 0xee, 0x64, 0x6c, 0x6f, 0xa7, 0xd9, + 0x99, 0xb6, 0x74, 0x17, 0x9f, 0x59, 0xd6, 0x7d, 0xe9, 0x3e, 0x74, 0xef, 0x7c, 0xcb, 0x43, 0xed, + 0x5d, 0xd4, 0xcf, 0x72, 0xe6, 0xfb, 0xd0, 0x1a, 0x79, 0x08, 0xe5, 0x30, 0x96, 0xca, 0xc1, 0x0b, + 0xb7, 0x38, 0xf8, 0x71, 0xce, 0x9b, 0xe1, 0x48, 0x0d, 0x4a, 0xc8, 0x79, 0xc8, 0xf5, 0x7b, 0x1c, + 0xe7, 0x3c, 0xbd, 0xec, 0x56, 0xc0, 0xe4, 0x28, 0xe2, 0x73, 0xe9, 0xec, 0xc2, 0xce, 0x4a, 0x4f, + 0x7a, 0x40, 0x9d, 0x1a, 0x6c, 0xbd, 0x66, 0x62, 0x65, 0xb0, 0x9c, 0x37, 0xb0, 0xbd, 0xb4, 0x3f, + 0x9d, 0xe8, 0x47, 0x00, 0xf3, 0xbf, 0x99, 0xd6, 0xe9, 0xbc, 0x46, 0xad, 0xd3, 0x34, 0x65, 0x01, + 0xd8, 0xf9, 0x59, 0x80, 0xea, 0x3c, 0x42, 0x4e, 0xc0, 0xd4, 0x8e, 0x41, 0x9a, 0xe9, 0xc0, 0x64, + 0xff, 0x10, 0xeb, 0xff, 0xdc, 0x82, 0x98, 0x36, 0x91, 0x23, 0x4f, 0xc1, 0x38, 0x42, 0x49, 0xfe, + 0x4e, 0xb1, 0x19, 0x6e, 0x51, 0xcf, 0xae, 0xd3, 0xc9, 0x91, 0x97, 0x50, 0x4c, 0xba, 0x25, 0x8d, + 0x34, 0x3f, 0xeb, 0x56, 0xea, 0xf6, 0x8d, 0xf1, 0x79, 0x25, 0x87, 0x60, 0xea, 0xc9, 0x5f, 0x6c, + 0x2d, 0xdb, 0x0b, 0x6e, 0xac, 0xe7, 0x41, 0x3e, 0xb9, 0x21, 0xfd, 0x64, 0x8b, 0xc7, 0x64, 0x0b, + 0x73, 0xf1, 0x86, 0x6e, 0x7a, 0xe6, 0x5c, 0xb7, 0xf4, 0xde, 0xa0, 0x11, 0xeb, 0x99, 0x4a, 0x48, + 0x07, 0xbf, 0x02, 0x00, 0x00, 0xff, 0xff, 0xb1, 0xfc, 0x30, 0xe0, 0x0b, 0x09, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. diff --git a/protobuf/api/execution.proto b/protobuf/api/execution.proto index 9d798a344..0f7a52148 100644 --- a/protobuf/api/execution.proto +++ b/protobuf/api/execution.proto @@ -70,6 +70,11 @@ message CreateExecutionRequest { (gogoproto.customtype) = "github.com/mesg-foundation/engine/hash.Hash", (gogoproto.nullable) = false ]; + + // price of running the execution. + string price = 10 [ + (gogoproto.moretags) = 'validate:"coins"' + ]; } // CreateExecutionResponse defines response for execution creation. diff --git a/protobuf/types/execution.proto b/protobuf/types/execution.proto index 90bd60436..55aa00508 100644 --- a/protobuf/types/execution.proto +++ b/protobuf/types/execution.proto @@ -109,4 +109,9 @@ message Execution { (gogoproto.customtype) = "github.com/mesg-foundation/engine/hash.Hash", (gogoproto.nullable) = false ]; + + // price of running the exeuction. + string price = 14 [ + (gogoproto.moretags) = 'hash:"name:14" validate:"coins"' + ]; } From 3d6aacf2643c864ec6d42c560bafd4d12511402c Mon Sep 17 00:00:00 2001 From: krhubert Date: Mon, 17 Feb 2020 15:46:08 +0100 Subject: [PATCH 52/87] Add supply keeper to execution module and distribute tokens --- app/app.go | 8 +- x/execution/internal/keeper/keeper.go | 162 ++++++++++++++++-- .../internal/types/expected_keepers.go | 12 ++ x/execution/internal/types/msg.go | 9 +- x/ownership/internal/keeper/keeper.go | 20 +++ 5 files changed, 196 insertions(+), 15 deletions(-) diff --git a/app/app.go b/app/app.go index ecf28968d..118b97106 100644 --- a/app/app.go +++ b/app/app.go @@ -71,6 +71,7 @@ var ( maccPerms = map[string][]string{ auth.FeeCollectorName: nil, distr.ModuleName: nil, + execution.ModuleName: {supply.Staking}, staking.BondedPoolName: {supply.Burner, supply.Staking}, staking.NotBondedPoolName: {supply.Burner, supply.Staking}, } @@ -247,7 +248,7 @@ func NewInitApp( app.processKeeper = process.NewKeeper(app.cdc, keys[process.StoreKey], app.instanceKeeper, app.ownershipKeeper) app.serviceKeeper = service.NewKeeper(app.cdc, keys[service.StoreKey], app.ownershipKeeper) app.runnerKeeper = runner.NewKeeper(app.cdc, keys[runner.StoreKey], app.instanceKeeper) - app.executionKeeper = execution.NewKeeper(app.cdc, keys[execution.StoreKey], app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper) + app.executionKeeper = execution.NewKeeper(app.cdc, keys[execution.StoreKey], app.supplyKeeper, app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper, app.ownershipKeeper) // NOTE: Any module instantiated in the module manager that is later modified // must be passed by reference here. @@ -385,6 +386,11 @@ func (app *NewApp) ModuleAccountAddrs() map[string]bool { return modAccAddrs } +// BankKeeper returns a keeper. +func (app *NewApp) BankKeeper() bank.Keeper { + return app.bankKeeper +} + // Codec returns the application's sealed codec. func (app *NewApp) Codec() *codec.Codec { return app.cdc diff --git a/x/execution/internal/keeper/keeper.go b/x/execution/internal/keeper/keeper.go index 68fb19540..692794438 100644 --- a/x/execution/internal/keeper/keeper.go +++ b/x/execution/internal/keeper/keeper.go @@ -3,36 +3,47 @@ package keeper import ( "errors" "fmt" + "math/big" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" executionpb "github.com/mesg-foundation/engine/execution" "github.com/mesg-foundation/engine/hash" + ownershippb "github.com/mesg-foundation/engine/ownership" "github.com/mesg-foundation/engine/protobuf/api" typespb "github.com/mesg-foundation/engine/protobuf/types" "github.com/mesg-foundation/engine/x/execution/internal/types" "github.com/tendermint/tendermint/libs/log" ) +// price share for the execution runner +const runnerShare = 0.9 + // Keeper of the execution store type Keeper struct { - storeKey sdk.StoreKey - cdc *codec.Codec - serviceKeeper types.ServiceKeeper - instanceKeeper types.InstanceKeeper - runnerKeeper types.RunnerKeeper - processKeeper types.ProcessKeeper + storeKey sdk.StoreKey + cdc *codec.Codec + + supplyKeeper types.SupplyKeeper + serviceKeeper types.ServiceKeeper + instanceKeeper types.InstanceKeeper + runnerKeeper types.RunnerKeeper + processKeeper types.ProcessKeeper + ownershipKeeper types.OwnershipKeeper } // NewKeeper creates a execution keeper -func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, serviceKeeper types.ServiceKeeper, instanceKeeper types.InstanceKeeper, runnerKeeper types.RunnerKeeper, processKeeper types.ProcessKeeper) Keeper { +func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, supplyKeeper types.SupplyKeeper, serviceKeeper types.ServiceKeeper, instanceKeeper types.InstanceKeeper, runnerKeeper types.RunnerKeeper, processKeeper types.ProcessKeeper, ownershipKeeper types.OwnershipKeeper) Keeper { keeper := Keeper{ - storeKey: key, - cdc: cdc, - serviceKeeper: serviceKeeper, - instanceKeeper: instanceKeeper, - runnerKeeper: runnerKeeper, - processKeeper: processKeeper, + storeKey: key, + cdc: cdc, + supplyKeeper: supplyKeeper, + serviceKeeper: serviceKeeper, + instanceKeeper: instanceKeeper, + runnerKeeper: runnerKeeper, + processKeeper: processKeeper, + ownershipKeeper: ownershipKeeper, } return keeper } @@ -44,6 +55,10 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { // Create creates a new execution from definition. func (k *Keeper) Create(ctx sdk.Context, msg types.MsgCreateExecution) (*executionpb.Execution, error) { + price, err := sdk.ParseCoins(msg.Request.Price) + if err != nil { + return nil, err + } run, err := k.runnerKeeper.Get(ctx, msg.Request.ExecutorHash) if err != nil { return nil, err @@ -71,6 +86,7 @@ func (k *Keeper) Create(ctx sdk.Context, msg types.MsgCreateExecution) (*executi msg.Request.EventHash, msg.Request.NodeKey, msg.Request.TaskKey, + msg.Request.Price, msg.Request.Inputs, msg.Request.Tags, msg.Request.ExecutorHash, @@ -86,9 +102,15 @@ func (k *Keeper) Create(ctx sdk.Context, msg types.MsgCreateExecution) (*executi if err != nil { return nil, err } + if !ctx.IsCheckTx() { M.InProgress.Add(1) } + + if err := k.supplyKeeper.DelegateCoinsFromAccountToModule(ctx, msg.Signer, types.ModuleName, price); err != nil { + return nil, err + } + store.Set(exec.Hash, value) return exec, nil } @@ -123,13 +145,70 @@ func (k *Keeper) Update(ctx sdk.Context, msg types.MsgUpdateExecution) (*executi if err != nil { return nil, err } + if !ctx.IsCheckTx() { M.Completed.Add(1) } + + inst, err := k.instanceKeeper.Get(ctx, exec.InstanceHash) + if err != nil { + return nil, err + } + srv, err := k.serviceKeeper.Get(ctx, inst.ServiceHash) + if err != nil { + return nil, err + } + + serviceOwners, err := k.ownershipKeeper.GetOwners(ctx, srv.Hash) + if err != nil { + return nil, err + } + + addrs, err := parseAccAddresses(serviceOwners) + if err != nil { + return nil, err + } + + if err := k.distributePriceShares(ctx, msg.Executor, addrs, exec.Price); err != nil { + return nil, err + } + store.Set(exec.Hash, value) return exec, nil } +func (k *Keeper) distributePriceShares(ctx sdk.Context, runnerOwner sdk.AccAddress, serviceOwners []sdk.AccAddress, coinsStr string) error { + coins, err := sdk.ParseCoins(coinsStr) + if err != nil { + return fmt.Errorf("cannot parse coins: %w", err) + } + + // send all to runner. + if len(serviceOwners) == 0 { + return k.supplyKeeper.UndelegateCoinsFromModuleToAccount(ctx, types.ModuleName, runnerOwner, coins) + } + + runnerCoins := coinsMulFloat(coins, runnerShare) + + if err := k.supplyKeeper.UndelegateCoinsFromModuleToAccount(ctx, types.ModuleName, runnerOwner, runnerCoins); err != nil { + return sdkerrors.Wrapf(err, "cannot send coins %s to runner owner %s", runnerCoins, runnerOwner) + } + + serviceCoin, serviceRemCoin := coinsQuoRem(coins.Sub(runnerCoins), len(serviceOwners)) + + // TODO: the service with rem coins should be randomized + for i := 0; i < len(serviceOwners)-1; i++ { + if err := k.supplyKeeper.UndelegateCoinsFromModuleToAccount(ctx, types.ModuleName, serviceOwners[i], serviceCoin); err != nil { + return sdkerrors.Wrapf(err, "cannot send coins %s to service owner %s", serviceCoin, serviceOwners[1]) + } + } + + if err := k.supplyKeeper.UndelegateCoinsFromModuleToAccount(ctx, types.ModuleName, serviceOwners[len(serviceOwners)-1], serviceRemCoin); err != nil { + return sdkerrors.Wrapf(err, "cannot send rem coins %s to service owner %s", serviceRemCoin, serviceOwners[len(serviceOwners)-1]) + } + return nil +} + func (k *Keeper) validateExecutionOutput(ctx sdk.Context, instanceHash hash.Hash, taskKey string, outputs *typespb.Struct) error { inst, err := k.instanceKeeper.Get(ctx, instanceHash) if err != nil { @@ -170,3 +249,60 @@ func (k *Keeper) List(ctx sdk.Context) ([]*executionpb.Execution, error) { iter.Close() return execs, nil } + +// parseAccAddresses parses multiple accAddresses at once. +func parseAccAddresses(addresses []*ownershippb.Ownership) ([]sdk.AccAddress, error) { + var out []sdk.AccAddress + for i := range addresses { + // send to first one for now + addr, err := sdk.AccAddressFromBech32(addresses[i].Owner) + if err != nil { + return nil, err + } + out = append(out, addr) + } + return out, nil +} + +// coinsQuoRem divides coins and returns quotient and remainder. +// NOTE: if reminder is zero then it will be set to quotient. +func coinsQuoRem(coins sdk.Coins, div int) (sdk.Coins, sdk.Coins) { + var quo, rem sdk.Coins + for i := range coins { + q := sdk.Coin{ + Denom: coins[i].Denom, + Amount: coins[i].Amount.QuoRaw(int64(div)), + } + r := sdk.Coin{ + Denom: coins[i].Denom, + Amount: coins[i].Amount.ModRaw(int64(div)), + } + + quo = append(quo, q) + if r.IsZero() { + rem = append(rem, q) + } else { + rem = append(rem, r) + } + } + return quo, rem +} + +// coinsProcentage multiply coins with float. +func coinsMulFloat(coins sdk.Coins, mul float64) sdk.Coins { + var ret sdk.Coins + for i := range coins { + f := new(big.Float).SetInt(coins[i].Amount.BigInt()) + f.Mul(f, big.NewFloat(mul)) + amt := new(big.Int) + f.Int(amt) + + c := sdk.Coin{ + Denom: coins[i].Denom, + Amount: sdk.NewIntFromBigInt(amt), + } + + ret = append(ret, c) + } + return ret +} diff --git a/x/execution/internal/types/expected_keepers.go b/x/execution/internal/types/expected_keepers.go index 74acb81f9..3f0520dad 100644 --- a/x/execution/internal/types/expected_keepers.go +++ b/x/execution/internal/types/expected_keepers.go @@ -5,6 +5,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/params" "github.com/mesg-foundation/engine/hash" instancepb "github.com/mesg-foundation/engine/instance" + ownershippb "github.com/mesg-foundation/engine/ownership" processpb "github.com/mesg-foundation/engine/process" runnerpb "github.com/mesg-foundation/engine/runner" servicepb "github.com/mesg-foundation/engine/service" @@ -18,6 +19,12 @@ type ParamSubspace interface { SetParamSet(ctx sdk.Context, ps params.ParamSet) } +// SupplyKeeper module interface. +type SupplyKeeper interface { + DelegateCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error + UndelegateCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error +} + // ServiceKeeper module interface. type ServiceKeeper interface { Get(ctx sdk.Context, hash hash.Hash) (*servicepb.Service, error) @@ -37,3 +44,8 @@ type RunnerKeeper interface { type ProcessKeeper interface { Get(ctx sdk.Context, hash hash.Hash) (*processpb.Process, error) } + +// OwnershipKeeper module interface. +type OwnershipKeeper interface { + GetOwners(ctx sdk.Context, resourceHash hash.Hash) ([]*ownershippb.Ownership, error) +} diff --git a/x/execution/internal/types/msg.go b/x/execution/internal/types/msg.go index f51860211..0657d82c1 100644 --- a/x/execution/internal/types/msg.go +++ b/x/execution/internal/types/msg.go @@ -34,8 +34,15 @@ func (msg MsgCreateExecution) Type() string { // ValidateBasic runs stateless checks on the message. func (msg MsgCreateExecution) ValidateBasic() error { + price, err := sdk.ParseCoins(msg.Request.Price) + if err != nil { + return sdkerrors.Wrap(errors.ErrValidation, "cannot parse price") + } + if price.IsAnyNegative() { + return sdkerrors.Wrap(sdkerrors.ErrInsufficientFunds, "price must be positive") + } if err := xvalidator.Validate.Struct(msg); err != nil { - return err + return sdkerrors.Wrap(errors.ErrValidation, err.Error()) } if !msg.Request.ParentHash.IsZero() && !msg.Request.EventHash.IsZero() { return sdkerrors.Wrap(errors.ErrValidation, "cannot have both parent and event hash") diff --git a/x/ownership/internal/keeper/keeper.go b/x/ownership/internal/keeper/keeper.go index 7a6a3a05e..f4c5358b2 100644 --- a/x/ownership/internal/keeper/keeper.go +++ b/x/ownership/internal/keeper/keeper.go @@ -31,6 +31,26 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) } +// GetOwners returns the ownership from the keeper. +func (k Keeper) GetOwners(ctx sdk.Context, resourceHash hash.Hash) ([]*ownership.Ownership, error) { + store := ctx.KVStore(k.storeKey) + var ownerships []*ownership.Ownership + + iter := store.Iterator(nil, nil) + for ; iter.Valid(); iter.Next() { + var o *ownership.Ownership + if err := k.cdc.UnmarshalBinaryLengthPrefixed(iter.Value(), &o); err != nil { + return nil, err + } + if o.ResourceHash.Equal(resourceHash) { + ownerships = append(ownerships, o) + } + + } + iter.Close() + return ownerships, nil +} + // List returns all ownerships. func (k *Keeper) List(ctx sdk.Context) ([]*ownership.Ownership, error) { var ( From c115a1bd0cf02bf42554dfe20a6acc4b339c0752 Mon Sep 17 00:00:00 2001 From: krhubert Date: Wed, 19 Feb 2020 20:24:39 +0100 Subject: [PATCH 53/87] Add simple e2e test for execution with price --- e2e/execution_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/e2e/execution_test.go b/e2e/execution_test.go index 40d283ca5..027999696 100644 --- a/e2e/execution_test.go +++ b/e2e/execution_test.go @@ -189,6 +189,35 @@ func testExecution(t *testing.T) { }) }) + t.Run("execution with price", func(t *testing.T) { + var ( + inputs = &types.Struct{ + Fields: map[string]*types.Value{ + "msg": { + Kind: &types.Value_StringValue{ + StringValue: "test", + }, + }, + }, + } + ) + resp, err := client.ExecutionClient.Create(context.Background(), &pb.CreateExecutionRequest{ + TaskKey: "task1", + EventHash: hash.Int(1), + ExecutorHash: executorHash, + Price: "50atto", + Inputs: inputs, + }) + require.NoError(t, err) + + _, err = streamInProgress.Recv() + require.NoError(t, err) + + exec, err := streamCompleted.Recv() + require.NoError(t, err) + require.Equal(t, resp.Hash, exec.Hash) + + }) t.Run("many executions in parallel", func(t *testing.T) { var ( n = 10 From 282149eab7bcce17b12df2dffca48078cf120899 Mon Sep 17 00:00:00 2001 From: krhubert Date: Wed, 19 Feb 2020 21:47:53 +0100 Subject: [PATCH 54/87] Lint --- e2e/execution_test.go | 2 +- x/ownership/internal/keeper/keeper.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/e2e/execution_test.go b/e2e/execution_test.go index 027999696..eac8d3423 100644 --- a/e2e/execution_test.go +++ b/e2e/execution_test.go @@ -216,8 +216,8 @@ func testExecution(t *testing.T) { exec, err := streamCompleted.Recv() require.NoError(t, err) require.Equal(t, resp.Hash, exec.Hash) - }) + t.Run("many executions in parallel", func(t *testing.T) { var ( n = 10 diff --git a/x/ownership/internal/keeper/keeper.go b/x/ownership/internal/keeper/keeper.go index f4c5358b2..84883155d 100644 --- a/x/ownership/internal/keeper/keeper.go +++ b/x/ownership/internal/keeper/keeper.go @@ -45,7 +45,6 @@ func (k Keeper) GetOwners(ctx sdk.Context, resourceHash hash.Hash) ([]*ownership if o.ResourceHash.Equal(resourceHash) { ownerships = append(ownerships, o) } - } iter.Close() return ownerships, nil From 442764948af1f7aafacaef8e455ac0cf87067e48 Mon Sep 17 00:00:00 2001 From: krhubert Date: Thu, 20 Feb 2020 06:56:44 +0100 Subject: [PATCH 55/87] Fix typo + remove unused function --- app/app.go | 5 ----- ext/xvalidator/validator.go | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/app/app.go b/app/app.go index 118b97106..c4524eff2 100644 --- a/app/app.go +++ b/app/app.go @@ -386,11 +386,6 @@ func (app *NewApp) ModuleAccountAddrs() map[string]bool { return modAccAddrs } -// BankKeeper returns a keeper. -func (app *NewApp) BankKeeper() bank.Keeper { - return app.bankKeeper -} - // Codec returns the application's sealed codec. func (app *NewApp) Codec() *codec.Codec { return app.cdc diff --git a/ext/xvalidator/validator.go b/ext/xvalidator/validator.go index e869a3a47..7fd2e187e 100644 --- a/ext/xvalidator/validator.go +++ b/ext/xvalidator/validator.go @@ -74,7 +74,7 @@ func NewWithPrefix(prefix string) (*validator.Validate, ut.Translator) { }) validate.RegisterValidation("coins", IsCoins) - validate.RegisterTranslation("cosin", trans, func(ut ut.Translator) error { + validate.RegisterTranslation("coins", trans, func(ut ut.Translator) error { return ut.Add("coins", "{0} must be a valid coins", false) }, func(ut ut.Translator, fe validator.FieldError) string { t, _ := ut.T(fe.Tag(), fe.Field(), prefix) From 98202e9e341e255997caf9433fc0232abdd3c49c Mon Sep 17 00:00:00 2001 From: krhubert Date: Thu, 20 Feb 2020 20:39:55 +0100 Subject: [PATCH 56/87] Send coin directly to service instead of owners --- app/app.go | 2 +- x/execution/internal/keeper/keeper.go | 89 +++---------------- .../internal/types/expected_keepers.go | 7 +- 3 files changed, 18 insertions(+), 80 deletions(-) diff --git a/app/app.go b/app/app.go index c4524eff2..b295f20c6 100644 --- a/app/app.go +++ b/app/app.go @@ -248,7 +248,7 @@ func NewInitApp( app.processKeeper = process.NewKeeper(app.cdc, keys[process.StoreKey], app.instanceKeeper, app.ownershipKeeper) app.serviceKeeper = service.NewKeeper(app.cdc, keys[service.StoreKey], app.ownershipKeeper) app.runnerKeeper = runner.NewKeeper(app.cdc, keys[runner.StoreKey], app.instanceKeeper) - app.executionKeeper = execution.NewKeeper(app.cdc, keys[execution.StoreKey], app.supplyKeeper, app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper, app.ownershipKeeper) + app.executionKeeper = execution.NewKeeper(app.cdc, keys[execution.StoreKey], app.bankKeeper, app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper, app.ownershipKeeper) // NOTE: Any module instantiated in the module manager that is later modified // must be passed by reference here. diff --git a/x/execution/internal/keeper/keeper.go b/x/execution/internal/keeper/keeper.go index 692794438..b564b2f9e 100644 --- a/x/execution/internal/keeper/keeper.go +++ b/x/execution/internal/keeper/keeper.go @@ -10,22 +10,25 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" executionpb "github.com/mesg-foundation/engine/execution" "github.com/mesg-foundation/engine/hash" - ownershippb "github.com/mesg-foundation/engine/ownership" "github.com/mesg-foundation/engine/protobuf/api" typespb "github.com/mesg-foundation/engine/protobuf/types" "github.com/mesg-foundation/engine/x/execution/internal/types" + "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/libs/log" ) // price share for the execution runner const runnerShare = 0.9 +// ModuleAddress is the address of the module to recive coins for execution. +var ModuleAddress = sdk.AccAddress(crypto.AddressHash([]byte(types.ModuleName))) + // Keeper of the execution store type Keeper struct { storeKey sdk.StoreKey cdc *codec.Codec - supplyKeeper types.SupplyKeeper + bankKeeper types.BankKeeper serviceKeeper types.ServiceKeeper instanceKeeper types.InstanceKeeper runnerKeeper types.RunnerKeeper @@ -34,18 +37,17 @@ type Keeper struct { } // NewKeeper creates a execution keeper -func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, supplyKeeper types.SupplyKeeper, serviceKeeper types.ServiceKeeper, instanceKeeper types.InstanceKeeper, runnerKeeper types.RunnerKeeper, processKeeper types.ProcessKeeper, ownershipKeeper types.OwnershipKeeper) Keeper { - keeper := Keeper{ +func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, bankKeeper types.BankKeeper, serviceKeeper types.ServiceKeeper, instanceKeeper types.InstanceKeeper, runnerKeeper types.RunnerKeeper, processKeeper types.ProcessKeeper, ownershipKeeper types.OwnershipKeeper) Keeper { + return Keeper{ storeKey: key, cdc: cdc, - supplyKeeper: supplyKeeper, + bankKeeper: bankKeeper, serviceKeeper: serviceKeeper, instanceKeeper: instanceKeeper, runnerKeeper: runnerKeeper, processKeeper: processKeeper, ownershipKeeper: ownershipKeeper, } - return keeper } // Logger returns a module-specific logger. @@ -107,7 +109,7 @@ func (k *Keeper) Create(ctx sdk.Context, msg types.MsgCreateExecution) (*executi M.InProgress.Add(1) } - if err := k.supplyKeeper.DelegateCoinsFromAccountToModule(ctx, msg.Signer, types.ModuleName, price); err != nil { + if err := k.bankKeeper.SendCoins(ctx, msg.Signer, ModuleAddress, price); err != nil { return nil, err } @@ -159,17 +161,7 @@ func (k *Keeper) Update(ctx sdk.Context, msg types.MsgUpdateExecution) (*executi return nil, err } - serviceOwners, err := k.ownershipKeeper.GetOwners(ctx, srv.Hash) - if err != nil { - return nil, err - } - - addrs, err := parseAccAddresses(serviceOwners) - if err != nil { - return nil, err - } - - if err := k.distributePriceShares(ctx, msg.Executor, addrs, exec.Price); err != nil { + if err := k.distributePriceShares(ctx, msg.Executor, srv.Hash, exec.Price); err != nil { return nil, err } @@ -177,34 +169,19 @@ func (k *Keeper) Update(ctx sdk.Context, msg types.MsgUpdateExecution) (*executi return exec, nil } -func (k *Keeper) distributePriceShares(ctx sdk.Context, runnerOwner sdk.AccAddress, serviceOwners []sdk.AccAddress, coinsStr string) error { +func (k *Keeper) distributePriceShares(ctx sdk.Context, runnerOwner sdk.AccAddress, serviceHash hash.Hash, coinsStr string) error { coins, err := sdk.ParseCoins(coinsStr) if err != nil { return fmt.Errorf("cannot parse coins: %w", err) } - // send all to runner. - if len(serviceOwners) == 0 { - return k.supplyKeeper.UndelegateCoinsFromModuleToAccount(ctx, types.ModuleName, runnerOwner, coins) - } - runnerCoins := coinsMulFloat(coins, runnerShare) - if err := k.supplyKeeper.UndelegateCoinsFromModuleToAccount(ctx, types.ModuleName, runnerOwner, runnerCoins); err != nil { + if err := k.bankKeeper.SendCoins(ctx, ModuleAddress, runnerOwner, runnerCoins); err != nil { return sdkerrors.Wrapf(err, "cannot send coins %s to runner owner %s", runnerCoins, runnerOwner) } - - serviceCoin, serviceRemCoin := coinsQuoRem(coins.Sub(runnerCoins), len(serviceOwners)) - - // TODO: the service with rem coins should be randomized - for i := 0; i < len(serviceOwners)-1; i++ { - if err := k.supplyKeeper.UndelegateCoinsFromModuleToAccount(ctx, types.ModuleName, serviceOwners[i], serviceCoin); err != nil { - return sdkerrors.Wrapf(err, "cannot send coins %s to service owner %s", serviceCoin, serviceOwners[1]) - } - } - - if err := k.supplyKeeper.UndelegateCoinsFromModuleToAccount(ctx, types.ModuleName, serviceOwners[len(serviceOwners)-1], serviceRemCoin); err != nil { - return sdkerrors.Wrapf(err, "cannot send rem coins %s to service owner %s", serviceRemCoin, serviceOwners[len(serviceOwners)-1]) + if err := k.bankKeeper.SendCoins(ctx, ModuleAddress, sdk.AccAddress(serviceHash), coins.Sub(runnerCoins)); err != nil { + return sdkerrors.Wrapf(err, "cannot send coins %s to service %s", runnerCoins, runnerOwner) } return nil } @@ -250,44 +227,6 @@ func (k *Keeper) List(ctx sdk.Context) ([]*executionpb.Execution, error) { return execs, nil } -// parseAccAddresses parses multiple accAddresses at once. -func parseAccAddresses(addresses []*ownershippb.Ownership) ([]sdk.AccAddress, error) { - var out []sdk.AccAddress - for i := range addresses { - // send to first one for now - addr, err := sdk.AccAddressFromBech32(addresses[i].Owner) - if err != nil { - return nil, err - } - out = append(out, addr) - } - return out, nil -} - -// coinsQuoRem divides coins and returns quotient and remainder. -// NOTE: if reminder is zero then it will be set to quotient. -func coinsQuoRem(coins sdk.Coins, div int) (sdk.Coins, sdk.Coins) { - var quo, rem sdk.Coins - for i := range coins { - q := sdk.Coin{ - Denom: coins[i].Denom, - Amount: coins[i].Amount.QuoRaw(int64(div)), - } - r := sdk.Coin{ - Denom: coins[i].Denom, - Amount: coins[i].Amount.ModRaw(int64(div)), - } - - quo = append(quo, q) - if r.IsZero() { - rem = append(rem, q) - } else { - rem = append(rem, r) - } - } - return quo, rem -} - // coinsProcentage multiply coins with float. func coinsMulFloat(coins sdk.Coins, mul float64) sdk.Coins { var ret sdk.Coins diff --git a/x/execution/internal/types/expected_keepers.go b/x/execution/internal/types/expected_keepers.go index 3f0520dad..77e1897b0 100644 --- a/x/execution/internal/types/expected_keepers.go +++ b/x/execution/internal/types/expected_keepers.go @@ -19,10 +19,9 @@ type ParamSubspace interface { SetParamSet(ctx sdk.Context, ps params.ParamSet) } -// SupplyKeeper module interface. -type SupplyKeeper interface { - DelegateCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error - UndelegateCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error +// BankKeeper module interface. +type BankKeeper interface { + SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error } // ServiceKeeper module interface. From 304c185d468e25b4796ddf48d1b16973998ea08c Mon Sep 17 00:00:00 2001 From: krhubert Date: Thu, 20 Feb 2020 21:02:27 +0100 Subject: [PATCH 57/87] Use DecCoins api instead of math/big --- x/execution/internal/keeper/keeper.go | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/x/execution/internal/keeper/keeper.go b/x/execution/internal/keeper/keeper.go index b564b2f9e..3bf32964d 100644 --- a/x/execution/internal/keeper/keeper.go +++ b/x/execution/internal/keeper/keeper.go @@ -3,7 +3,6 @@ package keeper import ( "errors" "fmt" - "math/big" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -18,7 +17,7 @@ import ( ) // price share for the execution runner -const runnerShare = 0.9 +var runnerShare = sdk.NewDecWithPrec(9, 1) // ModuleAddress is the address of the module to recive coins for execution. var ModuleAddress = sdk.AccAddress(crypto.AddressHash([]byte(types.ModuleName))) @@ -175,7 +174,7 @@ func (k *Keeper) distributePriceShares(ctx sdk.Context, runnerOwner sdk.AccAddre return fmt.Errorf("cannot parse coins: %w", err) } - runnerCoins := coinsMulFloat(coins, runnerShare) + runnerCoins, _ := sdk.NewDecCoinsFromCoins(coins...).MulDecTruncate(runnerShare).TruncateDecimal() if err := k.bankKeeper.SendCoins(ctx, ModuleAddress, runnerOwner, runnerCoins); err != nil { return sdkerrors.Wrapf(err, "cannot send coins %s to runner owner %s", runnerCoins, runnerOwner) @@ -226,22 +225,3 @@ func (k *Keeper) List(ctx sdk.Context) ([]*executionpb.Execution, error) { iter.Close() return execs, nil } - -// coinsProcentage multiply coins with float. -func coinsMulFloat(coins sdk.Coins, mul float64) sdk.Coins { - var ret sdk.Coins - for i := range coins { - f := new(big.Float).SetInt(coins[i].Amount.BigInt()) - f.Mul(f, big.NewFloat(mul)) - amt := new(big.Int) - f.Int(amt) - - c := sdk.Coin{ - Denom: coins[i].Denom, - Amount: sdk.NewIntFromBigInt(amt), - } - - ret = append(ret, c) - } - return ret -} From fd70aa02946f32306b0af2fd41a604a93d83ec81 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Fri, 21 Feb 2020 11:31:43 +0700 Subject: [PATCH 58/87] send coins to resources' address --- x/execution/internal/keeper/keeper.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/x/execution/internal/keeper/keeper.go b/x/execution/internal/keeper/keeper.go index 3bf32964d..c04a59f12 100644 --- a/x/execution/internal/keeper/keeper.go +++ b/x/execution/internal/keeper/keeper.go @@ -19,9 +19,6 @@ import ( // price share for the execution runner var runnerShare = sdk.NewDecWithPrec(9, 1) -// ModuleAddress is the address of the module to recive coins for execution. -var ModuleAddress = sdk.AccAddress(crypto.AddressHash([]byte(types.ModuleName))) - // Keeper of the execution store type Keeper struct { storeKey sdk.StoreKey @@ -108,7 +105,8 @@ func (k *Keeper) Create(ctx sdk.Context, msg types.MsgCreateExecution) (*executi M.InProgress.Add(1) } - if err := k.bankKeeper.SendCoins(ctx, msg.Signer, ModuleAddress, price); err != nil { + execAddress := sdk.AccAddress(crypto.AddressHash(exec.Hash)) + if err := k.bankKeeper.SendCoins(ctx, msg.Signer, execAddress, price); err != nil { return nil, err } @@ -159,8 +157,15 @@ func (k *Keeper) Update(ctx sdk.Context, msg types.MsgUpdateExecution) (*executi if err != nil { return nil, err } + run, err := k.runnerKeeper.Get(ctx, exec.ExecutorHash) + if err != nil { + return nil, err + } - if err := k.distributePriceShares(ctx, msg.Executor, srv.Hash, exec.Price); err != nil { + execAddress := sdk.AccAddress(crypto.AddressHash(exec.Hash)) + runnerAddress := sdk.AccAddress(crypto.AddressHash(run.Hash)) + serviceAddress := sdk.AccAddress(crypto.AddressHash(srv.Hash)) + if err := k.distributePriceShares(ctx, execAddress, runnerAddress, serviceAddress, exec.Price); err != nil { return nil, err } @@ -168,19 +173,20 @@ func (k *Keeper) Update(ctx sdk.Context, msg types.MsgUpdateExecution) (*executi return exec, nil } -func (k *Keeper) distributePriceShares(ctx sdk.Context, runnerOwner sdk.AccAddress, serviceHash hash.Hash, coinsStr string) error { +func (k *Keeper) distributePriceShares(ctx sdk.Context, execAddress, runnerAddress, serviceAddress sdk.AccAddress, coinsStr string) error { coins, err := sdk.ParseCoins(coinsStr) if err != nil { return fmt.Errorf("cannot parse coins: %w", err) } runnerCoins, _ := sdk.NewDecCoinsFromCoins(coins...).MulDecTruncate(runnerShare).TruncateDecimal() + serviceCoins := coins.Sub(runnerCoins) - if err := k.bankKeeper.SendCoins(ctx, ModuleAddress, runnerOwner, runnerCoins); err != nil { - return sdkerrors.Wrapf(err, "cannot send coins %s to runner owner %s", runnerCoins, runnerOwner) + if err := k.bankKeeper.SendCoins(ctx, execAddress, runnerAddress, runnerCoins); err != nil { + return sdkerrors.Wrapf(err, "cannot send coins %s to runner %s", runnerCoins, runnerAddress) } - if err := k.bankKeeper.SendCoins(ctx, ModuleAddress, sdk.AccAddress(serviceHash), coins.Sub(runnerCoins)); err != nil { - return sdkerrors.Wrapf(err, "cannot send coins %s to service %s", runnerCoins, runnerOwner) + if err := k.bankKeeper.SendCoins(ctx, execAddress, serviceAddress, serviceCoins); err != nil { + return sdkerrors.Wrapf(err, "cannot send coins %s to service %s", runnerCoins, serviceAddress) } return nil } From df7360e584f4eff96b98045d1ba54b1b9cb50385 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Fri, 21 Feb 2020 11:46:09 +0700 Subject: [PATCH 59/87] use function bankKeeper.InputOutputCoins --- x/execution/internal/keeper/keeper.go | 16 ++++++++++------ x/execution/internal/types/expected_keepers.go | 2 ++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/x/execution/internal/keeper/keeper.go b/x/execution/internal/keeper/keeper.go index c04a59f12..43c41f749 100644 --- a/x/execution/internal/keeper/keeper.go +++ b/x/execution/internal/keeper/keeper.go @@ -7,6 +7,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/cosmos/cosmos-sdk/x/bank" executionpb "github.com/mesg-foundation/engine/execution" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/protobuf/api" @@ -173,8 +174,8 @@ func (k *Keeper) Update(ctx sdk.Context, msg types.MsgUpdateExecution) (*executi return exec, nil } -func (k *Keeper) distributePriceShares(ctx sdk.Context, execAddress, runnerAddress, serviceAddress sdk.AccAddress, coinsStr string) error { - coins, err := sdk.ParseCoins(coinsStr) +func (k *Keeper) distributePriceShares(ctx sdk.Context, execAddress, runnerAddress, serviceAddress sdk.AccAddress, price string) error { + coins, err := sdk.ParseCoins(price) if err != nil { return fmt.Errorf("cannot parse coins: %w", err) } @@ -182,11 +183,14 @@ func (k *Keeper) distributePriceShares(ctx sdk.Context, execAddress, runnerAddre runnerCoins, _ := sdk.NewDecCoinsFromCoins(coins...).MulDecTruncate(runnerShare).TruncateDecimal() serviceCoins := coins.Sub(runnerCoins) - if err := k.bankKeeper.SendCoins(ctx, execAddress, runnerAddress, runnerCoins); err != nil { - return sdkerrors.Wrapf(err, "cannot send coins %s to runner %s", runnerCoins, runnerAddress) + inputs := []bank.Input{bank.NewInput(execAddress, coins)} + outputs := []bank.Output{ + bank.NewOutput(runnerAddress, runnerCoins), + bank.NewOutput(serviceAddress, serviceCoins), } - if err := k.bankKeeper.SendCoins(ctx, execAddress, serviceAddress, serviceCoins); err != nil { - return sdkerrors.Wrapf(err, "cannot send coins %s to service %s", runnerCoins, serviceAddress) + + if err := k.bankKeeper.InputOutputCoins(ctx, inputs, outputs); err != nil { + return sdkerrors.Wrapf(err, "cannot distribute coins from execution adddress %s with inputs %s and outputs %s", execAddress, inputs, outputs) } return nil } diff --git a/x/execution/internal/types/expected_keepers.go b/x/execution/internal/types/expected_keepers.go index 77e1897b0..7b5fa9831 100644 --- a/x/execution/internal/types/expected_keepers.go +++ b/x/execution/internal/types/expected_keepers.go @@ -2,6 +2,7 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/bank" "github.com/cosmos/cosmos-sdk/x/params" "github.com/mesg-foundation/engine/hash" instancepb "github.com/mesg-foundation/engine/instance" @@ -22,6 +23,7 @@ type ParamSubspace interface { // BankKeeper module interface. type BankKeeper interface { SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error + InputOutputCoins(ctx sdk.Context, inputs []bank.Input, outputs []bank.Output) error } // ServiceKeeper module interface. From fc2498b2daa6e9c163ae88a2dbc1e840e99bbea9 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Fri, 21 Feb 2020 12:09:03 +0700 Subject: [PATCH 60/87] check if coins are empty in execution distributePriceShares function --- x/execution/internal/keeper/keeper.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/x/execution/internal/keeper/keeper.go b/x/execution/internal/keeper/keeper.go index 43c41f749..f0b614d5c 100644 --- a/x/execution/internal/keeper/keeper.go +++ b/x/execution/internal/keeper/keeper.go @@ -179,6 +179,9 @@ func (k *Keeper) distributePriceShares(ctx sdk.Context, execAddress, runnerAddre if err != nil { return fmt.Errorf("cannot parse coins: %w", err) } + if coins.Empty() { + return nil + } runnerCoins, _ := sdk.NewDecCoinsFromCoins(coins...).MulDecTruncate(runnerShare).TruncateDecimal() serviceCoins := coins.Sub(runnerCoins) From d33c7103ae934970d9db92a209b1cf051b1b13f7 Mon Sep 17 00:00:00 2001 From: krhubert Date: Mon, 24 Feb 2020 09:49:32 +0100 Subject: [PATCH 61/87] Remove ownership keeper from execution module --- app/app.go | 2 +- x/execution/internal/keeper/keeper.go | 28 +++++++++---------- .../internal/types/expected_keepers.go | 6 ---- x/ownership/internal/keeper/keeper.go | 19 ------------- 4 files changed, 14 insertions(+), 41 deletions(-) diff --git a/app/app.go b/app/app.go index b295f20c6..3dde1c4a6 100644 --- a/app/app.go +++ b/app/app.go @@ -248,7 +248,7 @@ func NewInitApp( app.processKeeper = process.NewKeeper(app.cdc, keys[process.StoreKey], app.instanceKeeper, app.ownershipKeeper) app.serviceKeeper = service.NewKeeper(app.cdc, keys[service.StoreKey], app.ownershipKeeper) app.runnerKeeper = runner.NewKeeper(app.cdc, keys[runner.StoreKey], app.instanceKeeper) - app.executionKeeper = execution.NewKeeper(app.cdc, keys[execution.StoreKey], app.bankKeeper, app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper, app.ownershipKeeper) + app.executionKeeper = execution.NewKeeper(app.cdc, keys[execution.StoreKey], app.bankKeeper, app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper) // NOTE: Any module instantiated in the module manager that is later modified // must be passed by reference here. diff --git a/x/execution/internal/keeper/keeper.go b/x/execution/internal/keeper/keeper.go index 3bf32964d..89afe8d8a 100644 --- a/x/execution/internal/keeper/keeper.go +++ b/x/execution/internal/keeper/keeper.go @@ -27,25 +27,23 @@ type Keeper struct { storeKey sdk.StoreKey cdc *codec.Codec - bankKeeper types.BankKeeper - serviceKeeper types.ServiceKeeper - instanceKeeper types.InstanceKeeper - runnerKeeper types.RunnerKeeper - processKeeper types.ProcessKeeper - ownershipKeeper types.OwnershipKeeper + bankKeeper types.BankKeeper + serviceKeeper types.ServiceKeeper + instanceKeeper types.InstanceKeeper + runnerKeeper types.RunnerKeeper + processKeeper types.ProcessKeeper } // NewKeeper creates a execution keeper -func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, bankKeeper types.BankKeeper, serviceKeeper types.ServiceKeeper, instanceKeeper types.InstanceKeeper, runnerKeeper types.RunnerKeeper, processKeeper types.ProcessKeeper, ownershipKeeper types.OwnershipKeeper) Keeper { +func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, bankKeeper types.BankKeeper, serviceKeeper types.ServiceKeeper, instanceKeeper types.InstanceKeeper, runnerKeeper types.RunnerKeeper, processKeeper types.ProcessKeeper) Keeper { return Keeper{ - storeKey: key, - cdc: cdc, - bankKeeper: bankKeeper, - serviceKeeper: serviceKeeper, - instanceKeeper: instanceKeeper, - runnerKeeper: runnerKeeper, - processKeeper: processKeeper, - ownershipKeeper: ownershipKeeper, + storeKey: key, + cdc: cdc, + bankKeeper: bankKeeper, + serviceKeeper: serviceKeeper, + instanceKeeper: instanceKeeper, + runnerKeeper: runnerKeeper, + processKeeper: processKeeper, } } diff --git a/x/execution/internal/types/expected_keepers.go b/x/execution/internal/types/expected_keepers.go index 77e1897b0..13e77b31f 100644 --- a/x/execution/internal/types/expected_keepers.go +++ b/x/execution/internal/types/expected_keepers.go @@ -5,7 +5,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/params" "github.com/mesg-foundation/engine/hash" instancepb "github.com/mesg-foundation/engine/instance" - ownershippb "github.com/mesg-foundation/engine/ownership" processpb "github.com/mesg-foundation/engine/process" runnerpb "github.com/mesg-foundation/engine/runner" servicepb "github.com/mesg-foundation/engine/service" @@ -43,8 +42,3 @@ type RunnerKeeper interface { type ProcessKeeper interface { Get(ctx sdk.Context, hash hash.Hash) (*processpb.Process, error) } - -// OwnershipKeeper module interface. -type OwnershipKeeper interface { - GetOwners(ctx sdk.Context, resourceHash hash.Hash) ([]*ownershippb.Ownership, error) -} diff --git a/x/ownership/internal/keeper/keeper.go b/x/ownership/internal/keeper/keeper.go index 84883155d..7a6a3a05e 100644 --- a/x/ownership/internal/keeper/keeper.go +++ b/x/ownership/internal/keeper/keeper.go @@ -31,25 +31,6 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) } -// GetOwners returns the ownership from the keeper. -func (k Keeper) GetOwners(ctx sdk.Context, resourceHash hash.Hash) ([]*ownership.Ownership, error) { - store := ctx.KVStore(k.storeKey) - var ownerships []*ownership.Ownership - - iter := store.Iterator(nil, nil) - for ; iter.Valid(); iter.Next() { - var o *ownership.Ownership - if err := k.cdc.UnmarshalBinaryLengthPrefixed(iter.Value(), &o); err != nil { - return nil, err - } - if o.ResourceHash.Equal(resourceHash) { - ownerships = append(ownerships, o) - } - } - iter.Close() - return ownerships, nil -} - // List returns all ownerships. func (k *Keeper) List(ctx sdk.Context) ([]*ownership.Ownership, error) { var ( From 9db0ea4418ff0448ff0031a7a762b145960293dd Mon Sep 17 00:00:00 2001 From: krhubert Date: Mon, 24 Feb 2020 20:02:13 +0100 Subject: [PATCH 62/87] Remove execution module from maac --- app/app.go | 1 - 1 file changed, 1 deletion(-) diff --git a/app/app.go b/app/app.go index 3dde1c4a6..d3361f794 100644 --- a/app/app.go +++ b/app/app.go @@ -71,7 +71,6 @@ var ( maccPerms = map[string][]string{ auth.FeeCollectorName: nil, distr.ModuleName: nil, - execution.ModuleName: {supply.Staking}, staking.BondedPoolName: {supply.Burner, supply.Staking}, staking.NotBondedPoolName: {supply.Burner, supply.Staking}, } From d0af8cc938ed495cb3b2cc982537574b88d0b22b Mon Sep 17 00:00:00 2001 From: krhubert Date: Mon, 24 Feb 2020 20:50:20 +0100 Subject: [PATCH 63/87] Add block height to execution --- execution/execution.pb.go | 84 ++++++++++++++------------- protobuf/types/execution.proto | 5 ++ x/execution/internal/keeper/keeper.go | 2 + 3 files changed, 52 insertions(+), 39 deletions(-) diff --git a/execution/execution.pb.go b/execution/execution.pb.go index 1c53e792c..70c7f5530 100644 --- a/execution/execution.pb.go +++ b/execution/execution.pb.go @@ -97,7 +97,9 @@ type Execution struct { // runner that should execute this execution. ExecutorHash github_com_mesg_foundation_engine_hash.Hash `protobuf:"bytes,13,opt,name=executorHash,proto3,customtype=github.com/mesg-foundation/engine/hash.Hash" json:"executorHash" hash:"name:13"` // price of running the exeuction. - Price string `protobuf:"bytes,14,opt,name=price,proto3" json:"price,omitempty" hash:"name:14" validate:"coins"` + Price string `protobuf:"bytes,14,opt,name=price,proto3" json:"price,omitempty" hash:"name:14" validate:"coins"` + // blockHeight where the execution was included into blockchain. + BlockHeight int64 `protobuf:"varint,15,opt,name=blockHeight,proto3" json:"blockHeight,omitempty" hash:"-"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -135,44 +137,45 @@ func init() { func init() { proto.RegisterFile("execution.proto", fileDescriptor_776e2c5022e94aef) } var fileDescriptor_776e2c5022e94aef = []byte{ - // 577 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x54, 0xcd, 0x6e, 0xd3, 0x4c, - 0x14, 0xad, 0xdb, 0xd4, 0xa9, 0x6f, 0xd2, 0x7c, 0xf9, 0x46, 0x42, 0xb2, 0x8a, 0x54, 0x5b, 0xc3, - 0x26, 0xd0, 0xd6, 0xa6, 0x29, 0xe5, 0xa7, 0x12, 0x02, 0x52, 0x8a, 0x40, 0x08, 0x09, 0xb9, 0x62, - 0xc3, 0x6e, 0x62, 0x4f, 0x9d, 0x51, 0x9b, 0x19, 0x6b, 0x66, 0x1c, 0xe8, 0xdb, 0xb0, 0xe4, 0x51, - 0xfa, 0x0c, 0x2c, 0x22, 0xc1, 0x23, 0xe4, 0x09, 0x90, 0xc7, 0x75, 0xeb, 0x14, 0x09, 0xa1, 0x2a, - 0xbb, 0xdc, 0x9c, 0x7b, 0xce, 0x3d, 0xf7, 0xf8, 0xda, 0xf0, 0x1f, 0xfd, 0x4a, 0xe3, 0x5c, 0x33, - 0xc1, 0x83, 0x4c, 0x0a, 0x2d, 0x10, 0x8c, 0xa9, 0x4a, 0x03, 0x7d, 0x9e, 0x51, 0xb5, 0x81, 0x53, - 0x91, 0x8a, 0xd0, 0xfc, 0x3f, 0xcc, 0x4f, 0xc2, 0xa2, 0x32, 0x85, 0xf9, 0x55, 0xf6, 0x6f, 0xdc, - 0xbd, 0x82, 0x0d, 0x27, 0x54, 0x5a, 0xe6, 0xb1, 0x2e, 0x41, 0xfc, 0x6d, 0x0d, 0x9c, 0xa3, 0x6a, - 0x00, 0x3a, 0x86, 0xc6, 0x88, 0xa8, 0x91, 0x6b, 0xf9, 0x56, 0xaf, 0x3d, 0x78, 0x71, 0x31, 0xf5, - 0x96, 0x7e, 0x4c, 0xbd, 0xad, 0x94, 0xe9, 0x51, 0x3e, 0x0c, 0x62, 0x31, 0x0e, 0x8b, 0xd9, 0x3b, - 0x27, 0x22, 0xe7, 0x09, 0x29, 0x18, 0x21, 0xe5, 0x29, 0xe3, 0x34, 0x2c, 0x58, 0xc1, 0x5b, 0xa2, - 0x46, 0xb3, 0xa9, 0xb7, 0x56, 0x14, 0x07, 0x78, 0x07, 0x47, 0x46, 0x0c, 0x25, 0x00, 0x19, 0x91, - 0x94, 0xeb, 0x02, 0x77, 0x97, 0x8d, 0xf4, 0xeb, 0xdb, 0x49, 0xaf, 0x97, 0xd2, 0x9c, 0x8c, 0xe9, - 0x41, 0x1f, 0x47, 0x35, 0x5d, 0x34, 0x04, 0x87, 0x4e, 0xaa, 0x21, 0x2b, 0x8b, 0x1a, 0xb2, 0x87, - 0xa3, 0x6b, 0x59, 0xf4, 0x14, 0x6c, 0xa5, 0x89, 0xce, 0x95, 0xdb, 0xf0, 0xad, 0x5e, 0xa7, 0x8f, - 0x82, 0xeb, 0x47, 0x11, 0x1c, 0x1b, 0x64, 0xd0, 0x9e, 0x4b, 0xe0, 0xb2, 0x1f, 0x8d, 0xa0, 0xcd, - 0xb8, 0xd2, 0x84, 0xc7, 0xd4, 0x18, 0x5c, 0x5d, 0x94, 0xc1, 0x7d, 0x1c, 0xcd, 0x29, 0xa3, 0x2d, - 0x68, 0x6a, 0xa2, 0x4e, 0xdf, 0xd3, 0x73, 0xd7, 0xf6, 0xad, 0x9e, 0x33, 0xf8, 0xff, 0x06, 0xe3, - 0x31, 0x8e, 0xaa, 0x0e, 0xf4, 0x12, 0x6c, 0xc6, 0xb3, 0x5c, 0x2b, 0xb7, 0xe9, 0x5b, 0xbd, 0x56, - 0xff, 0x4e, 0xb9, 0x50, 0x75, 0x30, 0xc1, 0xb1, 0x39, 0x95, 0x3f, 0x24, 0x9e, 0xe0, 0xe8, 0x92, - 0x87, 0x9e, 0x43, 0x53, 0xe4, 0xda, 0x48, 0xac, 0xfd, 0x4d, 0x62, 0x3e, 0x96, 0x8a, 0x83, 0x30, - 0xac, 0x52, 0x29, 0x85, 0x74, 0x1d, 0xe3, 0x75, 0xbe, 0xab, 0x84, 0xd0, 0x2b, 0x68, 0x68, 0x92, - 0x2a, 0x17, 0xfc, 0x95, 0x9e, 0x33, 0xd8, 0x99, 0x4d, 0xbd, 0xfb, 0x35, 0x2f, 0xbb, 0x0f, 0xb1, - 0x3f, 0x21, 0x67, 0x2c, 0x21, 0x9a, 0x1e, 0xe0, 0x84, 0x4d, 0xe8, 0x76, 0x26, 0x19, 0xd7, 0x44, - 0xc5, 0x8c, 0xe1, 0xc8, 0x50, 0x51, 0x0a, 0xad, 0x4c, 0x8a, 0x98, 0x2a, 0x65, 0xd2, 0x6f, 0x99, - 0xf4, 0x8f, 0x6e, 0x97, 0x7e, 0xa7, 0x3e, 0x7c, 0x17, 0x47, 0x75, 0x65, 0xb4, 0x0d, 0x4d, 0x2e, - 0x12, 0x5a, 0xa4, 0xdf, 0x36, 0x1b, 0xa1, 0x9b, 0x8c, 0x3e, 0x8e, 0xaa, 0x16, 0xc4, 0xa0, 0x5d, - 0xbe, 0xdc, 0x42, 0x1a, 0x5f, 0xeb, 0x0b, 0xf3, 0xb5, 0x87, 0xa3, 0x39, 0x69, 0xf4, 0x0c, 0x56, - 0x33, 0xc9, 0x62, 0xea, 0x76, 0x8c, 0xad, 0x7b, 0xb3, 0xa9, 0xe7, 0xd5, 0x09, 0x8f, 0xea, 0x29, - 0xc6, 0x82, 0x71, 0x85, 0xa3, 0x92, 0xf1, 0xe0, 0x03, 0xd8, 0xe5, 0x6d, 0xa3, 0x16, 0x34, 0x3f, - 0xf1, 0x53, 0x2e, 0xbe, 0xf0, 0xee, 0x52, 0x51, 0x1c, 0x4a, 0x4a, 0x34, 0x4d, 0xba, 0x16, 0xea, - 0x00, 0xbc, 0xe3, 0x1f, 0xa5, 0x48, 0x25, 0x55, 0xaa, 0xbb, 0x8c, 0xd6, 0xc1, 0x39, 0x14, 0xe3, - 0xec, 0x8c, 0x16, 0xf0, 0x0a, 0x02, 0xb0, 0xdf, 0x10, 0x76, 0x46, 0x93, 0x6e, 0x63, 0xb0, 0x7f, - 0xf1, 0x73, 0x73, 0xe9, 0xfb, 0xaf, 0x4d, 0xeb, 0xf3, 0x3f, 0x2c, 0x78, 0xf5, 0xed, 0x1b, 0xda, - 0xe6, 0xa2, 0xf6, 0x7e, 0x07, 0x00, 0x00, 0xff, 0xff, 0x9f, 0x6c, 0x7b, 0xe0, 0x0f, 0x05, 0x00, - 0x00, + // 598 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x54, 0x5f, 0x4f, 0xdb, 0x3e, + 0x14, 0x25, 0xb4, 0xb4, 0xf4, 0xb6, 0x94, 0xfe, 0x2c, 0xfd, 0xa4, 0x88, 0x49, 0x24, 0xf2, 0x5e, + 0xb2, 0x01, 0xc9, 0x28, 0x63, 0x7f, 0x90, 0xa6, 0x6d, 0x65, 0x4c, 0x4c, 0xd3, 0xa4, 0x29, 0x68, + 0x2f, 0x7b, 0x73, 0x13, 0x93, 0x58, 0xb4, 0x76, 0x64, 0x3b, 0x6c, 0x7c, 0xa3, 0x7d, 0x14, 0x1e, + 0xf7, 0xbc, 0x87, 0x4a, 0xdb, 0x47, 0xe0, 0x13, 0x4c, 0x71, 0x08, 0xa4, 0x4c, 0x9a, 0x26, 0xc4, + 0x5b, 0x6f, 0xef, 0x39, 0xe7, 0x1e, 0x1f, 0x5f, 0x07, 0x56, 0xe9, 0x57, 0x1a, 0xe5, 0x9a, 0x09, + 0xee, 0x67, 0x52, 0x68, 0x81, 0x60, 0x4a, 0x55, 0xe2, 0xeb, 0xb3, 0x8c, 0xaa, 0x35, 0x9c, 0x88, + 0x44, 0x04, 0xe6, 0xff, 0x71, 0x7e, 0x1c, 0x14, 0x95, 0x29, 0xcc, 0xaf, 0x12, 0xbf, 0x76, 0xef, + 0xaa, 0x6d, 0x38, 0x81, 0xd2, 0x32, 0x8f, 0x74, 0xd9, 0xc4, 0xdf, 0x97, 0xa1, 0x73, 0x50, 0x0d, + 0x40, 0x47, 0xd0, 0x4c, 0x89, 0x4a, 0x6d, 0xcb, 0xb5, 0xbc, 0xde, 0xe8, 0xe5, 0xf9, 0xcc, 0x59, + 0xf8, 0x31, 0x73, 0x36, 0x12, 0xa6, 0xd3, 0x7c, 0xec, 0x47, 0x62, 0x1a, 0x14, 0xb3, 0xb7, 0x8e, + 0x45, 0xce, 0x63, 0x52, 0x30, 0x02, 0xca, 0x13, 0xc6, 0x69, 0x50, 0xb0, 0xfc, 0x43, 0xa2, 0xd2, + 0x8b, 0x99, 0xb3, 0x5c, 0x14, 0x7b, 0x78, 0x0b, 0x87, 0x46, 0x0c, 0xc5, 0x00, 0x19, 0x91, 0x94, + 0xeb, 0xa2, 0x6f, 0x2f, 0x1a, 0xe9, 0x37, 0xb7, 0x93, 0x5e, 0x29, 0xa5, 0x39, 0x99, 0xd2, 0xbd, + 0x21, 0x0e, 0x6b, 0xba, 0x68, 0x0c, 0x1d, 0x7a, 0x5a, 0x0d, 0x69, 0xdc, 0xd5, 0x90, 0x1d, 0x1c, + 0x5e, 0xcb, 0xa2, 0x67, 0xd0, 0x52, 0x9a, 0xe8, 0x5c, 0xd9, 0x4d, 0xd7, 0xf2, 0xfa, 0x43, 0xe4, + 0x5f, 0x5f, 0x85, 0x7f, 0x64, 0x3a, 0xa3, 0xde, 0x5c, 0x02, 0x97, 0x78, 0x94, 0x42, 0x8f, 0x71, + 0xa5, 0x09, 0x8f, 0xa8, 0x31, 0xb8, 0x74, 0x57, 0x06, 0x77, 0x71, 0x38, 0xa7, 0x8c, 0x36, 0xa0, + 0xad, 0x89, 0x3a, 0x79, 0x4f, 0xcf, 0xec, 0x96, 0x6b, 0x79, 0x9d, 0xd1, 0x7f, 0x37, 0x18, 0x4f, + 0x70, 0x58, 0x21, 0xd0, 0x2b, 0x68, 0x31, 0x9e, 0xe5, 0x5a, 0xd9, 0x6d, 0xd7, 0xf2, 0xba, 0xc3, + 0xff, 0xcb, 0x03, 0x55, 0x0b, 0xe3, 0x1f, 0x99, 0x55, 0xf9, 0x43, 0xe2, 0x29, 0x0e, 0x2f, 0x79, + 0xe8, 0x05, 0xb4, 0x45, 0xae, 0x8d, 0xc4, 0xf2, 0xdf, 0x24, 0xe6, 0x63, 0xa9, 0x38, 0x08, 0xc3, + 0x12, 0x95, 0x52, 0x48, 0xbb, 0x63, 0xbc, 0xce, 0xa3, 0xca, 0x16, 0x7a, 0x0d, 0x4d, 0x4d, 0x12, + 0x65, 0x83, 0xdb, 0xf0, 0x3a, 0xa3, 0xad, 0x8b, 0x99, 0xf3, 0xa0, 0xe6, 0x65, 0xfb, 0x11, 0x76, + 0x4f, 0xc9, 0x84, 0xc5, 0x44, 0xd3, 0x3d, 0x1c, 0xb3, 0x53, 0xba, 0x99, 0x49, 0xc6, 0x35, 0x51, + 0x11, 0x63, 0x38, 0x34, 0x54, 0x94, 0x40, 0x37, 0x93, 0x22, 0xa2, 0x4a, 0x99, 0xf4, 0xbb, 0x26, + 0xfd, 0x83, 0xdb, 0xa5, 0xdf, 0xaf, 0x0f, 0xdf, 0xc6, 0x61, 0x5d, 0x19, 0x6d, 0x42, 0x9b, 0x8b, + 0x98, 0x16, 0xe9, 0xf7, 0xcc, 0x89, 0xd0, 0x4d, 0xc6, 0x10, 0x87, 0x15, 0x04, 0x31, 0xe8, 0x95, + 0x8f, 0x5b, 0x48, 0xe3, 0x6b, 0xe5, 0xce, 0x7c, 0xed, 0xe0, 0x70, 0x4e, 0x1a, 0x3d, 0x87, 0xa5, + 0x4c, 0xb2, 0x88, 0xda, 0x7d, 0x63, 0xeb, 0xfe, 0xc5, 0xcc, 0x71, 0xea, 0x84, 0xc7, 0xf5, 0x14, + 0x23, 0xc1, 0xb8, 0xc2, 0x61, 0xc9, 0x40, 0x3e, 0x74, 0xc7, 0x13, 0x11, 0x9d, 0x1c, 0x52, 0x96, + 0xa4, 0xda, 0x5e, 0x75, 0x2d, 0xaf, 0x71, 0xe3, 0xa6, 0xea, 0x80, 0x87, 0x1f, 0xa0, 0x55, 0xbe, + 0x05, 0xd4, 0x85, 0xf6, 0x27, 0x7e, 0xc2, 0xc5, 0x17, 0x3e, 0x58, 0x28, 0x8a, 0x7d, 0x49, 0x89, + 0xa6, 0xf1, 0xc0, 0x42, 0x7d, 0x80, 0x77, 0xfc, 0xa3, 0x14, 0x89, 0xa4, 0x4a, 0x0d, 0x16, 0xd1, + 0x0a, 0x74, 0xf6, 0xc5, 0x34, 0x9b, 0xd0, 0xa2, 0xdd, 0x40, 0x00, 0xad, 0xb7, 0x84, 0x4d, 0x68, + 0x3c, 0x68, 0x8e, 0x76, 0xcf, 0x7f, 0xae, 0x2f, 0x7c, 0xfb, 0xb5, 0x6e, 0x7d, 0xfe, 0x87, 0x40, + 0xae, 0xbe, 0x95, 0xe3, 0x96, 0xd9, 0xc0, 0x9d, 0xdf, 0x01, 0x00, 0x00, 0xff, 0xff, 0xab, 0x05, + 0x78, 0x67, 0x3f, 0x05, 0x00, 0x00, } func (this *Execution) Equal(that interface{}) bool { @@ -241,6 +244,9 @@ func (this *Execution) Equal(that interface{}) bool { if this.Price != that1.Price { return false } + if this.BlockHeight != that1.BlockHeight { + return false + } if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) { return false } diff --git a/protobuf/types/execution.proto b/protobuf/types/execution.proto index 55aa00508..71e203c25 100644 --- a/protobuf/types/execution.proto +++ b/protobuf/types/execution.proto @@ -114,4 +114,9 @@ message Execution { string price = 14 [ (gogoproto.moretags) = 'hash:"name:14" validate:"coins"' ]; + + // blockHeight where the execution was included into blockchain. + int64 blockHeight = 15 [ + (gogoproto.moretags) = 'hash:"-"' + ]; } diff --git a/x/execution/internal/keeper/keeper.go b/x/execution/internal/keeper/keeper.go index 42af7a37b..73b152f33 100644 --- a/x/execution/internal/keeper/keeper.go +++ b/x/execution/internal/keeper/keeper.go @@ -88,6 +88,8 @@ func (k *Keeper) Create(ctx sdk.Context, msg types.MsgCreateExecution) (*executi msg.Request.Tags, msg.Request.ExecutorHash, ) + exec.BlockHeight = ctx.BlockHeight() + store := ctx.KVStore(k.storeKey) if store.Has(exec.Hash) { return nil, fmt.Errorf("execution %q already exists", exec.Hash) From 59296d1da0c1f9cd08ee233ba268f1e726d2e5c0 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Tue, 25 Feb 2020 13:53:08 +0700 Subject: [PATCH 64/87] add e2e test to check the modification of balances when an execution with price occurs --- e2e/api_test.go | 8 ++++++++ e2e/execution_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/e2e/api_test.go b/e2e/api_test.go index 436f542ea..5dcb2db13 100644 --- a/e2e/api_test.go +++ b/e2e/api_test.go @@ -9,6 +9,8 @@ import ( "github.com/cosmos/cosmos-sdk/types/rest" "github.com/mesg-foundation/engine/app" + "github.com/mesg-foundation/engine/config" + "github.com/mesg-foundation/engine/cosmos" pb "github.com/mesg-foundation/engine/protobuf/api" "github.com/stretchr/testify/require" "google.golang.org/grpc" @@ -67,6 +69,12 @@ func TestAPI(t *testing.T) { t.Skip() } + cfg, err := config.New() + if err != nil { + panic(err) + } + cosmos.CustomizeConfig(cfg) + conn, err := grpc.DialContext(context.Background(), "localhost:50052", grpc.WithInsecure()) require.NoError(t, err) diff --git a/e2e/execution_test.go b/e2e/execution_test.go index eac8d3423..b48339d01 100644 --- a/e2e/execution_test.go +++ b/e2e/execution_test.go @@ -5,12 +5,14 @@ import ( "sync" "testing" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/mesg-foundation/engine/execution" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/protobuf/acknowledgement" pb "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/protobuf/types" "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto" ) func testExecution(t *testing.T) { @@ -210,12 +212,42 @@ func testExecution(t *testing.T) { }) require.NoError(t, err) + // check balance of execution before completed + t.Run("execution balance before completed", func(t *testing.T) { + coins := sdk.Coins{} + execAddress := sdk.AccAddress(crypto.AddressHash(resp.Hash)) + lcdGet(t, "bank/balances/"+execAddress.String(), &coins) + require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(50))) + }) + _, err = streamInProgress.Recv() require.NoError(t, err) exec, err := streamCompleted.Recv() require.NoError(t, err) require.Equal(t, resp.Hash, exec.Hash) + + // check balance of executor + t.Run("executor balance", func(t *testing.T) { + coins := sdk.Coins{} + executorAddress := sdk.AccAddress(crypto.AddressHash(executorHash)) + lcdGet(t, "bank/balances/"+executorAddress.String(), &coins) + require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(45))) + }) + // check balance of service + t.Run("service balance", func(t *testing.T) { + coins := sdk.Coins{} + serviceAddress := sdk.AccAddress(crypto.AddressHash(testServiceHash)) + lcdGet(t, "bank/balances/"+serviceAddress.String(), &coins) + require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(5))) + }) + // check balance of execution + t.Run("execution balance", func(t *testing.T) { + coins := sdk.Coins{} + execAddress := sdk.AccAddress(crypto.AddressHash(resp.Hash)) + lcdGet(t, "bank/balances/"+execAddress.String(), &coins) + require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(0))) + }) }) t.Run("many executions in parallel", func(t *testing.T) { From bd52d4e97f1f3046a8a2e636659797ab914d9aa7 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 26 Feb 2020 17:01:26 +0700 Subject: [PATCH 65/87] Implement Stop function on orchestrator --- orchestrator/orchestrator.go | 8 ++++++++ orchestrator/type.go | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/orchestrator/orchestrator.go b/orchestrator/orchestrator.go index 902fc30c8..bc275c521 100644 --- a/orchestrator/orchestrator.go +++ b/orchestrator/orchestrator.go @@ -23,6 +23,7 @@ func New(mc *cosmos.ModuleClient, ep *publisher.EventPublisher) *Orchestrator { mc: mc, ep: ep, ErrC: make(chan error), + stopC: make(chan bool), } } @@ -55,10 +56,17 @@ func (s *Orchestrator) Start() error { go s.execute(s.dependencyFilter(execution), execution, nil, execution.Outputs) case err := <-errC: s.ErrC <- err + case <-s.stopC: + return nil } } } +// Stop stops the orchestrator engine +func (s *Orchestrator) Stop() { + s.stopC <- true +} + func (s *Orchestrator) eventFilter(event *event.Event) func(wf *process.Process, node *process.Process_Node) (bool, error) { return func(wf *process.Process, node *process.Process_Node) (bool, error) { if e := node.GetEvent(); e != nil { diff --git a/orchestrator/type.go b/orchestrator/type.go index cf19adc26..41db73f24 100644 --- a/orchestrator/type.go +++ b/orchestrator/type.go @@ -15,5 +15,6 @@ type Orchestrator struct { eventStream *event.Listener executionStream <-chan *execution.Execution - ErrC chan error + ErrC chan error + stopC chan bool } From b4a961b638c4e18f0730c0e62deb147b2f0acd10 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 26 Feb 2020 17:02:16 +0700 Subject: [PATCH 66/87] Call tendermint.Stop. Call orchestrator.Stop. Put stop function in defer function when possible. --- core/main.go | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/core/main.go b/core/main.go index 1efa6742c..ccac62c7b 100644 --- a/core/main.go +++ b/core/main.go @@ -172,6 +172,14 @@ func main() { if err != nil { logrus.WithField("module", "main").Fatalln(err) } + defer func() { + logrus.WithField("module", "main").Info("stopping tendermint") + if node.IsRunning() { + if err := node.Stop(); err != nil { + logrus.WithField("module", "main").Errorln(err) + } + } + }() // create cosmos client client, err := cosmos.NewClient(node, cdc, kb, genesis.ChainID, cfg.Account.Name, cfg.Account.Password, cfg.Cosmos.MinGasPrices) @@ -196,6 +204,10 @@ func main() { // init gRPC server. server := grpc.New(mc, ep, b) logrus.WithField("module", "main").Infof("starting MESG Engine version %s", version.Version) + defer func() { + logrus.WithField("module", "main").Info("stopping grpc server") + server.Close() + }() go func() { if err := server.Serve(cfg.Server.Address); err != nil { @@ -204,14 +216,18 @@ func main() { }() logrus.WithField("module", "main").Info("starting process engine") - s := orchestrator.New(mc, ep) + orch := orchestrator.New(mc, ep) + defer func() { + logrus.WithField("module", "main").Info("stopping orchestrator") + orch.Stop() + }() go func() { - if err := s.Start(); err != nil { + if err := orch.Start(); err != nil { logrus.WithField("module", "main").Fatalln(err) } }() go func() { - for err := range s.ErrC { + for err := range orch.ErrC { logrus.WithField("module", "orchestrator").Warn(err) } }() @@ -222,6 +238,12 @@ func main() { if err != nil { logrus.WithField("module", "main").Fatalln(err) } + defer func() { + logrus.WithField("module", "main").Info("stopping lcd server") + if err := lcdServer.Close(); err != nil { + logrus.WithField("module", "main").Errorln(err) + } + }() cliCtx := context.NewCLIContext(). WithCodec(cdc). @@ -239,23 +261,13 @@ func main() { <-xsignal.WaitForInterrupt() - logrus.WithField("module", "main").Info("stopping lcd server") - if err := lcdServer.Close(); err != nil { - logrus.WithField("module", "main").Fatalln(err) - } - logrus.WithField("module", "main").Info("stopping running services") if err := stopRunningServices(mc, b, acc.GetAddress().String()); err != nil { - logrus.WithField("module", "main").Fatalln(err) + logrus.WithField("module", "main").Errorln(err) } logrus.WithField("module", "main").Info("cleanup container") if err := container.Cleanup(); err != nil { - logrus.WithField("module", "main").Fatalln(err) + logrus.WithField("module", "main").Errorln(err) } - - logrus.WithField("module", "main").Info("stopping grpc server") - server.Close() - - logrus.WithField("module", "main").Info("everything is stopped") } From 725ad85a0a732c0d45a7ffb75532c0c169eab93b Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 26 Feb 2020 17:21:05 +0700 Subject: [PATCH 67/87] fix lint --- orchestrator/orchestrator.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/orchestrator/orchestrator.go b/orchestrator/orchestrator.go index bc275c521..1b9bd1b54 100644 --- a/orchestrator/orchestrator.go +++ b/orchestrator/orchestrator.go @@ -20,10 +20,10 @@ import ( // New creates a new Process instance func New(mc *cosmos.ModuleClient, ep *publisher.EventPublisher) *Orchestrator { return &Orchestrator{ - mc: mc, - ep: ep, - ErrC: make(chan error), - stopC: make(chan bool), + mc: mc, + ep: ep, + ErrC: make(chan error), + stopC: make(chan bool), } } From b2e6165a0c83ea98c1824cb4dbb09d071fe1cff9 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 26 Feb 2020 17:25:41 +0700 Subject: [PATCH 68/87] return cosmos app load function instead of exiting. --- app/app.go | 10 ++++------ core/main.go | 5 ++++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/app.go b/app/app.go index d3361f794..499d7dd65 100644 --- a/app/app.go +++ b/app/app.go @@ -7,7 +7,6 @@ import ( abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" - tmos "github.com/tendermint/tendermint/libs/os" dbm "github.com/tendermint/tm-db" bam "github.com/cosmos/cosmos-sdk/baseapp" @@ -136,7 +135,7 @@ var _ simapp.App = (*NewApp)(nil) func NewInitApp( logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, invCheckPeriod uint, baseAppOptions ...func(*bam.BaseApp), -) *NewApp { +) (*NewApp, error) { // First define the top level codec that will be shared by the different modules cdc := MakeCodec() @@ -334,13 +333,12 @@ func NewInitApp( app.MountTransientStores(tKeys) if loadLatest { - err := app.LoadLatestVersion(app.keys[bam.MainStoreKey]) - if err != nil { - tmos.Exit(err.Error()) + if err := app.LoadLatestVersion(app.keys[bam.MainStoreKey]); err != nil { + return nil, err } } - return app + return app, nil } // GenesisState represents chain state at the start of the chain. Any initial state (account balances) are stored here. diff --git a/core/main.go b/core/main.go index ccac62c7b..2c0c5b223 100644 --- a/core/main.go +++ b/core/main.go @@ -145,7 +145,10 @@ func main() { logrus.WithField("module", "main").Fatalln(err) } - initApp := app.NewInitApp(tendermintLogger, db, nil, true, 0, bam.SetMinGasPrices(cfg.Cosmos.MinGasPrices)) + initApp, err := app.NewInitApp(tendermintLogger, db, nil, true, 0, bam.SetMinGasPrices(cfg.Cosmos.MinGasPrices)) + if err != nil { + logrus.WithField("module", "main").Fatalln(err) + } cdc := initApp.Codec() // init key manager From 37cd8609694b7f4bc325e19777bc8cb6da9f925d Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 26 Feb 2020 17:39:48 +0700 Subject: [PATCH 69/87] fix cli daemon --- cmd/mesg-daemon/main.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/cmd/mesg-daemon/main.go b/cmd/mesg-daemon/main.go index a90378768..85c722116 100644 --- a/cmd/mesg-daemon/main.go +++ b/cmd/mesg-daemon/main.go @@ -85,7 +85,7 @@ func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer) abci.Application cache = store.NewCommitKVStoreCacheManager() } - return app.NewInitApp( + initApp, err := app.NewInitApp( logger, db, traceStore, true, invCheckPeriod, baseapp.SetPruning(store.NewPruningOptionsFromString(viper.GetString("pruning"))), baseapp.SetMinGasPrices(viper.GetString(server.FlagMinGasPrices)), @@ -93,22 +93,30 @@ func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer) abci.Application baseapp.SetHaltTime(viper.GetUint64(server.FlagHaltTime)), baseapp.SetInterBlockCache(cache), ) + if err != nil { + panic(err) + } + return initApp } func exportAppStateAndTMValidators( logger log.Logger, db dbm.DB, traceStore io.Writer, height int64, forZeroHeight bool, jailWhiteList []string, ) (json.RawMessage, []tmtypes.GenesisValidator, error) { if height != -1 { - aApp := app.NewInitApp(logger, db, traceStore, false, uint(1)) - err := aApp.LoadHeight(height) + aApp, err := app.NewInitApp(logger, db, traceStore, false, uint(1)) if err != nil { return nil, nil, err } + if err := aApp.LoadHeight(height); err != nil { + return nil, nil, err + } return aApp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList) } - aApp := app.NewInitApp(logger, db, traceStore, true, uint(1)) - + aApp, err := app.NewInitApp(logger, db, traceStore, true, uint(1)) + if err != nil { + return nil, nil, err + } return aApp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList) } From a918013b0d11ab4011ef1d9e31207ef17fb38b95 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 26 Feb 2020 17:51:26 +0700 Subject: [PATCH 70/87] disable gocycle on core/main function --- core/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/main.go b/core/main.go index 2c0c5b223..aa6380459 100644 --- a/core/main.go +++ b/core/main.go @@ -116,6 +116,7 @@ func loadOrGenDevGenesis(cdc *codec.Codec, kb *cosmos.Keybase, cfg *config.Confi return cosmos.GenGenesis(cdc, kb, app.NewDefaultGenesisState(), cfg.DevGenesis.ChainID, cfg.DevGenesis.InitialBalances, cfg.DevGenesis.ValidatorDelegationCoin, cfg.Tendermint.Config.GenesisFile(), []cosmos.GenesisValidator{validator}) } +//nolint:gocyclo func main() { xrand.SeedInit() From 2c6bc4a2805bdd6bd1694434fbb775d14095c335 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 27 Feb 2020 18:46:00 +0700 Subject: [PATCH 71/87] Add pagination system on http handler of executions/list --- execution/sort.go | 8 ++++++++ x/execution/client/rest/query.go | 32 +++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 execution/sort.go diff --git a/execution/sort.go b/execution/sort.go new file mode 100644 index 000000000..d3938b433 --- /dev/null +++ b/execution/sort.go @@ -0,0 +1,8 @@ +package execution + +// ByBlockHeight implements sort.Interface for []*Execution based on the block height field. +type ByBlockHeight []*Execution + +func (a ByBlockHeight) Len() int { return len(a) } +func (a ByBlockHeight) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a ByBlockHeight) Less(i, j int) bool { return a[i].BlockHeight < a[j].BlockHeight } diff --git a/x/execution/client/rest/query.go b/x/execution/client/rest/query.go index 9c4bc4127..234ea20d3 100644 --- a/x/execution/client/rest/query.go +++ b/x/execution/client/rest/query.go @@ -3,10 +3,13 @@ package rest import ( "fmt" "net/http" + "sort" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/types/rest" "github.com/gorilla/mux" + "github.com/mesg-foundation/engine/execution" "github.com/mesg-foundation/engine/x/execution/internal/types" ) @@ -50,6 +53,12 @@ func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return } + tags, page, limit, err := rest.ParseHTTPArgs(r) + if err != nil { + rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + return + } + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryListExecution) res, height, err := cliCtx.QueryWithData(route, nil) @@ -58,7 +67,28 @@ func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return } + execs := make([]*execution.Execution, 0) + if err := cliCtx.Codec.UnmarshalJSON(res, &execs); err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + sortedExecs := sort.Interface(execution.ByBlockHeight(execs)) + for _, tag := range tags { + if tag == "sort='reverse'" { + sortedExecs = sort.Reverse(sortedExecs) + } + } + sort.Sort(sortedExecs) + + start, end := client.Paginate(len(execs), page, limit, limit) + if start < 0 || end < 0 { + execs = make([]*execution.Execution, 0) + } else { + execs = execs[start:end] + } + cliCtx = cliCtx.WithHeight(height) - rest.PostProcessResponse(w, cliCtx, res) + rest.PostProcessResponse(w, cliCtx, execs) } } From 81ae3be7453a8eae128559012f53c4a74b69e596 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Thu, 27 Feb 2020 18:50:08 +0700 Subject: [PATCH 72/87] rename a variable for clarity --- x/execution/client/rest/query.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/execution/client/rest/query.go b/x/execution/client/rest/query.go index 234ea20d3..11a781d9e 100644 --- a/x/execution/client/rest/query.go +++ b/x/execution/client/rest/query.go @@ -73,13 +73,13 @@ func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return } - sortedExecs := sort.Interface(execution.ByBlockHeight(execs)) + sortFunc := sort.Interface(execution.ByBlockHeight(execs)) for _, tag := range tags { if tag == "sort='reverse'" { - sortedExecs = sort.Reverse(sortedExecs) + sortFunc = sort.Reverse(sortFunc) } } - sort.Sort(sortedExecs) + sort.Sort(sortFunc) start, end := client.Paginate(len(execs), page, limit, limit) if start < 0 || end < 0 { From 5d41033794f94eb182d905e6660288d71ce71046 Mon Sep 17 00:00:00 2001 From: antho1404 Date: Fri, 28 Feb 2020 09:56:39 +0700 Subject: [PATCH 73/87] make sure to call end block for each modules --- x/execution/module.go | 3 ++- x/instance/module.go | 3 ++- x/ownership/module.go | 3 ++- x/process/module.go | 3 ++- x/runner/module.go | 3 ++- x/service/module.go | 3 ++- 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/x/execution/module.go b/x/execution/module.go index 48476076b..784d7f1f6 100644 --- a/x/execution/module.go +++ b/x/execution/module.go @@ -134,6 +134,7 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { // EndBlock returns the end blocker for the execution module. It returns no validator // updates. -func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { +func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + EndBlocker(ctx, am.keeper) return []abci.ValidatorUpdate{} } diff --git a/x/instance/module.go b/x/instance/module.go index d371a647a..00d1baf39 100644 --- a/x/instance/module.go +++ b/x/instance/module.go @@ -134,6 +134,7 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { // EndBlock returns the end blocker for the instance module. It returns no validator // updates. -func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { +func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + EndBlocker(ctx, am.keeper) return []abci.ValidatorUpdate{} } diff --git a/x/ownership/module.go b/x/ownership/module.go index 2d1be1575..9d6eea94e 100644 --- a/x/ownership/module.go +++ b/x/ownership/module.go @@ -134,6 +134,7 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { // EndBlock returns the end blocker for the ownership module. It returns no validator // updates. -func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { +func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + EndBlocker(ctx, am.keeper) return []abci.ValidatorUpdate{} } diff --git a/x/process/module.go b/x/process/module.go index ccf907beb..788e5c1a9 100644 --- a/x/process/module.go +++ b/x/process/module.go @@ -134,6 +134,7 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { // EndBlock returns the end blocker for the process module. It returns no validator // updates. -func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { +func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + EndBlocker(ctx, am.keeper) return []abci.ValidatorUpdate{} } diff --git a/x/runner/module.go b/x/runner/module.go index f75326f11..ad668ec83 100644 --- a/x/runner/module.go +++ b/x/runner/module.go @@ -137,6 +137,7 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { // EndBlock returns the end blocker for the runner module. It returns no validator // updates. -func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { +func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + EndBlocker(ctx, am.keeper) return []abci.ValidatorUpdate{} } diff --git a/x/service/module.go b/x/service/module.go index 0504f2978..592f15ce8 100644 --- a/x/service/module.go +++ b/x/service/module.go @@ -136,6 +136,7 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { // EndBlock returns the end blocker for the service module. It returns no validator // updates. -func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { +func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + EndBlocker(ctx, am.keeper) return []abci.ValidatorUpdate{} } From 76b517f7291d812d699c4d6835bb868f21d83dd2 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Fri, 28 Feb 2020 10:33:14 +0700 Subject: [PATCH 74/87] remove tag sort=reverse in executions/list http endpoint --- x/execution/client/rest/query.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/x/execution/client/rest/query.go b/x/execution/client/rest/query.go index 11a781d9e..fb09b27e3 100644 --- a/x/execution/client/rest/query.go +++ b/x/execution/client/rest/query.go @@ -53,7 +53,7 @@ func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return } - tags, page, limit, err := rest.ParseHTTPArgs(r) + _, page, limit, err := rest.ParseHTTPArgs(r) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return @@ -73,17 +73,11 @@ func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return } - sortFunc := sort.Interface(execution.ByBlockHeight(execs)) - for _, tag := range tags { - if tag == "sort='reverse'" { - sortFunc = sort.Reverse(sortFunc) - } - } - sort.Sort(sortFunc) + sort.Sort(sort.Reverse(execution.ByBlockHeight(execs))) start, end := client.Paginate(len(execs), page, limit, limit) if start < 0 || end < 0 { - execs = make([]*execution.Execution, 0) + execs = []*execution.Execution{} } else { execs = execs[start:end] } From 77015cb8f50683cb46528be1f4be8bbf5149de73 Mon Sep 17 00:00:00 2001 From: antho1404 Date: Fri, 28 Feb 2020 13:23:28 +0700 Subject: [PATCH 75/87] add new param minprice in genesis --- app/app.go | 12 ++++++- x/execution/genesis.go | 4 ++- x/execution/internal/keeper/keeper.go | 5 ++- x/execution/internal/keeper/params.go | 34 +++++++++++++++++++ x/execution/internal/types/genesis.go | 12 ++++--- x/execution/internal/types/params.go | 48 +++++++++++++++++++++------ x/execution/module.go | 3 +- 7 files changed, 99 insertions(+), 19 deletions(-) create mode 100644 x/execution/internal/keeper/params.go diff --git a/app/app.go b/app/app.go index 499d7dd65..cba1fe69d 100644 --- a/app/app.go +++ b/app/app.go @@ -182,6 +182,7 @@ func NewInitApp( app.subspaces[staking.ModuleName] = app.paramsKeeper.Subspace(staking.DefaultParamspace) app.subspaces[distr.ModuleName] = app.paramsKeeper.Subspace(distr.DefaultParamspace) app.subspaces[slashing.ModuleName] = app.paramsKeeper.Subspace(slashing.DefaultParamspace) + app.subspaces[execution.ModuleName] = app.paramsKeeper.Subspace(execution.DefaultParamspace) // The AccountKeeper handles address -> account lookups app.accountKeeper = auth.NewAccountKeeper( @@ -246,7 +247,16 @@ func NewInitApp( app.processKeeper = process.NewKeeper(app.cdc, keys[process.StoreKey], app.instanceKeeper, app.ownershipKeeper) app.serviceKeeper = service.NewKeeper(app.cdc, keys[service.StoreKey], app.ownershipKeeper) app.runnerKeeper = runner.NewKeeper(app.cdc, keys[runner.StoreKey], app.instanceKeeper) - app.executionKeeper = execution.NewKeeper(app.cdc, keys[execution.StoreKey], app.bankKeeper, app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper) + app.executionKeeper = execution.NewKeeper( + app.cdc, + keys[execution.StoreKey], + app.bankKeeper, + app.serviceKeeper, + app.instanceKeeper, + app.runnerKeeper, + app.processKeeper, + app.subspaces[execution.ModuleName], + ) // NOTE: Any module instantiated in the module manager that is later modified // must be passed by reference here. diff --git a/x/execution/genesis.go b/x/execution/genesis.go index 6ec641d75..d37afa254 100644 --- a/x/execution/genesis.go +++ b/x/execution/genesis.go @@ -9,6 +9,7 @@ import ( // InitGenesis initialize default parameters // and the keeper's address to pubkey map func InitGenesis(ctx sdk.Context, k Keeper, data types.GenesisState) []abci.ValidatorUpdate { + k.SetParams(ctx, data.Params) return []abci.ValidatorUpdate{} } @@ -16,5 +17,6 @@ func InitGenesis(ctx sdk.Context, k Keeper, data types.GenesisState) []abci.Vali // to a genesis file, which can be imported again // with InitGenesis func ExportGenesis(ctx sdk.Context, k Keeper) (data types.GenesisState) { - return types.NewGenesisState() + params := k.GetParams(ctx) + return types.NewGenesisState(params) } diff --git a/x/execution/internal/keeper/keeper.go b/x/execution/internal/keeper/keeper.go index 73b152f33..eb88ea288 100644 --- a/x/execution/internal/keeper/keeper.go +++ b/x/execution/internal/keeper/keeper.go @@ -8,6 +8,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/params" executionpb "github.com/mesg-foundation/engine/execution" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/protobuf/api" @@ -30,10 +31,11 @@ type Keeper struct { instanceKeeper types.InstanceKeeper runnerKeeper types.RunnerKeeper processKeeper types.ProcessKeeper + paramstore params.Subspace } // NewKeeper creates a execution keeper -func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, bankKeeper types.BankKeeper, serviceKeeper types.ServiceKeeper, instanceKeeper types.InstanceKeeper, runnerKeeper types.RunnerKeeper, processKeeper types.ProcessKeeper) Keeper { +func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, bankKeeper types.BankKeeper, serviceKeeper types.ServiceKeeper, instanceKeeper types.InstanceKeeper, runnerKeeper types.RunnerKeeper, processKeeper types.ProcessKeeper, paramstore params.Subspace) Keeper { return Keeper{ storeKey: key, cdc: cdc, @@ -42,6 +44,7 @@ func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, bankKeeper types.BankKeeper, instanceKeeper: instanceKeeper, runnerKeeper: runnerKeeper, processKeeper: processKeeper, + paramstore: paramstore.WithKeyTable(types.ParamKeyTable()), } } diff --git a/x/execution/internal/keeper/params.go b/x/execution/internal/keeper/params.go new file mode 100644 index 000000000..ef5f6b088 --- /dev/null +++ b/x/execution/internal/keeper/params.go @@ -0,0 +1,34 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/mesg-foundation/engine/x/execution/internal/types" +) + +// Default parameter namespace +const ( + DefaultParamspace = types.ModuleName +) + +// MinPrice - Minimum price of an execution +func (k Keeper) MinPrice(ctx sdk.Context) sdk.Coins { + var coinstr string + k.paramstore.Get(ctx, types.KeyMinPrice, &coinstr) + price, err := sdk.ParseCoins(coinstr) + if err != nil { + panic(err) + } + return price +} + +// SetParams will populate all the params +func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { + k.paramstore.SetParamSet(ctx, ¶ms) +} + +// GetParams returns all the params of the module +func (k Keeper) GetParams(ctx sdk.Context) types.Params { + var params types.Params + k.paramstore.GetParamSet(ctx, ¶ms) + return params +} diff --git a/x/execution/internal/types/genesis.go b/x/execution/internal/types/genesis.go index bebeec0dc..d325d1db0 100644 --- a/x/execution/internal/types/genesis.go +++ b/x/execution/internal/types/genesis.go @@ -1,16 +1,20 @@ package types // GenesisState - all instance state that must be provided at genesis -type GenesisState struct{} +type GenesisState struct { + Params Params `json:"params" yaml:"params"` +} // NewGenesisState creates a new GenesisState object -func NewGenesisState() GenesisState { - return GenesisState{} +func NewGenesisState(params Params) GenesisState { + return GenesisState{Params: params} } // DefaultGenesisState - default GenesisState used by Cosmos Hub func DefaultGenesisState() GenesisState { - return GenesisState{} + return GenesisState{ + Params: DefaultParams(), + } } // ValidateGenesis validates the instance genesis parameters diff --git a/x/execution/internal/types/params.go b/x/execution/internal/types/params.go index 4940992b5..db9f588b9 100644 --- a/x/execution/internal/types/params.go +++ b/x/execution/internal/types/params.go @@ -1,38 +1,66 @@ package types import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/params" + "github.com/cosmos/cosmos-sdk/x/params/subspace" ) // Default parameter namespace const ( DefaultParamspace = ModuleName + DefaultMinPrice = "10000atto" ) -// ParamKeyTable for instance module -func ParamKeyTable() params.KeyTable { - return params.NewKeyTable().RegisterParamSet(&Params{}) -} +var ( + // KeyMinPrice key for the parameter MinPrice + KeyMinPrice = []byte("MinPrice") +) // Params - used for initializing default parameter for instance at genesis -type Params struct{} +type Params struct { + MinPrice string `json:"min_price" yaml:"min_price"` // min price to pay for an execution +} // NewParams creates a new Params object -func NewParams() Params { - return Params{} +func NewParams(minPrice string) Params { + return Params{ + MinPrice: minPrice, + } +} + +// ParamKeyTable for auth module +func ParamKeyTable() subspace.KeyTable { + return subspace.NewKeyTable().RegisterParamSet(&Params{}) } // String implements the stringer interface for Params func (p Params) String() string { - return "" + return fmt.Sprintf(`Params: + MinPrice: %s + `, p.MinPrice) } // ParamSetPairs - Implements params.ParamSet func (p *Params) ParamSetPairs() params.ParamSetPairs { - return params.ParamSetPairs{} + return params.ParamSetPairs{ + params.NewParamSetPair(KeyMinPrice, &p.MinPrice, validateMinPrice), + } } // DefaultParams defines the parameters for this module func DefaultParams() Params { - return NewParams() + return NewParams(DefaultMinPrice) +} + +func validateMinPrice(i interface{}) error { + v, ok := i.(string) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + _, err := sdk.ParseCoins(v) + return err } diff --git a/x/execution/module.go b/x/execution/module.go index 48476076b..6f2016471 100644 --- a/x/execution/module.go +++ b/x/execution/module.go @@ -116,8 +116,7 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { var genesisState GenesisState ModuleCdc.MustUnmarshalJSON(data, &genesisState) - InitGenesis(ctx, am.keeper, genesisState) - return []abci.ValidatorUpdate{} + return InitGenesis(ctx, am.keeper, genesisState) } // ExportGenesis returns the exported genesis state as raw bytes for the execution From 3355204553b78f94b89d429bff0aa312e130cb0d Mon Sep 17 00:00:00 2001 From: antho1404 Date: Fri, 28 Feb 2020 13:24:09 +0700 Subject: [PATCH 76/87] check execution price gte min exec price --- x/execution/internal/keeper/keeper.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/x/execution/internal/keeper/keeper.go b/x/execution/internal/keeper/keeper.go index eb88ea288..940067059 100644 --- a/x/execution/internal/keeper/keeper.go +++ b/x/execution/internal/keeper/keeper.go @@ -59,6 +59,12 @@ func (k *Keeper) Create(ctx sdk.Context, msg types.MsgCreateExecution) (*executi if err != nil { return nil, err } + + minPrice := k.MinPrice(ctx) + if !price.IsAllGTE(minPrice) { + return nil, fmt.Errorf("execution price too low. Min value: %q", minPrice) + } + run, err := k.runnerKeeper.Get(ctx, msg.Request.ExecutorHash) if err != nil { return nil, err From f06e49067949af263773ad03c8ba240bdb030da3 Mon Sep 17 00:00:00 2001 From: antho1404 Date: Fri, 28 Feb 2020 13:24:49 +0700 Subject: [PATCH 77/87] add default exec price in config for clients --- config/config.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/config/config.go b/config/config.go index b66587c60..fa441aa4a 100644 --- a/config/config.go +++ b/config/config.go @@ -29,6 +29,8 @@ type Config struct { IpfsEndpoint string `validate:"required"` + DefaultExecutionPrice string `validate:"required"` + Server struct { Address string `validate:"required"` } @@ -95,6 +97,8 @@ func defaultConfig() (*Config, error) { c.IpfsEndpoint = "http://ipfs.app.mesg.com:8080/ipfs/" + c.DefaultExecutionPrice = "10000atto" // /x/execution/internal/type/params.go#DefaultMinPrice + c.Server.Address = ":50052" c.Log.Format = "text" c.Log.Level = "info" From e716ef647f6b6f21f1bc073a0ecf0e118e455f60 Mon Sep 17 00:00:00 2001 From: antho1404 Date: Fri, 28 Feb 2020 13:25:23 +0700 Subject: [PATCH 78/87] update server to execute with the config default price --- core/main.go | 2 +- server/grpc/api/execution.go | 10 +++++++--- server/grpc/server.go | 20 +++++++++++--------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/core/main.go b/core/main.go index aa6380459..0d12aaf1d 100644 --- a/core/main.go +++ b/core/main.go @@ -206,7 +206,7 @@ func main() { } // init gRPC server. - server := grpc.New(mc, ep, b) + server := grpc.New(mc, ep, b, cfg.DefaultExecutionPrice) logrus.WithField("module", "main").Infof("starting MESG Engine version %s", version.Version) defer func() { logrus.WithField("module", "main").Info("stopping grpc server") diff --git a/server/grpc/api/execution.go b/server/grpc/api/execution.go index 438d3a8dd..3ad00307d 100644 --- a/server/grpc/api/execution.go +++ b/server/grpc/api/execution.go @@ -11,16 +11,20 @@ import ( // ExecutionServer serve execution functions. type ExecutionServer struct { - mc *cosmos.ModuleClient + mc *cosmos.ModuleClient + execPrice string } // NewExecutionServer creates a new ExecutionServer. -func NewExecutionServer(mc *cosmos.ModuleClient) *ExecutionServer { - return &ExecutionServer{mc: mc} +func NewExecutionServer(mc *cosmos.ModuleClient, execPrice string) *ExecutionServer { + return &ExecutionServer{mc: mc, execPrice: execPrice} } // Create creates an execution. func (s *ExecutionServer) Create(ctx context.Context, req *api.CreateExecutionRequest) (*api.CreateExecutionResponse, error) { + if req.Price == "" { + req.Price = s.execPrice + } exec, err := s.mc.CreateExecution(req) if err != nil { return nil, err diff --git a/server/grpc/server.go b/server/grpc/server.go index aca365dd5..e164f72c7 100644 --- a/server/grpc/server.go +++ b/server/grpc/server.go @@ -22,18 +22,20 @@ import ( // Server contains the server config. type Server struct { - instance *grpc.Server - mc *cosmos.ModuleClient - ep *publisher.EventPublisher - b *builder.Builder + instance *grpc.Server + mc *cosmos.ModuleClient + ep *publisher.EventPublisher + b *builder.Builder + execPrice string } // New returns a new gRPC server. -func New(mc *cosmos.ModuleClient, ep *publisher.EventPublisher, b *builder.Builder) *Server { +func New(mc *cosmos.ModuleClient, ep *publisher.EventPublisher, b *builder.Builder, execPrice string) *Server { return &Server{ - mc: mc, - ep: ep, - b: b, + mc: mc, + ep: ep, + b: b, + execPrice: execPrice, } } @@ -82,7 +84,7 @@ func validateInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryS // register all server func (s *Server) register() { protobuf_api.RegisterEventServer(s.instance, api.NewEventServer(s.ep)) - protobuf_api.RegisterExecutionServer(s.instance, api.NewExecutionServer(s.mc)) + protobuf_api.RegisterExecutionServer(s.instance, api.NewExecutionServer(s.mc, s.execPrice)) protobuf_api.RegisterInstanceServer(s.instance, api.NewInstanceServer(s.mc)) protobuf_api.RegisterServiceServer(s.instance, api.NewServiceServer(s.mc)) protobuf_api.RegisterProcessServer(s.instance, api.NewProcessServer(s.mc)) From d8966d4fe71c5e33c1ac2c0ab0579bf7366f1d40 Mon Sep 17 00:00:00 2001 From: antho1404 Date: Fri, 28 Feb 2020 13:25:54 +0700 Subject: [PATCH 79/87] update orchestrator to use default exec price --- core/main.go | 2 +- orchestrator/orchestrator.go | 12 +++++++----- orchestrator/type.go | 2 ++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/core/main.go b/core/main.go index 0d12aaf1d..d958ec5b8 100644 --- a/core/main.go +++ b/core/main.go @@ -220,7 +220,7 @@ func main() { }() logrus.WithField("module", "main").Info("starting process engine") - orch := orchestrator.New(mc, ep) + orch := orchestrator.New(mc, ep, cfg.DefaultExecutionPrice) defer func() { logrus.WithField("module", "main").Info("stopping orchestrator") orch.Stop() diff --git a/orchestrator/orchestrator.go b/orchestrator/orchestrator.go index 1b9bd1b54..e5d196c98 100644 --- a/orchestrator/orchestrator.go +++ b/orchestrator/orchestrator.go @@ -18,12 +18,13 @@ import ( ) // New creates a new Process instance -func New(mc *cosmos.ModuleClient, ep *publisher.EventPublisher) *Orchestrator { +func New(mc *cosmos.ModuleClient, ep *publisher.EventPublisher, execPrice string) *Orchestrator { return &Orchestrator{ - mc: mc, - ep: ep, - ErrC: make(chan error), - stopC: make(chan bool), + mc: mc, + ep: ep, + ErrC: make(chan error), + stopC: make(chan bool), + execPrice: execPrice, } } @@ -273,6 +274,7 @@ func (s *Orchestrator) processTask(nodeKey string, task *process.Process_Node_Ta TaskKey: task.TaskKey, Inputs: data, ExecutorHash: executor.Hash, + Price: s.execPrice, Tags: nil, }) return err diff --git a/orchestrator/type.go b/orchestrator/type.go index 41db73f24..b7f007beb 100644 --- a/orchestrator/type.go +++ b/orchestrator/type.go @@ -17,4 +17,6 @@ type Orchestrator struct { ErrC chan error stopC chan bool + + execPrice string } From 8c77c5a2b595822449783c3ab7907b2b8864d555 Mon Sep 17 00:00:00 2001 From: antho1404 Date: Fri, 28 Feb 2020 13:30:55 +0700 Subject: [PATCH 80/87] fix server test --- server/grpc/server_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/grpc/server_test.go b/server/grpc/server_test.go index 207295145..697cd3427 100644 --- a/server/grpc/server_test.go +++ b/server/grpc/server_test.go @@ -8,7 +8,7 @@ import ( ) func TestServerServe(t *testing.T) { - s := New(nil, nil, nil) + s := New(nil, nil, nil, "10000atto") go func() { time.Sleep(500 * time.Millisecond) s.Close() From 25b274f63d25c03b8252f9cc460f79bb9618e7c6 Mon Sep 17 00:00:00 2001 From: antho1404 Date: Fri, 28 Feb 2020 13:46:35 +0700 Subject: [PATCH 81/87] udpate e2e tests based on min price --- e2e/execution_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/e2e/execution_test.go b/e2e/execution_test.go index b48339d01..4b54ec11c 100644 --- a/e2e/execution_test.go +++ b/e2e/execution_test.go @@ -207,7 +207,7 @@ func testExecution(t *testing.T) { TaskKey: "task1", EventHash: hash.Int(1), ExecutorHash: executorHash, - Price: "50atto", + Price: "50000atto", Inputs: inputs, }) require.NoError(t, err) @@ -217,7 +217,7 @@ func testExecution(t *testing.T) { coins := sdk.Coins{} execAddress := sdk.AccAddress(crypto.AddressHash(resp.Hash)) lcdGet(t, "bank/balances/"+execAddress.String(), &coins) - require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(50))) + require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(50000))) }) _, err = streamInProgress.Recv() @@ -232,14 +232,14 @@ func testExecution(t *testing.T) { coins := sdk.Coins{} executorAddress := sdk.AccAddress(crypto.AddressHash(executorHash)) lcdGet(t, "bank/balances/"+executorAddress.String(), &coins) - require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(45))) + require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(45000))) }) // check balance of service t.Run("service balance", func(t *testing.T) { coins := sdk.Coins{} serviceAddress := sdk.AccAddress(crypto.AddressHash(testServiceHash)) lcdGet(t, "bank/balances/"+serviceAddress.String(), &coins) - require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(5))) + require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(5000))) }) // check balance of execution t.Run("execution balance", func(t *testing.T) { From acad04ede13032002c545b79d941e9022ea1d02f Mon Sep 17 00:00:00 2001 From: antho1404 Date: Fri, 28 Feb 2020 15:47:08 +0700 Subject: [PATCH 82/87] update e2e to take into account existing balance --- e2e/execution_test.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/e2e/execution_test.go b/e2e/execution_test.go index 4b54ec11c..d1eeb73ab 100644 --- a/e2e/execution_test.go +++ b/e2e/execution_test.go @@ -212,14 +212,22 @@ func testExecution(t *testing.T) { }) require.NoError(t, err) + execAddress := sdk.AccAddress(crypto.AddressHash(resp.Hash)) + executorAddress := sdk.AccAddress(crypto.AddressHash(executorHash)) + serviceAddress := sdk.AccAddress(crypto.AddressHash(testServiceHash)) + // check balance of execution before completed t.Run("execution balance before completed", func(t *testing.T) { coins := sdk.Coins{} - execAddress := sdk.AccAddress(crypto.AddressHash(resp.Hash)) lcdGet(t, "bank/balances/"+execAddress.String(), &coins) require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(50000))) }) + var executorBalance sdk.Coins + var serviceBalance sdk.Coins + lcdGet(t, "bank/balances/"+executorAddress.String(), &executorBalance) + lcdGet(t, "bank/balances/"+serviceAddress.String(), &serviceBalance) + _, err = streamInProgress.Recv() require.NoError(t, err) @@ -230,16 +238,14 @@ func testExecution(t *testing.T) { // check balance of executor t.Run("executor balance", func(t *testing.T) { coins := sdk.Coins{} - executorAddress := sdk.AccAddress(crypto.AddressHash(executorHash)) lcdGet(t, "bank/balances/"+executorAddress.String(), &coins) - require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(45000))) + require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(45000).Add(executorBalance.AmountOf("atto")))) }) // check balance of service t.Run("service balance", func(t *testing.T) { coins := sdk.Coins{} - serviceAddress := sdk.AccAddress(crypto.AddressHash(testServiceHash)) lcdGet(t, "bank/balances/"+serviceAddress.String(), &coins) - require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(5000))) + require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(5000).Add(serviceBalance.AmountOf("atto")))) }) // check balance of execution t.Run("execution balance", func(t *testing.T) { From eff9c6b36b931280cafbcec7b14cf7e96dbeffa3 Mon Sep 17 00:00:00 2001 From: antho1404 Date: Fri, 28 Feb 2020 18:05:51 +0700 Subject: [PATCH 83/87] move parsing of parameter in the keeper --- x/execution/internal/keeper/keeper.go | 9 ++++++--- x/execution/internal/keeper/params.go | 12 ++++-------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/x/execution/internal/keeper/keeper.go b/x/execution/internal/keeper/keeper.go index 940067059..c17453dc2 100644 --- a/x/execution/internal/keeper/keeper.go +++ b/x/execution/internal/keeper/keeper.go @@ -60,9 +60,12 @@ func (k *Keeper) Create(ctx sdk.Context, msg types.MsgCreateExecution) (*executi return nil, err } - minPrice := k.MinPrice(ctx) - if !price.IsAllGTE(minPrice) { - return nil, fmt.Errorf("execution price too low. Min value: %q", minPrice) + minPriceCoin, err := sdk.ParseCoins(k.MinPrice(ctx)) + if err != nil { + return nil, err + } + if !price.IsAllGTE(minPriceCoin) { + return nil, fmt.Errorf("execution price too low. Min value: %q", minPriceCoin.String()) } run, err := k.runnerKeeper.Get(ctx, msg.Request.ExecutorHash) diff --git a/x/execution/internal/keeper/params.go b/x/execution/internal/keeper/params.go index ef5f6b088..823b91a9c 100644 --- a/x/execution/internal/keeper/params.go +++ b/x/execution/internal/keeper/params.go @@ -11,14 +11,10 @@ const ( ) // MinPrice - Minimum price of an execution -func (k Keeper) MinPrice(ctx sdk.Context) sdk.Coins { - var coinstr string - k.paramstore.Get(ctx, types.KeyMinPrice, &coinstr) - price, err := sdk.ParseCoins(coinstr) - if err != nil { - panic(err) - } - return price +func (k Keeper) MinPrice(ctx sdk.Context) string { + var coins string + k.paramstore.Get(ctx, types.KeyMinPrice, &coins) + return coins } // SetParams will populate all the params From 9fe2bf717d91f93e7c7370bd49c8eebc54f9c672 Mon Sep 17 00:00:00 2001 From: Anthony Date: Fri, 28 Feb 2020 18:07:13 +0700 Subject: [PATCH 84/87] Update x/execution/internal/types/params.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Nicolas Mahé --- x/execution/internal/types/params.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/execution/internal/types/params.go b/x/execution/internal/types/params.go index db9f588b9..c960ea1d3 100644 --- a/x/execution/internal/types/params.go +++ b/x/execution/internal/types/params.go @@ -21,7 +21,7 @@ var ( // Params - used for initializing default parameter for instance at genesis type Params struct { - MinPrice string `json:"min_price" yaml:"min_price"` // min price to pay for an execution + MinPrice string `json:"minPrice" yaml:"minPrice"` // min price to pay for an execution } // NewParams creates a new Params object From 10be2cfbfe466211b7c07684f3c5f2f593d79901 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Mon, 2 Mar 2020 17:44:17 +0700 Subject: [PATCH 85/87] add changelog of v0.19 --- CHANGELOG.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index acdd1807d..032f97923 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,34 @@ # Changelog +## [v0.19.0](https://github.com/mesg-foundation/engine/releases/tag/v0.19.0) + +#### Added + +- ([#1651](https://github.com/mesg-foundation/engine/pull/1651)) Refactor ownership module to cosmos style. +- ([#1653](https://github.com/mesg-foundation/engine/pull/1653)) Refactor instance module to cosmos style. +- ([#1654](https://github.com/mesg-foundation/engine/pull/1654)) Add e2e lcd tests for ownerships. +- ([#1657](https://github.com/mesg-foundation/engine/pull/1657)) Refactor runner module to cosmos style. +- ([#1658](https://github.com/mesg-foundation/engine/pull/1658)) Refactor process module to cosmos style. +- ([#1661](https://github.com/mesg-foundation/engine/pull/1661)) Refactor service module to cosmos style. +- ([#1662](https://github.com/mesg-foundation/engine/pull/1662)) Refactor execution module to cosmos style. +- ([#1663](https://github.com/mesg-foundation/engine/pull/1663)) Add e2e tests using the light client daemon of Cosmos. +- ([#1670](https://github.com/mesg-foundation/engine/pull/1670)) Implement first iteration of the execution economy. +- ([#1680](https://github.com/mesg-foundation/engine/pull/1680)) Add block height to execution data. +- ([#1689](https://github.com/mesg-foundation/engine/pull/1689)) Add pagination system to http endpoint executions/list. +- ([#1691](https://github.com/mesg-foundation/engine/pull/1691)) Add min price for execution. + +#### Changed + +- ([#1640](https://github.com/mesg-foundation/engine/pull/1640)) Update to cosmos v0.38 and tendermint v0.33. +- ([#1645](https://github.com/mesg-foundation/engine/pull/1645)) Add trusted config to LCD server and index all events. +- ([#1650](https://github.com/mesg-foundation/engine/pull/1650)) Refactor cosmos modules. +- ([#1669](https://github.com/mesg-foundation/engine/pull/1669)) Use disable-all instead of enable-all in golangci config. + +#### Fixed + +- ([#1682](https://github.com/mesg-foundation/engine/pull/1682)) Fix Tendermint graceful stop. +- ([#1683](https://github.com/mesg-foundation/engine/pull/1683)) Call end block function in each cosmos modules. + ## [v0.18.3](https://github.com/mesg-foundation/engine/releases/tag/v0.18.3) #### Added From 46bf93157beabcd232d8ea21db03be65586e9d28 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Mon, 2 Mar 2020 17:47:26 +0700 Subject: [PATCH 86/87] add dependencies PR to changelog --- CHANGELOG.md | 6 ++++++ scripts/changelog.sh | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 032f97923..48bea6585 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,12 @@ - ([#1682](https://github.com/mesg-foundation/engine/pull/1682)) Fix Tendermint graceful stop. - ([#1683](https://github.com/mesg-foundation/engine/pull/1683)) Call end block function in each cosmos modules. +#### Dependencies + +- ([#1646](https://github.com/mesg-foundation/engine/pull/1646)) Bump google.golang.org/grpc from 1.27.0 to 1.27.1. +- ([#1648](https://github.com/mesg-foundation/engine/pull/1648)) Bump github.com/prometheus/client_golang from 1.4.0 to 1.4.1. +- ([#1656](https://github.com/mesg-foundation/engine/pull/1656)) Bump github.com/cosmos/cosmos-sdk from 0.38.0 to 0.38.1. + ## [v0.18.3](https://github.com/mesg-foundation/engine/releases/tag/v0.18.3) #### Added diff --git a/scripts/changelog.sh b/scripts/changelog.sh index 239ed5318..d386ad90d 100755 --- a/scripts/changelog.sh +++ b/scripts/changelog.sh @@ -4,8 +4,8 @@ MILESTONE="$1" -LABELS=("breaking change" "release:add" "release:change" "release:fix" "release:remove") -CATEGORIES=("Breaking Changes" "Added" "Changed" "Fixed" "Removed") +LABELS=("breaking change" "release:add" "release:change" "release:fix" "release:remove" "dependencies") +CATEGORIES=("Breaking Changes" "Added" "Changed" "Fixed" "Removed" "Dependencies") PR=$(hub pr list -s merged -f "%Mt %L|- ([%i](%U)) %t.%n" --sort-ascending | grep $MILESTONE) From 83b2a375ce88f6b6c9051673203ace23d14c540d Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Mon, 2 Mar 2020 17:53:44 +0700 Subject: [PATCH 87/87] improve changelog --- CHANGELOG.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48bea6585..07cffeb12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,15 +4,9 @@ #### Added -- ([#1651](https://github.com/mesg-foundation/engine/pull/1651)) Refactor ownership module to cosmos style. -- ([#1653](https://github.com/mesg-foundation/engine/pull/1653)) Refactor instance module to cosmos style. - ([#1654](https://github.com/mesg-foundation/engine/pull/1654)) Add e2e lcd tests for ownerships. -- ([#1657](https://github.com/mesg-foundation/engine/pull/1657)) Refactor runner module to cosmos style. -- ([#1658](https://github.com/mesg-foundation/engine/pull/1658)) Refactor process module to cosmos style. -- ([#1661](https://github.com/mesg-foundation/engine/pull/1661)) Refactor service module to cosmos style. -- ([#1662](https://github.com/mesg-foundation/engine/pull/1662)) Refactor execution module to cosmos style. - ([#1663](https://github.com/mesg-foundation/engine/pull/1663)) Add e2e tests using the light client daemon of Cosmos. -- ([#1670](https://github.com/mesg-foundation/engine/pull/1670)) Implement first iteration of the execution economy. +- ([#1670](https://github.com/mesg-foundation/engine/pull/1670)) Introduce execution payment. - ([#1680](https://github.com/mesg-foundation/engine/pull/1680)) Add block height to execution data. - ([#1689](https://github.com/mesg-foundation/engine/pull/1689)) Add pagination system to http endpoint executions/list. - ([#1691](https://github.com/mesg-foundation/engine/pull/1691)) Add min price for execution. @@ -22,6 +16,12 @@ - ([#1640](https://github.com/mesg-foundation/engine/pull/1640)) Update to cosmos v0.38 and tendermint v0.33. - ([#1645](https://github.com/mesg-foundation/engine/pull/1645)) Add trusted config to LCD server and index all events. - ([#1650](https://github.com/mesg-foundation/engine/pull/1650)) Refactor cosmos modules. +- ([#1651](https://github.com/mesg-foundation/engine/pull/1651)) Refactor ownership module to cosmos style. +- ([#1653](https://github.com/mesg-foundation/engine/pull/1653)) Refactor instance module to cosmos style. +- ([#1657](https://github.com/mesg-foundation/engine/pull/1657)) Refactor runner module to cosmos style. +- ([#1658](https://github.com/mesg-foundation/engine/pull/1658)) Refactor process module to cosmos style. +- ([#1661](https://github.com/mesg-foundation/engine/pull/1661)) Refactor service module to cosmos style. +- ([#1662](https://github.com/mesg-foundation/engine/pull/1662)) Refactor execution module to cosmos style. - ([#1669](https://github.com/mesg-foundation/engine/pull/1669)) Use disable-all instead of enable-all in golangci config. #### Fixed