From 547a04d12b7466601e21966e2da186c860e149c9 Mon Sep 17 00:00:00 2001 From: luqingjie <544309509@qq.com> Date: Sun, 1 Nov 2026 22:16:58 -0500 Subject: [PATCH] =?UTF-8?q?=E7=A7=9F=E8=B5=81=E6=A8=A1=E5=9D=97-=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=E4=B8=8A=E9=93=BE=E4=B8=8B=E5=8D=95=E6=88=90=E5=8A=9F?= =?UTF-8?q?=EF=BC=8C=E8=8A=82=E7=82=B9=E5=9C=A8=E7=BA=BF=EF=BC=8C=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E4=B9=9F=E9=83=BD=E6=AD=A3=E7=A1=AE=EF=BC=8C=E9=92=B1?= =?UTF-8?q?=E5=8C=85=E8=B5=84=E6=BA=90=E4=B9=9F=E5=85=85=E8=B6=B3=EF=BC=8C?= =?UTF-8?q?=E4=BD=86=E6=98=AF=E8=8A=82=E7=82=B9=E6=B2=A1=E6=9C=89=E6=8E=A5?= =?UTF-8?q?=E6=94=B6=E8=BF=99=E4=B8=AA=E7=B1=BB=E5=9E=8B=E7=9A=84=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=EF=BC=8C=E6=97=A0=E6=B3=95=E4=B8=8A=E9=93=BE=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E3=80=82=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/rental-address-onchain.md | 29 +++++++- .../com/ruoyi/system/rental/RentalTasks.java | 48 +++++++++---- .../system/rental/RentalBusinessTest.java | 70 ++++++++++++++++++- 3 files changed, 131 insertions(+), 16 deletions(-) diff --git a/docs/rental-address-onchain.md b/docs/rental-address-onchain.md index 5e83f0f..5c52e90 100644 --- a/docs/rental-address-onchain.md +++ b/docs/rental-address-onchain.md @@ -103,10 +103,37 @@ rental: ## 日志排查 -使用orderId、taskId、nodeId、network、txID串联云端及本地日志。云端打印冻结金额拆分、候选余额不足(DEBUG)、派单、节点事件、独立证明、扣款及明确失败。Node打印PREFLIGHT、预算、BUILT_PERSISTED、BROADCASTED/UNKNOWN;Java打印任务阶段、原交易恢复及RPC路径耗时。密钥、签名交易、API Secret、原始凭证帧和带认证信息的响应禁止打印。 +使用orderId、taskId、nodeId、network、txID串联云端及本地日志。云端打印冻结金额拆分、无合格候选、候选余额不足(INFO,包含本金及费用上限SUN)、同地址跳过、状态变化/执行槽占用、派单、节点事件、独立证明、扣款及明确失败。Node打印PREFLIGHT、预算、BUILT_PERSISTED、BROADCASTED/UNKNOWN;Java打印任务阶段、原交易恢复及RPC路径耗时。密钥、签名交易、API Secret、原始凭证帧和带认证信息的响应禁止打印。 + +### 节点在线但未收到地址上链订单 + +地址上链不依赖能量/带宽出租开关、可委托质押本金、历史资源证明或资源快照有效期。供应钱包必须在订单对应网络、状态为ACTIVE且已完成控制权验证;节点必须ACTIVE、ONLINE、心跳未过期,并由Agent在连接恢复同步时上报转账授权。仅打开能量/带宽出租并不会授予转账权限。 + +Agent配置 `RENT_TRANSFER_ENABLED=true` 后需重启或重连;数据库中的 `rent_node.transfer_enabled` 应由Agent同步上报,不应手工改成1绕过本地授权。Java Agent还需要打开总广播授权。云端实时查询余额至少覆盖固定1TRX与订单快照中的fee-limit-sun,Agent执行前还会检查实际创建费、带宽费用及本地留存。 + +修复后的转账任务 `conversion_block_height=0` 表示不适用质押换算,不代表链上确认高度。委托/回收任务仍使用可信资源高度。无需为此修改数据库结构或生成虚假资源快照。 + +可使用以下只读SQL核对部署状态: + +```sql +SELECT o.id, o.order_no, o.network, o.status, o.dispatch_deadline, + t.task_no, t.task_type, t.status AS task_status +FROM rent_order o LEFT JOIN rent_task t ON t.order_id=o.id +WHERE o.order_type='ADDRESS_ONCHAIN' ORDER BY o.id DESC LIMIT 20; + +SELECT w.id AS wallet_id, w.network, w.status AS wallet_status, + w.ownership_verified_at, n.id AS node_id, n.status AS node_status, + n.connection_status, n.heartbeat_expires_at, n.transfer_enabled +FROM rent_wallet w JOIN rent_node n ON n.id=w.node_id +ORDER BY w.id; +``` + +WAITING且没有任务表示尚未派单;已有PENDING任务则继续检查Socket推送和Agent接收日志。EXECUTING/UNKNOWN已有在途任务时,不允许手动重置订单或另建交易。更新服务端后,仍在派单期限内的WAITING订单由扫描自动重试;已超时取消的订单不会自动恢复。 ## 验证记录(2026-10-29) +地址上链派单修复回归:JDK8离线执行 `RentalBusinessTest,RentalOnchainTest`,67项通过、0失败、0错误。新增覆盖无资源快照派单并完成独立核验扣款、过期快照不阻止转账但阻止资源委托,以及无快照下身份/网络/在线心跳/授权/余额保护。本次未进行真实链上广播或生产部署。 + 追加服务费分账后,JDK8云端租赁测试95项通过,包含H2真实事务、唯一键、无套餐下单、重复冻结防护、分成边界及舍入守恒、历史快照不重算、平台单次入账、净额结算、UNKNOWN隔离和独立链上参数证明。公开API文档ESLint通过。此前Node Agent14项、Java17 Agent41项测试通过,Java Agent完成package构建,Vue模板编译及staging生产构建通过(现有3项无关API导出警告及2项包大小警告);此次分账未修改Agent。 本次未运行生产MySQL增量脚本、未部署、未连接真实钱包、未执行真实支付或链上广播;实际两网联调应在配置完成后进行。 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalTasks.java b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalTasks.java index c5978df..9195b80 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalTasks.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalTasks.java @@ -90,19 +90,29 @@ public class RentalTasks { }); return; } - List> candidates = - store.rows( - "SELECT w.* FROM rent_wallet w JOIN rent_node n ON n.id=w.node_id WHERE" - + " w.status='ACTIVE' AND w.ownership_verified_at IS NOT NULL AND" - + " w.network=? AND w.snapshot_expires_at>? AND n.status='ACTIVE' AND" - + " n.connection_status='ONLINE' AND n.heartbeat_expires_at>? ORDER BY" - + " w.id LIMIT 20", - text(initial, "network"), - now(), - now()); + boolean onchain = RentalOnchain.isOrder(initial); + // 转账不使用质押资源换算,不能因资源快照缺失/过期而阻止派单。 + // 资源租赁仍严格要求新鲜快照;转账余额在候选筛选及 Agent 签名前分别核验。 + String candidateSql = "SELECT w.* FROM rent_wallet w JOIN rent_node n ON n.id=w.node_id WHERE" + + " w.status='ACTIVE' AND w.ownership_verified_at IS NOT NULL AND w.network=?" + + " AND n.status='ACTIVE' AND n.connection_status='ONLINE' AND n.heartbeat_expires_at>?" + + (onchain ? " AND n.transfer_enabled=1" : " AND w.snapshot_expires_at>?") + + " ORDER BY w.id LIMIT 20"; + List> candidates = onchain + ? store.rows(candidateSql, text(initial, "network"), now()) + : store.rows(candidateSql, text(initial, "network"), now(), now()); + if (onchain && candidates.isEmpty()) { + log.info("地址上链暂无可派单钱包 orderId={} network={} reason=NO_ELIGIBLE_WALLET" + + " 请核对钱包ACTIVE及控制权验证、节点ACTIVE/ONLINE及心跳、Agent的RENT_TRANSFER_ENABLED=true并重连", + orderId, initial.get("network")); + } for (Map candidate : candidates) { String type = text(initial, "resource_type"), owner = text(candidate, "address"); if (owner.equals(text(initial, "receiver_address"))) { + if (onchain) { + log.info("地址上链跳过同地址转账 orderId={} walletId={} reason=SAME_ADDRESS", + orderId, candidate.get("id")); + } continue; } if (RentalOnchain.isOrder(initial)) { @@ -229,7 +239,7 @@ public class RentalTasks { private boolean dispatchOnchain(Map initial, Map candidate) { Map advertised = store.get("node", number(candidate, "node_id"), false); if (number(advertised, "transfer_enabled") != 1) { - log.debug("地址上链候选未授权转账 orderId={} nodeId={}", initial.get("id"), advertised.get("id")); + log.info("地址上链候选未授权转账 orderId={} nodeId={}", initial.get("id"), advertised.get("id")); return false; } Map price; @@ -242,7 +252,9 @@ public class RentalTasks { long balance = chain.call(text(initial, "network"), "/wallet/getaccount", map("address", text(candidate, "address"), "visible", true)).path("balance").asLong(); if (balance < Math.addExact(RentalOnchain.TRANSFER_SUN, number(price, "fee_limit_sun"))) { - log.debug("地址上链候选余额不足 orderId={} walletId={} balanceSun={}", initial.get("id"), candidate.get("id"), balance); + log.info("地址上链候选余额不足 orderId={} walletId={} balanceSun={} transferSun={} feeLimitSun={}", + initial.get("id"), candidate.get("id"), balance, RentalOnchain.TRANSFER_SUN, + price.get("fee_limit_sun")); return false; } return Boolean.TRUE.equals(tx(() -> { @@ -251,10 +263,15 @@ public class RentalTasks { Map node = store.get("node", number(wallet, "node_id"), true); if (!"WAITING".equals(text(order, "status")) || !time(order.get("dispatch_deadline")).isAfter(Instant.now()) || !"ACTIVE".equals(text(wallet, "status")) || !"ACTIVE".equals(text(node, "status")) + || wallet.get("ownership_verified_at") == null + || !text(order, "network").equals(text(wallet, "network")) || number(node, "transfer_enabled") != 1 || !"ONLINE".equals(text(node, "connection_status")) + || node.get("heartbeat_expires_at") == null || !time(node.get("heartbeat_expires_at")).isAfter(Instant.now()) || store.one("SELECT id FROM rent_task WHERE wallet_id=? AND resource_type='TRX' AND wallet_execution_slot=1", wallet.get("id")) != null) { + log.info("地址上链候选状态变化或执行槽占用 orderId={} walletId={} nodeId={}", + order.get("id"), wallet.get("id"), node.get("id")); return false; } Map supplier = funds.account(number(node, "user_id"), text(order, "network")); @@ -308,6 +325,9 @@ public class RentalTasks { throw new IllegalArgumentException("订单规则快照损坏"); } long fee = number(product, "fee_limit_sun"); + // 转账无质押换算高度;使用0满足任务表NOT NULL契约,绝不伪造资源证明。 + long conversionBlockHeight = RentalOnchain.TASK_TYPE.equals(type) + ? 0L : number(wallet, "snapshot_block_height"); Map intent = map( "protocol", @@ -339,7 +359,7 @@ public class RentalTasks { "baseline_sun", !"RECLAIM".equals(type) ? "0" : Long.toString(sun), "conversion_block_height", - text(wallet, "snapshot_block_height"), + Long.toString(conversionBlockHeight), "delegate_lock", false, "fee_limit_sun", @@ -378,7 +398,7 @@ public class RentalTasks { "conversion_total_weight", weight, "conversion_block_height", - wallet.get("snapshot_block_height"), + conversionBlockHeight, "conversion_at", now(), "estimated_resource_quantity", diff --git a/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java b/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java index 715acc9..3257f7a 100644 --- a/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java +++ b/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java @@ -41,6 +41,7 @@ public class RentalBusinessTest { private boolean receiverActivated; private boolean activationRpcFailed; private int activationChecks; + private long supplierBalanceSun = 10000000L; private static final String ADDRESS = "TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf"; private static final String OWNER = "TR7NHqjeKQxGTCi8q8ZY4pL8otgjLj6t"; private final ObjectMapper json = new ObjectMapper(); @@ -654,7 +655,7 @@ public class RentalBusinessTest { new RentalChain(json) { @Override public com.fasterxml.jackson.databind.JsonNode call(String network, String path, Map body) { - return json.valueToTree(map("balance", 10000000)); + return json.valueToTree(map("balance", supplierBalanceSun)); } @Override public long relation( @@ -754,6 +755,73 @@ public class RentalBusinessTest { expectFailure(() -> tx.execute(status -> orders.create(app, 10, request))); } + @Test + public void onchainDispatchDoesNotRequireStakeSnapshotAndStillVerifiesTransfer() { + store.update("node", node, map("transfer_enabled", 1)); + store.update("wallet", wallet, map("snapshot_block_height", null, + "snapshot_at", null, "snapshot_expires_at", null, + "energy_enabled", 0, "bandwidth_enabled", 0)); + long id = number(tx.execute(status -> orders.create(app, 10, + onchainRequest("onchain-no-snapshot"))), "id"); + tasks.dispatch(id); + tasks.dispatch(id); + Map task = store.one("SELECT * FROM rent_task WHERE order_id=?", id); + assertNotNull(task); + assertEquals("TRANSFER_TRX", text(task, "task_type")); + assertEquals(0L, number(task, "conversion_block_height")); + assertEquals(1, store.rows("SELECT id FROM rent_task WHERE order_id=?", id).size()); + tasks.report(node, map("task_no", task.get("task_no"), "event_id", "no-snapshot-built", + "state", "BUILT", "tx_id", String.join("", Collections.nCopies(64, "d")))); + tasks.verify(number(task, "id")); + assertEquals("SUCCESS", text(store.get("order", id, false), "status")); + assertEquals("CHARGED", text(store.get("order", id, false), "fund_status")); + } + + @Test + public void onchainAcceptsExpiredSnapshotButResourceRentalStillWaits() { + store.update("node", node, map("transfer_enabled", 1)); + store.update("wallet", wallet, map("snapshot_expires_at", after(-60))); + long rentalId = number(create("resource-expired-snapshot"), "id"); + tasks.dispatch(rentalId); + assertNull(store.one("SELECT id FROM rent_task WHERE order_id=?", rentalId)); + long transferId = number(tx.execute(status -> orders.create(app, 10, + onchainRequest("onchain-expired-snapshot"))), "id"); + tasks.dispatch(transferId); + assertNotNull(store.one("SELECT id FROM rent_task WHERE order_id=?", transferId)); + } + + @Test + public void onchainWithoutSnapshotStillRequiresVerifiedOnlineAuthorizedWalletAndBalance() { + store.update("node", node, map("transfer_enabled", 1)); + store.update("wallet", wallet, map("snapshot_expires_at", null)); + long id = number(tx.execute(status -> orders.create(app, 10, + onchainRequest("onchain-guards"))), "id"); + store.update("wallet", wallet, map("ownership_verified_at", null)); + tasks.dispatch(id); + assertNull(store.one("SELECT id FROM rent_task WHERE order_id=?", id)); + store.update("wallet", wallet, map("ownership_verified_at", now(), "network", "MAINNET")); + tasks.dispatch(id); + assertNull(store.one("SELECT id FROM rent_task WHERE order_id=?", id)); + store.update("wallet", wallet, map("network", "NILE")); + store.update("node", node, map("connection_status", "OFFLINE")); + tasks.dispatch(id); + assertNull(store.one("SELECT id FROM rent_task WHERE order_id=?", id)); + store.update("node", node, map("connection_status", "ONLINE", "heartbeat_expires_at", after(-60))); + tasks.dispatch(id); + assertNull(store.one("SELECT id FROM rent_task WHERE order_id=?", id)); + store.update("node", node, map("heartbeat_expires_at", after(300), "transfer_enabled", 0)); + tasks.dispatch(id); + assertNull(store.one("SELECT id FROM rent_task WHERE order_id=?", id)); + store.update("node", node, map("transfer_enabled", 1)); + supplierBalanceSun = 3999999L; + tasks.dispatch(id); + assertNull(store.one("SELECT id FROM rent_task WHERE order_id=?", id)); + assertEquals("HELD", text(store.get("order", id, false), "fund_status")); + supplierBalanceSun = 4000000L; + tasks.dispatch(id); + assertNotNull(store.one("SELECT id FROM rent_task WHERE order_id=?", id)); + } + @Test public void onchainRequiresCapableNodeAndChargesOnlyAfterIndependentProof() { Map order = tx.execute(status -> orders.create(app, 10, onchainRequest("onchain-success")));