本篇文章的重點是我們將使用Fabric-Evm在Hyperledger架構網路上部署以太坊智慧合約,並對以太坊智慧合約進行跨平臺檢查。構成元件1.Hyperledger Fabric EVM在Hyperledger架構peer上執行的自定義以太坊虛擬機器2.Fab3代理EVM相容區塊鏈(Hyperledger)節點客戶端,用於與節點進行互動。3.Web3.js一個用於與本地或遠端區塊鏈節點進行互動的javascript庫。 在此專案場景中,它將與Fab3互動。4.BYFN架構網路一個簡單的Byfn網路可讓您建立以2個組織執行的Hyperledger Fabric網路,每個ORG具有兩個peer。工作流程1.構建Fabric網路2.將EVM安裝在Fabric-peers上。3.啟動Fabric網路4.安裝和例項化EVM Chaincode5.設定Fab36.部署Smartcontract。先決條件1.Go安裝Go並將其新增到您的路徑wget https://dl.google.com/go/go1.12.6.linux-amd64.tar.gzsudo tar -xvf go1.12.6.linux-amd64.tar.gzsudo mv go /usr/localexport GOROOT=/usr/local/goexport GOPATH=$HOME/go #choose your prferred work directory pathexport PATH=$GOPATH/bin:$GOROOT/bin:$PATHgo version #check if it is installed correctly2.node.js 和 Npmsudo apt-get install nodejssudo apt-get install npm3.Dockersudo apt install apt-transport-https ca-certificates curl software-properties-commoncurl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"sudo apt updateapt-cache policy docker-cesudo apt install docker-ce操作步驟1.構建Fabric網路將Fabric樣本複製到$ GOPATH / src / github.com / hyperledger目錄。 為此我們需要先建立目錄,然後進行復制。mkdir gocd gomkdir srccd $GOPATH/srcmkdir github.comcd github.commkdir hyperledgercd hyperledgergit clone https://github.com/hyperledger/fabric-samples.gitcd fabric-samplesgit checkout release-1.4檔案架構go├─src│ ├─github.com│ │--hyperledger │--fabric-samples │--fabric-chaincode-evm為fabric samples first網路下載docker的映象./scripts/bootstrap.sh2.將EVM安裝在Fabric-peers上在GOPATH /目錄中複製fabric-chaincode-evm到儲存庫。cd $GOPATH/src/github.com/hyperledger/git clone https://github.com/hyperledger/fabric-chaincode-evmcd fabric-chaincode-evmgit checkout release-0.1我們需要將fabric-chaincode-evm嵌入到我們的第一個網路docker映像中。為此請導航回fabric-samples / first-network目錄。cd $GOPATH/src/github.com/hyperledger/fabric-samples/first-network使用儲存卷更新docker-compose-cli.yaml以包含EVM。使用任何程式碼編輯器新增如下所示的EVM卷cli: volumes: - ./../../fabric-chaincode-evm:/opt/gopath/src/github.com/hyperledger/fabric-chaincode-evm3.啟動fabric網路生成證書並啟動網路./byfn.sh generate./byfn.sh up4.安裝和例項化EVM Chaincode現在我們需要在evm上安裝並例項化chaincode。導航到docker容器的CLIdocker exec -it cli bash新增網路所需的peer依賴項。export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/admin@chaindaily/mspexport CORE_PEER_ADDRESS=peer0.org1.example.com:7051export CORE_PEER_LOCALMSPID=”Org1MSP”export 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在所有peer節點上安裝並例項化Chaincodepeer chaincode install -n evmcc -l golang -v 0 -p github.com/hyperledger/fabric-chaincode-evm/evmcc #Installs Chain codepeer chaincode instantiate -n evmcc -v 0 -C mychannel -c ‘{“Args”:[]}’ -o orderer.example.com:7050 — tls — cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem #Instantiates Chaincode on all peersexit5.設定Fab3執行以下操作來設定設定Fab3所需的某些環境變數。export FABPROXY_CONFIG=${GOPATH}/src/github.com/hyperledger/fabric-chaincode-evm/examples/first-network-sdk-config.yaml # Path to a compatible Fabric SDK Go config fileexport FABPROXY_USER=User1 # User identity being used for the proxy (Matches the users names in the crypto-config directory specified in the config)export FABPROXY_ORG=Org1 # Organization of the specified userexport FABPROXY_CHANNEL=mychannel # Channel to be used for thetransactionsexport FABPROXY_CCID=evmcc # ID of the EVM Chaincode deployed in your fabric networkexport PORT=5000 # Port the proxy will listen on. If not provided default is 5000.現在導航回到fabric-chaincode-evm目錄構建和執行代理cd $GOPATH/src/github.com/hyperledger/fabric-chaincode-evm/go build -o fab3 ./fabproxy/cmd #builds the proxy with predefined env variables./fab3 #starts proxy on your host現在您的Fabric網路已啟動並在http//localhost:5000上執行,並準備部署Smartcotracts6.部署智慧合約開啟另一個終端,安裝web3庫npm install admin@chaindaily建立您選擇的工作目錄。 在該目錄上執行npm install以安裝所需的npm模組。現在我們將進入節點控制檯以設定我們的web3。 在工作目錄中的命令下面執行node分配Web3庫並使用在上一個終端中執行的fab3作為提供程式Web3 = require(‘web3’)web3 = new Web3(new Web3.providers.HttpProvider(‘http://localhost:5000'))要檢視您的帳戶資訊,請執行web3.eth.accounts您會看到類似於以太坊的帳戶地址。將此帳戶分配為defaultAccountweb3.eth.defaultAccount = web3.eth.accounts[0]現在您可以透過以與以太坊相似的方式指向Abi和bytecode來部署智慧合約。在remix或truffle編制智慧合約。為部署做好abi和bytecode準備。abi=<paste abi here>byteCode=<paste your Byte Code here>contract= web3.eth.contract(abi)deployedContract= contract.new([], { data: byteCode })您可以使用如下所示的已部署智慧合約的交易雜湊獲取智慧合約地址web3.eth.getTransactionReceipt(deployedContract.transactionHash)
相關資料連結:
1. [Hyperledger Fabric EVM](https://github.com/hyperledger/fabric-chaincode-evm "Hyperledger Fabric EVM")
2. [Fabric-samples](https://github.com/hyperledger/fabric-samples "Fabric-samples")
3. [Hyperledger Fabric docs](https://hyperledger-fabric.readthedocs.io/en/latest/build_network.html "Hyperledger Fabric docs")
4. [IBM Hyperledger-Ethereum](https://github.com/IBM/vote-hyperledger-ethereum "IBM Hyperledger-Ethereum")
5. [web3.js](https://web3js.readthedocs.io/en/1.0/ "web3.js")