From b84675df3c3eb50c503e56cd13a6fe32927f0dc2 Mon Sep 17 00:00:00 2001 From: luqingjie <544309509@qq.com> Date: Sun, 1 Nov 2026 22:43:41 -0500 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BF=AB=E7=85=A7=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/rental-address-onchain.md | 4 + .../com/ruoyi/system/rental/RentalChain.java | 159 +++++++++++------- .../com/ruoyi/system/rental/RentalTasks.java | 10 ++ .../ruoyi/system/rental/RentalWorkers.java | 23 ++- 4 files changed, 133 insertions(+), 63 deletions(-) diff --git a/docs/rental-address-onchain.md b/docs/rental-address-onchain.md index 5c52e90..efaf601 100644 --- a/docs/rental-address-onchain.md +++ b/docs/rental-address-onchain.md @@ -107,6 +107,10 @@ rental: ### 节点在线但未收到地址上链订单 +快照更新时间长期不变时,先检查启动日志“租赁快照定时扫描已启用”。扫描每轮结束后延迟15秒再次运行,每批最多20个ACTIVE且快照缺失/过期的钱包;游标走到末尾后下一轮从头开始,因此不是每个钱包严格每15秒更新。刷新成功输出INFO(walletId、网络、旧快照时间、耗时),失败输出WARN及安全异常因果链;批次汇总输出成功/失败数量。无候选和区块夹读明细为DEBUG。失败不会推进snapshot_at或有效期。 + +链上采集失败日志stage含义:BEFORE_BLOCK/AFTER_BLOCK为区块RPC,ACCOUNT_RESOURCE为资源RPC,ENERGY_DELEGATABLE/BANDWIDTH_DELEGATABLE为对应可委托本金RPC,BLOCK_CONSISTENCY为前后区块一致性检查,GLOBAL_PARAMETERS为全网参数检查。BLOCK_CHANGED_OR_INVALID显示前后高度;STALE_OR_CONCURRENT显示钱包版本、新旧高度及快照屏障。RPC另记录路径和非成功HTTP状态码,不记录认证头或响应原文。请按walletId、网络、地址和相邻时间关联日志。 + 地址上链不依赖能量/带宽出租开关、可委托质押本金、历史资源证明或资源快照有效期。供应钱包必须在订单对应网络、状态为ACTIVE且已完成控制权验证;节点必须ACTIVE、ONLINE、心跳未过期,并由Agent在连接恢复同步时上报转账授权。仅打开能量/带宽出租并不会授予转账权限。 Agent配置 `RENT_TRANSFER_ENABLED=true` 后需重启或重连;数据库中的 `rent_node.transfer_enabled` 应由Agent同步上报,不应手工改成1绕过本地授权。Java Agent还需要打开总广播授权。云端实时查询余额至少覆盖固定1TRX与订单快照中的fee-limit-sun,Agent执行前还会检查实际创建费、带宽费用及本地留存。 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalChain.java b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalChain.java index 59a792a..029e2b7 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalChain.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalChain.java @@ -80,14 +80,18 @@ public class RentalChain { json.writeValueAsString(body))) .build(); try (Response response = client.newCall(request).execute()) { + if (!response.isSuccessful()) { + // 不记录完整URL、认证头或响应原文;状态码足以区分限流、路径错误和服务故障。 + log.warn("租赁RPC返回非成功状态 path={} httpStatus={}", path, response.code()); + } boolean fullBlock = path.endsWith("/getnowblock") || path.endsWith("/getblockbynum") || path.endsWith("/gettransactioninfobyblocknum"); return json.readTree(RentalHttp.body(response, (fullBlock ? 16 : 1) * 1024 * 1024)); } } catch (Exception ex) { - log.error(ex.getMessage(), ex); - throw new IllegalStateException("链上查询失败,保留原业务状态 " + base + path); + log.error("租赁RPC查询失败 path={} diagnostic={}", path, RentalDiagnostics.describe(ex)); + throw new IllegalStateException("链上查询失败,保留原业务状态", ex); } } @@ -118,67 +122,100 @@ public class RentalChain { } public Map snapshot(String network, String address) { - JsonNode beforeBlock = call(network, "/wallet/getnowblock", map()); - long before = block(beforeBlock); - JsonNode resources = - call( - network, - "/wallet/getaccountresource", - map("address", address, "visible", true)); - long energy = delegatable(network, address, 1); - long bandwidth = delegatable(network, address, 0); - JsonNode afterBlock = call(network, "/wallet/getnowblock", map()); - long after = block(afterBlock); - require( - before >= 0 && before == after && energy >= 0 && bandwidth >= 0 - && beforeBlock.path("blockID").asText().matches("[0-9a-fA-F]{64}") - && beforeBlock.path("blockID").asText().equalsIgnoreCase(afterBlock.path("blockID").asText()), - "快照跨区块或本金查询不完整,请重试"); - Map result = - map( - "snapshot_block_height", - after, - "energy_delegatable_sun", - energy, - "bandwidth_delegatable_sun", - bandwidth, - "energy_available", - Math.max( - 0, - resources.path("EnergyLimit").asLong() - - resources.path("EnergyUsed").asLong()), - "bandwidth_available", - Math.max( - 0, - resources.path("NetLimit").asLong() - - resources.path("NetUsed").asLong()), - "total_energy_limit", - resources.path("TotalEnergyLimit").asLong(), - "total_energy_weight", - resources.path("TotalEnergyWeight").asLong(), - "total_bandwidth_limit", - resources.path("TotalNetLimit").asLong(), - "total_bandwidth_weight", - resources.path("TotalNetWeight").asLong(), - "snapshot_at", - now(), - "snapshot_expires_at", - after(15)); - require( - number(result, "total_energy_limit") > 0 - && number(result, "total_energy_weight") > 0 - && number(result, "total_bandwidth_limit") > 0 - && number(result, "total_bandwidth_weight") > 0, - "全网参数不完整"); - if (embeddedHistory && historyService != null) { - // 复用本次夹读,不再为每个订单额外请求全网资源参数;不读取旧rent_wallet快照。 - try { - historyService.getObject().offerParameters(network, beforeBlock, afterBlock, resources); - } catch (RuntimeException ex) { - log.warn("钱包全局参数采样未入库 network={},独立采样器将继续重试", network); + long started = System.nanoTime(); + String stage = "BEFORE_BLOCK"; + try { + JsonNode beforeBlock = call(network, "/wallet/getnowblock", map()); + long before = block(beforeBlock); + stage = "ACCOUNT_RESOURCE"; + JsonNode resources = + call( + network, + "/wallet/getaccountresource", + map("address", address, "visible", true)); + stage = "ENERGY_DELEGATABLE"; + long energy = delegatable(network, address, 1); + stage = "BANDWIDTH_DELEGATABLE"; + long bandwidth = delegatable(network, address, 0); + stage = "AFTER_BLOCK"; + JsonNode afterBlock = call(network, "/wallet/getnowblock", map()); + long after = block(afterBlock); + stage = "BLOCK_CONSISTENCY"; + log.debug("租赁快照区块夹读 network={} address={} beforeHeight={} afterHeight={} sameBlockId={} elapsedMs={}", + network, address, before, after, + beforeBlock.path("blockID").asText().equalsIgnoreCase(afterBlock.path("blockID").asText()), + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started)); + if (before < 0 || before != after + || !beforeBlock.path("blockID").asText().matches("[0-9a-fA-F]{64}") + || !beforeBlock.path("blockID").asText().equalsIgnoreCase(afterBlock.path("blockID").asText())) { + log.warn("租赁快照区块不一致 network={} address={} beforeHeight={} afterHeight={}" + + " reason=BLOCK_CHANGED_OR_INVALID;本次不更新,禁止混用不同区块资源", + network, address, before, after); } + require( + before >= 0 && before == after && energy >= 0 && bandwidth >= 0 + && beforeBlock.path("blockID").asText().matches("[0-9a-fA-F]{64}") + && beforeBlock.path("blockID").asText().equalsIgnoreCase(afterBlock.path("blockID").asText()), + "快照跨区块或本金查询不完整,请重试"); + Map result = + map( + "snapshot_block_height", + after, + "energy_delegatable_sun", + energy, + "bandwidth_delegatable_sun", + bandwidth, + "energy_available", + Math.max( + 0, + resources.path("EnergyLimit").asLong() + - resources.path("EnergyUsed").asLong()), + "bandwidth_available", + Math.max( + 0, + resources.path("NetLimit").asLong() + - resources.path("NetUsed").asLong()), + "total_energy_limit", + resources.path("TotalEnergyLimit").asLong(), + "total_energy_weight", + resources.path("TotalEnergyWeight").asLong(), + "total_bandwidth_limit", + resources.path("TotalNetLimit").asLong(), + "total_bandwidth_weight", + resources.path("TotalNetWeight").asLong(), + "snapshot_at", + now(), + "snapshot_expires_at", + after(15)); + stage = "GLOBAL_PARAMETERS"; + if (number(result, "total_energy_limit") <= 0 || number(result, "total_energy_weight") <= 0 + || number(result, "total_bandwidth_limit") <= 0 || number(result, "total_bandwidth_weight") <= 0) { + log.warn("租赁快照全网参数不完整 network={} address={} energyLimit={} energyWeight={}" + + " bandwidthLimit={} bandwidthWeight={}", network, address, + result.get("total_energy_limit"), result.get("total_energy_weight"), + result.get("total_bandwidth_limit"), result.get("total_bandwidth_weight")); + } + require( + number(result, "total_energy_limit") > 0 + && number(result, "total_energy_weight") > 0 + && number(result, "total_bandwidth_limit") > 0 + && number(result, "total_bandwidth_weight") > 0, + "全网参数不完整"); + if (embeddedHistory && historyService != null) { + // 复用本次夹读,不再为每个订单额外请求全网资源参数;不读取旧rent_wallet快照。 + try { + historyService.getObject().offerParameters(network, beforeBlock, afterBlock, resources); + } catch (RuntimeException ex) { + log.warn("钱包全局参数采样未入库 network={},独立采样器将继续重试", network); + } + } + return result; + } catch (RuntimeException ex) { + log.warn("租赁链上快照采集失败 network={} address={} stage={} elapsedMs={} diagnostic={}", + network, address, stage, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started), + RentalDiagnostics.describe(ex)); + throw ex; } - return result; } /** protobuf省略零字段的空对象表示0;显式错误和非整数绝不能当作0。 */ 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 9195b80..83ba84c 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 @@ -50,6 +50,16 @@ public class RentalTasks { tx( () -> { Map current = store.get("wallet", id, true); + if (number(previous, "version") != number(current, "version") + || number(snapshot, "snapshot_block_height") < number(current, "snapshot_min_block_height") + || (current.get("snapshot_block_height") != null + && number(snapshot, "snapshot_block_height") < number(current, "snapshot_block_height"))) { + log.warn("租赁钱包快照写入被拒绝 walletId={} network={} reason=STALE_OR_CONCURRENT" + + " previousVersion={} currentVersion={} sampledHeight={} storedHeight={} minHeight={}", + id, current.get("network"), previous.get("version"), current.get("version"), + snapshot.get("snapshot_block_height"), current.get("snapshot_block_height"), + current.get("snapshot_min_block_height")); + } require( number(previous, "version") == number(current, "version") && number(snapshot, "snapshot_block_height") diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalWorkers.java b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalWorkers.java index 142c598..81e3660 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalWorkers.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalWorkers.java @@ -43,6 +43,7 @@ public class RentalWorkers { "任务推送间隔必须为500到60000毫秒"); LOG.info("租赁交付调度配置 dispatchIntervalMs={} pushIntervalMs={}", dispatchIntervalMs, pushIntervalMs); + LOG.info("租赁快照定时扫描已启用 intervalMs=15000 batchSize=20,刷新ACTIVE且快照缺失或过期的钱包"); } @org.springframework.beans.factory.annotation.Autowired @@ -158,28 +159,46 @@ public class RentalWorkers { }); } + /** 仅刷新ACTIVE且缺失/过期的快照;失败不推进快照时间,避免将旧数据伪装成新数据。 */ @Scheduled(fixedDelay = 15000) public void snapshots() { // 按主键轮转,不能让最早20个故障RPC钱包永久阻塞后续供应钱包。 List> rows = store.rows( - "SELECT id FROM rent_wallet WHERE id>? AND status='ACTIVE' AND" + "SELECT id,node_id,network,snapshot_at,snapshot_expires_at FROM rent_wallet WHERE id>? AND status='ACTIVE' AND" + " (snapshot_expires_at IS NULL OR snapshot_expires_at<=?) ORDER BY id" + " LIMIT 20", walletCursor, now()); if (rows.isEmpty()) { + LOG.debug("租赁快照扫描无候选 cursor={},下一轮从头扫描;仅处理ACTIVE且快照过期的钱包", walletCursor); walletCursor = 0; return; } + long batchStarted = System.nanoTime(); + int succeeded = 0; for (Map row : rows) { walletCursor = number(row, "id"); + long started = System.nanoTime(); try { tasks.refresh(walletCursor); + succeeded++; + LOG.info("租赁钱包快照刷新成功 walletId={} nodeId={} network={} previousSnapshotAt={} elapsedMs={}", + walletCursor, row.get("node_id"), row.get("network"), row.get("snapshot_at"), + java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started)); } catch (Exception ex) { - LOG.warn("租赁钱包快照暂不可用,walletId={}", walletCursor); + // 异常原文可能包含RPC地址、认证参数或SQL值,只输出安全因果链和代码位置。 + LOG.warn("租赁钱包快照暂不可用 walletId={} nodeId={} network={} previousSnapshotAt={}" + + " previousExpiresAt={} elapsedMs={} diagnostic={};保留旧快照及原时间,后续轮次重试", + walletCursor, row.get("node_id"), row.get("network"), row.get("snapshot_at"), + row.get("snapshot_expires_at"), + java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started), + RentalDiagnostics.describe(ex)); } } + LOG.info("租赁快照扫描完成 candidates={} succeeded={} failed={} cursor={} elapsedMs={}", + rows.size(), succeeded, rows.size() - succeeded, walletCursor, + java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - batchStarted)); } private void each(String sql, Consumer action) {