github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/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 (1) that the transaction proposal is well formed,
    47  (2) it has not been submitted already in the past (replay-attack protection),
    48  (3) the signature is valid (using MSP), and (4) that the
    49  submitter (Client A, in the example) is properly authorized to perform
    50  the proposed operation on that channel (namely, each endorsing peer ensures that
    51  the submitter satisfies the channel's *Writers* policy).
    52  The endorsing peers take the transaction proposal inputs as
    53  arguments to the invoked chaincode's function. The chaincode is then
    54  executed against the current state database to produce transaction
    55  results including a response value, read set, and write set.  No updates are
    56  made to the ledger at this point. The set of these values, along with the
    57  endorsing peer’s signature and a YES/NO endorsement statement is passed back as
    58  a “proposal response” to the SDK which parses the payload for the application to
    59  consume.
    60  
    61  *{The MSP is a peer component that allows them to verify
    62  transaction requests arriving from clients and to sign transaction results(endorsements).
    63  The *Writing* policy is defined at channel creation time, and determines
    64  which user is entitled to submit a transaction to that channel.}*
    65  
    66  
    67  .. image:: images/step3.png
    68  
    69  3. **Proposal responses are inspected**
    70  
    71  The application verifies the endorsing peer signatures and compares the proposal
    72  responses (link to glossary term which will contain a representation of the payload)
    73  to determine if the proposal responses are the same and if the specified endorsement
    74  policy has been fulfilled (i.e. did peerA and peerB both endorse).  The architecture
    75  is such that even if an application chooses not to inspect responses or otherwise
    76  forwards an unendorsed transaction, the policy will still be enforced by peers
    77  and upheld at the commit validation phase.
    78  
    79  .. image:: images/step4.png
    80  
    81  4. **Client assembles endorsements into a transaction**
    82  
    83  The application “broadcasts” the transaction proposal and response within a
    84  “transaction message” to the Ordering Service. The transaction will contain the
    85  read/write sets, the endorsing peers signatures and the Channel ID.  The
    86  Ordering Service does not need to inspect the entire content of a transaction in order to perform
    87  its operation, it simply receives
    88  transactions from all channels in the network, orders them chronologically by
    89  channel, and creates blocks of transactions per channel.
    90  
    91  .. image:: images/step5.png
    92  
    93  5. **Transaction is validated and committed**
    94  
    95  The blocks of transactions are “delivered” to all peers on the channel.  The
    96  transactions within the block are validated to ensure endorsement policy is
    97  fulfilled and to ensure that there have been no changes to ledger state for read
    98  set variables since the read set was generated by the transaction execution.
    99  Transactions in the block are tagged as being valid or invalid.
   100  
   101  .. image:: images/step6.png
   102  
   103  6. **Ledger updated**
   104  
   105  Each peer appends the block to the channel’s chain, and for each valid transaction
   106  the write sets are committed to current state database. An event is emitted, to
   107  notify the client application that the transaction (invocation) has been
   108  immutably appended to the chain, as well as notification of whether the
   109  transaction was validated or invalidated.
   110  
   111  **Note**: See the :ref:`swimlane` diagram to better understand the server side flow and the
   112  protobuffers.
   113  
   114  .. Licensed under Creative Commons Attribution 4.0 International License
   115     https://creativecommons.org/licenses/by/4.0/
   116