github.com/KYVENetwork/cometbft/v38@v38.0.3/docs/app-dev/getting-started.md (about)

     1  ---
     2  order: 2
     3  ---
     4  
     5  # Getting Started
     6  
     7  ## First CometBFT App
     8  
     9  As a general purpose blockchain engine, CometBFT 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 CometBFT,
    12  the other is your application, which can be written in any programming
    13  language.
    14  
    15  CometBFT handles all the p2p and consensus logic, and just forwards transactions to the
    16  application when they need to be validated, or when they're ready to be
    17  executed and committed.
    18  
    19  In this guide, we show you some examples of how to run an application
    20  using CometBFT.
    21  
    22  ### Install
    23  
    24  The first apps we will work with are written in Go. To install them, you
    25  need to [install Go](https://golang.org/doc/install), put
    26  `$GOPATH/bin` in your `$PATH` and enable go modules. If you use `bash`,
    27  follow these instructions:
    28  
    29  ```bash
    30  echo export GOPATH=\"\$HOME/go\" >> ~/.bash_profile
    31  echo export PATH=\"\$PATH:\$GOPATH/bin\" >> ~/.bash_profile
    32  ```
    33  
    34  Then run
    35  
    36  ```bash
    37  go get github.com/cometbft/cometbft
    38  cd $GOPATH/src/github.com/cometbft/cometbft
    39  make install_abci
    40  ```
    41  
    42  Now you should have the `abci-cli` installed; run `abci-cli` to see the list of commands:
    43  
    44  ```
    45  Usage:
    46    abci-cli [command]
    47  
    48  Available Commands:
    49    batch            run a batch of abci commands against an application
    50    check_tx         validate a transaction
    51    commit           commit the application state and return the Merkle root hash
    52    completion       Generate the autocompletion script for the specified shell
    53    console          start an interactive ABCI console for multiple commands
    54    echo             have the application echo a message
    55    finalize_block   deliver a block of transactions to the application
    56    help             Help about any command
    57    info             get some info about the application
    58    kvstore          ABCI demo example
    59    prepare_proposal prepare proposal
    60    process_proposal process proposal
    61    query            query the application state
    62    test             run integration tests
    63    version          print ABCI console version
    64  
    65  Flags:
    66        --abci string        either socket or grpc (default "socket")
    67        --address string     address of application socket (default "tcp://0.0.0.0:26658")
    68    -h, --help               help for abci-cli
    69        --log_level string   set the logger level (default "debug")
    70    -v, --verbose            print the command and results as if it were a console session
    71  
    72  Use "abci-cli [command] --help" for more information about a command.
    73  ```
    74  
    75  You'll notice the `kvstore` command, an example application written in Go.
    76  
    77  Now, let's run an app!
    78  
    79  ## KVStore - A First Example
    80  
    81  The kvstore app is a [Merkle
    82  tree](https://en.wikipedia.org/wiki/Merkle_tree) that just stores all
    83  transactions. If the transaction contains an `=`, e.g. `key=value`, then
    84  the `value` is stored under the `key` in the Merkle tree. Otherwise, the
    85  full transaction bytes are stored as the key and the value.
    86  
    87  Let's start a kvstore application.
    88  
    89  ```sh
    90  abci-cli kvstore
    91  ```
    92  
    93  In another terminal, we can start CometBFT. You should already have the
    94  CometBFT binary installed. If not, follow the steps from
    95  [here](../guides/install.md). If you have never run CometBFT
    96  before, use:
    97  
    98  ```sh
    99  cometbft init
   100  cometbft node
   101  ```
   102  
   103  If you have used CometBFT, you may want to reset the data for a new
   104  blockchain by running `cometbft unsafe-reset-all`. Then you can run
   105  `cometbft node` to start CometBFT, and connect to the app. For more
   106  details, see [the guide on using CometBFT](../core/using-cometbft.md).
   107  
   108  You should see CometBFT making blocks! We can get the status of our
   109  CometBFT node as follows:
   110  
   111  ```sh
   112  curl -s localhost:26657/status
   113  ```
   114  
   115  The `-s` just silences `curl`. For nicer output, pipe the result into a
   116  tool like [jq](https://stedolan.github.io/jq/) or `json_pp`.
   117  
   118  Now let's send some transactions to the kvstore.
   119  
   120  ```sh
   121  curl -s 'localhost:26657/broadcast_tx_commit?tx="abcd"'
   122  ```
   123  
   124  Note the single quote (`'`) around the url, which ensures that the
   125  double quotes (`"`) are not escaped by bash. This command sent a
   126  transaction with bytes `abcd`, so `abcd` will be stored as both the key
   127  and the value in the Merkle tree. The response should look something
   128  like:
   129  
   130  ```json
   131  {
   132    "jsonrpc": "2.0",
   133    "id": "",
   134    "result": {
   135      "check_tx": {},
   136      "deliver_tx": {
   137        "tags": [
   138          {
   139            "key": "YXBwLmNyZWF0b3I=",
   140            "value": "amFl"
   141          },
   142          {
   143            "key": "YXBwLmtleQ==",
   144            "value": "YWJjZA=="
   145          }
   146        ]
   147      },
   148      "hash": "9DF66553F98DE3C26E3C3317A3E4CED54F714E39",
   149      "height": 14
   150    }
   151  }
   152  ```
   153  
   154  We can confirm that our transaction worked and the value got stored by
   155  querying the app:
   156  
   157  ```sh
   158  curl -s 'localhost:26657/abci_query?data="abcd"'
   159  ```
   160  
   161  The result should look like:
   162  
   163  ```json
   164  {
   165    "jsonrpc": "2.0",
   166    "id": "",
   167    "result": {
   168      "response": {
   169        "log": "exists",
   170        "index": "-1",
   171        "key": "YWJjZA==",
   172        "value": "YWJjZA=="
   173      }
   174    }
   175  }
   176  ```
   177  
   178  Note the `value` in the result (`YWJjZA==`); this is the base64-encoding
   179  of the ASCII of `abcd`. You can verify this in a python 2 shell by
   180  running `"YWJjZA==".decode('base64')` or in python 3 shell by running
   181  `import codecs; codecs.decode(b"YWJjZA==", 'base64').decode('ascii')`.
   182  Stay tuned for a future release that [makes this output more
   183  human-readable](https://github.com/tendermint/tendermint/issues/1794).
   184  
   185  Now let's try setting a different key and value:
   186  
   187  ```sh
   188  curl -s 'localhost:26657/broadcast_tx_commit?tx="name=satoshi"'
   189  ```
   190  
   191  Now if we query for `name`, we should get `satoshi`, or `c2F0b3NoaQ==`
   192  in base64:
   193  
   194  ```sh
   195  curl -s 'localhost:26657/abci_query?data="name"'
   196  ```
   197  
   198  Try some other transactions and queries to make sure everything is
   199  working!