fabric-chaincode-external

背景

native部署 源码链码部分采用两种模式 一个采用原先docker方式,另一个是外部部署,下面是从网络部署情况记录的

Step1. 修改peer(每一个)的core.yaml配置文件

## 启动区块链网络之前 修改 然后再启动
chaincode:
   ...
   externalBuilders:
       - name: external-chaincode
       path: /opt/gopath/src/github.com/hyperledger/fabric/peer/external
       environmentWhitelist:
       - GOPROXY
       - CHAINCODE_CCID
       - CHAINCODE_ADDRESS

Step2. 构建外部目录和脚本

2.1 构建目录

mkdir -p  ${PWD}/external/bin 

2.2 创建脚本

2.2.1 detect

cd ${PWD}/external/bin	   
touch detect	
 #!/bin/sh
 CHAINCODE_METADATA_DIR="$2"

 set -euo pipefail

# use jq to extract the chaincode type from metadata.json and exit with
# success if the chaincode type is golang
if [ "$(cat "$CHAINCODE_METADATA_DIR/metadata.json" | sed -e 's/[{}]/''/g' | awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'type'\042/){print $(i+1)}}}' | tr -d '"')" = "external" ]; then
exit 0
fi

exit 1
chmod +x detect

2.2.2 build

cd ${PWD}/external/bin 
		
touch build
#!/bin/sh
CHAINCODE_SOURCE_DIR="$1"
CHAINCODE_METADATA_DIR="$2"
BUILD_OUTPUT_DIR="$3"

set -euo pipefail

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

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

if [ -d "$CHAINCODE_SOURCE_DIR/metadata" ]; then
cp -a $CHAINCODE_SOURCE_DIR/metadata $BUILD_OUTPUT_DIR/metadata
fi

exit 0
chmod +x build

2.2.3 release

cd ${PWD}/external/bin 
		
touch release
#!/bin/sh
set -euo pipefail

BUILD_OUTPUT_DIR="$1"
RELEASE_OUTPUT_DIR="$2"
#external chaincodes expect artifacts to be placed under "$RELEASE_OUTPUT_DIR"/chaincode/server
if [ -f $BUILD_OUTPUT_DIR/connection.json ]; then
mkdir -p "$RELEASE_OUTPUT_DIR"/chaincode/server
cp $BUILD_OUTPUT_DIR/connection.json "$RELEASE_OUTPUT_DIR"/chaincode/server

#if tls_required is true, copy TLS files (using above example, the fully qualified path for these fils would be "$RELEASE_OUTPUT_DIR"/chaincode/server/tls)

exit 0
fi

exit 1
chmod +x release

Step3. 启动网络

3.1 orderer

./orderer

3.2 org1

./peer node start

3.3 org2

./peer node start

3.4 peer创建通道命令

## 命令参考部署教程

3.5 加入通道

## 命令参考部署教程

3.6 更新锚节点

## 命令参考部署教程

Step4. 构建链码packing

## 注意
##  此处打包的core.tar.gz 只包含connection.json文件 不包含链码源码 这个与使用容器打包不一样

4.1 org1

mkdir ${pwd}/packing
touch  connection.json
{
    "address": "0.0.0.0:7752", #端口是自己独立的 不是7052 避免端口冲突
    "dial_timeout": "10s",
    "tls_required": false,
    "client_auth_required": false,
    "client_key": "-----BEGIN EC PRIVATE KEY----- ... -----END EC PRIVATE KEY-----",
    "client_cert": "-----BEGIN CERTIFICATE----- ... -----END CERTIFICATE-----",
    "root_cert": "-----BEGIN CERTIFICATE---- ... -----END CERTIFICATE-----"
}
tar cfz code.tar.gz connection.json
touch metadata.json
## label 基本都是链码名称
{"path":"","type":"external","label":"mycc"}
tar cfz mycc.tgz code.tar.gz metadata.json
mv mycc.tgz ../

4.2 org2

mkdir ${pwd}/packing
touch  connection.json
{
    "address": "0.0.0.0:7752",
    "dial_timeout": "10s",
    "tls_required": false,
    "client_auth_required": false,
    "client_key": "-----BEGIN EC PRIVATE KEY----- ... -----END EC PRIVATE KEY-----",
    "client_cert": "-----BEGIN CERTIFICATE----- ... -----END CERTIFICATE-----",
    "root_cert": "-----BEGIN CERTIFICATE---- ... -----END CERTIFICATE-----"
}
tar cfz code.tar.gz connection.json
touch metadata.json
{"path":"","type":"external","label":"mycc"}
tar cfz mycc.tgz code.tar.gz metadata.json
mv mycc.tgz ../

Step5. 安装包

5.1 org1

./peer lifecycle chaincode install mycc.tgz
export CHAINCODE_CCID=mycc:108295702fdd32d56a22854f8b476a927239c0daa4d718911df40f6ffa74f82b

export PACKAGE_ID=mycc:108295702fdd32d56a22854f8b476a927239c0daa4d718911df40f6ffa74f82b

export CHAINCODE_ADDRESS=127.0.0.1:7752

5.2 org2

./peer lifecycle chaincode install mycc.tgz
export CHAINCODE_CCID=mycc:283082de173345a622e96758703e68d0749e85d096736839aeb3b0e224d78231


export PACKAGE_ID=mycc:283082de173345a622e96758703e68d0749e85d096736839aeb3b0e224d78231


export CHAINCODE_ADDRESS=127.0.0.1:7752

Step6. 修改链码

func main() {
	archiveChaincode, err := contractapi.NewChaincode(&chaincode.SmartContract{})
	if err != nil {
		log.Panicf("Error creating archive chaincode: %v", err)
	}

	server := &shim.ChaincodeServer{
		CCID:    os.Getenv("CHAINCODE_CCID"),
		Address: os.Getenv("CHAINCODE_ADDRESS"),
		CC:      archiveChaincode,
		TLSProps: shim.TLSProperties{
			Disabled: true,
		},
	}
	if err := server.Start(); err != nil {
		log.Panicf("Error starting archive chaincode: %v", err)
	}
}

Step7. 运行链码

## 对链码进行编译启动  检查端口7752是否启动
go bulid -o mycc
./mycc

Step8. approveformyorg

## 接下来与原生命令相同
./peer lifecycle chaincode approveformyorg  .......
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值