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.

168 lines
5.5 KiB

6 years ago
  1. # Application BlockChain Interface (ABCI)
  2. [![CircleCI](https://circleci.com/gh/tendermint/abci.svg?style=svg)](https://circleci.com/gh/tendermint/abci)
  3. Blockchains are systems for multi-master state machine replication.
  4. **ABCI** is an interface that defines the boundary between the replication engine (the blockchain),
  5. and the state machine (the application).
  6. Using a socket protocol, a consensus engine running in one process
  7. can manage an application state running in another.
  8. Previously, the ABCI was referred to as TMSP.
  9. The community has provided a number of addtional implementations, see the [Tendermint Ecosystem](https://tendermint.com/ecosystem)
  10. ## Specification
  11. A detailed description of the ABCI methods and message types is contained in:
  12. - [A prose specification](specification.md)
  13. - [A protobuf file](https://github.com/tendermint/abci/blob/master/types/types.proto)
  14. - [A Go interface](https://github.com/tendermint/abci/blob/master/types/application.go).
  15. For more background information on ABCI, motivations, and tendermint, please visit [the documentation](http://tendermint.readthedocs.io/en/master/).
  16. The two guides to focus on are the `Application Development Guide` and `Using ABCI-CLI`.
  17. ## Protocl Buffers
  18. To compile the protobuf file, run:
  19. ```
  20. make protoc
  21. ```
  22. See `protoc --help` and [the Protocol Buffers site](https://developers.google.com/protocol-buffers)
  23. for details on compiling for other languages. Note we also include a [GRPC](http://www.grpc.io/docs)
  24. service definition.
  25. ## Install ABCI-CLI
  26. The `abci-cli` is a simple tool for debugging ABCI servers and running some
  27. example apps. To install it:
  28. ```
  29. go get github.com/tendermint/abci
  30. cd $GOPATH/src/github.com/tendermint/abci
  31. make get_vendor_deps
  32. make install
  33. ```
  34. ## Implementation
  35. We provide three implementations of the ABCI in Go:
  36. - Golang in-process
  37. - ABCI-socket
  38. - GRPC
  39. Note the GRPC version is maintained primarily to simplify onboarding and prototyping and is not receiving the same
  40. attention to security and performance as the others
  41. ### In Process
  42. The simplest implementation just uses function calls within Go.
  43. This means ABCI applications written in Golang can be compiled with TendermintCore and run as a single binary.
  44. See the [examples](#examples) below for more information.
  45. ### Socket (TSP)
  46. ABCI is best implemented as a streaming protocol.
  47. The socket implementation provides for asynchronous, ordered message passing over unix or tcp.
  48. Messages are serialized using Protobuf3 and length-prefixed with a [signed Varint](https://developers.google.com/protocol-buffers/docs/encoding?csw=1#signed-integers)
  49. For example, if the Protobuf3 encoded ABCI message is `0xDEADBEEF` (4 bytes), the length-prefixed message is `0x08DEADBEEF`, since `0x08` is the signed varint
  50. encoding of `4`. If the Protobuf3 encoded ABCI message is 65535 bytes long, the length-prefixed message would be like `0xFEFF07...`.
  51. Note the benefit of using this `varint` encoding over the old version (where integers were encoded as `<len of len><big endian len>` is that
  52. it is the standard way to encode integers in Protobuf. It is also generally shorter.
  53. ### GRPC
  54. GRPC is an rpc framework native to Protocol Buffers with support in many languages.
  55. Implementing the ABCI using GRPC can allow for faster prototyping, but is expected to be much slower than
  56. the ordered, asynchronous socket protocol. The implementation has also not received as much testing or review.
  57. Note the length-prefixing used in the socket implementation does not apply for GRPC.
  58. ## Usage
  59. The `abci-cli` tool wraps an ABCI client and can be used for probing/testing an ABCI server.
  60. For instance, `abci-cli test` will run a test sequence against a listening server running the Counter application (see below).
  61. It can also be used to run some example applications.
  62. See [the documentation](http://tendermint.readthedocs.io/en/master/) for more details.
  63. ### Examples
  64. Check out the variety of example applications in the [example directory](example/).
  65. It also contains the code refered to by the `counter` and `kvstore` apps; these apps come
  66. built into the `abci-cli` binary.
  67. #### Counter
  68. The `abci-cli counter` application illustrates nonce checking in transactions. It's code looks like:
  69. ```golang
  70. func cmdCounter(cmd *cobra.Command, args []string) error {
  71. app := counter.NewCounterApplication(flagSerial)
  72. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  73. // Start the listener
  74. srv, err := server.NewServer(flagAddrC, flagAbci, app)
  75. if err != nil {
  76. return err
  77. }
  78. srv.SetLogger(logger.With("module", "abci-server"))
  79. if err := srv.Start(); err != nil {
  80. return err
  81. }
  82. // Wait forever
  83. cmn.TrapSignal(func() {
  84. // Cleanup
  85. srv.Stop()
  86. })
  87. return nil
  88. }
  89. ```
  90. and can be found in [this file](cmd/abci-cli/abci-cli.go).
  91. #### kvstore
  92. The `abci-cli kvstore` application, which illustrates a simple key-value Merkle tree
  93. ```golang
  94. func cmdKVStore(cmd *cobra.Command, args []string) error {
  95. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  96. // Create the application - in memory or persisted to disk
  97. var app types.Application
  98. if flagPersist == "" {
  99. app = kvstore.NewKVStoreApplication()
  100. } else {
  101. app = kvstore.NewPersistentKVStoreApplication(flagPersist)
  102. app.(*kvstore.PersistentKVStoreApplication).SetLogger(logger.With("module", "kvstore"))
  103. }
  104. // Start the listener
  105. srv, err := server.NewServer(flagAddrD, flagAbci, app)
  106. if err != nil {
  107. return err
  108. }
  109. srv.SetLogger(logger.With("module", "abci-server"))
  110. if err := srv.Start(); err != nil {
  111. return err
  112. }
  113. // Wait forever
  114. cmn.TrapSignal(func() {
  115. // Cleanup
  116. srv.Stop()
  117. })
  118. return nil
  119. }
  120. ```