Skip to content

Commit

Permalink
Wire in the PurgePD capability check to the tx simulator
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew B White <[email protected]>
  • Loading branch information
mbwhite authored and denyeart committed Oct 26, 2022
1 parent a431115 commit d68294c
Show file tree
Hide file tree
Showing 12 changed files with 504 additions and 157 deletions.
20 changes: 20 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,25 @@ Please visit the
[contributors guide](http://hyperledger-fabric.readthedocs.io/en/latest/CONTRIBUTING.html) in the
docs to learn how to make contributions to this exciting project.

## Running Unit tests

An example of using the script as used in the CI pipeline to run Unit Tests

```
TEST_PKGS=github.com/hyperledger/fabric/core/chaincode/... ./scripts/run-unit-tests.sh
```

## Creating the mocks for unit tests

A number of mock implementations of interfaces are used within the unit tests. For historical reasons there are two tools
with the repo to generate these mocks. [mockery](https://github.com/vektra/mockery) and [counterfeiter](https://github.com/maxbrunsfeld/counterfeiter)

- look in the already created mock - the first line will indicate which tool it was created with
- for counterfieter, run `go generate ./<dir>/...` in the <dir> where you want the mocks directory to be created

- for mockery the command is `mockery --name ApplicationCapabilities --dir ~/github.com/hyperledger/fabric/common/channelconfig --filename application_capabilities.go`
- this will create the mock with the filename given in the mocks directory based of the cwd
- the `--name` and `--dir` indicate where the 'source' interface is to be mocked

<a rel="license" href="http://creativecommons.org/licenses/by/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by/4.0/88x31.png" /></a><br />This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>.
s
4 changes: 4 additions & 0 deletions common/channelconfig/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ type ApplicationCapabilities interface {
// KeyLevelEndorsement returns true if this channel supports endorsement
// policies expressible at a ledger key granularity, as described in FAB-8812
KeyLevelEndorsement() bool

// PurgePvtData returns true if this channel supports purging of private
// data entries
PurgePvtData() bool
}

// OrdererCapabilities defines the capabilities for the orderer portion of a channel
Expand Down
16 changes: 16 additions & 0 deletions core/chaincode/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,18 @@ func (h *Handler) checkMetadataCap(msg *pb.ChaincodeMessage) error {
return nil
}

func (h *Handler) checkPurgePrivateDataCap(msg *pb.ChaincodeMessage) error {
ac, exists := h.AppConfig.GetApplicationConfig(msg.ChannelId)
if !exists {
return errors.Errorf("application config does not exist for %s", msg.ChannelId)
}

if !ac.Capabilities().PurgePvtData() {
return errors.New("purge private data is not enabled, channel application capability of V2_5 or later is required")
}
return nil
}

func errorIfCreatorHasNoReadPermission(chaincodeName, collection string, txContext *TransactionContext) error {
rwPermission, err := getReadWritePermission(chaincodeName, collection, txContext)
if err != nil {
Expand Down Expand Up @@ -1080,6 +1092,10 @@ func (h *Handler) HandleDelState(msg *pb.ChaincodeMessage, txContext *Transactio
}

func (h *Handler) HandlePurgePrivateData(msg *pb.ChaincodeMessage, txContext *TransactionContext) (*pb.ChaincodeMessage, error) {
err := h.checkPurgePrivateDataCap(msg)
if err != nil {
return nil, err
}
delState := &pb.DelState{}
if err := proto.Unmarshal(msg.Payload, delState); err != nil {
return nil, errors.Wrap(err, "unmarshal failed")
Expand Down
12 changes: 12 additions & 0 deletions core/chaincode/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ var _ = Describe("Handler", func() {
fakeApplicationConfig := &mock.ApplicationConfig{}
fakeCapabilites = &mock.ApplicationCapabilities{}
fakeCapabilites.KeyLevelEndorsementReturns(true)
fakeCapabilites.PurgePvtDataReturns(true)
fakeApplicationConfig.CapabilitiesReturns(fakeCapabilites)
fakeApplicationConfigRetriever = &fake.ApplicationConfigRetriever{}
fakeApplicationConfigRetriever.GetApplicationConfigReturns(fakeApplicationConfig, true)
Expand Down Expand Up @@ -680,6 +681,17 @@ var _ = Describe("Handler", func() {
})
})

Context("when purge private data is not supported", func() {
BeforeEach(func() {
fakeCapabilites.PurgePvtDataReturns(false)
})

It("returns an error", func() {
_, err := handler.HandlePurgePrivateData(incomingMessage, txContext)
Expect(err).To(MatchError("purge private data is not enabled, channel application capability of V2_5 or later is required"))
})
})

Context("when unmarshalling the request fails", func() {
BeforeEach(func() {
incomingMessage.Payload = []byte("this-is-a-bogus-payload")
Expand Down
Loading

0 comments on commit d68294c

Please sign in to comment.