Network, Contract, and Transaction Classes
In the previous sections, we have implemented the client application that can connect to test-network and submit transactions on behalf of different users. We have discussed wallets and gateways in detail, but we have seen only limited usage of the Network, Contract, and Transaction. All of them are interfaces declaring a common set of functions providing a really simple and straightforward API for application developers. Let’s make a quick overview of their usage.
The Network interface represents a channel (i.e., subnetwork) in the blockchain network. Network covers only channel capabilities related to the transaction processing. Applications should get the Network object using Gateway.getNetwork(). The Network interface defines the following methods:
getContract returns the Contract object representing an instance of the chaincode deployed in the underlying channel. We will discuss the Contract class later in this section.
addBlockListener and addCommitListener create block and commit event listeners respectively. The listeners can also be removed using removeBlockListener and removeCommitListener respectively. Hyperledger Fabric events, their types, and processing mechanisms will be discussed in the Events Processing chapter.
getChannel returns an underlying Channel object. The Channel object captures the settings needed to interact with the blockchain network in the context of a channel. These settings include the list of participating organizations, endorsers, and committers.
getGateway returns the Gateway object that has produced the current Network object.
The Contract interface represents a chaincode (i.e., smart contract) in a channel. An object of the Contract type should be obtained using Network.getContract(). The Contract class helps application developers to construct and run transactions in the blockchain network. There are three methods working with transactions: createTransaction, submitTransaction, and evaluateTransaction.
Contract.createTransaction returns the Transaction class object, which provides more control over the transaction invocation. Using the Transaction object, you can set the recommended endorsing organizations and peers, as well as specify transient data that will be passed to the transaction function but will not be stored on the ledger. The transient data and its usage will be covered in the Private Data Collections chapter.
After setting all the necessary parameters, submit(args) or evaluate(args) methods can be applied to Transaction. These methods are equivalent to the peer chaincode invoke and peer chaincode query CLI command respectively. The difference is that the Transaction methods return only a payload extracted from the successful chaincode response and throw an exception otherwise, performing error handling for a developer.
Contract.submitTransaction and Contract.evaluateTransaction create the Transaction object with default invocation setting internally and then call the corresponding Transaction method.
Last updated
Was this helpful?