Skip to content
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

Feature/Edit CCAPI to support FPC #75

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
29 changes: 25 additions & 4 deletions ccapi/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
# Use an official Golang runtime as a parent image
FROM golang:1.18-alpine AS build
FROM golang:1.19-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 current directory contents into the container at /rest-server
COPY . .
# Copy the go.mod and go.sum files for dependency management
COPY go.mod go.sum ./

# Install go dependencies
RUN go mod download

RUN go mod vendor

# Copy the current directory contents into the container at /rest-server
COPY . .

# Build the Go ccapi
RUN go build -o ccapi

Expand All @@ -19,7 +40,7 @@ ENV PATH="${PATH}:/usr/bin/"

RUN apk update

RUN apk add --no-cache \
RUN apk add \
docker \
openrc \
git \
Expand Down
24 changes: 24 additions & 0 deletions ccapi/chaincode/invokeFPC.go
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-labs/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
}
24 changes: 24 additions & 0 deletions ccapi/chaincode/invokeFPCDefault.go
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-labs/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
}
24 changes: 24 additions & 0 deletions ccapi/chaincode/queryFPC.go
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-labs/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
}
24 changes: 24 additions & 0 deletions ccapi/chaincode/queryFPCDefault.go
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-labs/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
}
68 changes: 68 additions & 0 deletions ccapi/common/fpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright IBM Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package common

import (
"fmt"
"os"
"strconv"

pkgFpc "github.com/hyperledger-labs/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"),
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)
}
83 changes: 83 additions & 0 deletions ccapi/fpc-docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
version: "2"
services:
ccapi.org1.example.com:
build:
dockerfile: Dockerfile
context: .
ports:
- 80:80
volumes:
- ./:/rest-server
- /src/github.com/hyperledger/fabric-private-chaincode/samples/deployment/test-network/fabric-samples/test-network/organizations:/fabric/organizations
- /src/github.com/hyperledger/fabric-private-chaincode/samples/deployment/test-network/fabric-samples/test-network/organizations/:/project/src/github.com/hyperledger/fabric-private-chaincode/samples/deployment/test-network/fabric-samples/test-network/organizations/
logging:
options:
max-size: 50m
environment:
- SDK_PATH=./config/configsdk-org1.yaml
- USER=Admin
- ORG=org1
- DOMAIN=example.com
- CHANNEL=mychannel
- CCNAME=cc-tools-demo
- FABRIC_GATEWAY_ENDPOINT=peer0.org1.example.com:7051
- FABRIC_GATEWAY_NAME=peer0.org1.example.com
- GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn
- FPC_ENABLED=true
- SGX_MODE=SIM
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
- CORE_PEER_ID=peer0.org1.example.com
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_PEER_MSPCONFIGPATH=/fabric/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
- CORE_PEER_TLS_CERT_FILE=/fabric/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
- CORE_PEER_TLS_ENABLED="true"
- CORE_PEER_TLS_KEY_FILE=/fabric/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/fabric/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
- ORDERER_CA=/fabric/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
- GATEWAY_CONFIG=/fabric/organizations/peerOrganizations/org1.example.com/external-connection-org1.yaml
working_dir: /rest-server
container_name: ccapi.org1.example.com
networks:
- fabric_test
ccapi.org2.example.com:
build:
dockerfile: Dockerfile
context: .
ports:
- 980:80
volumes:
- ./:/rest-server
- /src/github.com/hyperledger/fabric-private-chaincode/samples/deployment/test-network/fabric-samples/test-network/organizations:/fabric/organizations
- /src/github.com/hyperledger/fabric-private-chaincode/samples/deployment/test-network/fabric-samples/test-network/organizations/:/project/src/github.com/hyperledger/fabric-private-chaincode/samples/deployment/test-network/fabric-samples/test-network/organizations/
logging:
options:
max-size: 50m
environment:
- SDK_PATH=./config/configsdk-org2.yaml
- USER=Admin
- ORG=org2
- DOMAIN=example.com
- CHANNEL=mychannel
- CCNAME=cc-tools-demo
- FABRIC_GATEWAY_ENDPOINT=peer0.org2.example.com:7051
- FABRIC_GATEWAY_NAME=peer0.org2.example.com
- GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn
- FPC_MODE=true
- SGX_MODE=SIM
- CORE_PEER_ADDRESS=peer0.org2.example.com:7051
- CORE_PEER_ID=peer0.org2.example.com
- CORE_PEER_LOCALMSPID=Org2MSP
- CORE_PEER_MSPCONFIGPATH=/fabric/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp
- CORE_PEER_TLS_CERT_FILE=/fabric/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.crt
- CORE_PEER_TLS_ENABLED="true"
- CORE_PEER_TLS_KEY_FILE=/fabric/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/fabric/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
- ORDERER_CA=/fabric/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
- GATEWAY_CONFIG=/fabric/organizations/peerOrganizations/org2.example.com/external-connection-org2.yaml
working_dir: /rest-server
container_name: ccapi.org2.example.com
networks:
- fabric_test
networks:
fabric_test:
external: true
23 changes: 23 additions & 0 deletions ccapi/fpcUtils/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright IBM Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package fpcUtils

type Config struct {
CorePeerAddress string
CorePeerId string
CorePeerLocalMSPID string
CorePeerMSPConfigPath string
CorePeerTLSCertFile string
CorePeerTLSEnabled bool
CorePeerTLSKeyFile string
CorePeerTLSRootCertFile string
OrdererCA string
FpcPath string
ChaincodeId string
ChannelId string
GatewayConfigPath string
}
41 changes: 41 additions & 0 deletions ccapi/fpcUtils/connections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright IBM Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package fpcUtils

import (
"os"

"gopkg.in/yaml.v2"
)

type Connections struct {
Peers map[string]struct {
Url string
}

Orderers map[string]struct {
Url string
}
}

func NewConnections(path string) (*Connections, error) {
connections := &Connections{}

file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()

d := yaml.NewDecoder(file)

if err := d.Decode(&connections); err != nil {
return nil, err
}

return connections, nil
}
Loading