Skip to content

Commit

Permalink
feat: implement dataspace creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Mar 1, 2022
1 parent 7ee947e commit 8ec1074
Show file tree
Hide file tree
Showing 25 changed files with 1,247 additions and 66 deletions.
13 changes: 13 additions & 0 deletions proto/knowledge/dataspace.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

package okp4.okp4d.knowledge;

option go_package = "github.com/okp4/okp4d/x/knowledge/types";

// Dataspace describes the Dataspace entity in the keeper.
message Dataspace {
// id is the unique identifier of the dataspace.
string id = 1;
// name is the (short) name, human readable, of the dataspace.
string name = 2;
}
3 changes: 1 addition & 2 deletions proto/knowledge/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ option go_package = "github.com/okp4/okp4d/x/knowledge/types";
// Params defines the parameters for the module.
message Params {
option (gogoproto.goproto_stringer) = false;

}
}
1 change: 1 addition & 0 deletions proto/knowledge/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "knowledge/params.proto";

// this line is used by starport scaffolding # 1

option go_package = "github.com/okp4/okp4d/x/knowledge/types";
Expand Down
22 changes: 20 additions & 2 deletions proto/knowledge/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,25 @@ option go_package = "github.com/okp4/okp4d/x/knowledge/types";

// Msg defines the Msg service.
service Msg {
// this line is used by starport scaffolding # proto/tx/rpc
// BangDataspace defines a method for creating a new Dataspace in the knowledge space.
rpc BangDataspace(MsgBangDataspace) returns (MsgBangDataspaceResponse);
// this line is used by starport scaffolding # proto/tx/rpc
}

// this line is used by starport scaffolding # proto/tx/message
// MsgBangDataspace represents a message to create a new dataspace in the knowledge space..
message MsgBangDataspace {
// creator is the creator of the dataspace (the originator of the request).
string creator = 1;
// id is the unique id of the dataspace.
string id = 2;
// name is the (short) name, human readable, of the dataspace.
string name = 3;
// description is the descirption, human readable, of the dataspace, with markdown support.
string description = 4;
}

// MsgBangDataspaceResponse is the response given after a successful Dataspace creation.
message MsgBangDataspaceResponse {
}

// this line is used by starport scaffolding # proto/tx/message
9 changes: 2 additions & 7 deletions x/knowledge/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@ package cli

import (
"fmt"
// "strings"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
// "github.com/cosmos/cosmos-sdk/client/flags"
// sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/okp4/okp4d/x/knowledge/types"
"github.com/spf13/cobra"
)

// GetQueryCmd returns the cli query commands for this module
// GetQueryCmd returns the cli query commands for this module.
func GetQueryCmd(queryRoute string) *cobra.Command {
// Group knowledge queries under a subcommand
cmd := &cobra.Command{
Expand Down
9 changes: 2 additions & 7 deletions x/knowledge/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@ import (
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
// "github.com/cosmos/cosmos-sdk/client/flags"
"github.com/okp4/okp4d/x/knowledge/types"
)

var (
DefaultRelativePacketTimeoutTimestamp = uint64((time.Duration(10) * time.Minute).Nanoseconds())
)

const (
flagPacketTimeoutTimestamp = "packet-timeout-timestamp"
listSeparator = ","
)

// GetTxCmd returns the transaction commands for this module
// GetTxCmd returns the transaction commands for this module.
func GetTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Expand All @@ -30,6 +24,7 @@ func GetTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdBangDataspace())
// this line is used by starport scaffolding # 1

return cmd
Expand Down
44 changes: 44 additions & 0 deletions x/knowledge/client/cli/tx_bang_dataspace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cli

import (
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/okp4/okp4d/x/knowledge/types"
"github.com/spf13/cobra"
)

var _ = strconv.Itoa(0)

func CmdBangDataspace() *cobra.Command {
cmd := &cobra.Command{
Use: "bang-dataspace [name] [description]",
Short: "Broadcast message bang-dataspace",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argName := args[0]
argDescription := args[1]

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgBangDataspace(
clientCtx.GetFromAddress().String(),
argName,
argDescription,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
7 changes: 5 additions & 2 deletions x/knowledge/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import (

// NewHandler ...
func NewHandler(k keeper.Keeper) sdk.Handler {
// this line is used by starport scaffolding # handler/msgServer
msgServer := keeper.NewMsgServerImpl(k)

return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())

switch msg := msg.(type) {
// this line is used by starport scaffolding # 1
case *types.MsgBangDataspace:
res, err := msgServer.BangDataspace(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
// this line is used by starport scaffolding # 1
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
Expand Down
29 changes: 29 additions & 0 deletions x/knowledge/keeper/dataspace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package keeper

import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/okp4/okp4d/x/knowledge/types"
)

func (k Keeper) SaveDataspace(
ctx sdk.Context,
id string,
name string) (*types.MsgBangDataspaceResponse, error) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.DataspaceKeyPrefix)
key := types.GetDataspaceKey(id)

store.Set(key, []byte(name))

return &types.MsgBangDataspaceResponse{}, nil
}

func (k Keeper) HasDataspace(
ctx sdk.Context,
id string,
) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.DataspaceKeyPrefix)
key := types.GetDataspaceKey(id)

return store.Has(key)
}
4 changes: 1 addition & 3 deletions x/knowledge/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package keeper
import (
"fmt"

"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/okp4/okp4d/x/knowledge/types"
"github.com/tendermint/tendermint/libs/log"
)

type (
Expand Down Expand Up @@ -36,7 +35,6 @@ func NewKeeper(
}

return &Keeper{

cdc: cdc,
storeKey: storeKey,
memKey: memKey,
Expand Down
23 changes: 23 additions & 0 deletions x/knowledge/keeper/msg_server_bang_dataspace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package keeper

import (
"context"
"fmt"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/okp4/okp4d/x/knowledge/types"
)

func (k msgServer) BangDataspace(goCtx context.Context, msg *types.MsgBangDataspace) (*types.MsgBangDataspaceResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

if k.HasDataspace(ctx, msg.Id) {
return nil, sdkerrors.Wrap(types.ErrEntityAlreadyExists, fmt.Sprintf("dataspace %s", msg.Id))
}

k.SaveDataspace(ctx, msg.Id, msg.Name)

return &types.MsgBangDataspaceResponse{}, nil
}
4 changes: 2 additions & 2 deletions x/knowledge/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"github.com/okp4/okp4d/x/knowledge/types"
)

// GetParams get all parameters as types.Params
// GetParams get all parameters as types.Params.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams()
}

// SetParams set the params
// SetParams set the params.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramstore.SetParamSet(ctx, &params)
}
3 changes: 2 additions & 1 deletion x/knowledge/module.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package knowledge

// nolint:goimports
import (
"encoding/json"
"fmt"
Expand Down Expand Up @@ -52,7 +53,7 @@ func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
types.RegisterCodec(cdc)
}

// RegisterInterfaces registers the module's interface types
// RegisterInterfaces registers the module's interface types.
func (a AppModuleBasic) RegisterInterfaces(reg cdctypes.InterfaceRegistry) {
types.RegisterInterfaces(reg)
}
Expand Down
29 changes: 22 additions & 7 deletions x/knowledge/module_simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/okp4/okp4d/x/knowledge/types"
)

// avoid unused import issue
// avoid unused import issue.
var (
_ = sample.AccAddress
_ = knowledgesimulation.FindAccount
Expand All @@ -24,10 +24,15 @@ var (
)

const (
// this line is used by starport scaffolding # simapp/module/const
//nolint:gosec
opWeightMsgBangDataspace = "op_weight_msg_create_chain"
// TODO: Determine the simulation weight value
defaultWeightMsgBangDataspace int = 100

// this line is used by starport scaffolding # simapp/module/const
)

// GenerateGenesisState creates a randomized GenState of the module
// GenerateGenesisState creates a randomized GenState of the module.
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
accs := make([]string, len(simState.Accounts))
for i, acc := range simState.Accounts {
Expand All @@ -39,24 +44,34 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&knowledgeGenesis)
}

// ProposalContents doesn't return any content functions for governance proposals
// ProposalContents doesn't return any content functions for governance proposals.
func (AppModule) ProposalContents(_ module.SimulationState) []simtypes.WeightedProposalContent {
return nil
}

// RandomizedParams creates randomized param changes for the simulator
// RandomizedParams creates randomized param changes for the simulator.
func (am AppModule) RandomizedParams(_ *rand.Rand) []simtypes.ParamChange {

return []simtypes.ParamChange{}
}

// RegisterStoreDecoder registers a decoder
// RegisterStoreDecoder registers a decoder.
func (am AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {}

// WeightedOperations returns the all the gov module operations with their respective weights.
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
operations := make([]simtypes.WeightedOperation, 0)

var weightMsgBangDataspace int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgBangDataspace, &weightMsgBangDataspace, nil,
func(_ *rand.Rand) {
weightMsgBangDataspace = defaultWeightMsgBangDataspace
},
)
operations = append(operations, simulation.NewWeightedOperation(
weightMsgBangDataspace,
knowledgesimulation.SimulateMsgBangDataspace(am.accountKeeper, am.bankKeeper, am.keeper),
))

// this line is used by starport scaffolding # simapp/module/operation

return operations
Expand Down
29 changes: 29 additions & 0 deletions x/knowledge/simulation/bang_dataspace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package simulation

import (
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/okp4/okp4d/x/knowledge/keeper"
"github.com/okp4/okp4d/x/knowledge/types"
)

func SimulateMsgBangDataspace(
ak types.AccountKeeper,
bk types.BankKeeper,
k keeper.Keeper,
) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
simAccount, _ := simtypes.RandomAcc(r, accs)
msg := &types.MsgBangDataspace{
Creator: simAccount.Address.String(),
}

// TODO: Handling the BangDataspace simulation

return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "BangDataspace simulation not implemented"), nil, nil
}
}
2 changes: 1 addition & 1 deletion x/knowledge/simulation/simap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
)

// FindAccount find a specific address from an account list
// FindAccount find a specific address from an account list.
func FindAccount(accs []simtypes.Account, address string) (simtypes.Account, bool) {
creator, err := sdk.AccAddressFromBech32(address)
if err != nil {
Expand Down
Loading

0 comments on commit 8ec1074

Please sign in to comment.