添加快照日志

This commit is contained in:
lu7213 committed 2026-11-01 22:43:41 -05:00
1 parent 547a04d12b
commit b84675df3c
4 files changed
+74 -4

No files matched your search

+4
View File
@@ -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执行前还会检查实际创建费、带宽费用及本地留存。
@@ -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,17 +122,36 @@ public class RentalChain {
}
public Map<String, Object> snapshot(String network, String address) {
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}")
@@ -164,6 +187,14 @@ public class RentalChain {
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
@@ -179,6 +210,12 @@ public class RentalChain {
}
}
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;
}
}
/** protobuf省略零字段的空对象表示0;显式错误和非整数绝不能当作0。 */
@@ -50,6 +50,16 @@ public class RentalTasks {
tx(
() -> {
Map<String, Object> 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")
@@ -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<Map<String, Object>> 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<String, Object> 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<Long> action) {