You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

362 lines
9.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. # Using ABCI-CLI
  2. To facilitate testing and debugging of ABCI servers and simple apps, we
  3. built a CLI, the `abci-cli`, for sending ABCI messages from the command
  4. line.
  5. ## Install
  6. Make sure you [have Go installed](https://golang.org/doc/install).
  7. Next, install the `abci-cli` tool and example applications:
  8. ```
  9. mkdir -p $GOPATH/src/github.com/tendermint
  10. cd $GOPATH/src/github.com/tendermint
  11. git clone https://github.com/tendermint/tendermint.git
  12. cd tendermint
  13. make get_tools
  14. make install_abci
  15. ```
  16. Now run `abci-cli` to see the list of commands:
  17. ```
  18. Usage:
  19. abci-cli [command]
  20. Available Commands:
  21. batch Run a batch of abci commands against an application
  22. check_tx Validate a tx
  23. commit Commit the application state and return the Merkle root hash
  24. console Start an interactive abci console for multiple commands
  25. counter ABCI demo example
  26. deliver_tx Deliver a new tx to the application
  27. kvstore ABCI demo example
  28. echo Have the application echo a message
  29. help Help about any command
  30. info Get some info about the application
  31. query Query the application state
  32. set_option Set an options on the application
  33. Flags:
  34. --abci string socket or grpc (default "socket")
  35. --address string address of application socket (default "tcp://127.0.0.1:26658")
  36. -h, --help help for abci-cli
  37. -v, --verbose print the command and results as if it were a console session
  38. Use "abci-cli [command] --help" for more information about a command.
  39. ```
  40. ## KVStore - First Example
  41. The `abci-cli` tool lets us send ABCI messages to our application, to
  42. help build and debug them.
  43. The most important messages are `deliver_tx`, `check_tx`, and `commit`,
  44. but there are others for convenience, configuration, and information
  45. purposes.
  46. We'll start a kvstore application, which was installed at the same time
  47. as `abci-cli` above. The kvstore just stores transactions in a merkle
  48. tree.
  49. Its code can be found
  50. [here](https://github.com/tendermint/tendermint/blob/master/abci/cmd/abci-cli/abci-cli.go)
  51. and looks like:
  52. ```
  53. func cmdKVStore(cmd *cobra.Command, args []string) error {
  54. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  55. // Create the application - in memory or persisted to disk
  56. var app types.Application
  57. if flagPersist == "" {
  58. app = kvstore.NewKVStoreApplication()
  59. } else {
  60. app = kvstore.NewPersistentKVStoreApplication(flagPersist)
  61. app.(*kvstore.PersistentKVStoreApplication).SetLogger(logger.With("module", "kvstore"))
  62. }
  63. // Start the listener
  64. srv, err := server.NewServer(flagAddrD, flagAbci, app)
  65. if err != nil {
  66. return err
  67. }
  68. srv.SetLogger(logger.With("module", "abci-server"))
  69. if err := srv.Start(); err != nil {
  70. return err
  71. }
  72. // Stop upon receiving SIGTERM or CTRL-C.
  73. cmn.TrapSignal(logger, func() {
  74. // Cleanup
  75. srv.Stop()
  76. })
  77. // Run forever.
  78. select {}
  79. }
  80. ```
  81. Start by running:
  82. ```
  83. abci-cli kvstore
  84. ```
  85. And in another terminal, run
  86. ```
  87. abci-cli echo hello
  88. abci-cli info
  89. ```
  90. You'll see something like:
  91. ```
  92. -> data: hello
  93. -> data.hex: 68656C6C6F
  94. ```
  95. and:
  96. ```
  97. -> data: {"size":0}
  98. -> data.hex: 7B2273697A65223A307D
  99. ```
  100. An ABCI application must provide two things:
  101. - a socket server
  102. - a handler for ABCI messages
  103. When we run the `abci-cli` tool we open a new connection to the
  104. application's socket server, send the given ABCI message, and wait for a
  105. response.
  106. The server may be generic for a particular language, and we provide a
  107. [reference implementation in
  108. Golang](https://github.com/tendermint/tendermint/tree/master/abci/server). See the
  109. [list of other ABCI implementations](./ecosystem.md) for servers in
  110. other languages.
  111. The handler is specific to the application, and may be arbitrary, so
  112. long as it is deterministic and conforms to the ABCI interface
  113. specification.
  114. So when we run `abci-cli info`, we open a new connection to the ABCI
  115. server, which calls the `Info()` method on the application, which tells
  116. us the number of transactions in our Merkle tree.
  117. Now, since every command opens a new connection, we provide the
  118. `abci-cli console` and `abci-cli batch` commands, to allow multiple ABCI
  119. messages to be sent over a single connection.
  120. Running `abci-cli console` should drop you in an interactive console for
  121. speaking ABCI messages to your application.
  122. Try running these commands:
  123. ```
  124. > echo hello
  125. -> code: OK
  126. -> data: hello
  127. -> data.hex: 0x68656C6C6F
  128. > info
  129. -> code: OK
  130. -> data: {"size":0}
  131. -> data.hex: 0x7B2273697A65223A307D
  132. > commit
  133. -> code: OK
  134. -> data.hex: 0x0000000000000000
  135. > deliver_tx "abc"
  136. -> code: OK
  137. > info
  138. -> code: OK
  139. -> data: {"size":1}
  140. -> data.hex: 0x7B2273697A65223A317D
  141. > commit
  142. -> code: OK
  143. -> data.hex: 0x0200000000000000
  144. > query "abc"
  145. -> code: OK
  146. -> log: exists
  147. -> height: 0
  148. -> value: abc
  149. -> value.hex: 616263
  150. > deliver_tx "def=xyz"
  151. -> code: OK
  152. > commit
  153. -> code: OK
  154. -> data.hex: 0x0400000000000000
  155. > query "def"
  156. -> code: OK
  157. -> log: exists
  158. -> height: 0
  159. -> value: xyz
  160. -> value.hex: 78797A
  161. ```
  162. Note that if we do `deliver_tx "abc"` it will store `(abc, abc)`, but if
  163. we do `deliver_tx "abc=efg"` it will store `(abc, efg)`.
  164. Similarly, you could put the commands in a file and run
  165. `abci-cli --verbose batch < myfile`.
  166. ## Counter - Another Example
  167. Now that we've got the hang of it, let's try another application, the
  168. "counter" app.
  169. Like the kvstore app, its code can be found
  170. [here](https://github.com/tendermint/tendermint/blob/master/abci/cmd/abci-cli/abci-cli.go)
  171. and looks like:
  172. ```
  173. func cmdCounter(cmd *cobra.Command, args []string) error {
  174. app := counter.NewCounterApplication(flagSerial)
  175. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  176. // Start the listener
  177. srv, err := server.NewServer(flagAddrC, flagAbci, app)
  178. if err != nil {
  179. return err
  180. }
  181. srv.SetLogger(logger.With("module", "abci-server"))
  182. if err := srv.Start(); err != nil {
  183. return err
  184. }
  185. // Stop upon receiving SIGTERM or CTRL-C.
  186. cmn.TrapSignal(logger, func() {
  187. // Cleanup
  188. srv.Stop()
  189. })
  190. // Run forever.
  191. select {}
  192. }
  193. ```
  194. The counter app doesn't use a Merkle tree, it just counts how many times
  195. we've sent a transaction, asked for a hash, or committed the state. The
  196. result of `commit` is just the number of transactions sent.
  197. This application has two modes: `serial=off` and `serial=on`.
  198. When `serial=on`, transactions must be a big-endian encoded incrementing
  199. integer, starting at 0.
  200. If `serial=off`, there are no restrictions on transactions.
  201. We can toggle the value of `serial` using the `set_option` ABCI message.
  202. When `serial=on`, some transactions are invalid. In a live blockchain,
  203. transactions collect in memory before they are committed into blocks. To
  204. avoid wasting resources on invalid transactions, ABCI provides the
  205. `check_tx` message, which application developers can use to accept or
  206. reject transactions, before they are stored in memory or gossipped to
  207. other peers.
  208. In this instance of the counter app, `check_tx` only allows transactions
  209. whose integer is greater than the last committed one.
  210. Let's kill the console and the kvstore application, and start the
  211. counter app:
  212. ```
  213. abci-cli counter
  214. ```
  215. In another window, start the `abci-cli console`:
  216. ```
  217. > set_option serial on
  218. -> code: OK
  219. -> log: OK (SetOption doesn't return anything.)
  220. > check_tx 0x00
  221. -> code: OK
  222. > check_tx 0xff
  223. -> code: OK
  224. > deliver_tx 0x00
  225. -> code: OK
  226. > check_tx 0x00
  227. -> code: BadNonce
  228. -> log: Invalid nonce. Expected >= 1, got 0
  229. > deliver_tx 0x01
  230. -> code: OK
  231. > deliver_tx 0x04
  232. -> code: BadNonce
  233. -> log: Invalid nonce. Expected 2, got 4
  234. > info
  235. -> code: OK
  236. -> data: {"hashes":0,"txs":2}
  237. -> data.hex: 0x7B22686173686573223A302C22747873223A327D
  238. ```
  239. This is a very simple application, but between `counter` and `kvstore`,
  240. its easy to see how you can build out arbitrary application states on
  241. top of the ABCI. [Hyperledger's
  242. Burrow](https://github.com/hyperledger/burrow) also runs atop ABCI,
  243. bringing with it Ethereum-like accounts, the Ethereum virtual-machine,
  244. Monax's permissioning scheme, and native contracts extensions.
  245. But the ultimate flexibility comes from being able to write the
  246. application easily in any language.
  247. We have implemented the counter in a number of languages [see the
  248. example directory](https://github.com/tendermint/tendermint/tree/master/abci/example).
  249. To run the Node.js version, fist download & install [the Javascript ABCI server](https://github.com/tendermint/js-abci):
  250. ```
  251. git clone https://github.com/tendermint/js-abci.git
  252. cd js-abci
  253. npm install abci
  254. ```
  255. Now you can start the app:
  256. ```bash
  257. node example/counter.js
  258. ```
  259. (you'll have to kill the other counter application process). In another
  260. window, run the console and those previous ABCI commands. You should get
  261. the same results as for the Go version.
  262. ## Bounties
  263. Want to write the counter app in your favorite language?! We'd be happy
  264. to add you to our [ecosystem](https://tendermint.com/ecosystem)! We're
  265. also offering [bounties](https://hackerone.com/tendermint/) for
  266. implementations in new languages!
  267. The `abci-cli` is designed strictly for testing and debugging. In a real
  268. deployment, the role of sending messages is taken by Tendermint, which
  269. connects to the app using three separate connections, each with its own
  270. pattern of messages.
  271. For more information, see the [application developers
  272. guide](./app-development.md). For examples of running an ABCI app with
  273. Tendermint, see the [getting started guide](./getting-started.md).
  274. Next is the ABCI specification.