From 608104c80b4475de7a34c65dadcb17b273084922 Mon Sep 17 00:00:00 2001 From: Alexandr Kolesov Date: Wed, 15 Dec 2021 11:50:35 +0300 Subject: [PATCH 1/3] Get gid of prefix store in configs --- x/cheqd/keeper/keeper_config.go | 7 ++----- x/cheqd/types/codec.go | 2 ++ x/cheqd/types/keys.go | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/x/cheqd/keeper/keeper_config.go b/x/cheqd/keeper/keeper_config.go index 1642ced99..7ae7da5e0 100644 --- a/x/cheqd/keeper/keeper_config.go +++ b/x/cheqd/keeper/keeper_config.go @@ -2,14 +2,12 @@ package keeper import ( "github.com/cheqd/cheqd-node/x/cheqd/types" - "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" ) // GetDidNamespace get the total number of did func (k Keeper) GetDidNamespace(ctx sdk.Context) string { - // FIXME: This leads to double prefix. Should be just KVStore. Migration is needed to fix. - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DidNamespaceKey)) + store := ctx.KVStore(k.storeKey) byteKey := types.KeyPrefix(types.DidNamespaceKey) bz := store.Get(byteKey) @@ -20,8 +18,7 @@ func (k Keeper) GetDidNamespace(ctx sdk.Context) string { // SetDidNamespace set did namespace func (k Keeper) SetDidNamespace(ctx sdk.Context, namespace string) { - // FIXME: This leads to double prefix. Should be just KVStore. Migration is needed to fix. - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DidNamespaceKey)) + store := ctx.KVStore(k.storeKey) byteKey := types.KeyPrefix(types.DidNamespaceKey) bz := []byte(namespace) diff --git a/x/cheqd/types/codec.go b/x/cheqd/types/codec.go index 69cf1c3e2..d98e5ff9d 100644 --- a/x/cheqd/types/codec.go +++ b/x/cheqd/types/codec.go @@ -7,6 +7,8 @@ import ( "github.com/cosmos/cosmos-sdk/types/msgservice" ) +// FIXME: Double check that registration is correct + func RegisterCodec(cdc *codec.LegacyAmino) { // this line is used by starport scaffolding # 2 cdc.RegisterConcrete(&MsgCreateDid{}, "cheqd/CreateDid", nil) diff --git a/x/cheqd/types/keys.go b/x/cheqd/types/keys.go index 5604b50bf..df9b64624 100644 --- a/x/cheqd/types/keys.go +++ b/x/cheqd/types/keys.go @@ -27,5 +27,5 @@ const ( DidCountKey = "did-count:" // DidNamespaceKey FIXME: Should be `did-namespace:`. // Networks was started with `testnet` value so we need a migration now. - DidNamespaceKey = "testnet" + DidNamespaceKey = "testnettestnet" ) From a6f498f3de8e5d8a841f3a2e8399d58b30fe6ec7 Mon Sep 17 00:00:00 2001 From: Alexandr Kolesov Date: Wed, 15 Dec 2021 12:52:17 +0300 Subject: [PATCH 2/3] Optimize type registration --- x/cheqd/types/codec.go | 7 ------- x/cheqd/types/genesis.go | 4 ++-- x/cheqd/types/stateValue.go | 6 +----- x/cheqd/types/utils.go | 8 ++++++++ x/cheqd/types/utils_test.go | 10 ++++++++++ 5 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 x/cheqd/types/utils.go create mode 100644 x/cheqd/types/utils_test.go diff --git a/x/cheqd/types/codec.go b/x/cheqd/types/codec.go index d98e5ff9d..49d3b67d5 100644 --- a/x/cheqd/types/codec.go +++ b/x/cheqd/types/codec.go @@ -7,24 +7,17 @@ import ( "github.com/cosmos/cosmos-sdk/types/msgservice" ) -// FIXME: Double check that registration is correct - func RegisterCodec(cdc *codec.LegacyAmino) { - // this line is used by starport scaffolding # 2 cdc.RegisterConcrete(&MsgCreateDid{}, "cheqd/CreateDid", nil) cdc.RegisterConcrete(&MsgUpdateDid{}, "cheqd/UpdateDid", nil) } func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { - // this line is used by starport scaffolding # 3 registry.RegisterImplementations((*sdk.Msg)(nil), &MsgCreateDid{}, &MsgUpdateDid{}, ) - registry.RegisterInterface(MessageCreateDid, (*IdentityMsg)(nil), &MsgCreateDidPayload{}) - registry.RegisterInterface(MessageUpdateDid, (*IdentityMsg)(nil), &MsgUpdateDidPayload{}) - msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } diff --git a/x/cheqd/types/genesis.go b/x/cheqd/types/genesis.go index 2ce4680c5..ad658a124 100644 --- a/x/cheqd/types/genesis.go +++ b/x/cheqd/types/genesis.go @@ -2,13 +2,13 @@ package types import "fmt" -const DidNamespace = "testnet" +const DefaultDidNamespace = "testnet" // DefaultGenesis returns the default Capability genesis state func DefaultGenesis() *GenesisState { return &GenesisState{ DidList: []*StateValue{}, - DidNamespace: DidNamespace, + DidNamespace: DefaultDidNamespace, } } diff --git a/x/cheqd/types/stateValue.go b/x/cheqd/types/stateValue.go index 7d8c8f4f6..918a1f541 100644 --- a/x/cheqd/types/stateValue.go +++ b/x/cheqd/types/stateValue.go @@ -8,10 +8,6 @@ import ( "github.com/tendermint/tendermint/crypto/tmhash" ) -const ( - StateValueDid = "/cheqdid.cheqdnode.cheqd.v1.Did" -) - func NewStateValue(msg proto.Message, metadata *Metadata) (*StateValue, error) { data, err := types.NewAnyWithValue(msg) if err != nil { @@ -34,7 +30,7 @@ func (m StateValue) GetDid() (*Did, error) { return &value, nil } - if m.Data.TypeUrl != StateValueDid { + if m.Data.TypeUrl != MsgTypeURL(&Did{}) { return nil, ErrInvalidDidStateValue.Wrap(m.Data.TypeUrl) } diff --git a/x/cheqd/types/utils.go b/x/cheqd/types/utils.go new file mode 100644 index 000000000..80f3a0312 --- /dev/null +++ b/x/cheqd/types/utils.go @@ -0,0 +1,8 @@ +package types + +import "github.com/gogo/protobuf/proto" + +// MsgTypeURL returns the TypeURL of a `proto.Message`. +func MsgTypeURL(msg proto.Message) string { + return "/" + proto.MessageName(msg) +} diff --git a/x/cheqd/types/utils_test.go b/x/cheqd/types/utils_test.go new file mode 100644 index 000000000..4e5191838 --- /dev/null +++ b/x/cheqd/types/utils_test.go @@ -0,0 +1,10 @@ +package types + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func Test_MsgTypeUrl(t *testing.T) { + assert.Equal(t, "/cheqdid.cheqdnode.cheqd.v1.Did", MsgTypeURL(&Did{})) +} From 4edfec0548b5d42e1129df7f62abf914ccc47221 Mon Sep 17 00:00:00 2001 From: Alexandr Kolesov Date: Wed, 15 Dec 2021 18:53:58 +0300 Subject: [PATCH 3/3] Optimize stateValue marshalling --- x/cheqd/genesis.go | 2 +- x/cheqd/keeper/grpc_query_did.go | 6 ++--- x/cheqd/keeper/msg_server_did.go | 2 +- x/cheqd/keeper/verify.go | 4 +-- x/cheqd/tests/handler_test_setup.go | 11 ++++---- x/cheqd/types/codec.go | 10 +++++++ x/cheqd/types/did.go | 2 ++ x/cheqd/types/genesis.go | 6 +++-- x/cheqd/types/stateValue.go | 41 ++++++++++++++++++----------- x/cheqd/types/stateValueData.go | 8 ++++++ x/cheqd/types/stateValue_test.go | 37 ++++++++++++++++++++++++++ 11 files changed, 100 insertions(+), 29 deletions(-) create mode 100644 x/cheqd/types/stateValueData.go create mode 100644 x/cheqd/types/stateValue_test.go diff --git a/x/cheqd/genesis.go b/x/cheqd/genesis.go index 1dec57157..f4a60ab2e 100644 --- a/x/cheqd/genesis.go +++ b/x/cheqd/genesis.go @@ -11,7 +11,7 @@ import ( // state. func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) { for _, elem := range genState.DidList { - did, err := elem.GetDid() + did, err := elem.UnpackDataAsDid() if err != nil { panic(fmt.Sprintf("Cannot import geneses case: %s", err.Error())) } diff --git a/x/cheqd/keeper/grpc_query_did.go b/x/cheqd/keeper/grpc_query_did.go index 1f9f764c4..1b4622552 100644 --- a/x/cheqd/keeper/grpc_query_did.go +++ b/x/cheqd/keeper/grpc_query_did.go @@ -15,15 +15,15 @@ func (k Keeper) Did(c context.Context, req *types.QueryGetDidRequest) (*types.Qu ctx := sdk.UnwrapSDKContext(c) - state, err := k.GetDid(&ctx, req.Id) + stateValue, err := k.GetDid(&ctx, req.Id) if err != nil { return nil, err } - did, err := state.GetDid() + did, err := stateValue.UnpackDataAsDid() if err != nil { return nil, err } - return &types.QueryGetDidResponse{Did: did, Metadata: state.Metadata}, nil + return &types.QueryGetDidResponse{Did: did, Metadata: stateValue.Metadata}, nil } diff --git a/x/cheqd/keeper/msg_server_did.go b/x/cheqd/keeper/msg_server_did.go index 543460205..355890111 100644 --- a/x/cheqd/keeper/msg_server_did.go +++ b/x/cheqd/keeper/msg_server_did.go @@ -77,7 +77,7 @@ func (k msgServer) UpdateDid(goCtx context.Context, msg *types.MsgUpdateDid) (*t return nil, err } - oldDIDDoc, err := oldStateValue.GetDid() + oldDIDDoc, err := oldStateValue.UnpackDataAsDid() if err != nil { return nil, err } diff --git a/x/cheqd/keeper/verify.go b/x/cheqd/keeper/verify.go index e7acb8e1c..f90c3861f 100644 --- a/x/cheqd/keeper/verify.go +++ b/x/cheqd/keeper/verify.go @@ -28,7 +28,7 @@ func (k *Keeper) VerifySignature(ctx *sdk.Context, msg types.IdentityMsg, signer return types.ErrDidDocNotFound.Wrap(signer.Signer) } - didDoc, err := state.GetDid() + didDoc, err := state.UnpackDataAsDid() if err != nil { return types.ErrDidDocNotFound.Wrap(signer.Signer) } @@ -58,7 +58,7 @@ func (k *Keeper) ValidateController(ctx *sdk.Context, id string, controller stri if err != nil { return types.ErrDidDocNotFound.Wrap(controller) } - didDoc, err := state.GetDid() + didDoc, err := state.UnpackDataAsDid() if err != nil { return types.ErrDidDocNotFound.Wrap(controller) } diff --git a/x/cheqd/tests/handler_test_setup.go b/x/cheqd/tests/handler_test_setup.go index 6dd078f01..ab885ade0 100644 --- a/x/cheqd/tests/handler_test_setup.go +++ b/x/cheqd/tests/handler_test_setup.go @@ -7,10 +7,10 @@ import ( "github.com/btcsuite/btcutil/base58" "github.com/cheqd/cheqd-node/x/cheqd" "github.com/cheqd/cheqd-node/x/cheqd/types" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/multiformats/go-multibase" "time" - "github.com/cheqd/cheqd-node/app/params" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/store" "github.com/tendermint/tendermint/libs/log" @@ -37,8 +37,9 @@ type TestSetup struct { func Setup() TestSetup { // Init Codec - encodingConfig := params.MakeEncodingConfig() - cdc := encodingConfig.Codec + ir := codectypes.NewInterfaceRegistry() + types.RegisterInterfaces(ir) + cdc := codec.NewProtoCodec(ir) // Init KVSore db := dbm.NewMemDB() @@ -198,7 +199,7 @@ func (s *TestSetup) SendUpdateDid(msg *types.MsgUpdateDidPayload, keys map[strin } updated, _ := s.Keeper.GetDid(&s.Ctx, msg.Id) - return updated.GetDid() + return updated.UnpackDataAsDid() } func (s *TestSetup) SendCreateDid(msg *types.MsgCreateDidPayload, keys map[string]ed25519.PrivateKey) (*types.Did, error) { @@ -208,7 +209,7 @@ func (s *TestSetup) SendCreateDid(msg *types.MsgCreateDidPayload, keys map[strin } created, _ := s.Keeper.GetDid(&s.Ctx, msg.Id) - return created.GetDid() + return created.UnpackDataAsDid() } func ConcatKeys(dst map[string]ed25519.PrivateKey, src map[string]ed25519.PrivateKey) map[string]ed25519.PrivateKey { diff --git a/x/cheqd/types/codec.go b/x/cheqd/types/codec.go index 49d3b67d5..21d800df5 100644 --- a/x/cheqd/types/codec.go +++ b/x/cheqd/types/codec.go @@ -8,16 +8,26 @@ import ( ) func RegisterCodec(cdc *codec.LegacyAmino) { + // Sdk messages cdc.RegisterConcrete(&MsgCreateDid{}, "cheqd/CreateDid", nil) cdc.RegisterConcrete(&MsgUpdateDid{}, "cheqd/UpdateDid", nil) + + // State value data + cdc.RegisterInterface((*StateValueData)(nil), nil) + cdc.RegisterConcrete(&Did{}, "cheqd/Did", nil) } func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { + // Sdk messages registry.RegisterImplementations((*sdk.Msg)(nil), &MsgCreateDid{}, &MsgUpdateDid{}, ) + // State value data + registry.RegisterInterface("StateValueData", (*StateValueData)(nil)) + registry.RegisterImplementations((*StateValueData)(nil), &Did{}) + msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } diff --git a/x/cheqd/types/did.go b/x/cheqd/types/did.go index 752b2a0b9..eded52861 100644 --- a/x/cheqd/types/did.go +++ b/x/cheqd/types/did.go @@ -2,6 +2,8 @@ package types import "github.com/multiformats/go-multibase" +var _ StateValueData = &Did{} + func (v VerificationMethod) GetPublicKey() ([]byte, error) { if len(v.PublicKeyMultibase) > 0 { _, key, err := multibase.Decode(v.PublicKeyMultibase) diff --git a/x/cheqd/types/genesis.go b/x/cheqd/types/genesis.go index ad658a124..e6a9b11df 100644 --- a/x/cheqd/types/genesis.go +++ b/x/cheqd/types/genesis.go @@ -1,6 +1,8 @@ package types -import "fmt" +import ( + "fmt" +) const DefaultDidNamespace = "testnet" @@ -18,7 +20,7 @@ func (gs GenesisState) Validate() error { didIdMap := make(map[string]bool) for _, elem := range gs.DidList { - did, err := elem.GetDid() + did, err := elem.UnpackDataAsDid() if err != nil { return err } diff --git a/x/cheqd/types/stateValue.go b/x/cheqd/types/stateValue.go index 918a1f541..0b2560ee3 100644 --- a/x/cheqd/types/stateValue.go +++ b/x/cheqd/types/stateValue.go @@ -4,17 +4,24 @@ import ( "encoding/base64" "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/gogo/protobuf/proto" "github.com/tendermint/tendermint/crypto/tmhash" + "reflect" ) -func NewStateValue(msg proto.Message, metadata *Metadata) (*StateValue, error) { - data, err := types.NewAnyWithValue(msg) +var _ types.UnpackInterfacesMessage = &StateValue{} + +func (m *StateValue) UnpackInterfaces(unpacker types.AnyUnpacker) error { + var data StateValueData + return unpacker.UnpackAny(m.Data, &data) +} + +func NewStateValue(data StateValueData, metadata *Metadata) (*StateValue, error) { + any, err := types.NewAnyWithValue(data) if err != nil { return nil, ErrInvalidDidStateValue.Wrap(err.Error()) } - return &StateValue{Data: data, Metadata: metadata}, nil + return &StateValue{Data: any, Metadata: metadata}, nil } func NewMetadata(ctx sdk.Context) Metadata { @@ -24,21 +31,25 @@ func NewMetadata(ctx sdk.Context) Metadata { return Metadata{Created: created, Updated: created, Deactivated: false, VersionId: txHash} } -func (m StateValue) GetDid() (*Did, error) { - value, isValue := m.Data.GetCachedValue().(Did) - if isValue { - return &value, nil - } - - if m.Data.TypeUrl != MsgTypeURL(&Did{}) { +func (m StateValue) UnpackData() (StateValueData, error) { + value, isOk := m.Data.GetCachedValue().(StateValueData) + if !isOk { return nil, ErrInvalidDidStateValue.Wrap(m.Data.TypeUrl) } - state := Did{} - err := state.Unmarshal(m.Data.Value) + return value, nil +} + +func (m StateValue) UnpackDataAsDid() (*Did, error) { + data, err := m.UnpackData() if err != nil { - return nil, ErrInvalidDidStateValue.Wrap(err.Error()) + return nil, err + } + + value, isValue := data.(*Did) + if !isValue { + return nil, ErrInvalidDidStateValue.Wrap(reflect.TypeOf(data).String()) } - return &state, nil + return value, nil } diff --git a/x/cheqd/types/stateValueData.go b/x/cheqd/types/stateValueData.go new file mode 100644 index 000000000..0697ec122 --- /dev/null +++ b/x/cheqd/types/stateValueData.go @@ -0,0 +1,8 @@ +package types + +import "github.com/gogo/protobuf/proto" + +// StateValueData is interface uniting possible types to be used for stateValue.data field +type StateValueData interface { + proto.Message +} diff --git a/x/cheqd/types/stateValue_test.go b/x/cheqd/types/stateValue_test.go new file mode 100644 index 000000000..689350499 --- /dev/null +++ b/x/cheqd/types/stateValue_test.go @@ -0,0 +1,37 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "testing" +) + +func Test_PackUnpackAny(t *testing.T) { + original := &Did{ + Id: "test", + } + + // Construct codec + registry := types.NewInterfaceRegistry() + RegisterInterfaces(registry) + cdc := codec.NewProtoCodec(registry) + + // Marshal + bz, err := cdc.MarshalInterface(original) + require.NoError(t, err) + + // Assert type url + var any types.Any + err = any.Unmarshal(bz) + assert.NoError(t, err) + assert.Equal(t, any.TypeUrl, MsgTypeURL(&Did{})) + + // Unmarshal + var decoded StateValueData + err = cdc.UnmarshalInterface(bz, &decoded) + require.NoError(t, err) + require.IsType(t, &Did{}, decoded) + require.Equal(t, original, decoded) +}