Fabric Smart Contract Specific Code template analysis
The smart contract of fabric is called chain code (CHAINCODE), which is divided into system chain code and user chain code. The system chain code is used to realize the function of the system level, and the user chain code realizes the user's application function. The chain code is compiled into a standalone application that runs in an isolated Docker container.
Compared with Ethereum, the fabric chain code and the underlying ledger are separate, upgrade the chain code does not need to migrate the ledger data into the new chain code, the real realization of the separation of logic and data, while the chain code in Go, Java, Nodejs language written.
The fabric chain code interacts with the peer node through GPRC,
(1) When the peer node receives input from the client request (PROPSAL), it sends a link-code message object (with input information, caller information) to the corresponding chain code.
(2) The link code calls the Invoke method inside the Chaincodebase, obtains the ledger status information and sends the pre-commit status to the peer node by sending the fetch data (getState) and writing the data (putstate) message.
(3) The link code sends the final output result to the peer node, the node pairs the input (propsal) and the output (Propsalreponse) to endorse the signature, completes the first paragraph signature submission.
(4) After the client collects the first submission information of all peer nodes, assembles the transaction (transaction) and signs, sends the transaction to the Orderer node queue, eventually orderer the block, and sends to each peer node, the input and output falls on the ledger, Complete the second paragraph submission process.
Fabric Smart contract Chain code development basis, a sentence summed up is: A base class, two queries, a write.
▲ a base class
Java Chaincodebase is a custom chain code of the contract base class, inside the run (chaincodestub stub, String function, string[] args) method is the peer call chain Code entry function, One must customize an Init method for contract initialization and upgrade initialization actions. The code is shown below:
Go because of its dynamic interface characteristics, do not need to specifically declare the implementation of the contract interface. However, two interface methods must be implemented:
▪init (Stubshim. Chaincodestubinterface): Init for contract initialization and upgrade initialization action;
▪invoke (Stubshim. Chaincodestubinterface): Invoke is the entry function of the peer call chain code, the code is as follows:
Chaincodestub contains a wealth of ledger operations, commonly used getcallercertificate (), GetState (k), Putstate (k,v), Invokechaincode (...), Rangequerystate (K1,K2), Gettxid () and so on.
▲ Two queries
Because the fabric default state database is a k-v library, the common approach is to use the following two queries.
1. GetState (k) Gets the value values corresponding to the individual keys.
2. Rangequerystate (K1,K2), gets all K-v objects that K1 start, K2 ends, and returns a Map<string,string> object, where K1,k2 is sorted by dictionary order.
▲ One Write
Putstate (k,v), write data, it is important to note that putstate data will not immediately fall into the ledger, until the second transaction submitted consensus reached, the data will be landed. The code is shown below:
My brother under my guidance wrote, for the time being, the follow-up should be changed.