github.com/kaituanwang/hyperledger@v2.0.1+incompatible/docs/source/developapps/chaincodenamespace.md (about)

     1  # Chaincode namespace
     2  
     3  **Audience**: Architects, application and smart contract developers,
     4  administrators
     5  
     6  A chaincode namespace allows it to keep its world state separate from other
     7  chaincodes. Specifically, smart contracts in the same chaincode share direct
     8  access to the same world state, whereas smart contracts in different chaincodes
     9  cannot directly access each other's world state. If a smart contract needs to
    10  access another chaincode world state, it can do this by performing a
    11  chaincode-to-chaincode invocation. Finally, a blockchain can contain
    12  transactions which relate to different world states.
    13  
    14  In this topic, we're going to cover:
    15  
    16  * [The importance of namespaces](#motivation)
    17  * [What is a chaincode namespace](#scenario)
    18  * [Channels and namespaces](#channels)
    19  * [How to use chaincode namespaces](#usage)
    20  * [How to access world states across smart contracts](#cross-chaincode-access)
    21  * [Design considerations for chaincode namespaces](#considerations)
    22  
    23  ## Motivation
    24  
    25  A namespace is a common concept. We understand that *Park Street, New York* and
    26  *Park Street, Seattle* are different streets even though they have the same
    27  name. The city forms a **namespace** for Park Street, simultaneously providing
    28  freedom and clarity.
    29  
    30  It's the same in a computer system. Namespaces allow different users to program
    31  and operate different parts of a shared system, without getting in each other's
    32  way. Many programming languages have namespaces so that programs can freely
    33  assign unique identifiers, such as variable names, without worrying about other
    34  programs doing the same. We'll see that Hyperledger Fabric uses namespaces to
    35  help smart contracts keep their ledger world state separate from other smart
    36  contracts.
    37  
    38  ## Scenario
    39  
    40  Let's examine how the ledger world state organizes facts about business objects
    41  that are important to the organizations in a channel using the diagram below.
    42  Whether these objects are commercial papers, bonds, or vehicle registrations,
    43  and wherever they are in their lifecycle, they are maintained as states within
    44  the ledger world state database. A smart contract manages these business objects
    45  by interacting with the ledger (world state and blockchain), and in most cases
    46  this will involve it querying or updating the ledger world state.
    47  
    48  It's vitally important to understand that the ledger world state is partitioned
    49  according to the chaincode of the smart contract that accesses it, and this
    50  partitioning, or *namespacing* is an important design consideration for
    51  architects, administrators and programmers.
    52  
    53  ![chaincodens.scenario](./develop.diagram.50.png) *The ledger world state is
    54  separated into different namespaces according to the chaincode that accesses it.
    55  Within a given channel, smart contracts in the same chaincode share the same
    56  world state, and smart contracts in different chaincodes cannot directly access
    57  each other's world state. Likewise, a blockchain can contain transactions that
    58  relate to different chaincode world states.*
    59  
    60  In our example, we can see four smart contracts defined in two different
    61  chaincodes, each of which is in their own chaincode container. The `euroPaper`
    62  and `yenPaper` smart contracts are defined in the `papers` chaincode. The
    63  situation is similar for the `euroBond` and `yenBond` smart contracts  -- they
    64  are defined in the `bonds` chaincode. This design helps application programmers
    65  understand whether they are working with commercial papers or bonds priced in
    66  Euros or Yen, and because the rules for each financial product don't really
    67  change for different currencies, it makes sense to manage their deployment in
    68  the same chaincode.
    69  
    70  The [diagram](#scenario) also shows the consequences of this deployment choice.
    71  The database management system (DBMS) creates different world state databases
    72  for the `papers` and `bonds` chaincodes and the smart contracts contained within
    73  them. `World state A` and `world state B` are each held within distinct
    74  databases; the data are isolated from each other such that a single world state
    75  query (for example) cannot access both world states. The world state is said to
    76  be *namespaced* according to its chaincode.
    77  
    78  See how `world state A` contains two lists of commercial papers `paperListEuro`
    79  and `paperListYen`. The states `PAP11` and `PAP21` are instances of each paper
    80  managed by the `euroPaper` and `yenPaper` smart contracts respectively. Because
    81  they share the same chaincode namespace, their keys (`PAPxyz`) must be unique
    82  within the namespace of the `papers` chaincode, a little like a street name is
    83  unique within a town. Notice how it would be possible to write a smart contract
    84  in the `papers` chaincode that performed an aggregate calculation over all the
    85  commercial papers -- whether priced in Euros or Yen -- because they share the
    86  same namespace. The situation is similar for bonds -- they are held within
    87  `world state B` which maps to a separate `bonds` database, and their keys must
    88  be unique.
    89  
    90  Just as importantly, namespaces mean that `euroPaper` and `yenPaper` cannot
    91  directly access `world state B`, and that `euroBond` and `yenBond` cannot
    92  directly access `world state A`. This isolation is helpful, as commercial papers
    93  and bonds are very distinct financial instruments; they have different
    94  attributes and are subject to different rules. It also means that `papers` and
    95  `bonds` could have the same keys, because they are in different namespaces. This
    96  is helpful; it provides a significant degree of freedom for naming. Use this
    97  freedom to name different business objects meaningfully.
    98  
    99  Most importantly, we can see that a blockchain is associated with the peer
   100  operating in a particular channel, and that it contains transactions that affect
   101  both `world state A` and `world state B`. That's because the blockchain is the
   102  most fundamental data structure contained in a peer. The set of world states can
   103  always be recreated from this blockchain, because they are the cumulative
   104  results of the blockchain's transactions. A world state helps simplify smart
   105  contracts and improve their efficiency, as they usually only require the current
   106  value of a state. Keeping world states separate via namespaces helps smart
   107  contracts isolate their logic from other smart contracts, rather than having to
   108  worry about transactions that correspond to different world states. For example,
   109  a `bonds` contract does not need to worry about `paper` transactions, because it
   110  cannot see their resultant world state.
   111  
   112  It's also worth noticing that the peer, chaincode containers and DBMS all are
   113  logically different processes. The peer and all its chaincode containers are
   114  always in physically separate operating system processes, but the DBMS can be
   115  configured to be embedded or separate, depending on its
   116  [type](../ledger/ledger.html#world-state-database-options). For LevelDB, the
   117  DBMS is wholly contained within the peer, but for CouchDB, it is a separate
   118  operating system process.
   119  
   120  It's important to remember that namespace choices in this example are the result
   121  of a business requirement to share commercial papers in different currencies but
   122  isolate them separate from bonds. Think about how the namespace structure would
   123  be modified to meet a business requirement to keep every financial asset class
   124  separate, or share all commercial papers and bonds?
   125  
   126  ## Channels
   127  
   128  If a peer is joined to multiple channels, then a new blockchain is created
   129  and managed for each channel. Moreover, every time a chaincode is deployed to
   130  a new channel, a new world state database is created for it. It means that
   131  the channel also forms a kind of namespace alongside that of the chaincode for
   132  the world state.
   133  
   134  However, the same peer and chaincode container processes can be simultaneously
   135  joined to multiple channels -- unlike blockchains, and world state databases,
   136  these processes do not increase with the number of channels joined.
   137  
   138  For example, if you deployed the `papers` and `bonds` chaincode to a new
   139  channel, there would a totally separate blockchain created, and two new world
   140  state databases created. However, the peer and chaincode containers would not
   141  increase; each would just be connected to multiple channels.
   142  
   143  ## Usage
   144  
   145  Let's use our commercial paper [example](#scenario) to show how an application
   146  uses a smart contract with namespaces. It's worth noting that an application
   147  communicates with the peer, and the peer routes the request to the appropriate
   148  chaincode container which then accesses the DBMS. This routing is done by the
   149  peer **core** component shown in the diagram.
   150  
   151  Here's the code for an application that uses both commercial papers and bonds,
   152  priced in Euros and Yen. The code is fairly self-explanatory:
   153  
   154  ```javascript
   155  const euroPaper = network.getContract(papers, euroPaper);
   156  paper1 = euroPaper.submit(issue, PAP11);
   157  
   158  const yenPaper = network.getContract(papers, yenPaper);
   159  paper2 = yenPaper.submit(redeem, PAP21);
   160  
   161  const euroBond = network.getContract(bonds, euroBond);
   162  bond1 = euroBond.submit(buy, BON31);
   163  
   164  const yenBond = network.getContract(bonds, yenBond);
   165  bond2 = yenBond.submit(sell, BON41);
   166  ```
   167  
   168  See how the application:
   169  
   170  * Accesses the `euroPaper` and `yenPaper` contracts using the `getContract()`
   171    API specifying the `papers` chaincode. See interaction points **1a** and
   172    **2a**.
   173  
   174  * Accesses the `euroBond` and `yenBond` contracts using the `getContract()` API
   175    specifying the `bonds` chaincode. See interaction points **3a** and **4a**.
   176  
   177  * Submits an `issue` transaction to the network for commercial paper `PAP11`
   178    using the `euroPaper` contract. See interaction point **1a**. This results in
   179    the creation of a commercial paper represented by state `PAP11` in `world
   180    state A`; interaction point **1b**. This operation is captured as a
   181    transaction in the blockchain at interaction point **1c**.
   182  
   183  * Submits a `redeem` transaction to the network for commercial paper `PAP21`
   184    using the `yenPaper` contract. See interaction point **2a**. This results in
   185    the creation of a commercial paper represented by state `PAP21` in `world
   186    state A`; interaction point **2b**. This operation is captured as a
   187    transaction in the blockchain at interaction point **2c**.
   188  
   189  * Submits a `buy` transaction to the network for bond `BON31` using the
   190    `euroBond` contract. See interaction point **3a**. This results in the
   191    creation of a bond represented by state `BON31` in `world state B`;
   192    interaction point **3b**. This operation is captured as a transaction in the
   193    blockchain at interaction point **3c**.
   194  
   195  * Submits a `sell` transaction to the network for bond `BON41` using the
   196    `yenBond` contract. See interaction point **4a**. This results in the creation
   197    of a bond represented by state `BON41` in `world state B`; interaction point
   198    **4b**. This operation is captured as a transaction in the blockchain at
   199    interaction point **4c**.
   200  
   201  
   202  See how smart contracts interact with the world state:
   203  
   204  * `euroPaper` and `yenPaper` contracts can directly access `world state A`, but
   205    cannot directly access `world state B`. `World state A` is physically held in
   206    the `papers` database in the database management system (DBMS) corresponding
   207    to the `papers` chaincode.
   208  
   209  * `euroBond` and `yenBond` contracts can directly access `world state B`, but
   210    cannot directly access `world state A`. `World state B` is physically held in
   211    the `bonds` database in the database management system (DBMS) corresponding to
   212    the `bonds` chaincode.
   213  
   214  
   215  See how the blockchain captures transactions for all world states:
   216  
   217  * Interactions **1c** and **2c** correspond to transactions create and update
   218    commercial papers `PAP11` and `PAP21` respectively. These are both contained
   219    within `world state A`.
   220  
   221  * Interactions **3c** and **4c** correspond to transactions both update bonds
   222    `BON31` and `BON41`. These are both contained within `world state B`.
   223  
   224  * If `world state A` or `world state B` were destroyed for any reason, they
   225    could be recreated by replaying all the transactions in the blockchain.
   226  
   227  
   228  ## Cross chaincode access
   229  
   230  As we saw in our example [scenario](#scenario), `euroPaper` and `yenPaper`
   231  cannot directly access `world state B`.  That's because we have designed our
   232  chaincodes and smart contracts so that these chaincodes and world states are
   233  kept separately from each other.  However, let's imagine that `euroPaper` needs
   234  to access `world state B`.
   235  
   236  Why might this happen? Imagine that when a commercial paper was issued, the
   237  smart contract wanted to price the paper according to the current return on
   238  bonds with a similar maturity date.  In this case it will be necessary for the
   239  `euroPaper` contract to be able to query the price of bonds in `world state B`.
   240  Look at the following diagram to see how we might structure this interaction.
   241  
   242  ![chaincodens.scenario](./develop.diagram.51.png) *How chaincodes and smart
   243  contracts can indirectly access another world state -- via its chaincode.*
   244  
   245  Notice how:
   246  
   247  * the application submits an `issue` transaction in the `euroPaper` smart
   248    contract to issue `PAP11`. See interaction **1a**.
   249  
   250  * the `issue` transaction in the `euroPaper` smart contract calls the `query`
   251    transaction in the `euroBond` smart contract. See interaction point **1b**.
   252  
   253  * the `query`in `euroBond` can retrieve information from `world state B`. See
   254     interaction point **1c**.
   255  
   256  * when control returns to the `issue` transaction, it can use the information in
   257    the response to price the paper and update `world state A` with information.
   258    See interaction point **1d**.
   259  
   260  * the flow of control for issuing commercial paper priced in Yen is the same.
   261    See interaction points **2a**, **2b**, **2c** and **2d**.
   262  
   263  Control is passed between chaincode using the `invokeChaincode()`
   264  [API](https://hyperledger.github.io/fabric-chaincode-node/{BRANCH}/api/fabric-shim.ChaincodeStub.html#invokeChaincode__anchor).
   265  This API passes control from one chaincode to another chaincode.
   266  
   267  Although we have only discussed query transactions in the example, it is
   268  possible to invoke a smart contract which will update the called chaincode's
   269  world state.  See the [considerations](#considerations) below.
   270  
   271  ## Considerations
   272  
   273  * In general, each chaincode will have a single smart contract in it.
   274  
   275  * Multiple smart contracts should only be deployed in the same chaincode if they
   276    are very closely related.  Usually, this is only necessary if they share the
   277    same world state.
   278  
   279  * Chaincode namespaces provide isolation between different world states. In
   280    general it makes sense to isolate unrelated data from each other. Note that
   281    you cannot choose the chaincode namespace; it is assigned by Hyperledger
   282    Fabric, and maps directly to the name of the chaincode.
   283  
   284  * For chaincode to chaincode interactions using the `invokeChaincode()` API,
   285    both chaincodes must be installed on the same peer.
   286  
   287      * For interactions that only require the called chaincode's world state to
   288        be queried, the invocation can be in a different channel to the caller's
   289        chaincode.
   290  
   291      * For interactions that require the called chaincode's world state to be
   292        updated, the invocation must be in the same channel as the caller's
   293        chaincode.
   294  
   295  <!--- Licensed under Creative Commons Attribution 4.0 International License
   296  https://creativecommons.org/licenses/by/4.0/ -->