Skip to content

Commit

Permalink
comments resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
osamamagdy committed Oct 21, 2024
1 parent a16a291 commit 87db6e1
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 53 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
!ecc/ecc
!ecc_go/ecc_go
!ecc_go/enclave.json
!ecc_go/ccToolsDemoEnclave.json
!ecc_enclave/_build/lib/libsgxcc.so
# note: docker seems to have troubles with '+' in filenames in this file, at least in exceptions ..
33 changes: 33 additions & 0 deletions ecc_go/ccToolsDemoEnclave.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"exe": "ecc",
"key": "private.pem",
"debug": true,
"heapSize": 512,
"productID": 1,
"securityVersion": 1,
"mounts": null,
"files": null,
"env": [
{
"name": "CHAINCODE_SERVER_ADDRESS",
"fromHost": true
},
{
"name": "CHAINCODE_PKG_ID",
"fromHost": true
},
{
"name": "FPC_ENABLED",
"fromHost": true
},
{
"name": "RUN_CCAAS",
"fromHost": true
},

{
"name": "FABRIC_LOGGING_SPEC",
"fromHost": true
}
]
}
32 changes: 18 additions & 14 deletions ecc_go/chaincode/enclave_go/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ package enclave_go
import (
"fmt"

protoV1 "github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-private-chaincode/internal/utils"
common "github.com/hyperledger/fabric-protos-go/common"
pb "github.com/hyperledger/fabric-protos-go/peer"

"google.golang.org/protobuf/proto"
timestamp "google.golang.org/protobuf/types/known/timestamppb"
)

Expand Down Expand Up @@ -250,21 +254,21 @@ func (f *FpcStubInterface) GetSignedProposal() (*pb.SignedProposal, error) {
return f.stub.GetSignedProposal()
}

// GetTxTimestamp documentation can be found in interfaces.go
func (s *FpcStubInterface) GetTxTimestamp() (*timestamp.Timestamp, error) {
// hdr := &common.Header{}
// if err := proto.Unmarshal(s.proposal.Header, hdr); err != nil {
// return nil, fmt.Errorf("error unmarshaling Header: %s", err)
// }

// chdr := &common.ChannelHeader{}
// if err := proto.Unmarshal(hdr.ChannelHeader, chdr); err != nil {
// return nil, fmt.Errorf("error unmarshaling ChannelHeader: %s", err)
// }
// return chdr.GetTimestamp(), nil
println(timestamp.Now())
return timestamp.Now(), nil
func (f *FpcStubInterface) GetTxTimestamp() (*timestamp.Timestamp, error) {
hdr := &common.Header{}
proposal, Proposalerr := f.GetSignedProposal()
if Proposalerr != nil {
return nil, fmt.Errorf("Error retrieving the proposal from the FPC Stub")
}
if err := proto.Unmarshal(proposal.ProposalBytes, protoV1.MessageV2(hdr)); err != nil {
return nil, fmt.Errorf("error unmarshaling Header: %s", err)
}

chdr := &common.ChannelHeader{}
if err := proto.Unmarshal(hdr.ChannelHeader, protoV1.MessageV2(chdr)); err != nil {
return nil, fmt.Errorf("error unmarshaling ChannelHeader: %s", err)
}
return chdr.GetTimestamp(), nil
}

func (f *FpcStubInterface) SetEvent(name string, payload []byte) error {
Expand Down
8 changes: 0 additions & 8 deletions ecc_go/enclave.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@
"name": "CHAINCODE_PKG_ID",
"fromHost": true
},
{
"name": "FPC_ENABLED",
"fromHost": true
},
{
"name": "RUN_CCAAS",
"fromHost": true
},
{
"name": "FABRIC_LOGGING_SPEC",
"fromHost": true
Expand Down
9 changes: 6 additions & 3 deletions samples/application/ccapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ The transaction client invocation process, as illustrated in the diagram, consis

![CCAPIFlow](./CCAPIFlow.png)


## User Experience

CCAPI is using docker and docker-compose for spinning up all the required components needed to work.
CCAPI is using docker and docker-compose for spinning up all the required components needed to work.

Have a look at the [fpc-docker-compose.yaml](./fpc-docker-compose.yaml) to see how we use different env vars. Most of these environment variables are required by any client application to work and communicate with FPC. If you followed the [cc-tools-demo](../../chaincode/cc-tools-demo/README.md) tutorial, the values should be the same.

Start by running `docker-compose -f fpc-docker-compose.yaml up`
Start by running `docker-compose -f fpc-docker-compose.yaml up` then go to the browser and type `localhost:80` to open the swagger api and start executing functions.

## Future work

CCAPI have another component for the dashboard frontend application but it's not yet utilized with
77 changes: 49 additions & 28 deletions samples/chaincode/cc-tools-demo/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# CC-Tools-Demo Tutorial

This tutorial shows how to build, install and test a Go Chaincode developed using the [CC-Tools]() framework and integrating it with the Fabric Private Chaincode (FPC) framework.
This tutorial shows how to build, install and test a Go Chaincode developed using the [CC-Tools](https://github.com/hyperledger-labs/cc-tools) framework and integrating it with the Fabric Private Chaincode (FPC) framework.

This tutorial illustrates a simple use case where we follow the [cc-tools-demo]() chaincode which is based on standard Fabric and then convert it to an FPC chaincode achieving FPC security capabilities.
This tutorial illustrates a simple use case where we follow the [cc-tools-demo](https://github.com/hyperledger-labs/cc-tools-demo) chaincode which is based on standard Fabric and then convert it to an FPC chaincode achieving FPC security capabilities.

This tutorial is based on the [FPC with CC-Tools integration project]() and all our design choices are explained here in the [design document]().
This tutorial is based on the [FPC with CC-Tools integration project](https://lf-hyperledger.atlassian.net/wiki/spaces/INTERN/pages/21954957/Hyperledger+Fabric+CC-Tools+Support+for+Fabric+Private+Chaincode) and all our design choices are explained here in the [design document](https://github.com/hyperledger/fabric-private-chaincode/tree/main/docs/design/integrate-with-cc-tools).
Here are the steps to accomplish this:

* Clone and copy the cc-tools-demo chaincode
* Clone the cc-tools-demo chaincode
* Modify the chaincode to use FPC
* Build your FPC CC-tools-demo chaincode
* Launch a Fabric network
Expand All @@ -16,26 +16,32 @@ Here are the steps to accomplish this:

## Prerequisites

This tutorial presumes that you have installed FPC on your `$GOPATH` as described in the FPC [README.md](../../../README.md#clone-fabric-private-chaincode) and `$FPC_PATH` is set accordingly.
* This tutorial presumes that you have installed FPC on your `$GOPATH` as described in the FPC [README.md](../../../README.md#clone-fabric-private-chaincode) and `$FPC_PATH` is set accordingly.

We also need a working FPC development environment. As described in the "Setup your Development Environment" Section of the FPC [README.md](../../../README.md#setup-your-development-environment), you can use our docker-based dev environment (Option 1) or setup your local development environment (Option 2).
* We need a working FPC development environment. As described in the "Setup your Development Environment" Section of the FPC [README.md](../../../README.md#setup-your-development-environment), you can use our docker-based dev environment (Option 1) or setup your local development environment (Option 2).
We recommend using the docker-based development environment and continue this tutorial within the dev container terminal.

Moreover, within your FPC development you have already installed the FPC Go Chaincode Support components.
* Moreover, within your FPC development you have already installed the FPC Go Chaincode Support components.
See the installation steps in [ecc_go/README.md](../../../ecc_go/README.md#installation).

We also assume that you are familiar with Fabric chaincode development in go.
* We assume that you are familiar with Fabric chaincode development in go.
Most of the steps in this tutorial follow the normal Fabric chaincode development process, however, there are a few differences that we will highlight here.

## Clone and copy the cc-tools-demo chaincode
* Also, since the tutorial is on the integration between cc-tools and FPC, we expect you to have a grasp knowledge of [cc-tools](https://github.com/hyperledger-labs/cc-tools) framework and that you've at least tried to run the [cc-tools-demo](https://github.com/hyperledger-labs/cc-tools-demo) once by yourself on a Fabric network

Clone the [cc-tools-demo]() repository and copy the [chaincode]() folder. Then paste it in the root directory for cc-tools-demo here.
## Clone the cc-tools-demo chaincode

We need to clone the chaincode folder from the [cc-tools-demo](https://github.com/hyperledger-labs/cc-tools-demo) repository here.

```bash
cd ~
git clone https://github.com/hyperledger-labs/cc-tools-demo.git
cp -a ~/cc-tools-demo/chaincode/. $FPC_PATH/samples/chaincode/cc-tools-demo/
cd $FPC_PATH/samples/chaincode/cc-tools-demo
export ccToolsDemoPath=$FPC_PATH/samples/chaincode/cc-tools-demo
git clone -n --no-checkout --depth=1 --filter=tree:0 https://github.com/hyperledger-labs/cc-tools-demo.git "$ccToolsDemoPath/chaincode"
cd "$ccToolsDemoPath/chaincode" || { echo "$ccToolsDemoPath/chaincode does not exist" ; exit 1; }
git sparse-checkout set --no-cone chaincode/*
git checkout
mv chaincode/* $ccToolsDemoPath
cd $ccToolsDemoPath
sudo rm -r $ccToolsDemoPath/chaincode
```

The chaincode code structure is different than normal chaincode as it's using the cc-tools framework.
Expand Down Expand Up @@ -115,15 +121,6 @@ func runCCaaS() error {

```

## Set the needed env vars in the docker-compose file

From the code above, we need to set two env variables for the chaincode application to work and use FPC and chaincode-as-a-service (CCAAS). One way to do this is to go to `$FPC_PATH/samples/deployment/test-network/docker-compose.yml` and edit both `ecc.peer0.org1.example.com` and `ecc.peer0.org2.example.com` environment block to have
```yaml
- RUN_CCAAS=true
- FPC_ENABLED=true
```
## Building FPC Go Chaincode

Create a `Makefile` (i.e., `touch $FPC_PATH/samples/chaincode/cc-tools-demo/Makefile`) with the following content:
Expand All @@ -133,30 +130,38 @@ TOP = ../../..
include $(TOP)/ecc_go/build.mk

CC_NAME ?= fpc-cc-tools-demo

EGO_CONFIG_FILE ?= $(FPC_PATH)/ecc_go/ccToolsDemoEnclave.json

```

Please make sure that in the file above the variable `TOP` points to the FPC root directory (i.e., `$FPC_PATH`) as it uses the `$FPC_PATH/ecc_go/build.mk` file.

**Note**: In our case, we need to change the build command in the `$FPC_PATH/ecc_go/build.mk` file at the `ecc` target to be `ego-go build $(GOTAGS) -o $(ECC_BINARY)` instead of `ego-go build $(GOTAGS) -o $(ECC_BINARY) main.go`


In `$FPC_PATH/samples/chaincode/cc-tools-demo` directory, to build the chaincode and package it as docker image, execute:

```bash
make
```

Note: this command runs inside the FPC dev environment and not your local host.

**Note**: If you faced this error:

```bash
/project/pkg/mod/github.com/hyperledger-labs/cc-tools@v1.0.1/mock/mockstub.go:146:22: cannot use stub (variable of type *MockStub) as shim.ChaincodeStubInterface value in argument to stub.cc.Init: *MockStub does not implement shim.ChaincodeStubInterface (missing method PurgePrivateData)
```

This is because there is a minor difference between the `ChaincodeStubInterface` used in the cc-tools `Mockstub` as it's missing the `PurgePrivateData` method.
To solve this, run `go mod vendor` in the `$FPC_PATH/samples/chaincode/cc-tools-demo` root directory to download all used packages and go to the file of the error to add the missing method there.

```bash
nano $FPC_PATH/vendor/github.com/hyperledger-labs/cc-tools/mock/mockstub.go
```

add the following function

```go
// PurgePrivateData ...
func (stub *MockStub) PurgePrivateData(collection, key string) error {
Expand Down Expand Up @@ -203,7 +208,19 @@ cd $FPC_PATH/samples/deployment/test-network/fabric-samples/test-network
./network.sh createChannel -c mychannel
```

Once the network is up and running, we install the simple asset chaincode and the FPC Enclave Registry.

### Set the needed env vars in the docker-compose file

From the code above, we need to set two env variables for the chaincode application to work and use FPC and chaincode-as-a-service (CCAAS). One way to do this is to go to `$FPC_PATH/samples/deployment/test-network/docker-compose.yml` and edit both `ecc.peer0.org1.example.com` and `ecc.peer0.org2.example.com` environment block to have

```yaml
- RUN_CCAAS=true
- FPC_ENABLED=true
```
### Install the chaincode
Once the network is up and running, we install the cc-tools-demo chaincode and the FPC Enclave Registry.
We provide a small shell script to make this task a bit easier.
```bash
Expand All @@ -214,8 +231,6 @@ cd $FPC_PATH/samples/deployment/test-network
./installFPC.sh
```

Note that the `installFPC.sh` script returns an export statement you need to copy and paste in the terminal.
This sets environment variables for the package IDs for each chaincode container.
Continue by running:

```bash
Expand All @@ -224,7 +239,7 @@ make ercc-ecc-start

You should see now four containers running (i.e., `cc-tools-demo.peer0.org1`, `cc-tools-demo.peer0.org2`, `ercc.peer0.org1`, and `ercc.peer0.org2`).

### Invoke simple asset
### Invoke simple getSchema transaction

Open a new terminal and connect to the `fpc-development-go-support` container by running

Expand Down Expand Up @@ -270,3 +285,9 @@ export RUN_CCAAS=true
```

Congratulations! You have successfully created an FPC chaincode with go using cc-tools and invoked it using our simple cli.

Now you can test all your work again by running the [test](./testTutorial.sh) script

## Next Step

CC-tools-demo also provides a unique API server called CCAPI that is able to communicate with the peers and execute transactions through a REST API. We integrated this either in the [CCAPI tutorial](../../application/ccapi/)
File renamed without changes.

0 comments on commit 87db6e1

Please sign in to comment.