github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/docs/app-dev/getting-started.md (about) 1 --- 2 order: 1 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. Recall from [the intro to 14 ABCI](../introduction/what-is-cometbft.md#abci-overview) that CometBFT 15 handles all the p2p and consensus stuff, 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 deliver_tx deliver a new transaction to the application 55 echo have the application echo a message 56 help Help about any command 57 info get some info about the application 58 kvstore ABCI demo example 59 query query the application state 60 test run integration tests 61 version print ABCI console version 62 63 Flags: 64 --abci string either socket or grpc (default "socket") 65 --address string address of application socket (default "tcp://0.0.0.0:26658") 66 -h, --help help for abci-cli 67 --log_level string set the logger level (default "debug") 68 -v, --verbose print the command and results as if it were a console session 69 70 Use "abci-cli [command] --help" for more information about a command. 71 ``` 72 73 You'll notice the `kvstore` command, an example application written in Go. 74 75 Now, let's run an app! 76 77 ## KVStore - A First Example 78 79 The kvstore app is a [Merkle 80 tree](https://en.wikipedia.org/wiki/Merkle_tree) that just stores all 81 transactions. If the transaction contains an `=`, e.g. `key=value`, then 82 the `value` is stored under the `key` in the Merkle tree. Otherwise, the 83 full transaction bytes are stored as the key and the value. 84 85 Let's start a kvstore application. 86 87 ```sh 88 abci-cli kvstore 89 ``` 90 91 In another terminal, we can start CometBFT. You should already have the 92 CometBFT binary installed. If not, follow the steps from 93 [here](../guides/install.md). If you have never run CometBFT 94 before, use: 95 96 ```sh 97 cometbft init 98 cometbft node 99 ``` 100 101 If you have used CometBFT, you may want to reset the data for a new 102 blockchain by running `cometbft unsafe-reset-all`. Then you can run 103 `cometbft node` to start CometBFT, and connect to the app. For more 104 details, see [the guide on using CometBFT](../core/using-cometbft.md). 105 106 You should see CometBFT making blocks! We can get the status of our 107 CometBFT node as follows: 108 109 ```sh 110 curl -s localhost:26657/status 111 ``` 112 113 The `-s` just silences `curl`. For nicer output, pipe the result into a 114 tool like [jq](https://stedolan.github.io/jq/) or `json_pp`. 115 116 Now let's send some transactions to the kvstore. 117 118 ```sh 119 curl -s 'localhost:26657/broadcast_tx_commit?tx="abcd"' 120 ``` 121 122 Note the single quote (`'`) around the url, which ensures that the 123 double quotes (`"`) are not escaped by bash. This command sent a 124 transaction with bytes `abcd`, so `abcd` will be stored as both the key 125 and the value in the Merkle tree. The response should look something 126 like: 127 128 ```json 129 { 130 "jsonrpc": "2.0", 131 "id": "", 132 "result": { 133 "check_tx": {}, 134 "deliver_tx": { 135 "tags": [ 136 { 137 "key": "YXBwLmNyZWF0b3I=", 138 "value": "amFl" 139 }, 140 { 141 "key": "YXBwLmtleQ==", 142 "value": "YWJjZA==" 143 } 144 ] 145 }, 146 "hash": "9DF66553F98DE3C26E3C3317A3E4CED54F714E39", 147 "height": 14 148 } 149 } 150 ``` 151 152 We can confirm that our transaction worked and the value got stored by 153 querying the app: 154 155 ```sh 156 curl -s 'localhost:26657/abci_query?data="abcd"' 157 ``` 158 159 The result should look like: 160 161 ```json 162 { 163 "jsonrpc": "2.0", 164 "id": "", 165 "result": { 166 "response": { 167 "log": "exists", 168 "index": "-1", 169 "key": "YWJjZA==", 170 "value": "YWJjZA==" 171 } 172 } 173 } 174 ``` 175 176 Note the `value` in the result (`YWJjZA==`); this is the base64-encoding 177 of the ASCII of `abcd`. You can verify this in a python 2 shell by 178 running `"YWJjZA==".decode('base64')` or in python 3 shell by running 179 `import codecs; codecs.decode(b"YWJjZA==", 'base64').decode('ascii')`. 180 Stay tuned for a future release that [makes this output more 181 human-readable](https://github.com/cometbft/cometbft/issues/1794). 182 183 Now let's try setting a different key and value: 184 185 ```sh 186 curl -s 'localhost:26657/broadcast_tx_commit?tx="name=satoshi"' 187 ``` 188 189 Now if we query for `name`, we should get `satoshi`, or `c2F0b3NoaQ==` 190 in base64: 191 192 ```sh 193 curl -s 'localhost:26657/abci_query?data="name"' 194 ``` 195 196 Try some other transactions and queries to make sure everything is 197 working!