From 1fa7dc0f5b8d4b894fcbc08d8d52a814ffe6be4b Mon Sep 17 00:00:00 2001 From: krhubert Date: Tue, 11 Feb 2020 11:31:39 +0100 Subject: [PATCH 1/5] Implement list rest query for ownership --- x/ownership/client/rest/query.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/ownership/client/rest/query.go b/x/ownership/client/rest/query.go index 5c2165d05..351acd8b7 100644 --- a/x/ownership/client/rest/query.go +++ b/x/ownership/client/rest/query.go @@ -29,7 +29,6 @@ func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryListOwnerships) - res, height, err := cliCtx.QueryWithData(route, nil) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) From e42ed83800fa91735fe5de405a0fe20472d72b8f Mon Sep 17 00:00:00 2001 From: krhubert Date: Tue, 11 Feb 2020 18:26:34 +0100 Subject: [PATCH 2/5] Use template to move instance cosmos module to x/ dir --- app/app.go | 14 +- sdk/event/event.go | 26 ++-- sdk/execution/keeper.go | 6 +- sdk/execution/sdk.go | 15 +- sdk/instance/keeper.go | 80 ---------- sdk/instance/module.go | 56 ------- sdk/instance/sdk.go | 48 ------ sdk/process/keeper.go | 6 +- sdk/runner/keeper.go | 6 +- sdk/runner/sdk.go | 16 +- sdk/sdk.go | 10 +- server/grpc/api/instance.go | 30 +++- server/grpc/server.go | 2 +- x/instance/abci.go | 13 ++ x/instance/alias.go | 36 +++++ x/instance/client/cli/query.go | 72 +++++++++ x/instance/client/cli/tx.go | 25 ++++ x/instance/client/rest/query.go | 46 ++++++ x/instance/client/rest/rest.go | 12 ++ x/instance/client/rest/tx.go | 8 + x/instance/genesis.go | 18 +++ x/instance/handler.go | 17 +++ x/instance/internal/keeper/keeper.go | 88 +++++++++++ x/instance/internal/keeper/querier.go | 62 ++++++++ x/instance/internal/types/codec.go | 18 +++ x/instance/internal/types/expected_keepers.go | 14 ++ x/instance/internal/types/genesis.go | 19 +++ x/instance/internal/types/key.go | 15 ++ x/instance/internal/types/params.go | 38 +++++ x/instance/internal/types/querier.go | 8 + x/instance/module.go | 139 ++++++++++++++++++ 31 files changed, 718 insertions(+), 245 deletions(-) delete mode 100644 sdk/instance/keeper.go delete mode 100644 sdk/instance/module.go delete mode 100644 sdk/instance/sdk.go create mode 100644 x/instance/abci.go create mode 100644 x/instance/alias.go create mode 100644 x/instance/client/cli/query.go create mode 100644 x/instance/client/cli/tx.go create mode 100644 x/instance/client/rest/query.go create mode 100644 x/instance/client/rest/rest.go create mode 100644 x/instance/client/rest/tx.go create mode 100644 x/instance/genesis.go create mode 100644 x/instance/handler.go create mode 100644 x/instance/internal/keeper/keeper.go create mode 100644 x/instance/internal/keeper/querier.go create mode 100644 x/instance/internal/types/codec.go create mode 100644 x/instance/internal/types/expected_keepers.go create mode 100644 x/instance/internal/types/genesis.go create mode 100644 x/instance/internal/types/key.go create mode 100644 x/instance/internal/types/params.go create mode 100644 x/instance/internal/types/querier.go create mode 100644 x/instance/module.go diff --git a/app/app.go b/app/app.go index 1c74b7fcc..81e739819 100644 --- a/app/app.go +++ b/app/app.go @@ -29,10 +29,10 @@ import ( mesgcodec "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/cosmos" executionsdk "github.com/mesg-foundation/engine/sdk/execution" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" processsdk "github.com/mesg-foundation/engine/sdk/process" runnersdk "github.com/mesg-foundation/engine/sdk/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" + "github.com/mesg-foundation/engine/x/instance" "github.com/mesg-foundation/engine/x/ownership" ) @@ -60,8 +60,8 @@ var ( // Engine's AppModuleBasic ownership.AppModuleBasic{}, + instance.AppModuleBasic{}, cosmos.NewAppModuleBasic(servicesdk.ModuleName), - cosmos.NewAppModuleBasic(instancesdk.ModuleName), cosmos.NewAppModuleBasic(runnersdk.ModuleName), cosmos.NewAppModuleBasic(executionsdk.ModuleName), cosmos.NewAppModuleBasic(processsdk.ModuleName), @@ -116,8 +116,8 @@ type NewApp struct { // Engine's keepers ownershipKeeper ownership.Keeper + instanceKeeper instance.Keeper serviceKeeper *servicesdk.Keeper - instanceKeeper *instancesdk.Keeper runnerKeeper *runnersdk.Keeper processKeeper *processsdk.Keeper executionKeeper *executionsdk.Keeper @@ -156,8 +156,8 @@ func NewInitApp( // Engine's module keys ownership.ModuleName, + instance.ModuleName, servicesdk.ModuleName, - instancesdk.ModuleName, runnersdk.ModuleName, executionsdk.ModuleName, processsdk.ModuleName, @@ -243,8 +243,8 @@ func NewInitApp( // Engine's module keepers app.ownershipKeeper = ownership.NewKeeper(app.cdc, keys[ownership.StoreKey]) + app.instanceKeeper = instance.NewKeeper(app.cdc, keys[instance.StoreKey]) app.serviceKeeper = servicesdk.NewKeeper(keys[servicesdk.ModuleName], app.ownershipKeeper) - app.instanceKeeper = instancesdk.NewKeeper(keys[instancesdk.ModuleName]) app.runnerKeeper = runnersdk.NewKeeper(keys[runnersdk.ModuleName], app.instanceKeeper) app.processKeeper = processsdk.NewKeeper(keys[processsdk.ModuleName], app.ownershipKeeper, app.instanceKeeper) app.executionKeeper = executionsdk.NewKeeper(keys[executionsdk.ModuleName], app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper) @@ -261,8 +261,8 @@ func NewInitApp( // Engine's modules ownership.NewAppModule(app.ownershipKeeper), + instance.NewAppModule(app.instanceKeeper), servicesdk.NewModule(app.serviceKeeper), - instancesdk.NewModule(app.instanceKeeper), runnersdk.NewModule(app.runnerKeeper), executionsdk.NewModule(app.executionKeeper), processsdk.NewModule(app.processKeeper), @@ -288,8 +288,8 @@ func NewInitApp( // Engine's modules ownership.ModuleName, + instance.ModuleName, servicesdk.ModuleName, - instancesdk.ModuleName, runnersdk.ModuleName, executionsdk.ModuleName, processsdk.ModuleName, diff --git a/sdk/event/event.go b/sdk/event/event.go index 87319697a..17862fad3 100644 --- a/sdk/event/event.go +++ b/sdk/event/event.go @@ -1,12 +1,16 @@ package eventsdk import ( + "fmt" + "github.com/cskr/pubsub" + "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/event" "github.com/mesg-foundation/engine/hash" + instancepb "github.com/mesg-foundation/engine/instance" "github.com/mesg-foundation/engine/protobuf/types" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" servicesdk "github.com/mesg-foundation/engine/sdk/service" + "github.com/mesg-foundation/engine/x/instance" ) const ( @@ -16,17 +20,17 @@ const ( // Event exposes event APIs of MESG. type Event struct { - ps *pubsub.PubSub - instance *instancesdk.SDK - service *servicesdk.SDK + ps *pubsub.PubSub + client *cosmos.Client + service *servicesdk.SDK } // New creates a new Event SDK with given options. -func New(ps *pubsub.PubSub, service *servicesdk.SDK, instance *instancesdk.SDK) *Event { +func New(ps *pubsub.PubSub, service *servicesdk.SDK, client *cosmos.Client) *Event { return &Event{ - ps: ps, - service: service, - instance: instance, + ps: ps, + client: client, + service: service, } } @@ -34,12 +38,12 @@ func New(ps *pubsub.PubSub, service *servicesdk.SDK, instance *instancesdk.SDK) func (e *Event) Create(instanceHash hash.Hash, eventKey string, eventData *types.Struct) (*event.Event, error) { event := event.Create(instanceHash, eventKey, eventData) - instance, err := e.instance.Get(event.InstanceHash) - if err != nil { + var inst instancepb.Instance + if err := e.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", instance.QuerierRoute, instance.QueryGetInstance, event.InstanceHash), nil, &inst); err != nil { return nil, err } - service, err := e.service.Get(instance.ServiceHash) + service, err := e.service.Get(inst.ServiceHash) if err != nil { return nil, err } diff --git a/sdk/execution/keeper.go b/sdk/execution/keeper.go index f27b9f986..feba6d8ee 100644 --- a/sdk/execution/keeper.go +++ b/sdk/execution/keeper.go @@ -10,23 +10,23 @@ import ( "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/protobuf/types" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" processsdk "github.com/mesg-foundation/engine/sdk/process" runnersdk "github.com/mesg-foundation/engine/sdk/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" + "github.com/mesg-foundation/engine/x/instance" ) // Keeper holds the logic to read and write data. type Keeper struct { storeKey *cosmostypes.KVStoreKey serviceKeeper *servicesdk.Keeper - instanceKeeper *instancesdk.Keeper + instanceKeeper instance.Keeper runnerKeeper *runnersdk.Keeper processKeeper *processsdk.Keeper } // NewKeeper initialize a new keeper. -func NewKeeper(storeKey *cosmostypes.KVStoreKey, serviceKeeper *servicesdk.Keeper, instanceKeeper *instancesdk.Keeper, runnerKeeper *runnersdk.Keeper, processKeeper *processsdk.Keeper) *Keeper { +func NewKeeper(storeKey *cosmostypes.KVStoreKey, serviceKeeper *servicesdk.Keeper, instanceKeeper instance.Keeper, runnerKeeper *runnersdk.Keeper, processKeeper *processsdk.Keeper) *Keeper { return &Keeper{ storeKey: storeKey, serviceKeeper: serviceKeeper, diff --git a/sdk/execution/sdk.go b/sdk/execution/sdk.go index 111f76930..6d657ee28 100644 --- a/sdk/execution/sdk.go +++ b/sdk/execution/sdk.go @@ -7,7 +7,6 @@ import ( "github.com/mesg-foundation/engine/execution" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/protobuf/api" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" runnersdk "github.com/mesg-foundation/engine/sdk/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" ) @@ -16,18 +15,16 @@ import ( type SDK struct { client *cosmos.Client - serviceSDK *servicesdk.SDK - instanceSDK *instancesdk.SDK - runnerSDK *runnersdk.SDK + serviceSDK *servicesdk.SDK + runnerSDK *runnersdk.SDK } // New returns the execution sdk. -func New(client *cosmos.Client, serviceSDK *servicesdk.SDK, instanceSDK *instancesdk.SDK, runnerSDK *runnersdk.SDK) *SDK { +func New(client *cosmos.Client, serviceSDK *servicesdk.SDK, runnerSDK *runnersdk.SDK) *SDK { sdk := &SDK{ - client: client, - serviceSDK: serviceSDK, - instanceSDK: instanceSDK, - runnerSDK: runnerSDK, + client: client, + serviceSDK: serviceSDK, + runnerSDK: runnerSDK, } return sdk } diff --git a/sdk/instance/keeper.go b/sdk/instance/keeper.go deleted file mode 100644 index d59c8573a..000000000 --- a/sdk/instance/keeper.go +++ /dev/null @@ -1,80 +0,0 @@ -package instancesdk - -import ( - "fmt" - - cosmostypes "github.com/cosmos/cosmos-sdk/types" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/instance" - "github.com/mesg-foundation/engine/protobuf/api" -) - -// Keeper holds the logic to read and write data. -type Keeper struct { - storeKey *cosmostypes.KVStoreKey -} - -// NewKeeper initialize a new keeper. -func NewKeeper(storeKey *cosmostypes.KVStoreKey) *Keeper { - return &Keeper{ - storeKey: storeKey, - } -} - -// FetchOrCreate creates a new instance if needed. -func (k *Keeper) FetchOrCreate(request cosmostypes.Request, serviceHash hash.Hash, envHash hash.Hash) (*instance.Instance, error) { - inst := &instance.Instance{ - ServiceHash: serviceHash, - EnvHash: envHash, - } - inst.Hash = hash.Dump(inst) - - if store := request.KVStore(k.storeKey); !store.Has(inst.Hash) { - value, err := codec.MarshalBinaryBare(inst) - if err != nil { - return nil, err - } - store.Set(inst.Hash, value) - } - - return inst, nil -} - -// Get returns the instance that matches given hash. -func (k *Keeper) Get(request cosmostypes.Request, hash hash.Hash) (*instance.Instance, error) { - var i *instance.Instance - store := request.KVStore(k.storeKey) - if !store.Has(hash) { - return nil, fmt.Errorf("instance %q not found", hash) - } - value := store.Get(hash) - return i, codec.UnmarshalBinaryBare(value, &i) -} - -// Exists returns true if a specific set of data exists in the database, false otherwise -func (k *Keeper) Exists(request cosmostypes.Request, hash hash.Hash) (bool, error) { - return request.KVStore(k.storeKey).Has(hash), nil -} - -// List returns all instances. -func (k *Keeper) List(request cosmostypes.Request, f *api.ListInstanceRequest_Filter) ([]*instance.Instance, error) { - var ( - instances []*instance.Instance - iter = request.KVStore(k.storeKey).Iterator(nil, nil) - ) - - // filter results - for iter.Valid() { - var i *instance.Instance - if err := codec.UnmarshalBinaryBare(iter.Value(), &i); err != nil { - return nil, err - } - if f == nil || f.ServiceHash.IsZero() || i.ServiceHash.Equal(f.ServiceHash) { - instances = append(instances, i) - } - iter.Next() - } - iter.Close() - return instances, nil -} diff --git a/sdk/instance/module.go b/sdk/instance/module.go deleted file mode 100644 index db691c72f..000000000 --- a/sdk/instance/module.go +++ /dev/null @@ -1,56 +0,0 @@ -package instancesdk - -import ( - "fmt" - - cosmostypes "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/cosmos-sdk/types/module" - "github.com/mesg-foundation/engine/codec" - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/protobuf/api" - abci "github.com/tendermint/tendermint/abci/types" -) - -// ModuleName is the name of this module. -const ModuleName = "instance" - -// NewModule returns the module of this sdk. -func NewModule(k *Keeper) module.AppModule { - return cosmos.NewAppModule(cosmos.NewAppModuleBasic(ModuleName), handler(k), querier(k)) -} - -func handler(k *Keeper) cosmos.Handler { - return func(request cosmostypes.Request, msg cosmostypes.Msg) (hash.Hash, error) { - errmsg := fmt.Sprintf("Unrecognized instance Msg type: %v", msg.Type()) - return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errmsg) - } -} - -func querier(k *Keeper) cosmos.Querier { - return func(request cosmostypes.Request, path []string, req abci.RequestQuery) (res interface{}, err error) { - switch path[0] { - case "get": - hash, err := hash.Decode(path[1]) - if err != nil { - return nil, err - } - return k.Get(request, hash) - case "list": - var f api.ListInstanceRequest_Filter - if err := codec.UnmarshalBinaryBare(req.Data, &f); err != nil { - return nil, err - } - return k.List(request, &f) - case "exists": - hash, err := hash.Decode(path[1]) - if err != nil { - return nil, err - } - return k.Exists(request, hash) - default: - return nil, fmt.Errorf("unknown instance query endpoint %s", path[0]) - } - } -} diff --git a/sdk/instance/sdk.go b/sdk/instance/sdk.go deleted file mode 100644 index 9400e518d..000000000 --- a/sdk/instance/sdk.go +++ /dev/null @@ -1,48 +0,0 @@ -package instancesdk - -import ( - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/instance" - "github.com/mesg-foundation/engine/protobuf/api" -) - -// SDK is the instance sdk. -type SDK struct { - client *cosmos.Client -} - -// New returns the instance sdk. -func New(client *cosmos.Client) *SDK { - sdk := &SDK{ - client: client, - } - return 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/"+ModuleName+"/get/"+hash.String(), nil, &instance); err != nil { - return nil, err - } - return &instance, nil -} - -// 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/"+ModuleName+"/list", f, &instances); err != nil { - return nil, err - } - return instances, nil -} - -// 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/"+ModuleName+"/exists/"+hash.String(), nil, &exists); err != nil { - return false, err - } - return exists, nil -} diff --git a/sdk/process/keeper.go b/sdk/process/keeper.go index c4a51aa19..726ef201b 100644 --- a/sdk/process/keeper.go +++ b/sdk/process/keeper.go @@ -8,7 +8,7 @@ import ( "github.com/mesg-foundation/engine/hash" ownershippb "github.com/mesg-foundation/engine/ownership" "github.com/mesg-foundation/engine/process" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" + "github.com/mesg-foundation/engine/x/instance" "github.com/mesg-foundation/engine/x/ownership" ) @@ -16,11 +16,11 @@ import ( type Keeper struct { storeKey *cosmostypes.KVStoreKey ownershipKeeper ownership.Keeper - instanceKeeper *instancesdk.Keeper + instanceKeeper instance.Keeper } // NewKeeper returns the keeper of the service sdk. -func NewKeeper(storeKey *cosmostypes.KVStoreKey, ownershipKeeper ownership.Keeper, instanceKeeper *instancesdk.Keeper) *Keeper { +func NewKeeper(storeKey *cosmostypes.KVStoreKey, ownershipKeeper ownership.Keeper, instanceKeeper instance.Keeper) *Keeper { return &Keeper{ storeKey: storeKey, ownershipKeeper: ownershipKeeper, diff --git a/sdk/runner/keeper.go b/sdk/runner/keeper.go index 4ba932e32..21a2d5be3 100644 --- a/sdk/runner/keeper.go +++ b/sdk/runner/keeper.go @@ -8,17 +8,17 @@ import ( "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/runner" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" + "github.com/mesg-foundation/engine/x/instance" ) // Keeper holds the logic to read and write data. type Keeper struct { storeKey *cosmostypes.KVStoreKey - instanceKeeper *instancesdk.Keeper + instanceKeeper instance.Keeper } // NewKeeper initialize a new keeper. -func NewKeeper(storeKey *cosmostypes.KVStoreKey, instanceKeeper *instancesdk.Keeper) *Keeper { +func NewKeeper(storeKey *cosmostypes.KVStoreKey, instanceKeeper instance.Keeper) *Keeper { return &Keeper{ storeKey: storeKey, instanceKeeper: instanceKeeper, diff --git a/sdk/runner/sdk.go b/sdk/runner/sdk.go index 35dcd4e91..fe301699b 100644 --- a/sdk/runner/sdk.go +++ b/sdk/runner/sdk.go @@ -8,17 +8,16 @@ import ( "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/ext/xos" "github.com/mesg-foundation/engine/hash" - "github.com/mesg-foundation/engine/instance" + instancepb "github.com/mesg-foundation/engine/instance" "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/runner" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" servicesdk "github.com/mesg-foundation/engine/sdk/service" + "github.com/mesg-foundation/engine/x/instance" ) // SDK is the runner sdk. type SDK struct { serviceSDK *servicesdk.SDK - instanceSDK *instancesdk.SDK client *cosmos.Client container container.Container port string @@ -33,11 +32,10 @@ type Filter struct { } // New returns the runner sdk. -func New(client *cosmos.Client, serviceSDK *servicesdk.SDK, instanceSDK *instancesdk.SDK, container container.Container, engineName, port, ipfsEndpoint string) *SDK { +func New(client *cosmos.Client, serviceSDK *servicesdk.SDK, container container.Container, engineName, port, ipfsEndpoint string) *SDK { sdk := &SDK{ container: container, serviceSDK: serviceSDK, - instanceSDK: instanceSDK, client: client, port: port, engineName: engineName, @@ -57,7 +55,7 @@ func (s *SDK) Create(req *api.CreateRunnerRequest) (*runner.Runner, error) { instanceEnv := xos.EnvMergeSlices(srv.Configuration.Env, req.Env) envHash := hash.Dump(instanceEnv) // TODO: should be done by instance or runner - instanceHash := hash.Dump(&instance.Instance{ + instanceHash := hash.Dump(&instancepb.Instance{ ServiceHash: srv.Hash, EnvHash: envHash, }) @@ -122,11 +120,11 @@ func (s *SDK) Delete(req *api.DeleteRunnerRequest) error { return err } - // get instance and service - inst, err := s.instanceSDK.Get(runner.InstanceHash) - if err != nil { + var inst instancepb.Instance + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s/%s", instance.QuerierRoute, instance.QueryGetInstance, runner.InstanceHash), nil, &inst); err != nil { return err } + srv, err := s.serviceSDK.Get(inst.ServiceHash) if err != nil { return err diff --git a/sdk/sdk.go b/sdk/sdk.go index c1f511d83..5ddbdbd4d 100644 --- a/sdk/sdk.go +++ b/sdk/sdk.go @@ -6,7 +6,6 @@ import ( "github.com/mesg-foundation/engine/cosmos" eventsdk "github.com/mesg-foundation/engine/sdk/event" executionsdk "github.com/mesg-foundation/engine/sdk/execution" - instancesdk "github.com/mesg-foundation/engine/sdk/instance" processesdk "github.com/mesg-foundation/engine/sdk/process" runnersdk "github.com/mesg-foundation/engine/sdk/runner" servicesdk "github.com/mesg-foundation/engine/sdk/service" @@ -15,7 +14,6 @@ import ( // SDK exposes all functionalities of MESG Engine. type SDK struct { Service *servicesdk.SDK - Instance *instancesdk.SDK Execution *executionsdk.SDK Event *eventsdk.Event Process *processesdk.SDK @@ -26,14 +24,12 @@ type SDK struct { func New(client *cosmos.Client, kb *cosmos.Keybase, container container.Container, engineName, port, ipfsEndpoint string) *SDK { ps := pubsub.New(0) serviceSDK := servicesdk.New(client) - instanceSDK := instancesdk.New(client) - runnerSDK := runnersdk.New(client, serviceSDK, instanceSDK, container, engineName, port, ipfsEndpoint) + runnerSDK := runnersdk.New(client, serviceSDK, container, engineName, port, ipfsEndpoint) processSDK := processesdk.New(client) - executionSDK := executionsdk.New(client, serviceSDK, instanceSDK, runnerSDK) - eventSDK := eventsdk.New(ps, serviceSDK, instanceSDK) + executionSDK := executionsdk.New(client, serviceSDK, runnerSDK) + eventSDK := eventsdk.New(ps, serviceSDK, client) return &SDK{ Service: serviceSDK, - Instance: instanceSDK, Execution: executionSDK, Event: eventSDK, Process: processSDK, diff --git a/server/grpc/api/instance.go b/server/grpc/api/instance.go index 5c833070d..645e6d6a5 100644 --- a/server/grpc/api/instance.go +++ b/server/grpc/api/instance.go @@ -2,32 +2,46 @@ package api import ( "context" + "fmt" - "github.com/mesg-foundation/engine/instance" + "github.com/mesg-foundation/engine/cosmos" + instancepb "github.com/mesg-foundation/engine/instance" protobuf_api "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/sdk" + "github.com/mesg-foundation/engine/x/instance" ) // InstanceServer is the type to aggregate all Instance APIs. type InstanceServer struct { - sdk *sdk.SDK + sdk *sdk.SDK + client *cosmos.Client } // NewInstanceServer creates a new ServiceServer. -func NewInstanceServer(sdk *sdk.SDK) *InstanceServer { - return &InstanceServer{sdk: sdk} +func NewInstanceServer(sdk *sdk.SDK, client *cosmos.Client) *InstanceServer { + return &InstanceServer{ + sdk: sdk, + client: client, + } } // List instances. func (s *InstanceServer) List(ctx context.Context, request *protobuf_api.ListInstanceRequest) (*protobuf_api.ListInstanceResponse, error) { - instances, err := s.sdk.Instance.List(request.Filter) - if err != nil { + var instances []*instancepb.Instance + if err := s.client.QueryJSON(fmt.Sprintf("custom/%s/%s", instance.QuerierRoute, instance.QueryListInstances), request.Filter, &instances); err != nil { return nil, err } return &protobuf_api.ListInstanceResponse{Instances: instances}, nil } // Get retrives instance. -func (s *InstanceServer) Get(ctx context.Context, request *protobuf_api.GetInstanceRequest) (*instance.Instance, error) { - return s.sdk.Instance.Get(request.Hash) +func (s *InstanceServer) Get(ctx context.Context, request *protobuf_api.GetInstanceRequest) (*instancepb.Instance, error) { + var inst instancepb.Instance + err := s.client.QueryJSON( + fmt.Sprintf("custom/%s/%s/%s", instance.QuerierRoute, instance.QueryGetInstance, request.Hash), + nil, &inst) + if err != nil { + return nil, err + } + return &inst, nil } diff --git a/server/grpc/server.go b/server/grpc/server.go index 847d79db2..d43ab13f6 100644 --- a/server/grpc/server.go +++ b/server/grpc/server.go @@ -83,7 +83,7 @@ func validateInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryS func (s *Server) register() { protobuf_api.RegisterEventServer(s.instance, api.NewEventServer(s.sdk)) protobuf_api.RegisterExecutionServer(s.instance, api.NewExecutionServer(s.sdk)) - protobuf_api.RegisterInstanceServer(s.instance, api.NewInstanceServer(s.sdk)) + protobuf_api.RegisterInstanceServer(s.instance, api.NewInstanceServer(s.sdk, s.client)) protobuf_api.RegisterServiceServer(s.instance, api.NewServiceServer(s.sdk)) protobuf_api.RegisterProcessServer(s.instance, api.NewProcessServer(s.sdk)) protobuf_api.RegisterOwnershipServer(s.instance, api.NewOwnershipServer(s.sdk, s.client)) diff --git a/x/instance/abci.go b/x/instance/abci.go new file mode 100644 index 000000000..5b75cbec2 --- /dev/null +++ b/x/instance/abci.go @@ -0,0 +1,13 @@ +package instance + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// BeginBlocker check for infraction evidence or downtime of validators +// on every begin block +func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k Keeper) {} + +// EndBlocker called every block, process inflation, update validator set. +func EndBlocker(ctx sdk.Context, k Keeper) {} diff --git a/x/instance/alias.go b/x/instance/alias.go new file mode 100644 index 000000000..558da0ee9 --- /dev/null +++ b/x/instance/alias.go @@ -0,0 +1,36 @@ +package instance + +import ( + "github.com/mesg-foundation/engine/x/instance/internal/keeper" + "github.com/mesg-foundation/engine/x/instance/internal/types" +) + +// const aliases +const ( + ModuleName = types.ModuleName + RouterKey = types.RouterKey + StoreKey = types.StoreKey + DefaultParamspace = types.DefaultParamspace + QuerierRoute = types.QuerierRoute +) + +// functions and variable aliases +var ( + NewKeeper = keeper.NewKeeper + NewQuerier = keeper.NewQuerier + RegisterCodec = types.RegisterCodec + NewGenesisState = types.NewGenesisState + DefaultGenesisState = types.DefaultGenesisState + ValidateGenesis = types.ValidateGenesis + + ModuleCdc = types.ModuleCdc + QueryGetInstance = types.QueryGetInstance + QueryListInstances = types.QueryListInstances +) + +// module types +type ( + Keeper = keeper.Keeper + GenesisState = types.GenesisState + Params = types.Params +) diff --git a/x/instance/client/cli/query.go b/x/instance/client/cli/query.go new file mode 100644 index 000000000..507a8f152 --- /dev/null +++ b/x/instance/client/cli/query.go @@ -0,0 +1,72 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/mesg-foundation/engine/instance" + "github.com/mesg-foundation/engine/x/instance/internal/types" + "github.com/spf13/cobra" +) + +// GetQueryCmd returns the cli query commands for this module +func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { + // Group instance queries under a subcommand + instanceQueryCmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + instanceQueryCmd.AddCommand( + flags.GetCommands( + GetCmdGetInstance(queryRoute, cdc), + GetCmdListInstances(queryRoute, cdc), + )..., + ) + + return instanceQueryCmd +} + +func GetCmdGetInstance(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "get", + Short: "get", + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetInstance), nil) + if err != nil { + fmt.Printf("could not get instance\n%s\n", err.Error()) + return nil + } + + var out *instance.Instance + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} + +func GetCmdListInstances(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "list", + Short: "list", + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryListInstances), nil) + if err != nil { + fmt.Printf("could not list instances\n%s\n", err.Error()) + return nil + } + + var out []*instance.Instance + cdc.MustUnmarshalJSON(res, &out) + return cliCtx.PrintOutput(out) + }, + } +} diff --git a/x/instance/client/cli/tx.go b/x/instance/client/cli/tx.go new file mode 100644 index 000000000..43d50b641 --- /dev/null +++ b/x/instance/client/cli/tx.go @@ -0,0 +1,25 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/mesg-foundation/engine/x/instance/internal/types" + "github.com/spf13/cobra" +) + +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + ownershipTxCmd := &cobra.Command{ + Use: types.ModuleName, + Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName), + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + ownershipTxCmd.AddCommand(flags.PostCommands()...) + return ownershipTxCmd +} diff --git a/x/instance/client/rest/query.go b/x/instance/client/rest/query.go new file mode 100644 index 000000000..8090daa9b --- /dev/null +++ b/x/instance/client/rest/query.go @@ -0,0 +1,46 @@ +package rest + +import ( + "fmt" + "net/http" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/types/rest" + "github.com/gorilla/mux" + "github.com/mesg-foundation/engine/x/instance/internal/types" +) + +func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { + r.HandleFunc( + "/ownership/get", + queryHandlerFn(cliCtx, types.QueryGetInstance), + ).Methods(http.MethodGet) + r.HandleFunc( + "/ownership/list", + queryHandlerFn(cliCtx, types.QueryListInstances), + ).Methods(http.MethodGet) + r.HandleFunc( + "/instance/parameters", + queryHandlerFn(cliCtx, types.QuerierRoute), + ).Methods("GET") +} + +func queryHandlerFn(cliCtx context.CLIContext, path string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, path) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} diff --git a/x/instance/client/rest/rest.go b/x/instance/client/rest/rest.go new file mode 100644 index 000000000..5a4ce220e --- /dev/null +++ b/x/instance/client/rest/rest.go @@ -0,0 +1,12 @@ +package rest + +import ( + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/gorilla/mux" +) + +// RegisterRoutes registers instance-related REST handlers to a router +func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) { + registerQueryRoutes(cliCtx, r) + registerTxRoutes(cliCtx, r) +} diff --git a/x/instance/client/rest/tx.go b/x/instance/client/rest/tx.go new file mode 100644 index 000000000..14207f21a --- /dev/null +++ b/x/instance/client/rest/tx.go @@ -0,0 +1,8 @@ +package rest + +import ( + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/gorilla/mux" +) + +func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {} diff --git a/x/instance/genesis.go b/x/instance/genesis.go new file mode 100644 index 000000000..798a6b22a --- /dev/null +++ b/x/instance/genesis.go @@ -0,0 +1,18 @@ +package instance + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/mesg-foundation/engine/x/instance/internal/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// InitGenesis initialize default parameters and the keeper's address to pubkey map. +func InitGenesis(ctx sdk.Context, k Keeper, data types.GenesisState) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} + +// ExportGenesis writes the current store values // to a genesis file, +// which can be imported again with InitGenesis. +func ExportGenesis(ctx sdk.Context, k Keeper) types.GenesisState { + return types.NewGenesisState() +} diff --git a/x/instance/handler.go b/x/instance/handler.go new file mode 100644 index 000000000..227f262e3 --- /dev/null +++ b/x/instance/handler.go @@ -0,0 +1,17 @@ +package instance + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/x/instance/internal/types" +) + +// NewHandler creates an sdk.Handler for all the instance type messages +func NewHandler(k Keeper) sdk.Handler { + return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg) + return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg) + } +} diff --git a/x/instance/internal/keeper/keeper.go b/x/instance/internal/keeper/keeper.go new file mode 100644 index 000000000..751a89042 --- /dev/null +++ b/x/instance/internal/keeper/keeper.go @@ -0,0 +1,88 @@ +package keeper + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/instance" + "github.com/mesg-foundation/engine/protobuf/api" + "github.com/mesg-foundation/engine/x/instance/internal/types" + "github.com/tendermint/tendermint/libs/log" +) + +// Keeper of the instance store +type Keeper struct { + storeKey sdk.StoreKey + cdc *codec.Codec +} + +// NewKeeper creates a instance keeper +func NewKeeper(cdc *codec.Codec, key sdk.StoreKey) Keeper { + keeper := Keeper{ + storeKey: key, + cdc: cdc, + } + return keeper +} + +// Logger returns a module-specific logger. +func (k Keeper) Logger(ctx sdk.Context) log.Logger { + return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) +} + +// FetchOrCreate creates a new instance if needed. +func (k Keeper) FetchOrCreate(ctx sdk.Context, serviceHash hash.Hash, envHash hash.Hash) (*instance.Instance, error) { + store := ctx.KVStore(k.storeKey) + + inst := &instance.Instance{ + ServiceHash: serviceHash, + EnvHash: envHash, + } + inst.Hash = hash.Dump(inst) + + if !store.Has(inst.Hash) { + value, err := k.cdc.MarshalBinaryLengthPrefixed(inst) + if err != nil { + return nil, err + } + store.Set(inst.Hash, value) + } + + return inst, nil +} + +// Get returns the instance from the keeper. +func (k Keeper) Get(ctx sdk.Context, instanceHash hash.Hash) (*instance.Instance, error) { + store := ctx.KVStore(k.storeKey) + if !store.Has(instanceHash) { + return nil, fmt.Errorf("instance %q not found", instanceHash) + } + + var item *instance.Instance + if err := k.cdc.UnmarshalBinaryLengthPrefixed(store.Get(instanceHash), &item); err != nil { + return nil, err + } + return item, nil +} + +// List returns instances from the keeper. +func (k Keeper) List(ctx sdk.Context, f *api.ListInstanceRequest_Filter) ([]*instance.Instance, error) { + store := ctx.KVStore(k.storeKey) + iter := store.Iterator(nil, nil) + var items []*instance.Instance + + for iter.Valid() { + var item *instance.Instance + if err := k.cdc.UnmarshalBinaryLengthPrefixed(iter.Value(), &item); err != nil { + return nil, err + } + if f == nil || f.ServiceHash.IsZero() || item.ServiceHash.Equal(f.ServiceHash) { + items = append(items, item) + } + iter.Next() + } + iter.Close() + return items, nil +} diff --git a/x/instance/internal/keeper/querier.go b/x/instance/internal/keeper/querier.go new file mode 100644 index 000000000..ace4b7df2 --- /dev/null +++ b/x/instance/internal/keeper/querier.go @@ -0,0 +1,62 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/protobuf/api" + "github.com/mesg-foundation/engine/x/instance/internal/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// NewQuerier creates a new querier for instance clients. +func NewQuerier(k Keeper) sdk.Querier { + return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) { + switch path[0] { + case types.QueryGetInstance: + return getInstance(ctx, path[1:], k) + case types.QueryListInstances: + return listInstance(ctx, req, k) + default: + return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown instance query endpoint") + } + } +} + +func getInstance(ctx sdk.Context, path []string, k Keeper) ([]byte, error) { + hash, err := hash.Decode(path[0]) + if err != nil { + return nil, err + } + + instance, err := k.Get(ctx, hash) + if err != nil { + return nil, err + } + + res, err := types.ModuleCdc.MarshalJSON(instance) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return res, nil +} + +func listInstance(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) { + var f *api.ListInstanceRequest_Filter + if len(req.Data) > 0 { + if err := types.ModuleCdc.UnmarshalJSON(req.Data, &f); err != nil { + return nil, err + } + } + + instances, err := k.List(ctx, f) + if err != nil { + return nil, err + } + + res, err := types.ModuleCdc.MarshalJSON(instances) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return res, nil +} diff --git a/x/instance/internal/types/codec.go b/x/instance/internal/types/codec.go new file mode 100644 index 000000000..4b5e77ba8 --- /dev/null +++ b/x/instance/internal/types/codec.go @@ -0,0 +1,18 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" +) + +// RegisterCodec registers concrete types on codec +func RegisterCodec(cdc *codec.Codec) {} + +// ModuleCdc defines the module codec +var ModuleCdc *codec.Codec + +func init() { + ModuleCdc = codec.New() + RegisterCodec(ModuleCdc) + codec.RegisterCrypto(ModuleCdc) + ModuleCdc.Seal() +} diff --git a/x/instance/internal/types/expected_keepers.go b/x/instance/internal/types/expected_keepers.go new file mode 100644 index 000000000..23128499d --- /dev/null +++ b/x/instance/internal/types/expected_keepers.go @@ -0,0 +1,14 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/params" +) + +// ParamSubspace defines the expected Subspace interfacace +type ParamSubspace interface { + WithKeyTable(table params.KeyTable) params.Subspace + Get(ctx sdk.Context, key []byte, ptr interface{}) + GetParamSet(ctx sdk.Context, ps params.ParamSet) + SetParamSet(ctx sdk.Context, ps params.ParamSet) +} diff --git a/x/instance/internal/types/genesis.go b/x/instance/internal/types/genesis.go new file mode 100644 index 000000000..bebeec0dc --- /dev/null +++ b/x/instance/internal/types/genesis.go @@ -0,0 +1,19 @@ +package types + +// GenesisState - all instance state that must be provided at genesis +type GenesisState struct{} + +// NewGenesisState creates a new GenesisState object +func NewGenesisState() GenesisState { + return GenesisState{} +} + +// DefaultGenesisState - default GenesisState used by Cosmos Hub +func DefaultGenesisState() GenesisState { + return GenesisState{} +} + +// ValidateGenesis validates the instance genesis parameters +func ValidateGenesis(data GenesisState) error { + return nil +} diff --git a/x/instance/internal/types/key.go b/x/instance/internal/types/key.go new file mode 100644 index 000000000..023017612 --- /dev/null +++ b/x/instance/internal/types/key.go @@ -0,0 +1,15 @@ +package types + +const ( + // ModuleName is the name of the module + ModuleName = "instance" + + // StoreKey to be used when creating the KVStore + StoreKey = ModuleName + + // RouterKey to be used for routing msgs + RouterKey = ModuleName + + // QuerierRoute to be used for routing + QuerierRoute = ModuleName +) diff --git a/x/instance/internal/types/params.go b/x/instance/internal/types/params.go new file mode 100644 index 000000000..4940992b5 --- /dev/null +++ b/x/instance/internal/types/params.go @@ -0,0 +1,38 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/x/params" +) + +// Default parameter namespace +const ( + DefaultParamspace = ModuleName +) + +// ParamKeyTable for instance module +func ParamKeyTable() params.KeyTable { + return params.NewKeyTable().RegisterParamSet(&Params{}) +} + +// Params - used for initializing default parameter for instance at genesis +type Params struct{} + +// NewParams creates a new Params object +func NewParams() Params { + return Params{} +} + +// String implements the stringer interface for Params +func (p Params) String() string { + return "" +} + +// ParamSetPairs - Implements params.ParamSet +func (p *Params) ParamSetPairs() params.ParamSetPairs { + return params.ParamSetPairs{} +} + +// DefaultParams defines the parameters for this module +func DefaultParams() Params { + return NewParams() +} diff --git a/x/instance/internal/types/querier.go b/x/instance/internal/types/querier.go new file mode 100644 index 000000000..88d4c3574 --- /dev/null +++ b/x/instance/internal/types/querier.go @@ -0,0 +1,8 @@ +package types + +// Query endpoints supported by the instance querier +const ( + QueryGetInstance = "get" + QueryExistInstance = "exist" + QueryListInstances = "list" +) diff --git a/x/instance/module.go b/x/instance/module.go new file mode 100644 index 000000000..d371a647a --- /dev/null +++ b/x/instance/module.go @@ -0,0 +1,139 @@ +package instance + +import ( + "encoding/json" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/gorilla/mux" + "github.com/mesg-foundation/engine/x/instance/client/cli" + "github.com/mesg-foundation/engine/x/instance/client/rest" + "github.com/mesg-foundation/engine/x/instance/internal/types" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" +) + +var ( + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} +) + +// AppModuleBasic defines the basic application module used by the instance module. +type AppModuleBasic struct{} + +var _ module.AppModuleBasic = AppModuleBasic{} + +// Name returns the instance module's name. +func (AppModuleBasic) Name() string { + return types.ModuleName +} + +// RegisterCodec registers the instance module's types for the given codec. +func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { + RegisterCodec(cdc) +} + +// DefaultGenesis returns default genesis state as raw bytes for the instance +// module. +func (AppModuleBasic) DefaultGenesis() json.RawMessage { + return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) +} + +// ValidateGenesis performs genesis state validation for the instance module. +func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { + var data GenesisState + err := ModuleCdc.UnmarshalJSON(bz, &data) + if err != nil { + return err + } + return ValidateGenesis(data) +} + +// RegisterRESTRoutes registers the REST routes for the instance module. +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { + rest.RegisterRoutes(ctx, rtr) +} + +// GetTxCmd returns the root tx command for the instance module. +func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { + return nil +} + +// GetQueryCmd returns no root query command for the instance module. +func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { + return cli.GetQueryCmd(StoreKey, cdc) +} + +//____________________________________________________________________________ + +// AppModule implements an application module for the instance module. +type AppModule struct { + AppModuleBasic + + keeper Keeper +} + +// NewAppModule creates a new AppModule object +func NewAppModule(k Keeper) AppModule { + return AppModule{ + AppModuleBasic: AppModuleBasic{}, + keeper: k, + } +} + +// Name returns the instance module's name. +func (AppModule) Name() string { + return ModuleName +} + +// RegisterInvariants registers the instance module invariants. +func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} + +// Route returns the message routing key for the instance module. +func (AppModule) Route() string { + return RouterKey +} + +// NewHandler returns an sdk.Handler for the instance module. +func (am AppModule) NewHandler() sdk.Handler { + return NewHandler(am.keeper) +} + +// QuerierRoute returns the instance module's querier route name. +func (AppModule) QuerierRoute() string { + return QuerierRoute +} + +// NewQuerierHandler returns the instance module sdk.Querier. +func (am AppModule) NewQuerierHandler() sdk.Querier { + return NewQuerier(am.keeper) +} + +// InitGenesis performs genesis initialization for the instance module. It returns +// no validator updates. +func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { + var genesisState GenesisState + ModuleCdc.MustUnmarshalJSON(data, &genesisState) + InitGenesis(ctx, am.keeper, genesisState) + return []abci.ValidatorUpdate{} +} + +// ExportGenesis returns the exported genesis state as raw bytes for the instance +// module. +func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { + gs := ExportGenesis(ctx, am.keeper) + return ModuleCdc.MustMarshalJSON(gs) +} + +// BeginBlock returns the begin blocker for the instance module. +func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { + BeginBlocker(ctx, req, am.keeper) +} + +// EndBlock returns the end blocker for the instance module. It returns no validator +// updates. +func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} From 92ff3ac20486c7fdd1f906593a03c797078042e5 Mon Sep 17 00:00:00 2001 From: krhubert Date: Wed, 12 Feb 2020 09:07:59 +0100 Subject: [PATCH 3/5] Fix rest and cli client for instance moduel --- x/instance/client/cli/query.go | 3 +- x/instance/client/rest/query.go | 56 ++++++++++++++++++++++++---- x/instance/internal/keeper/keeper.go | 4 +- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/x/instance/client/cli/query.go b/x/instance/client/cli/query.go index 507a8f152..2152c26e9 100644 --- a/x/instance/client/cli/query.go +++ b/x/instance/client/cli/query.go @@ -37,9 +37,10 @@ func GetCmdGetInstance(queryRoute string, cdc *codec.Codec) *cobra.Command { return &cobra.Command{ Use: "get", Short: "get", + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) - res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetInstance), nil) + res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", queryRoute, types.QueryGetInstance, args[0]), nil) if err != nil { fmt.Printf("could not get instance\n%s\n", err.Error()) return nil diff --git a/x/instance/client/rest/query.go b/x/instance/client/rest/query.go index 8090daa9b..43437bdf9 100644 --- a/x/instance/client/rest/query.go +++ b/x/instance/client/rest/query.go @@ -12,27 +12,69 @@ import ( func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { r.HandleFunc( - "/ownership/get", - queryHandlerFn(cliCtx, types.QueryGetInstance), + "/instance/get/{hash}", + queryGetHandlerFn(cliCtx), ).Methods(http.MethodGet) r.HandleFunc( - "/ownership/list", - queryHandlerFn(cliCtx, types.QueryListInstances), + "/instance/list", + queryListHandlerFn(cliCtx), ).Methods(http.MethodGet) r.HandleFunc( "/instance/parameters", - queryHandlerFn(cliCtx, types.QuerierRoute), + queryParametersHandlerFn(cliCtx), ).Methods("GET") } -func queryHandlerFn(cliCtx context.CLIContext, path string) http.HandlerFunc { +func queryGetHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryGetInstance, vars["hash"]) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + +func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryListInstances) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + +func queryParametersHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) if !ok { return } - route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, path) + route := fmt.Sprintf("custom/%s/parameters", types.QuerierRoute) res, height, err := cliCtx.QueryWithData(route, nil) if err != nil { diff --git a/x/instance/internal/keeper/keeper.go b/x/instance/internal/keeper/keeper.go index 751a89042..849b1c87b 100644 --- a/x/instance/internal/keeper/keeper.go +++ b/x/instance/internal/keeper/keeper.go @@ -74,12 +74,12 @@ func (k Keeper) List(ctx sdk.Context, f *api.ListInstanceRequest_Filter) ([]*ins var items []*instance.Instance for iter.Valid() { - var item *instance.Instance + var item instance.Instance if err := k.cdc.UnmarshalBinaryLengthPrefixed(iter.Value(), &item); err != nil { return nil, err } if f == nil || f.ServiceHash.IsZero() || item.ServiceHash.Equal(f.ServiceHash) { - items = append(items, item) + items = append(items, &item) } iter.Next() } From 6b84f84aab811e11bd75e244d762a0456ba216b5 Mon Sep 17 00:00:00 2001 From: krhubert Date: Wed, 12 Feb 2020 09:08:50 +0100 Subject: [PATCH 4/5] Remove unused insntace module query type --- x/instance/internal/types/querier.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/instance/internal/types/querier.go b/x/instance/internal/types/querier.go index 88d4c3574..ec91f8b77 100644 --- a/x/instance/internal/types/querier.go +++ b/x/instance/internal/types/querier.go @@ -3,6 +3,5 @@ package types // Query endpoints supported by the instance querier const ( QueryGetInstance = "get" - QueryExistInstance = "exist" QueryListInstances = "list" ) From 0564530244ff4c1de200a493a87024a08a598d7d Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 12 Feb 2020 18:18:58 +0700 Subject: [PATCH 5/5] Put back pointer on instance unmarshal in instance keeper --- x/instance/internal/keeper/keeper.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/instance/internal/keeper/keeper.go b/x/instance/internal/keeper/keeper.go index 849b1c87b..751a89042 100644 --- a/x/instance/internal/keeper/keeper.go +++ b/x/instance/internal/keeper/keeper.go @@ -74,12 +74,12 @@ func (k Keeper) List(ctx sdk.Context, f *api.ListInstanceRequest_Filter) ([]*ins var items []*instance.Instance for iter.Valid() { - var item instance.Instance + var item *instance.Instance if err := k.cdc.UnmarshalBinaryLengthPrefixed(iter.Value(), &item); err != nil { return nil, err } if f == nil || f.ServiceHash.IsZero() || item.ServiceHash.Equal(f.ServiceHash) { - items = append(items, &item) + items = append(items, item) } iter.Next() }