In our previous posts, we discussed BlockChain and presented the comprehensive Blockchain developer’s guide along with its use cases, especially mentioning how the rise of cryptocurrencies made the term ‘BlockChain’ a synonymous term to it. We also discussed a systematic approach to an enterprise blockchain solution by understanding foundation pillars involved and key modules for all significant requirements.
In this blog, we will learn about creating cryptocurrencies using the IBM Blockchain Hyperledger Fabric Network consisting of two organizations each maintaining two peer nodes.
Getting Prerequisites Ready
Before we begin, check if your platform has all the required components – the latest version of cURL, Docker Compose, Go Programming Language, Node.js runtime and NPM. Once this is done, download and install Hyperledger Fabric Samples from a number of samples that are included in fabric-samples repository. In this post, we will be utilizing first-network sample.
How to run ‘first-network’
IBM provides a fully-annotated script to leverage docker images to bootstrap Hyperledger Fabric Network. The script will further launch a container that will run a script to join peers to a channel, deploy and instantiate chaincode.
Herewith the help text of byfn.sh script.
If no channel is supplied a default name of ‘Mychannel’ will be used by the script to execute.
Generate Network Artifacts
Once you are ready to generate network artifacts generate the following command.
A command line prompt will appear with a brief description, enter ‘y’ and this will generate network entity keys and certificates. Along with certificates and keys for genesis block and collection of configuration transactions.
Next step is to bring up the network, enter the below command and type ‘y’ when prompted.
The logs will be generated and containers will be launched to drive end-to-end application scenario. These logs can then be scrolled to see the transactions. If a terminal window showing ‘END’ is not displayed, you must go to troubleshooting section to identify the issue with bringing up the network.
Further to bring down the network the command ./byfn.sh -m down can be executed. This will kill the containers, remove the crypto material, artifacts and delete chaincode images from Docker registry.
The cryptogen tool will be used to generate the cryptographic material for network entities. These certificates are representatives of identities and they allow authentication to take place while the entities transact and communicate with each other.
In order to work, cryptogen uses a file – ‘crypto-config.yaml’. This file will let us generate a set of certificates and keys for organizations as well as the components that belong to the organizations. This information is received by the network topology in the file. This unique CA certificate to each organization will help in mirroring a typical network where every participating member can use its own CA. Further, every transaction or communication happening in a Hyperledger Fabric is first signed by a ‘keystore’ containing entity’s private key which is then verified by ‘signcerts’, a mode of the public key.
The ‘count’ variable within the file will specify the number of peers per organization. Before we run the tool, we should have an understanding of ‘Name’, ‘Domain’ and ‘Specs’ under OrdererOrgs header of ‘crypto-config.yaml’ file.
After the cryptogen tool is executed and certificates are generated, these certificates along with the keys will be saved to ‘crypto-config’ folder.
Configuration Transaction Generator
Next comes the creation of three major configuration artifacts which can be carried out by ‘configtxgen tool’.
- Orderer: This is a genesis block for ordering service.
- Channel configuration transaction: At the channel creation time, the channel transaction file is broadcast at the orderer.
- Anchor Peer Transaction: Two anchor peer transactions are created with one for each org.
The tool ‘configtxgen’ then makes use of the file configtx.yaml to create the artifacts. This file also contains additional specifications which specify anchor peers for each Peer Org. Secondly, the file points to the location of MSP directory for each member, which allows the root certificates to be stored for each ORG in orderer genesis block. This will enable the network entities communicating with ordering service to have its digital signature verified.
Once we manually run the tools and generate the artifacts, we create a channel configuration transaction by following the below steps.
1) Create an environment variable $CHANNEL_NAME for channel name.
2) Define anchor peer for Org1
3) Define the anchor peer for Org2 in the same channel.
Start the Network
The network can be started by docker-compose script, it references the previously downloaded images and bootstrap orderer with previously downloaded ‘genesis.block’.
Then we can run the below command to start the network.
Post the start of the network, we can create and join the channel following the previously mentioned steps of creating a channel configuration transaction.
Install and Instantiate Chaincode
Chaincode should now be installed on every peer so that the transactions can be carried out between the peers and applications to interact with blockchain ledger.
The code below will install sample GO Code and places source code onto file system of one of four peer nodes.
Once installed, instantiate chaincode on the channel and set endorsement policy. You can also go through documentation policies for details on policy implementation.
What does this demonstrate?
There are a couple of things that are must in order to successfully perform read/write operation. A chaincode must be installed, and a transaction should occur to start a chaincode container. A same copy of ledger is maintained by each peer in a channel. This will make the blockchain to store the sequenced and unchallengeable record in the blocks. Also, a state database is maintained to maintain a snapshot of the current state. Finally, a chaincode will be accessible after it is installed and instantiated.
The transactions can then be seen by checking the logs for CLI Docker container by running the command.
The following output will be generated to see various transactions.
The individual chaincode containers can be inspected to identify separate transactions executed against each container.
There should be no second thoughts while considering that blockchain and cryptocurrency will play a major role in shaping the future of global economics. Considering this potential, the technology giant IBM has launched an initiative towards the cryptocurrencies by partnering with a startup to create a platform to be first public cryptocurrency foray. This platform will define how public and private blockchain technologies can together perform safe transactions via cryptocurrency. Also, the transactions can be performed without two cryptocurrencies directly contacting each other. This platform could be just a start of how the technology of banking might look in future.