6

I'm working on the latest rev of hyperledger-composer (V0.13) and have built a network with multiple roles, each of which can invoke selected transactions within the blockchain. I would now like to query the blockchain (?Historian?) for all transactions which have been executed against a specific Order (defined type of asset).

I've used two different appoaches to pulling Historian data, once through direct API access historian.getall() and the other through a defined query:

query getHistorianRecords {
  description: "get all Historian records"
  statement: SELECT org.hyperledger.composer.system.HistorianRecord

}

Both queries succeed in that they return all transactions within the system. per ex:

ValidatedResource {
    '$modelManager': ModelManager { modelFiles: [Object] },
    '$namespace': 'org.hyperledger.composer.system',
    '$type': 'HistorianRecord',
    '$identifier': '0c3274475fed3703692bb7344453ab0910686905451b41d5d08ff1b032732aa1',
    '$validator': ResourceValidator { options: {} },
    transactionId: '0c3274475fed3703692bb7344453ab0910686905451b41d5d08ff1b032732aa1',
    transactionType: 'org.acme.Z2BTestNetwork.CreateOrder',
    transactionInvoked: 
     Relationship {
       '$modelManager': [Object],
       '$namespace': 'org.acme.Z2BTestNetwork',
       '$type': 'CreateOrder',
       '$identifier': '0c3274475fed3703692bb7344453ab0910686905451b41d5d08ff1b032732aa1',
       '$class': 'Relationship' },
    eventsEmitted: [],
    transactionTimestamp: 2017-09-22T19:32:48.182Z }

What I can't find, and need, is a way to query the history of transactions against a single Order. An Order is defined (partial listing) as follows:

asset Order identified by orderNumber {
    o String orderNumber
    o String[] items
    o String status
    ...
    o String approved
    o String paid
    --> Provider provider
    --> Shipper shipper
    --> Buyer buyer
    --> Seller seller 
    --> FinanceCo financeCo 

What I'm looking for is a mechanism that will allow me to query the blockchain to get every record pertaining to an Order with orderNumber = '009'. I can, and have, easily found the current state of Order # 009, but am now looking for the history of transactions against that order. How to I tell Historian, or another service in the hyperledger-composer system, to give me that information?

Bob Dill
  • 938
  • 4
  • 12

4 Answers4

6

What you are trying to do makes total sense, however the Historian doesn't yet support it. This requirement is being tracked here: https://github.com/hyperledger/composer/issues/991

The plan is to add metadata into the HistorianRecord to capture the IDs of the assets and participants that where impacted by the transaction along with the operation performed (delete, update, create, read?).

Once that is in place you will be able to query for HistorianRecords that reference a given asset/participant id.

Dan Selman
  • 2,237
  • 1
  • 7
  • 17
  • can you please give an example or more explanation on how to add metadata to the HistorianRecord?. I have tried adding my asset id inside the events emitted so that I can query for all the transaction containing the events with the asset id but the unfortunate bit I could write a query that can do that. – Gsp Ivan Apr 03 '19 at 05:26
1

My work around is to have the transaction emit the results as event and have a query to then fetch the transaction by id from HistorianRecords which will contains the results emitted as event before.

I've posted the detailed answer here: https://github.com/hyperledger/composer/issues/2458#issuecomment-383377837

tiengtinh
  • 121
  • 7
  • can u provide an example on how to write such query because I tried doing link you suggested but I failed to write a query that can filter the transactions by the events emitted. Am emitting an events that contains the asset Id and the asset itself – Gsp Ivan Apr 03 '19 at 05:29
0

A good workaround would be write a script function to update asset , emit the results as an event . And filter it from the rest endpoint in transactions tab .

Skadoosh
  • 567
  • 7
  • 22
0

One workaround solution is to emit an Event for your transaction. Mention ASSET as a field of the event. Please experiment with the following code.

let historian = await businessNetworkConnection.getHistorian(); let historianRecords = await historian.getAll(); for(let i=0; i< historianRecords.length;i++) {console.log(historianRecords[i].transactionId+ "-----> " + historianRecords[i].eventsEmitted[0].YOUR_ASSET_NAME);}

  • Hey, please [format your question](https://stackoverflow.com/help/formatting) properly. It is not a big issue but best practice. – Aaron3219 Apr 30 '19 at 12:16