Skip to content

Commit

Permalink
Implement CommittedInLedger function
Browse files Browse the repository at this point in the history
  • Loading branch information
bandreghetti committed Nov 26, 2021
1 parent a78566d commit fc6b95f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
45 changes: 43 additions & 2 deletions assets/existsInLedger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,59 @@ func existsInLedger(stub *sw.StubWrapper, isPrivate bool, typeTag, key string) (
return false, nil
}

// ExistsInLedger checks if asset currently has a state on the ledger.
// ExistsInLedger checks if asset currently has a state.
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())
}

// ExistsInLedger checks if asset referenced by a key object currently has a state on the ledger.
// 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())
}

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

func committedInLedger(stub *sw.StubWrapper, isPrivate bool, typeTag, 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)
} else {
assetBytes, err = stub.Stub.GetPrivateDataHash(typeTag, key)
}
} else {
assetBytes, err = stub.Stub.GetState(key)
}
if err != nil {
return false, errors.WrapErrorWithStatus(err, "unable to check asset existence", 400)
}
if assetBytes != nil {
return true, nil
}

return false, nil
}

// CommittedInLedger checks if asset currently has a state committed in ledger.
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())
}

// 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())
}
47 changes: 47 additions & 0 deletions test/assets_committedInLedger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package test

import (
"testing"

"github.com/goledgerdev/cc-tools/assets"
"github.com/goledgerdev/cc-tools/mock"
sw "github.com/goledgerdev/cc-tools/stubwrapper"
)

func TestCommittedInLedgerNoKey(t *testing.T) {
key := assets.Key{
"@assetType": "person",
// "@key": "person:47061146-c642-51a1-844a-bf0b17cb5e19",
}

stub := mock.NewMockStub("org1MSP", new(testCC))
stub.MockTransactionStart("TestCommittedInLedgerNoKey")
sw := &sw.StubWrapper{
Stub: stub,
}
_, err := key.CommittedInLedger(sw)
if err.Status() != 500 || err.Message() != "key is empty" {
t.FailNow()
}
stub.MockTransactionEnd("TestCommittedInLedgerNoAssetKey")
}

func TestCommittedInLedgerNoAssetKey(t *testing.T) {
asset := assets.Asset{
// "@key": "person:47061146-c642-51a1-844a-bf0b17cb5e19",
"@assetType": "person",
"name": "Maria",
"id": "31820792048",
}

stub := mock.NewMockStub("org1MSP", new(testCC))
stub.MockTransactionStart("TestCommittedInLedgerNoAssetKey")
sw := &sw.StubWrapper{
Stub: stub,
}
_, err := asset.CommittedInLedger(sw)
if err.Status() != 500 || err.Message() != "asset key is empty" {
t.FailNow()
}
stub.MockTransactionEnd("TestCommittedInLedgerNoAssetKey")
}

0 comments on commit fc6b95f

Please sign in to comment.