索引交易

Tendermint允许您索引交易,然后查询或订阅它们的结果。

让我们来看看 [tx_index] 配置部分: Let’s take a look at the [tx_index] config section:

  1. ##### transactions indexer configuration options #####
  2. [tx_index]
  3. # What indexer to use for transactions
  4. #
  5. # Options:
  6. # 1) "null"
  7. # 2) "kv" (default) - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  8. indexer = "kv"
  9. # Comma-separated list of tags to index (by default the only tag is "tx.hash")
  10. #
  11. # You can also index transactions by height by adding "tx.height" tag here.
  12. #
  13. # It's recommended to index only a subset of tags due to possible memory
  14. # bloat. This is, of course, depends on the indexer's DB and the volume of
  15. # transactions.
  16. index_tags = ""
  17. # When set to true, tells indexer to index all tags (predefined tags:
  18. # "tx.hash", "tx.height" and all tags from DeliverTx responses).
  19. #
  20. # Note this may be not desirable (see the comment above). IndexTags has a
  21. # precedence over IndexAllTags (i.e. when given both, IndexTags will be
  22. # indexed).
  23. index_all_tags = false

默认情况下,Tendermint 将使用嵌入的简单索引器按各自的哈希索引所有交易。注意,我们计划在将来添加更多的选项(例如:Postgresql 索引器)。

添加标记

在应用程序的 DeliverTx 方法中,使用 UTF-8 编码的字符串对添加 Tags 字段(例如:”account.owner”: “Bob”, “balance”: “100.0”, “date”: “2018-01-02”)。

例子:

  1. func (app *KVStoreApplication) DeliverTx(tx []byte) types.Result {
  2. ...
  3. tags := []cmn.KVPair{
  4. {[]byte("account.name"), []byte("igor")},
  5. {[]byte("account.address"), []byte("0xdeadbeef")},
  6. {[]byte("tx.amount"), []byte("7")},
  7. }
  8. return types.ResponseDeliverTx{Code: code.CodeTypeOK, Tags: tags}
  9. }

如果您想让 Tendermint 只通过 “account.name” 标记索引交易,请在配置集中设置 tx_index.index_tags="account.name"。如果要索引所有标签,请设置 index_all_tags=true

注意,这里有一些预定义的标签:

  • tx.hash (交易哈希)
  • tx.height (交易提交的块高度)

如果您尝试使用以上任何一个键,Tendermint 都会发出警告。

查询交易

您可以通过调用 /tx_search RPC 端点查询交易结果:

  1. curl "localhost:26657/tx_search?query=\"account.name='igor'\"&prove=true"

查看API 文档,了解更多关于查询语法和其他选项的信息。

订阅交易

通过向 /subscribe RPC 端点提供查询,客户端可以通过 Websocket 订阅具有给定标记的交易。

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "subscribe",
  4. "id": "0",
  5. "params": {
  6. "query": "account.name='igor'"
  7. }
  8. }

请查看API 文档,以获得关于查询语法和其他选项的更多信息。