From dfe9ef028c8fd8705bf9887f788b627c0c2a57ca Mon Sep 17 00:00:00 2001 From: h5law Date: Wed, 10 Jan 2024 17:51:08 +0000 Subject: [PATCH 01/22] feat: add genesis ServiceList and genesis validation and tests --- go.mod | 4 ++-- proto/pocket/service/genesis.proto | 2 ++ proto/pocket/service/query.proto | 3 +-- proto/pocket/service/relay.proto | 2 -- proto/pocket/service/tx.proto | 9 ++++++++- x/service/genesis.go | 7 +++++++ x/service/types/errors.go | 4 ++-- x/service/types/genesis.go | 12 +++++++++++- x/service/types/genesis_test.go | 27 ++++++++++++++++++++++++-- x/service/types/key_service.go | 23 ++++++++++++++++++++++ x/service/types/keys.go | 11 +++++++++++ x/service/types/message_add_service.go | 27 +++++++++++++++++++------- 12 files changed, 112 insertions(+), 19 deletions(-) create mode 100644 x/service/types/key_service.go diff --git a/go.mod b/go.mod index 56ed2d4fc..9be97f1d6 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,6 @@ require ( github.com/athanorlabs/go-dleq v0.1.0 github.com/cometbft/cometbft v0.37.2 github.com/cometbft/cometbft-db v0.8.0 - github.com/cosmos/cosmos-proto v1.0.0-beta.2 github.com/cosmos/cosmos-sdk v0.47.3 github.com/cosmos/gogoproto v1.4.11 github.com/cosmos/ibc-go/v7 v7.1.0 @@ -48,7 +47,6 @@ require ( golang.org/x/crypto v0.15.0 golang.org/x/exp v0.0.0-20230905200255-921286631fa9 golang.org/x/sync v0.5.0 - google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb google.golang.org/grpc v1.59.0 gopkg.in/yaml.v2 v2.4.0 ) @@ -92,6 +90,7 @@ require ( github.com/containerd/cgroups v1.1.0 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/cosmos/btcutil v1.0.5 // indirect + github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect github.com/cosmos/go-bip39 v1.0.0 // indirect github.com/cosmos/gogogateway v1.2.0 // indirect github.com/cosmos/iavl v0.20.0 // indirect @@ -286,6 +285,7 @@ require ( google.golang.org/api v0.143.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto v0.0.0-20230913181813-007df8e322eb // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 // indirect google.golang.org/protobuf v1.31.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect diff --git a/proto/pocket/service/genesis.proto b/proto/pocket/service/genesis.proto index 5ca50c9f0..e2f559467 100644 --- a/proto/pocket/service/genesis.proto +++ b/proto/pocket/service/genesis.proto @@ -3,10 +3,12 @@ package pocket.service; import "gogoproto/gogo.proto"; import "pocket/service/params.proto"; +import "pocket/shared/service.proto"; option go_package = "github.com/pokt-network/poktroll/x/service/types"; // GenesisState defines the service module's genesis state. message GenesisState { Params params = 1 [(gogoproto.nullable) = false]; + repeated pocket.shared.Service serviceList = 2 [(gogoproto.nullable) = false]; } diff --git a/proto/pocket/service/query.proto b/proto/pocket/service/query.proto index 4abf2a13e..0ce354465 100644 --- a/proto/pocket/service/query.proto +++ b/proto/pocket/service/query.proto @@ -3,7 +3,6 @@ package pocket.service; import "gogoproto/gogo.proto"; import "google/api/annotations.proto"; -import "cosmos/base/query/v1beta1/pagination.proto"; import "pocket/service/params.proto"; option go_package = "github.com/pokt-network/poktroll/x/service/types"; @@ -23,4 +22,4 @@ message QueryParamsRequest {} message QueryParamsResponse { // params holds all the parameters of this module. Params params = 1 [(gogoproto.nullable) = false]; -} \ No newline at end of file +} diff --git a/proto/pocket/service/relay.proto b/proto/pocket/service/relay.proto index 10a4c9ea5..538f87610 100644 --- a/proto/pocket/service/relay.proto +++ b/proto/pocket/service/relay.proto @@ -1,8 +1,6 @@ syntax = "proto3"; package pocket.service; -import "cosmos_proto/cosmos.proto"; -import "pocket/application/application.proto"; import "pocket/session/session.proto"; option go_package = "github.com/pokt-network/poktroll/x/service/types"; diff --git a/proto/pocket/service/tx.proto b/proto/pocket/service/tx.proto index ab4f72a9a..6c1432888 100644 --- a/proto/pocket/service/tx.proto +++ b/proto/pocket/service/tx.proto @@ -2,6 +2,11 @@ syntax = "proto3"; package pocket.service; +import "cosmos/msg/v1/msg.proto"; +import "cosmos_proto/cosmos.proto"; +import "gogoproto/gogo.proto"; +import "pocket/shared/service.proto"; + option go_package = "github.com/pokt-network/poktroll/x/service/types"; // Msg defines the Msg service. @@ -10,7 +15,9 @@ service Msg { } message MsgAddService { - string address = 1; + option (cosmos.msg.v1.signer) = "supplier_address"; // https://docs.cosmos.network/main/build/building-modules/messages-and-queries + string supplier_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"]; // The Bech32 address of the application using cosmos' ScalarDescriptor to ensure deterministic deterministic encoding using cosmos' ScalarDescriptor to ensure deterministic deterministic encoding + shared.Service service = 2 [(gogoproto.nullable) = false]; // The Service for which the supplier is adding to the network } message MsgAddServiceResponse {} diff --git a/x/service/genesis.go b/x/service/genesis.go index a0c3211ba..3b575385e 100644 --- a/x/service/genesis.go +++ b/x/service/genesis.go @@ -9,6 +9,11 @@ import ( // InitGenesis initializes the module's state from a provided genesis state. func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) { + // Set all the supplier + for _, supplier := range genState.ServiceList { + // TODO(@h5law): Add this method + // k.SetSupplier(ctx, supplier) + } // this line is used by starport scaffolding # genesis/module/init k.SetParams(ctx, genState.Params) } @@ -18,6 +23,8 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { genesis := types.DefaultGenesis() genesis.Params = k.GetParams(ctx) + // TODO(@h5law): Add this method + // genesis.ServiceList = k.GetAllServices(ctx) // this line is used by starport scaffolding # genesis/module/export return genesis diff --git a/x/service/types/errors.go b/x/service/types/errors.go index 12f77b277..cbeb81fc1 100644 --- a/x/service/types/errors.go +++ b/x/service/types/errors.go @@ -6,7 +6,7 @@ import ( sdkerrors "cosmossdk.io/errors" ) -// x/service module sentinel errors var ( - ErrSample = sdkerrors.Register(ModuleName, 1100, "sample error") + ErrServiceDuplicateIndex = sdkerrors.Register(ModuleName, 1, "duplicate index") + ErrServiceInvalidAddress = sdkerrors.Register(ModuleName, 2, "invalid address") ) diff --git a/x/service/types/genesis.go b/x/service/types/genesis.go index 0af9b4416..64073a68a 100644 --- a/x/service/types/genesis.go +++ b/x/service/types/genesis.go @@ -1,7 +1,7 @@ package types import ( -// this line is used by starport scaffolding # genesis/types/import + sharedtypes "github.com/pokt-network/poktroll/x/shared/types" ) // DefaultIndex is the default global index @@ -10,6 +10,7 @@ const DefaultIndex uint64 = 1 // DefaultGenesis returns the default genesis state func DefaultGenesis() *GenesisState { return &GenesisState{ + ServiceList: []sharedtypes.Service{}, // this line is used by starport scaffolding # genesis/types/default Params: DefaultParams(), } @@ -18,6 +19,15 @@ func DefaultGenesis() *GenesisState { // Validate performs basic genesis state validation returning an error upon any // failure. func (gs GenesisState) Validate() error { + // Check for duplicated index in services + serviceIndexMap := make(map[string]struct{}) + for _, service := range gs.ServiceList { + index := string(ServiceKey(service.Id)) + if _, ok := serviceIndexMap[index]; ok { + return ErrServiceDuplicateIndex.Wrapf("duplicated index for service: %s", index) + } + serviceIndexMap[index] = struct{}{} + } // this line is used by starport scaffolding # genesis/types/validate return gs.Params.Validate() diff --git a/x/service/types/genesis_test.go b/x/service/types/genesis_test.go index 77e357f78..c7af5fd1a 100644 --- a/x/service/types/genesis_test.go +++ b/x/service/types/genesis_test.go @@ -6,9 +6,20 @@ import ( "github.com/stretchr/testify/require" "github.com/pokt-network/poktroll/x/service/types" + sharedtypes "github.com/pokt-network/poktroll/x/shared/types" ) func TestGenesisState_Validate(t *testing.T) { + srv1 := &sharedtypes.Service{ + Id: "srv1", + Name: "srv1", + } + + srv2 := &sharedtypes.Service{ + Id: "srv2", + Name: "srv2", + } + tests := []struct { desc string genState *types.GenesisState @@ -20,15 +31,27 @@ func TestGenesisState_Validate(t *testing.T) { valid: true, }, { - desc: "valid genesis state", + desc: "valid genesis state", genState: &types.GenesisState{ - + ServiceList: []sharedtypes.Service{ + *srv1, *srv2, + }, // this line is used by starport scaffolding # types/genesis/validField }, valid: true, }, + { + desc: "invalid - duplicate service ID", + genState: &types.GenesisState{ + ServiceList: []sharedtypes.Service{ + *srv1, *srv1, + }, + }, + valid: false, + }, // this line is used by starport scaffolding # types/genesis/testcase } + for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { err := tc.genState.Validate() diff --git a/x/service/types/key_service.go b/x/service/types/key_service.go new file mode 100644 index 000000000..37fe1ec5b --- /dev/null +++ b/x/service/types/key_service.go @@ -0,0 +1,23 @@ +package types + +import ( + "encoding/binary" +) + +var _ binary.ByteOrder + +const ( + // ServiceKeyPrefix is the prefix to retrieve all Services + ServiceKeyPrefix = "Service/value/" +) + +// ServiceKey returns the store key to retrieve a Service from the index fields +func ServiceKey(serviceID string) []byte { + var key []byte + + serviceIDBytes := []byte(serviceID) + key = append(key, serviceIDBytes...) + key = append(key, []byte("/")...) + + return key +} diff --git a/x/service/types/keys.go b/x/service/types/keys.go index 64199e3da..f84e13d4e 100644 --- a/x/service/types/keys.go +++ b/x/service/types/keys.go @@ -1,5 +1,7 @@ package types +import "bytes" + const ( // ModuleName defines the module name ModuleName = "service" @@ -14,6 +16,15 @@ const ( MemStoreKey = "mem_service" ) +// KeyDelimiter is the delimiter for composite keys. +var KeyDelimiter = []byte("/") + +// KeyPrefix returns the given prefix as a byte slice for use with the KVStore. func KeyPrefix(p string) []byte { return []byte(p) } + +// KeyComposite combines the given keys into a single key for use with KVStore. +func KeyComposite(keys ...[]byte) []byte { + return bytes.Join(keys, KeyDelimiter) +} diff --git a/x/service/types/message_add_service.go b/x/service/types/message_add_service.go index 68a16817f..554bf28fe 100644 --- a/x/service/types/message_add_service.go +++ b/x/service/types/message_add_service.go @@ -1,45 +1,58 @@ package types import ( + sdkerrors "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + + shared "github.com/pokt-network/poktroll/x/shared/types" ) +// TypeMsgAddService is the name of the service message const TypeMsgAddService = "add_service" var _ sdk.Msg = (*MsgAddService)(nil) -func NewMsgAddService(address string) *MsgAddService { +// NewMsgAddService creates a new MsgAddService instance +func NewMsgAddService(address string, serviceID, serviceName string) *MsgAddService { return &MsgAddService{ - Address: address, + SupplierAddress: address, + Service: shared.Service{Id: serviceID, Name: serviceName}, } } +// Route returns the roter key for the message func (msg *MsgAddService) Route() string { return RouterKey } +// Type returns the message type func (msg *MsgAddService) Type() string { return TypeMsgAddService } +// GetSigners returns the signers of the message func (msg *MsgAddService) GetSigners() []sdk.AccAddress { - address, err := sdk.AccAddressFromBech32(msg.Address) + address, err := sdk.AccAddressFromBech32(msg.SupplierAddress) if err != nil { panic(err) } return []sdk.AccAddress{address} } +// GetSignBytes returns the signable bytes of the message func (msg *MsgAddService) GetSignBytes() []byte { bz := ModuleCdc.MustMarshalJSON(msg) return sdk.MustSortJSON(bz) } +// ValidateBasic performs basic validation of the message and its fields func (msg *MsgAddService) ValidateBasic() error { - _, err := sdk.AccAddressFromBech32(msg.Address) - if err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid address address (%s)", err) + if _, err := sdk.AccAddressFromBech32(msg.SupplierAddress); err != nil { + return sdkerrors.Wrapf( + ErrServiceInvalidAddress, + "invalid supplier address %s; (%v)", msg.SupplierAddress, err, + ) } + // TODO(@h5law): Check the service ID is not already found in the store return nil } From 1c14818158f29f957c10c29775eec49e7fcde7e9 Mon Sep 17 00:00:00 2001 From: h5law Date: Wed, 10 Jan 2024 18:03:22 +0000 Subject: [PATCH 02/22] feat: add more message validation and initial implementation --- x/service/keeper/msg_server_add_service.go | 11 ++++++-- x/service/types/errors.go | 2 ++ x/service/types/message_add_service.go | 6 ++++ x/service/types/message_add_service_test.go | 31 ++++++++++++++++----- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/x/service/keeper/msg_server_add_service.go b/x/service/keeper/msg_server_add_service.go index f6e7dd160..830ca2e84 100644 --- a/x/service/keeper/msg_server_add_service.go +++ b/x/service/keeper/msg_server_add_service.go @@ -2,16 +2,23 @@ package keeper import ( "context" + "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/pokt-network/poktroll/x/service/types" ) func (k msgServer) AddService(goCtx context.Context, msg *types.MsgAddService) (*types.MsgAddServiceResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - // TODO: Handling the message - _ = ctx + logger := k.Logger(ctx).With("method", "AddService") + logger.Info(fmt.Sprintf("About to add a new service with msg: %v", msg)) + + if err := msg.ValidateBasic(); err != nil { + logger.Error(fmt.Sprintf("Adding service failed basic validation: %v", err)) + return nil, err + } return &types.MsgAddServiceResponse{}, nil } diff --git a/x/service/types/errors.go b/x/service/types/errors.go index cbeb81fc1..864dfec41 100644 --- a/x/service/types/errors.go +++ b/x/service/types/errors.go @@ -9,4 +9,6 @@ import ( var ( ErrServiceDuplicateIndex = sdkerrors.Register(ModuleName, 1, "duplicate index") ErrServiceInvalidAddress = sdkerrors.Register(ModuleName, 2, "invalid address") + ErrServiceMissingID = sdkerrors.Register(ModuleName, 3, "missing service ID") + ErrServiceMissingName = sdkerrors.Register(ModuleName, 4, "missing service name") ) diff --git a/x/service/types/message_add_service.go b/x/service/types/message_add_service.go index 554bf28fe..38425f2f9 100644 --- a/x/service/types/message_add_service.go +++ b/x/service/types/message_add_service.go @@ -53,6 +53,12 @@ func (msg *MsgAddService) ValidateBasic() error { "invalid supplier address %s; (%v)", msg.SupplierAddress, err, ) } + if msg.Service.Id == "" { + return ErrServiceMissingID + } + if msg.Service.Name == "" { + return ErrServiceMissingName + } // TODO(@h5law): Check the service ID is not already found in the store return nil } diff --git a/x/service/types/message_add_service_test.go b/x/service/types/message_add_service_test.go index c660e7751..5260675ab 100644 --- a/x/service/types/message_add_service_test.go +++ b/x/service/types/message_add_service_test.go @@ -3,9 +3,10 @@ package types import ( "testing" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/pokt-network/poktroll/testutil/sample" "github.com/stretchr/testify/require" + + "github.com/pokt-network/poktroll/testutil/sample" + sharedtypes "github.com/pokt-network/poktroll/x/shared/types" ) func TestMsgAddService_ValidateBasic(t *testing.T) { @@ -15,15 +16,31 @@ func TestMsgAddService_ValidateBasic(t *testing.T) { err error }{ { - name: "invalid address", + name: "invalid supplier address - no service", + msg: MsgAddService{ + SupplierAddress: "invalid_address", + // Service: intentionally omitted, + }, + err: ErrServiceInvalidAddress, + }, { + name: "valid supplier address - no service ID", + msg: MsgAddService{ + SupplierAddress: sample.AccAddress(), + Service: sharedtypes.Service{Name: "service name"}, + }, + err: ErrServiceMissingID, + }, { + name: "valid supplier address - no service name", msg: MsgAddService{ - Address: "invalid_address", + SupplierAddress: sample.AccAddress(), + Service: sharedtypes.Service{Id: "srv1"}, }, - err: sdkerrors.ErrInvalidAddress, + err: ErrServiceMissingName, }, { - name: "valid address", + name: "valid address and service", msg: MsgAddService{ - Address: sample.AccAddress(), + SupplierAddress: sample.AccAddress(), + Service: sharedtypes.Service{Id: "srv1", Name: "service name"}, }, }, } From 62046e443b7e27236bd8ea8f79d6039eeeb220f8 Mon Sep 17 00:00:00 2001 From: h5law Date: Thu, 11 Jan 2024 13:14:15 +0000 Subject: [PATCH 03/22] feat: add CRUD operations for the service keeper --- x/service/keeper/service.go | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 x/service/keeper/service.go diff --git a/x/service/keeper/service.go b/x/service/keeper/service.go new file mode 100644 index 000000000..b9ccad8f3 --- /dev/null +++ b/x/service/keeper/service.go @@ -0,0 +1,63 @@ +package keeper + +import ( + "github.com/cosmos/cosmos-sdk/store/prefix" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/pokt-network/poktroll/x/service/types" + sharedtypes "github.com/pokt-network/poktroll/x/shared/types" +) + +// SetService set a specific service in the store from its index +func (k Keeper) SetService(ctx sdk.Context, service sharedtypes.Service) { + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ServiceKeyPrefix)) + b := k.cdc.MustMarshal(&service) + store.Set(types.ServiceKey( + service.Id, + ), b) +} + +// GetService returns a service from its index +func (k Keeper) GetService( + ctx sdk.Context, + id string, +) (val sharedtypes.Service, found bool) { + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ServiceKeyPrefix)) + + b := store.Get(types.ServiceKey( + id, + )) + if b == nil { + return val, false + } + + k.cdc.MustUnmarshal(b, &val) + return val, true +} + +// RemoveService removes a service from the store +func (k Keeper) RemoveService( + ctx sdk.Context, + address string, +) { + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ServiceKeyPrefix)) + store.Delete(types.ServiceKey( + address, + )) +} + +// GetAllService returns all services +func (k Keeper) GetAllService(ctx sdk.Context) (list []sharedtypes.Service) { + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ServiceKeyPrefix)) + iterator := sdk.KVStorePrefixIterator(store, []byte{}) + + defer iterator.Close() + + for ; iterator.Valid(); iterator.Next() { + val := sharedtypes.Service{} + k.cdc.MustUnmarshal(iterator.Value(), &val) + list = append(list, val) + } + + return +} From da1b4d36c76013c6c78ad13f04e50aa26ba287d4 Mon Sep 17 00:00:00 2001 From: h5law Date: Thu, 11 Jan 2024 13:35:39 +0000 Subject: [PATCH 04/22] feat: add service keeper functions and tests --- x/service/keeper/service.go | 4 +- x/service/keeper/service_test.go | 79 ++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 x/service/keeper/service_test.go diff --git a/x/service/keeper/service.go b/x/service/keeper/service.go index b9ccad8f3..f5924b54c 100644 --- a/x/service/keeper/service.go +++ b/x/service/keeper/service.go @@ -46,8 +46,8 @@ func (k Keeper) RemoveService( )) } -// GetAllService returns all services -func (k Keeper) GetAllService(ctx sdk.Context) (list []sharedtypes.Service) { +// GetAllServices returns all services +func (k Keeper) GetAllServices(ctx sdk.Context) (list []sharedtypes.Service) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ServiceKeyPrefix)) iterator := sdk.KVStorePrefixIterator(store, []byte{}) diff --git a/x/service/keeper/service_test.go b/x/service/keeper/service_test.go new file mode 100644 index 000000000..38be8deda --- /dev/null +++ b/x/service/keeper/service_test.go @@ -0,0 +1,79 @@ +package keeper_test + +import ( + "fmt" + "strconv" + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/stretchr/testify/require" + + "github.com/pokt-network/poktroll/cmd/pocketd/cmd" + keepertest "github.com/pokt-network/poktroll/testutil/keeper" + "github.com/pokt-network/poktroll/testutil/nullify" + "github.com/pokt-network/poktroll/x/service/keeper" + "github.com/pokt-network/poktroll/x/service/types" + sharedtypes "github.com/pokt-network/poktroll/x/shared/types" +) + +// Prevent strconv unused error +var _ = strconv.IntSize + +func init() { + cmd.InitSDKConfig() +} + +func createNservices(keeper *keeper.Keeper, ctx sdk.Context, n int) []sharedtypes.Service { + items := make([]sharedtypes.Service, n) + for i := range items { + items[i].Id = fmt.Sprintf("srv%d", i) + items[i].Name = fmt.Sprintf("srv%d", i) + + keeper.SetService(ctx, items[i]) + } + return items +} + +func TestServiceModuleAddress(t *testing.T) { + moduleAddress := authtypes.NewModuleAddress(types.ModuleName) + require.Equal(t, "pokt1nhmtqf4gcmpxu0p6e53hpgtwj0llmsqpxtumcf", moduleAddress.String()) +} + +func TestServiceGet(t *testing.T) { + keeper, ctx := keepertest.ServiceKeeper(t) + items := createNservices(keeper, ctx, 10) + for _, item := range items { + rst, found := keeper.GetService(ctx, + item.Id, + ) + require.True(t, found) + require.Equal(t, + nullify.Fill(&item), + nullify.Fill(&rst), + ) + } +} + +func TestServiceRemove(t *testing.T) { + keeper, ctx := keepertest.ServiceKeeper(t) + items := createNservices(keeper, ctx, 10) + for _, item := range items { + keeper.RemoveService(ctx, + item.Id, + ) + _, found := keeper.GetService(ctx, + item.Id, + ) + require.False(t, found) + } +} + +func TestServiceGetAll(t *testing.T) { + keeper, ctx := keepertest.ServiceKeeper(t) + items := createNservices(keeper, ctx, 10) + require.ElementsMatch(t, + nullify.Fill(items), + nullify.Fill(keeper.GetAllServices(ctx)), + ) +} From cc0c8a3dbf582d7cf97b50aef237adbdc748d606 Mon Sep 17 00:00:00 2001 From: h5law Date: Thu, 11 Jan 2024 13:36:06 +0000 Subject: [PATCH 05/22] feat: implement the add-service CLI command --- x/service/client/cli/tx_add_service.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/x/service/client/cli/tx_add_service.go b/x/service/client/cli/tx_add_service.go index ba35a0a5d..a1a4cd1b2 100644 --- a/x/service/client/cli/tx_add_service.go +++ b/x/service/client/cli/tx_add_service.go @@ -6,18 +6,27 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/tx" - "github.com/pokt-network/poktroll/x/service/types" "github.com/spf13/cobra" + + "github.com/pokt-network/poktroll/x/service/types" ) var _ = strconv.Itoa(0) func CmdAddService() *cobra.Command { cmd := &cobra.Command{ - Use: "add-service", - Short: "Broadcast message add-service", - Args: cobra.ExactArgs(0), + Use: "add-service ", + Short: "Add a new service to the network", + Long: `Add a new service to the network that will be available for applications, +gateways and suppliers to use. The service id MUST be unique - or the command +will fail, however the name you use to describe it does not have to be unique. + +Example: +$ poktrolld --home=$(POKTROLLD_HOME) tx service add-service "srv1" "service_one" --keyring-backend test --from $(SUPPLIER) --node $(POCKET_NODE)`, + Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) (err error) { + serviceIdStr := args[0] + serviceNameStr := args[1] clientCtx, err := client.GetClientTxContext(cmd) if err != nil { @@ -26,6 +35,8 @@ func CmdAddService() *cobra.Command { msg := types.NewMsgAddService( clientCtx.GetFromAddress().String(), + serviceIdStr, + serviceNameStr, ) if err := msg.ValidateBasic(); err != nil { return err From f607760cc3f970340c89905aed60dd3ac3e85221 Mon Sep 17 00:00:00 2001 From: h5law Date: Thu, 11 Jan 2024 13:36:36 +0000 Subject: [PATCH 06/22] feat: update genesis and simulation files --- x/service/genesis.go | 10 ++++------ x/service/simulation/add_service.go | 9 ++++++++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/x/service/genesis.go b/x/service/genesis.go index 3b575385e..c81e649dd 100644 --- a/x/service/genesis.go +++ b/x/service/genesis.go @@ -9,10 +9,9 @@ import ( // InitGenesis initializes the module's state from a provided genesis state. func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) { - // Set all the supplier - for _, supplier := range genState.ServiceList { - // TODO(@h5law): Add this method - // k.SetSupplier(ctx, supplier) + // Set all the initial services + for _, service := range genState.ServiceList { + k.SetService(ctx, service) } // this line is used by starport scaffolding # genesis/module/init k.SetParams(ctx, genState.Params) @@ -23,8 +22,7 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { genesis := types.DefaultGenesis() genesis.Params = k.GetParams(ctx) - // TODO(@h5law): Add this method - // genesis.ServiceList = k.GetAllServices(ctx) + genesis.ServiceList = k.GetAllServices(ctx) // this line is used by starport scaffolding # genesis/module/export return genesis diff --git a/x/service/simulation/add_service.go b/x/service/simulation/add_service.go index f6ebdb485..737f0f07e 100644 --- a/x/service/simulation/add_service.go +++ b/x/service/simulation/add_service.go @@ -1,13 +1,16 @@ package simulation import ( + "fmt" "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/pokt-network/poktroll/x/service/keeper" "github.com/pokt-network/poktroll/x/service/types" + sharedtypes "github.com/pokt-network/poktroll/x/shared/types" ) func SimulateMsgAddService( @@ -19,7 +22,11 @@ func SimulateMsgAddService( ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { simAccount, _ := simtypes.RandomAcc(r, accs) msg := &types.MsgAddService{ - Address: simAccount.Address.String(), + SupplierAddress: simAccount.Address.String(), + Service: sharedtypes.Service{ + Id: fmt.Sprintf("srv%d", rand.Intn(100)), + Name: fmt.Sprintf("srv%d", rand.Intn(100)), + }, } // TODO: Handling the AddService simulation From 8ce4697e6b51505ebe4d89bd32d0c5aee96164d4 Mon Sep 17 00:00:00 2001 From: h5law Date: Thu, 11 Jan 2024 14:05:48 +0000 Subject: [PATCH 07/22] feat: implement AddService --- x/service/keeper/msg_server_add_service.go | 9 +++++++++ x/service/types/errors.go | 1 + x/service/types/message_add_service.go | 2 +- x/service/types/message_add_service_test.go | 2 +- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/x/service/keeper/msg_server_add_service.go b/x/service/keeper/msg_server_add_service.go index 830ca2e84..8ed6e0b5c 100644 --- a/x/service/keeper/msg_server_add_service.go +++ b/x/service/keeper/msg_server_add_service.go @@ -9,6 +9,8 @@ import ( "github.com/pokt-network/poktroll/x/service/types" ) +// AddService handles MsgAddService and adds a service to the network storing +// it in the service keeper's store using the provided ID from the message. func (k msgServer) AddService(goCtx context.Context, msg *types.MsgAddService) (*types.MsgAddServiceResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) @@ -20,5 +22,12 @@ func (k msgServer) AddService(goCtx context.Context, msg *types.MsgAddService) ( return nil, err } + if _, found := k.GetService(ctx, msg.Service.Id); found { + logger.Error(fmt.Sprintf("Service already exists: %v", msg.Service)) + return nil, types.ErrServiceAlreadyExists + } + + k.SetService(ctx, msg.Service) + return &types.MsgAddServiceResponse{}, nil } diff --git a/x/service/types/errors.go b/x/service/types/errors.go index 864dfec41..52d0f5045 100644 --- a/x/service/types/errors.go +++ b/x/service/types/errors.go @@ -11,4 +11,5 @@ var ( ErrServiceInvalidAddress = sdkerrors.Register(ModuleName, 2, "invalid address") ErrServiceMissingID = sdkerrors.Register(ModuleName, 3, "missing service ID") ErrServiceMissingName = sdkerrors.Register(ModuleName, 4, "missing service name") + ErrServiceAlreadyExists = sdkerrors.Register(ModuleName, 5, "service already exists") ) diff --git a/x/service/types/message_add_service.go b/x/service/types/message_add_service.go index 38425f2f9..a1024453c 100644 --- a/x/service/types/message_add_service.go +++ b/x/service/types/message_add_service.go @@ -59,6 +59,6 @@ func (msg *MsgAddService) ValidateBasic() error { if msg.Service.Name == "" { return ErrServiceMissingName } - // TODO(@h5law): Check the service ID is not already found in the store + return nil } diff --git a/x/service/types/message_add_service_test.go b/x/service/types/message_add_service_test.go index 5260675ab..9514f733b 100644 --- a/x/service/types/message_add_service_test.go +++ b/x/service/types/message_add_service_test.go @@ -37,7 +37,7 @@ func TestMsgAddService_ValidateBasic(t *testing.T) { }, err: ErrServiceMissingName, }, { - name: "valid address and service", + name: "valid supplier address and service", msg: MsgAddService{ SupplierAddress: sample.AccAddress(), Service: sharedtypes.Service{Id: "srv1", Name: "service name"}, From 14fddcbef375e72975438eaa22d3295c17aa25f3 Mon Sep 17 00:00:00 2001 From: h5law Date: Thu, 11 Jan 2024 16:01:32 +0000 Subject: [PATCH 08/22] feat: renaming, minor refactor in simulation --- x/service/keeper/msg_server_add_service.go | 5 ++++- x/service/simulation/add_service.go | 5 +++-- x/service/types/key_service.go | 4 ++-- x/service/types/message_add_service.go | 4 ++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/x/service/keeper/msg_server_add_service.go b/x/service/keeper/msg_server_add_service.go index 8ed6e0b5c..a8af37d0d 100644 --- a/x/service/keeper/msg_server_add_service.go +++ b/x/service/keeper/msg_server_add_service.go @@ -11,7 +11,10 @@ import ( // AddService handles MsgAddService and adds a service to the network storing // it in the service keeper's store using the provided ID from the message. -func (k msgServer) AddService(goCtx context.Context, msg *types.MsgAddService) (*types.MsgAddServiceResponse, error) { +func (k msgServer) AddService( + goCtx context.Context, + msg *types.MsgAddService, +) (*types.MsgAddServiceResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) logger := k.Logger(ctx).With("method", "AddService") diff --git a/x/service/simulation/add_service.go b/x/service/simulation/add_service.go index 737f0f07e..68e0403e9 100644 --- a/x/service/simulation/add_service.go +++ b/x/service/simulation/add_service.go @@ -21,11 +21,12 @@ func SimulateMsgAddService( 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) + rndNum := r.Intn(100) msg := &types.MsgAddService{ SupplierAddress: simAccount.Address.String(), Service: sharedtypes.Service{ - Id: fmt.Sprintf("srv%d", rand.Intn(100)), - Name: fmt.Sprintf("srv%d", rand.Intn(100)), + Id: fmt.Sprintf("srv%d", rndNum), + Name: fmt.Sprintf("service %d", rndNum), }, } diff --git a/x/service/types/key_service.go b/x/service/types/key_service.go index 37fe1ec5b..6893f7e29 100644 --- a/x/service/types/key_service.go +++ b/x/service/types/key_service.go @@ -15,8 +15,8 @@ const ( func ServiceKey(serviceID string) []byte { var key []byte - serviceIDBytes := []byte(serviceID) - key = append(key, serviceIDBytes...) + serviceIDBz := []byte(serviceID) + key = append(key, serviceIDBz...) key = append(key, []byte("/")...) return key diff --git a/x/service/types/message_add_service.go b/x/service/types/message_add_service.go index a1024453c..abd8078ba 100644 --- a/x/service/types/message_add_service.go +++ b/x/service/types/message_add_service.go @@ -4,7 +4,7 @@ import ( sdkerrors "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" - shared "github.com/pokt-network/poktroll/x/shared/types" + sharedtypes "github.com/pokt-network/poktroll/x/shared/types" ) // TypeMsgAddService is the name of the service message @@ -16,7 +16,7 @@ var _ sdk.Msg = (*MsgAddService)(nil) func NewMsgAddService(address string, serviceID, serviceName string) *MsgAddService { return &MsgAddService{ SupplierAddress: address, - Service: shared.Service{Id: serviceID, Name: serviceName}, + Service: sharedtypes.Service{Id: serviceID, Name: serviceName}, } } From cf031bf1ac59f4d2f573c7a00136d8a7c1d99d15 Mon Sep 17 00:00:00 2001 From: h5law Date: Thu, 11 Jan 2024 16:02:56 +0000 Subject: [PATCH 09/22] feat: add info log line when adding service --- x/service/keeper/msg_server_add_service.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/service/keeper/msg_server_add_service.go b/x/service/keeper/msg_server_add_service.go index a8af37d0d..463da0a3b 100644 --- a/x/service/keeper/msg_server_add_service.go +++ b/x/service/keeper/msg_server_add_service.go @@ -30,6 +30,7 @@ func (k msgServer) AddService( return nil, types.ErrServiceAlreadyExists } + logger.Info(fmt.Sprintf("Adding service: %v", msg.Service)) k.SetService(ctx, msg.Service) return &types.MsgAddServiceResponse{}, nil From d0dab7460535e74565050fbd9f002af04fdc1a76 Mon Sep 17 00:00:00 2001 From: h5law Date: Thu, 11 Jan 2024 19:53:53 +0000 Subject: [PATCH 10/22] feat: add CmdAddService tx tests --- x/service/client/cli/helpers_test.go | 34 +++++ x/service/client/cli/tx_add_service_test.go | 139 ++++++++++++++++++++ x/service/types/message_add_service_test.go | 4 +- 3 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 x/service/client/cli/helpers_test.go create mode 100644 x/service/client/cli/tx_add_service_test.go diff --git a/x/service/client/cli/helpers_test.go b/x/service/client/cli/helpers_test.go new file mode 100644 index 000000000..d6066c28a --- /dev/null +++ b/x/service/client/cli/helpers_test.go @@ -0,0 +1,34 @@ +// Package cli_test provides unit tests for the CLI functionality. +package cli_test + +import ( + "strconv" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/pokt-network/poktroll/cmd/pocketd/cmd" + "github.com/pokt-network/poktroll/testutil/network" + sharedtypes "github.com/pokt-network/poktroll/x/shared/types" + "github.com/pokt-network/poktroll/x/supplier/types" +) + +// Dummy variable to avoid unused import error. +var _ = strconv.IntSize + +// init initializes the SDK configuration. +func init() { + cmd.InitSDKConfig() +} + +// networkWithSupplierObjects creates a new network with a given number of supplier objects. +// It returns the network and a slice of the created supplier objects. +func networkWithSupplierObjects(t *testing.T, n int) (*network.Network, []sharedtypes.Supplier) { + t.Helper() + cfg := network.DefaultConfig() + supplierGenesisState := network.DefaultSupplierModuleGenesisState(t, n) + buf, err := cfg.Codec.MarshalJSON(supplierGenesisState) + require.NoError(t, err) + cfg.GenesisState[types.ModuleName] = buf + return network.New(t, cfg), supplierGenesisState.SupplierList +} diff --git a/x/service/client/cli/tx_add_service_test.go b/x/service/client/cli/tx_add_service_test.go new file mode 100644 index 000000000..45c42c9b8 --- /dev/null +++ b/x/service/client/cli/tx_add_service_test.go @@ -0,0 +1,139 @@ +package cli_test + +import ( + "fmt" + "testing" + + sdkerrors "cosmossdk.io/errors" + sdkmath "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/testutil" + clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" + "google.golang.org/grpc/status" + + "github.com/pokt-network/poktroll/testutil/network" + "github.com/pokt-network/poktroll/x/service/client/cli" + "github.com/pokt-network/poktroll/x/service/types" + sharedtypes "github.com/pokt-network/poktroll/x/shared/types" +) + +func TestCLI_AddService(t *testing.T) { + net, _ := networkWithSupplierObjects(t, 1) + val := net.Validators[0] + ctx := val.ClientCtx + + // Create a keyring and add an account for the supplier adding the service + kr := ctx.Keyring + accounts := testutil.CreateKeyringAccounts(t, kr, 1) + supplierAccount := accounts[0] + + // Update the context with the new keyring + ctx = ctx.WithKeyring(kr) + + // Common args used for all requests + commonArgs := []string{ + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()), + } + + // Initialize the Supplier account by sending it some funds from the + // validator account that is part of genesis + network.InitAccountWithSequence(t, net, supplierAccount.Address, 1) + + // Wait for a new block to be committed + require.NoError(t, net.WaitForNextBlock()) + + // Prepare two valid services + srv1 := sharedtypes.Service{ + Id: "srv1", + Name: "service name", + } + srv2 := sharedtypes.Service{ + Id: "srv2", + Name: "service name 2", + } + // Add srv2 to the network + args := []string{ + srv2.Id, + srv2.Name, + fmt.Sprintf("--%s=%s", flags.FlagFrom, supplierAccount.Address.String()), + } + args = append(args, commonArgs...) + _, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdAddService(), args) + require.NoError(t, err) + + tests := []struct { + desc string + supplierAddress string + service sharedtypes.Service + err *sdkerrors.Error + }{ + { + desc: "valid - add new service", + supplierAddress: supplierAccount.Address.String(), + service: srv1, + }, + { + desc: "invalid - missing service id", + supplierAddress: supplierAccount.Address.String(), + service: sharedtypes.Service{Name: "service name"}, // ID intentionally omitted + err: types.ErrServiceMissingID, + }, + { + desc: "invalid - missing service name", + supplierAddress: supplierAccount.Address.String(), + service: sharedtypes.Service{Id: "srv1"}, // Name intentionally omitted + err: types.ErrServiceMissingName, + }, + { + desc: "invalid - invalid supplier address", + supplierAddress: "invalid address", + service: srv1, + err: types.ErrServiceInvalidAddress, + }, + { + desc: "invalid - service already staked", + supplierAddress: supplierAccount.Address.String(), + service: srv2, + err: types.ErrServiceAlreadyExists, + }, + } + + // Run the tests + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + // Wait for a new block to be committed + require.NoError(t, net.WaitForNextBlock()) + + // Prepare the arguments for the CLI command + args := []string{ + tt.service.Id, + tt.service.Name, + fmt.Sprintf("--%s=%s", flags.FlagFrom, tt.supplierAddress), + } + args = append(args, commonArgs...) + + // Execute the command + addServiceOutput, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdAddService(), args) + + // Validate the error if one is expected + if tt.err != nil { + stat, ok := status.FromError(tt.err) + require.True(t, ok) + require.Contains(t, stat.Message(), tt.err.Error()) + return + } + require.NoError(t, err) + + // Check the response + var resp sdk.TxResponse + require.NoError(t, net.Config.Codec.UnmarshalJSON(addServiceOutput.Bytes(), &resp)) + require.NotNil(t, resp) + require.NotNil(t, resp.TxHash) + require.Equal(t, uint32(0), resp.Code) + }) + } +} diff --git a/x/service/types/message_add_service_test.go b/x/service/types/message_add_service_test.go index 9514f733b..94ba0eb2a 100644 --- a/x/service/types/message_add_service_test.go +++ b/x/service/types/message_add_service_test.go @@ -26,14 +26,14 @@ func TestMsgAddService_ValidateBasic(t *testing.T) { name: "valid supplier address - no service ID", msg: MsgAddService{ SupplierAddress: sample.AccAddress(), - Service: sharedtypes.Service{Name: "service name"}, + Service: sharedtypes.Service{Name: "service name"}, // ID intentionally omitted }, err: ErrServiceMissingID, }, { name: "valid supplier address - no service name", msg: MsgAddService{ SupplierAddress: sample.AccAddress(), - Service: sharedtypes.Service{Id: "srv1"}, + Service: sharedtypes.Service{Id: "srv1"}, // Name intentionally omitted }, err: ErrServiceMissingName, }, { From 193e454ab18807fd2d252930ac5ba8c3ff4408fa Mon Sep 17 00:00:00 2001 From: h5law Date: Thu, 11 Jan 2024 21:08:37 +0000 Subject: [PATCH 11/22] feat: add message handler tests --- .../keeper/msg_server_add_service_test.go | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 x/service/keeper/msg_server_add_service_test.go diff --git a/x/service/keeper/msg_server_add_service_test.go b/x/service/keeper/msg_server_add_service_test.go new file mode 100644 index 000000000..cbefe790b --- /dev/null +++ b/x/service/keeper/msg_server_add_service_test.go @@ -0,0 +1,139 @@ +package keeper_test + +import ( + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" + + keepertest "github.com/pokt-network/poktroll/testutil/keeper" + "github.com/pokt-network/poktroll/testutil/sample" + "github.com/pokt-network/poktroll/x/service/keeper" + "github.com/pokt-network/poktroll/x/service/types" + sharedtypes "github.com/pokt-network/poktroll/x/shared/types" +) + +func TestMsgServer_AddService(t *testing.T) { + k, ctx := keepertest.ServiceKeeper(t) + srv := keeper.NewMsgServerImpl(*k) + wctx := sdk.WrapSDKContext(ctx) + + // Generate a supplier address + supplierAddr := sample.AccAddress() + // Create a service + preExistingService := sharedtypes.Service{ + Id: "svc2", + Name: "service 2", + } + // Add the service to the store + _, err := srv.AddService(wctx, &types.MsgAddService{ + SupplierAddress: supplierAddr, + Service: preExistingService, + }) + require.NoError(t, err) + // Validate the service was added + serviceFound, found := k.GetService(ctx, preExistingService.Id) + require.True(t, found) + require.Equal(t, preExistingService, serviceFound) + + tests := []struct { + desc string + supplierAddress string + service sharedtypes.Service + expectedError error + }{ + { + desc: "valid - service added successfully", + supplierAddress: sample.AccAddress(), + service: sharedtypes.Service{ + Id: "svc1", + Name: "service 1", + }, + expectedError: nil, + }, + { + desc: "invalid - supplier address is empty", + supplierAddress: "", // explicitly set to empty string + service: sharedtypes.Service{ + Id: "svc1", + Name: "service 1", + }, + expectedError: types.ErrServiceInvalidAddress, + }, + { + desc: "invalid - invalid supplier address", + supplierAddress: "invalid address", + service: sharedtypes.Service{ + Id: "svc1", + Name: "service 1", + }, + expectedError: types.ErrServiceInvalidAddress, + }, + { + desc: "invalid - missing service ID", + supplierAddress: sample.AccAddress(), + service: sharedtypes.Service{ + // Explicitly omitting Id field + Name: "service 1", + }, + expectedError: types.ErrServiceMissingID, + }, + { + desc: "invalid - empty service ID", + supplierAddress: sample.AccAddress(), + service: sharedtypes.Service{ + Id: "", // explicitly set to empty string + Name: "service 1", + }, + expectedError: types.ErrServiceMissingID, + }, + { + desc: "invalid - missing service name", + supplierAddress: sample.AccAddress(), + service: sharedtypes.Service{ + Id: "svc1", + // Explicitly omitting Name field + }, + expectedError: types.ErrServiceMissingName, + }, + { + desc: "invalid - empty service name", + supplierAddress: sample.AccAddress(), + service: sharedtypes.Service{ + Id: "svc1", + Name: "", // explicitly set to empty string + }, + expectedError: types.ErrServiceMissingName, + }, + { + desc: "invalid - service already exists (same supplier)", + supplierAddress: supplierAddr, + service: preExistingService, + expectedError: types.ErrServiceAlreadyExists, + }, + { + desc: "invalid - service already exists (different supplier)", + supplierAddress: sample.AccAddress(), + service: preExistingService, + expectedError: types.ErrServiceAlreadyExists, + }, + } + + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + _, err := srv.AddService(wctx, &types.MsgAddService{ + SupplierAddress: tt.supplierAddress, + Service: tt.service, + }) + if tt.expectedError != nil { + require.ErrorIs(t, err, tt.expectedError) + return + } + require.NoError(t, err) + // Validate the service was added + serviceFound, found := k.GetService(ctx, tt.service.Id) + require.True(t, found) + require.Equal(t, tt.service, serviceFound) + }) + } +} From 099f36838eb410389e096c1df7cd3af3b8e358e9 Mon Sep 17 00:00:00 2001 From: h5law Date: Fri, 12 Jan 2024 15:43:34 +0000 Subject: [PATCH 12/22] chore: localnet residuals --- docs/static/openapi.yml | 2 -- go.mod | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/docs/static/openapi.yml b/docs/static/openapi.yml index 83bbefe5f..af8b41430 100644 --- a/docs/static/openapi.yml +++ b/docs/static/openapi.yml @@ -79107,8 +79107,6 @@ definitions: description: params holds all the parameters of this module. type: object description: QueryParamsResponse is response type for the Query/Params RPC method. - poktroll.service.MsgAddServiceResponse: - type: object poktroll.tokenomics.Params: type: object properties: diff --git a/go.mod b/go.mod index 9be97f1d6..56ed2d4fc 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( github.com/athanorlabs/go-dleq v0.1.0 github.com/cometbft/cometbft v0.37.2 github.com/cometbft/cometbft-db v0.8.0 + github.com/cosmos/cosmos-proto v1.0.0-beta.2 github.com/cosmos/cosmos-sdk v0.47.3 github.com/cosmos/gogoproto v1.4.11 github.com/cosmos/ibc-go/v7 v7.1.0 @@ -47,6 +48,7 @@ require ( golang.org/x/crypto v0.15.0 golang.org/x/exp v0.0.0-20230905200255-921286631fa9 golang.org/x/sync v0.5.0 + google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb google.golang.org/grpc v1.59.0 gopkg.in/yaml.v2 v2.4.0 ) @@ -90,7 +92,6 @@ require ( github.com/containerd/cgroups v1.1.0 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/cosmos/btcutil v1.0.5 // indirect - github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect github.com/cosmos/go-bip39 v1.0.0 // indirect github.com/cosmos/gogogateway v1.2.0 // indirect github.com/cosmos/iavl v0.20.0 // indirect @@ -285,7 +286,6 @@ require ( google.golang.org/api v0.143.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto v0.0.0-20230913181813-007df8e322eb // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 // indirect google.golang.org/protobuf v1.31.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect From cb7cc56265abc4f020c43b19c4dadb98bf171161 Mon Sep 17 00:00:00 2001 From: h5law Date: Fri, 12 Jan 2024 16:49:58 +0000 Subject: [PATCH 13/22] chore: rename test struct fields --- x/service/types/message_add_service_test.go | 26 ++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/x/service/types/message_add_service_test.go b/x/service/types/message_add_service_test.go index 94ba0eb2a..c95d76ea9 100644 --- a/x/service/types/message_add_service_test.go +++ b/x/service/types/message_add_service_test.go @@ -11,33 +11,33 @@ import ( func TestMsgAddService_ValidateBasic(t *testing.T) { tests := []struct { - name string - msg MsgAddService - err error + desc string + msg MsgAddService + expectedErr error }{ { - name: "invalid supplier address - no service", + desc: "invalid supplier address - no service", msg: MsgAddService{ SupplierAddress: "invalid_address", // Service: intentionally omitted, }, - err: ErrServiceInvalidAddress, + expectedErr: ErrServiceInvalidAddress, }, { - name: "valid supplier address - no service ID", + desc: "valid supplier address - no service ID", msg: MsgAddService{ SupplierAddress: sample.AccAddress(), Service: sharedtypes.Service{Name: "service name"}, // ID intentionally omitted }, - err: ErrServiceMissingID, + expectedErr: ErrServiceMissingID, }, { - name: "valid supplier address - no service name", + desc: "valid supplier address - no service name", msg: MsgAddService{ SupplierAddress: sample.AccAddress(), Service: sharedtypes.Service{Id: "srv1"}, // Name intentionally omitted }, - err: ErrServiceMissingName, + expectedErr: ErrServiceMissingName, }, { - name: "valid supplier address and service", + desc: "valid supplier address and service", msg: MsgAddService{ SupplierAddress: sample.AccAddress(), Service: sharedtypes.Service{Id: "srv1", Name: "service name"}, @@ -45,10 +45,10 @@ func TestMsgAddService_ValidateBasic(t *testing.T) { }, } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + t.Run(tt.desc, func(t *testing.T) { err := tt.msg.ValidateBasic() - if tt.err != nil { - require.ErrorIs(t, err, tt.err) + if tt.expectedErr != nil { + require.ErrorIs(t, err, tt.expectedErr) return } require.NoError(t, err) From cd73b31ee2df55ddf6294ad045700b0cfe1d558c Mon Sep 17 00:00:00 2001 From: h5law Date: Thu, 18 Jan 2024 16:56:30 +0000 Subject: [PATCH 14/22] chore: ignore mise local config file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 9aaa16ba7..b9ee9601a 100644 --- a/.gitignore +++ b/.gitignore @@ -40,6 +40,7 @@ localnet/*/config/*.json .vscode .env .idea/ +.mise.local.toml # Compiled protos **/*.pb.go From 7cba05fc8e7950a250f3e988af55f4b5fed2312e Mon Sep 17 00:00:00 2001 From: h5law Date: Thu, 18 Jan 2024 17:18:34 +0000 Subject: [PATCH 15/22] chore: address comments --- go.mod | 4 +- proto/pocket/service/genesis.proto | 2 +- proto/pocket/service/tx.proto | 10 ++- x/service/keeper/msg_server_add_service.go | 3 + .../keeper/msg_server_add_service_test.go | 64 +++++++++---------- x/service/keeper/service.go | 28 +++++--- x/service/simulation/add_service.go | 2 +- x/service/types/errors.go | 2 - x/service/types/message_add_service.go | 10 +-- x/service/types/message_add_service_test.go | 23 +++---- 10 files changed, 84 insertions(+), 64 deletions(-) diff --git a/go.mod b/go.mod index 56ed2d4fc..9be97f1d6 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,6 @@ require ( github.com/athanorlabs/go-dleq v0.1.0 github.com/cometbft/cometbft v0.37.2 github.com/cometbft/cometbft-db v0.8.0 - github.com/cosmos/cosmos-proto v1.0.0-beta.2 github.com/cosmos/cosmos-sdk v0.47.3 github.com/cosmos/gogoproto v1.4.11 github.com/cosmos/ibc-go/v7 v7.1.0 @@ -48,7 +47,6 @@ require ( golang.org/x/crypto v0.15.0 golang.org/x/exp v0.0.0-20230905200255-921286631fa9 golang.org/x/sync v0.5.0 - google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb google.golang.org/grpc v1.59.0 gopkg.in/yaml.v2 v2.4.0 ) @@ -92,6 +90,7 @@ require ( github.com/containerd/cgroups v1.1.0 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/cosmos/btcutil v1.0.5 // indirect + github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect github.com/cosmos/go-bip39 v1.0.0 // indirect github.com/cosmos/gogogateway v1.2.0 // indirect github.com/cosmos/iavl v0.20.0 // indirect @@ -286,6 +285,7 @@ require ( google.golang.org/api v0.143.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto v0.0.0-20230913181813-007df8e322eb // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 // indirect google.golang.org/protobuf v1.31.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect diff --git a/proto/pocket/service/genesis.proto b/proto/pocket/service/genesis.proto index e2f559467..9e32feb53 100644 --- a/proto/pocket/service/genesis.proto +++ b/proto/pocket/service/genesis.proto @@ -10,5 +10,5 @@ option go_package = "github.com/pokt-network/poktroll/x/service/types"; // GenesisState defines the service module's genesis state. message GenesisState { Params params = 1 [(gogoproto.nullable) = false]; - repeated pocket.shared.Service serviceList = 2 [(gogoproto.nullable) = false]; + repeated pocket.shared.Service service_list = 2 [(gogoproto.nullable) = false]; } diff --git a/proto/pocket/service/tx.proto b/proto/pocket/service/tx.proto index 6c1432888..4a22016e1 100644 --- a/proto/pocket/service/tx.proto +++ b/proto/pocket/service/tx.proto @@ -14,9 +14,15 @@ service Msg { rpc AddService (MsgAddService) returns (MsgAddServiceResponse); } +// MsgAddService defines a message for adding a new message to the network. +// Services can be added by any actor in the network making them truly +// permissionless. +// TODO_DOCUMENT(@h5law): This is a key function in making services +// permissionless, document it's usage and design - in docusaurus covering how +// the entire process works. message MsgAddService { - option (cosmos.msg.v1.signer) = "supplier_address"; // https://docs.cosmos.network/main/build/building-modules/messages-and-queries - string supplier_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"]; // The Bech32 address of the application using cosmos' ScalarDescriptor to ensure deterministic deterministic encoding using cosmos' ScalarDescriptor to ensure deterministic deterministic encoding + option (cosmos.msg.v1.signer) = "address"; // https://docs.cosmos.network/main/build/building-modules/messages-and-queries + string address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"]; // The Bech32 address of the service supplier using cosmos' ScalarDescriptor shared.Service service = 2 [(gogoproto.nullable) = false]; // The Service for which the supplier is adding to the network } diff --git a/x/service/keeper/msg_server_add_service.go b/x/service/keeper/msg_server_add_service.go index 463da0a3b..11737a559 100644 --- a/x/service/keeper/msg_server_add_service.go +++ b/x/service/keeper/msg_server_add_service.go @@ -11,6 +11,9 @@ import ( // AddService handles MsgAddService and adds a service to the network storing // it in the service keeper's store using the provided ID from the message. +// TODO(@h5law): Add a governance parameter defining the cost to add a service +// and enforce it then send the fee to the service module, if the signer has +// enough funds otherwise fail. func (k msgServer) AddService( goCtx context.Context, msg *types.MsgAddService, diff --git a/x/service/keeper/msg_server_add_service_test.go b/x/service/keeper/msg_server_add_service_test.go index cbefe790b..9c9718507 100644 --- a/x/service/keeper/msg_server_add_service_test.go +++ b/x/service/keeper/msg_server_add_service_test.go @@ -18,8 +18,8 @@ func TestMsgServer_AddService(t *testing.T) { srv := keeper.NewMsgServerImpl(*k) wctx := sdk.WrapSDKContext(ctx) - // Generate a supplier address - supplierAddr := sample.AccAddress() + // Generate a valid address + addr := sample.AccAddress() // Create a service preExistingService := sharedtypes.Service{ Id: "svc2", @@ -27,8 +27,8 @@ func TestMsgServer_AddService(t *testing.T) { } // Add the service to the store _, err := srv.AddService(wctx, &types.MsgAddService{ - SupplierAddress: supplierAddr, - Service: preExistingService, + Address: addr, + Service: preExistingService, }) require.NoError(t, err) // Validate the service was added @@ -37,14 +37,14 @@ func TestMsgServer_AddService(t *testing.T) { require.Equal(t, preExistingService, serviceFound) tests := []struct { - desc string - supplierAddress string - service sharedtypes.Service - expectedError error + desc string + address string + service sharedtypes.Service + expectedError error }{ { - desc: "valid - service added successfully", - supplierAddress: sample.AccAddress(), + desc: "valid - service added successfully", + address: sample.AccAddress(), service: sharedtypes.Service{ Id: "svc1", Name: "service 1", @@ -52,8 +52,8 @@ func TestMsgServer_AddService(t *testing.T) { expectedError: nil, }, { - desc: "invalid - supplier address is empty", - supplierAddress: "", // explicitly set to empty string + desc: "invalid - service supplier address is empty", + address: "", // explicitly set to empty string service: sharedtypes.Service{ Id: "svc1", Name: "service 1", @@ -61,8 +61,8 @@ func TestMsgServer_AddService(t *testing.T) { expectedError: types.ErrServiceInvalidAddress, }, { - desc: "invalid - invalid supplier address", - supplierAddress: "invalid address", + desc: "invalid - invalid service supplier address", + address: "invalid address", service: sharedtypes.Service{ Id: "svc1", Name: "service 1", @@ -70,8 +70,8 @@ func TestMsgServer_AddService(t *testing.T) { expectedError: types.ErrServiceInvalidAddress, }, { - desc: "invalid - missing service ID", - supplierAddress: sample.AccAddress(), + desc: "invalid - missing service ID", + address: sample.AccAddress(), service: sharedtypes.Service{ // Explicitly omitting Id field Name: "service 1", @@ -79,8 +79,8 @@ func TestMsgServer_AddService(t *testing.T) { expectedError: types.ErrServiceMissingID, }, { - desc: "invalid - empty service ID", - supplierAddress: sample.AccAddress(), + desc: "invalid - empty service ID", + address: sample.AccAddress(), service: sharedtypes.Service{ Id: "", // explicitly set to empty string Name: "service 1", @@ -88,8 +88,8 @@ func TestMsgServer_AddService(t *testing.T) { expectedError: types.ErrServiceMissingID, }, { - desc: "invalid - missing service name", - supplierAddress: sample.AccAddress(), + desc: "invalid - missing service name", + address: sample.AccAddress(), service: sharedtypes.Service{ Id: "svc1", // Explicitly omitting Name field @@ -97,8 +97,8 @@ func TestMsgServer_AddService(t *testing.T) { expectedError: types.ErrServiceMissingName, }, { - desc: "invalid - empty service name", - supplierAddress: sample.AccAddress(), + desc: "invalid - empty service name", + address: sample.AccAddress(), service: sharedtypes.Service{ Id: "svc1", Name: "", // explicitly set to empty string @@ -106,24 +106,24 @@ func TestMsgServer_AddService(t *testing.T) { expectedError: types.ErrServiceMissingName, }, { - desc: "invalid - service already exists (same supplier)", - supplierAddress: supplierAddr, - service: preExistingService, - expectedError: types.ErrServiceAlreadyExists, + desc: "invalid - service already exists (same service supplier)", + address: addr, + service: preExistingService, + expectedError: types.ErrServiceAlreadyExists, }, { - desc: "invalid - service already exists (different supplier)", - supplierAddress: sample.AccAddress(), - service: preExistingService, - expectedError: types.ErrServiceAlreadyExists, + desc: "invalid - service already exists (different service supplier)", + address: sample.AccAddress(), + service: preExistingService, + expectedError: types.ErrServiceAlreadyExists, }, } for _, tt := range tests { t.Run(tt.desc, func(t *testing.T) { _, err := srv.AddService(wctx, &types.MsgAddService{ - SupplierAddress: tt.supplierAddress, - Service: tt.service, + Address: tt.address, + Service: tt.service, }) if tt.expectedError != nil { require.ErrorIs(t, err, tt.expectedError) diff --git a/x/service/keeper/service.go b/x/service/keeper/service.go index f5924b54c..ec5a1e7ad 100644 --- a/x/service/keeper/service.go +++ b/x/service/keeper/service.go @@ -8,21 +8,27 @@ import ( sharedtypes "github.com/pokt-network/poktroll/x/shared/types" ) -// SetService set a specific service in the store from its index +// SetService set a specific service in the store from its index. func (k Keeper) SetService(ctx sdk.Context, service sharedtypes.Service) { - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ServiceKeyPrefix)) + store := prefix.NewStore( + ctx.KVStore(k.storeKey), + types.KeyPrefix(types.ServiceKeyPrefix), + ) b := k.cdc.MustMarshal(&service) store.Set(types.ServiceKey( service.Id, ), b) } -// GetService returns a service from its index +// GetService returns a service from the store by its index. func (k Keeper) GetService( ctx sdk.Context, id string, ) (val sharedtypes.Service, found bool) { - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ServiceKeyPrefix)) + store := prefix.NewStore( + ctx.KVStore(k.storeKey), + types.KeyPrefix(types.ServiceKeyPrefix), + ) b := store.Get(types.ServiceKey( id, @@ -35,20 +41,26 @@ func (k Keeper) GetService( return val, true } -// RemoveService removes a service from the store +// RemoveService removes a service from the store. func (k Keeper) RemoveService( ctx sdk.Context, address string, ) { - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ServiceKeyPrefix)) + store := prefix.NewStore( + ctx.KVStore(k.storeKey), + types.KeyPrefix(types.ServiceKeyPrefix), + ) store.Delete(types.ServiceKey( address, )) } -// GetAllServices returns all services +// GetAllServices returns all services from the store. func (k Keeper) GetAllServices(ctx sdk.Context) (list []sharedtypes.Service) { - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ServiceKeyPrefix)) + store := prefix.NewStore( + ctx.KVStore(k.storeKey), + types.KeyPrefix(types.ServiceKeyPrefix), + ) iterator := sdk.KVStorePrefixIterator(store, []byte{}) defer iterator.Close() diff --git a/x/service/simulation/add_service.go b/x/service/simulation/add_service.go index 68e0403e9..76188162f 100644 --- a/x/service/simulation/add_service.go +++ b/x/service/simulation/add_service.go @@ -23,7 +23,7 @@ func SimulateMsgAddService( simAccount, _ := simtypes.RandomAcc(r, accs) rndNum := r.Intn(100) msg := &types.MsgAddService{ - SupplierAddress: simAccount.Address.String(), + Address: simAccount.Address.String(), Service: sharedtypes.Service{ Id: fmt.Sprintf("srv%d", rndNum), Name: fmt.Sprintf("service %d", rndNum), diff --git a/x/service/types/errors.go b/x/service/types/errors.go index 52d0f5045..9278abf38 100644 --- a/x/service/types/errors.go +++ b/x/service/types/errors.go @@ -1,7 +1,5 @@ package types -// DONTCOVER - import ( sdkerrors "cosmossdk.io/errors" ) diff --git a/x/service/types/message_add_service.go b/x/service/types/message_add_service.go index abd8078ba..73a089189 100644 --- a/x/service/types/message_add_service.go +++ b/x/service/types/message_add_service.go @@ -15,8 +15,8 @@ var _ sdk.Msg = (*MsgAddService)(nil) // NewMsgAddService creates a new MsgAddService instance func NewMsgAddService(address string, serviceID, serviceName string) *MsgAddService { return &MsgAddService{ - SupplierAddress: address, - Service: sharedtypes.Service{Id: serviceID, Name: serviceName}, + Address: address, + Service: sharedtypes.Service{Id: serviceID, Name: serviceName}, } } @@ -32,7 +32,7 @@ func (msg *MsgAddService) Type() string { // GetSigners returns the signers of the message func (msg *MsgAddService) GetSigners() []sdk.AccAddress { - address, err := sdk.AccAddressFromBech32(msg.SupplierAddress) + address, err := sdk.AccAddressFromBech32(msg.Address) if err != nil { panic(err) } @@ -47,10 +47,10 @@ func (msg *MsgAddService) GetSignBytes() []byte { // ValidateBasic performs basic validation of the message and its fields func (msg *MsgAddService) ValidateBasic() error { - if _, err := sdk.AccAddressFromBech32(msg.SupplierAddress); err != nil { + if _, err := sdk.AccAddressFromBech32(msg.Address); err != nil { return sdkerrors.Wrapf( ErrServiceInvalidAddress, - "invalid supplier address %s; (%v)", msg.SupplierAddress, err, + "invalid supplier address %s; (%v)", msg.Address, err, ) } if msg.Service.Id == "" { diff --git a/x/service/types/message_add_service_test.go b/x/service/types/message_add_service_test.go index c95d76ea9..be94b9d03 100644 --- a/x/service/types/message_add_service_test.go +++ b/x/service/types/message_add_service_test.go @@ -16,32 +16,33 @@ func TestMsgAddService_ValidateBasic(t *testing.T) { expectedErr error }{ { - desc: "invalid supplier address - no service", + desc: "invalid service supplier address - no service", msg: MsgAddService{ - SupplierAddress: "invalid_address", + Address: "invalid_address", // Service: intentionally omitted, }, expectedErr: ErrServiceInvalidAddress, }, { - desc: "valid supplier address - no service ID", + desc: "valid service supplier address - no service ID", msg: MsgAddService{ - SupplierAddress: sample.AccAddress(), - Service: sharedtypes.Service{Name: "service name"}, // ID intentionally omitted + Address: sample.AccAddress(), + Service: sharedtypes.Service{Name: "service name"}, // ID intentionally omitted }, expectedErr: ErrServiceMissingID, }, { - desc: "valid supplier address - no service name", + desc: "valid service supplier address - no service name", msg: MsgAddService{ - SupplierAddress: sample.AccAddress(), - Service: sharedtypes.Service{Id: "srv1"}, // Name intentionally omitted + Address: sample.AccAddress(), + Service: sharedtypes.Service{Id: "srv1"}, // Name intentionally omitted }, expectedErr: ErrServiceMissingName, }, { - desc: "valid supplier address and service", + desc: "valid service supplier address and service", msg: MsgAddService{ - SupplierAddress: sample.AccAddress(), - Service: sharedtypes.Service{Id: "srv1", Name: "service name"}, + Address: sample.AccAddress(), + Service: sharedtypes.Service{Id: "srv1", Name: "service name"}, }, + expectedErr: nil, }, } for _, tt := range tests { From f651a49dfa40e2807cc541c6a5f260b459514fa7 Mon Sep 17 00:00:00 2001 From: h5law Date: Thu, 18 Jan 2024 17:33:09 +0000 Subject: [PATCH 16/22] chore: ignite chain build --- docs/static/openapi.yml | 10 ---------- go.mod | 4 ++-- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/docs/static/openapi.yml b/docs/static/openapi.yml index 3b94cb82a..af8b41430 100644 --- a/docs/static/openapi.yml +++ b/docs/static/openapi.yml @@ -47279,14 +47279,12 @@ paths: - GRPC - WEBSOCKET - JSON_RPC - - REST default: UNKNOWN_RPC description: |- - UNKNOWN_RPC: Undefined RPC type - GRPC: gRPC - WEBSOCKET: WebSocket - JSON_RPC: JSON-RPC - - REST: REST configs: type: array items: @@ -77807,14 +77805,12 @@ definitions: - GRPC - WEBSOCKET - JSON_RPC - - REST default: UNKNOWN_RPC description: |- - UNKNOWN_RPC: Undefined RPC type - GRPC: gRPC - WEBSOCKET: WebSocket - JSON_RPC: JSON-RPC - - REST: REST configs: type: array items: @@ -78061,14 +78057,12 @@ definitions: - GRPC - WEBSOCKET - JSON_RPC - - REST default: UNKNOWN_RPC description: |- - UNKNOWN_RPC: Undefined RPC type - GRPC: gRPC - WEBSOCKET: WebSocket - JSON_RPC: JSON-RPC - - REST: REST configs: type: array items: @@ -78215,7 +78209,6 @@ definitions: - GRPC: gRPC - WEBSOCKET: WebSocket - JSON_RPC: JSON-RPC - - REST: REST title: Enum to define RPC types pocket.shared.Supplier: type: object @@ -78284,7 +78277,6 @@ definitions: - GRPC: gRPC - WEBSOCKET: WebSocket - JSON_RPC: JSON-RPC - - REST: REST configs: type: array items: @@ -78343,7 +78335,6 @@ definitions: - GRPC: gRPC - WEBSOCKET: WebSocket - JSON_RPC: JSON-RPC - - REST: REST configs: type: array items: @@ -78416,7 +78407,6 @@ definitions: - GRPC: gRPC - WEBSOCKET: WebSocket - JSON_RPC: JSON-RPC - - REST: REST configs: type: array items: diff --git a/go.mod b/go.mod index 9be97f1d6..56ed2d4fc 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( github.com/athanorlabs/go-dleq v0.1.0 github.com/cometbft/cometbft v0.37.2 github.com/cometbft/cometbft-db v0.8.0 + github.com/cosmos/cosmos-proto v1.0.0-beta.2 github.com/cosmos/cosmos-sdk v0.47.3 github.com/cosmos/gogoproto v1.4.11 github.com/cosmos/ibc-go/v7 v7.1.0 @@ -47,6 +48,7 @@ require ( golang.org/x/crypto v0.15.0 golang.org/x/exp v0.0.0-20230905200255-921286631fa9 golang.org/x/sync v0.5.0 + google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb google.golang.org/grpc v1.59.0 gopkg.in/yaml.v2 v2.4.0 ) @@ -90,7 +92,6 @@ require ( github.com/containerd/cgroups v1.1.0 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/cosmos/btcutil v1.0.5 // indirect - github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect github.com/cosmos/go-bip39 v1.0.0 // indirect github.com/cosmos/gogogateway v1.2.0 // indirect github.com/cosmos/iavl v0.20.0 // indirect @@ -285,7 +286,6 @@ require ( google.golang.org/api v0.143.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto v0.0.0-20230913181813-007df8e322eb // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 // indirect google.golang.org/protobuf v1.31.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect From 031956fc36e1e8561378950da9526b95b5462d67 Mon Sep 17 00:00:00 2001 From: h5law Date: Fri, 19 Jan 2024 13:43:40 +0000 Subject: [PATCH 17/22] chore: address comments --- x/service/client/cli/helpers_test.go | 34 ------------------ x/service/client/cli/tx_add_service.go | 2 +- x/service/client/cli/tx_add_service_test.go | 38 ++++++++++++--------- x/service/keeper/msg_server_add_service.go | 2 +- x/service/keeper/service.go | 30 ++++++++-------- x/service/keeper/service_test.go | 32 ++++++++--------- x/service/simulation/add_service.go | 4 +-- x/service/types/errors.go | 4 +-- x/service/types/genesis_test.go | 16 ++++----- x/service/types/message_add_service.go | 1 + 10 files changed, 67 insertions(+), 96 deletions(-) delete mode 100644 x/service/client/cli/helpers_test.go diff --git a/x/service/client/cli/helpers_test.go b/x/service/client/cli/helpers_test.go deleted file mode 100644 index d6066c28a..000000000 --- a/x/service/client/cli/helpers_test.go +++ /dev/null @@ -1,34 +0,0 @@ -// Package cli_test provides unit tests for the CLI functionality. -package cli_test - -import ( - "strconv" - "testing" - - "github.com/stretchr/testify/require" - - "github.com/pokt-network/poktroll/cmd/pocketd/cmd" - "github.com/pokt-network/poktroll/testutil/network" - sharedtypes "github.com/pokt-network/poktroll/x/shared/types" - "github.com/pokt-network/poktroll/x/supplier/types" -) - -// Dummy variable to avoid unused import error. -var _ = strconv.IntSize - -// init initializes the SDK configuration. -func init() { - cmd.InitSDKConfig() -} - -// networkWithSupplierObjects creates a new network with a given number of supplier objects. -// It returns the network and a slice of the created supplier objects. -func networkWithSupplierObjects(t *testing.T, n int) (*network.Network, []sharedtypes.Supplier) { - t.Helper() - cfg := network.DefaultConfig() - supplierGenesisState := network.DefaultSupplierModuleGenesisState(t, n) - buf, err := cfg.Codec.MarshalJSON(supplierGenesisState) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), supplierGenesisState.SupplierList -} diff --git a/x/service/client/cli/tx_add_service.go b/x/service/client/cli/tx_add_service.go index a1a4cd1b2..559a31eae 100644 --- a/x/service/client/cli/tx_add_service.go +++ b/x/service/client/cli/tx_add_service.go @@ -22,7 +22,7 @@ gateways and suppliers to use. The service id MUST be unique - or the command will fail, however the name you use to describe it does not have to be unique. Example: -$ poktrolld --home=$(POKTROLLD_HOME) tx service add-service "srv1" "service_one" --keyring-backend test --from $(SUPPLIER) --node $(POCKET_NODE)`, +$ poktrolld tx service add-service "srv1" "service_one" --keyring-backend test --from $(SUPPLIER) --node $(POCKET_NODE) --home=$(POKTROLLD_HOME)`, Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) (err error) { serviceIdStr := args[0] diff --git a/x/service/client/cli/tx_add_service_test.go b/x/service/client/cli/tx_add_service_test.go index 45c42c9b8..38cdf9983 100644 --- a/x/service/client/cli/tx_add_service_test.go +++ b/x/service/client/cli/tx_add_service_test.go @@ -20,14 +20,14 @@ import ( ) func TestCLI_AddService(t *testing.T) { - net, _ := networkWithSupplierObjects(t, 1) + net := network.New(t, network.DefaultConfig()) val := net.Validators[0] ctx := val.ClientCtx - // Create a keyring and add an account for the supplier adding the service + // Create a keyring and add an account for the address adding the service kr := ctx.Keyring accounts := testutil.CreateKeyringAccounts(t, kr, 1) - supplierAccount := accounts[0] + account := accounts[0] // Update the context with the new keyring ctx = ctx.WithKeyring(kr) @@ -36,30 +36,34 @@ func TestCLI_AddService(t *testing.T) { commonArgs := []string{ fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()), + fmt.Sprintf( + "--%s=%s", + flags.FlagFees, + sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String(), + ), } // Initialize the Supplier account by sending it some funds from the // validator account that is part of genesis - network.InitAccountWithSequence(t, net, supplierAccount.Address, 1) + network.InitAccountWithSequence(t, net, account.Address, 1) // Wait for a new block to be committed require.NoError(t, net.WaitForNextBlock()) // Prepare two valid services - srv1 := sharedtypes.Service{ + svc1 := sharedtypes.Service{ Id: "srv1", Name: "service name", } - srv2 := sharedtypes.Service{ + svc2 := sharedtypes.Service{ Id: "srv2", Name: "service name 2", } // Add srv2 to the network args := []string{ - srv2.Id, - srv2.Name, - fmt.Sprintf("--%s=%s", flags.FlagFrom, supplierAccount.Address.String()), + svc2.Id, + svc2.Name, + fmt.Sprintf("--%s=%s", flags.FlagFrom, account.Address.String()), } args = append(args, commonArgs...) _, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdAddService(), args) @@ -73,31 +77,31 @@ func TestCLI_AddService(t *testing.T) { }{ { desc: "valid - add new service", - supplierAddress: supplierAccount.Address.String(), - service: srv1, + supplierAddress: account.Address.String(), + service: svc1, }, { desc: "invalid - missing service id", - supplierAddress: supplierAccount.Address.String(), + supplierAddress: account.Address.String(), service: sharedtypes.Service{Name: "service name"}, // ID intentionally omitted err: types.ErrServiceMissingID, }, { desc: "invalid - missing service name", - supplierAddress: supplierAccount.Address.String(), + supplierAddress: account.Address.String(), service: sharedtypes.Service{Id: "srv1"}, // Name intentionally omitted err: types.ErrServiceMissingName, }, { desc: "invalid - invalid supplier address", supplierAddress: "invalid address", - service: srv1, + service: svc1, err: types.ErrServiceInvalidAddress, }, { desc: "invalid - service already staked", - supplierAddress: supplierAccount.Address.String(), - service: srv2, + supplierAddress: account.Address.String(), + service: svc2, err: types.ErrServiceAlreadyExists, }, } diff --git a/x/service/keeper/msg_server_add_service.go b/x/service/keeper/msg_server_add_service.go index 11737a559..b7c75bd58 100644 --- a/x/service/keeper/msg_server_add_service.go +++ b/x/service/keeper/msg_server_add_service.go @@ -11,7 +11,7 @@ import ( // AddService handles MsgAddService and adds a service to the network storing // it in the service keeper's store using the provided ID from the message. -// TODO(@h5law): Add a governance parameter defining the cost to add a service +// TODO(#337): Add a governance parameter defining the cost to add a service // and enforce it then send the fee to the service module, if the signer has // enough funds otherwise fail. func (k msgServer) AddService( diff --git a/x/service/keeper/service.go b/x/service/keeper/service.go index ec5a1e7ad..dd7b860f0 100644 --- a/x/service/keeper/service.go +++ b/x/service/keeper/service.go @@ -14,44 +14,44 @@ func (k Keeper) SetService(ctx sdk.Context, service sharedtypes.Service) { ctx.KVStore(k.storeKey), types.KeyPrefix(types.ServiceKeyPrefix), ) - b := k.cdc.MustMarshal(&service) + serviceBz := k.cdc.MustMarshal(&service) store.Set(types.ServiceKey( service.Id, - ), b) + ), serviceBz) } // GetService returns a service from the store by its index. func (k Keeper) GetService( ctx sdk.Context, - id string, -) (val sharedtypes.Service, found bool) { + serviceID string, +) (service sharedtypes.Service, found bool) { store := prefix.NewStore( ctx.KVStore(k.storeKey), types.KeyPrefix(types.ServiceKeyPrefix), ) - b := store.Get(types.ServiceKey( - id, + serviceBz := store.Get(types.ServiceKey( + serviceID, )) - if b == nil { - return val, false + if serviceBz == nil { + return service, false } - k.cdc.MustUnmarshal(b, &val) - return val, true + k.cdc.MustUnmarshal(serviceBz, &service) + return service, true } // RemoveService removes a service from the store. func (k Keeper) RemoveService( ctx sdk.Context, - address string, + serviceID string, ) { store := prefix.NewStore( ctx.KVStore(k.storeKey), types.KeyPrefix(types.ServiceKeyPrefix), ) store.Delete(types.ServiceKey( - address, + serviceID, )) } @@ -66,9 +66,9 @@ func (k Keeper) GetAllServices(ctx sdk.Context) (list []sharedtypes.Service) { defer iterator.Close() for ; iterator.Valid(); iterator.Next() { - val := sharedtypes.Service{} - k.cdc.MustUnmarshal(iterator.Value(), &val) - list = append(list, val) + service := sharedtypes.Service{} + k.cdc.MustUnmarshal(iterator.Value(), &service) + list = append(list, service) } return diff --git a/x/service/keeper/service_test.go b/x/service/keeper/service_test.go index 38be8deda..b9e6db544 100644 --- a/x/service/keeper/service_test.go +++ b/x/service/keeper/service_test.go @@ -25,14 +25,14 @@ func init() { } func createNservices(keeper *keeper.Keeper, ctx sdk.Context, n int) []sharedtypes.Service { - items := make([]sharedtypes.Service, n) - for i := range items { - items[i].Id = fmt.Sprintf("srv%d", i) - items[i].Name = fmt.Sprintf("srv%d", i) + services := make([]sharedtypes.Service, n) + for i := range services { + services[i].Id = fmt.Sprintf("srvId%d", i) + services[i].Name = fmt.Sprintf("srvName%d", i) - keeper.SetService(ctx, items[i]) + keeper.SetService(ctx, services[i]) } - return items + return services } func TestServiceModuleAddress(t *testing.T) { @@ -42,28 +42,28 @@ func TestServiceModuleAddress(t *testing.T) { func TestServiceGet(t *testing.T) { keeper, ctx := keepertest.ServiceKeeper(t) - items := createNservices(keeper, ctx, 10) - for _, item := range items { - rst, found := keeper.GetService(ctx, + services := createNservices(keeper, ctx, 10) + for _, item := range services { + service, found := keeper.GetService(ctx, item.Id, ) require.True(t, found) require.Equal(t, nullify.Fill(&item), - nullify.Fill(&rst), + nullify.Fill(&service), ) } } func TestServiceRemove(t *testing.T) { keeper, ctx := keepertest.ServiceKeeper(t) - items := createNservices(keeper, ctx, 10) - for _, item := range items { + services := createNservices(keeper, ctx, 10) + for _, service := range services { keeper.RemoveService(ctx, - item.Id, + service.Id, ) _, found := keeper.GetService(ctx, - item.Id, + service.Id, ) require.False(t, found) } @@ -71,9 +71,9 @@ func TestServiceRemove(t *testing.T) { func TestServiceGetAll(t *testing.T) { keeper, ctx := keepertest.ServiceKeeper(t) - items := createNservices(keeper, ctx, 10) + services := createNservices(keeper, ctx, 10) require.ElementsMatch(t, - nullify.Fill(items), + nullify.Fill(services), nullify.Fill(keeper.GetAllServices(ctx)), ) } diff --git a/x/service/simulation/add_service.go b/x/service/simulation/add_service.go index 76188162f..dd6d8717c 100644 --- a/x/service/simulation/add_service.go +++ b/x/service/simulation/add_service.go @@ -25,8 +25,8 @@ func SimulateMsgAddService( msg := &types.MsgAddService{ Address: simAccount.Address.String(), Service: sharedtypes.Service{ - Id: fmt.Sprintf("srv%d", rndNum), - Name: fmt.Sprintf("service %d", rndNum), + Id: fmt.Sprintf("srvId%d", rndNum), + Name: fmt.Sprintf("servName%d", rndNum), }, } diff --git a/x/service/types/errors.go b/x/service/types/errors.go index 9278abf38..a3742d8b5 100644 --- a/x/service/types/errors.go +++ b/x/service/types/errors.go @@ -5,8 +5,8 @@ import ( ) var ( - ErrServiceDuplicateIndex = sdkerrors.Register(ModuleName, 1, "duplicate index") - ErrServiceInvalidAddress = sdkerrors.Register(ModuleName, 2, "invalid address") + ErrServiceDuplicateIndex = sdkerrors.Register(ModuleName, 1, "duplicate index when adding a new service") + ErrServiceInvalidAddress = sdkerrors.Register(ModuleName, 2, "invalid address when adding a new service") ErrServiceMissingID = sdkerrors.Register(ModuleName, 3, "missing service ID") ErrServiceMissingName = sdkerrors.Register(ModuleName, 4, "missing service name") ErrServiceAlreadyExists = sdkerrors.Register(ModuleName, 5, "service already exists") diff --git a/x/service/types/genesis_test.go b/x/service/types/genesis_test.go index c7af5fd1a..c28b70996 100644 --- a/x/service/types/genesis_test.go +++ b/x/service/types/genesis_test.go @@ -10,14 +10,14 @@ import ( ) func TestGenesisState_Validate(t *testing.T) { - srv1 := &sharedtypes.Service{ - Id: "srv1", - Name: "srv1", + svc1 := &sharedtypes.Service{ + Id: "srvId1", + Name: "srvName1", } - srv2 := &sharedtypes.Service{ - Id: "srv2", - Name: "srv2", + svc2 := &sharedtypes.Service{ + Id: "srvId2", + Name: "srvName2", } tests := []struct { @@ -34,7 +34,7 @@ func TestGenesisState_Validate(t *testing.T) { desc: "valid genesis state", genState: &types.GenesisState{ ServiceList: []sharedtypes.Service{ - *srv1, *srv2, + *svc1, *svc2, }, // this line is used by starport scaffolding # types/genesis/validField }, @@ -44,7 +44,7 @@ func TestGenesisState_Validate(t *testing.T) { desc: "invalid - duplicate service ID", genState: &types.GenesisState{ ServiceList: []sharedtypes.Service{ - *srv1, *srv1, + *svc1, *svc1, }, }, valid: false, diff --git a/x/service/types/message_add_service.go b/x/service/types/message_add_service.go index 73a089189..571026243 100644 --- a/x/service/types/message_add_service.go +++ b/x/service/types/message_add_service.go @@ -53,6 +53,7 @@ func (msg *MsgAddService) ValidateBasic() error { "invalid supplier address %s; (%v)", msg.Address, err, ) } + // TODO_TECHDEBT: Add a validate basic function to the `Service` object if msg.Service.Id == "" { return ErrServiceMissingID } From bcaf2fac8c4187413d2ce6e4c65bb080850bd775 Mon Sep 17 00:00:00 2001 From: Daniel Olshansky Date: Fri, 19 Jan 2024 11:26:31 -0500 Subject: [PATCH 18/22] Ran make go_lint && make go_imports --- go.mod | 4 ++-- pkg/relayer/proxy/relay_verifier.go | 1 + x/service/client/cli/tx_add_service.go | 2 +- x/service/client/cli/tx_add_service_test.go | 13 ++++++------- x/service/keeper/service_test.go | 19 +++++++++---------- x/service/simulation/add_service.go | 2 +- x/service/types/genesis_test.go | 8 ++++---- x/service/types/message_add_service_test.go | 4 ++-- 8 files changed, 26 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index 56ed2d4fc..9be97f1d6 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,6 @@ require ( github.com/athanorlabs/go-dleq v0.1.0 github.com/cometbft/cometbft v0.37.2 github.com/cometbft/cometbft-db v0.8.0 - github.com/cosmos/cosmos-proto v1.0.0-beta.2 github.com/cosmos/cosmos-sdk v0.47.3 github.com/cosmos/gogoproto v1.4.11 github.com/cosmos/ibc-go/v7 v7.1.0 @@ -48,7 +47,6 @@ require ( golang.org/x/crypto v0.15.0 golang.org/x/exp v0.0.0-20230905200255-921286631fa9 golang.org/x/sync v0.5.0 - google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb google.golang.org/grpc v1.59.0 gopkg.in/yaml.v2 v2.4.0 ) @@ -92,6 +90,7 @@ require ( github.com/containerd/cgroups v1.1.0 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/cosmos/btcutil v1.0.5 // indirect + github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect github.com/cosmos/go-bip39 v1.0.0 // indirect github.com/cosmos/gogogateway v1.2.0 // indirect github.com/cosmos/iavl v0.20.0 // indirect @@ -286,6 +285,7 @@ require ( google.golang.org/api v0.143.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto v0.0.0-20230913181813-007df8e322eb // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 // indirect google.golang.org/protobuf v1.31.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect diff --git a/pkg/relayer/proxy/relay_verifier.go b/pkg/relayer/proxy/relay_verifier.go index f33c59454..ce48e055e 100644 --- a/pkg/relayer/proxy/relay_verifier.go +++ b/pkg/relayer/proxy/relay_verifier.go @@ -6,6 +6,7 @@ import ( sdkerrors "cosmossdk.io/errors" ring_secp256k1 "github.com/athanorlabs/go-dleq/secp256k1" ring "github.com/noot/ring-go" + "github.com/pokt-network/poktroll/x/service/types" sharedtypes "github.com/pokt-network/poktroll/x/shared/types" ) diff --git a/x/service/client/cli/tx_add_service.go b/x/service/client/cli/tx_add_service.go index 559a31eae..af8e4861e 100644 --- a/x/service/client/cli/tx_add_service.go +++ b/x/service/client/cli/tx_add_service.go @@ -22,7 +22,7 @@ gateways and suppliers to use. The service id MUST be unique - or the command will fail, however the name you use to describe it does not have to be unique. Example: -$ poktrolld tx service add-service "srv1" "service_one" --keyring-backend test --from $(SUPPLIER) --node $(POCKET_NODE) --home=$(POKTROLLD_HOME)`, +$ poktrolld tx service add-service "svc1" "service_one" --keyring-backend test --from $(SUPPLIER) --node $(POCKET_NODE) --home=$(POKTROLLD_HOME)`, Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) (err error) { serviceIdStr := args[0] diff --git a/x/service/client/cli/tx_add_service_test.go b/x/service/client/cli/tx_add_service_test.go index 38cdf9983..00a0cdc8d 100644 --- a/x/service/client/cli/tx_add_service_test.go +++ b/x/service/client/cli/tx_add_service_test.go @@ -10,13 +10,12 @@ import ( "github.com/cosmos/cosmos-sdk/testutil" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/stretchr/testify/require" - "google.golang.org/grpc/status" - "github.com/pokt-network/poktroll/testutil/network" "github.com/pokt-network/poktroll/x/service/client/cli" "github.com/pokt-network/poktroll/x/service/types" sharedtypes "github.com/pokt-network/poktroll/x/shared/types" + "github.com/stretchr/testify/require" + "google.golang.org/grpc/status" ) func TestCLI_AddService(t *testing.T) { @@ -52,14 +51,14 @@ func TestCLI_AddService(t *testing.T) { // Prepare two valid services svc1 := sharedtypes.Service{ - Id: "srv1", + Id: "svc1", Name: "service name", } svc2 := sharedtypes.Service{ - Id: "srv2", + Id: "svc2", Name: "service name 2", } - // Add srv2 to the network + // Add svc2 to the network args := []string{ svc2.Id, svc2.Name, @@ -89,7 +88,7 @@ func TestCLI_AddService(t *testing.T) { { desc: "invalid - missing service name", supplierAddress: account.Address.String(), - service: sharedtypes.Service{Id: "srv1"}, // Name intentionally omitted + service: sharedtypes.Service{Id: "svc1"}, // Name intentionally omitted err: types.ErrServiceMissingName, }, { diff --git a/x/service/keeper/service_test.go b/x/service/keeper/service_test.go index b9e6db544..203e0d524 100644 --- a/x/service/keeper/service_test.go +++ b/x/service/keeper/service_test.go @@ -7,14 +7,13 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - "github.com/stretchr/testify/require" - "github.com/pokt-network/poktroll/cmd/pocketd/cmd" keepertest "github.com/pokt-network/poktroll/testutil/keeper" "github.com/pokt-network/poktroll/testutil/nullify" "github.com/pokt-network/poktroll/x/service/keeper" "github.com/pokt-network/poktroll/x/service/types" sharedtypes "github.com/pokt-network/poktroll/x/shared/types" + "github.com/stretchr/testify/require" ) // Prevent strconv unused error @@ -24,11 +23,11 @@ func init() { cmd.InitSDKConfig() } -func createNservices(keeper *keeper.Keeper, ctx sdk.Context, n int) []sharedtypes.Service { +func createNServices(keeper *keeper.Keeper, ctx sdk.Context, n int) []sharedtypes.Service { services := make([]sharedtypes.Service, n) for i := range services { services[i].Id = fmt.Sprintf("srvId%d", i) - services[i].Name = fmt.Sprintf("srvName%d", i) + services[i].Name = fmt.Sprintf("svcName%d", i) keeper.SetService(ctx, services[i]) } @@ -42,14 +41,14 @@ func TestServiceModuleAddress(t *testing.T) { func TestServiceGet(t *testing.T) { keeper, ctx := keepertest.ServiceKeeper(t) - services := createNservices(keeper, ctx, 10) - for _, item := range services { + services := createNServices(keeper, ctx, 10) + for _, service := range services { service, found := keeper.GetService(ctx, - item.Id, + service.Id, ) require.True(t, found) require.Equal(t, - nullify.Fill(&item), + nullify.Fill(&service), nullify.Fill(&service), ) } @@ -57,7 +56,7 @@ func TestServiceGet(t *testing.T) { func TestServiceRemove(t *testing.T) { keeper, ctx := keepertest.ServiceKeeper(t) - services := createNservices(keeper, ctx, 10) + services := createNServices(keeper, ctx, 10) for _, service := range services { keeper.RemoveService(ctx, service.Id, @@ -71,7 +70,7 @@ func TestServiceRemove(t *testing.T) { func TestServiceGetAll(t *testing.T) { keeper, ctx := keepertest.ServiceKeeper(t) - services := createNservices(keeper, ctx, 10) + services := createNServices(keeper, ctx, 10) require.ElementsMatch(t, nullify.Fill(services), nullify.Fill(keeper.GetAllServices(ctx)), diff --git a/x/service/simulation/add_service.go b/x/service/simulation/add_service.go index dd6d8717c..00dbdcaa7 100644 --- a/x/service/simulation/add_service.go +++ b/x/service/simulation/add_service.go @@ -26,7 +26,7 @@ func SimulateMsgAddService( Address: simAccount.Address.String(), Service: sharedtypes.Service{ Id: fmt.Sprintf("srvId%d", rndNum), - Name: fmt.Sprintf("servName%d", rndNum), + Name: fmt.Sprintf("svcName%d", rndNum), }, } diff --git a/x/service/types/genesis_test.go b/x/service/types/genesis_test.go index c28b70996..1dba82ad3 100644 --- a/x/service/types/genesis_test.go +++ b/x/service/types/genesis_test.go @@ -11,13 +11,13 @@ import ( func TestGenesisState_Validate(t *testing.T) { svc1 := &sharedtypes.Service{ - Id: "srvId1", - Name: "srvName1", + Id: "svcId1", + Name: "svcName1", } svc2 := &sharedtypes.Service{ - Id: "srvId2", - Name: "srvName2", + Id: "svcId2", + Name: "svcName2", } tests := []struct { diff --git a/x/service/types/message_add_service_test.go b/x/service/types/message_add_service_test.go index be94b9d03..dfcc674e0 100644 --- a/x/service/types/message_add_service_test.go +++ b/x/service/types/message_add_service_test.go @@ -33,14 +33,14 @@ func TestMsgAddService_ValidateBasic(t *testing.T) { desc: "valid service supplier address - no service name", msg: MsgAddService{ Address: sample.AccAddress(), - Service: sharedtypes.Service{Id: "srv1"}, // Name intentionally omitted + Service: sharedtypes.Service{Id: "svc1"}, // Name intentionally omitted }, expectedErr: ErrServiceMissingName, }, { desc: "valid service supplier address and service", msg: MsgAddService{ Address: sample.AccAddress(), - Service: sharedtypes.Service{Id: "srv1", Name: "service name"}, + Service: sharedtypes.Service{Id: "svc1", Name: "service name"}, }, expectedErr: nil, }, From c3e842b6b2ff4d3b1d83ce9d4d32fadd15dbb141 Mon Sep 17 00:00:00 2001 From: Daniel Olshansky Date: Fri, 19 Jan 2024 11:32:17 -0500 Subject: [PATCH 19/22] A few minor nits --- x/service/keeper/service_test.go | 2 +- x/service/simulation/add_service.go | 3 +-- x/shared/helpers/service_test.go | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/x/service/keeper/service_test.go b/x/service/keeper/service_test.go index 203e0d524..63a470b21 100644 --- a/x/service/keeper/service_test.go +++ b/x/service/keeper/service_test.go @@ -26,7 +26,7 @@ func init() { func createNServices(keeper *keeper.Keeper, ctx sdk.Context, n int) []sharedtypes.Service { services := make([]sharedtypes.Service, n) for i := range services { - services[i].Id = fmt.Sprintf("srvId%d", i) + services[i].Id = fmt.Sprintf("svcId%d", i) services[i].Name = fmt.Sprintf("svcName%d", i) keeper.SetService(ctx, services[i]) diff --git a/x/service/simulation/add_service.go b/x/service/simulation/add_service.go index 00dbdcaa7..26fc06048 100644 --- a/x/service/simulation/add_service.go +++ b/x/service/simulation/add_service.go @@ -7,7 +7,6 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" - "github.com/pokt-network/poktroll/x/service/keeper" "github.com/pokt-network/poktroll/x/service/types" sharedtypes "github.com/pokt-network/poktroll/x/shared/types" @@ -25,7 +24,7 @@ func SimulateMsgAddService( msg := &types.MsgAddService{ Address: simAccount.Address.String(), Service: sharedtypes.Service{ - Id: fmt.Sprintf("srvId%d", rndNum), + Id: fmt.Sprintf("svcId%d", rndNum), Name: fmt.Sprintf("svcName%d", rndNum), }, } diff --git a/x/shared/helpers/service_test.go b/x/shared/helpers/service_test.go index d74b77afa..e3dcf57d6 100644 --- a/x/shared/helpers/service_test.go +++ b/x/shared/helpers/service_test.go @@ -28,7 +28,7 @@ func TestIsValidService(t *testing.T) { { desc: "Valid ID and empty Name", - serviceId: "Srv", + serviceId: "svc", serviceName: "", // Valid because the service name can be empty expectedIsValid: true, From d7678dca7841b21479eb8c4eacecadb7d495b3df Mon Sep 17 00:00:00 2001 From: Daniel Olshansky Date: Fri, 19 Jan 2024 14:52:23 -0500 Subject: [PATCH 20/22] Ran make go_lint && make go_imports --- x/service/client/cli/tx_add_service.go | 3 ++- x/service/client/cli/tx_add_service_test.go | 4 +--- x/service/simulation/add_service.go | 1 + 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/x/service/client/cli/tx_add_service.go b/x/service/client/cli/tx_add_service.go index 45af6056d..af8e4861e 100644 --- a/x/service/client/cli/tx_add_service.go +++ b/x/service/client/cli/tx_add_service.go @@ -6,8 +6,9 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/tx" - "github.com/pokt-network/poktroll/x/service/types" "github.com/spf13/cobra" + + "github.com/pokt-network/poktroll/x/service/types" ) var _ = strconv.Itoa(0) diff --git a/x/service/client/cli/tx_add_service_test.go b/x/service/client/cli/tx_add_service_test.go index ab9cd2320..00a0cdc8d 100644 --- a/x/service/client/cli/tx_add_service_test.go +++ b/x/service/client/cli/tx_add_service_test.go @@ -10,12 +10,10 @@ import ( "github.com/cosmos/cosmos-sdk/testutil" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/stretchr/testify/require" - "google.golang.org/grpc/status" "github.com/pokt-network/poktroll/testutil/network" "github.com/pokt-network/poktroll/x/service/client/cli" "github.com/pokt-network/poktroll/x/service/types" - sharedtypes "github.com/pokt-network/poktroll/x/shared/types + sharedtypes "github.com/pokt-network/poktroll/x/shared/types" "github.com/stretchr/testify/require" "google.golang.org/grpc/status" ) diff --git a/x/service/simulation/add_service.go b/x/service/simulation/add_service.go index 26fc06048..8462edcd2 100644 --- a/x/service/simulation/add_service.go +++ b/x/service/simulation/add_service.go @@ -7,6 +7,7 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/pokt-network/poktroll/x/service/keeper" "github.com/pokt-network/poktroll/x/service/types" sharedtypes "github.com/pokt-network/poktroll/x/shared/types" From ea72e9782fc59ab960e4fbd4dfab3bbed79c2eca Mon Sep 17 00:00:00 2001 From: Daniel Olshansky Date: Fri, 19 Jan 2024 15:00:58 -0500 Subject: [PATCH 21/22] Fix imports --- x/service/client/cli/tx_add_service_test.go | 5 +++-- x/service/keeper/service_test.go | 3 ++- x/service/types/message_add_service_test.go | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/x/service/client/cli/tx_add_service_test.go b/x/service/client/cli/tx_add_service_test.go index 00a0cdc8d..b74d56b79 100644 --- a/x/service/client/cli/tx_add_service_test.go +++ b/x/service/client/cli/tx_add_service_test.go @@ -10,12 +10,13 @@ import ( "github.com/cosmos/cosmos-sdk/testutil" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" + "google.golang.org/grpc/status" + "github.com/pokt-network/poktroll/testutil/network" "github.com/pokt-network/poktroll/x/service/client/cli" "github.com/pokt-network/poktroll/x/service/types" sharedtypes "github.com/pokt-network/poktroll/x/shared/types" - "github.com/stretchr/testify/require" - "google.golang.org/grpc/status" ) func TestCLI_AddService(t *testing.T) { diff --git a/x/service/keeper/service_test.go b/x/service/keeper/service_test.go index 63a470b21..2e7d3e763 100644 --- a/x/service/keeper/service_test.go +++ b/x/service/keeper/service_test.go @@ -7,13 +7,14 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/stretchr/testify/require" + "github.com/pokt-network/poktroll/cmd/pocketd/cmd" keepertest "github.com/pokt-network/poktroll/testutil/keeper" "github.com/pokt-network/poktroll/testutil/nullify" "github.com/pokt-network/poktroll/x/service/keeper" "github.com/pokt-network/poktroll/x/service/types" sharedtypes "github.com/pokt-network/poktroll/x/shared/types" - "github.com/stretchr/testify/require" ) // Prevent strconv unused error diff --git a/x/service/types/message_add_service_test.go b/x/service/types/message_add_service_test.go index 501114297..dfcc674e0 100644 --- a/x/service/types/message_add_service_test.go +++ b/x/service/types/message_add_service_test.go @@ -3,9 +3,10 @@ package types import ( "testing" + "github.com/stretchr/testify/require" + "github.com/pokt-network/poktroll/testutil/sample" sharedtypes "github.com/pokt-network/poktroll/x/shared/types" - "github.com/stretchr/testify/require" ) func TestMsgAddService_ValidateBasic(t *testing.T) { From 27b890edb6c62d111d802982609b4d15541466c6 Mon Sep 17 00:00:00 2001 From: Daniel Olshansky Date: Fri, 19 Jan 2024 15:01:56 -0500 Subject: [PATCH 22/22] Fix imports --- x/service/types/genesis_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/service/types/genesis_test.go b/x/service/types/genesis_test.go index fc385bc61..1dba82ad3 100644 --- a/x/service/types/genesis_test.go +++ b/x/service/types/genesis_test.go @@ -3,9 +3,10 @@ package types_test import ( "testing" + "github.com/stretchr/testify/require" + "github.com/pokt-network/poktroll/x/service/types" sharedtypes "github.com/pokt-network/poktroll/x/shared/types" - "github.com/stretchr/testify/require" ) func TestGenesisState_Validate(t *testing.T) {