diff --git a/.dockerignore b/.dockerignore index fd7872f47..dbdd64280 100644 --- a/.dockerignore +++ b/.dockerignore @@ -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 .. diff --git a/ecc_go/ccToolsDemoEnclave.json b/ecc_go/ccToolsDemoEnclave.json new file mode 100644 index 000000000..327d3d9ea --- /dev/null +++ b/ecc_go/ccToolsDemoEnclave.json @@ -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 + } +] +} \ No newline at end of file diff --git a/ecc_go/chaincode/enclave_go/shim.go b/ecc_go/chaincode/enclave_go/shim.go index dcb5d4e59..af29db90d 100644 --- a/ecc_go/chaincode/enclave_go/shim.go +++ b/ecc_go/chaincode/enclave_go/shim.go @@ -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" ) @@ -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 { diff --git a/ecc_go/enclave.json b/ecc_go/enclave.json index 5ffe012ca..e4ccc2de1 100644 --- a/ecc_go/enclave.json +++ b/ecc_go/enclave.json @@ -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 diff --git a/samples/application/ccapi/README.md b/samples/application/ccapi/README.md index bc08d84f2..8f1fb7f86 100644 --- a/samples/application/ccapi/README.md +++ b/samples/application/ccapi/README.md @@ -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` \ No newline at end of file +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 diff --git a/samples/chaincode/cc-tools-demo/README.md b/samples/chaincode/cc-tools-demo/README.md index 0b4f8ec02..29c271db1 100644 --- a/samples/chaincode/cc-tools-demo/README.md +++ b/samples/chaincode/cc-tools-demo/README.md @@ -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 @@ -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. @@ -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: @@ -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 { @@ -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 @@ -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 @@ -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 @@ -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/) diff --git a/commands.sh b/samples/chaincode/cc-tools-demo/testTutorial.sh old mode 100755 new mode 100644 similarity index 100% rename from commands.sh rename to samples/chaincode/cc-tools-demo/testTutorial.sh