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

我怕爱的太早我们不能终老 提交于 2019-12-07 02:33:45

问题


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?


回答1:


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.




回答2:


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




回答3:


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 .




回答4:


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);}



来源:https://stackoverflow.com/questions/46372302/how-do-i-find-the-history-of-transactions-for-an-asset-in-a-blockchain-implement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!