Hyperledger Fabric practice material for Korea Polytechnics students
- Build Your First Network
- Chaincode Development
- Chaincode Devolopment via IBM Blockchain Platform
- Balance Transfer
- Vote system example using 'Balance Transfer' tutorial
8. ๊ฐ์
! The build your first network (BYFN) scenario provisions a sample Hyperledger
Fabric network consisting of two organizations, each maintaining two peer
nodes. It also will deploy a โSoloโ ordering service by default, though other
ordering service implementations are available.
source : https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html
Org1MSP - Peer 0
- Peer 1
Org2MSP - Peer 0
- Peer 1
9. crypto-config.yaml
! ์ธ์ฆ์ ์์ฑ์ ์ํ ์กฐ์ง๊ณผ Peer ๊ฐ์, ์ฌ์ฉ์(์ด๋๋ฏผ ์ ์ธ) ์ ์ค์
Note that theย crypto-config.yamlย ๏ฌle lists ๏ฌve orderers as being tied to the orderer organization. While theย cryptogenย tool will create
certi๏ฌcates for all ๏ฌve of these orderers.ย
Orderer
orderer2
orderer3
orderer4
orderer5
peer0.org0
peer1.org0
peer0.org1
peer1.org1
38. [์ฐธ๊ณ ] block data (peer)
docker cp -a peer0.org1.example.com:/var/hyperledger/production ./
cd production/
more ledgersData/chains/chains/mychannel/blockfile_000000
52. Chaincode tutorial (Advance)
! Code review
! Build and deploy
- ftp upload (to newly created directory. Eg> saccnew)
- [Terminal 2] go get
- [Terminal 2] go build -o saccnew
- [Terminal 2] CORE_CHAINCODE_ID_NAME=mycc:0 CORE_PEER_TLS_ENABLED=false ./saccnew -peer.address peer:7052
! Chaincode install/run on Terminal 3
- go get github.com/hyperledger/fabric-chaincode-go/shim
Git hub : https://github.com/wonyongHwang/chaincode-tutorial/blob/master/kopoChainCode.go
peer chaincode install -p chaincodedev/chaincode/saccnew -n mycc -v 0
peer chaincode instantiate -n mycc -v 0 -c '{"Args":["init","a","100","b","200"]}' -C myc
peer chaincode invoke -n mycc -c '{"Args":["more","tt","Here your are, this is a book 2","1200"]}' -C myc
peer chaincode query -n mycc -c '{"Args":["morequery","tt"]}' -C myc
53. Chaincode tutorial (Advance)
PutState func (t *SimpleChaincode) more(stub shim.ChaincodeStubInterface, param []string) pb.Response {
if len(param) != 3 {
return shim.Error("# of parameter mismatching")
}
title, text, code := param[0], param[1], param[2]
codeNum, err := strconv.ParseInt(code, 10, 32)
if err != nil {
return shim.Error("code error")
}
if len(title) == 0 || len(text) == 0 || len(code) == 0 {
return shim.Error("value of paramter is not properly formatted")
}
// make my data of AdditionalInfo
addInfo := &AdditionalInfo{Title: title, Text: text, code: codeNum}
addInfoBytes, err := json.Marshal(addInfo)
if err != nil {
return shim.Error("failed to convert bytes " + err.Error())
}
err = stub.PutState(title, addInfoBytes) // key value์ ๊ณต๋ฐฑ ๋ค์ด๊ฐ๋ฉด ์กฐํ์ ๊ฐ์ ์ฐพ์ง ๋ชปํจ
if err != nil {
return shim.Error("PutState failure " + err.Error())
}
type AdditionalInfo struct {
Title string "json:"title""
Text string "json:"text""
code int64 "json:"code""
}
54. Chaincode tutorial (Advance)
- GetState, GetHistoryForKey
func (t *SimpleChaincode) morequery(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var A string // Entities
var err error
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting name of the person to query")
}
A = args[0]
// Get the state from the ledger
valbytes, err := stub.GetState(A)
if err != nil {
jsonResp := "{"Error":"Failed to get state for " + A + ""}"
return shim.Error(jsonResp)
}
valueStr := &AdditionalInfo{}
err = json.Unmarshal(valbytes, &valueStr)
if err != nil {
jsonResp := "{"Error":"Failed to Unmarshal " + A + ""}"
return shim.Error(jsonResp)
}
jsonResp := "{"Title":"" + A + "","Text":"" + string(valueStr.Text) + ""}"
fmt.Printf("Query Response:%sn", jsonResp)
// history
iter, _ := stub.GetHistoryForKey(A)
fmt.Println("Here is History for " + A)
for iter.HasNext() {
kv, _ := iter.Next()
fmt.Println(string(kv.GetValue()) + " " + kv.GetTimestamp().String())
}
return shim.Success([]byte(string(valueStr.code)))
}
GetHistoryForKey ํจ์๋ก ๋ณ๊ฒฝ ์ด๋ ฅ ์กฐํ
๊ธฐ์กด์ ๋ณ๊ฒฝ๋ ์ด๋ ฅ์ด ์๋ ๊ฒฝ์ฐ
๋ชจ๋ ์กฐํ ๊ฐ๋ฅ
75. Check dependencies
! VS Code version 1.38.0 or higher (code โversion)
! Node v8.x or v10.x and npm v6.x or greater
node โversion
npm โversion
! Go version V1.12 or greater
go version
! Docker version v17.06.02-ce or greater
docker โversion
! Docker Compose v1.14.0 or greater
docker-compose โversion
nvm์ ์ฌ์ฉ ์ค์ธ ๊ฒฝ์ฐ ์ฌ์ฉํ ๋ ธ๋ ๋ฒ์ ์ alias ์ค์ ํ์ฌ
default๋ global๋ก ๋ค์ด๋ฐ ํด์ค์ผ ํจ
82. Preparation
! Balance-Transfer example was built on SDK v1.4
! But at the fabric 2.0, we should use SDK v2.0
! Work around
1) remove docker images of ccenv 2.x and reinstall ccenv 1.4
docker pull hyperledger/fabric-ccenv:1.4
https://jira.hyperledger.org/browse/FABN-1357
93. Commit Chaincode
! ์ฒด์ธ์ฝ๋์์ ํ์ํ ์ต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ go get ๋ช ๋ น์ด ์ถ๊ฐ(base.yaml)
command: >
sh -c "peer node start
&& go get github.com/hyperledger/fabric-chaincode-go/shim
&& go get github.com/hyperledger/fabric-protos-go/peer"
94. Prepare shell script for booting up
! POST request Enroll on Org1/Org2
! POST request Create channel โฆ
! POST request Join channel on Org1/Org2
! POST request Update anchor peers on Org1/Org2
! POST Install chaincode on Org1/Org2
! POST instantiate chaincode on Org1