执行器检查逻辑修改

This commit is contained in:
lu7213 committed 2026-11-07 03:23:30 -05:00
1 parent f3f4a306e5
commit f55f4c2961
14 files changed
+298 -26

No files matched your search

+14 -1
View File
@@ -6,7 +6,20 @@
<artifactId>usdt-chain-executor</artifactId>
<description>仅内网调用的独立链上执行进程</description>
<dependencies>
<dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-system</artifactId></dependency>
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-system</artifactId>
<!--
执行器可能在服务器上单模块构建,此时 Maven 会读取本地仓库中的旧版
ruoyi-system POM。这里再次排除 javalite,确保不依赖多模块构建顺序。
-->
<exclusions>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-javalite</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.0</version></dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.30</version></dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency>
@@ -43,11 +43,12 @@ public class ChainExecutorApplication {
// 配置错误只输出固定提示;Spring 启动异常可能包含 JDBC URL 等配置,不打印异常对象。
try {
log.info("USDT链上执行器正在启动,运行模式=独立内网执行进程");
ProtobufRuntimeValidator.validate();
new SpringApplicationBuilder(ChainExecutorApplication.class).web(WebApplicationType.NONE)
.properties("spring.config.name=executor").run(args);
log.info("USDT链上执行器启动完成");
} catch (Exception ex) {
log.error("USDT链上执行器启动失败,异常类型={}", ex.getClass().getSimpleName());
log.error("USDT链上执行器启动失败,异常类型={},msg {}", ex.getClass().getSimpleName(), ex.getMessage());
System.err.println("内网执行服务启动失败,请检查内网监听、白名单、环境变量、数据库及钱包密码配置");
System.exit(1);
}
@@ -89,6 +89,12 @@ public class IntranetExecutionServer {
// 启动校验表结构和解密能力,不执行任何链上广播。
store.jdbc().queryForObject("select count(*) from usdt_chain_execution where 1=0", Long.class);
checkReady();
int requeued = store.requeueUnsignedRemoteExecutions();
if (requeued > 0) {
notified = true;
log.warn("执行器启动时恢复未签名远程任务,重新排队数量={};任务将重新校验有效期、余额和资源", requeued);
}
warnUnfinishedRunningTasks();
log.info("启动检查通过,网络={},允许来源数量={},广播已启用", network, allowed.size());
InetSocketAddress address = new InetSocketAddress(bind, Integer.parseInt(required("CHAIN_EXECUTOR_PORT")));
// 执行器只提供HTTP。需要HTTPS时由同机Nginx终止TLS并转发到回环监听端口。
@@ -160,6 +166,23 @@ public class IntranetExecutionServer {
signing.validateConfigurationPassword(password);
}
/**
* RUNNING 不允许自动重领,因为旧进程可能已完成签名但尚未来得及更新状态。
* 启动时输出受控诊断,便于管理员结合交易哈希和签名字节判断是否可以人工置为失败。
*/
private void warnUnfinishedRunningTasks() {
List<Map<String, Object>> rows = store.jdbc().queryForList(
"select execution_id,business_key,transaction_id,signed_transaction,updated_at "
+ "from usdt_chain_execution where status='RUNNING' order by updated_at limit 20");
if (rows.isEmpty()) return;
log.warn("发现未完成的RUNNING链上任务,数量={};系统不会自动重领,请人工核验是否已签名或广播", rows.size());
for (Map<String, Object> row : rows) {
log.warn("未完成链上任务,executionId={},businessKey={},updatedAt={},hasTransactionId={},hasSignedTransaction={}",
row.get("execution_id"), row.get("business_key"), row.get("updated_at"),
row.get("transaction_id") != null, row.get("signed_transaction") != null);
}
}
@Scheduled(fixedDelay = 200, initialDelay = 2000)
public void process() {
if (!notified && System.currentTimeMillis() < nextScanAt) return;
@@ -197,9 +220,10 @@ public class IntranetExecutionServer {
}
if (!rows.isEmpty()) log.info("链上任务批次处理结束,执行数量={},恢复数量={},跳过数量={},耗时={}ms",
executed, recovered, skipped, System.currentTimeMillis() - startedAt);
} catch (Exception failure) {
} catch (Exception | LinkageError failure) {
// 固定延迟补扫;不输出密码、信封或 SDK 异常。恢复后继续已有任务。
log.error("链上任务扫描失败,将按固定延迟重试,异常类型={}", failure.getClass().getSimpleName());
log.error("链上任务扫描失败,将按固定延迟重试,异常类型={},已处理任务由任务级状态机记录结果",
failure.getClass().getSimpleName());
} finally {
processing.set(false);
}
@@ -0,0 +1,62 @@
package com.ruoyi.executor;
import com.google.protobuf.Any;
import com.google.protobuf.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Method;
import java.net.URL;
/**
* 校验执行器实际加载的 Protobuf 运行时是否兼容 TRON Trident。
*
* <p>protobuf-java 与 protobuf-javalite 包含若干完全相同的类名,但方法签名不同。
* 若部署包或外部 classpath 同时包含两者,应用可以正常启动,却会在首次构造交易时
* 抛出 {@link NoSuchMethodError}。因此必须在接收链上任务之前完成 ABI 校验。</p>
*/
final class ProtobufRuntimeValidator {
private static final Logger LOG = LoggerFactory.getLogger(ProtobufRuntimeValidator.class);
private ProtobufRuntimeValidator() {
}
/**
* Trident 0.10.0 调用 Any.pack(Message),该方法仅由完整 protobuf-java 提供。
*/
static void validate() {
try {
Any.class.getMethod("pack", Message.class);
} catch (NoSuchMethodException ex) {
throw new IllegalStateException("Protobuf运行时与TRON Trident不兼容,请移除protobuf-javalite或旧版protobuf-java;"
+ "Any类加载位置=" + sourceLocation() + ";检测到的pack签名=" + packSignatures(), ex);
}
Package runtimePackage = Any.class.getPackage();
String implementationVersion = runtimePackage == null ? null : runtimePackage.getImplementationVersion();
LOG.info("Protobuf运行时兼容性检查通过,版本={},加载位置={}",
implementationVersion == null ? "unknown" : implementationVersion,
sourceLocation());
}
private static String sourceLocation() {
URL source = Any.class.getProtectionDomain() == null
|| Any.class.getProtectionDomain().getCodeSource() == null
? null : Any.class.getProtectionDomain().getCodeSource().getLocation();
return source == null ? "unknown" : source.toExternalForm();
}
private static String packSignatures() {
StringBuilder signatures = new StringBuilder();
for (Method method : Any.class.getMethods()) {
if (!"pack".equals(method.getName())) {
continue;
}
if (signatures.length() > 0) {
signatures.append(',');
}
signatures.append(method.toGenericString());
}
return signatures.length() == 0 ? "none" : signatures.toString();
}
}
@@ -75,7 +75,9 @@ public class ExecutorBootstrapTest {
public static class Probe {
public static void main(String[] args) throws Exception {
JdbcTemplate jdbc = new JdbcTemplate(new DriverManagerDataSource(JDBC, "sa", ""));
jdbc.execute("create table usdt_chain_execution (execution_id varchar(36), status varchar(16), execution_mode varchar(16), created_at timestamp)");
jdbc.execute("create table usdt_chain_execution (execution_id varchar(36),business_key varchar(160),"
+ "status varchar(16),execution_mode varchar(16),transaction_id varchar(128),"
+ "signed_transaction blob,error_message varchar(512),created_at timestamp,updated_at timestamp)");
jdbc.execute("create table usdt_config (id bigint, pay_usdt_num decimal, receive_usdt_num decimal, tron_net_type int, "
+ "version bigint, encryption_algorithm varchar, password_kdf varchar, iterations bigint, encrypted_mnemonic varchar, "
+ "mnemonic_nonce varchar, encrypted_dek varchar, dek_nonce varchar, key_salt varchar, password_salt varchar, "
@@ -79,7 +79,9 @@ public class IntranetExecutionServerTest {
environment.put("CHAIN_EXECUTOR_BROADCAST_ENABLED", "true");
ChainExecutionStore store = new ChainExecutionStore(new DriverManagerDataSource(
"jdbc:h2:mem:server" + System.nanoTime() + ";DB_CLOSE_DELAY=-1", "sa", ""));
store.jdbc().execute("create table usdt_chain_execution(id int)");
store.jdbc().execute("create table usdt_chain_execution(id int,execution_id varchar(36),business_key varchar(160),"
+ "status varchar(16),execution_mode varchar(16),transaction_id varchar(128),signed_transaction blob,"
+ "error_message varchar(512),updated_at timestamp)");
UsdtConfig config = new UsdtConfig();
config.setTronNetType(0);
UsdtConfigMapper configs = (UsdtConfigMapper) Proxy.newProxyInstance(getClass().getClassLoader(),
@@ -0,0 +1,19 @@
package com.ruoyi.executor;
import com.google.protobuf.Any;
import com.google.protobuf.Message;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
/**
* 防止执行器依赖调整时再次混入 protobuf-javalite,导致 Trident 构造交易失败。
*/
public class ProtobufRuntimeValidatorTest {
@Test
public void runtimeProvidesFullProtobufApiRequiredByTrident() throws Exception {
ProtobufRuntimeValidator.validate();
assertNotNull(Any.class.getMethod("pack", Message.class));
}
}