github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/docs/app-dev/getting-started.md (about)

     1  ---
     2  order: 1
     3  ---
     4  
     5  # Getting Started
     6  
     7  ## First Tendermint App
     8  
     9  As a general purpose blockchain engine, Tendermint is agnostic to the
    10  application you want to run. So, to run a complete blockchain that does
    11  something useful, you must start two programs: one is Tendermint Core,
    12  the other is your application, which can be written in any programming
    13  language. Recall from [the intro to
    14  ABCI](../introduction/what-is-tendermint.md#abci-overview) that Tendermint Core handles all the p2p and consensus stuff, and just forwards transactions to the
    15  application when they need to be validated, or when they're ready to be
    16  committed to a block.
    17  
    18  In this guide, we show you some examples of how to run an application
    19  using Tendermint.
    20  
    21  ### Install
    22  
    23  The first apps we will work with are written in Go. To install them, you
    24  need to [install Go](https://golang.org/doc/install), put
    25  `$GOPATH/bin` in your `$PATH` and enable go modules with these instructions:
    26  
    27  ```bash
    28  echo export GOPATH=\"\$HOME/go\" >> ~/.bash_profile
    29  echo export PATH=\"\$PATH:\$GOPATH/bin\" >> ~/.bash_profile
    30  echo export GO111MODULE=on >> ~/.bash_profile
    31  ```
    32  
    33  Then run
    34  
    35  ```
    36  go get github.com/tendermint/tendermint
    37  cd $GOPATH/src/github.com/tendermint/tendermint
    38  make tools
    39  make install_abci
    40  ```
    41  
    42  Now you should have the `abci-cli` installed; you'll see a couple of
    43  commands (`counter` and `kvstore`) that are example applications written
    44  in Go. See below for an application written in JavaScript.
    45  
    46  Now, let's run some apps!
    47  
    48  ## KVStore - A First Example
    49  
    50  The kvstore app is a [Merkle
    51  tree](https://en.wikipedia.org/wiki/Merkle_tree) that just stores all
    52  transactions. If the transaction contains an `=`, e.g. `key=value`, then
    53  the `value` is stored under the `key` in the Merkle tree. Otherwise, the
    54  full transaction bytes are stored as the key and the value.
    55  
    56  Let's start a kvstore application.
    57  
    58  ```
    59  abci-cli kvstore
    60  ```
    61  
    62  In another terminal, we can start Tendermint. You should already have the
    63  Tendermint binary installed. If not, follow the steps from
    64  [here](../introduction/install.md). If you have never run Tendermint
    65  before, use:
    66  
    67  ```
    68  tendermint init
    69  tendermint node
    70  ```
    71  
    72  If you have used Tendermint, you may want to reset the data for a new
    73  blockchain by running `tendermint unsafe_reset_all`. Then you can run
    74  `tendermint node` to start Tendermint, and connect to the app. For more
    75  details, see [the guide on using Tendermint](../tendermint-core/using-tendermint.md).
    76  
    77  You should see Tendermint making blocks! We can get the status of our
    78  Tendermint node as follows:
    79  
    80  ```
    81  curl -s localhost:26657/status
    82  ```
    83  
    84  The `-s` just silences `curl`. For nicer output, pipe the result into a
    85  tool like [jq](https://stedolan.github.io/jq/) or `json_pp`.
    86  
    87  Now let's send some transactions to the kvstore.
    88  
    89  ```
    90  curl -s 'localhost:26657/broadcast_tx_commit?tx="abcd"'
    91  ```
    92  
    93  Note the single quote (`'`) around the url, which ensures that the
    94  double quotes (`"`) are not escaped by bash. This command sent a
    95  transaction with bytes `abcd`, so `abcd` will be stored as both the key
    96  and the value in the Merkle tree. The response should look something
    97  like:
    98  
    99  ```
   100  {
   101    "jsonrpc": "2.0",
   102    "id": "",
   103    "result": {
   104      "check_tx": {},
   105      "deliver_tx": {
   106        "tags": [
   107          {
   108            "key": "YXBwLmNyZWF0b3I=",
   109            "value": "amFl"
   110          },
   111          {
   112            "key": "YXBwLmtleQ==",
   113            "value": "YWJjZA=="
   114          }
   115        ]
   116      },
   117      "hash": "9DF66553F98DE3C26E3C3317A3E4CED54F714E39",
   118      "height": 14
   119    }
   120  }
   121  ```
   122  
   123  We can confirm that our transaction worked and the value got stored by
   124  querying the app:
   125  
   126  ```
   127  curl -s 'localhost:26657/abci_query?data="abcd"'
   128  ```
   129  
   130  The result should look like:
   131  
   132  ```
   133  {
   134    "jsonrpc": "2.0",
   135    "id": "",
   136    "result": {
   137      "response": {
   138        "log": "exists",
   139        "index": "-1",
   140        "key": "YWJjZA==",
   141        "value": "YWJjZA=="
   142      }
   143    }
   144  }
   145  ```
   146  
   147  Note the `value` in the result (`YWJjZA==`); this is the base64-encoding
   148  of the ASCII of `abcd`. You can verify this in a python 2 shell by
   149  running `"YWJjZA==".decode('base64')` or in python 3 shell by running
   150  `import codecs; codecs.decode(b"YWJjZA==", 'base64').decode('ascii')`.
   151  Stay tuned for a future release that [makes this output more
   152  human-readable](https://github.com/tendermint/tendermint/issues/1794).
   153  
   154  Now let's try setting a different key and value:
   155  
   156  ```
   157  curl -s 'localhost:26657/broadcast_tx_commit?tx="name=satoshi"'
   158  ```
   159  
   160  Now if we query for `name`, we should get `satoshi`, or `c2F0b3NoaQ==`
   161  in base64:
   162  
   163  ```
   164  curl -s 'localhost:26657/abci_query?data="name"'
   165  ```
   166  
   167  Try some other transactions and queries to make sure everything is
   168  working!
   169  
   170  ## Counter - Another Example
   171  
   172  Now that we've got the hang of it, let's try another application, the
   173  `counter` app.
   174  
   175  The counter app doesn't use a Merkle tree, it just counts how many times
   176  we've sent a transaction, or committed the state.
   177  
   178  This application has two modes: `serial=off` and `serial=on`.
   179  
   180  When `serial=on`, transactions must be a big-endian encoded incrementing
   181  integer, starting at 0.
   182  
   183  If `serial=off`, there are no restrictions on transactions.
   184  
   185  In a live blockchain, transactions collect in memory before they are
   186  committed into blocks. To avoid wasting resources on invalid
   187  transactions, ABCI provides the `CheckTx` message, which application
   188  developers can use to accept or reject transactions, before they are
   189  stored in memory or gossipped to other peers.
   190  
   191  In this instance of the counter app, with `serial=on`, `CheckTx` only
   192  allows transactions whose integer is greater than the last committed
   193  one.
   194  
   195  Let's kill the previous instance of `tendermint` and the `kvstore`
   196  application, and start the counter app. We can enable `serial=on` with a
   197  flag:
   198  
   199  ```
   200  abci-cli counter --serial
   201  ```
   202  
   203  In another window, reset then start Tendermint:
   204  
   205  ```
   206  tendermint unsafe_reset_all
   207  tendermint node
   208  ```
   209  
   210  Once again, you can see the blocks streaming by. Let's send some
   211  transactions. Since we have set `serial=on`, the first transaction must
   212  be the number `0`:
   213  
   214  ```
   215  curl localhost:26657/broadcast_tx_commit?tx=0x00
   216  ```
   217  
   218  Note the empty (hence successful) response. The next transaction must be
   219  the number `1`. If instead, we try to send a `5`, we get an error:
   220  
   221  ```
   222  > curl localhost:26657/broadcast_tx_commit?tx=0x05
   223  {
   224    "jsonrpc": "2.0",
   225    "id": "",
   226    "result": {
   227      "check_tx": {},
   228      "deliver_tx": {
   229        "code": 2,
   230        "log": "Invalid nonce. Expected 1, got 5"
   231      },
   232      "hash": "33B93DFF98749B0D6996A70F64071347060DC19C",
   233      "height": 34
   234    }
   235  }
   236  ```
   237  
   238  But if we send a `1`, it works again:
   239  
   240  ```
   241  > curl localhost:26657/broadcast_tx_commit?tx=0x01
   242  {
   243    "jsonrpc": "2.0",
   244    "id": "",
   245    "result": {
   246      "check_tx": {},
   247      "deliver_tx": {},
   248      "hash": "F17854A977F6FA7EEA1BD758E296710B86F72F3D",
   249      "height": 60
   250    }
   251  }
   252  ```
   253  
   254  For more details on the `broadcast_tx` API, see [the guide on using
   255  Tendermint](../tendermint-core/using-tendermint.md).
   256  
   257  ## CounterJS - Example in Another Language
   258  
   259  We also want to run applications in another language - in this case,
   260  we'll run a Javascript version of the `counter`. To run it, you'll need
   261  to [install node](https://nodejs.org/en/download/).
   262  
   263  You'll also need to fetch the relevant repository, from
   264  [here](https://github.com/tendermint/js-abci), then install it:
   265  
   266  ```
   267  git clone https://github.com/tendermint/js-abci.git
   268  cd js-abci
   269  npm install abci
   270  ```
   271  
   272  Kill the previous `counter` and `tendermint` processes. Now run the app:
   273  
   274  ```
   275  node example/counter.js
   276  ```
   277  
   278  In another window, reset and start `tendermint`:
   279  
   280  ```
   281  tendermint unsafe_reset_all
   282  tendermint node
   283  ```
   284  
   285  Once again, you should see blocks streaming by - but now, our
   286  application is written in Javascript! Try sending some transactions, and
   287  like before - the results should be the same:
   288  
   289  ```
   290  # ok
   291  curl localhost:26657/broadcast_tx_commit?tx=0x00
   292  # invalid nonce
   293  curl localhost:26657/broadcast_tx_commit?tx=0x05
   294  # ok
   295  curl localhost:26657/broadcast_tx_commit?tx=0x01
   296  ```
   297  
   298  Neat, eh?