Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all transaction history against a chaincode in Hyperledger fabric

I am able to do transactions in Hyperledger (fabric implementation). I want to see all the transactions and its payload details initiated by a user by passing the user's key.

for example:

A transfers 10 units to B
A transfers 5 units to C
D transfers 8 units to A

When I pass A's key then fabric must provide me all the transactions of A. Is there any way? Or which fabric API function call should I use?

like image 974
Ahmed Shareef Avatar asked Nov 07 '16 13:11

Ahmed Shareef


People also ask

How do you get all the transactions in Hyperledger fabric?

/chain/blocks/{Block} endpoint carries ordered list of transactions in a specified block. Use /chain endpoint to get the height (number of blocks) of your chain, and then retrieve transactions from each block using /chain/blocks/{Block} REST endpoint.

How do you query historical data in Hyperledger fabric?

For querying the ledger and historic data in Hyperledger Fabric, you can use QSCC. QSCC is a system chain code that is leveraged to query peers. Numerous queries can be performed including get block by number, get block by the transaction ID, etc.

Where are transactions stored in Hyperledger fabric?

Transactions are collected inside blocks that are appended to the blockchain – enabling you to understand the history of changes that have resulted in the current world state. The blockchain data structure is very different to the world state because once written, it cannot be modified; it is immutable.

Which transactions deploy new chaincode to the Hyperledger fabric blockchain?

Hyperledger Fabric users often use the terms smart contract and chaincode interchangeably. In general, a smart contract defines the transaction logic that controls the lifecycle of a business object contained in the world state. It is then packaged into a chaincode which is then deployed to a blockchain network.


3 Answers

The best and simplest way is to use the shim package function

GetHistoryForKey(key string)

As the documentation says:

GetHistoryForKey function can be invoked by a chaincode to return a history of key values across time. GetHistoryForKey is intended to be used for read-only queries.

like image 181
giou-k Avatar answered Oct 30 '22 19:10

giou-k


/chain/blocks/{Block} endpoint carries ordered list of transactions in a specified block.

Use /chain endpoint to get the height (number of blocks) of your chain, and then retrieve transactions from each block using /chain/blocks/{Block} REST endpoint.

like image 5
Sufiyan Ghori Avatar answered Oct 30 '22 20:10

Sufiyan Ghori


You can develop the proper indexing and query function in your chaincode.

Meaning for each transaction you store its details in the internal key/value store (stub.PutState) with the user as key and return all the transactions associated to a user in your query (stub.GetState).

like image 3
Marc Campora Avatar answered Oct 30 '22 20:10

Marc Campora