Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix codecs #255

Merged
merged 4 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x/cheqd/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand Down
6 changes: 3 additions & 3 deletions x/cheqd/keeper/grpc_query_did.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 2 additions & 5 deletions x/cheqd/keeper/keeper_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion x/cheqd/keeper/msg_server_did.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions x/cheqd/keeper/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
11 changes: 6 additions & 5 deletions x/cheqd/tests/handler_test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
13 changes: 9 additions & 4 deletions x/cheqd/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ import (
)

func RegisterCodec(cdc *codec.LegacyAmino) {
// this line is used by starport scaffolding # 2
// 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) {
// this line is used by starport scaffolding # 3
// Sdk messages
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgCreateDid{},
&MsgUpdateDid{},
)

registry.RegisterInterface(MessageCreateDid, (*IdentityMsg)(nil), &MsgCreateDidPayload{})
registry.RegisterInterface(MessageUpdateDid, (*IdentityMsg)(nil), &MsgUpdateDidPayload{})
// State value data
registry.RegisterInterface("StateValueData", (*StateValueData)(nil))
registry.RegisterImplementations((*StateValueData)(nil), &Did{})

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}
Expand Down
2 changes: 2 additions & 0 deletions x/cheqd/types/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions x/cheqd/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package types

import "fmt"
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,
}
}

Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion x/cheqd/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
ankurdotb marked this conversation as resolved.
Show resolved Hide resolved
)
43 changes: 25 additions & 18 deletions x/cheqd/types/stateValue.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +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"
)

const (
StateValueDid = "/cheqdid.cheqdnode.cheqd.v1.Did"
)
var _ types.UnpackInterfacesMessage = &StateValue{}

func (m *StateValue) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var data StateValueData
return unpacker.UnpackAny(m.Data, &data)
}

func NewStateValue(msg proto.Message, metadata *Metadata) (*StateValue, error) {
data, err := types.NewAnyWithValue(msg)
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 {
Expand All @@ -28,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 != StateValueDid {
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
}
8 changes: 8 additions & 0 deletions x/cheqd/types/stateValueData.go
Original file line number Diff line number Diff line change
@@ -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
}
37 changes: 37 additions & 0 deletions x/cheqd/types/stateValue_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 8 additions & 0 deletions x/cheqd/types/utils.go
Original file line number Diff line number Diff line change
@@ -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)
}
10 changes: 10 additions & 0 deletions x/cheqd/types/utils_test.go
Original file line number Diff line number Diff line change
@@ -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{}))
}