拓冰建站拓冰建站
首页 / 资讯中心 / 正文

EOSIO cleos system undelegatebw 实战指南:撤销已委托的 CPU 带宽

区块链【免费下载链接】eosAn open source smart contract platform项目地址https://gitcode.com/gh_mirrors/eo/eos点击查看免费下载本指南以 EOSIO 官方cleos客户端为操作主线讲解如何使用cleos system undelegatebw撤销此前通过delegatebw委托给其他账户的 CPU及 NET带宽资源。读完本文你将掌握该命令的完整参数语义、常用选项、底层实现原理与验证方法能够在实际链上安全、准确地执行带宽撤销操作。目标撤销为账户或应用委托的资源在 EOSIO 网络中账户间的 CPU 与 NET 带宽通过delegatebw动作进行委托delegate。当业务场景变化——例如合作方不再需要你为其质押的带宽、或你自己需要收回质押的 CPU 资源时就需要使用undelegatebw动作来撤销undelegate这部分资源。执行撤销操作时必须牢记一条硬性约束只有当初发起委托的账户原始委托方才能撤销对应的带宽资源。被委托方或其他任何第三方账户都无法代为撤销。这一点在 how-to-undelegate-CPU.md 中有明确警示Beware that only the account which originally delegated resource can undelegate也是整个撤销流程最重要的前提。开始之前前置条件与概念准备在执行撤销操作前请确认以下条件与概念安装受支持的cleos版本cleos随 EOSIO 软件一同发布安装 EOSIO 即会安装cleos具体安装方式参见 00_install。本仓库中cleos的入口实现位于 programs/cleos/main.cpp。确保系统合约已部署eosio.contracts仓库中的参考系统合约reference system contracts必须已部署并用于管理系统资源。undelegatebw动作最终由系统合约中的eosio账户处理本仓库本身不包含该系统合约源码cleos只负责构造并发送该动作。理解以下基础概念账户AccountEOSIO 网络中的基本身份单元资源的委托与撤销都以账户为粒度网络带宽NET Bandwidth决定账户每单位时间内可发送交易字节数上限的资源CPU 带宽CPU Bandwidth决定账户交易执行可用 CPU 时间上限的资源。核心操作撤销 0.01 SYS 的 CPU 带宽以下命令将此前从账户bob委托给账户alice的0.01 SYSCPU 带宽撤销回来本示例来自 how-to-undelegate-CPU.mdcleos system undelegatebw bob alice 0 SYS 0.01 SYS命令执行成功后会得到类似如下的输出executed transaction: e7e7edb6c5556de933f9d663fea8b4a9cd56ece6ff2cebf056ddd0835efa6606 184 bytes 452 us # eosio eosio::undelegatebw {from:alice,receiver:bob,unstake_net_quantity:0.0000 EOS,unstake_cpu_qu... warning: transaction executed locally, but may not be confirmed by the network yet ]输出含义解析executed transaction: txid交易 ID随后是交易字节数184 bytes与本地执行耗时452 us# eosio eosio::undelegatebweosio系统账户接收并执行了undelegatebw动作动作载荷中包含from委托方、receiver被委托方、unstake_net_quantity撤销的 NET 数量与unstake_cpu_quantity撤销的 CPU 数量末尾的warning是正常提示交易已在本地节点执行但尚未确认上链。位置参数逐一说明根据命令参考文档 system-undelegatebw.md该命令包含 4 个必填位置参数参数类型说明fromTEXT撤销带宽的账户即原始委托方receiverTEXT被撤销带宽的账户即此前接收委托的账户unstake_net_quantityTEXT为 NET 带宽撤销的 EOS/SYS 数量unstake_cpu_quantityTEXT为 CPU 带宽撤销的 EOS/SYS 数量因此cleos system undelegatebw bob alice 0 SYS 0.01 SYS的含义是委托方bob从接收方alice处撤销0.01 SYS的 CPU 带宽NET 部分撤销0 SYS。数量字符串必须带精度如0.01 SYS且需与链上符号Symbol一致。需要留意的是命令行中from与receiver的先后顺序与自然语言描述容易混淆请严格以命令参考文档中的参数顺序先from后receiver为准。命令选项详解控制交易签名、广播与费用预算undelegatebw复用 cleos 的标准交易选项同样来自 system-undelegatebw.md可在实际使用中按需组合选项说明-h, --help打印帮助信息并退出-x, --expirationTEXT设置交易过期时间秒默认 30s-f, --force-unique强制交易唯一会消耗额外带宽用于防止意外重复提交相同交易-s, --skip-sign跳过签名不使用钱包密钥签名-d, --dont-broadcast不广播交易到网络仅打印到 stdout-r, --ref-blockTEXT设置 TAPOSTransaction as Proof-of-Stake使用的参考区块号或区块 ID-p, --permissionTEXT授权使用的账户与权限级别格式accountpermission默认accountactive--max-cpu-usage-msUINT设置交易执行的 CPU 预算上限毫秒默认 0 表示不限制--max-net-usageUINT设置交易的 NET 用量预算上限字节默认 0 表示不限制--delay-secUINT设置交易延迟秒数默认 0s-j, --json以 JSON 格式打印结果常用组合示例——只打印交易内容而不广播可用于预先审查cleos system undelegatebw bob alice 0 SYS 0.01 SYS -d -j源码级剖析cleos 如何构造 undelegatebw 动作cleos system undelegatebw的底层实现在 programs/cleos/main.cpp 的undelegate_bandwidth_subcommand结构中。从源码可以看到完整的数据流undelegate_bandwidth-add_option(from, from_str, localized(The account undelegating bandwidth))-required(); undelegate_bandwidth-add_option(receiver, receiver_str, localized(The account to undelegate bandwidth from))-required(); undelegate_bandwidth-add_option(unstake_net_quantity, unstake_net_amount, localized(The amount of tokens to undelegate for network bandwidth))-required(); undelegate_bandwidth-add_option(unstake_cpu_quantity, unstake_cpu_amount, localized(The amount of tokens to undelegate for CPU bandwidth))-required(); add_standard_transaction_options_plus_signing(undelegate_bandwidth, fromactive);其回调函数则完成动作载荷的构造与发送undelegate_bandwidth-callback([this] { fc::variant act_payload fc::mutable_variant_object() (from, from_str) (receiver, receiver_str) (unstake_net_quantity, to_asset(unstake_net_amount)) (unstake_cpu_quantity, to_asset(unstake_cpu_amount)); auto accountPermissions get_account_permissions(tx_permission, {name(from_str), config::active_name}); send_actions({create_action(accountPermissions, config::system_account_name, undelegatebw_n, act_payload)}, signing_keys_opt.get_keys()); });关键实现事实动作目标动作发送到config::system_account_name即eosio系统账户动作名为undelegatebw由已部署的系统合约执行资源账本更新数量转换用户输入的字符串数量经to_asset()转换为asset类型保证精度与符号合法性默认权限默认使用fromactive权限签名add_standard_transaction_options_plus_signing(undelegate_bandwidth, fromactive)也解释了为何只有from账户原始委托方能发起该操作——权限模型天然约束了操作主体注意对比与delegatebw实现于 programs/cleos/main.cpp不同undelegatebw的载荷只有from、receiver、unstake_net_quantity、unstake_cpu_quantity四个字段没有transfer标志——带宽一旦撤销其投票权与撤销权默认回归委托方。相关操作自我撤销unstake与 NET 撤销撤销自己账户的带宽unstake 场景当委托方与接收方是同一账户时即自己给自己质押带宽撤销操作退化为常见的 unstake 场景。参考 how-to-unstake-CPU.md撤销alice自己账户0.01 SYSCPU 带宽的命令为cleos system undelegatebw alice alice 0.01 SYS 0 SYS此时from与receiver均为alice动作载荷为{from:alice,receiver:alice,...}。撤销 NET 带宽同样的命令结构也适用于 NET 带宽的撤销参考 how-to-undelegate-NET.md将 CPU 数量的0.01 SYS移到 NET 位置即可cleos system undelegatebw bob alice 0.01 SYS 0 SYS与委托命令的对照撤销操作是委托操作的逆过程委托命令见 how-to-delegate-CPU-resource.mdcleos system delegatebw bob alice 0 SYS 0.01 SYS两者参数一一对应stake_net_quantity/stake_cpu_quantity对应撤销时的unstake_net_quantity/unstake_cpu_quantity完整的参数对照可查阅命令参考文档 system-delegatebw.md 与 system-undelegatebw.md命令总索引见 system/index.md。测试与验证仓库中的撤销用例本仓库的单元测试基建为我们提供了撤销操作的验证模板。在 unittests/eosio_system_tester.hpp 中eosio_system_tester测试工具封装了unstake辅助函数其构造的动作与cleos完全一致action_result unstake( const account_name from, const account_name to, const asset net, const asset cpu ) { return push_action( name(from), undelegatebw_n, mvo() (from, from) (receiver, to) (unstake_net_quantity, net) (unstake_cpu_quantity, cpu) ); }这印证了两点事实undelegatebw动作的标准载荷字段即为from、receiver、unstake_net_quantity、unstake_cpu_quantity与 unittests/contracts/eosio.system/eosio.system.abi 中声明的 ABI 一致测试中以from账户为签名者推送动作再次确认了只有原始委托方才能撤销的权限模型。同时tests/Node.py 等集成测试脚本中也有对undelegatebw的调用覆盖可用于端到端流程验证。注意事项与常见问题只有原始委托方可以撤销若账户 A 曾将带宽委托给 B只有 A 能发起undelegatebwB 或第三方账户无权操作撤销后资源回归 A而非自动归属 B。不能超额撤销撤销数量不能超过此前委托的数量否则系统合约会拒绝该动作。交易确认输出中的warning: transaction executed locally, but may not be confirmed by the network yet属正常提示最终以上链后的区块确认为准。资源回收时机undelegatebw动作由系统合约处理撤销的 token 会进入系统合约的资源回收流程具体到账时机取决于系统合约实现该合约源码不在本仓库内来自独立的eosio.contracts仓库。权限设置默认使用fromactive权限如委托方使用自定义权限管理资源可通过-p选项显式指定。赞分享区块链【免费下载链接】eosAn open source smart contract platform项目地址https://gitcode.com/gh_mirrors/eo/eos点击查看免费下载相关推荐EOSIO cleos system listbw 命令详解查询账户委托带宽Delegated BandwidthEOSIO cleos system listbw 命令详解查询账户委托带宽Delegated Bandwidth 导读 cleos system lis区块链EOSIO 委托网络带宽NET资源cleos system delegatebw 操作指南与底层原理EOSIO 委托网络带宽NET资源cleos system delegatebw 操作指南与底层原理 导读 本文基于本仓库eo/eos中《How to区块链EOS 网络带宽NET解除质押完全指南cleos system undelegatebw 实战解析EOS 网络带宽NET解除质押完全指南cleos system undelegatebw 实战解析 在 EOS 主网与基于 eosio 的链上账户通过质区块链创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门