How to send messages between instances of a smart contract in Hyperledger Fabric? - hyperledger-fabric

In a Hyperledger Fabric Network, I would like the different instances of a smart contract in different peers to be able to communicate through messages (let say for example, a communication could be a message with text string).
Then I would like that the instances of the smart contract receiving the message to be able to invoke a smart contract method based on the message content (like in a switch/case control flow statement) or send its own message to the network.
Example:
We have a network made of several organizations. Each organization has a copy of the distributed ledger and an instance of a smart contract up and running.
Let say that a smart contract can read the ledger at a specific index and triggers an event message when it read the ledger. The event message could for example contain the name of the reader and the time of the read.
Then when another instance of the smart contract receives the message, it could either send another message to all the peers or invoke a smart contract method.
I would appreciate if anyone has a solution for this use case but any ideas, thoughts or pointers would be also highly appreciated !

This feature has already been proposed in the past, and I implemented a prototype of it here.
From a high level point of view, the way it works is that a smart contract has an ability to send a message to the same smart contract running the same transaction, on another peer by sending the message to its peer, and asking it to route it to a specific peer. That peer, sends the message through the native Fabric communication infrastructure (the same used for disseminating blocks) and that remote peer forwards the message to the chaincode and inside the chaincode it routes it to the right transaction.
If you want, you can roll your own fork of Fabric and cherry pick the commits, or just use this one, but note that this fork is from 2 years ago, so all the bug fixes and security fixes in these 2 years do not exist there.

Related

How to get all past transactions in fabric?

As currently, I'm working on fabric SDKs. I want to get all the past transactions of fabric on the client-side.
Example: I already have 1 installed chain code. On fabric, I called delete_user and edit_user methods. I want those all transaction on client side without storing in offchain DB.
Can anyone suggest Node SDK method for the same?
Have each transaction function emit a suitably named chaincode event (such as "deleteUser" and "editUser"). The chaincode event gets emitted be peers only when the transaction is successfully committed and updates the ledger. Your client application can listen for those chaincode events and take action on each chaincode event.
You can start listening for chaincode events from a specific block number to replay historic events. You may also want to use a "checkpointer" (or roll-your-own) mechanism for persisting the block number and transaction ID your listener last successfully processed so you can resume listening from exactly the same point after an application restart. This would allow you to process each chaincode event exactly once, with no duplication or missed events.
See these Fabric samples for examples:
https://github.com/hyperledger/fabric-samples/tree/main/asset-transfer-events
https://github.com/hyperledger/fabric-samples/tree/main/off_chain_data

Using HLF peers for off ledger communication

I would like to leverage Hyperledger Fabric Peers (specifically identities) to communicate data that does not need to be recorded as a world state.
The Private Data Collection seems a step in that direction but everything is ordered and recorded.
Is there a way to send a payload between participants that does not get persisted?
Of course, every parameter sent is recorded in the channel's chain (but not in the world state unless your chaincode specifically does it).
Maybe you can save the payload (encrypted if you need it) in a distributed storage system (IPFS, for instance), share the IPFS hash/index via Fabric and delete from IPFS when it is no longer needed. I don't know if it fits your use case.

How to handle an event raised in one channel in a different channel in Hyperledger Composer of Fabric?

I have this scenario that a single node is part of different channels. In one channel there are only traders. In the other channel there are traders and regulator. Details of the trades should not be shared with the regulator. The only thing which is being shared is aggregated value at the end of the business day. By aggregated value, we mean how much of each asset is sold, not the detail of each trade. In other words there is confidential information between two parties that cannot be shared with regulator, and there is aggregated value that is not confidential and must be shared with the regulator.
Now I have an event when there is a nomination. By nomination I mean a trade made over some asset in the first channel, the one without regulator. Is it possible to handle this trading event in the other channel where the regulator is?
As I read through the documents, channels are isolated. But how isolated are they? Are they isolated in a way that events raised in one channel cannot be seen or handled in another channel? In that case, how do you suggest to implement this scenario? I mean do you suggest any specific architecture or tools enabling implementation of this scenario? Also do you think Hyperledger Fabric or Composer are suitable for these requirements?
I appreciate any help. Thank you.
So you have two channels, two separate ledgers and two separate business networks (as you've configured it).
Your 'nomination' is, in effect, a 'transaction' as described above. (events in Composer mean, business events that are usually subscribed to by a client application FYI).
In Hyperledger Composer, you can do 'cross-channel' or 'inter business network' queries. So can allow [only] the Regulator (from the 2nd channel) to query [only] those trade 'events' (ie transactions with the fields that contain the 'trade') or indeed assets (updated by the transaction), if need be - back in the '1st' channel (and access controls) in the 2nd channel can restrict the Regulator to do only that 'query' (if that's all he/she is supposed to have jurisdiction to do). Hope this helps.
See example
https://hyperledger.github.io/composer/latest/tutorials/invoke-composer-network

Using endorsements in Hyperledger Fabric to design a process

I would really like to understand how endorsments work in Hyperledger Fabric in order to help me in designing a solution to a problem.
let's assume I am an endorser and a transaction proposal has just arrived. I would randomly select a participant within my organization, use its identity to perform the validations, checking for replay attacks etc then sign an endorsement with that participant's private key. Assuming I used an admin's credentials, the admin(person) may not be aware that I used its identities to validate and endorse a transaction proposal. Is this example correct?
Initial discussions here makes me feel like the more I look into it, the more confused I become. Could anyone help?
Just to start with, the endorser is the peer that capable to handle incoming invocation, maintain and run the chaincode. The flow works as following, support you have a client (C) and the endorsing peer (P), which runs a chaincode (CC).
Client forms transaction proposal request which includes parameters for chaincode invocation.
In order to get an endorsement for this proposal he sends it the endorsing peer.
Endorsing peer opens a transaction proposal and forwards requests to the required chaincode along the way it passes all parameters.
Chaincode get invoked which produces a RWset (set of keys and values read of changed during the invocation)
Peer collects RWset and forms proposal response and signs it
Client gets the proposal response, signs it as well and send it to the ordering service
Ordering service collects proposal responses and cuts the block which got distributed to the peers in the network.
Upon arrival peers opens a block and validates all transactions, one of the validation is to check whenever transaction conforms the endorsement policy, where basically it checks whenever transaction has enough signatures which satisfies the policy.
Back to your question, please note that at each step everyone uses its own key and certificate to sign, no one randomly selects participants to use they identities for signatures or whatever else.
PS. Note that process above a bit simplified and lack a lot of technical details.
PPS. There is a new course on Coursera which covers pretty well many technical aspects of Hyperledger Fabric architecture and the interaction between different components, I would urge you to consider taking this course.

malicious peers in hyperledger fabric

I have a quite theoretical question about the scheme used in Hyperledger Fabric for the developers.
If a chaincode has been made by malicious node and only endorse some of the malicious nodes. Then, if a client's transaction triggers this code, the malicious nodes could manipulate with their response. Let us assume that all the responses are 1 while it should be 0. Then, if the client somehow accept their responses, it will be send through the rest of the transaction flow and eventually end up in the ledger. Hence, this incorrect result will be in the ledger.
Would this ever happen? Or did I misunderstood some parts?
For a theoretical question, the theoretical answer is no, it wouldn't happen. For a blockchain network, all peers on a channel that are involved in a transaction needs to have the same chaincode.
Also, even though a peer validates a transaction and sends those successful responses to the client. When the client submit these responses for the transaction to be committed, it will be validated by all peers involved in that transaction before being committed. Basically all parties are agreeing that this is correct at once.
If one node has a different value than all the other nodes do, then something is wrong.
You can follow this simplified explanation of the transaction flow here: http://hyperledger-fabric.readthedocs.io/en/latest/txflow.html, you can view especially step number 5.

Resources