github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/docs/source/txflow.rst (about)

     1  Transaction Flow
     2  ================
     3  
     4  This document outlines the transactional mechanics that take place during a standard asset
     5  exchange.  The scenario includes two clients, A and B, who are buying and selling
     6  radishes.  They each have a peer on the network through which they send their
     7  transactions and interact with the ledger.
     8  
     9  .. image:: images/step0.png
    10  
    11  **Assumptions**
    12  
    13  This flow assumes that a channel is set up and running.  The application user
    14  has registered and enrolled with the organization’s certificate authority (CA)
    15  and received back necessary cryptographic material, which is used to authenticate
    16  to the network.
    17  
    18  The chaincode (containing a set of key value pairs representing the initial
    19  state of the radish market) is installed on the peers and instantiated on the
    20  channel.  The chaincode contains logic defining a set of transaction
    21  instructions and the agreed upon price for a radish. An endorsement policy has
    22  also been set for this chaincode, stating that both ``peerA`` and ``peerB`` must endorse
    23  any transaction.
    24  
    25  .. image:: images/step1.png
    26  
    27  1. **Client A initiates a transaction**
    28  
    29  What's happening? - Client A is sending a request to purchase radishes.  The
    30  request targets ``peerA`` and ``peerB``, who are respectively representative of
    31  Client A and Client B. The endorsement policy states that both peers must endorse
    32  any transaction, therefore the request goes to ``peerA`` and ``peerB``.
    33  
    34  Next, the transaction proposal is constructed.  An application leveraging a supported
    35  SDK (node, java, python) utilizes one of the available API's which generates a
    36  transaction proposal.  The proposal is a request to invoke a chaincode function
    37  so that data can be read and/or written to the ledger (i.e. write new key value
    38  pairs for the assets).  The SDK serves as a shim to package the transaction proposal
    39  into the properly architected format (protocol buffer over gRPC) and takes the user’s
    40  cryptographic credentials to produce a unique signature for this transaction proposal.
    41  
    42  .. image:: images/step2.png
    43  
    44  2. **Endorsing peers verify signature & execute the transaction**
    45  
    46  The endorsing peers verify the signature (using MSP) and determine if the
    47  submitter is properly authorized to perform the proposed operation (using the
    48  channel's ACL). The endorsing peers take the transaction proposal arguments as
    49  inputs and execute them against the current state database to produce transaction
    50  results including a response value, read set, and write set.  No updates are
    51  made to the ledger at this point. The set of these values, along with the
    52  endorsing peer’s signature and a YES/NO endorsement statement is passed back as
    53  a “proposal response” to the SDK which parses the payload for the application to
    54  consume.
    55  
    56  *{The MSP is a local process running on the peers which allows them to verify
    57  transaction requests arriving from clients and to sign transaction results(endorsements).
    58  The ACL (Access Control List) is defined at channel creation time, and determines
    59  which peers and end users are permitted to perform certain actions.}*
    60  
    61  
    62  .. image:: images/step3.png
    63  
    64  3. **Proposal responses are inspected**
    65  
    66  The application verifies the endorsing peer signatures and compares the proposal
    67  responses (link to glossary term which will contain a representation of the payload)
    68  to determine if the proposal responses are the same and if the specified endorsement
    69  policy has been fulfilled (i.e. did peerA and peerB both endorse).  The architecture
    70  is such that even if an application chooses not to inspect responses or otherwise
    71  forwards an unendorsed transaction, the policy will still be enforced by peers
    72  and upheld at the commit validation phase.
    73  
    74  .. image:: images/step4.png
    75  
    76  4. **Client assembles endorsements into a transaction**
    77  
    78  The application “broadcasts” the transaction proposal and response within a
    79  “transaction message” to the Ordering Service. The transaction will contain the
    80  read/write sets, the endorsing peers signatures and the Channel ID.  The
    81  Ordering Service does not read the transaction details, it simply receives
    82  transactions from all channels in the network, orders them chronologically by
    83  channel, and creates blocks of transactions per channel.
    84  
    85  .. image:: images/step5.png
    86  
    87  5. **Transaction is validated and committed**
    88  
    89  The blocks of transactions are “delivered” to all peers on the channel.  The
    90  transactions within the block are validated to ensure endorsement policy is
    91  fulfilled and to ensure that there have been no changes to ledger state for read
    92  set variables since the read set was generated by the transaction execution.
    93  Transactions in the block are tagged as being valid or invalid.
    94  
    95  .. image:: images/step6.png
    96  
    97  6. **Ledger updated**
    98  
    99  Each peer appends the block to the channel’s chain, and for each valid transaction
   100  the write sets are committed to current state database. An event is emitted, to
   101  notify the client application that the transaction (invocation) has been
   102  immutably appended to the chain, as well as notification of whether the
   103  transaction was validated or invalidated.
   104  
   105  **Note**: See the :ref:`swimlane` diagram to better understand the server side flow and the
   106  protobuffers.