Learn to deploy a private Blockchain with Hyperledger Fabric using Docker compose.
Learn to deploy a private Blockchain with Hyperledger Fabric using Docker compose.
Step 1: In this lab, we will deploy a local Hyperledger fabric blockchain using Docker and Docker Compose and interact with our blockchain.
Step 2: Open VS Code first and open the terminal in VS Code.
Step 3: Create a directory named hyperledger-fabric and change directory to hyperledger-fabric. This is the directory where we will keep all the files related to our local blockchain.
Step 4: Now run this curl command to get all the necessary scripts from the official Hyperledger Fabric repository to create our blockchain network.Basically this command downloads the 'install-fabric.sh' script from GitHub and then makes it executable in the current directory.
Step 5: Now run the install-fabric.sh script to download all the necessary binaries and docker images to create our blockchain network.
Step 6: Wait for some time till all the docker images are downloaded and all other dependencies are installed successfully.
Step 7: Change directory to fabric-samples/test-network. This is the directory where we already have all the files related to our blockchain test network.
Step 8: Run the network.sh script with down argument to stop and remove any pre-existing the docker containers and networks related to our blockchain network. This is just to clean the slate for our new deployment.
Step 9: Now run the network.sh script with up argument to create a new blockchain network with 2 organizations, 2 peers per organization, 1 orderer, 1 CA per organization and 1 CLI container.
Step 10: The './network.sh' script is used to manage your network. In this case, we are using it to create a new channel named 'mychannel.' By default, the channel name is 'mychannel' when the -c argument is not provided. Alternatively, you can set up the network and create the channel with a single command using './network.sh up createChannel'.
Step 11: To deploy the chaincode named 'basic,' use the './network.sh' script with the 'deployCC' argument. The '-ccn' argument specifies the name of the chaincode, '-ccp' specifies the path to the chaincode, and '-ccl' specifies the language of the chaincode, which is 'typescript' in this case.
Step 12: We need to set the environment variables to interact with our blockchain network. The first command sets the PATH variable to include the bin directory of our blockchain network. The second command sets the FABRIC_CFG_PATH variable to include the config directory of our blockchain network.
Step 13: We will set several environment variables for the Peer node of 'Org1MSP' in the Hyperledger Fabric network through the following commands. Here's what each line does: 1. 'export CORE_PEER_TLS_ENABLED=true' enables TLS encryption for communication. 2. 'export CORE_PEER_LOCALMSPID="Org1MSP"' sets the local MSP (Membership Service Provider) ID to 'Org1MSP'. 3. 'export CORE_PEER_TLS_ROOTCERT_FILE' specifies the root TLS certificate file for the Peer node's communication security. 4. 'export CORE_PEER_MSPCONFIGPATH' sets the path to the MSP configuration, specifying the location of the user's cryptographic materials. 5. 'export CORE_PEER_ADDRESS=localhost:7051' sets the address for communication with the Peer node, using the hostname 'localhost' and port '7051'.
Step 14: Next step is to prepare our blockchain for interactions. The provided command triggers a transaction on our Hyperledger Fabric network, utilizing the 'basic' chaincode(smart-contract). It connects to an orderer, configures TLS settings for secure communication, and executes the 'InitLedger' function to initialize the ledger with assets, making it ready for future peer interactions.
Step 15: This command queries the 'basic' chaincode on the 'mychannel' channel, specifically calling the 'GetAllAssets' function to retrieve information about all assets in the ledger.
Step 16: Now we will make our first transaction/interaction in our network by running the peer chaincode invoke command. The command invokes a chaincode function to transfer an asset to 'Cris' on the Hyperledger Fabric network.
Step 17: Let's configure environment variables for our second Peer node to enable TLS security. We'll set the local MSP ID to 'Org2MSP,' define the TLS certificate paths, specify the MSP configuration, and set the Peer address to 'localhost:9051' for 'Org2.' It's worth noting that this script is similar to the one in Step 13, with the only differences being the port number and the change of MSPID to 'Org2MSP.' This change allows us to operate 'Peer 2' and observe how our transactions affect the network.
Step 18: Query the asset that we transferred to 'Cris' in the previous step. By running this command we only query to read asset3 to confirm if the transaction is updated for all the peers in our Private Blockchain Network.
Step 19: Now stop the blockchain network by running the network.sh script with down argument.
Step 24: We have successfully deployed our local Hyperledger Fabric blockchain and have also interacted with it, by completing an asset transfer.