github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/docs/source/txflow.rst (about)

     1  Transaction Flow
     2  ================
     3  
     4  This document outlines the transactional mechanics that take place during a
     5  standard asset exchange. The scenario includes two clients, A and B, who are
     6  buying and selling radishes. They each have a peer on the network through which
     7  they send their 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 has
    14  registered and enrolled with the organization’s Certificate Authority (CA) and
    15  received back necessary cryptographic material, which is used to authenticate to
    16  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 deployed to the
    20  channel. The chaincode contains logic defining a set of transaction instructions
    21  and the agreed upon price for a radish. An endorsement policy has also been set
    22  for this chaincode, stating that both ``peerA`` and ``peerB`` must endorse any
    23  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. This
    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
    32  endorse any transaction, therefore the request goes to ``peerA`` and ``peerB``.
    33  
    34  Next, the transaction proposal is constructed. An application leveraging a
    35  supported SDK (Node, Java, Go) utilizes one of the available API's
    36  to generate a transaction proposal. The proposal is a request to invoke a
    37  chaincode function with certain input parameters, with the intent of reading
    38  and/or updating the ledger.
    39  
    40  The SDK serves as a shim to package the transaction proposal into the properly
    41  architected format (protocol buffer over gRPC) and takes the user’s
    42  cryptographic credentials to produce a unique signature for this transaction
    43  proposal. The SDK submits the transaction proposal to a target peer,
    44  which will manage the transaction submission on behalf of the client.
    45  The target peer first forwards the transaction proposal to other peers
    46  for execution, as required by the endorsement policy.
    47  
    48  .. image:: images/step2.png
    49  
    50  2. **Endorsing peers verify signature & execute the transaction**
    51  
    52  The endorsing peers verify (1) that the transaction proposal is well formed, (2)
    53  it has not been submitted already in the past (replay-attack protection), (3)
    54  the signature is valid (using the MSP), and (4) that the submitter (Client A, in the
    55  example) is properly authorized to perform the proposed operation on that
    56  channel (namely, each endorsing peer ensures that the submitter satisfies the
    57  channel's *Writers* policy). The endorsing peers take the transaction proposal
    58  inputs as arguments to the invoked chaincode's function. The chaincode is then
    59  executed against the current state database to produce transaction results
    60  including a response value, read set, and write set (i.e. key/value pairs
    61  representing an asset to create or update). No updates are made to the
    62  ledger at this point. The set of these values, along with the endorsing peer’s
    63  signature is passed back as a “proposal response” to the target peer.
    64  
    65  .. note:: The MSP is a peer component that allows peers to verify transaction
    66            requests arriving from clients and to sign transaction results
    67            (endorsements). The writing policy is defined at channel creation time
    68            and determines which users are entitled to submit a transaction to
    69            that channel. For more information about membership, check out our
    70            :doc:`membership/membership` documentation.
    71  
    72  .. image:: images/step3.png
    73  
    74  3. **Proposal responses are inspected**
    75  
    76  The target peer verifies the proposal responses are the same prior to proceeding with the transaction submission.
    77  The architecture is such that even if a transaction is submitted without this check,
    78  the endorsement policy will still be checked and enforced when each peer validates transactions prior to committing them.
    79  
    80  .. image:: images/step4.png
    81  
    82  4. **Target peer assembles endorsements into a transaction**
    83  
    84  The target peer “broadcasts” the transaction proposal and response within a
    85  “transaction message” to the ordering service. The transaction contains the
    86  Channel ID, the read/write sets, and a signature from each endorsing peer.
    87  The ordering service does not need to inspect the entire content of a transaction in
    88  order to perform its operation, it simply receives transactions, orders them, and creates
    89  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
   106  transaction the write sets are committed to current state database. An event is
   107  emitted by each peer to notify the client application that the transaction (invocation)
   108  has been immutably appended to the chain, as well as notification of whether the
   109  transaction was validated or invalidated.
   110  
   111  .. note:: Applications should listen for the transaction event after submitting
   112            a transaction, for example by using the ``submitTransaction``
   113            API, which automatically listen for transaction events. Without
   114            listening for transaction events, you will not know
   115            whether your transaction has actually been ordered, validated, and
   116            committed to the ledger.
   117  
   118  You can also use the swimlane sequence diagram below to examine the
   119  transaction flow in more detail.
   120  
   121  .. image:: images/flow-4.png
   122  
   123  .. Licensed under Creative Commons Attribution 4.0 International License
   124     https://creativecommons.org/licenses/by/4.0/