Skip to content

Commit

Permalink
Merge pull request #30 from hyperledger-labs/refactor/private-coll-names
Browse files Browse the repository at this point in the history
Allow private assets to have different collection names
  • Loading branch information
samuelvenzi authored Oct 27, 2023
2 parents 463aa4f + 47b940c commit 6ea1747
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 22 deletions.
10 changes: 10 additions & 0 deletions assets/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ func (a Asset) IsPrivate() bool {
return assetTypeDef.IsPrivate()
}

func (a Asset) CollectionName() string {
// Fetch asset properties
assetTypeDef := a.Type()
if assetTypeDef == nil {
return ""
}

return assetTypeDef.CollectionName()
}

// TypeTag returns the @assetType attribute.
func (a Asset) TypeTag() string {
assetType, _ := a["@assetType"].(string)
Expand Down
13 changes: 13 additions & 0 deletions assets/assetType.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type AssetType struct {

// Dynamic is a flag that indicates if the asset type is dynamic.
Dynamic bool `json:"dynamic,omitempty"`

// Private collection name it belongs to. When empty and len(readers) > 0,
// Tag is considered instead
Collection string `json:"collection,omitempty"`
}

// Keys returns a list of asset properties which are defined as primary keys. (IsKey == true)
Expand Down Expand Up @@ -81,6 +85,15 @@ func (t AssetType) IsPrivate() bool {
return len(t.Readers) > 0
}

// CollectionName returns the private collection name. Default is tag.
func (t AssetType) CollectionName() string {
if t.Collection == "" {
return t.Tag
}

return t.Collection
}

// ToMap returns a map representation of the asset type.
func (t AssetType) ToMap() map[string]interface{} {
return map[string]interface{}{
Expand Down
2 changes: 1 addition & 1 deletion assets/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (a *Asset) delete(stub *sw.StubWrapper) ([]byte, errors.ICCError) {
return nil, errors.WrapError(err, "failed to marshal asset")
}
} else {
err = stub.DelPrivateData(a.TypeTag(), a.Key())
err = stub.DelPrivateData(a.CollectionName(), a.Key())
if err != nil {
return nil, errors.WrapError(err, "failed to delete state from private collection")
}
Expand Down
20 changes: 10 additions & 10 deletions assets/existsInLedger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
sw "github.com/hyperledger-labs/cc-tools/stubwrapper"
)

func existsInLedger(stub *sw.StubWrapper, isPrivate bool, typeTag, key string) (bool, errors.ICCError) {
func existsInLedger(stub *sw.StubWrapper, isPrivate bool, collection, key string) (bool, errors.ICCError) {
var assetBytes []byte
var err error
if isPrivate {
_, isMock := stub.Stub.(*mock.MockStub)
if isMock {
assetBytes, err = stub.GetPrivateData(typeTag, key)
assetBytes, err = stub.GetPrivateData(collection, key)
} else {
assetBytes, err = stub.GetPrivateDataHash(typeTag, key)
assetBytes, err = stub.GetPrivateDataHash(collection, key)
}
} else {
assetBytes, err = stub.GetState(key)
Expand All @@ -34,28 +34,28 @@ func (a *Asset) ExistsInLedger(stub *sw.StubWrapper) (bool, errors.ICCError) {
if a.Key() == "" {
return false, errors.NewCCError("asset key is empty", 500)
}
return existsInLedger(stub, a.IsPrivate(), a.TypeTag(), a.Key())
return existsInLedger(stub, a.IsPrivate(), a.CollectionName(), a.Key())
}

// ExistsInLedger checks if asset referenced by a key object currently has a state.
func (k *Key) ExistsInLedger(stub *sw.StubWrapper) (bool, errors.ICCError) {
if k.Key() == "" {
return false, errors.NewCCError("key is empty", 500)
}
return existsInLedger(stub, k.IsPrivate(), k.TypeTag(), k.Key())
return existsInLedger(stub, k.IsPrivate(), k.CollectionName(), k.Key())
}

// ----------------------------------------

func committedInLedger(stub *sw.StubWrapper, isPrivate bool, typeTag, key string) (bool, errors.ICCError) {
func committedInLedger(stub *sw.StubWrapper, isPrivate bool, collection, key string) (bool, errors.ICCError) {
var assetBytes []byte
var err error
if isPrivate {
_, isMock := stub.Stub.(*mock.MockStub)
if isMock {
assetBytes, err = stub.Stub.GetPrivateData(typeTag, key)
assetBytes, err = stub.Stub.GetPrivateData(collection, key)
} else {
assetBytes, err = stub.Stub.GetPrivateDataHash(typeTag, key)
assetBytes, err = stub.Stub.GetPrivateDataHash(collection, key)
}
} else {
assetBytes, err = stub.Stub.GetState(key)
Expand All @@ -75,13 +75,13 @@ func (a *Asset) CommittedInLedger(stub *sw.StubWrapper) (bool, errors.ICCError)
if a.Key() == "" {
return false, errors.NewCCError("asset key is empty", 500)
}
return committedInLedger(stub, a.IsPrivate(), a.TypeTag(), a.Key())
return committedInLedger(stub, a.IsPrivate(), a.CollectionName(), a.Key())
}

// CommittedInLedger checks if asset referenced by a key object currently has a state committed in ledger.
func (k *Key) CommittedInLedger(stub *sw.StubWrapper) (bool, errors.ICCError) {
if k.Key() == "" {
return false, errors.NewCCError("key is empty", 500)
}
return committedInLedger(stub, k.IsPrivate(), k.TypeTag(), k.Key())
return committedInLedger(stub, k.IsPrivate(), k.CollectionName(), k.Key())
}
20 changes: 10 additions & 10 deletions assets/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func get(stub *sw.StubWrapper, pvtCollection, key string, committed bool) (*Asse
func (a *Asset) Get(stub *sw.StubWrapper) (*Asset, errors.ICCError) {
var pvtCollection string
if a.IsPrivate() {
pvtCollection = a.TypeTag()
pvtCollection = a.CollectionName()
}

return get(stub, pvtCollection, a.Key(), false)
Expand All @@ -60,7 +60,7 @@ func (a *Asset) Get(stub *sw.StubWrapper) (*Asset, errors.ICCError) {
func (k *Key) Get(stub *sw.StubWrapper) (*Asset, errors.ICCError) {
var pvtCollection string
if k.IsPrivate() {
pvtCollection = k.TypeTag()
pvtCollection = k.CollectionName()
}

return get(stub, pvtCollection, k.Key(), false)
Expand All @@ -73,7 +73,7 @@ func GetMany(stub *sw.StubWrapper, keys []Key) ([]*Asset, errors.ICCError) {
for _, k := range keys {
var pvtCollection string
if k.IsPrivate() {
pvtCollection = k.TypeTag()
pvtCollection = k.CollectionName()
}

asset, err := get(stub, pvtCollection, k.Key(), false)
Expand All @@ -90,7 +90,7 @@ func GetMany(stub *sw.StubWrapper, keys []Key) ([]*Asset, errors.ICCError) {
func (a *Asset) GetCommitted(stub *sw.StubWrapper) (*Asset, errors.ICCError) {
var pvtCollection string
if a.IsPrivate() {
pvtCollection = a.TypeTag()
pvtCollection = a.CollectionName()
}

return get(stub, pvtCollection, a.Key(), true)
Expand All @@ -100,7 +100,7 @@ func (a *Asset) GetCommitted(stub *sw.StubWrapper) (*Asset, errors.ICCError) {
func (k *Key) GetCommitted(stub *sw.StubWrapper) (*Asset, errors.ICCError) {
var pvtCollection string
if k.IsPrivate() {
pvtCollection = k.TypeTag()
pvtCollection = k.CollectionName()
}

return get(stub, pvtCollection, k.Key(), true)
Expand All @@ -111,7 +111,7 @@ func (k *Key) GetBytes(stub *sw.StubWrapper) ([]byte, errors.ICCError) {
var assetBytes []byte
var err error
if k.IsPrivate() {
assetBytes, err = stub.GetPrivateData(k.TypeTag(), k.Key())
assetBytes, err = stub.GetPrivateData(k.CollectionName(), k.Key())
} else {
assetBytes, err = stub.GetState(k.Key())
}
Expand Down Expand Up @@ -221,7 +221,7 @@ func getRecursive(stub *sw.StubWrapper, pvtCollection, key string, keysChecked [

var subAsset map[string]interface{}
if propKey.IsPrivate() {
subAsset, err = getRecursive(stub, propKey.TypeTag(), propKey.Key(), keysChecked)
subAsset, err = getRecursive(stub, propKey.CollectionName(), propKey.Key(), keysChecked)
} else {
subAsset, err = getRecursive(stub, "", propKey.Key(), keysChecked)
}
Expand Down Expand Up @@ -268,7 +268,7 @@ func getRecursive(stub *sw.StubWrapper, pvtCollection, key string, keysChecked [

var subAsset map[string]interface{}
if elemKey.IsPrivate() {
subAsset, err = getRecursive(stub, elemKey.TypeTag(), elemKey.Key(), keysChecked)
subAsset, err = getRecursive(stub, elemKey.CollectionName(), elemKey.Key(), keysChecked)
} else {
subAsset, err = getRecursive(stub, "", elemKey.Key(), keysChecked)
}
Expand All @@ -290,7 +290,7 @@ func getRecursive(stub *sw.StubWrapper, pvtCollection, key string, keysChecked [
func (a *Asset) GetRecursive(stub *sw.StubWrapper) (map[string]interface{}, errors.ICCError) {
var pvtCollection string
if a.IsPrivate() {
pvtCollection = a.TypeTag()
pvtCollection = a.CollectionName()
}

return getRecursive(stub, pvtCollection, a.Key(), []string{})
Expand All @@ -300,7 +300,7 @@ func (a *Asset) GetRecursive(stub *sw.StubWrapper) (map[string]interface{}, erro
func (k *Key) GetRecursive(stub *sw.StubWrapper) (map[string]interface{}, errors.ICCError) {
var pvtCollection string
if k.IsPrivate() {
pvtCollection = k.TypeTag()
pvtCollection = k.CollectionName()
}

return getRecursive(stub, pvtCollection, k.Key(), []string{})
Expand Down
10 changes: 10 additions & 0 deletions assets/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ func (k Key) IsPrivate() bool {
return assetTypeDef.IsPrivate()
}

func (k Key) CollectionName() string {
// Fetch asset properties
assetTypeDef := k.Type()
if assetTypeDef == nil {
return ""
}

return assetTypeDef.CollectionName()
}

// TypeTag returns @assetType attribute
func (k Key) TypeTag() string {
assetType, _ := k["@assetType"].(string)
Expand Down
2 changes: 1 addition & 1 deletion assets/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (a *Asset) put(stub *sw.StubWrapper) (map[string]interface{}, errors.ICCErr

// Write asset to blockchain
if a.IsPrivate() {
err = stub.PutPrivateData(a.TypeTag(), a.Key(), assetJSON)
err = stub.PutPrivateData(a.CollectionName(), a.Key(), assetJSON)
if err != nil {
return nil, errors.WrapError(err, "failed to write asset to ledger")
}
Expand Down
2 changes: 2 additions & 0 deletions transactions/getSchema.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var GetSchema = Transaction{
Readers []string `json:"readers,omitempty"`
Writers []string `json:"writers"`
Dynamic bool `json:"dynamic"`
Collection string `json:"collection,omitempty"`
}
var assetList []assetListElem
for _, assetTypeDef := range assetTypeList {
Expand All @@ -68,6 +69,7 @@ var GetSchema = Transaction{
Description: assetTypeDef.Description,
Readers: assetTypeDef.Readers,
Dynamic: assetTypeDef.Dynamic,
Collection: assetTypeDef.Collection,
})
}

Expand Down

0 comments on commit 6ea1747

Please sign in to comment.