github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/docs/chaincode.md (about)

     1  # What is chaincode?
     2  
     3  [WIP]
     4  
     5  coming soon ... end-to-end examples of chaincode demonstrating the available
     6  APIs.
     7  
     8  Chaincode is a piece of code that is written in one of the supported languages
     9  such as Go or Java.  It is installed and instantiated through an SDK or CLI
    10  onto a network of Hyperledger Fabric peer nodes, enabling interaction with that
    11  network's shared ledger.
    12  
    13  There are three aspects to chaincode development:
    14  * The interfaces that the chaincode should implement
    15  * APIs the chaincode can use to interact with the Fabric
    16  * A chaincode response
    17  
    18  ## Chaincode interfaces
    19  
    20  A chaincode implements the Chaincode Interface that supports two methods:
    21  * `Init`
    22  * `Invoke`
    23  
    24  #### Init()
    25  
    26  Init is called when you first deploy your chaincode. As the name implies, this
    27  function is used to do any initialization your chaincode needs.
    28  
    29  #### Invoke()
    30  
    31  Invoke is called when you want to call chaincode functions to do real work (i.e. read
    32  and write to the ledger). Invocations are captured as transactions, which get
    33  grouped into blocks on the chain. When you need to update or query the ledger,
    34  you do so by invoking your chaincode.
    35  
    36  ## Dependencies
    37  
    38  The import statement lists a few dependencies for the chaincode to compile successfully.
    39  * fmt – contains Println for debugging/logging.
    40  * errors – standard go error format.
    41  * [shim](https://github.com/hyperledger/fabric/tree/master/core/chaincode/shim) –
    42  contains the definitions for the chaincode interface and the chaincode stub,
    43  which you are required to interact with the ledger.
    44  
    45  ## Chaincode APIs
    46  
    47  When the Init or Invoke function of a chaincode is called, the fabric passes the
    48  `stub shim.ChaincodeStubInterface` parameter and the chaincode returns a `pb.Response`.
    49  This stub can be used to call APIs to access to the ledger services, transaction
    50  context, or to invoke other chaincodes.
    51  
    52  The current APIs are defined in the shim package, and can be generated with the
    53  following command:
    54  ```bash
    55  godoc github.com/hyperledger/fabric/core/chaincode/shim
    56  ```
    57  However, it also includes functions from chaincode.pb.go (protobuffer functions)
    58  that are not intended as public APIs. The best practice is to look at the function
    59  definitions in chaincode.go and and the
    60  [examples](https://github.com/hyperledger/fabric/tree/master/examples/chaincode/go)
    61  directory.
    62  
    63  ## Response
    64  
    65  The chaincode response comes in the form of a protobuffer.
    66  
    67  ```go
    68  message Response {  
    69  
    70      // A status code that should follow the HTTP status codes.
    71      int32 status = 1;
    72  
    73      // A message associated with the response code.
    74      string message = 2;
    75  
    76      // A payload that can be used to include metadata with this response.
    77      bytes payload = 3;
    78  
    79  }
    80  ```
    81  The chaincode will also return events.  Message events and chaincode events.
    82  ```go
    83  messageEvent {  
    84  
    85      oneof Event {
    86  
    87      //Register consumer sent event  
    88      Register register = 1;
    89  
    90      //producer events common.  
    91      Block block = 2;  
    92      ChaincodeEvent chaincodeEvent = 3;  
    93      Rejection rejection = 4;
    94  
    95      //Unregister consumer sent events  
    96      Unregister unregister = 5;  
    97  
    98      }  
    99  
   100  }
   101  ```
   102  
   103  ```go
   104  messageChaincodeEvent {
   105  
   106      string chaincodeID = 1;  
   107      string txID = 2;  
   108      string eventName = 3;  
   109      bytes payload = 4;
   110  
   111  }
   112  ```
   113  
   114  Once developed and deployed, there are two ways to interact with the chaincode -
   115  through an SDK or the CLI. The steps for CLI are described below. For SDK interaction,
   116  refer to the [balance transfer](https://github.com/hyperledger/fabric-sdk-node/tree/master/examples/balance-transfer)
   117  samples.  __Note__: This SDK interaction is covered in the __Getting Started__ section.
   118  
   119  ## Command Line Interfaces
   120  
   121  To view the currently available CLI commands, execute the following:
   122  ```bash
   123  # this assumes that you have correctly set the GOPATH variable and cloned the Fabric codebase into that path
   124  cd /opt/gopath/src/github.com/hyperledger/fabric
   125  build /bin/peer
   126  ```
   127  You will see output similar to the example below. (__NOTE__: rootcommand below is
   128  hardcoded in main.go. Currently, the build will create a _peer_ executable file).
   129  ```bash
   130  Usage:
   131        peer [flags]
   132        peer [command]
   133  
   134      Available Commands:
   135        version     Print fabric peer version.
   136        node        node specific commands.
   137        channel     channel specific commands.
   138        chaincode   chaincode specific commands.
   139        logging     logging specific commands
   140  
   141  
   142      Flags:
   143        --logging-level string: Default logging level and overrides, see core.yaml for full syntax
   144        --test.coverprofile string: Done (default “coverage.cov)
   145        -v, --version: Display current version of fabric peer server
   146      Use "peer [command] --help" for more information about a command.
   147  ```
   148  The `peer` command supports several subcommands and flags, as shown above. To
   149  facilitate its use in scripted applications, the `peer` command always produces a
   150  non-zero return code in the event of command failure. Upon success, many of the
   151  subcommands produce a result on stdout as shown in the table below:
   152  
   153  <table width="665" cellpadding="8" cellspacing="0"><colgroup><col width="262"> <col width="371"></colgroup>
   154  
   155  <thead>
   156  
   157  <tr>
   158  
   159  <th width="262" bgcolor="#ffffff" style="border-top: none; border-bottom: 1.50pt solid #e1e4e5; border-left: none; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0in; padding-right: 0in">
   160  
   161  <font size="2" style="font-size: 9pt">Command</font>
   162  
   163  </th>
   164  
   165  <th width="371" bgcolor="#ffffff" style="border-top: none; border-bottom: 1.50pt solid #e1e4e5; border-left: none; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0in; padding-right: 0in">
   166  
   167  <font size="2" style="font-size: 9pt">stdout</font><font size="2" style="font-size: 9pt"> result in the event of success</font>
   168  
   169  </th>
   170  
   171  </tr>
   172  
   173  </thead>
   174  
   175  <tbody>
   176  
   177  <tr>
   178  
   179  <td width="262" bgcolor="#f3f6f6" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   180  
   181  <span style="display: inline-block; border: 1px solid #e1e4e5; padding: 0.01in"><font color="#e74c3c"><font face="Consolas, Andale Mono WT, Andale Mono, Lucida Console, Lucida Sans Typewriter, DejaVu Sans Mono, Bitstream Vera Sans Mono, Liberation Mono, Nimbus Mono L, Monaco, Courier New, Courier, monospace"><font size="1" style="font-size: 7pt">version</font></font></font></span>
   182  
   183  </td>
   184  
   185  <td width="371" bgcolor="#f3f6f6" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   186  
   187  <font size="2" style="font-size: 9pt">String form of </font><span style="display: inline-block; border: 1px solid #e1e4e5; padding: 0.01in"><font color="#e74c3c"><font face="Consolas, Andale Mono WT, Andale Mono, Lucida Console, Lucida Sans Typewriter, DejaVu Sans Mono, Bitstream Vera Sans Mono, Liberation Mono, Nimbus Mono L, Monaco, Courier New, Courier, monospace"><font size="1" style="font-size: 7pt">peer.version</font></font></font></span><font size="2" style="font-size: 9pt"> defined in core.yaml</font>
   188  
   189  </td>
   190  
   191  </tr>
   192  
   193  <tr>
   194  
   195  <td width="262" bgcolor="#ffffff" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   196  
   197  <span style="display: inline-block; border: 1px solid #e1e4e5; padding: 0.01in"><font color="#e74c3c"><font face="Consolas, Andale Mono WT, Andale Mono, Lucida Console, Lucida Sans Typewriter, DejaVu Sans Mono, Bitstream Vera Sans Mono, Liberation Mono, Nimbus Mono L, Monaco, Courier New, Courier, monospace"><font size="1" style="font-size: 7pt">node start</font></font></font></span>
   198  
   199  </td>
   200  
   201  <td width="371" bgcolor="#ffffff" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   202  
   203  <font size="2" style="font-size: 9pt">N/A</font>
   204  
   205  </td>
   206  
   207  </tr>
   208  
   209  <tr>
   210  
   211  <td width="262" bgcolor="#f3f6f6" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   212  
   213  <span style="display: inline-block; border: 1px solid #e1e4e5; padding: 0.01in"><font color="#e74c3c"><font face="Consolas, Andale Mono WT, Andale Mono, Lucida Console, Lucida Sans Typewriter, DejaVu Sans Mono, Bitstream Vera Sans Mono, Liberation Mono, Nimbus Mono L, Monaco, Courier New, Courier, monospace"><font size="1" style="font-size: 7pt">node status</font></font></font></span>
   214  
   215  </td>
   216  
   217  <td width="371" bgcolor="#f3f6f6" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   218  
   219  <font size="2" style="font-size: 9pt">String form of StatusCode</font>
   220  
   221  </td>
   222  
   223  </tr>
   224  
   225  <tr>
   226  
   227  <td width="262" bgcolor="#ffffff" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   228  
   229  <span style="display: inline-block; border: 1px solid #e1e4e5; padding: 0.01in"><font color="#e74c3c"><font face="Consolas, Andale Mono WT, Andale Mono, Lucida Console, Lucida Sans Typewriter, DejaVu Sans Mono, Bitstream Vera Sans Mono, Liberation Mono, Nimbus Mono L, Monaco, Courier New, Courier, monospace"><font size="1" style="font-size: 7pt">node stop</font></font></font></span>
   230  
   231  </td>
   232  
   233  <td width="371" bgcolor="#ffffff" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   234  
   235  <font size="2" style="font-size: 9pt">String form of StatusCode</font>
   236  
   237  </td>
   238  
   239  </tr>
   240  
   241  <tr>
   242  
   243  <td width="262" bgcolor="#f3f6f6" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   244  
   245  <span style="display: inline-block; border: 1px solid #e1e4e5; padding: 0.01in"><font color="#e74c3c"><font face="Consolas, Andale Mono WT, Andale Mono, Lucida Console, Lucida Sans Typewriter, DejaVu Sans Mono, Bitstream Vera Sans Mono, Liberation Mono, Nimbus Mono L, Monaco, Courier New, Courier, monospace"><font size="1" style="font-size: 7pt">chaincode deploy</font></font></font></span>
   246  
   247  </td>
   248  
   249  <td width="371" bgcolor="#f3f6f6" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   250  
   251  <font size="2" style="font-size: 9pt">The chaincode container name (hash) required for subsequent</font><span style="display: inline-block; border: 1px solid #e1e4e5; padding: 0.01in"><font color="#e74c3c"><font face="Consolas, Andale Mono WT, Andale Mono, Lucida Console, Lucida Sans Typewriter, DejaVu Sans Mono, Bitstream Vera Sans Mono, Liberation Mono, Nimbus Mono L, Monaco, Courier New, Courier, monospace"><font size="1" style="font-size: 7pt"> chaincode invoke</font></font></font></span><font size="2" style="font-size: 9pt"> and </font><span style="display: inline-block; border: 1px solid #e1e4e5; padding: 0.01in"><font color="#e74c3c"><font face="Consolas, Andale Mono WT, Andale Mono, Lucida Console, Lucida Sans Typewriter, DejaVu Sans Mono, Bitstream Vera Sans Mono, Liberation Mono, Nimbus Mono L, Monaco, Courier New, Courier, monospace"><font size="1" style="font-size: 7pt">chaincode query</font></font></font></span><font size="2" style="font-size: 9pt"> commands</font>
   252  
   253  </td>
   254  
   255  </tr>
   256  
   257  <tr>
   258  
   259  <td width="262" bgcolor="#ffffff" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   260  
   261  <span style="display: inline-block; border: 1px solid #e1e4e5; padding: 0.01in"><font color="#e74c3c"><font face="Consolas, Andale Mono WT, Andale Mono, Lucida Console, Lucida Sans Typewriter, DejaVu Sans Mono, Bitstream Vera Sans Mono, Liberation Mono, Nimbus Mono L, Monaco, Courier New, Courier, monospace"><font size="1" style="font-size: 7pt">chaincode invoke</font></font></font></span>
   262  
   263  </td>
   264  
   265  <td width="371" bgcolor="#ffffff" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   266  
   267  <font size="2" style="font-size: 9pt">The transaction ID (UUID)</font>
   268  
   269  </td>
   270  
   271  </tr>
   272  
   273  <tr>
   274  
   275  <td width="262" bgcolor="#f3f6f6" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   276  
   277  <span style="display: inline-block; border: 1px solid #e1e4e5; padding: 0.01in"><font color="#e74c3c"><font face="Consolas, Andale Mono WT, Andale Mono, Lucida Console, Lucida Sans Typewriter, DejaVu Sans Mono, Bitstream Vera Sans Mono, Liberation Mono, Nimbus Mono L, Monaco, Courier New, Courier, monospace"><font size="1" style="font-size: 7pt">chaincode query</font></font></font></span>
   278  
   279  </td>
   280  
   281  <td width="371" bgcolor="#f3f6f6" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   282  
   283  <font size="2" style="font-size: 9pt">By default, the query result is formatted as a printable</font>
   284  
   285  </td>
   286  
   287  </tr>
   288  
   289  <tr>
   290  
   291  <td width="262" bgcolor="#f3f6f6" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   292  
   293  <span style="display: inline-block; border: 1px solid #e1e4e5; padding: 0.01in"><font color="#e74c3c"><font face="Consolas, Andale Mono WT, Andale Mono, Lucida Console, Lucida Sans Typewriter, DejaVu Sans Mono, Bitstream Vera Sans Mono, Liberation Mono, Nimbus Mono L, Monaco, Courier New, Courier, monospace"><font size="1" style="font-size: 7pt"><span lang="en-US">channel create</span></font></font></font></span>
   294  
   295  </td>
   296  
   297  <td width="371" bgcolor="#f3f6f6" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   298  
   299  <font color="#00000a"><font size="2" style="font-size: 9pt"><span lang="en-US">Create a chain</span></font></font>
   300  
   301  </td>
   302  
   303  </tr>
   304  
   305  <tr>
   306  
   307  <td width="262" bgcolor="#f3f6f6" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   308  
   309  <span style="display: inline-block; border: 1px solid #e1e4e5; padding: 0.01in"><font color="#e74c3c"><font face="Consolas, Andale Mono WT, Andale Mono, Lucida Console, Lucida Sans Typewriter, DejaVu Sans Mono, Bitstream Vera Sans Mono, Liberation Mono, Nimbus Mono L, Monaco, Courier New, Courier, monospace"><font size="1" style="font-size: 7pt"><span lang="en-US">channel join</span></font></font></font></span>
   310  
   311  </td>
   312  
   313  <td width="371" bgcolor="#f3f6f6" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   314  
   315  <font color="#00000a"><font size="2" style="font-size: 9pt"><span lang="en-US">Adds a peer to the chain</span></font></font>
   316  
   317  </td>
   318  
   319  </tr>
   320  
   321  <tr>
   322  
   323  <td width="262" bgcolor="#f3f6f6" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   324  
   325  <pre class="western" style="orphans: 2; widows: 2"><span style="display: inline-block; border: 1px solid #e1e4e5; padding: 0.01in"><span style="font-variant: normal"><font color="#e74c3c"><font face="Consolas, Andale Mono WT, Andale Mono, Lucida Console, Lucida Sans Typewriter, DejaVu Sans Mono, Bitstream Vera Sans Mono, Liberation Mono, Nimbus Mono L, Monaco, Courier New, Courier, monospace"><font size="1" style="font-size: 7pt"><span style="letter-spacing: normal"><span lang="en-US"><span style="font-style: normal"><span style="font-weight: normal">--peer-defaultchain=true</span></span></span></span></font></font></font></span></span></pre>
   326  
   327  </td>
   328  
   329  <td width="371" bgcolor="#f3f6f6" style="border-top: 1px solid #e1e4e5; border-bottom: 1px solid #e1e4e5; border-left: 1px solid #e1e4e5; border-right: none; padding-top: 0in; padding-bottom: 0.08in; padding-left: 0.16in; padding-right: 0in">
   330  
   331  <font color="#00000a"><font size="2" style="font-size: 9pt"><span lang="en-US"> Allows users to continue to work with the default TEST_CHAINID string. Command line options support writing this value as raw bytes (-r, –raw) or formatted as the hexadecimal representation of the raw bytes (-x, –hex). If the query response is empty then nothing is output.</span></font></font>
   332  
   333  </td>
   334  
   335  
   336  </tbody>
   337  
   338  </table>
   339  
   340  ## Deploy a chaincode
   341  
   342  [WIP] - the CLI commands need to be refactored based on the new deployment model.
   343  Channel Create and Channel Join will remain the same.