ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

使用 cleos get transaction 查询 EOS 链上交易信息:完整实战指南

使用 cleos get transaction 查询 EOS 链上交易信息:完整实战指南 区块链【免费下载链接】eosAn open source smart contract platform项目地址https://gitcode.com/gh_mirrors/eo/eos点击查看免费下载本指南介绍如何使用 EOS 官方命令行工具cleos的get transaction子命令通过交易 ID 查询链上任意一笔交易的详细信息包括交易所在区块、执行时间、签名、action 轨迹与收据等。阅读本文后你将掌握cleos get transaction的完整用法、输出字段含义、底层 RPC 调用链以及查询失败如历史数据缺失时的排查思路可直接用于 EOS 节点的日常开发与运维调试。一、查询交易信息的目标与前置条件交易transaction是 EOS 链上状态变更的基本载体一笔交易内含一个或多个 action经过签名后被广播到网络由出块节点打包进区块并最终不可逆。开发者常常需要根据交易 ID 回溯这笔交易的处理细节例如排查转账失败、核对 action 参数、确认上链时间此时cleos get transaction就是最直接的入口。开始之前需要满足以下前置条件安装当前仓库支持的 cleos 版本cleos是 EOS 的命令行客户端构建产物位于本仓库 programs/cleos 目录可通过 scripts/eosio_build.sh 等构建脚本编译得到。理解交易transaction的基本概念包括交易 IDtransaction id、action、签名signature、收据receipt等。连接一个启用了 history API 插件的 nodeos 节点这是本命令能够工作的关键前提详见下文第三节。二、基本用法按交易 ID 查询cleos get transaction的语法非常简洁位置参数id为必填即你要查询的交易 IDcleos get transaction id例如查询一笔交易 ID 为eb4b94b72718a369af09eb2e7885b3f494dd1d8a20278a6634611d5edd76b703的交易cleos get transaction eb4b94b72718a369af09eb2e7885b3f494dd1d8a20278a6634611d5edd76b703从 programs/cleos/main.cpp#L2998-L3010 可以确认该子命令的定义与请求构造逻辑// get transaction string transaction_id_str; uint32_t block_num_hint 0; auto getTransaction get-add_subcommand(transaction, localized(Retrieve a transaction from the blockchain)); getTransaction-add_option(id, transaction_id_str, localized(ID of the transaction to retrieve))-required(); getTransaction-add_option( -b,--block-hint, block_num_hint, localized(The block number this transaction may be in) ); getTransaction-callback([] { auto arg fc::mutable_variant_object( id, transaction_id_str); if ( block_num_hint 0 ) { arg arg(block_num_hint, block_num_hint); } std::cout fc::json::to_pretty_string(call(get_transaction_func, arg)) std::endl; });可以看出id参数被标记为-required()缺省会直接报错只有当显式传入--block-hint即block_num_hint 0时请求体才会携带block_num_hint字段查询结果通过fc::json::to_pretty_string以格式化 JSON输出到标准输出便于人读与二次解析。注意示例中的交易 ID 仅用于演示不会存在于你的链上直接复制执行会得到交易未找到的报错请使用自己链上真实产生的交易 ID。三、可选项-b, --block-hint区块号提示cleos get transaction支持一个可选参数用于在历史数据中找不到交易时加速定位选项类型说明-b, --block-hintUINT这笔交易可能所在的区块号block number它的作用体现在底层实现上详见第五节当 history 插件的历史索引中检索不到该交易时如果提供了block_num_hint插件会直接去指定区块号的区块中扫描全部交易收据逐条比对交易 ID从而在不依赖完整历史索引的情况下返回结果。因此当你知道交易大约在哪个区块例如刚打包进链、历史索引尚未同步完成时带上该参数可以显著提高查询成功率。四、返回结果示例与字段解析以下 JSON 是一次cleos get transaction的典型返回示例格式来自 docs/02_cleos/03_command-reference/get/transaction.md{ transaction_id: eb4b94b72718a369af09eb2e7885b3f494dd1d8a20278a6634611d5edd76b703, processed: { refBlockNum: 2206, refBlockPrefix: 221394282, expiration: 2017-09-05T08:03:58, scope: [ inita, tester ], signatures: [ 1f22e64240e1e479eee6ccbbd79a29f1a6eb6020384b4cca1a958e7c708d3e562009ae6e60afac96f9a3b89d729a50cd5a7b5a7a647540ba1678831bf970e83312 ], messages: [{ code: eos, type: transfer, authorization: [{ account: inita, permission: active } ], data: { from: inita, to: tester, amount: 1000, memo: }, hex_data: 000000008040934b00000000c84267a1e80300000000000000 } ], output: [{ notify: [{ name: tester, output: { notify: [], sync_transactions: [], async_transactions: [] } },{ name: inita, output: { notify: [], sync_transactions: [], async_transactions: [] } } ], sync_transactions: [], async_transactions: [] } ] } }从源码实现看当前版本的返回结构还包含以下关键字段见 plugins/history_plugin/history_plugin.cpp#L475-L520字段含义id命中的交易 ID支持前缀匹配时返回完整 IDblock_num该交易被打包进链的区块号block_time该交易的出块时间戳last_irreversible_block查询时刻链上最后一个不可逆区块号用于判断该交易是否已不可逆traces该交易内所有 action 的执行轨迹action_trace经 ABI 序列化展开后返回可看到每个 action 的接收方、数据、收据等trx交易对象本体内含receipt链上收据、signatures签名列表、context_free_data上下文无关数据等其中traces由历史插件存储的 packed action trace 反序列化并结合 ABI 展开得到trx则从对应区块的交易收据中还原完整交易与签名。据此你可以核对该笔交易执行了哪些 action、每个 action 的入参是什么、由谁签名、位于哪个区块、当前是否已经不可逆。五、底层原理从 cleos 到 history 插件的完整调用链cleos get transaction并非直接读取链上区块而是通过 HTTP 请求转发给 nodeos 节点上注册的history API 插件处理。整条链路如下1. RPC 端点定义programs/cleos/httpc.hpp#L105-L109 中明确了历史相关的 RPC 路径const string history_func_base /v1/history; const string get_actions_func history_func_base /get_actions; const string get_transaction_func history_func_base /get_transaction; const string get_key_accounts_func history_func_base /get_key_accounts; const string get_controlled_accounts_func history_func_base /get_controlled_accounts;即cleos get transaction最终向节点发送POST /v1/history/get_transaction请求。2. 服务端端点注册plugins/history_api_plugin/history_api_plugin.cpp#L33-L44 将该 RPC 注册到 HTTP 插件app().get_pluginhttp_plugin().add_api({ CHAIN_RO_CALL(get_actions, http_params_types::params_required), CHAIN_RO_CALL(get_transaction, http_params_types::params_required), CHAIN_RO_CALL(get_key_accounts, http_params_types::params_required), CHAIN_RO_CALL(get_controlled_accounts, http_params_types::params_required) });可见get_transaction是history_api_plugin对外暴露的只读接口它从history_plugin中取出只读 API 并委托执行。这解释了原文档中的提醒必须连接一个启用了 history API 插件的 nodeos 实例才能查询交易信息。3. 核心查询逻辑真正执行查询的逻辑位于 plugins/history_plugin/history_plugin.cpp#L442-L568 的read_only::get_transaction关键点包括交易 ID 校验输入 ID 必须是十六进制字符串长度不超过 64完整 ID 为 64 位十六进制即 32 字节且至少 8 个字符避免过高的碰撞概率非法输入会抛出transaction_id_type_exception。前缀匹配插件允许传入不完整的交易 ID 前缀通过十六进制前缀比对txn_id_matched支持半字节对齐比较在action_history_index索引中定位交易因此即使只记得交易 ID 的一部分也可以尝试查询。两路查找优先在历史索引action_history_index按trx_id与 action 序号索引中查找若历史中没有且未提供block_num_hint直接抛出tx_not_foundTransaction ${id} not found in history and no block hint was given。若提供了block_num_hint则直接fetch_block_by_number拉取指定区块遍历其中的交易收据比对 ID找到则组装trx与收据返回找不到则抛出 Transaction ${id} not found in history or in block number ${n}。结果组装命中后返回id、block_num、block_time、last_irreversible_block并将该交易的全部 action tracetraces与交易本体trx含收据、签名、上下文无关数据一并组装返回。六、常见问题与排查1. 报错 Transaction ... not found in history and no block hint was given说明节点上的 history 插件没有检索到该交易。可能原因历史索引未覆盖该交易所在的区块、节点启用了--disable-replay-opts等导致历史不完整或者交易 ID 有误。此时可尝试补上--block-hintcleos get transaction id -b block_num2. 报错 Invalid transaction ID / hex string is too long ...交易 ID 格式不正确。请确认传入的是十六进制字符串且长度在 864 字符之间。3. 提示无法连接到节点 / RPC 不存在确认cleos的-u, --url指向的 nodeos 节点确实加载了history_api_plugin配套的history_plugin同时负责维护历史索引。相关插件说明可参考 docs/01_nodeos/03_plugins/history_plugin/index.md 与 docs/01_nodeos/03_plugins/history_api_plugin/index.md。注意该 RPC 端点会因节点未启用插件而返回 404/未知端点错误。七、延伸阅读若你只有交易对象JSON 字符串或文件而没有交易 ID可用cleos get transaction_id transaction先离线计算交易 ID命令详情见 docs/02_cleos/03_command-reference/get/transaction_id.md。若想按账户批量浏览其参与的历史 actioncleos get actions见 docs/02_cleos/03_command-reference/get/actions.md它与get transaction共用同一套/v1/history端点与历史索引。命令参考汇总见 docs/02_cleos/03_command-reference/get/index.md。赞分享区块链【免费下载链接】eosAn open source smart contract platform项目地址https://gitcode.com/gh_mirrors/eo/eos点击查看免费下载相关推荐使用 keosd 创建钱包EOS 项目 cleos wallet create 完整实战指南使用 keosd 创建钱包EOS 项目 cleos wallet create 完整实战指南 导读 本文以 EOS 开源仓库 eo/eos 官方文档 ho区块链AWS CLI 实战指南使用 aws apigateway get-deployment 查询 API Gateway 部署信息AWS CLI 实战指南使用 aws apigateway get deployment 查询 API Gateway 部署信息 导读 get deploym开发工具云原生运维AWS CLI codecommit get-branch 实战指南查询 CodeCommit 分支信息与最新提交AWS CLI codecommit get branch 实战指南查询 CodeCommit 分支信息与最新提交 本篇技术指南以 AWS CLI 中 cod开发工具云原生运维创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表