Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

HyperLeger Fabric Development (8)-- HyperLeger Fabric chain Code Development and testing

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

HyperLeger Fabric development (8)-- HyperLeger Fabric chain code development and testing 1. Chain code examples

An example of SACC project chain code is as follows:

The package mainimport ("fmt"github.com/hyperledger/fabric/core/chaincode/shim"github.com/hyperledger/fabric/protos/peer") / / SimpleAsset implements a simple chaincode to manage an assettype SimpleAsset struct {} / / Init method calls func (t * SimpleAsset) Init (stub shim.ChaincodeStubInterface) peer.Response {/ / get the parameter args: = stub.GetStringArgs () if len (during chain code instantiation or chain code upgrade) from the transaction proposal Args)! = 2 {return shim.Error ("Incorrect arguments. Expecting a key and a value ")} / / Storage assets to the book err: = stub.PutState (args [0], [] byte (args [1])) if err! = nil {return shim.Error (" Failed to create asset:% s ") Args [0])} return shim.Success (nil)} / / the Invoke method is called func (t * SimpleAsset) Invoke (stub shim.ChaincodeStubInterface) peer.Response {/ / extract the parameter fn from the transaction proposal, args: = stub.GetFunctionAndParameters () var result string var err error if fn = = "set" {result, err = set (stub, args)} else {/ / assume 'get' even if fn is nil result Err = get (stub, args)} if err! = nil {return shim.Error (err.Error ())} / / Return the result as success payload return shim.Success ([] byte (result))} / / use key-value to set assets on the account book If key exists, update func set (stub shim.ChaincodeStubInterface, args [] string) (string, error) {if len (args)! = 2 {return ", fmt.Errorf (" Incorrect arguments. Expecting a key and a value ")} err: = stub.PutState (args [0], [] byte (args [1])) if err! = nil {return", fmt.Errorf ("Failed to set asset:% s", args [0])} return args [1], nil} / / get the value of the specified asset key func get (stub shim.ChaincodeStubInterface, args [] string) (string, error) {if len (args)! = 1 {return "" Fmt.Errorf ("Incorrect arguments. Expecting a key ")} value, err: = stub.GetState (args [0]) if err! = nil {return", fmt.Errorf ("Failed to get asset:% s with error:% s", args [0], err)} if value = = nil {return ", fmt.Errorf (" Asset not found:% s ", args [0])} return string (value) Nil} / / launch chain code func main () {if err: = shim.Start (new (SimpleAsset)) in the container during instantiation Err! = nil {fmt.Printf ("Error starting SimpleAsset chaincode:% s", err)} 2. Unit test of chain code 1. Brief introduction of unit test of chain code

After the chain code development is completed, the chain code does not need to be deployed in the block chain environment to debug. Shim.MockStub can be used to write unit test code and debug directly in the GE language development environment (environment without Fabric block chain network).

2. Write chain code test code

Go to the sacc directory and create a new sacc_test.go file to write the test code

Package mainimport ("testing"github.com/hyperledger/fabric/core/chaincode/shim"fmt") func TestSet (t * testing.T) {/ / Analog chain code deployment scc: = new (SimpleAsset) stub: = shim.NewMockStub ("SimpleAsset", scc) mockInit (t, stub, [] byte {[] byte ("user1"), [] byte ("0")}) / / call the chain code transaction method invokeSet (t, stub) [] string {"user1", "10000"}) invokeSet (t, stub, [] string {"user1", "1000"})} func mockInit (t * testing.T, stub * shim.MockStub, args [] [] byte) {res: = stub.MockInit ("1", args) if res.Status! = shim.OK {fmt.Println ("Init failed", string (res.Message)) t.FailNow ()} func invokeSet (t * testing.T) Stub * shim.MockStub, args [] string) {/ / invoke call res: = stub.MockInvoke ("1", [] [] byte {[] byte ("set"), [] byte (args [0]), [] byte (args [1])}) fmt.Println ("set (" + args [0] + "," + args [1] + ") if res.Status! = shim.OK {fmt.Println (" invoke set failed: ", args [0]) String (res.Message)) t.FailNow ()}} func TestGet (t * testing.T) {/ / simulated chain code deployment scc: = new (SimpleAsset) stub: = shim.NewMockStub ("SimpleAsset", scc) mockInit (t, stub, [] byte {[] byte ("user1"), [] byte ("10000")}) / / call chain code invokeGet (t, stub, [] string {"user1"})} func invokeGet (t * testing.T) Stub * shim.MockStub, args [] string) {/ / invoke calls res: = stub.MockInvoke ("1", [] [] byte {[] byte ("get"), [] byte (args [0]}) fmt.Println ("get (" + args [0] + ")" + "- >" + string (res.Payload)) if res.Status! = shim.OK {fmt.Println ("invoke get failed:", args [0]) String (res.Message)) t.FailNow ()}} / / output://=== RUN TestSet//set (user1,10000) / / set (user1,1000) / /-PASS: TestSet (0.00s) / = RUN TestGet//get (user1)-> 10000 Maxima PASS: TestGet (0.00s) / / PASS3, unit testing

Perform a unit test under the chain code directory sacc

Go test-v sacc_test.go sacc.go

Third, chain code development environment testing 1. Start chain code development and debugging environment

Open terminal T1, enter the chaincode-docker-devmode directory, and start the Fabric network:

Docker-compose- f docker-compose-simple.yaml up

2. Compile and start the chain code

Open terminal T2 and enter the chaincode container

Docker exec-it chaincode bash

Enter the SACC (simple Asset chain Code) project directory:

Cd sacc

Compile chain code:

Go build-o sacc

Start the chain code:

CORE_PEER_ADDRESS=peer:7052 CORE_CHAINCODE_ID_NAME=sacc:0. / sacc

3. Operation chain code

Open terminal T3 and enter the client cli container:

Docker exec-it cli bash

Install chain code:

Peer chaincode install-p chaincodedev/chaincode/sacc-n sacc-v 0

Print message for successful installation: Installed remotely response:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report