Fabric v1.x intelligent interaction principle contract with the life cycle

First, what is a smart contract

Intelligent Fabric contract is the heart of the block chain network, the definition of rules between different organizations in the executable code. Intelligent contracts packaged into chaincode, you can record generated by chaincode traded to the books after deployment chaincode.

Interactive Second, smart contract with the books

Developers need to develop modules, including Application and SmartContract, Application API can be accessed directly through the block chain ledger, you can also send out event can be accessed through a smart contract block chain and the world's states, intelligent contracts and books, as shown below:
Here Insert Picture Description
intelligent access World state contract interface:

  • get: direct access to the world state can get the results returned, not to block access chain
  • put: write world state, while adding a block write transactions in the chain
  • delete: delete a record in the world state, does not remove the recording block in the chain, will add a deleted transaction

Three, chaincode sample

chaincode support Java, Node.js, Go and other languages, gives examples Go below:

import (
	"fmt"
	"github.com/hyperledger/fabric/core/chaincode/shim"
	"github.com/hyperledger/fabric/protos/peer"
)
// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}

// Init is called during chaincode instantiation to initialize any data. 
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
	// Initialize the chaincode
	A = args[0]
	Aval, err = strconv.Atoi(args[1])
	B = args[2]
	Bval, err = strconv.Atoi(args[3])
	fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)
	// Write the state to the ledger
	err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
	err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
	return shim.Success(nil)
}

// Invoke is called per transaction on the chaincode. Each transaction is
// either a 'get' or a 'set' on the asset created by Init function. The Set
// method may create a new asset by specifying a new key-value pair.
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {

	function, args := stub.GetFunctionAndParameters()
	if function == "invoke" {
		// Make payment of X units from A to B
		return t.invoke(stub, args)
	} else if function == "delete" {
		// Deletes an entity from its state
		return t.delete(stub, args)
	} else if function == "query" {
		// the old "Query" is now implemtned in invoke
		return t.query(stub, args)
	}

	return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"delete\" \"query\"")
}
  • Init method for initializing chaincode, chaincode is called instantiation, the above example initializes the value value of A and B, and write data to the interface call PutState account book.
  • Invoke method it will be called when running transaction can be achieved through different trading logic function parameters, in the example above realized invoke, delete, and query three kinds of transactions.

Four, chaincode life cycle

chaincode lifecycle including packaging (Package), installed (Install), examples of the (Instantiate), running (Running) and upgrade (Upgrade).

4.1 package (Package)

When it package will generate a ChaincodeDeploymentSpec (CDS), which contains chaincode source code, name, version number; you can also specify the instance of the policy, specify who will be instantiated operation, the same expressions and endorsement strategy; chaincode owner also You need to sign the package.

An example of packing operation is shown below, specifies the name mycc, the source path, version 1.0, the instantiation strategy instantiated, packaged destination file is only the admin role to organize OrgA is ccpack.out.

peer chaincode package -n mycc -p  github.com/hyperledger/fabric-samples/chaincode/abstore/go -v 1.0 -s -S -i "AND('OrgA.admin')"  ccpack.out

Packaging may be made to complete the package after signing an object file signedccpack.out, example is shown below:

peer chaincode signpackage ccpack.out signedccpack.out

4.2 Installation (Install)

After complete package can be installed, you can be mounted a plurality chaincode Peer, be noted that must be installed on each of the chaincode Endorsing peer on the channel. Installation ccpack.out example as follows:

peer chaincode install ccpack.out

4.3 Examples of (Instantiate)

Examples of the time required to set policy endorsement, following the example initialization data specified by the -c argument, by -P parameter specifies the members of the organization Org1 endorsement strategy requires endorsement and Org2 together:

peer chaincode instantiate -C mychannel -n mycc -v 1.0 -c '{"Args":[“a”, “100”, “b”, “200”]}' -P "AND ('Org1.member','Org2.member')"

4.4 running (Running)

After completion of the instantiated client can submit a transaction request, a transaction and update processing chaincode books, and then returns a response, the client will be received in response, the following is an example of a query and transfers:

peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}’
peer chaincode invoke -o order-url -C mychannel -n mycc -c  '{"Args":["invoke","a","b","10"]}'

4.5 Upgrade (Upgrade)

New business needs for chaincode needs to be updated, need to change the version number of updates; Before the upgrade, the new version of chaincode have been installed on all nodes endorsement requirements; upgrade process is similar to instantiate operation, upgrade affects only a channel, example is shown below:

peer chaincode upgrade -C mychannel -n mycc -v 1.0 -c '{"Args":["a","100","b","200"]}'

五、System Chaincode

System Chaincode run in Peer process, rather than as an ordinary chaincode run in isolated vessels; System Chaincode achieve a number of system behavior, some of which are listed below System Chaincode:

  • LSCC (Lifecycle system chaincodeC): chaincode request processing lifecycle management application, includes an upper section of packing, installation, instantiation, upgrades and the like;
  • CSCC (Configuration system chaincode): Peer-side channel configuration process, such as addition of a peer channel and the like;
  • QSCC (Query system chaincode): books provide query API, such as access to blocks and transaction records.
Published 29 original articles · won praise 3 · Views 5580

Guess you like

Origin blog.csdn.net/ice_fire_x/article/details/104362247