Skip to content

Commit

Permalink
wip #2
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasMahe committed Dec 10, 2019
1 parent a653cf3 commit 5989d34
Show file tree
Hide file tree
Showing 17 changed files with 150 additions and 153 deletions.
16 changes: 6 additions & 10 deletions core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
8 changes: 3 additions & 5 deletions cosmos/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions internal/tools/gen-genesis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
44 changes: 22 additions & 22 deletions sdk/execution/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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])
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions sdk/execution/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions sdk/execution/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand Down
36 changes: 18 additions & 18 deletions sdk/instance/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions sdk/instance/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit 5989d34

Please sign in to comment.