执行器检查逻辑修改
This commit is contained in:
1 parent
f3f4a306e5
commit
f55f4c2961
14 files changed
+298
-26
No files matched your search
@@ -26,6 +26,7 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* 两个进程复用的受控执行引擎:核对业务、实时预算、解锁、构造一次、落盘、广播。
|
||||
@@ -53,6 +54,7 @@ public class ChainCommandExecutor {
|
||||
|
||||
public void execute(String id, String password) {
|
||||
long startedAt = System.currentTimeMillis();
|
||||
AtomicReference<String> stage = new AtomicReference<>("LOAD_COMMAND");
|
||||
// 等待租赁到账不是签名失败,必须在领取RUNNING之前返回,让资源Worker异步推进。
|
||||
ChainCommand pending = store.command(store.get(id));
|
||||
if (pending.rentalRequired && pending.validUntil >= System.currentTimeMillis()) {
|
||||
@@ -74,22 +76,36 @@ public class ChainCommandExecutor {
|
||||
}
|
||||
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={},operation={},network={},授权剩余={}ms", id,
|
||||
command.businessKey, command.type, command.network,
|
||||
Math.max(0L, command.validUntil - System.currentTimeMillis()));
|
||||
WalletAddressPlus wallet = validate(command, stage);
|
||||
log.info("链上任务业务校验通过,executionId={},businessKey={}", id, command.businessKey);
|
||||
stage.set("UNLOCK_WALLET");
|
||||
signing.execute(wallet, password, (tron, api, current) -> {
|
||||
if (!networkName(command.network).equals(tron.type())) {
|
||||
throw new CustomException("执行期间网络发生变化,已拒绝签名");
|
||||
}
|
||||
log.info("链上任务钱包解锁及网络复核通过,executionId={},businessKey={},network={}",
|
||||
id, command.businessKey, command.network);
|
||||
// 使用实时余额和资源,不能信任页面估算或旧数据库快照。
|
||||
stage.set("ESTIMATE_RESOURCES");
|
||||
requireBudget(command, wallet, tron, api);
|
||||
if (command.rentalRequired) resourceGate.require(command);
|
||||
if (command.rentalRequired) {
|
||||
stage.set("VERIFY_RENTED_RESOURCES");
|
||||
resourceGate.require(command);
|
||||
log.info("链上任务租赁资源到账复核通过,executionId={},businessKey={}", id, command.businessKey);
|
||||
}
|
||||
stage.set("BUILD_TRANSACTION");
|
||||
Chain.Transaction unsigned = build(command, tron, api);
|
||||
if (!unsigned.hasRawData() || unsigned.getRawData().getContractCount() != 1) {
|
||||
throw new CustomException("节点未构造有效交易");
|
||||
}
|
||||
log.info("链上交易构造完成,executionId={},businessKey={},contractCount={}", id,
|
||||
command.businessKey, unsigned.getRawData().getContractCount());
|
||||
stage.set("SIGN_TRANSACTION");
|
||||
Chain.Transaction signed = api.signTransaction(unsigned);
|
||||
stage.set("PERSIST_SIGNED_TRANSACTION");
|
||||
store.signed(id, transactionId(signed), signed.toByteArray());
|
||||
log.info("链上交易已签名并持久化,executionId={},businessKey={},transactionId={}", id,
|
||||
command.businessKey, transactionId(signed));
|
||||
@@ -100,11 +116,12 @@ public class ChainCommandExecutor {
|
||||
command.businessKey, System.currentTimeMillis() - startedAt);
|
||||
return null;
|
||||
});
|
||||
} catch (Exception ex) {
|
||||
store.failBeforeSigning(id);
|
||||
} catch (Exception | LinkageError failure) {
|
||||
store.failBeforeSigning(id, stage.get(), failure.getClass().getSimpleName());
|
||||
// LinkageError(例如Protobuf NoSuchMethodError)同样发生在签名前,必须结束RUNNING状态。
|
||||
// 不打印 SDK 异常对象或消息,避免私钥客户端的内部上下文进入日志。
|
||||
log.error("链上任务签名前执行失败,executionId={},异常类型={},耗时={}ms", id,
|
||||
ex.getClass().getSimpleName(), System.currentTimeMillis() - startedAt);
|
||||
log.error("链上任务签名前执行失败,executionId={},阶段={},异常类型={},耗时={}ms", id,
|
||||
stage.get(), failure.getClass().getSimpleName(), System.currentTimeMillis() - startedAt);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,16 +164,20 @@ public class ChainCommandExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
private WalletAddressPlus validate(ChainCommand command) {
|
||||
private WalletAddressPlus validate(ChainCommand command, AtomicReference<String> stage) {
|
||||
stage.set("VALIDATE_EXPIRY");
|
||||
if (command.validUntil < System.currentTimeMillis()) throw new CustomException("执行请求已过期");
|
||||
stage.set("VALIDATE_CONFIGURATION");
|
||||
UsdtConfig config = configs.selectFirst();
|
||||
if (config == null || !config.getVersion().equals(command.envelopeVersion)
|
||||
|| !config.getTronNetType().equals(command.network)) {
|
||||
throw new CustomException("钱包配置或网络已变化,请重新发起操作");
|
||||
}
|
||||
stage.set("VALIDATE_BROADCAST_PERMISSION");
|
||||
if (command.operation == null || !Boolean.TRUE.equals(command.operation.getBroadcast())) {
|
||||
throw new CustomException("本次操作未确认广播");
|
||||
}
|
||||
stage.set("VALIDATE_WALLET");
|
||||
WalletAddressPlus wallet = new WalletAddressPlus();
|
||||
wallet.setId(command.walletId);
|
||||
wallet.setToken(command.address);
|
||||
@@ -174,6 +195,7 @@ public class ChainCommandExecutor {
|
||||
throw new CustomException("钱包已禁用或派生地址不一致");
|
||||
}
|
||||
}
|
||||
stage.set("VALIDATE_BUSINESS_RECORD");
|
||||
validateBusiness(command);
|
||||
return wallet;
|
||||
}
|
||||
@@ -218,6 +240,10 @@ public class ChainCommandExecutor {
|
||||
private void requireBudget(ChainCommand command, WalletAddressPlus wallet, ITronService tron, ApiWrapper api) throws Exception {
|
||||
WalletOperationRequest request = command.operation;
|
||||
WalletResourceEstimateVo estimate = estimates.estimate(command.type, wallet, request);
|
||||
log.info("链上任务资源估算完成,businessKey={},safeEnergy={},energyGap={},safeBandwidth={},"
|
||||
+ "bandwidthGap={},burnRequired={}", command.businessKey,
|
||||
estimate.getSafeEnergy(), estimate.getEnergyGap(), estimate.getSafeBandwidth(),
|
||||
estimate.getBandwidthGap(), estimate.isBurnRequired());
|
||||
if (Boolean.TRUE.equals(request.getResourceOnly()) && estimate.isBurnRequired()) {
|
||||
throw new CustomException("当前资源不足,禁止燃烧 TRX");
|
||||
}
|
||||
@@ -235,12 +261,19 @@ public class ChainCommandExecutor {
|
||||
.add(BigDecimal.valueOf(estimate.getBandwidthGap()).multiply(BigDecimal.valueOf(bandwidthFee))).movePointLeft(6);
|
||||
BigDecimal needed = burn.add(reserve);
|
||||
if ("trx".equals(command.type) || "stake".equals(command.type)) needed = needed.add(request.getAmount());
|
||||
if (tron.getTRxBalance(api, wallet.getToken()).compareTo(needed) < 0) {
|
||||
BigDecimal trxBalance = tron.getTRxBalance(api, wallet.getToken());
|
||||
log.info("链上任务TRX预算复核完成,businessKey={},energyFeeSun={},bandwidthFeeSun={},"
|
||||
+ "burnTrx={},reserveTrx={},neededTrx={},availableTrx={}", command.businessKey,
|
||||
energyFee, bandwidthFee, burn.toPlainString(), reserve.toPlainString(), needed.toPlainString(),
|
||||
trxBalance.toPlainString());
|
||||
if (trxBalance.compareTo(needed) < 0) {
|
||||
throw new CustomException("TRX 余额不足以支付本次操作及资源预算");
|
||||
}
|
||||
if ("usdt".equals(command.type)
|
||||
&& tron.getUsdtBalance(api, wallet.getToken()).compareTo(request.getAmount()) < 0) {
|
||||
throw new CustomException("USDT 余额不足");
|
||||
if ("usdt".equals(command.type)) {
|
||||
BigDecimal usdtBalance = tron.getUsdtBalance(api, wallet.getToken());
|
||||
log.info("链上任务USDT余额复核完成,businessKey={},neededUsdt={},availableUsdt={}",
|
||||
command.businessKey, request.getAmount().toPlainString(), usdtBalance.toPlainString());
|
||||
if (usdtBalance.compareTo(request.getAmount()) < 0) throw new CustomException("USDT 余额不足");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -58,6 +58,22 @@ public class ChainExecutionStore {
|
||||
+ "where execution_id=? and status='QUEUED'", id) == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行器重启后恢复尚未产生任何签名痕迹的远程任务。
|
||||
*
|
||||
* <p>交易广播前必须先通过 {@link #signed(String, String, byte[])} 将状态从 RUNNING 原子更新为
|
||||
* SIGNED。即使旧进程仍在退出,只要本方法先将状态改回 QUEUED,旧进程便无法持久化签名并广播;
|
||||
* 若旧进程先写入 SIGNED,本条件也不会命中。因此该恢复不会产生两笔链上交易。</p>
|
||||
*
|
||||
* @return 恢复为 QUEUED 的任务数量
|
||||
*/
|
||||
public int requeueUnsignedRemoteExecutions() {
|
||||
return jdbc.update("update usdt_chain_execution set status='QUEUED',"
|
||||
+ "error_message='执行器重启,未签名远程任务已重新排队',updated_at=CURRENT_TIMESTAMP "
|
||||
+ "where status='RUNNING' and execution_mode='REMOTE' "
|
||||
+ "and transaction_id is null and signed_transaction is null");
|
||||
}
|
||||
|
||||
public void signed(String id, String hash, byte[] transaction) {
|
||||
if (jdbc.update("update usdt_chain_execution set status='SIGNED',transaction_id=?,signed_transaction=?,"
|
||||
+ "updated_at=CURRENT_TIMESTAMP where execution_id=? and status='RUNNING'",
|
||||
@@ -72,9 +88,17 @@ public class ChainExecutionStore {
|
||||
}
|
||||
|
||||
public void failBeforeSigning(String id) {
|
||||
// 节点、SDK 异常可能携带敏感上下文,只记录固定文案;SIGNED 永远不改成可重试失败。
|
||||
jdbc.update("update usdt_chain_execution set status='FAILED',error_message='执行前校验或签名失败,请检查配置、余额和资源',"
|
||||
+ "updated_at=CURRENT_TIMESTAMP where execution_id=? and status='RUNNING'", id);
|
||||
failBeforeSigning(id, "UNKNOWN", "UnknownException");
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅记录受控阶段和异常类型,不落库 SDK 异常消息,避免节点响应或钱包上下文泄露。
|
||||
* SIGNED 状态永远不会被此方法改成可重新执行的失败状态。
|
||||
*/
|
||||
public void failBeforeSigning(String id, String stage, String errorType) {
|
||||
String diagnostic = "执行前失败,阶段=" + safeDiagnostic(stage) + ",异常类型=" + safeDiagnostic(errorType);
|
||||
jdbc.update("update usdt_chain_execution set status='FAILED',error_message=?,"
|
||||
+ "updated_at=CURRENT_TIMESTAMP where execution_id=? and status='RUNNING'", diagnostic, id);
|
||||
}
|
||||
|
||||
/** 仅供原代付链上确认流程调用;已确认失败的交易允许下一次人工执行尝试。 */
|
||||
@@ -110,4 +134,9 @@ public class ChainExecutionStore {
|
||||
throw new CustomException("执行指令序列化失败");
|
||||
}
|
||||
}
|
||||
|
||||
private String safeDiagnostic(String value) {
|
||||
if (value == null || !value.matches("[A-Za-z0-9_.$-]{1,80}")) return "UNKNOWN";
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -10,17 +10,20 @@ import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.client.RestClientResponseException;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.net.URI;
|
||||
import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/** 内网调用客户端。仅允许内网字面 IP;禁用 HTTP 重定向,避免凭据被转发到其他主机。 */
|
||||
@Component
|
||||
@@ -36,6 +39,10 @@ public class ChainExecutorClient {
|
||||
private String tlsCertificateLocation;
|
||||
@Value("${chain.executor.health-failure-threshold:3}")
|
||||
private int healthFailureThreshold;
|
||||
@Value("${chain.executor.health-check-interval-ms:10000}")
|
||||
private long healthCheckIntervalMs;
|
||||
@Value("${chain.executor.health-check-initial-delay-ms:10000}")
|
||||
private long healthCheckInitialDelayMs;
|
||||
@Autowired(required = false)
|
||||
private ChainExecutorRuntimeState runtimeState;
|
||||
private RestTemplate http;
|
||||
@@ -45,6 +52,7 @@ public class ChainExecutorClient {
|
||||
private volatile int consecutiveFailures;
|
||||
private volatile long checkedAt;
|
||||
private volatile String lastDiagnostic = "NOT_CHECKED";
|
||||
private ScheduledExecutorService healthScheduler;
|
||||
|
||||
public ChainExecutorClient() {
|
||||
http = createHttp(null);
|
||||
@@ -104,6 +112,7 @@ public class ChainExecutorClient {
|
||||
log.error("内网链上执行器启动验证未通过,支付系统继续启动,远程链上操作暂不可用,诊断={}",
|
||||
unavailable.getMessage());
|
||||
}
|
||||
startHealthMonitor();
|
||||
}
|
||||
|
||||
public boolean isEnabled() { return enabled; }
|
||||
@@ -126,16 +135,24 @@ public class ChainExecutorClient {
|
||||
}
|
||||
|
||||
/** 后台持续探测,即使已自动停用也继续更新“已恢复、待管理员启用”状态。 */
|
||||
@Scheduled(fixedDelayString = "${chain.executor.health-check-interval-ms:10000}", initialDelayString = "${chain.executor.health-check-initial-delay-ms:10000}")
|
||||
public void monitor() {
|
||||
if (!enabled || !checking.compareAndSet(false, true)) return;
|
||||
try {
|
||||
probeNow();
|
||||
} catch (Exception failure) {
|
||||
// 避免状态持久化等外围异常终止后续固定延迟探测。
|
||||
log.error("内网链上执行器后台探测异常,后续仍将继续检查,异常类型={}", rootType(failure));
|
||||
} finally {
|
||||
checking.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
/** 页面状态读取同时触发实时探测,“立即检查”不得只返回进程内旧缓存。 */
|
||||
public Map<String, Object> refreshAvailability() {
|
||||
monitor();
|
||||
return availability();
|
||||
}
|
||||
|
||||
/** 只有实时探测可用时才允许管理员重新打开,避免误启用后立即接受远程任务。 */
|
||||
public void enableRuntime(String operator) {
|
||||
if (!probeNow()) throw new com.ruoyi.common.exception.CustomException("执行器尚未恢复,无法启用");
|
||||
@@ -227,6 +244,30 @@ public class ChainExecutorClient {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用独立守护线程执行健康检查,不依赖资源租赁模块是否启用Spring全局调度。
|
||||
*/
|
||||
private synchronized void startHealthMonitor() {
|
||||
if (!enabled || healthScheduler != null) return;
|
||||
healthScheduler = Executors.newSingleThreadScheduledExecutor(runnable -> {
|
||||
Thread thread = new Thread(runnable, "chain-executor-health-monitor");
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
});
|
||||
healthScheduler.scheduleWithFixedDelay(this::monitor, Math.max(0L, healthCheckInitialDelayMs),
|
||||
Math.max(1000L, healthCheckIntervalMs), TimeUnit.MILLISECONDS);
|
||||
log.info("内网链上执行器后台健康检查已启动,首次延迟={}ms,检查间隔={}ms",
|
||||
Math.max(0L, healthCheckInitialDelayMs), Math.max(1000L, healthCheckIntervalMs));
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public synchronized void closeHealthMonitor() {
|
||||
if (healthScheduler == null) return;
|
||||
healthScheduler.shutdownNow();
|
||||
healthScheduler = null;
|
||||
log.info("内网链上执行器后台健康检查已停止");
|
||||
}
|
||||
|
||||
private static String rootType(Throwable failure) {
|
||||
Throwable cause = failure;
|
||||
while (cause.getCause() != null && cause.getCause() != cause) cause = cause.getCause();
|
||||
|
||||
@@ -32,7 +32,8 @@ public class ChainExecutorController {
|
||||
+ "or @ss.hasPermi('system:usdtTaskItem:edit') or @ss.hasPermi('system:usdtPayoutOrder:execute')")
|
||||
@GetMapping("/status")
|
||||
public AjaxResult status() {
|
||||
Map<String, Object> result = new LinkedHashMap<>(client.availability());
|
||||
// 页面轮询和“立即检查”都执行实时探测,不能只返回进程内缓存状态。
|
||||
Map<String, Object> result = new LinkedHashMap<>(client.refreshAvailability());
|
||||
// 明确列出公开字段,禁止 SELECT * 结果进入 HTTP 响应。
|
||||
boolean rentalEnabled = rentalProperties != null && rentalProperties.isEnabled();
|
||||
result.put("executions", client.isEnabled() ? store.jdbc().queryForList(rentalEnabled
|
||||
|
||||
Reference in new issue
Block a user