Hyperledger Fabric 1.0 实战学习(三) chaincode开发和部署实战1

  之前主要讲解了fabric的环境搭建、chaincode go语言的重要的接口等,本节基于之前的知识和环境结合病人信息保存和查询场景进行开发。
1.chaincode go语言编写
代码中主要包含保存病人信息和查询的代码实现,chaincode的位置放在 /opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go/hosinfo
package main

import (
	"fmt"

	"github.com/hyperledger/fabric/core/chaincode/shim"
	pb "github.com/hyperledger/fabric/protos/peer"
)

// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}

func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
	fmt.Println("chaincode03 Init Success!")
	return shim.Success(nil)
}

func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
	fmt.Println("chaincode03 Invoke")
	function, args := stub.GetFunctionAndParameters()
	if function == "putvalue" { //保存病人信息
		return t.invoke(stub, args)
	} else if function == "query" {//查询病人信息
		return t.query(stub, args)
	}

	return shim.Error("Invalid 03 invoke function name. Expecting \"put\" \"query\"")
}

func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	var A, B string    // Entities
	var err error

	A = args[0]
	B = args[1]
        fmt.Println("chaincode03 Invoke arg "+A +","+B)
	err = stub.PutState(A, []byte(B))
  	fmt.Println("chaincode03 Invoke  put Success  arg "+A +","+B)	
	if err != nil {
		return shim.Error(err.Error())
	}
	//valbytes, err := stub.GetState(A)
	//fmt.Println("chaincode03 Invoke  get  Success  arg "+string(valbytes))
        
	return shim.Success(nil)
}


// query callback representing the query of a chaincode
func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	var A string // Entities
	var err error
	A = args[0]
	fmt.Println("chaincode03 query  arg "+A )
	// Get the state from the ledger
	Avalbytes, err := stub.GetState(A)
	if err != nil {
		jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}"
		return shim.Error(jsonResp)
	}

	jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
	fmt.Printf("Query Response:%s\n", jsonResp)
	return shim.Success(Avalbytes)
}

func main() {
	err := shim.Start(new(SimpleChaincode))
	if err != nil {
		fmt.Printf("Error starting Simple chaincode: %s", err)
	}
}

2.chaincode部署
在fabric启动之后,进行cli,就可以进行chaincode的安装和部署了,通过终端操作可以观察到简单进行了病人信息的存储和查询,病人的信息 "1001","lilin_cold"分别表示个人编号和姓名_患病,这块信息可以自己进行扩展
[root@orderer e2e_cli]# docker exec -it cli bash
root@35cc17f62b94:/opt/gopath/src/github.com/hyperledger/fabric/peer# 
root@35cc17f62b94:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode install -n hosinfo -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/hosinfo
2018-06-29 15:31:57.579 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-06-29 15:31:57.579 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-06-29 15:31:57.579 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-06-29 15:31:57.579 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-06-29 15:31:57.579 UTC [chaincodeCmd] getChaincodeSpec -> DEBU 005 java chaincode disabled
2018-06-29 15:31:57.623 UTC [golang-platform] getCodeFromFS -> DEBU 006 getCodeFromFS github.com/hyperledger/fabric/examples/chaincode/go/hosinfo
2018-06-29 15:31:57.770 UTC [golang-platform] func1 -> DEBU 007 Discarding GOROOT package fmt
2018-06-29 15:31:57.770 UTC [golang-platform] func1 -> DEBU 008 Discarding provided package github.com/hyperledger/fabric/core/chaincode/shim
2018-06-29 15:31:57.770 UTC [golang-platform] func1 -> DEBU 009 Discarding provided package github.com/hyperledger/fabric/protos/peer
2018-06-29 15:31:57.770 UTC [golang-platform] GetDeploymentPayload -> DEBU 00a done
2018-06-29 15:31:57.770 UTC [container] WriteFileToPackage -> DEBU 00b Writing file to tarball: src/github.com/hyperledger/fabric/examples/chaincode/go/hosinfo/hosinfo.go
2018-06-29 15:31:57.772 UTC [msp/identity] Sign -> DEBU 00c Sign: plaintext: 0AB8070A5C08031A0C08EDA1D9D90510...FEBAF8330000FFFF6F09669100120000 
2018-06-29 15:31:57.772 UTC [msp/identity] Sign -> DEBU 00d Sign: digest: 1BCFFF07E3B46C589FCB46DF8121A33484A0138CA2EB7E0A6D4447206761F379 
2018-06-29 15:31:57.776 UTC [chaincodeCmd] install -> DEBU 00e Installed remotely response: 
2018-06-29 15:31:57.776 UTC [main] main -> INFO 00f Exiting.....
root@35cc17f62b94:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode instantiate -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 -C mychannel -n hosinfo -v 1.0 -c '{"Args":["init"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')"
2018-06-29 15:32:06.873 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-06-29 15:32:06.873 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-06-29 15:32:06.881 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-06-29 15:32:06.881 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-06-29 15:32:06.881 UTC [chaincodeCmd] getChaincodeSpec -> DEBU 005 java chaincode disabled
2018-06-29 15:32:06.881 UTC [msp/identity] Sign -> DEBU 006 Sign: plaintext: 0AC3070A6708031A0C08F6A1D9D90510...535010030A04657363630A0476736363 
2018-06-29 15:32:06.881 UTC [msp/identity] Sign -> DEBU 007 Sign: digest: 05A4CB3FD0BB270783F15859B83D8E481B13A9AE42FE757BB4B1BE4D1D0B61B9 
2018-06-29 15:32:23.126 UTC [msp/identity] Sign -> DEBU 008 Sign: plaintext: 0AC3070A6708031A0C08F6A1D9D90510...C34C76EFC68231E87EE95CEA63DCCC00 
2018-06-29 15:32:23.126 UTC [msp/identity] Sign -> DEBU 009 Sign: digest: 34A095F22DA4359FA7C3939F5958E5447FC09B238784D781F7DDE6DEEC693872 
2018-06-29 15:32:23.133 UTC [main] main -> INFO 00a Exiting.....
root@35cc17f62b94:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode invoke -o orderer.example.com:7050 --tls true  --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n  hosinfo -c '{"Args":["putvalue","1001","lilin_cold"]}'
2018-06-29 15:32:25.430 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-06-29 15:32:25.431 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-06-29 15:32:25.437 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-06-29 15:32:25.437 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-06-29 15:32:25.437 UTC [chaincodeCmd] getChaincodeSpec -> DEBU 005 java chaincode disabled
2018-06-29 15:32:25.437 UTC [msp/identity] Sign -> DEBU 006 Sign: plaintext: 0AC6070A6A08031A0C0889A2D9D90510...313030310A0A6C696C696E5F636F6C64 
2018-06-29 15:32:25.437 UTC [msp/identity] Sign -> DEBU 007 Sign: digest: 74D9040CA7904C04BDDA1DB51098DF1DCDE5D67B85AB1F177BF32F5CB29B93DC 
2018-06-29 15:32:25.445 UTC [msp/identity] Sign -> DEBU 008 Sign: plaintext: 0AC6070A6A08031A0C0889A2D9D90510...FA680351ED183C0E25FD43771E877AF9 
2018-06-29 15:32:25.445 UTC [msp/identity] Sign -> DEBU 009 Sign: digest: 9E725ADD40426B5E4477E47D40B21E51C912A4C8C1C507C011FD92E9A1E87F7F 
2018-06-29 15:32:25.459 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> DEBU 00a ESCC invoke result: version:1 response: payload:"\n \036\247\247\376X\317\362O\2400\330\375Z\350\265\327\345eZ\031U\217s\003U\266\274\352\316\0242\t\022Y\nB\022'\n\007hosinfo\022\034\n\006\n\0041001\032\022\n\0041001\032\nlilin_cold\022\027\n\004lscc\022\017\n\r\n\007hosinfo\022\002\010\005\032\003\010\310\001\"\016\022\007hosinfo\032\0031.0" endorsement: 
2018-06-29 15:32:25.459 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 00b Chaincode invoke successful. result: status:200 
2018-06-29 15:32:25.459 UTC [main] main -> INFO 00c Exiting.....
root@35cc17f62b94:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n hosinfo  -c '{"Args":["query","1001"]}'
2018-06-29 15:32:37.428 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-06-29 15:32:37.428 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-06-29 15:32:37.428 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-06-29 15:32:37.428 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-06-29 15:32:37.429 UTC [chaincodeCmd] getChaincodeSpec -> DEBU 005 java chaincode disabled
2018-06-29 15:32:37.429 UTC [msp/identity] Sign -> DEBU 006 Sign: plaintext: 0AC6070A6A08031A0C0895A2D9D90510...6F1A0D0A0571756572790A0431303031 
2018-06-29 15:32:37.429 UTC [msp/identity] Sign -> DEBU 007 Sign: digest: 15280EA82002181C1CC7B3A3C948B749A412123C8B4C3F2D2870C08E206CEA74 
Query Result: lilin_cold
2018-06-29 15:32:37.436 UTC [main] main -> INFO 008 Exiting.....
root@35cc17f62b94:/opt/gopath/src/github.com/hyperledger/fabric/peer# 
目前博客更新可能有点慢,欢迎大家邮件进行技术交流。邮件:[email protected]

你可能感兴趣的:(区块链Hyperledger,fabric)