Linear
Linear

Reputation: 45

How can be a chaincode executed in parallel?

I've been studying Hyperledger Fabric for about 1 year.

But I still can't tell how a chaincode works and is dealt with exactly, especially in terms of its implementation and process.

This is how I understand a chaincode below.

A chaincode is running and is isolated from peers, as one of docker containers for its integrity in the network. The chaincode is a program which defines how functions in transactions(e.g., AddTwoIntegerValues()) should update the ledger

Also, endorsing peers access the chaincode for executing functions in the transaction proposals from clients or other peers to respond with endorsement.(This process is not clear)

In that case, I'd like to ask you how those endorsing peers can execute or access the chaincode container in parallel? I heard 'chaincode execution' means simulation of the chaincode logic computation. But I can't get that.

In other words, my question is how can they execute or access that at the same time? Do they have copies of that chaincode? How do peers know the chaincode logic? Could you please correct me or explain chaincode process if I'm wrong?

Please tell me if you have any idea about my question. I'm looking forward to your answer!

Upvotes: 1

Views: 768

Answers (1)

yacovm
yacovm

Reputation: 5140

Each peer has its own chaincode container for each version of a given chaincode code. When the chaincode is installed on a peer, the code package of the chaincode's code is written to the file system.

Later on, when the peer receives a request to invoke a function on the chaincode (Init() or Invoke(), when Init() runs on instantiate), the peer checks to see if the chaincode is already running, and if not, it:

  • Spins a container to compile the chaincode code package to produce the chaincode shim binary and afterwards destroys it
  • Spins a container that would actually run the chaincode shim binary.

The chaincode shim binary:

  • Connects to the peer via gRPC and registers itself to be the name it is installed with.
  • Runs an endless loop in which it waits for commands to simulate transactions from the peer.

Whenever the peer receives a proposal from a client, it:

  • Locates the chaincode container gRPC stream and forwards the proposal from the client to the chaincode
  • Prepares a map of key reads and writes that the chaincode shim would ask the peer during its execution, which is called a Read-Write set.

Then, the chaincode shim, running inside the container - extracts the arguments from the proposal and starts running the chaincode logic (part of its binary).

If the chaincode logic contains data access operations such as GetState or PutState, it sends a request to the peer down the same gRPC stream it is connected to the peer, and then the peer does the following:

  • If the data operation is a read operation, it adds to the read-write set the key and the version read from the key, and sends back the key and the value to the chaincode shim container over the gRPC stream.
  • If the data operation is a write operation, it adds to the read-write set the key and the value and the version, and then sends back an "OK" to the chaincode shim(*).

(*) I personally think this step is not necessary... it just prolongs the execution time

After the chaincode shim finished computing the transaction, it sends back the result (i.e "OK") to the peer, along with a marker that denotes that the transaction finished executing.

Since the values that are written, are not really written to the DB but just added to an in-memory map, this is called a "transaction simulation". The peer then proceeds to sign the transaction simulation's results (the read-write set map and the result from the chaincode shim) and then returns the signed transaction simulation (also called an "endorsement") back to the client as a response.

In that case, I'd like to ask you how those endorsing peers can execute or access the chaincode container in parallel?

You can do that in parallel because when the chaincode shim invokes the transaction, it does so on a separate goroutine.

Upvotes: 6

Related Questions