Skip to content

Commit

Permalink
Change service hash calculation
Browse files Browse the repository at this point in the history
- use only service struct to calculate the hash
- remove dirhash package
- remove container ref from backend struct
  • Loading branch information
krhubert committed Sep 27, 2019
1 parent 42ebfc4 commit 97a80d8
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 275 deletions.
2 changes: 1 addition & 1 deletion core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func main() {
appFactory := cosmos.NewAppFactory(logger.TendermintLogger(), db)

// register the backend modules to the app factory.
enginesdk.NewBackend(appFactory, c)
enginesdk.NewBackend(appFactory)

// init cosmos app
app, err := cosmos.NewApp(appFactory)
Expand Down
97 changes: 0 additions & 97 deletions hash/dirhash/dirhash.go

This file was deleted.

79 changes: 0 additions & 79 deletions hash/dirhash/dirhash_test.go

This file was deleted.

Empty file removed hash/dirhash/testdata/01/file
Empty file.
Empty file removed hash/dirhash/testdata/01_mode/file
Empty file.
Empty file removed hash/dirhash/testdata/02/dir/file
Empty file.
1 change: 0 additions & 1 deletion hash/dirhash/testdata/03/symlink

This file was deleted.

Empty file removed hash/dirhash/testdata/04/dir/file
Empty file.
Empty file removed hash/dirhash/testdata/04/file
Empty file.
1 change: 0 additions & 1 deletion hash/dirhash/testdata/04/symlink

This file was deleted.

5 changes: 2 additions & 3 deletions sdk/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"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/container"
"github.com/mesg-foundation/engine/cosmos"
ownershipsdk "github.com/mesg-foundation/engine/sdk/ownership"
servicesdk "github.com/mesg-foundation/engine/sdk/service"
Expand All @@ -23,10 +22,10 @@ type Backend struct {
}

// NewBackend creates a new backend and init the sub-backend modules.
func NewBackend(appFactory *cosmos.AppFactory, c container.Container) *Backend {
func NewBackend(appFactory *cosmos.AppFactory) *Backend {
initDefaultCosmosModules(appFactory)
ownership := ownershipsdk.NewBackend(appFactory)
service := servicesdk.NewBackend(appFactory, c, ownership)
service := servicesdk.NewBackend(appFactory, ownership)
return &Backend{
Service: service,
}
Expand Down
52 changes: 47 additions & 5 deletions sdk/service/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ 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/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/mr-tron/base58"
abci "github.com/tendermint/tendermint/abci/types"
)

const backendName = "service"

// Backend is the service backend.
type Backend struct {
container container.Container
cdc *codec.Codec
storeKey *cosmostypes.KVStoreKey
ownerships *ownershipsdk.Backend
}

// NewBackend returns the backend of the service sdk.
func NewBackend(appFactory *cosmos.AppFactory, c container.Container, ownerships *ownershipsdk.Backend) *Backend {
func NewBackend(appFactory *cosmos.AppFactory, ownerships *ownershipsdk.Backend) *Backend {
backend := &Backend{
container: c,
cdc: appFactory.Cdc(),
storeKey: cosmostypes.NewKVStoreKey(backendName),
ownerships: ownerships,
Expand Down Expand Up @@ -88,7 +88,7 @@ func (s *Backend) querier(request cosmostypes.Request, path []string, req abci.R

// Create creates a new service from definition.
func (s *Backend) Create(request cosmostypes.Request, msg *msgCreateService) (*service.Service, error) {
return create(s.container, s.db(request), msg.Request, msg.Owner, s.ownerships, request)
return create(s.db(request), msg.Request, msg.Owner, s.ownerships, request)
}

// Delete deletes the service by hash.
Expand All @@ -105,3 +105,45 @@ func (s *Backend) Get(request cosmostypes.Request, hash hash.Hash) (*service.Ser
func (s *Backend) List(request cosmostypes.Request) ([]*service.Service, error) {
return s.db(request).All()
}

func create(db *database.ServiceDB, req *api.CreateServiceRequest, owner cosmostypes.AccAddress, ownerships *ownershipsdk.Backend, request cosmostypes.Request) (*service.Service, error) {
// 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)

// check if service already exists.
if _, err := db.Get(srv.Hash); err == nil {
return nil, &AlreadyExistsError{Hash: 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 = "_" + base58.Encode(srv.Hash) // TODO: use string method after change type to hash.Hash
}

if err := validator.ValidateService(srv); err != nil {
return nil, err
}

if _, err := ownerships.CreateServiceOwnership(request, srv.Hash, owner); err != nil {
return nil, err
}

if err := db.Save(srv); err != nil {
return nil, err
}
return srv, nil
}
88 changes: 0 additions & 88 deletions sdk/service/utils.go

This file was deleted.

0 comments on commit 97a80d8

Please sign in to comment.