1. 程式人生 > >Fabric cli中建立channel和呼叫chaincode

Fabric cli中建立channel和呼叫chaincode

#!/bin/bash
# Copyright London Stock Exchange Group All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
echo
echo " ____    _____      _      ____    _____           _____   ____    _____ "
echo "/ ___|  |_   _|    / \    |  _ \  |_   _|         | ____| |___ \  | ____|"
echo "\___ \    | |     / _ \   | |_) |   | |    _____  |  _|     __) | |  _|  "
echo " ___) |   | |    / ___ \  |  _ <    | |   |_____| | |___   / __/  | |___ "
echo "|____/    |_|   /_/   \_\ |_| \_\   |_|           |_____| |_____| |_____|"
echo




CHANNEL_NAME="$1"
: ${CHANNEL_NAME:="mychannel"}
: ${TIMEOUT:="60"}
COUNTER=1
MAX_RETRY=5
ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer2.example.com/msp/tlscacerts/tlsca.example.com-cert.pem




echo "Channel name : "$CHANNEL_NAME




verifyResult () {
if [ $1 -ne 0 ] ; then
echo "!!!!!!!!!!!!!!! "$2" !!!!!!!!!!!!!!!!"
                echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="
echo
   exit 1
fi
}




setGlobals () {




if [ $1 -eq 0 -o $1 -eq 1 ] ; then
CORE_PEER_LOCALMSPID="Org1MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/
[email protected]
/msp
if [ $1 -eq 0 ]; then
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
else
CORE_PEER_ADDRESS=peer1.org1.example.com:7051
fi
else
CORE_PEER_LOCALMSPID="Org2MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/
[email protected]
/msp
if [ $1 -eq 2 ]; then
CORE_PEER_ADDRESS=peer0.org2.example.com:7051
else
CORE_PEER_ADDRESS=peer1.org2.example.com:7051
fi
fi




env |grep CORE
}




createChannel() {
setGlobals 0




        if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
peer channel create -o orderer2.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx >&log.txt
else
peer channel create -o orderer2.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt
fi
res=$?
cat log.txt
verifyResult $res "Channel creation failed"
echo "===================== Channel \"$CHANNEL_NAME\" is created successfully ===================== "
echo
}




updateAnchorPeers() {
        PEER=$1
        setGlobals $PEER




        if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
peer channel update -o orderer2.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt
else
peer channel update -o orderer2.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt
fi
res=$?
cat log.txt
verifyResult $res "Anchor peer update failed"
echo "===================== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on \"$CHANNEL_NAME\" is updated successfully ===================== "
sleep 5
echo
}




## Sometimes Join takes time hence RETRY atleast for 5 times
joinWithRetry () {
peer channel join -b $CHANNEL_NAME.block  >&log.txt
res=$?
cat log.txt
if [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; then
COUNTER=` expr $COUNTER + 1`
echo "PEER$1 failed to join the channel, Retry after 2 seconds"
sleep 2
joinWithRetry $1
else
COUNTER=1
fi
        verifyResult $res "After $MAX_RETRY attempts, PEER$ch has failed to Join the Channel"
}




joinChannel () {
for ch in 0 1 2 3; do
setGlobals $ch
joinWithRetry $ch
echo "===================== PEER$ch joined on the channel \"$CHANNEL_NAME\" ===================== "
sleep 2
echo
done
}




installChaincode () {
PEER=$1
setGlobals $PEER
peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 >&log.txt
res=$?
cat log.txt
        verifyResult $res "Chaincode installation on remote peer PEER$PEER has Failed"
echo "===================== Chaincode is installed on remote peer PEER$PEER ===================== "
echo
}




instantiateChaincode () {
PEER=$1
setGlobals $PEER
# while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
# lets supply it directly as we know it using the "-o" option
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
peer chaincode instantiate -o orderer2.example.com:7050 -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR('Org1MSP.member','Org2MSP.member')" >&log.txt
else
peer chaincode instantiate -o orderer2.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR('Org1MSP.member','Org2MSP.member')" >&log.txt
fi
res=$?
cat log.txt
verifyResult $res "Chaincode instantiation on PEER$PEER on channel '$CHANNEL_NAME' failed"
echo "===================== Chaincode Instantiation on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
echo
}




chaincodeQuery () {
  PEER=$1
  echo "===================== Querying on PEER$PEER on channel '$CHANNEL_NAME'... ===================== "
  setGlobals $PEER
  local rc=1
  local starttime=$(date +%s)




  # continue to poll
  # we either get a successful response, or reach TIMEOUT
  while test "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0
  do
     sleep 3
     echo "Attempting to Query PEER$PEER ...$(($(date +%s)-starttime)) secs"
     peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' >&log.txt
     test $? -eq 0 && VALUE=$(cat log.txt | awk '/Query Result/ {print $NF}')
     test "$VALUE" = "$2" && let rc=0
  done
  echo
  cat log.txt
  if test $rc -eq 0 ; then
echo "===================== Query on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
  else
echo "!!!!!!!!!!!!!!! Query result on PEER$PEER is INVALID !!!!!!!!!!!!!!!!"
        echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="
echo
exit 1
  fi
}




chaincodeInvoke () {
PEER=$1
setGlobals $PEER
# while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
# lets supply it directly as we know it using the "-o" option
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
peer chaincode invoke -o orderer2.example.com:7050 -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt
else
peer chaincode invoke -o orderer2.example.com:7050  --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt
fi
res=$?
cat log.txt
verifyResult $res "Invoke execution on PEER$PEER failed "
echo "===================== Invoke transaction on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
echo
}




## Create channel
echo "Creating channel..."
createChannel




## Join all the peers to the channel
echo "Having all peers join the channel..."
joinChannel




## Set the anchor peers for each org in the channel
echo "Updating anchor peers for org1..."
updateAnchorPeers 0
echo "Updating anchor peers for org2..."
updateAnchorPeers 2




## Install chaincode on Peer0/Org1 and Peer2/Org2
echo "Installing chaincode on org1/peer0..."
installChaincode 0
echo "Install chaincode on org2/peer2..."
installChaincode 2




#Instantiate chaincode on Peer2/Org2
echo "Instantiating chaincode on org2/peer2..."
instantiateChaincode 2




#Query on chaincode on Peer0/Org1
echo "Querying chaincode on org1/peer0..."
chaincodeQuery 0 100




#Invoke on chaincode on Peer0/Org1
echo "Sending invoke transaction on org1/peer0..."
chaincodeInvoke 0




## Install chaincode on Peer3/Org2
echo "Installing chaincode on org2/peer3..."
installChaincode 3




#Query on chaincode on Peer3/Org2, check if the result is 90
echo "Querying chaincode on org2/peer3..."
chaincodeQuery 3 90




echo
echo "===================== All GOOD, End-2-End execution completed ===================== "
echo




echo
echo " _____   _   _   ____            _____   ____    _____ "
echo "| ____| | \ | | |  _ \          | ____| |___ \  | ____|"
echo "|  _|   |  \| | | | | |  _____  |  _|     __) | |  _|  "
echo "| |___  | |\  | | |_| | |_____| | |___   / __/  | |___ "
echo "|_____| |_| \_| |____/          |_____| |_____| |_____|"
echo




exit 0

相關推薦

Fabric cli建立channel呼叫chaincode

#!/bin/bash# Copyright London Stock Exchange Group All Rights Reserved.## SPDX-License-Identifier: Apache-2.0#echoecho " ____    _____      _      ____   

UAP-STUDIO建立專案打包專案

在65中建立專案的方式:右鍵---新建---其它---UAP project development---找到業務元件專案;然後建立業務元件(元件是釋出的最小單元)建立步驟和專案步驟類似,UAP project development下選擇業務元件即可,只有建立了業務元件,整

php FastDFS開啟呼叫使用

php 中FastDFS開啟和呼叫使用 1 <?php 2 3 if (!class_exists('FastDFS', false)) { 4 define('FDFS_PROTO_PKG_LEN_SIZE', 8); 5

HyperLedger(1)啟動Fabric及建立channel、部署chaincode

HyperLedger 前提 git clone https://github.com/hyperledger/fabric.git #編譯configtxgen工具 cd $GOPATH

oracle 在plsql建立procedure並呼叫

Create table create table A   (   USERID NUMBER(38),   PWD VARCHAR2(30)   )   tablespace USERS   pctfree 10   initrans 1   max

Vux在vue-cli的搭建配置

一、vue安裝(node.js)1、安裝node.js https://nodejs.org/en/下載安裝2、安裝淘寶映象檢查方式 cnpm -V  出現版本號3、安裝webpacknpm install webpack -g檢查方式 webpack -V4、安裝腳手架np

Mysql建立使用者授權的方法

建立使用者語句:   create user 使用者名稱; eg: create user sa; 給建立的使用者授權:   grant 許可權1,許可權2,...許可權n on 資料庫名稱.表名稱 to 使用者名稱@使用者地址 identified by '連線口令';

Vue:如何在vue-cli建立並引入自定義元件

一、建立並引入一個元件 1、建立元件 vue-cli中的所有元件都是存放在components資料夾下面的,所以在components資料夾下面建立一個名為First.vue的自定義元件: <template> <div> <h1>{{m

shell建立陣列遍歷

linux 中定義一個數據的語法為: variable=(arg1 arg2 arg3 ....) 中間用空格分開。陣列的下標從0開始。 1  獲取下標為n的元素: variable[n] 而且不存在陣列下標溢位的情況,如果 n>= 陣列的長度,那麼為空,

Qt建立按鈕文字編輯框

本文目標是使用程式碼直接建立按鈕和文字編輯框 主要步驟是 1.在main函式中包含Qt按鈕類QPushButton和文字編輯框類QTextEdit 2.新建物件 3.顯示 程式碼如下:

如何在Android Studio建立jniLibasset資料夾

1.建立asset資料夾 如圖進行操作 2.建立jniLib資料夾 —開啟app下面的gradle檔案(不是project的gradle) —在gradle檔案的Android標籤裡面新增  sourceSets.main.jniLibs.srcDirs

Swift建立ArrayDictionary的初始化

首先看下Swift中初始化Array的方法,下面的型別都以Int為例 var a1 = [1, 2, 3] var a2: Int[] = [1, 2, 3] var a3: Int[] = Int[]([1, 2, 3]) var a4: Int[] = Array<

ros建立msgsrv檔案時,配置CMakeLists.txt檔案問題

作為一個ROS菜鳥,在按照ros wiki上的教程一步一步的走的過程中,在自己配置msg和srv檔案時,遇到了編譯的問題,分析問題,發現是package下的CMakeLists.txt檔案配置出現問題。 以下是建立並編譯一個新的package後生成的CMakeLists.t

利用反射來動態建立例項呼叫方法

1.使用Module瞭解包含模組的程式集以及模組中的類等,還可以獲取在模組上定義的所有全域性方法或其它特定的非全域性方法。 2.使用ConstructorInfo瞭解建構函式的名稱、引數、訪問修飾符(如public 或private)和實現詳細資訊(如abstract或virtual)等。使用Type的Get

rails建立model 修改欄位的方法

轉自:http://blog.csdn.net/remote_roamer/article/details/7977294 1. 新建一個model .用如下命令  rails g model category1 code:string name:string me

MFC:建立dll呼叫dll

一、建立dll檔案:1、開啟VS2010,新建Win32專案,如下圖所示:2、應用程式型別,選取DLL3、新增新類 DigitalProc,在標頭檔案 DigitalProc.h中新增如下程式碼:#if

建立呼叫儲存過程:查詢Stu資料庫某個同學的選修課程的資訊,包括學號,姓名,課程名稱,成績

CREATE PROCEDURE proc_select--建立儲存過程 @Sno char(10) output,--輸入輸出引數 @Sname varchar(20) out,--輸出引數 @Cno char(4) out,--輸出引數 @grade tinyint

Oracle建立儲存過程呼叫過程(一)

1、定義         所謂儲存過程(Stored Procedure),就是一組用於完成特定資料庫功能的SQL語句集,該SQL語句集經過 編譯後儲存在資料庫系統中。在使用時候,使用者通過指定已經定義的儲存過程名字並給出相應的儲存過程引數 來呼叫並執行

定義一個包含私有成員變數函式的類,再定義一個內部類,在內部類函式訪問外部成員變數,並呼叫外部函式。在外部類函式建立內部類物件,呼叫內部類函式

public class Test5 {         //定義包含私有成員變數和函式         private int a = 201320883;         private voi

codeblocks建立呼叫動態連結庫(dll)

一、建立C語言動態連結庫 1.建立。 File->New->Projects->Dynamic Link library->Go 給專案命名為:Dynamic librar