Hyperledger - Hyperledger Learning Report
1. What is blockchain?
The basic principles of blockchain, the basic components include:
Transactions: changes to the state of the ledger;
Block: record transaction and status, which is a confirmation of the current ledger status;
Chain: Log records of state changes.
Taking the blockchain as a state machine, each transaction is an attempt to change the state, and each generation of a block is the result of the participant's confirmation of the state of all transactions included in it.
There are currently three development projects of blockchain:
Classification:
Public (public) chain : Anyone can participate in the use and maintenance, typically such as the Bitcoin blockchain, the information is completely public .
After the introduction of the permission mechanism, a private chain and a consortium are generated,
Private chain: The centralized manager restricts, only a few people inside can use it, and the information is not public.
Alliance chain: Between the public chain and the private chain, several organizations cooperate to maintain a blockchain. The use of the blockchain must be managed with permission, and the relevant information will be protected, such as the UnionPay organization.
2. What is hyperledger ?
In December 2015, the Linux Foundation took the lead and jointly announced the establishment of the Hyperledger project with 30 initial members.
The emergence of this project actually announced that blockchain technology is no longer simply an open source technology, and has been officially recognized by mainstream institutions and the market; at the same time, Hyperledger first proposed and implemented complete rights management, innovative consensus algorithms, and removable The plug-in framework will have a profound impact on the development of blockchain-related technologies and industries.
There are currently two sub-projects, fabric and sawtooth Lake.
fabric : including fabric and fabric-api, the goal is the basic core platform of the blockchain, supports new consensus mechanisms such as pbft, and supports authority management. It was first initiated by IBM and DAH.
sawtooth Lake : including arcade, core, dev-tools, validator, mktplace, etc. It is the main contribution and leading blockchain platform of Intel, which supports the new consensus mechanism Proof of Elapsed Time (PoET).
3. Installation and deployment of the hyperledger fabric project.
System: The latest version of Linux , eg.ubuntu16.04 , can be downloaded from the official website.
①Install docker
Ubuntu advanced version comes with installation package, execute the command
# apt-get -y install docker.io
#sudo docker version // View version
②Install docker-compose
First, install pip
# sudo aptitude install python-pip
Install docker-compose
# sudo pip install docker-compose
③Download the relevant docker image and configure it. Mirror address https://hub.docker.com/r/yeasy/hyperledger
Of course, you can also download it from Github .
# docker pull yeasy/hyperledger:latest
# docker tag yeasy/hyperledger:latest hyperledger/fabric-baseimage:latest
# docker pull yeasy/hyperledger-peer:latest
# docker pull yeasy/hyperledger-membersrvc:latest
④Use PBFT mode
PBFT is a classic distributed consensus algorithm and is currently the most recommended algorithm by hyperledger , which requires at least 4 nodes.
First, download the compose file.
# git clone https://github.com/yeasy/docker-compose-files
At this point, there is a docker-compose-files folder in the directory.
(1) Test using CLI
Start docker-daemon manually
#service docker stop
#sudo docker daemon --api-cors-header="*" -H tcp://0.0.0.0:2375 -H unix:///var/run/do
cker.sock
Enter the hyperledger project and start the cluster.
# cd docker-compose-files/hyperledger
# docker-compose up
At this point, four nodes are generated , vp0, vp1, vp2, and vp3 .
Enter node vp0
#docker exec -it vp0 bash
Deploy two accounts a and b with 100 and 200 respectively .
#peer chaincode deploy -p github.com/hyperledger/fabric/examples/chaincode/go/
chaincode_example02 -c '{"Function":"init", "Args": ["a","100", "b", "200"]}'
returns CORE_CHAINCODE_ID_NAME ( a string of addresses ) ,
in the following form:
a5389f7dfb9efae379900a41db1503fea2199fe400272b61ac5fe7bd0c6b97cf10ce3aa8dd00cd7626ce02f18accc7e5f2059dae6eb0786838042958352b89fb
Check account balance
#peer chaincode query -nCORE_CHAINCODE_ID_NAME -c '{"Function": "query", "Args": ["a"]}'
should return 100
Trading
peer chaincode invoke -n CORE_CHAINCODE_ID_NAME -c '{"Function": "invoke", "Args": ["a", "b", "10"]}'
After the transaction is made, a decreases by 10 and b increases by 10 .
You can check and verify the transaction result again.
(2) Post rest test
Note: The POST test can be performed using the postman Google browser plug-in or various online POST tools. Here, the curl tool will be used to perform the POST operation in the Linux window .
Start docker-daemon manually
#service docker stop
#sudo docker daemon --api-cors-header="*" -H tcp://0.0.0.0:2375 -H unix:///var/run/do
cker.sock
Open the hyperledger project, start the pbft cluster, and generate nodes
#docker-compose -f docker-compose-with-membersrvc.yml up
1. Perform the Rest test and log in to the user Jim
#curl -H "Content-Type: application/json" -X POST --data '{"enrollId": "jim","enrollSecret": "6avZQLwcUe9b"}' http://localhost:5000/registrar
Shows as follows
2. Chaincode deployment
curl -H "Content-Type: application/json" -X POST --data '{
"jsonrpc": "2.0",
"method": "deploy",
"params": {
"type": 1,
"chaincodeID":{
"path":"github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
},
"ctorMsg": {
"function":"init",
"args":["a", "1000", "b", "2000"]
},
"secureContext": "jim"
},
"id": 1
}' http://localhost:5000/chaincode
Response is as follows
{
"jsonrpc": "2.0",
"result": {
"status": "OK",
"message": "28bb2b2316171a706bb2810ec35d095f430877bf443f1061ef0f60bbe753ed440700a
5312c16390d3b30199fe9465c3b75d5944358caae01ca81ef28128a1bfb"
},
"id": 1
}
3, chaincode call
transfer , invoke
#curl -H "Content-Type: application/json" -X POST --data '{
"jsonrpc": "2.0",
"method": "invoke",
"params": {
"type": 1,
"chaincodeID":{
"name":"28bb2b2316171a706bb2810ec35d095f430877bf443f1061ef0f60bbe753ed440700
a5312c16390d3b30199fe9465c3b75d5944358caae01ca81ef28128a1bfb"
},
"ctorMsg": {
"function":"invoke",
"args":["a", "b", "100"]
},
"secureContext": "jim"
},
"id": 3
}' http://localhost:5000/chaincode
Response is as follows:
{
"jsonrpc": "2.0",
"result": {
"status": "OK",
"message": "509b7500-96da-4411-8464-e24c61d5477d"
},
"id": 3
}
3. Chaincode query
call query
curl -H "Content-Type: application/json" -X POST --data '{
"jsonrpc": "2.0",
"method": "query",
"params": {
"type": 1,
"chaincodeID":{
"name":"28bb2b2316171a706bb2810ec35d095f430877bf443f1061ef0f60bbe753ed440700a5312c16390d3b30199fe9465c3b75d5944358caae01ca81ef28128a1bfb"
},
"ctorMsg": {
"function":"query",
"args":["a"]
},
"secureContext": "jim"
},
"id": 5
}' http://localhost:5000/chaincode
return
4. Block information query
URL
GET localhost:5000/chain/blocks/2
#curl http://localhost:5000/chain/blocks/2
(3) python client interaction
Leverage the Python client to interact with hyperledger
1. Install python
#pip install hyperledger --upgrade
2. Enter the python client
#python
>>>from hyperledger.client import Client
>>>c = Client(base_url="http://localhost:5000")
>>>c.peer_list() // Return the network information composed of blockchain nodes
Return as follows:
>>> c.block_get('2') // Get block information
>>>c.chain_list() // Get the current blockchain state
>>>
For specific API usage, see https://github.com/yeasy/hyperledger-py/blob/master/docs/api.md