-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add service sdk cosmos version step 2 #1252
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d5d28e5
final result
NicolasMahe 9e726ec
Merge branch 'cosmos/start-node' into cosmos/service-sdk-step-2
NicolasMahe ea47bd2
Add service sdk new sdk and its module. update sdk
NicolasMahe bd00075
Merge branch 'cosmos/service-sdk-step-1' into cosmos/service-sdk-step-2
NicolasMahe 5924acb
Merge branch 'cosmos/start-node' into cosmos/service-sdk-step-2
NicolasMahe 5e026b1
Merge branch 'cosmos/start-node' into cosmos/service-sdk-step-2
NicolasMahe fd64642
start system service only in non-network mode
NicolasMahe 4ed1bfd
Merge branch 'dev' into cosmos/service-sdk-step-2
a007ae6
Merge commit 'b68fcadcaab26e12a6b967afc578a4057f13206c' into cosmos/s…
NicolasMahe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package servicesdk | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
cosmostypes "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/mesg-foundation/engine/container" | ||
"github.com/mesg-foundation/engine/cosmos" | ||
"github.com/mesg-foundation/engine/database" | ||
"github.com/mesg-foundation/engine/database/store" | ||
"github.com/mesg-foundation/engine/hash" | ||
"github.com/mesg-foundation/engine/service" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
) | ||
|
||
// Module is the service module. | ||
type Module struct { | ||
container container.Container | ||
name string | ||
cdc *codec.Codec | ||
storeKey *cosmostypes.KVStoreKey | ||
} | ||
|
||
// NewModule returns the module of the service sdk. | ||
func NewModule(app *cosmos.App, c container.Container) *Module { | ||
name := "service" | ||
module := &Module{ | ||
container: c, | ||
name: name, | ||
cdc: app.Cdc(), | ||
storeKey: cosmostypes.NewKVStoreKey(name), | ||
} | ||
appModuleBasic := cosmos.NewAppModuleBasic(name) | ||
appModule := cosmos.NewAppModule(appModuleBasic, module.cdc, module.handler, module.querier) | ||
app.RegisterModule(appModule) | ||
app.RegisterStoreKey(module.storeKey) | ||
return module | ||
} | ||
|
||
func (s *Module) db(request cosmostypes.Request) *database.ServiceDB { | ||
return database.NewServiceDB(store.NewCosmosStore(request.KVStore(s.storeKey))) | ||
} | ||
|
||
func (s *Module) handler(request cosmostypes.Request, msg cosmostypes.Msg) cosmostypes.Result { | ||
panic("to implement") | ||
// switch msg := msg.(type) { | ||
// case MsgCreateService: | ||
// _, err := s.Create(request, msg.Service) | ||
// if err != nil { | ||
// return cosmostypes.ErrInternal(err.Error()).Result() | ||
// } | ||
// return cosmostypes.Result{} | ||
// case MsgRemoveService: | ||
// err := s.Delete(request, msg.Hash) | ||
// if err != nil { | ||
// return cosmostypes.ErrInternal(err.Error()).Result() | ||
// } | ||
// return cosmostypes.Result{} | ||
// default: | ||
// errmsg := fmt.Sprintf("Unrecognized service Msg type: %v", msg.Type()) | ||
// return cosmostypes.ErrUnknownRequest(errmsg).Result() | ||
// } | ||
} | ||
|
||
func (s *Module) querier(request cosmostypes.Request, path []string, req abci.RequestQuery) (interface{}, error) { | ||
panic("to implement") | ||
// switch path[0] { | ||
// case "get": | ||
// hash, err := hash.Decode(path[1]) | ||
// if err != nil { | ||
// return nil, err | ||
// } | ||
// return s.Get(request, hash) | ||
// case "list": | ||
// return s.All(request) | ||
// default: | ||
// return nil, fmt.Errorf("unknown service query endpoint %q", path[0]) | ||
// } | ||
} | ||
|
||
// Create creates a new service from definition. | ||
func (s *Module) Create(request cosmostypes.Request, srv *service.Service) (*service.Service, error) { | ||
return create(s.container, s.db(request), srv) | ||
} | ||
|
||
// Delete deletes the service by hash. | ||
func (s *Module) Delete(request cosmostypes.Request, hash hash.Hash) error { | ||
return s.db(request).Delete(hash) | ||
} | ||
|
||
// Get returns the service that matches given hash. | ||
func (s *Module) Get(request cosmostypes.Request, hash hash.Hash) (*service.Service, error) { | ||
return s.db(request).Get(hash) | ||
} | ||
|
||
// List returns all services. | ||
func (s *Module) List(request cosmostypes.Request) ([]*service.Service, error) { | ||
return s.db(request).All() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package servicesdk | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mesg-foundation/engine/cosmos" | ||
"github.com/mesg-foundation/engine/hash" | ||
"github.com/mesg-foundation/engine/service" | ||
) | ||
|
||
type sdk struct { | ||
app *cosmos.App | ||
} | ||
|
||
// NewSDK returns the service sdk. | ||
func NewSDK(app *cosmos.App) Service { | ||
sdk := &sdk{ | ||
app: app, | ||
} | ||
return sdk | ||
} | ||
|
||
// Create creates a new service from definition. | ||
func (s *sdk) Create(srv *service.Service) (*service.Service, error) { | ||
return nil, fmt.Errorf("create not implemented") | ||
} | ||
|
||
// Delete deletes the service by hash. | ||
func (s *sdk) Delete(hash hash.Hash) error { | ||
return fmt.Errorf("delete not implemented") | ||
} | ||
|
||
// Get returns the service that matches given hash. | ||
func (s *sdk) Get(hash hash.Hash) (*service.Service, error) { | ||
return nil, fmt.Errorf("get not implemented") | ||
} | ||
|
||
// List returns all services. | ||
func (s *sdk) List() ([]*service.Service, error) { | ||
return nil, fmt.Errorf("list not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package servicesdk | ||
|
||
import ( | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/docker/docker/pkg/archive" | ||
"github.com/mesg-foundation/engine/container" | ||
"github.com/mesg-foundation/engine/database" | ||
"github.com/mesg-foundation/engine/hash" | ||
"github.com/mesg-foundation/engine/hash/dirhash" | ||
"github.com/mesg-foundation/engine/service" | ||
"github.com/mesg-foundation/engine/service/validator" | ||
) | ||
|
||
func create(container container.Container, db *database.ServiceDB, srv *service.Service) (*service.Service, error) { | ||
if srv.Configuration == nil { | ||
srv.Configuration = &service.Configuration{} | ||
} | ||
// download and untar service context into path. | ||
path, err := ioutil.TempDir("", "mesg") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer os.RemoveAll(path) | ||
|
||
resp, err := http.Get("http://ipfs.app.mesg.com:8080/ipfs/" + srv.Source) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode != 200 { | ||
return nil, errors.New("service's source code is not reachable") | ||
} | ||
defer resp.Body.Close() | ||
|
||
if err := archive.Untar(resp.Body, path, nil); err != nil { | ||
return nil, err | ||
} | ||
|
||
// calculate and apply hash to service. | ||
dh := dirhash.New(path) | ||
h, err := dh.Sum(hash.Dump(srv)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
srv.Hash = hash.Hash(h) | ||
|
||
// check if service already exists. | ||
if _, err := db.Get(srv.Hash); err == nil { | ||
return nil, &AlreadyExistsError{Hash: srv.Hash} | ||
} | ||
|
||
// build service's Docker image. | ||
_, err = container.Build(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// 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 | ||
} | ||
return srv, db.Save(srv) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a warning here, core services are just started when we run the deprecated sdk but are stopped all the time. We might have some errors when we stop them.