diff --git a/core/main.go b/core/main.go index 5c736b54a..4e331b43f 100644 --- a/core/main.go +++ b/core/main.go @@ -91,7 +91,7 @@ func loadOrGenConfigAccount(kb *cosmos.Keybase, cfg *config.Config) (keys.Info, return kb.CreateAccount(cfg.Account.Name, mnemonic, "", cfg.Account.Password, 0, 0) } -func loadOrGenDevGenesis(app *cosmos.App, kb *cosmos.Keybase, cfg *config.Config) (*tmtypes.GenesisDoc, error) { +func loadOrGenDevGenesis(moduleManager *cosmos.ModuleManager, kb *cosmos.Keybase, cfg *config.Config) (*tmtypes.GenesisDoc, error) { if cosmos.GenesisExist(cfg.Tendermint.Config.GenesisFile()) { return cosmos.LoadGenesis(cfg.Tendermint.Config.GenesisFile()) } @@ -111,7 +111,7 @@ func loadOrGenDevGenesis(app *cosmos.App, kb *cosmos.Keybase, cfg *config.Config "nodeID": validator.NodeID, "peer": fmt.Sprintf("%s@%s:26656", validator.NodeID, validator.Name), }).Warnln("Validator") - return cosmos.GenGenesis(kb, app.DefaultGenesis(), cfg.DevGenesis.ChainID, cfg.Tendermint.Config.GenesisFile(), []cosmos.GenesisValidator{validator}) + return cosmos.GenGenesis(kb, moduleManager.BasicManager().DefaultGenesis(), cfg.DevGenesis.ChainID, cfg.Tendermint.Config.GenesisFile(), []cosmos.GenesisValidator{validator}) } func main() { @@ -144,15 +144,11 @@ func main() { if err != nil { logrus.WithField("module", "main").Fatalln(err) } - // TODO: rename NewModuleManager to something else - appFactory := cosmos.NewModuleManager(logger.TendermintLogger(), db) - - // register the backend modules to the module manager. - // TODO: this is a mandatory call so it should return a new types required by cosmos.NewApp - enginesdk.NewBackend(appFactory) + // register the module modules to the module manager. + modules := enginesdk.NewModules() // init cosmos app - app, err := cosmos.NewApp(appFactory) + app, err := cosmos.NewApp(modules.ModuleManager, logger.TendermintLogger(), db) if err != nil { logrus.WithField("module", "main").Fatalln(err) } @@ -171,7 +167,7 @@ func main() { logrus.WithField("address", acc.GetAddress().String()).Info("engine account") // load or gen genesis - genesis, err := loadOrGenDevGenesis(app, kb, cfg) + genesis, err := loadOrGenDevGenesis(modules.ModuleManager, kb, cfg) if err != nil { logrus.WithField("module", "main").Fatalln(err) } diff --git a/cosmos/cli/main.go b/cosmos/cli/main.go index 0d2c22e96..06d96b999 100644 --- a/cosmos/cli/main.go +++ b/cosmos/cli/main.go @@ -19,7 +19,6 @@ import ( genutilcmd "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/cosmos" enginesdk "github.com/mesg-foundation/engine/sdk" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -40,10 +39,9 @@ var ( func main() { cobra.EnableCommandSorting = false - // init app manager - moduleManager := cosmos.NewModuleManager() - enginesdk.NewBackend(moduleManager) - basicManager = moduleManager.BasicManager() + // init modules + modules := enginesdk.NewModules() + basicManager = modules.BasicManager() cdc := codec.Codec // Read in the configuration file for the sdk diff --git a/internal/tools/gen-genesis/main.go b/internal/tools/gen-genesis/main.go index 34adb5844..179e17adc 100644 --- a/internal/tools/gen-genesis/main.go +++ b/internal/tools/gen-genesis/main.go @@ -55,13 +55,12 @@ func main() { if err != nil { logrus.Fatalln(err) } - appFactory := cosmos.NewModuleManager(logger.TendermintLogger(), db) - // register the backend modules to the module manager. - enginesdk.NewBackend(appFactory) + // register the module modules to the module manager. + modules := enginesdk.NewModules() // init cosmos app - app, err := cosmos.NewApp(appFactory) + app, err := cosmos.NewApp(modules.ModuleManager, logger.TendermintLogger(), db) if err != nil { logrus.Fatalln(err) } @@ -116,7 +115,7 @@ func main() { } // generate and save genesis - _, err = cosmos.GenGenesis(kb, app.DefaultGenesis(), *chainid, filepath.Join(*path, "genesis.json"), vals) + _, err = cosmos.GenGenesis(kb, modules.BasicManager().DefaultGenesis(), *chainid, filepath.Join(*path, "genesis.json"), vals) if err != nil { logrus.Fatalln(err) } diff --git a/sdk/execution/backend.go b/sdk/execution/backend.go index 954ddae4b..e29971db3 100644 --- a/sdk/execution/backend.go +++ b/sdk/execution/backend.go @@ -17,33 +17,33 @@ import ( abci "github.com/tendermint/tendermint/abci/types" ) -const backendName = "execution" +const moduleName = "execution" -// Backend is the execution backend. -type Backend struct { +// Module is the execution module. +type Module struct { storeKey *cosmostypes.KVStoreKey - serviceBack *servicesdk.Backend - instanceBack *instancesdk.Backend - runnerBack *runnersdk.Backend + serviceBack *servicesdk.Module + instanceBack *instancesdk.Module + runnerBack *runnersdk.Module } -// NewBackend returns the backend of the execution sdk. -func NewBackend(appFactory *cosmos.ModuleManager, serviceBack *servicesdk.Backend, instanceBack *instancesdk.Backend, runnerBack *runnersdk.Backend) *Backend { - backend := &Backend{ - storeKey: cosmostypes.NewKVStoreKey(backendName), +// NewModule returns the module of the execution sdk. +func NewModule(appFactory *cosmos.ModuleManager, serviceBack *servicesdk.Module, instanceBack *instancesdk.Module, runnerBack *runnersdk.Module) *Module { + module := &Module{ + storeKey: cosmostypes.NewKVStoreKey(moduleName), serviceBack: serviceBack, instanceBack: instanceBack, runnerBack: runnerBack, } - appBackendBasic := cosmos.NewAppModuleBasic(backendName) - appBackend := cosmos.NewAppModule(appBackendBasic, backend.handler, backend.querier) - appFactory.RegisterModule(appBackend) - appFactory.RegisterStoreKey(backend.storeKey) + appModuleBasic := cosmos.NewAppModuleBasic(moduleName) + appModule := cosmos.NewAppModule(appModuleBasic, module.handler, module.querier) + appFactory.RegisterModule(appModule) + appFactory.RegisterStoreKey(module.storeKey) - return backend + return module } -func (s *Backend) handler(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { +func (s *Module) handler(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { switch msg := msg.(type) { case msgCreateExecution: exec, err := s.Create(request, msg) @@ -63,7 +63,7 @@ func (s *Backend) handler(request cosmostypes.Request, msg cosmostypes.Msg) (has } } -func (s *Backend) querier(request cosmostypes.Request, path []string, req abci.RequestQuery) (interface{}, error) { +func (s *Module) querier(request cosmostypes.Request, path []string, req abci.RequestQuery) (interface{}, error) { switch path[0] { case "get": hash, err := hash.Decode(path[1]) @@ -79,7 +79,7 @@ func (s *Backend) querier(request cosmostypes.Request, path []string, req abci.R } // Create creates a new execution from definition. -func (s *Backend) Create(request cosmostypes.Request, msg msgCreateExecution) (*execution.Execution, error) { +func (s *Module) Create(request cosmostypes.Request, msg msgCreateExecution) (*execution.Execution, error) { run, err := s.runnerBack.Get(request, msg.Request.ExecutorHash) if err != nil { return nil, err @@ -128,7 +128,7 @@ func (s *Backend) Create(request cosmostypes.Request, msg msgCreateExecution) (* } // Update updates a new execution from definition. -func (s *Backend) Update(request cosmostypes.Request, msg msgUpdateExecution) (*execution.Execution, error) { +func (s *Module) Update(request cosmostypes.Request, msg msgUpdateExecution) (*execution.Execution, error) { store := request.KVStore(s.storeKey) if !store.Has(msg.Request.Hash) { return nil, fmt.Errorf("execution %q doesn't exist", msg.Request.Hash) @@ -161,7 +161,7 @@ func (s *Backend) Update(request cosmostypes.Request, msg msgUpdateExecution) (* return exec, nil } -func (s *Backend) validateExecutionOutput(request cosmostypes.Request, instanceHash hash.Hash, taskKey string, outputs *types.Struct) error { +func (s *Module) validateExecutionOutput(request cosmostypes.Request, instanceHash hash.Hash, taskKey string, outputs *types.Struct) error { inst, err := s.instanceBack.Get(request, instanceHash) if err != nil { return err @@ -174,7 +174,7 @@ func (s *Backend) validateExecutionOutput(request cosmostypes.Request, instanceH } // Get returns the execution that matches given hash. -func (s *Backend) Get(request cosmostypes.Request, hash hash.Hash) (*execution.Execution, error) { +func (s *Module) Get(request cosmostypes.Request, hash hash.Hash) (*execution.Execution, error) { var exec *execution.Execution store := request.KVStore(s.storeKey) if !store.Has(hash) { @@ -184,7 +184,7 @@ func (s *Backend) Get(request cosmostypes.Request, hash hash.Hash) (*execution.E } // List returns all executions. -func (s *Backend) List(request cosmostypes.Request) ([]*execution.Execution, error) { +func (s *Module) List(request cosmostypes.Request) ([]*execution.Execution, error) { var ( execs []*execution.Execution iter = request.KVStore(s.storeKey).Iterator(nil, nil) diff --git a/sdk/execution/msgs.go b/sdk/execution/msgs.go index 1dfbd6753..bcac77e1e 100644 --- a/sdk/execution/msgs.go +++ b/sdk/execution/msgs.go @@ -26,7 +26,7 @@ func newMsgCreateExecution(req *api.CreateExecutionRequest, signer cosmostypes.A // Route should return the name of the module. func (msg msgCreateExecution) Route() string { - return backendName + return moduleName } // Type returns the action. @@ -77,7 +77,7 @@ func newMsgUpdateExecution(req *api.UpdateExecutionRequest, executor cosmostypes // Route should return the name of the module. func (msg msgUpdateExecution) Route() string { - return backendName + return moduleName } // Type returns the action. diff --git a/sdk/execution/sdk.go b/sdk/execution/sdk.go index 16ee9d4a2..5c014ce1f 100644 --- a/sdk/execution/sdk.go +++ b/sdk/execution/sdk.go @@ -66,7 +66,7 @@ func (s *SDK) Update(req *api.UpdateExecutionRequest, accountName, accountPasswo // 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/"+backendName+"/get/"+hash.String(), nil, &execution); err != nil { + if err := s.client.Query("custom/"+moduleName+"/get/"+hash.String(), nil, &execution); err != nil { return nil, err } return &execution, nil @@ -75,7 +75,7 @@ func (s *SDK) Get(hash hash.Hash) (*execution.Execution, error) { // List returns all executions. func (s *SDK) List() ([]*execution.Execution, error) { var executions []*execution.Execution - if err := s.client.Query("custom/"+backendName+"/list", nil, &executions); err != nil { + if err := s.client.Query("custom/"+moduleName+"/list", nil, &executions); err != nil { return nil, err } return executions, nil @@ -87,7 +87,7 @@ func (s *SDK) Stream(ctx context.Context, req *api.StreamExecutionRequest) (chan return nil, nil, err } - stream, serrC, err := s.client.Stream(ctx, cosmos.EventModuleQuery(backendName)) + stream, serrC, err := s.client.Stream(ctx, cosmos.EventModuleQuery(moduleName)) if err != nil { return nil, nil, err } diff --git a/sdk/instance/backend.go b/sdk/instance/backend.go index be6900def..74d29ce09 100644 --- a/sdk/instance/backend.go +++ b/sdk/instance/backend.go @@ -13,31 +13,31 @@ import ( abci "github.com/tendermint/tendermint/abci/types" ) -const backendName = "instance" +const moduleName = "instance" -// Backend is the instance backend. -type Backend struct { +// Module is the instance module. +type Module struct { storeKey *cosmostypes.KVStoreKey } -// NewBackend returns the backend of the instance sdk. -func NewBackend(appFactory *cosmos.ModuleManager) *Backend { - backend := &Backend{ - storeKey: cosmostypes.NewKVStoreKey(backendName), +// NewModule returns the module of the instance sdk. +func NewModule(appFactory *cosmos.ModuleManager) *Module { + module := &Module{ + storeKey: cosmostypes.NewKVStoreKey(moduleName), } - appBackendBasic := cosmos.NewAppModuleBasic(backendName) - appBackend := cosmos.NewAppModule(appBackendBasic, backend.handler, backend.querier) - appFactory.RegisterModule(appBackend) - appFactory.RegisterStoreKey(backend.storeKey) - return backend + appModuleBasic := cosmos.NewAppModuleBasic(moduleName) + appModule := cosmos.NewAppModule(appModuleBasic, module.handler, module.querier) + appFactory.RegisterModule(appModule) + appFactory.RegisterStoreKey(module.storeKey) + return module } -func (s *Backend) handler(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { +func (s *Module) handler(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { errmsg := fmt.Sprintf("Unrecognized instance Msg type: %v", msg.Type()) return nil, cosmostypes.ErrUnknownRequest(errmsg) } -func (s *Backend) querier(request cosmostypes.Request, path []string, req abci.RequestQuery) (interface{}, error) { +func (s *Module) querier(request cosmostypes.Request, path []string, req abci.RequestQuery) (interface{}, error) { switch path[0] { case "get": hash, err := hash.Decode(path[1]) @@ -63,7 +63,7 @@ func (s *Backend) querier(request cosmostypes.Request, path []string, req abci.R } // FetchOrCreate creates a new instance if needed. -func (s *Backend) FetchOrCreate(request cosmostypes.Request, serviceHash hash.Hash, envHash hash.Hash) (*instance.Instance, error) { +func (s *Module) FetchOrCreate(request cosmostypes.Request, serviceHash hash.Hash, envHash hash.Hash) (*instance.Instance, error) { inst := &instance.Instance{ ServiceHash: serviceHash, EnvHash: envHash, @@ -82,7 +82,7 @@ func (s *Backend) FetchOrCreate(request cosmostypes.Request, serviceHash hash.Ha } // Get returns the instance that matches given hash. -func (s *Backend) Get(request cosmostypes.Request, hash hash.Hash) (*instance.Instance, error) { +func (s *Module) Get(request cosmostypes.Request, hash hash.Hash) (*instance.Instance, error) { var i *instance.Instance store := request.KVStore(s.storeKey) if !store.Has(hash) { @@ -93,12 +93,12 @@ func (s *Backend) Get(request cosmostypes.Request, hash hash.Hash) (*instance.In } // Exists returns true if a specific set of data exists in the database, false otherwise -func (s *Backend) Exists(request cosmostypes.Request, hash hash.Hash) (bool, error) { +func (s *Module) Exists(request cosmostypes.Request, hash hash.Hash) (bool, error) { return request.KVStore(s.storeKey).Has(hash), nil } // List returns all instances. -func (s *Backend) List(request cosmostypes.Request, f *api.ListInstanceRequest_Filter) ([]*instance.Instance, error) { +func (s *Module) List(request cosmostypes.Request, f *api.ListInstanceRequest_Filter) ([]*instance.Instance, error) { var ( instances []*instance.Instance iter = request.KVStore(s.storeKey).Iterator(nil, nil) diff --git a/sdk/instance/sdk.go b/sdk/instance/sdk.go index 774ab94eb..7ce6f2f93 100644 --- a/sdk/instance/sdk.go +++ b/sdk/instance/sdk.go @@ -23,7 +23,7 @@ func New(client *cosmos.Client) *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/"+backendName+"/get/"+hash.String(), nil, &instance); err != nil { + if err := s.client.Query("custom/"+moduleName+"/get/"+hash.String(), nil, &instance); err != nil { return nil, err } return &instance, nil @@ -32,7 +32,7 @@ func (s *SDK) Get(hash hash.Hash) (*instance.Instance, error) { // 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/"+backendName+"/list", f, &instances); err != nil { + if err := s.client.Query("custom/"+moduleName+"/list", f, &instances); err != nil { return nil, err } return instances, nil @@ -41,7 +41,7 @@ func (s *SDK) List(f *api.ListInstanceRequest_Filter) ([]*instance.Instance, err // 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/"+backendName+"/exists/"+hash.String(), nil, &exists); err != nil { + if err := s.client.Query("custom/"+moduleName+"/exists/"+hash.String(), nil, &exists); err != nil { return false, err } return exists, nil diff --git a/sdk/modules.go b/sdk/modules.go index 0660d4961..502ffb12f 100644 --- a/sdk/modules.go +++ b/sdk/modules.go @@ -22,29 +22,33 @@ import ( servicesdk "github.com/mesg-foundation/engine/sdk/service" ) -// Backend handles all the backend functions. -type Backend struct { - Service *servicesdk.Backend - Execution *executionsdk.Backend - Ownership *ownershipsdk.Backend - Instance *instancesdk.Backend - Runner *runnersdk.Backend +// Modules handles all the module functions. +type Modules struct { + *cosmos.ModuleManager + + Service *servicesdk.Module + Execution *executionsdk.Module + Ownership *ownershipsdk.Module + Instance *instancesdk.Module + Runner *runnersdk.Module } -// NewBackend creates a new backend and init the sub-backend modules. -func NewBackend(moduleManager *cosmos.ModuleManager) *Backend { +// NewModules creates a new module manager and init the sub-module modules. +func NewModules() *Modules { + moduleManager := cosmos.NewModuleManager() initDefaultCosmosModules(moduleManager) - ownership := ownershipsdk.NewBackend(moduleManager) - service := servicesdk.NewBackend(moduleManager, ownership) - instance := instancesdk.NewBackend(moduleManager) - runner := runnersdk.NewBackend(moduleManager, instance) - execution := executionsdk.NewBackend(moduleManager, service, instance, runner) - return &Backend{ - Service: service, - Ownership: ownership, - Instance: instance, - Runner: runner, - Execution: execution, + ownership := ownershipsdk.NewModule(moduleManager) + service := servicesdk.NewModule(moduleManager, ownership) + instance := instancesdk.NewModule(moduleManager) + runner := runnersdk.NewModule(moduleManager, instance) + execution := executionsdk.NewModule(moduleManager, service, instance, runner) + return &Modules{ + ModuleManager: moduleManager, + Service: service, + Ownership: ownership, + Instance: instance, + Runner: runner, + Execution: execution, } } diff --git a/sdk/ownership/backend.go b/sdk/ownership/backend.go index 56e644c4e..b8efaf97b 100644 --- a/sdk/ownership/backend.go +++ b/sdk/ownership/backend.go @@ -12,31 +12,31 @@ import ( abci "github.com/tendermint/tendermint/abci/types" ) -const backendName = "ownership" +const moduleName = "ownership" -// Backend is the ownership backend. -type Backend struct { +// Module is the ownership module. +type Module struct { storeKey *cosmostypes.KVStoreKey } -// NewBackend returns the backend of the ownership sdk. -func NewBackend(appFactory *cosmos.ModuleManager) *Backend { - backend := &Backend{ - storeKey: cosmostypes.NewKVStoreKey(backendName), +// NewModule returns the module of the ownership sdk. +func NewModule(appFactory *cosmos.ModuleManager) *Module { + module := &Module{ + storeKey: cosmostypes.NewKVStoreKey(moduleName), } - appBackendBasic := cosmos.NewAppModuleBasic(backendName) - appBackend := cosmos.NewAppModule(appBackendBasic, backend.handler, backend.querier) - appFactory.RegisterModule(appBackend) - appFactory.RegisterStoreKey(backend.storeKey) - return backend + appModuleBasic := cosmos.NewAppModuleBasic(moduleName) + appModule := cosmos.NewAppModule(appModuleBasic, module.handler, module.querier) + appFactory.RegisterModule(appModule) + appFactory.RegisterStoreKey(module.storeKey) + return module } -func (s *Backend) handler(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { +func (s *Module) handler(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { errmsg := fmt.Sprintf("Unrecognized ownership Msg type: %v", msg.Type()) return nil, cosmostypes.ErrUnknownRequest(errmsg) } -func (s *Backend) querier(request cosmostypes.Request, path []string, req abci.RequestQuery) (interface{}, error) { +func (s *Module) querier(request cosmostypes.Request, path []string, req abci.RequestQuery) (interface{}, error) { switch path[0] { case "list": return s.List(request) @@ -46,7 +46,7 @@ func (s *Backend) querier(request cosmostypes.Request, path []string, req abci.R } // CreateServiceOwnership creates a new ownership. -func (s *Backend) CreateServiceOwnership(request cosmostypes.Request, serviceHash hash.Hash, owner cosmostypes.AccAddress) (*ownership.Ownership, error) { +func (s *Module) CreateServiceOwnership(request cosmostypes.Request, serviceHash hash.Hash, owner cosmostypes.AccAddress) (*ownership.Ownership, error) { store := request.KVStore(s.storeKey) // check if owner is authorized to create the ownership allOwnshp, err := s.List(request) @@ -74,7 +74,7 @@ func (s *Backend) CreateServiceOwnership(request cosmostypes.Request, serviceHas } // List returns all ownerships. -func (s *Backend) List(request cosmostypes.Request) ([]*ownership.Ownership, error) { +func (s *Module) List(request cosmostypes.Request) ([]*ownership.Ownership, error) { var ( ownerships []*ownership.Ownership iter = request.KVStore(s.storeKey).Iterator(nil, nil) diff --git a/sdk/ownership/sdk.go b/sdk/ownership/sdk.go index 7c9c9c46c..cd633ad71 100644 --- a/sdk/ownership/sdk.go +++ b/sdk/ownership/sdk.go @@ -21,7 +21,7 @@ func New(client *cosmos.Client) *SDK { // List returns all ownerships. func (s *SDK) List() ([]*ownership.Ownership, error) { var ownerships []*ownership.Ownership - if err := s.client.Query("custom/"+backendName+"/list", nil, &ownerships); err != nil { + if err := s.client.Query("custom/"+moduleName+"/list", nil, &ownerships); err != nil { return nil, err } return ownerships, nil diff --git a/sdk/runner/backend.go b/sdk/runner/backend.go index be8f084a8..e1b22bc91 100644 --- a/sdk/runner/backend.go +++ b/sdk/runner/backend.go @@ -13,29 +13,29 @@ import ( abci "github.com/tendermint/tendermint/abci/types" ) -const backendName = "runner" +const moduleName = "runner" -// Backend is the runner backend. -type Backend struct { +// Module is the runner module. +type Module struct { storeKey *cosmostypes.KVStoreKey - instanceBack *instancesdk.Backend + instanceBack *instancesdk.Module } -// NewBackend returns the backend of the runner sdk. -func NewBackend(appFactory *cosmos.ModuleManager, instanceBack *instancesdk.Backend) *Backend { - backend := &Backend{ - storeKey: cosmostypes.NewKVStoreKey(backendName), +// NewModule returns the module of the runner sdk. +func NewModule(appFactory *cosmos.ModuleManager, instanceBack *instancesdk.Module) *Module { + module := &Module{ + storeKey: cosmostypes.NewKVStoreKey(moduleName), instanceBack: instanceBack, } - appBackendBasic := cosmos.NewAppModuleBasic(backendName) - appBackend := cosmos.NewAppModule(appBackendBasic, backend.handler, backend.querier) - appFactory.RegisterModule(appBackend) - appFactory.RegisterStoreKey(backend.storeKey) + appModuleBasic := cosmos.NewAppModuleBasic(moduleName) + appModule := cosmos.NewAppModule(appModuleBasic, module.handler, module.querier) + appFactory.RegisterModule(appModule) + appFactory.RegisterStoreKey(module.storeKey) - return backend + return module } -func (s *Backend) handler(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { +func (s *Module) handler(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { switch msg := msg.(type) { case msgCreateRunner: run, err := s.Create(request, &msg) @@ -54,7 +54,7 @@ func (s *Backend) handler(request cosmostypes.Request, msg cosmostypes.Msg) (has } } -func (s *Backend) querier(request cosmostypes.Request, path []string, req abci.RequestQuery) (interface{}, error) { +func (s *Module) querier(request cosmostypes.Request, path []string, req abci.RequestQuery) (interface{}, error) { switch path[0] { case "get": hash, err := hash.Decode(path[1]) @@ -70,7 +70,7 @@ func (s *Backend) querier(request cosmostypes.Request, path []string, req abci.R } // Create creates a new runner. -func (s *Backend) Create(request cosmostypes.Request, msg *msgCreateRunner) (*runner.Runner, error) { +func (s *Module) Create(request cosmostypes.Request, msg *msgCreateRunner) (*runner.Runner, error) { store := request.KVStore(s.storeKey) inst, err := s.instanceBack.FetchOrCreate(request, msg.ServiceHash, msg.EnvHash) if err != nil { @@ -93,7 +93,7 @@ func (s *Backend) Create(request cosmostypes.Request, msg *msgCreateRunner) (*ru } // Delete deletes a runner. -func (s *Backend) Delete(request cosmostypes.Request, msg *msgDeleteRunner) error { +func (s *Module) Delete(request cosmostypes.Request, msg *msgDeleteRunner) error { store := request.KVStore(s.storeKey) run := runner.Runner{} value := store.Get(msg.RunnerHash) @@ -108,7 +108,7 @@ func (s *Backend) Delete(request cosmostypes.Request, msg *msgDeleteRunner) erro } // Get returns the runner that matches given hash. -func (s *Backend) Get(request cosmostypes.Request, hash hash.Hash) (*runner.Runner, error) { +func (s *Module) Get(request cosmostypes.Request, hash hash.Hash) (*runner.Runner, error) { store := request.KVStore(s.storeKey) if !store.Has(hash) { return nil, fmt.Errorf("runner %q not found", hash) @@ -119,7 +119,7 @@ func (s *Backend) Get(request cosmostypes.Request, hash hash.Hash) (*runner.Runn } // List returns all runners. -func (s *Backend) List(request cosmostypes.Request) ([]*runner.Runner, error) { +func (s *Module) List(request cosmostypes.Request) ([]*runner.Runner, error) { var ( runners []*runner.Runner iter = request.KVStore(s.storeKey).Iterator(nil, nil) diff --git a/sdk/runner/msgs.go b/sdk/runner/msgs.go index 418e01099..50b63af48 100644 --- a/sdk/runner/msgs.go +++ b/sdk/runner/msgs.go @@ -26,7 +26,7 @@ func newMsgCreateRunner(address cosmostypes.AccAddress, serviceHash hash.Hash, e // Route should return the name of the module. func (msg msgCreateRunner) Route() string { - return backendName + return moduleName } // Type returns the action. @@ -77,7 +77,7 @@ func newMsgDeleteRunner(address cosmostypes.AccAddress, runnerHash hash.Hash) *m // Route should return the name of the module. func (msg msgDeleteRunner) Route() string { - return backendName + return moduleName } // Type returns the action. diff --git a/sdk/runner/sdk.go b/sdk/runner/sdk.go index 39ebb04c9..0f2ee8fb0 100644 --- a/sdk/runner/sdk.go +++ b/sdk/runner/sdk.go @@ -167,7 +167,7 @@ func (s *SDK) Delete(req *api.DeleteRunnerRequest, accountName, accountPassword // 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/"+backendName+"/get/"+hash.String(), nil, &runner); err != nil { + if err := s.client.Query("custom/"+moduleName+"/get/"+hash.String(), nil, &runner); err != nil { return nil, err } return &runner, nil @@ -176,7 +176,7 @@ func (s *SDK) Get(hash hash.Hash) (*runner.Runner, error) { // List returns all runners. func (s *SDK) List(f *Filter) ([]*runner.Runner, error) { var runners []*runner.Runner - if err := s.client.Query("custom/"+backendName+"/list", nil, &runners); err != nil { + if err := s.client.Query("custom/"+moduleName+"/list", nil, &runners); err != nil { return nil, err } // no filter, returns diff --git a/sdk/service/backend.go b/sdk/service/backend.go index b3cb153d9..f87bc7d65 100644 --- a/sdk/service/backend.go +++ b/sdk/service/backend.go @@ -15,29 +15,29 @@ import ( abci "github.com/tendermint/tendermint/abci/types" ) -const backendName = "service" +const moduleName = "service" -// Backend is the service backend. -type Backend struct { +// Module is the service module. +type Module struct { storeKey *cosmostypes.KVStoreKey - ownerships *ownershipsdk.Backend + ownerships *ownershipsdk.Module } -// NewBackend returns the backend of the service sdk. -func NewBackend(appFactory *cosmos.ModuleManager, ownerships *ownershipsdk.Backend) *Backend { - backend := &Backend{ - storeKey: cosmostypes.NewKVStoreKey(backendName), +// NewModule returns the module of the service sdk. +func NewModule(appFactory *cosmos.ModuleManager, ownerships *ownershipsdk.Module) *Module { + module := &Module{ + storeKey: cosmostypes.NewKVStoreKey(moduleName), ownerships: ownerships, } - appBackendBasic := cosmos.NewAppModuleBasic(backendName) - appBackend := cosmos.NewAppModule(appBackendBasic, backend.handler, backend.querier) - appFactory.RegisterModule(appBackend) - appFactory.RegisterStoreKey(backend.storeKey) + appModuleBasic := cosmos.NewAppModuleBasic(moduleName) + appModule := cosmos.NewAppModule(appModuleBasic, module.handler, module.querier) + appFactory.RegisterModule(appModule) + appFactory.RegisterStoreKey(module.storeKey) - return backend + return module } -func (s *Backend) handler(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { +func (s *Module) handler(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { switch msg := msg.(type) { case msgCreateService: srv, err := s.Create(request, &msg) @@ -51,7 +51,7 @@ func (s *Backend) handler(request cosmostypes.Request, msg cosmostypes.Msg) (has } } -func (s *Backend) querier(request cosmostypes.Request, path []string, req abci.RequestQuery) (interface{}, error) { +func (s *Module) querier(request cosmostypes.Request, path []string, req abci.RequestQuery) (interface{}, error) { switch path[0] { case "get": hash, err := hash.Decode(path[1]) @@ -80,7 +80,7 @@ func (s *Backend) querier(request cosmostypes.Request, path []string, req abci.R } // Create creates a new service. -func (s *Backend) Create(request cosmostypes.Request, msg *msgCreateService) (*service.Service, error) { +func (s *Module) Create(request cosmostypes.Request, msg *msgCreateService) (*service.Service, error) { store := request.KVStore(s.storeKey) // create service srv := initializeService(msg.Request) @@ -113,7 +113,7 @@ func (s *Backend) Create(request cosmostypes.Request, msg *msgCreateService) (*s } // Get returns the service that matches given hash. -func (s *Backend) Get(request cosmostypes.Request, hash hash.Hash) (*service.Service, error) { +func (s *Module) Get(request cosmostypes.Request, hash hash.Hash) (*service.Service, error) { var sv *service.Service store := request.KVStore(s.storeKey) if !store.Has(hash) { @@ -124,17 +124,17 @@ func (s *Backend) Get(request cosmostypes.Request, hash hash.Hash) (*service.Ser } // Exists returns true if a specific set of data exists in the database, false otherwise -func (s *Backend) Exists(request cosmostypes.Request, hash hash.Hash) (bool, error) { +func (s *Module) Exists(request cosmostypes.Request, hash hash.Hash) (bool, error) { return request.KVStore(s.storeKey).Has(hash), nil } // Hash returns the hash of a service request. -func (s *Backend) Hash(serviceRequest *api.CreateServiceRequest) hash.Hash { +func (s *Module) Hash(serviceRequest *api.CreateServiceRequest) hash.Hash { return initializeService(serviceRequest).Hash } // List returns all services. -func (s *Backend) List(request cosmostypes.Request) ([]*service.Service, error) { +func (s *Module) List(request cosmostypes.Request) ([]*service.Service, error) { var ( services []*service.Service iter = request.KVStore(s.storeKey).Iterator(nil, nil) diff --git a/sdk/service/msgs.go b/sdk/service/msgs.go index de1f95f70..1985734fd 100644 --- a/sdk/service/msgs.go +++ b/sdk/service/msgs.go @@ -23,7 +23,7 @@ func newMsgCreateService(req *api.CreateServiceRequest, owner cosmostypes.AccAdd // Route should return the name of the module. func (msg msgCreateService) Route() string { - return backendName + return moduleName } // Type returns the action. diff --git a/sdk/service/sdk.go b/sdk/service/sdk.go index 7a8ed42e9..179ce7a55 100644 --- a/sdk/service/sdk.go +++ b/sdk/service/sdk.go @@ -46,7 +46,7 @@ func (s *SDK) Create(req *api.CreateServiceRequest, accountName, accountPassword // 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/"+backendName+"/get/"+hash.String(), nil, &service); err != nil { + if err := s.client.Query("custom/"+moduleName+"/get/"+hash.String(), nil, &service); err != nil { return nil, err } return &service, nil @@ -55,7 +55,7 @@ func (s *SDK) Get(hash hash.Hash) (*service.Service, error) { // List returns all services. func (s *SDK) List() ([]*service.Service, error) { var services []*service.Service - if err := s.client.Query("custom/"+backendName+"/list", nil, &services); err != nil { + if err := s.client.Query("custom/"+moduleName+"/list", nil, &services); err != nil { return nil, err } return services, nil @@ -64,7 +64,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/"+backendName+"/exists/"+hash.String(), nil, &exists); err != nil { + if err := s.client.Query("custom/"+moduleName+"/exists/"+hash.String(), nil, &exists); err != nil { return false, err } return exists, nil @@ -73,7 +73,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/"+backendName+"/hash", req, &h); err != nil { + if err := s.client.Query("custom/"+moduleName+"/hash", req, &h); err != nil { return nil, err } return h, nil