-
Notifications
You must be signed in to change notification settings - Fork 98
feat: add ccapi to application samples #780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mbrandenburger
merged 13 commits into
hyperledger:main
from
osamamagdy:feature/add-ccapi-for-samples
Jan 31, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b6f26f4
feat: add ccapi
osamamagdy 541d762
fix: fix linting errors
osamamagdy 709fb4e
fix: add go mod for ccapi
osamamagdy 429063e
fix: edit go mod to use fpc from main commit
osamamagdy 61d1d55
fix: edit path
osamamagdy cb14116
fix: remove go mod vendor from dockerfile
osamamagdy a52bcfd
doc: cd into ccapi before running
osamamagdy 189cdac
feat: remove hardcoded org name
osamamagdy 266287c
feat: edit ccapi to be just an extension
osamamagdy d3dee26
fix: add the org name for scripts
osamamagdy 2efeb72
fix: export CORE_PEER_ORG_NAME
osamamagdy ed6cea3
docs: edit readme
osamamagdy d9e443f
docs: add notes for ccapi
osamamagdy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # Use an official Golang runtime as a parent image | ||
| FROM golang:1.21-alpine AS build | ||
|
|
||
| ENV PATH="${PATH}:/usr/bin/" | ||
|
|
||
| RUN apk update | ||
|
|
||
| RUN apk add \ | ||
| docker \ | ||
| openrc \ | ||
| git \ | ||
| gcc \ | ||
| gcompat \ | ||
| libc-dev \ | ||
| libc6-compat \ | ||
| libstdc++ && \ | ||
| ln -s /lib/libc.so.6 /usr/lib/libresolv.so.2 | ||
|
|
||
| # Set the working directory to /rest-server | ||
| WORKDIR /rest-server | ||
|
|
||
| # Copy the go.mod and go.sum files for dependency management | ||
| COPY go.mod go.sum ./ | ||
|
|
||
| # Install go dependencies | ||
| RUN go mod download | ||
|
|
||
| # Copy the current directory contents into the container at /rest-server | ||
| COPY . . | ||
|
|
||
| # Build the Go ccapi | ||
| RUN go build -o ccapi | ||
|
|
||
| # # Use an official Alpine runtime as a parent image | ||
| # FROM alpine:latest | ||
|
|
||
| # ENV PATH="${PATH}:/usr/bin/" | ||
|
|
||
| # RUN apk update | ||
|
|
||
| # RUN apk add \ | ||
| # docker \ | ||
| # openrc \ | ||
| # git \ | ||
| # gcc \ | ||
| # gcompat \ | ||
| # libc-dev \ | ||
| # libc6-compat \ | ||
| # libstdc++ && \ | ||
| # ln -s /lib/libc.so.6 /usr/lib/libresolv.so.2 | ||
|
|
||
| # # Set the working directory to /rest-server | ||
| # WORKDIR /rest-server | ||
|
|
||
| # Copy the ccapi binary from the build container to the current directory in the Alpine container | ||
| RUN ls -l | ||
| RUN pwd | ||
|
|
||
| RUN cp ./ccapi /usr/bin/ccapi | ||
|
|
||
| # Run the ccapi binary | ||
| CMD ["ccapi"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # CCAPI - A web server developed to interface with CC-tools chaincode | ||
|
|
||
| ## Motivation | ||
|
|
||
| As continuation to the [cc-tools-demo]() tutorial on how to integrate the cc-tools project with FPC chaincodes, we start by utilizing another powerful solution offered by cc-tools which is the CCAPI. It's a complete web server that simplifies the communication with the peers and Fabric components to replace the need to deal with CLI applications. | ||
|
|
||
| ## Architecture | ||
|
|
||
| The following diagram explains the process where we modified the API server developed for a demo on cc-tools ([CCAPI](https://github.com/hyperledger-labs/cc-tools-demo/tree/main/ccapi)) and modified it to communicate with FPC code. | ||
|
|
||
| The transaction client invocation process, as illustrated in the diagram, consists of several key steps that require careful integration between FPC and cc-tools. | ||
|
|
||
| 1. Step 1-2: The API server is listening for requests on a specified port over an HTTP channel and sends it to the handler. | ||
| 2. Step 3: The handler starts by determining the appropriate transaction invocation based on the requested endpoint and calling the corresponding chaincode API. | ||
| 3. Step 4: The chaincode API is responsible for parsing and ensuring the payload is correctly parsed into a format that is FPC-friendly. This parsing step is crucial, as it prepares the data to meet FPC’s privacy and security requirements before it reaches the peer. | ||
| 4. Step 5: FPCUtils is the step where the actual transaction invocation happens. It follows the steps explained in [here](https://github.com/hyperledger/fabric-rfcs/blob/main/text/0000-fabric-private-chaincode-1.0.md#fpc-transaction-flow) as it builds on top of the FPC Client SDK. | ||
|
|
||
|  | ||
|
|
||
| ## User Experience | ||
|
|
||
| CCAPI uses docker and docker-compose to spin up all the required components 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. | ||
|
|
||
| Note: The following steps should run outside the FPC dev container | ||
|
|
||
| Start by running: | ||
|
|
||
| ```bash | ||
| cd $FPC_PATH/samples/application/ccapi | ||
| 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 has another component for the dashboard frontend application but it's not yet utilized with | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| Copyright IBM Corp. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package chaincode | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/hyperledger/fabric-private-chaincode/samples/application/ccapi/common" | ||
| ) | ||
|
|
||
| func InvokeFpc(channelName string, chaincodeName string, txname string, args [][]byte) ([]byte, int, error) { | ||
| stringArgs := make([]string, len(args)) | ||
| for i, b := range args { | ||
| stringArgs[i] = string(b) | ||
| } | ||
|
|
||
| client := common.NewFpcClient(channelName, chaincodeName) | ||
| res := client.Invoke(txname, stringArgs[0:]...) | ||
| return []byte(res), http.StatusOK, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| Copyright IBM Corp. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package chaincode | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/hyperledger/fabric-private-chaincode/samples/application/ccapi/common" | ||
| ) | ||
|
|
||
| func InvokeFpcDefault(txname string, args [][]byte) ([]byte, int, error) { | ||
| stringArgs := make([]string, len(args)) | ||
| for i, b := range args { | ||
| stringArgs[i] = string(b) | ||
| } | ||
|
|
||
| client := common.NewDefaultFpcClient() | ||
| res := client.Invoke(txname, stringArgs[0:]...) | ||
| return []byte(res), http.StatusOK, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| Copyright IBM Corp. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package chaincode | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/hyperledger/fabric-private-chaincode/samples/application/ccapi/common" | ||
| ) | ||
|
|
||
| func QueryFpc(chaincodeName string, channelName string, txName string, args [][]byte) ([]byte, int, error) { | ||
| stringArgs := make([]string, len(args)) | ||
| for i, b := range args { | ||
| stringArgs[i] = string(b) | ||
| } | ||
|
|
||
| client := common.NewFpcClient(chaincodeName, channelName) | ||
| res := client.Query(txName, stringArgs[0:]...) | ||
| return []byte(res), http.StatusOK, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| Copyright IBM Corp. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package chaincode | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/hyperledger/fabric-private-chaincode/samples/application/ccapi/common" | ||
| ) | ||
|
|
||
| func QueryFpcDefault(txName string, args [][]byte) ([]byte, int, error) { | ||
| stringArgs := make([]string, len(args)) | ||
| for i, b := range args { | ||
| stringArgs[i] = string(b) | ||
| } | ||
|
|
||
| client := common.NewDefaultFpcClient() | ||
| res := client.Query(txName, stringArgs[0:]...) | ||
| return []byte(res), http.StatusOK, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| Copyright IBM Corp. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package common | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "strconv" | ||
|
|
||
| pkgFpc "github.com/hyperledger/fabric-private-chaincode/samples/application/ccapi/fpcUtils" | ||
| ) | ||
|
|
||
| var ( | ||
| defaultFpcConfig *pkgFpc.Config | ||
| ) | ||
|
|
||
| func InitFpcConfig() { | ||
|
|
||
| getStrEnv := func(key string) string { | ||
| val := os.Getenv(key) | ||
| if val == "" { | ||
| panic(fmt.Sprintf("%s not set", key)) | ||
| } | ||
| return val | ||
| } | ||
|
|
||
| getBoolEnv := func(key string) bool { | ||
| val := getStrEnv(key) | ||
| ret, err := strconv.ParseBool(val) | ||
| if err != nil { | ||
| if val == "" { | ||
| panic(fmt.Sprintf("invalid bool value for %s", key)) | ||
| } | ||
| } | ||
| return ret | ||
| } | ||
|
|
||
| defaultFpcConfig = &pkgFpc.Config{ | ||
| CorePeerAddress: getStrEnv("CORE_PEER_ADDRESS"), | ||
| CorePeerId: getStrEnv("CORE_PEER_ID"), | ||
| CorePeerOrgName: getStrEnv("CORE_PEER_ORG_NAME"), | ||
| CorePeerLocalMSPID: getStrEnv("CORE_PEER_LOCALMSPID"), | ||
| CorePeerMSPConfigPath: getStrEnv("CORE_PEER_MSPCONFIGPATH"), | ||
| CorePeerTLSCertFile: getStrEnv("CORE_PEER_TLS_CERT_FILE"), | ||
| CorePeerTLSEnabled: getBoolEnv("CORE_PEER_TLS_ENABLED"), | ||
| CorePeerTLSKeyFile: getStrEnv("CORE_PEER_TLS_KEY_FILE"), | ||
| CorePeerTLSRootCertFile: getStrEnv("CORE_PEER_TLS_ROOTCERT_FILE"), | ||
| OrdererCA: getStrEnv("ORDERER_CA"), | ||
| ChaincodeId: getStrEnv("CCNAME"), | ||
| ChannelId: getStrEnv("CHANNEL"), | ||
| GatewayConfigPath: getStrEnv("GATEWAY_CONFIG"), | ||
| } | ||
|
|
||
| } | ||
|
|
||
| func NewDefaultFpcClient() *pkgFpc.Client { | ||
| return pkgFpc.NewClient(defaultFpcConfig) | ||
| } | ||
|
|
||
| func NewFpcClient(channelName string, chaincodeName string) *pkgFpc.Client { | ||
| fpcConfig := defaultFpcConfig | ||
| fpcConfig.ChannelId = channelName | ||
| fpcConfig.ChaincodeId = chaincodeName | ||
| return pkgFpc.NewClient(fpcConfig) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.