-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1252 from mesg-foundation/cosmos/service-sdk-step-2
Add service sdk cosmos version step 2
- Loading branch information
Showing
7 changed files
with
219 additions
and
69 deletions.
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) | ||
} |