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.

220 lines
6.8 KiB

  1. # Using the abci-cli
  2. To facilitate testing and debugging of ABCI servers and simple apps,
  3. we built a CLI, the `abci-cli`, for sending ABCI messages from the command line.
  4. ## Install
  5. Make sure you [have Go installed](https://golang.org/doc/install) and [put `$GOPATH/bin` in your `$PATH`](https://github.com/tendermint/tendermint/wiki/Setting-GOPATH).
  6. Next, install the `abci-cli` tool and example applications:
  7. ```
  8. go get -u github.com/tendermint/abci/cmd/...
  9. ```
  10. If this fails, you may need to use `glide` to get vendored dependencies:
  11. ```
  12. go get github.com/Masterminds/glide
  13. cd $GOPATH/src/github.com/tendermint/abci
  14. glide install
  15. go install ./cmd/...
  16. ```
  17. Now run `abci-cli --help` to see the list of commands:
  18. ```
  19. COMMANDS:
  20. batch Run a batch of ABCI commands against an application
  21. console Start an interactive console for multiple commands
  22. echo Have the application echo a message
  23. info Get some info about the application
  24. set_option Set an option on the application
  25. deliver_tx Append a new tx to application
  26. check_tx Validate a tx
  27. commit Get application Merkle root hash
  28. help, h Shows a list of commands or help for one command
  29. GLOBAL OPTIONS:
  30. --address "tcp://127.0.0.1:46658" address of application socket
  31. --help, -h show help
  32. --version, -v print the version
  33. ```
  34. ## First Example - Dummy
  35. The `abci-cli` tool lets us send ABCI messages to our application, to help build and debug them.
  36. The most important messages are `deliver_tx`, `check_tx`, and `commit`,
  37. but there are others for convenience, configuration, and information purposes.
  38. Let's start a dummy application. The dummy just stores transactions in a merkle tree:
  39. ```
  40. dummy
  41. ```
  42. In another terminal, run
  43. ```
  44. abci-cli echo hello
  45. abci-cli info
  46. ```
  47. The application should echo `hello` and give you some information about itself.
  48. An ABCI application must provide two things:
  49. - a socket server
  50. - a handler for ABCI messages
  51. When we run the `abci-cli` tool we open a new connection to the application's socket server,
  52. send the given ABCI message, and wait for a response.
  53. The server may be generic for a particular language, and we provide a [reference implementation
  54. in Golang](https://github.com/tendermint/abci/tree/master/server).
  55. See the [list of other ABCI implementations](https://tendermint.com/ecosystem)
  56. for servers in other languages.
  57. The handler is specific to the application, and may be arbitrary,
  58. so long as it is deterministic and conforms to the ABCI interface specification.
  59. So when we run `abci-cli info`, we open a new connection to the ABCI server, which calls the `Info()` method on the application, which tells us the number of transactions in our Merkle tree.
  60. Now, since every command opens a new connection, we provide the `abci-cli console` and `abci-cli batch` commands,
  61. to allow multiple ABCI messages to be sent over a single connection.
  62. Running `abci-cli console` should drop you in an interactive console for speaking ABCI messages to your application.
  63. Try running these commands:
  64. ```
  65. > echo hello
  66. -> data: hello
  67. > info
  68. -> data: {"size":0}
  69. > commit
  70. -> data: 0x
  71. > deliver_tx "abc"
  72. -> code: OK
  73. > info
  74. -> data: {"size":1}
  75. > commit
  76. -> data: 0x750502FC7E84BBD788ED589624F06CFA871845D1
  77. > query "abc"
  78. -> code: OK
  79. -> data: {"index":0,"value":"abc","exists":true}
  80. > deliver_tx "def=xyz"
  81. -> code: OK
  82. > commit
  83. -> data: 0x76393B8A182E450286B0694C629ECB51B286EFD5
  84. > query "def"
  85. -> code: OK
  86. -> data: {"index":1,"value":"xyz","exists":true}
  87. ```
  88. Note that if we do `deliver_tx "abc"` it will store `(abc, abc)`,
  89. but if we do `deliver_tx "abc=efg"` it will store `(abc, efg)`.
  90. Similarly, you could put the commands in a file and run `abci-cli --verbose batch < myfile`.
  91. ## Another Example - Counter
  92. Now that we've got the hang of it, let's try another application, the "counter" app.
  93. The counter app doesn't use a Merkle tree, it just counts how many times we've sent a transaction,
  94. asked for a hash, or committed the state. The result of `commit` is just the number of transactions sent.
  95. This application has two modes: `serial=off` and `serial=on`.
  96. When `serial=on`, transactions must be a big-endian encoded incrementing integer, starting at 0.
  97. If `serial=off`, there are no restrictions on transactions.
  98. We can toggle the value of `serial` using the `set_option` ABCI message.
  99. When `serial=on`, some transactions are invalid.
  100. In a live blockchain, transactions collect in memory before they are committed into blocks.
  101. To avoid wasting resources on invalid transactions,
  102. ABCI provides the `check_tx` message,
  103. which application developers can use to accept or reject transactions,
  104. before they are stored in memory or gossipped to other peers.
  105. In this instance of the counter app, `check_tx` only allows transactions whose integer is greater than the last committed one.
  106. Let's kill the console and the dummy application, and start the counter app:
  107. ```
  108. counter
  109. ```
  110. In another window, start the `abci-cli console`:
  111. ```
  112. > set_option serial on
  113. -> data: serial=on
  114. > check_tx 0x00
  115. -> code: OK
  116. > check_tx 0xff
  117. -> code: OK
  118. > deliver_tx 0x00
  119. -> code: OK
  120. > check_tx 0x00
  121. -> code: BadNonce
  122. -> log: Invalid nonce. Expected >= 1, got 0
  123. > deliver_tx 0x01
  124. -> code: OK
  125. > deliver_tx 0x04
  126. -> code: BadNonce
  127. -> log: Invalid nonce. Expected 2, got 4
  128. > info
  129. -> data: {"hashes":0,"txs":2}
  130. ```
  131. This is a very simple application, but between `counter` and `dummy`, its easy to see how you can build out arbitrary application states on top of the ABCI.
  132. In the near future, `erisdb` of Eris Industries will also run atop ABCI, bringing with it Ethereum-like accounts, the Ethereum virtual-machine, Eris's permissioning scheme, and native contracts extensions.
  133. But the ultimate flexibility comes from being able to write the application easily in any language.
  134. We have implemented the counter in a number of languages (see the example directory).
  135. To run the Node JS version, `cd` to `example/js` and run
  136. ```
  137. node app.js
  138. ```
  139. (you'll have to kill the other counter application process).
  140. In another window, run the console and those previous ABCI commands.
  141. You should get the same results as for the Go version.
  142. Want to write the counter app in your favorite language?! We'd be happy to add you to our [ecosystem](https://tendermint.com/ecosystem)! We're also offering [bounties](https://tendermint.com/bounties) for implementations in new languages!
  143. ## Notes
  144. The `abci-cli` is designed strictly for testing and debugging.
  145. In a real deployment, the role of sending messages is taken by Tendermint,
  146. which connects to the app using three separate connections,
  147. each with its own pattern of messages.
  148. For more information, see the [application developers guide](/docs/guides/app-development).
  149. For examples of running an ABCI app with Tendermint, see the [introductory guide](/docs/getting-started/first-abci-app).