Deploying Ethereum Smartcontracts on HyperledgerFabric
Who This Article is for?
This article is for those Who are Currently On Blockchain. knowledge on Ethereum and Hyperledger Eco-Systems is necessary to understand These Concepts.
What We Are going to do?
The point is very clear that we are going to deploy Ethereum Smartcontracts on Hyperledger fabric Network using Fabric-Evm and doing Cross-Platform check for Ethereum Smartcontracts.
Components
- Hyperledger Fabric EVM
Customized Ethereum virtual machine Running on Hyperledger fabric peers
2.Fab3 Proxy
EVM Compatiable Blockchain(Hyperledger) node client for interacting with nodes.
3.Web3.js
A javascript library for interacting with local or remote blockchain node. in this project scenario, it will interacts Fab3.
4.A BYFN Fabric Network
A simple Byfn network allows you to create Hyperledger Fabric network Running with 2 Organizations each ORG with two peers.
Work Flow
1. Building Fabric network
2. Mounting EVM on Fabric-peers.
3. Starting fabric network
4. Installing and Instantiating EVM Chaincode
5. Setting up Fab3
6. Deploying Smartcontract.
Pre-Requisites
1. Go
Installing Go and add it to your path
wget https://dl.google.com/go/go1.12.6.linux-amd64.tar.gzsudo tar -xvf go1.12.6.linux-amd64.tar.gz sudo mv go /usr/local export GOROOT=/usr/local/go export GOPATH=$HOME/go #choose your prferred work directory path export PATH=$GOPATH/bin:$GOROOT/bin:$PATHgo version #check if it is installed correctly
2. node.js and Npm
sudo apt-get install nodejs
sudo apt-get install npm
3. Docker
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -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 update
apt-cache policy docker-ce
sudo sudo curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo mv /usr/local/bin/docker-compose /usr/bin/docker-compose
sudo chmod +x /usr/bin/docker-composeapt install docker-ce
1. Building Fabric Network
Clone fabric-samples to your $GOPATH/src/github.com/hyperledger directory. for this, we need to create directories first and then clone
cd $GOPATH/src mkdir github.com cd github.com mkdir hyperledger cd hyperledger git clone https://github.com/hyperledger/fabric-samples.gitcd fabric-samples git checkout release-1.4
Download docker images for fabric-samples first network
./scripts/bootstrap.sh
2. Mounting EVM on Fabric-peers.
Clone the fabric-chaincode-evm repo in your GOPATH/ directory.
cd $GOPATH/src/github.com/hyperledger/ git clone https://github.com/hyperledger/fabric-chaincode-evmcd fabric-chaincode-evm git checkout release-0.1
We need to include fabric-chaincode-evm to our first-network docker images. for this, Navigate back to fabric-samples/first-network directory.
cd $GOPATH/src/github.com/hyperledger/fabric-samples/first-network
Update docker-compose-cli.yaml with the volumes to include EVM. add EVM volume like below using any code editor
cli:
volumes:
- ./../../fabric-chaincode-evm:/opt/gopath/src/github.com/hyperledger/fabric-chaincode-evm
3. Starting fabric-Network
Generate certificates and start your network
./byfn.sh generate
./byfn.sh up
4. Installing and Instantiating EVM Chaincode
Now we need to Install and instantiate chaincode on evm. navigate to cli of docker container
docker exec -it cli bash
Add required Peer dependies for network.
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
export 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
Install and Instantiate Chaincode on all peers
peer 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 ‘’ -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 peersexit
Execute the following to set certain environment variables required for setting up Fab3.
export FABPROXY_CONFIG=$/src/github.com/hyperledger/fabric-chaincode-evm/examples/first-network-sdk-config.yaml # Path to a compatible Fabric SDK Go config file
export 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 user
export FABPROXY_CHANNEL=mychannel # Channel to be used for the transactions
export FABPROXY_CCID=evmcc # ID of the EVM Chaincode deployed in your fabric network
export PORT=5000 # Port the proxy will listen on. If not provided default is 5000.
Now Navigate back to fabric-chaincode-evm directory to build and run proxy
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
Now your Fabric network is up and running at http://localhost:5000 and ready for deploying Smartcotracts.
6. Deploying Smartcontracts
Install web3 library
npm install web3@0.20.2
Create a Work directory of your choice. run npm install on that directory to install required npm modules.
Now we’ll enter node console to set up our web3. Run below command in your work directory
node
Assign Web3 library and use the fab3 running in the previous terminal as provider
Web3 = require(‘web3’)
web3 = new Web3(new Web3.providers.HttpProvider(‘http://localhost:5000'))
To see your account information, run
web3.eth.accounts
You’ll see account address similar to ethereum. Assign this account as defaultAccount
web3.eth.defaultAccount = web3.eth.accounts[0]
Now You can deploy your smartcontract by pointing Abi and bytecode in similar way as you do with ethereum. Compile Your contract on remix or truffle. make abi and bytecode ready for deployment.
abi=<paste abi here> byteCode=<paste your Byte Code here> contract=web3.eth.contract(abi)deployedContract=contract.new([], )
You can get Contract address with transaction hash of deployed contract like below
web3.eth.getTransactionReceipt(deployedContract.transactionHash)
References:
- Hyperledger Fabric EVM
- Fabric-samples
- IBM Hyperledger-Ethereum
- web3.js
Published at Tue, 02 Jul 2019 07:56:42 +0000
