From fc6b95f74168beff6126c5ad983a2d6aa59306cd Mon Sep 17 00:00:00 2001 From: Bruno Andreghetti Date: Fri, 26 Nov 2021 14:26:57 -0300 Subject: [PATCH] Implement CommittedInLedger function --- assets/existsInLedger.go | 45 +++++++++++++++++++++++-- test/assets_committedInLedger_test.go | 47 +++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 test/assets_committedInLedger_test.go diff --git a/assets/existsInLedger.go b/assets/existsInLedger.go index d80852e..1a729da 100644 --- a/assets/existsInLedger.go +++ b/assets/existsInLedger.go @@ -29,7 +29,7 @@ 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) @@ -37,10 +37,51 @@ func (a *Asset) ExistsInLedger(stub *sw.StubWrapper) (bool, errors.ICCError) { 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()) +} diff --git a/test/assets_committedInLedger_test.go b/test/assets_committedInLedger_test.go new file mode 100644 index 0000000..9322f06 --- /dev/null +++ b/test/assets_committedInLedger_test.go @@ -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") +}