Access Chaincode Functions
Posted By : Aman Sharma | 30-Nov-2022
Access Chaincode Functions
Objective: To create a class to access the chaincode functions in the project Tools required: Eclipse IDE Prerequisites: fabricjavaclient project, Hyperledger Fabric Test network |
Steps to be followed:
-
Setting up the Hyperledger Fabric Test network
-
Creating a new Class with Name ClientApp
-
Accessing chaincode functions of Carshowroom smart contract
Step 1: Setting up the Hyperledger Fabric Test network
-
Navigate to the test-network folder by using the following command:
cd eclipse-workspace/fabric-samples/test-network
-
Stop the previously running test network by running the following command:
sudo ./network.sh down
-
Remove the unused docker images by running the following command:
sudo docker system prune
-
Start the test network by executing the following command:
sudo ./network.sh up -ca -s couchdb
-
To create a communication channel for the peers in the test network use the following command:
sudo ./network.sh createChannel -c samplechannel
-
Check the list of the channels for the test network by executing the following commands:
sudo chmod 666 /var/run/docker.sock
docker exec peer0.org1.example.com peer channel list
Step 2: Creating a new Class with Name ClientApp
-
Right-click on fabricjavaclient project and navigate to New > Class to create a new class with the name ClientApp
-
Add the following code to import the libraries to ClientApp class:
import java.nio.file.Path;
import java.nio.file.Paths;
import org.hyperledger.fabric.gateway.Contract;
import org.hyperledger.fabric.gateway.Gateway;
import org.hyperledger.fabric.gateway.Network;
import org.hyperledger.fabric.gateway.Wallet;
import org.hyperledger.fabric.gateway.Wallets;
-
Add the following code under the class ClientApp to set the property:
static {
System.setProperty("org.hyperledger.fabric.sdk.service_discovery.as_localhost", "true");
}
-
Write the following code to create a gateway connect with the Carshowroom smart contract and access its chaincode functions:
public static void main(String[] args) throws Exception {
// Load a file system based wallet for managing identities.
Path walletPath = Paths.get("wallet");
Wallet wallet = Wallets.newFileSystemWallet(walletPath);
// load a CCP
Path networkConfigPath = Paths.get("..", "..", "test-network", "organizations", "peerOrganizations", "org1.example.com", "connection-org1.yaml");
Gateway.Builder builder = Gateway.createBuilder();
builder.identity(wallet, "appUser").networkConfig(networkConfigPath).discovery(true);
// create a gateway connection
try (Gateway gateway = builder.connect()) {
// get the network and contract
Network network = gateway.getNetwork("samplechannel");
Contract contract = network.getContract("CarShowroom");
byte[] result;
contract.submitTransaction("addNewCar", "1", "Ford", "Grey", "7890");
result = contract.evaluateTransaction("queryCarById", "1");
System.out.println(new String(result));
contract.submitTransaction("changeCarOwnership", "1", "Joe");
result = contract.evaluateTransaction("queryCarById", "1");
System.out.println(new String(result));
}
}
-
Note: Press Ctrl+S to save the file
Step 3: Accessing chaincode functions of Carshowroom chaincode
-
Now install, approve, and commit Carshowroom chaincode
-
In Eclipse, run the ClientApp file to access the chaincode functions
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Aman Sharma
Aman is an accomplished Backend developer with extensive industry experience, specializing in Odoo technology. He has a profound understanding and expertise in Python, Odoo ERP, Flask, MySQL, PostgreSQL, JavaScript, and payment gateway integration, particularly with Stripe. Aman's exceptional contributions to projects like Mymandi, Pando store, Markivia, DataInsite, and Tundra have been instrumental in driving the growth of his company. His strong analytical skills are pivotal in ensuring the success of his work, as he is able to identify and solve complex problems efficiently. Beyond his professional endeavors, he possesses a genuine passion for learning new technologies.