diff --git a/docs/chain-executor.md b/docs/chain-executor.md index 6a302a2..aa7e5db 100644 --- a/docs/chain-executor.md +++ b/docs/chain-executor.md @@ -9,7 +9,8 @@ 支付后端 `CHAIN_EXECUTOR_ENABLED` 默认 `false`,此时直接执行原密码代码,既不访问内网服务,也不依赖新增表。 启用后,支付后端启动时先调用 `GET /internal/v1/connectivity` 验证传输链路、来源白名单和服务令牌, -再调用 `GET /internal/v1/readiness` 验证数据库钱包网络和配置密码。两项必须同时通过,否则支付后端拒绝启动并输出受控诊断日志。 +再调用 `GET /internal/v1/readiness` 验证数据库钱包网络和配置密码。两项通过时输出服务可用日志;暂时不可用时 +支付系统继续启动并输出受控诊断,但所有无密码远程链上操作仍会被拒绝。URL、协议或令牌长度配置非法时仍拒绝启动。 启用后,用户输入密码时本地执行,未输入密码时仅允许交给已就绪的内网服务。两种方式共用执行记录和受控交易构造, 执行前实时校验余额、资源和链上费率。`RESOURCE_ONLY` 缺口拒绝;`DIRECT` 检查燃烧预算及全局 `trx_num` 保留量。 页面仍要求明确确认广播;服务上线不会触发历史业务自动广播。 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/chain/ChainExecutorClient.java b/ruoyi-system/src/main/java/com/ruoyi/system/chain/ChainExecutorClient.java index 2e26ee2..56feb33 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/chain/ChainExecutorClient.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/chain/ChainExecutorClient.java @@ -7,6 +7,7 @@ import org.springframework.http.HttpMethod; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; +import org.springframework.web.client.RestClientResponseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,9 +55,15 @@ public class ChainExecutorClient { throw new IllegalArgumentException("内网执行服务需配置内网 HTTP/HTTPS 根地址和至少32位服务凭据"); } log.info("内网链上执行通道已启用,开始验证执行器服务,传输协议={}", target.getScheme().toUpperCase()); - requireResponse("/internal/v1/connectivity", "CONNECTED", "联通测试"); - requireResponse("/internal/v1/readiness", "READY", "业务就绪检查"); - log.info("内网链上执行器服务验证通过,联通状态=CONNECTED,业务状态=READY"); + try { + requireResponse("/internal/v1/connectivity", "CONNECTED", "联通测试"); + requireResponse("/internal/v1/readiness", "READY", "业务就绪检查"); + log.info("内网链上执行器服务验证通过,联通状态=CONNECTED,业务状态=READY"); + } catch (IllegalStateException unavailable) { + // 执行器可能与支付系统存在短暂启动顺序差异。保留支付管理能力,实际远程执行仍由ready()拒绝。 + log.error("内网链上执行器启动验证未通过,支付系统继续启动,远程链上操作暂不可用,诊断={}", + unavailable.getMessage()); + } } public boolean isEnabled() { return enabled; } @@ -67,7 +74,7 @@ public class ChainExecutorClient { return "READY".equals(http.exchange(url + "/internal/v1/readiness", HttpMethod.GET, new HttpEntity(headers()), String.class).getBody()); } catch (Exception failure) { - log.warn("内网链上执行器业务就绪检查失败,异常类型={}", failure.getClass().getSimpleName()); + log.warn("内网链上执行器业务就绪检查失败,诊断={}", diagnostic(failure)); return false; } } @@ -79,8 +86,7 @@ public class ChainExecutorClient { log.info("已通知内网链上执行器扫描远程任务"); } catch (Exception failure) { // 数据库已保存任务;通知失败由执行器后台补扫,不能降级成另一笔密码交易。 - log.warn("通知内网链上执行器失败,任务已持久化并等待执行器补扫,异常类型={}", - failure.getClass().getSimpleName()); + log.warn("通知内网链上执行器失败,任务已持久化并等待执行器补扫,诊断={}", diagnostic(failure)); } } @@ -92,11 +98,22 @@ public class ChainExecutorClient { if (!expected.equals(body)) throw new IllegalStateException("响应内容不符合执行器协议"); log.info("内网链上执行器{}通过", stage); } catch (Exception failure) { - log.error("内网链上执行器{}失败,异常类型={}", stage, failure.getClass().getSimpleName()); - throw new IllegalStateException("内网链上执行器" + stage + "失败,请检查服务、TLS、白名单、令牌和钱包配置"); + String diagnostic = diagnostic(failure); + log.error("内网链上执行器{}失败,诊断={}", stage, diagnostic); + throw new IllegalStateException(stage + "失败[" + diagnostic + "]"); } } + /** 仅保留异常类型或HTTP状态,避免URL、令牌及底层响应正文进入日志。 */ + private static String diagnostic(Exception failure) { + if (failure instanceof RestClientResponseException) { + return "HTTP_" + ((RestClientResponseException) failure).getRawStatusCode(); + } + Throwable cause = failure; + while (cause.getCause() != null && cause.getCause() != cause) cause = cause.getCause(); + return cause.getClass().getSimpleName(); + } + private HttpHeaders headers() { HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", "Bearer " + token); diff --git a/ruoyi-system/src/test/java/com/ruoyi/system/chain/ChainExecutorClientTest.java b/ruoyi-system/src/test/java/com/ruoyi/system/chain/ChainExecutorClientTest.java new file mode 100644 index 0000000..8c133ac --- /dev/null +++ b/ruoyi-system/src/test/java/com/ruoyi/system/chain/ChainExecutorClientTest.java @@ -0,0 +1,41 @@ +package com.ruoyi.system.chain; + +import org.junit.Test; + +import java.lang.reflect.Field; + +import static org.junit.Assert.fail; + +/** 启动联通失败只降低远程执行能力,配置格式错误仍须阻止启动。 */ +public class ChainExecutorClientTest { + @Test + public void unavailableExecutorDoesNotPreventPaymentSystemStartup() throws Exception { + ChainExecutorClient client = client("http://127.0.0.1:1"); + client.validate(); + } + + @Test + public void invalidExecutorUrlStillPreventsStartup() throws Exception { + ChainExecutorClient client = client("http://example.com:9088"); + try { + client.validate(); + fail("域名地址应在发起请求前被拒绝"); + } catch (IllegalArgumentException expected) { + // 只允许内网字面IP,避免DNS重绑定或误连公网。 + } + } + + private ChainExecutorClient client(String url) throws Exception { + ChainExecutorClient client = new ChainExecutorClient(); + set(client, "enabled", true); + set(client, "url", url); + set(client, "token", "test-only-token-012345678901234567890"); + return client; + } + + private void set(Object target, String name, Object value) throws Exception { + Field field = target.getClass().getDeclaredField(name); + field.setAccessible(true); + field.set(target, value); + } +}