At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

,

How To Get Data Of Transaction History In Hyperledger Composer

  • By Guru Moorthy
  • March 12, 2019
  • 1115 Views

Have you ever tried of getting the data of transaction history in Hyperledger Composer? If yes, have you succeeded in fetching data? or still trying to get but couldn’t find the right way to acquire the data of transaction in hyperledger composer? Then this guide would definitely will help you to get any specific data of transaction history in Hyperledger Composer.

Hyperledger Installation

Here I am using hyperledger fabric 1.2, composer 0.20 (latest), and mac Os.
1) Hyperledger chain code,
2) Orderer
3) Business network
4) Composer rest api
Follow this official doc to install https://hyperledger.github.io/composer/latest/installing/installing-prereqs
I hope u have installed all the dependencies,
Check whether you have installed all the dependencies mentioned below,

  • Operating Systems: Ubuntu Linux 14.04 / 16.04 LTS (both 64-bit), or Mac OS 10.12
  • Docker Engine: Version 17.03 or higher
  • Docker-Compose: Version 1.8 or higher
  • Node: 8.9 or higher (note version 9 and higher is not supported)
  • npm: v5.x
  • git: 2.9.x or higher
  • Python: 2.7.x
  • We recommend VSCode for code editor choice
  • composer-cli@0.20
  • composer-rest-server@0.20
  • generator-hyperledger-composer@0.20
  • yo

 

Create bna File In Hyperledger

Before creating the business network, we need to understand the few basic concept of business module first. So, let me explain you this business module through simple example.
Assume that you are going to add a car as the asset, For that we’re going to store the data such as model name, owner name, price.
Before that, here you should understand the concept of “RESOURCES” and “REGISTRY” in hyperledger composer.

  • The resource class represents the type of asset or participant in the car model in registry.
  • New instances are created by “FACTORY”.
  • We can get the existing instances by “REGISTRY”.

 

Factory In Hyperledger Composer

You can use the Factory to create instances of Resource: transactions, participants and assets.
Do not attempt to create an instance of this class, but you can use the getFactory method instead.

newConcept Concept Create a new concept with a given namespace, type, and identifier
newEvent Resource Create a new type with a given namespace and type
newRelationship Relationship Create a new relationship with a given namespace, type, and identifier
newResource Resource Create a new resource (an instance of an asset, participant, or transaction)

 

REGISTRY

There are two type of registry,

  • Asset registry
  • participant registry


You can get the complete methods along with sample examples for both registry here.

Link for asset registry

https://hyperledger.github.io/composer/v0.19/api/runtime-assetregistry

Link for participant registry

https://hyperledger.github.io/composer/v0.19/api/runtime-participantregistry
Above links will have more examples so to narrow down the result here we’re going to use the getAssetRegistry and sub methods for crud operation.
crud - hyperledger

Related: Agira Featured as “10 Fastest Growing Blockchain Solution Provider To Watch In 2019”

 

Steps To Create BNA File

Step 1: Using yo generator we can create the scaffolding of business network & you could use the below command for that.

yo hyperledger-composer

 
Step 2: Here you will be asked to choose the type of project and there you can choose “Bussiness Network”.
Step 3: Give the required details of the project as mentioned in below screen.

hyperledger composer
Step 4: Now, it will create you a folder with the name as you mentioned in the business network.
Then enter into the car-bna folder
It will have three files such as,
model file – Where we need to change the namespace & also you can define assets, participants, transactions.
Logic file – Here you could write a logic of what’re the actions can happen while transaction happens.
Permission file – where you can give and restrict the permission.
Step 5: Change the name space and add the asset & the fields in the org.guru.cars.cto file.

namespace org.guru.cars
concept CarDetails {
o String modelName
o String ownerName
o String price
}
 
asset Car identified by carId {
o String carId
o CarDetails carDetails
}

 
In this stage, we must add the transaction in the model file.

Best To Read: 6 Essential Q&A We Must Know About Blockchain

 

Create Transaction In Hyperledger Composer

Transactions are executed by participant to change the asset, likely, events are emitted when the transaction is executed, here the transactions will automatically create transactionId and also will record the execution time using timestamp.
Hyperledger composer
Create transaction (createCar) with carNumber and carDetails like this,

transaction createCar {
  o String carId
  o CarDetails carDetails
}
Event for the transaction
event NewCarCreated {
  o Car car
}

 
Now the file will look like this,
hyperledger composer
Now we need to write the logic for this transaction,
Let’s start applying the REGISTRY and FACTORY.
Create new folder named “lib” and inside of it add new file called “logic.js”, also you can spread the LOGIC in different file and ANNOTATIONS will connect code to the model.
Here, we should write the logic for the transaction in this js file.
First we should make the connection to the mode we use “@param” to connect the model and asset like this “org.guru.cars.createCar” and right after that, you can pass the data “createCar”. Also the use key is “@transaction” this will indicate that this function is a transaction.

* Creates a new Car with a aadharNo and a Car object, contained in the
* createCar transaction passed into this function. The default Car access value
* is true. The Car will be added to the AssetRegistry.
* @param {org.guru.cars.createCar} createCar The createCar transaction.
* @transaction
*/
function createCar(createCar) {
   var newCar;
 
  return getAssetRegistry('org.guru.cars.Car')
  .then(function (carRegistry) {
     
      // create new instance of a Car
      newCar = getFactory().newResource('org.guru.cars', 'Car', createCar.carId);
 
      newCar.carId = createCar.carId;
      newCar.carDetails = createCar.carDetails;
      return carRegistry.add(newCar);
  })
  .then(function () {
      // Emit an event for the new Car creation.
      var event = getFactory().newEvent('org.guru.cars', 'NewCarCreated');
      event.car = newCar;
      emit(event);
  });
}

 
Then write the function name which should represent the transaction name.
And remember, while you pass the data, make sure to use the same params name which you assigned initially. For eg * @param {org.guru.cars.createCar} createCar
Here we’re using getAssetRegistry to get asset data in asset registry vice versa you can also use getPartcipantRegistry to get participants in participants registry.
getFactory().newResource() – To Create a new resource (an instance of an asset, participant, or transaction)
carRegistry.add(newCar); – used to add the new asset to asset registry
emit(event) – Used to declare the created event details in transaction.
Now model file will look like this,
hyperledger composer
Then follow the steps to build and deploy node,

  1. mkdir dist
  2. cd dist
  3. composer network install -a car-bna@0.0.1.bna -c PeerAdmin@hlfv1
  4. composer network start -c PeerAdmin@hlfv1 -n car-bna -V 0.0.1 -A admin -S adminpw
  5. composer card import -f ./admin@car-bna.card
  6. composer network ping -c admin@car-bna

Start the composer rest server to test

Composer-rest-server

 
Then give details like below image,

Then you create the asset in createCar transaction API, after executing the api you can able to get the desired transaction details in Historian.
So, the final output will be like this once you query this historian.

You can also get my full code in github : https://github.com/guru28/hyperledger-historian
That’s all guys, hope now you learned to get data of transaction in Hyperledger composer. Give it a try and let us know your doubts & thoughts in the comment section below! Our Blockchain enthusiasts would love to hear from you!
Happy coding “HYPERLEDGERS”
[contact-form-7 404 "Not Found"]