Skip to content

Commit

Permalink
feat: Add default resource url (#464)
Browse files Browse the repository at this point in the history
feat: Add default ressource url
  • Loading branch information
askolesov authored Dec 3, 2022
1 parent 8a45a03 commit bb0485c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 84 deletions.
48 changes: 0 additions & 48 deletions app/migration_v_0_7.go

This file was deleted.

6 changes: 5 additions & 1 deletion x/resource/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis := types.GenesisState{}

// Get all resource
resourceList := k.GetAllResources(&ctx)
resourceList, err := k.GetAllResources(&ctx)
if err != nil {
panic(fmt.Sprintf("Cannot get all resource: %s", err.Error()))
}

for _, elem := range resourceList {
genesis.Resources = append(genesis.Resources, &elem)
}
Expand Down
64 changes: 43 additions & 21 deletions x/resource/keeper/keeper_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ func (k Keeper) SetResourceCount(ctx *sdk.Context, count uint64) {
store.Set(byteKey, bz)
}

func (k Keeper) AddNewResourceVersion(ctx *sdk.Context, resource *types.ResourceWithMetadata) error {
// Find previous version and upgrade backward and forward version links
previousResourceVersionHeader, found := k.GetLastResourceVersionMetadata(ctx, resource.Metadata.CollectionId, resource.Metadata.Name, resource.Metadata.ResourceType)
if found {
// Set links
previousResourceVersionHeader.NextVersionId = resource.Metadata.Id
resource.Metadata.PreviousVersionId = previousResourceVersionHeader.Id

// Update previous version
err := k.UpdateResourceMetadata(ctx, &previousResourceVersionHeader)
if err != nil {
return err
}
}

// Set new version
err := k.SetResource(ctx, resource)
return err
}

// SetResource create or update a specific resource in the store
func (k Keeper) SetResource(ctx *sdk.Context, resource *types.ResourceWithMetadata) error {
if !k.HasResource(ctx, resource.Metadata.CollectionId, resource.Metadata.Id) {
Expand Down Expand Up @@ -157,33 +177,35 @@ func (k Keeper) UpdateResourceMetadata(ctx *sdk.Context, metadata *types.Metadat
return nil
}

// GetAllResources returns all resources as a list
// Loads everything in memory. Use only for genesis export!
func (k Keeper) GetAllResources(ctx *sdk.Context) (list []types.ResourceWithMetadata) {
metadataIterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), didutils.StrBytes(types.ResourceMetadataKey))
dataIterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), didutils.StrBytes(types.ResourceDataKey))
func (k Keeper) IterateAllResourceMetadatas(ctx *sdk.Context, callback func(metadata types.Metadata) (continue_ bool)) {
headerIterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), didutils.StrBytes(types.ResourceMetadataKey))
defer closeIteratorOrPanic(headerIterator)

defer closeIteratorOrPanic(metadataIterator)
defer closeIteratorOrPanic(dataIterator)
for headerIterator.Valid() {
var val types.Metadata
k.cdc.MustUnmarshal(headerIterator.Value(), &val)

for metadataIterator.Valid() {
if !dataIterator.Valid() {
panic("number of headers and data don't match")
if !callback(val) {
break
}

var metadata types.Metadata
k.cdc.MustUnmarshal(metadataIterator.Value(), &metadata)

data := types.Resource{Data: dataIterator.Value()}
headerIterator.Next()
}
}

list = append(list, types.ResourceWithMetadata{
Metadata: &metadata,
Resource: &data,
})
// GetAllResources returns all resources as a list
// Loads everything in memory. Use only for genesis export!
func (k Keeper) GetAllResources(ctx *sdk.Context) (list []types.ResourceWithMetadata, err_ error) {
k.IterateAllResourceMetadatas(ctx, func(metadata types.Metadata) bool {
resource, err := k.GetResource(ctx, metadata.CollectionId, metadata.Id)
if err != nil {
err_ = err
return false
}

metadataIterator.Next()
dataIterator.Next()
}
list = append(list, resource)
return true
})

return
}
Expand Down
25 changes: 12 additions & 13 deletions x/resource/keeper/msg_server_create_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"time"

"github.com/cheqd/cheqd-node/x/resource/utils"
Expand All @@ -15,6 +16,11 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
DefaultAlternativeUriTemplate = "did:cheqd:%s:%s/resources/%s"
DefaultAlternaticeUriDescription = "did-url"
)

func (k msgServer) CreateResource(goCtx context.Context, msg *types.MsgCreateResource) (*types.MsgCreateResourceResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

Expand Down Expand Up @@ -62,22 +68,15 @@ func (k msgServer) CreateResource(goCtx context.Context, msg *types.MsgCreateRes
resource.Metadata.Created = ctx.BlockTime().Format(time.RFC3339)
resource.Metadata.MediaType = utils.DetectMediaType(resource.Resource.Data)

// Find previous version and upgrade backward and forward version links
previousResourceVersionHeader, found := k.GetLastResourceVersionMetadata(&ctx, resource.Metadata.CollectionId, resource.Metadata.Name, resource.Metadata.ResourceType)
if found {
// Set links
previousResourceVersionHeader.NextVersionId = resource.Metadata.Id
resource.Metadata.PreviousVersionId = previousResourceVersionHeader.Id

// Update previous version
err := k.UpdateResourceMetadata(&ctx, &previousResourceVersionHeader)
if err != nil {
return nil, err
}
// Add default resource alternative url
defaultAlternativeUrl := types.AlternativeUri{
Uri: fmt.Sprintf(DefaultAlternativeUriTemplate, namespace, msg.Payload.CollectionId, msg.Payload.Id),
Description: DefaultAlternaticeUriDescription,
}
resource.Metadata.AlsoKnownAs = append(resource.Metadata.AlsoKnownAs, &defaultAlternativeUrl)

// Persist resource
err = k.SetResource(&ctx, &resource)
err = k.AddNewResourceVersion(&ctx, &resource)
if err != nil {
return nil, types.ErrInternal.Wrapf(err.Error())
}
Expand Down
8 changes: 7 additions & 1 deletion x/resource/tests/create_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ func ExpectPayloadToMatchResource(payload *resourcetypes.MsgCreateResourcePayloa
Expect(payload.CollectionId).To(Equal(resource.Metadata.CollectionId))
Expect(payload.Name).To(Equal(resource.Metadata.Name))
Expect(payload.ResourceType).To(Equal(resource.Metadata.ResourceType))
Expect(payload.AlsoKnownAs).To(Equal(resource.Metadata.AlsoKnownAs))

defaultAlternativeUrl := resourcetypes.AlternativeUri{
Uri: "did:cheqd:" + didsetup.DID_NAMESPACE + ":" + payload.CollectionId + "/resources/" + payload.Id,
Description: "did-url",
}

Expect(append(payload.AlsoKnownAs, &defaultAlternativeUrl)).To(Equal(resource.Metadata.AlsoKnownAs))

// Generated header
hash := sha256.Sum256(payload.Data)
Expand Down

0 comments on commit bb0485c

Please sign in to comment.