usdt-chain-executor 添加联通测试接口,如果CHAIN\_EXECUTOR\_ENABLED=true ,在系统启动时,添加调用验证,并打印调用日志,作为服务可用的日志

This commit is contained in:
lu7213 committed 2026-11-06 20:24:08 -05:00
1 parent 5ca2cc5202
commit ee9b209e3d
10 files changed
+166 -24

No files matched your search

@@ -11,6 +11,8 @@ import com.ruoyi.system.service.TronServiceProvider;
import com.ruoyi.system.service.WalletOperationEstimateService;
import com.ruoyi.system.service.WalletSigningService;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tron.trident.abi.datatypes.Address;
import org.tron.trident.abi.datatypes.Function;
import org.tron.trident.abi.datatypes.generated.Uint256;
@@ -31,6 +33,7 @@ import java.util.Map;
*/
@Service
public class ChainCommandExecutor {
private static final Logger log = LoggerFactory.getLogger(ChainCommandExecutor.class);
@org.springframework.beans.factory.annotation.Autowired(required = false)
private com.ruoyi.system.chain.resource.ResourceExecutionGate resourceGate;
private final ChainExecutionStore store;
@@ -49,20 +52,32 @@ public class ChainCommandExecutor {
}
public void execute(String id, String password) {
long startedAt = System.currentTimeMillis();
// 等待租赁到账不是签名失败,必须在领取RUNNING之前返回,让资源Worker异步推进。
ChainCommand pending = store.command(store.get(id));
if (pending.rentalRequired && pending.validUntil >= System.currentTimeMillis()) {
try {
if (resourceGate == null || !resourceGate.ready(pending)) {
log.info("链上任务等待租赁资源到账,executionId={},businessKey={}", id, pending.businessKey);
failPasswordPreparation(id);
return;
}
} catch (Exception temporarilyUnavailable) { failPasswordPreparation(id); return; }
} catch (Exception temporarilyUnavailable) {
log.warn("链上任务资源复检暂不可用,executionId={},businessKey={},异常类型={}", id,
pending.businessKey, temporarilyUnavailable.getClass().getSimpleName());
failPasswordPreparation(id); return;
}
}
if (!store.claim(id)) {
log.debug("链上任务未领取,可能已由其他执行线程处理,executionId={}", id);
return;
}
if (!store.claim(id)) return;
try {
ChainCommand command = store.command(store.get(id));
log.info("链上任务已领取,executionId={},businessKey={},operation={},network={}", id,
command.businessKey, command.type, command.network);
WalletAddressPlus wallet = validate(command);
log.info("链上任务业务校验通过,executionId={},businessKey={}", id, command.businessKey);
signing.execute(wallet, password, (tron, api, current) -> {
if (!networkName(command.network).equals(tron.type())) {
throw new CustomException("执行期间网络发生变化,已拒绝签名");
@@ -76,14 +91,20 @@ public class ChainCommandExecutor {
}
Chain.Transaction signed = api.signTransaction(unsigned);
store.signed(id, transactionId(signed), signed.toByteArray());
log.info("链上交易已签名并持久化,executionId={},businessKey={},transactionId={}", id,
command.businessKey, transactionId(signed));
// 同步签名哈希用于地址占用恢复;此后未知结果不能重新租赁或重新签名。
if (command.rentalRequired) resourceGate.transferred(command.businessKey, transactionId(signed));
broadcast(id, signed, api);
log.info("链上任务广播流程结束,executionId={},businessKey={},耗时={}ms", id,
command.businessKey, System.currentTimeMillis() - startedAt);
return null;
});
} catch (Exception ex) {
store.failBeforeSigning(id);
// 不打印 SDK 异常对象或消息,避免私钥客户端的内部上下文进入日志。
log.error("链上任务签名前执行失败,executionId={},异常类型={},耗时={}ms", id,
ex.getClass().getSimpleName(), System.currentTimeMillis() - startedAt);
}
}
@@ -100,12 +121,15 @@ public class ChainCommandExecutor {
try {
String id = (String) row.get("execution_id");
String hash = (String) row.get("transaction_id");
log.info("开始恢复已签名交易,executionId={},businessKey={},transactionId={}", id,
command.businessKey, hash);
// 签名已落库但准备状态同步前宕机时,恢复地址占用与哈希关联。
if (command.rentalRequired && resourceGate != null) resourceGate.transferred(command.businessKey, hash);
try {
Chain.Transaction found = api.getTransactionById(hash);
if (found != null && found.hasRawData()) {
store.broadcasted(id);
log.info("恢复任务已在链上查到交易,executionId={},transactionId={}", id, hash);
return;
}
} catch (Exception ignored) {
@@ -114,8 +138,10 @@ public class ChainCommandExecutor {
Chain.Transaction signed = Chain.Transaction.parseFrom((byte[]) row.get("signed_transaction"));
if (!transactionId(signed).equals(hash)) throw new IllegalStateException("签名记录不一致");
broadcast(id, signed, api);
} catch (Exception ignored) {
} catch (Exception failure) {
// 保持 SIGNED,等待下次查询;不能将不确定交易改成可重新转账的 FAILED。
log.warn("恢复已签名交易未完成,将保留SIGNED重试,executionId={},异常类型={}",
row.get("execution_id"), failure.getClass().getSimpleName());
} finally {
api.close();
}
@@ -247,17 +273,24 @@ public class ChainCommandExecutor {
}
private void broadcast(String id, Chain.Transaction signed, ApiWrapper api) {
String transactionId = transactionId(signed);
for (int attempt = 0; attempt < 4; attempt++) {
try {
String returned = api.broadcastTransaction(signed);
if (transactionId(signed).equalsIgnoreCase(returned)) {
if (transactionId.equalsIgnoreCase(returned)) {
store.broadcasted(id);
log.info("链上交易广播成功,executionId={},transactionId={},attempt={}", id,
transactionId, attempt + 1);
return;
}
} catch (Exception ignored) {
log.warn("节点返回的交易哈希不匹配,executionId={},attempt={}", id, attempt + 1);
} catch (Exception failure) {
// 包含首次共四次,始终广播同一笔交易;失败后由恢复任务查询。
log.warn("链上交易广播失败,将重试同一签名交易,executionId={},attempt={},异常类型={}",
id, attempt + 1, failure.getClass().getSimpleName());
}
}
log.warn("链上交易广播结果仍不确定,保留SIGNED等待恢复,executionId={},transactionId={}", id, transactionId);
}
public static String networkName(Integer network) {
@@ -7,6 +7,8 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import java.net.URI;
@@ -14,6 +16,7 @@ import java.net.URI;
/** 内网调用客户端。仅允许内网字面 IP;禁用 HTTP 重定向,避免凭据被转发到其他主机。 */
@Component
public class ChainExecutorClient {
private static final Logger log = LoggerFactory.getLogger(ChainExecutorClient.class);
@Value("${chain.executor.enabled:false}")
private boolean enabled;
@Value("${chain.executor.url:}")
@@ -38,7 +41,10 @@ public class ChainExecutorClient {
@PostConstruct
public void validate() {
if (!enabled) return;
if (!enabled) {
log.info("内网链上执行通道未启用,启动时跳过执行器联通验证");
return;
}
URI target = URI.create(url);
java.net.InetAddress address = IntranetAddress.parse(target.getHost());
if (target.getRawUserInfo() != null || target.getRawQuery() != null || target.getRawFragment() != null
@@ -47,6 +53,10 @@ public class ChainExecutorClient {
|| token == null || token.length() < 32) {
throw new IllegalArgumentException("内网执行服务需配置内网 HTTPS 根地址和至少32位服务凭据;仅回环允许 HTTP");
}
log.info("内网链上执行通道已启用,开始验证执行器服务");
requireResponse("/internal/v1/connectivity", "CONNECTED", "联通测试");
requireResponse("/internal/v1/readiness", "READY", "业务就绪检查");
log.info("内网链上执行器服务验证通过,联通状态=CONNECTED,业务状态=READY");
}
public boolean isEnabled() { return enabled; }
@@ -56,7 +66,8 @@ public class ChainExecutorClient {
try {
return "READY".equals(http.exchange(url + "/internal/v1/readiness", HttpMethod.GET,
new HttpEntity<Void>(headers()), String.class).getBody());
} catch (Exception ignored) {
} catch (Exception failure) {
log.warn("内网链上执行器业务就绪检查失败,异常类型={}", failure.getClass().getSimpleName());
return false;
}
}
@@ -65,8 +76,24 @@ public class ChainExecutorClient {
try {
http.exchange(url + "/internal/v1/executions", HttpMethod.POST,
new HttpEntity<String>("", headers()), String.class);
} catch (Exception ignored) {
log.info("已通知内网链上执行器扫描远程任务");
} catch (Exception failure) {
// 数据库已保存任务;通知失败由执行器后台补扫,不能降级成另一笔密码交易。
log.warn("通知内网链上执行器失败,任务已持久化并等待执行器补扫,异常类型={}",
failure.getClass().getSimpleName());
}
}
/** 启动验证必须得到协议约定的精确响应,代理错误页或其他服务的 200 响应不能视为可用。 */
private void requireResponse(String path, String expected, String stage) {
try {
String body = http.exchange(url + path, HttpMethod.GET,
new HttpEntity<Void>(headers()), String.class).getBody();
if (!expected.equals(body)) throw new IllegalStateException("响应内容不符合执行器协议");
log.info("内网链上执行器{}通过", stage);
} catch (Exception failure) {
log.error("内网链上执行器{}失败,异常类型={}", stage, failure.getClass().getSimpleName());
throw new IllegalStateException("内网链上执行器" + stage + "失败,请检查服务、TLS、白名单、令牌和钱包配置");
}
}