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

Support network with chaincode-as-a-service #71

Merged
merged 9 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ To apply chaincode changes, run `$ ./upgradeCC.sh <version> <sequence>` with a v

To apply CC API changes, run `$ ./reloadCCAPI.sh`.

## Deploying Chaincode as a service

After installing, use the script `./startDev.sh -ccaas` in the root folder to start the development environment. It will
start all components of the project with 3 organizations.

If you want to deploy with 1 organization, run the command `./startDev.sh -ccaas -n 1`.

To apply chaincode changes, run `$ ./upgradeCC.sh -ccaas <version> <sequence>` with a version higher than the current one (starts with 0.1). Append `-n 1` to the command if running with 1 organization.

To apply CC API changes, run `$ ./reloadCCAPI.sh`.

## Automated tryout and test

To test transactions after starting all components, run `$ ./tryout.sh`.
Expand Down
18 changes: 18 additions & 0 deletions chaincode/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0

ARG GO_VER=1.21
ARG ALPINE_VER=3.20
ARG CC_SERVER_PORT=9999

FROM golang:${GO_VER}-alpine${ALPINE_VER}

WORKDIR /go/src/github.com/hyperledger-labs/cc-tools-demo
COPY . .

RUN go get -d -v .
RUN go build -o cc-tools-demo -v .

EXPOSE ${CC_SERVER_PORT}
CMD ["./cc-tools-demo"]
71 changes: 70 additions & 1 deletion chaincode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"log"
"os"
"time"

"github.com/hyperledger-labs/cc-tools-demo/chaincode/assettypes"
Expand Down Expand Up @@ -62,11 +63,79 @@ func main() {
if err != nil {
return
}
if err = shim.Start(new(CCDemo)); err != nil {

if os.Getenv("RUN_CCAAS") == "true" {
err = runCCaaS()
} else {
err = shim.Start(new(CCDemo))
}

if err != nil {
fmt.Printf("Error starting chaincode: %s", err)
}
}

func runCCaaS() error {
address := os.Getenv("CHAINCODE_SERVER_ADDRESS")
ccid := os.Getenv("CHAINCODE_ID")

tlsProps, err := getTLSProperties()
if err != nil {
return err
}

server := &shim.ChaincodeServer{
CCID: ccid,
Address: address,
CC: new(CCDemo),
TLSProps: *tlsProps,
}

return server.Start()
}

func getTLSProperties() (*shim.TLSProperties, error) {
if enableTLS := os.Getenv("TLS_ENABLED"); enableTLS != "true" {
return &shim.TLSProperties{
Disabled: true,
}, nil
}

log.Printf("TLS enabled")

// Get key
keyPath := os.Getenv("KEY_PATH")
key, err := os.ReadFile(keyPath)

if err != nil {
fmt.Println("Failed to read key file")
return nil, err
}

// Get cert
certPath := os.Getenv("CERT_PATH")
cert, err := os.ReadFile(certPath)
if err != nil {
fmt.Println("Failed to read cert file")
return nil, err
}

// Get CA cert
clientCertPath := os.Getenv("CA_CERT_PATH")
caCert, err := os.ReadFile(clientCertPath)
if err != nil {
fmt.Println("Failed to read CA cert file")
return nil, err
}

return &shim.TLSProperties{
Disabled: false,
Key: key,
Cert: cert,
ClientCACerts: caCert,
}, nil
}

// CCDemo implements the shim.Chaincode interface
type CCDemo struct{}

Expand Down
2 changes: 1 addition & 1 deletion fabric/config/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ chaincode:
# The path must be an absolute path.
externalBuilders:
- name: ccaas_builder
path: /opt/hyperledger/ccaas_builder
path: /opt/hyperledger/external-builder
propagateEnvironment:
- CHAINCODE_AS_A_SERVICE_BUILDER_CONFIG

Expand Down
11 changes: 11 additions & 0 deletions fabric/docker/docker-compose-test-net-org.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ services:
- FABRIC_LOGGING_SPEC=INFO
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
- ORDERER_GENERAL_LISTENPORT=7050
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
- ORDERER_GENERAL_LOCALMSPID=OrdererMSP
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
- ORDERER_CHANNELPARTICIPATION_ENABLED=true
Expand Down Expand Up @@ -82,12 +84,21 @@ services:
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org.example.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org.example.com:7051
- CORE_PEER_LOCALMSPID=orgMSP
- CORE_CHAINCODE_EXTERNALBUILDERS=[{"name":"basic-builder","path":"/etc/hyperledger/external-builder","propagateEnvironment":["CCAAS_CLIENT_CERT_FILE","CCAAS_CLIENT_KEY_FILE","CCAAS_ROOT_CERT_FILE","CCAAS_ORG","CCAAS_PORT"]}]
- CCAAS_CLIENT_CERT_FILE=/etc/hyperledger/fabric/ccaas/tls/client/cert.pem
- CCAAS_CLIENT_KEY_FILE=/etc/hyperledger/fabric/ccaas/tls/client/key.pem
- CCAAS_ROOT_CERT_FILE=/etc/hyperledger/fabric/ccaas/tls/ca/cert.pem
- CCAAS_ORG=org
- CCAAS_PORT=9999
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/msp
volumes:
- /var/run/docker.sock:/host/var/run/docker.sock
- ../organizations/peerOrganizations/org.example.com/peers/peer0.org.example.com/msp:/etc/hyperledger/fabric/msp
- ../organizations/peerOrganizations/org.example.com/peers/peer0.org.example.com/tls:/etc/hyperledger/fabric/tls
- ../organizations/ccaas/org.example.com:/etc/hyperledger/fabric/ccaas/tls
- peer0.org.example.com:/var/hyperledger/production
- ../externalBuilder:/etc/hyperledger/external-builder
- ../bin/jq:/usr/local/bin/jq
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
ports:
Expand Down
29 changes: 29 additions & 0 deletions fabric/docker/docker-compose-test-net.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ services:
- FABRIC_LOGGING_SPEC=INFO
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
- ORDERER_GENERAL_LISTENPORT=7050
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
- ORDERER_GENERAL_LOCALMSPID=OrdererMSP
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
- ORDERER_CHANNELPARTICIPATION_ENABLED=true
Expand Down Expand Up @@ -84,12 +86,21 @@ services:
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org1.example.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
- CORE_PEER_LOCALMSPID=org1MSP
- CORE_CHAINCODE_EXTERNALBUILDERS=[{"name":"basic-builder","path":"/etc/hyperledger/external-builder","propagateEnvironment":["CCAAS_CLIENT_CERT_FILE","CCAAS_CLIENT_KEY_FILE","CCAAS_ROOT_CERT_FILE","CCAAS_ORG","CCAAS_PORT"]}]
- CCAAS_CLIENT_CERT_FILE=/etc/hyperledger/fabric/ccaas/tls/client/cert.pem
- CCAAS_CLIENT_KEY_FILE=/etc/hyperledger/fabric/ccaas/tls/client/key.pem
- CCAAS_ROOT_CERT_FILE=/etc/hyperledger/fabric/ccaas/tls/ca/cert.pem
- CCAAS_ORG=org1
- CCAAS_PORT=9999
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/msp
volumes:
- /var/run/docker.sock:/host/var/run/docker.sock
- ../organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/fabric/msp
- ../organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tls
- ../organizations/ccaas/org1.example.com:/etc/hyperledger/fabric/ccaas/tls
- peer0.org1.example.com:/var/hyperledger/production
- ../externalBuilder:/etc/hyperledger/external-builder
- ../bin/jq:/usr/local/bin/jq
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
ports:
Expand Down Expand Up @@ -124,12 +135,21 @@ services:
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org2.example.com:7051
- CORE_PEER_LOCALMSPID=org2MSP
- CORE_CHAINCODE_EXTERNALBUILDERS=[{"name":"basic-builder","path":"/etc/hyperledger/external-builder","propagateEnvironment":["CCAAS_CLIENT_CERT_FILE","CCAAS_CLIENT_KEY_FILE","CCAAS_ROOT_CERT_FILE","CCAAS_ORG","CCAAS_PORT"]}]
- CCAAS_CLIENT_CERT_FILE=/etc/hyperledger/fabric/ccaas/tls/client/cert.pem
- CCAAS_CLIENT_KEY_FILE=/etc/hyperledger/fabric/ccaas/tls/client/key.pem
- CCAAS_ROOT_CERT_FILE=/etc/hyperledger/fabric/ccaas/tls/ca/cert.pem
- CCAAS_ORG=org2
- CCAAS_PORT=9998
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/msp
volumes:
- /var/run/docker.sock:/host/var/run/docker.sock
- ../organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp:/etc/hyperledger/fabric/msp
- ../organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls:/etc/hyperledger/fabric/tls
- ../organizations/ccaas/org2.example.com:/etc/hyperledger/fabric/ccaas/tls
- peer0.org2.example.com:/var/hyperledger/production
- ../externalBuilder:/etc/hyperledger/external-builder
- ../bin/jq:/usr/local/bin/jq
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
ports:
Expand Down Expand Up @@ -164,12 +184,21 @@ services:
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org3.example.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org3.example.com:7051
- CORE_PEER_LOCALMSPID=org3MSP
- CORE_CHAINCODE_EXTERNALBUILDERS=[{"name":"basic-builder","path":"/etc/hyperledger/external-builder","propagateEnvironment":["CCAAS_CLIENT_CERT_FILE","CCAAS_CLIENT_KEY_FILE","CCAAS_ROOT_CERT_FILE","CCAAS_ORG","CCAAS_PORT"]}]
- CCAAS_CLIENT_CERT_FILE=/etc/hyperledger/fabric/ccaas/tls/client/cert.pem
- CCAAS_CLIENT_KEY_FILE=/etc/hyperledger/fabric/ccaas/tls/client/key.pem
- CCAAS_ROOT_CERT_FILE=/etc/hyperledger/fabric/ccaas/tls/ca/cert.pem
- CCAAS_ORG=org3
- CCAAS_PORT=9997
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/msp
volumes:
- /var/run/docker.sock:/host/var/run/docker.sock
- ../organizations/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/msp:/etc/hyperledger/fabric/msp
- ../organizations/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls:/etc/hyperledger/fabric/tls
- ../organizations/ccaas/org3.example.com:/etc/hyperledger/fabric/ccaas/tls
- peer0.org3.example.com:/var/hyperledger/production
- ../externalBuilder:/etc/hyperledger/external-builder
- ../bin/jq:/usr/local/bin/jq
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
ports:
Expand Down
21 changes: 21 additions & 0 deletions fabric/externalBuilder/bin/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

# set -euo pipefail

SOURCE=$1
OUTPUT=$3

#external chaincodes expect connection.json file in the chaincode package
if [ ! -f "$SOURCE/connection.json" ]; then
>&2 echo "$SOURCE/connection.json not found"
exit 1
fi

#simply copy the endpoint information to specified output location
cp $SOURCE/connection.json $OUTPUT/connection.json

if [ -d "$SOURCE/metadata" ]; then
cp -a $SOURCE/metadata $OUTPUT/metadata
fi

exit 0
14 changes: 14 additions & 0 deletions fabric/externalBuilder/bin/detect
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -euo pipefail

METADIR=$2
# check if the "type" field is set to "external"
# crude way without jq which is not in the default fabric peer image
TYPE=$(tr -d '\n' < "$METADIR/metadata.json" | awk -F':' '{ for (i = 1; i < NF; i++){ if ($i~/type/) { print $(i+1); break }}}'| cut -d\" -f2)

if [ "$TYPE" = "external" ]; then
exit 0
fi

exit 1
38 changes: 38 additions & 0 deletions fabric/externalBuilder/bin/release
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

set -euo pipefail

BLD="$1"
RELEASE="$2"

if [ -d "$BLD/metadata" ]; then
cp -a "$BLD/metadata/"* "$RELEASE/"
fi

#external chaincodes expect artifacts to be placed under "$RELEASE"/chaincode/server
if [ -f $BLD/connection.json ]; then
mkdir -p "$RELEASE"/chaincode/server

sed -i "s/{{.org}}/$CCAAS_ORG/" $BLD/connection.json
sed -i "s/{{.port}}/$CCAAS_PORT/" $BLD/connection.json

# Check if tls is enabled
if [ "$(jq -r .tls_required $BLD/connection.json)" = "true" ]; then
client_cert=$(awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}' "$CCAAS_CLIENT_CERT_FILE")
client_key=$(awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}' "$CCAAS_CLIENT_KEY_FILE")
root_cert=$(awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}' "$CCAAS_ROOT_CERT_FILE")

# Add certs files to connection.json
echo $(jq ".client_cert=\"$client_cert\"" $BLD/connection.json) > $BLD/connection.json
echo $(jq ".client_key=\"$client_key\"" $BLD/connection.json) > $BLD/connection.json
echo $(jq ".root_cert=\"$root_cert\"" $BLD/connection.json) > $BLD/connection.json
fi

cp $BLD/connection.json "$RELEASE"/chaincode/server

exit 0
fi

exit 1


Loading
Loading