Skip to content
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 ownership SDK and API #1368

Merged
merged 6 commits into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Dockerfile.tools
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ FROM golang:1.13.0-stretch

WORKDIR /project

# install dependencies
COPY go.mod go.sum ./
RUN go mod download

VOLUME /project

# install some dependencies from apt-get.
Expand Down Expand Up @@ -38,6 +34,10 @@ RUN mkdir -p /usr/local/include/gogo/protobuf/ && \
wget -qO- https://github.com/gogo/protobuf/archive/v${gogoprotobuf}.tar.gz | tar -xzf - protobuf-${gogoprotobuf}/gogoproto/gogo.proto && \
mv protobuf-${gogoprotobuf}/gogoproto /usr/local/include/gogo/protobuf/

# install dependencies
COPY go.mod go.sum ./
RUN go mod download

RUN go install github.com/go-bindata/go-bindata/go-bindata
RUN go install github.com/golang/protobuf/protoc-gen-go
RUN go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc
Expand Down
74 changes: 74 additions & 0 deletions database/ownership_db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package database

import (
"errors"
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/mesg-foundation/engine/database/store"
"github.com/mesg-foundation/engine/hash"
"github.com/mesg-foundation/engine/ownership"
)

var (
errCannotSaveOwnershipWithoutHash = errors.New("database: can't save ownership without hash")
)

// OwnershipDB is a database for storing ownership definition.
type OwnershipDB struct {
s store.Store
cdc *codec.Codec
}

// NewOwnershipDB returns the database which is located under given path.
func NewOwnershipDB(s store.Store, cdc *codec.Codec) *OwnershipDB {
return &OwnershipDB{
s: s,
cdc: cdc,
}
}

// unmarshal returns the ownership from byte slice.
func (d *OwnershipDB) unmarshalOwnership(hash hash.Hash, value []byte) (*ownership.Ownership, error) {
var s ownership.Ownership
if err := d.cdc.UnmarshalBinaryBare(value, &s); err != nil {
return nil, fmt.Errorf("database: could not decode ownership %q: %w", hash.String(), err)
}
return &s, nil
}

// All returns every ownership in database.
func (d *OwnershipDB) All() ([]*ownership.Ownership, error) {
var (
ownerships []*ownership.Ownership
iter = d.s.NewIterator()
)
for iter.Next() {
hash := hash.Hash(iter.Key())
s, err := d.unmarshalOwnership(hash, iter.Value())
if err != nil {
return nil, err
}
ownerships = append(ownerships, s)
}
iter.Release()
return ownerships, iter.Error()
}

// Save stores ownership in database.
// If there is an another ownership that uses the same sid, it'll be deleted.
func (d *OwnershipDB) Save(o *ownership.Ownership) error {
if o.Hash.IsZero() {
return errCannotSaveOwnershipWithoutHash
}
b, err := d.cdc.MarshalBinaryBare(o)
if err != nil {
return err
}
return d.s.Put(o.Hash, b)
}

// Close closes database.
func (d *OwnershipDB) Close() error {
return d.s.Close()
}
54 changes: 54 additions & 0 deletions database/ownership_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package database

import (
"io/ioutil"
"os"
"testing"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/mesg-foundation/engine/database/store"
"github.com/mesg-foundation/engine/hash"
"github.com/mesg-foundation/engine/ownership"
"github.com/stretchr/testify/require"
)

func TestOwnershipDB(t *testing.T) {
cdc := codec.New()
ownership.RegisterCodec(cdc)

dir, _ := ioutil.TempDir("", "ownership.db.test")
defer os.Remove(dir)

store, err := store.NewLevelDBStore(dir)
require.NoError(t, err)
db := NewOwnershipDB(store, cdc)
defer db.Close()

p := &ownership.Ownership{
Hash: hash.Int(1),
Owner: "alice",
Resource: &ownership.Ownership_ServiceHash{
ServiceHash: hash.Int(2),
},
}

p2 := &ownership.Ownership{
Hash: hash.Int(2),
Owner: "bob",
Resource: &ownership.Ownership_ServiceHash{
ServiceHash: hash.Int(4),
},
}

t.Run("save", func(t *testing.T) {
require.NoError(t, db.Save(p))
require.NoError(t, db.Save(p2))
})
t.Run("all", func(t *testing.T) {
ps, err := db.All()
require.NoError(t, err)
require.Len(t, ps, 2)
require.True(t, p.Equal(ps[0]))
require.True(t, p2.Equal(ps[1]))
})
}
7 changes: 6 additions & 1 deletion database/process_db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package database

import (
"errors"
"fmt"

"github.com/gogo/protobuf/proto"
Expand All @@ -10,6 +11,10 @@ import (
"github.com/syndtr/goleveldb/leveldb"
)

var (
errCannotSaveProcessWithoutHash = errors.New("database: can't save process without hash")
)

// ProcessDB describes the API of database package.
type ProcessDB interface {
// Save saves a process to database.
Expand Down Expand Up @@ -108,7 +113,7 @@ func (d *LevelDBProcessDB) Get(hash hash.Hash) (*process.Process, error) {
// If there is an another process that uses the same sid, it'll be deleted.
func (d *LevelDBProcessDB) Save(s *process.Process) error {
if s.Hash.IsZero() {
return errCannotSaveWithoutHash
return errCannotSaveProcessWithoutHash
}

b, err := d.marshal(s)
Expand Down
6 changes: 3 additions & 3 deletions database/service_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

var (
errCannotSaveWithoutHash = errors.New("database: can't save service without hash")
errCannotSaveServiceWithoutHash = errors.New("database: can't save service without hash")
)

// ServiceDB is a database for storing service definition.
Expand All @@ -33,7 +33,7 @@ func NewServiceDB(s store.Store, cdc *codec.Codec) *ServiceDB {
func (d *ServiceDB) unmarshalService(hash hash.Hash, value []byte) (*service.Service, error) {
var s service.Service
if err := d.cdc.UnmarshalBinaryBare(value, &s); err != nil {
return nil, fmt.Errorf("database: could not decode service %q: %s", hash, err)
return nil, fmt.Errorf("database: could not decode service %q: %w", hash.String(), err)
}
return &s, nil
}
Expand Down Expand Up @@ -82,7 +82,7 @@ func (d *ServiceDB) Get(hash hash.Hash) (*service.Service, error) {
// If there is an another service that uses the same sid, it'll be deleted.
func (d *ServiceDB) Save(s *service.Service) error {
if len(s.Hash) == 0 {
return errCannotSaveWithoutHash
return errCannotSaveServiceWithoutHash
}
b, err := d.cdc.MarshalBinaryBare(s)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion database/service_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestServiceDBSave(t *testing.T) {
require.Len(t, ss, 2)

// test service without hash.
require.EqualError(t, db.Save(&service.Service{}), errCannotSaveWithoutHash.Error())
require.EqualError(t, db.Save(&service.Service{}), errCannotSaveServiceWithoutHash.Error())
}

func TestServiceDBGet(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions ownership/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ownership

import (
"github.com/cosmos/cosmos-sdk/codec"
)

// RegisterCodec registers the ownership types to codec.
func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterInterface((*isOwnership_Resource)(nil), nil)
cdc.RegisterConcrete(&Ownership_ServiceHash{}, "mesg.types.Ownership.Ownership_ServiceHash", nil)
}
Loading