How do I find the history of transactions for an Asset in a blockchain implemented using hyperledger-composer?
Asked Answered
S

4

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?

Soapstone answered 22/9, 2017 at 19:54 Comment(0)
B
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.

Bharat answered 23/9, 2017 at 10:32 Comment(1)
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.Domingadomingo
A
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

Abstractionist answered 22/4, 2018 at 12:42 Comment(1)
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 itselfDomingadomingo
N
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 .

Nikkinikkie answered 10/7, 2018 at 4:48 Comment(0)
M
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);}

Mintz answered 30/4, 2019 at 11:53 Comment(1)
Hey, please format your question properly. It is not a big issue but best practice.Disturb

© 2022 - 2024 — McMap. All rights reserved.