固定转入 1 TRX,费用为“本金折价 USDT + 固定服务费”。
下单冻结资金,链上独立确认后扣款并记录供应商收入。 支持 NILE、主网独立配置;未知交易不退款、不重复构建。 已补充清晰注释、分阶段脱敏日志和已确认交易哈希展示。
This commit is contained in:
1 parent
1286f0df81
commit
fbc72b5b57
24 files changed
+851
-27
No files matched your search
@@ -11,6 +11,7 @@ import java.util.function.Supplier;
|
||||
|
||||
/** 持久任务状态机。数据库事务仅修改事实,所有RPC在事务外完成。 UNKNOWN保留三个占位和冻结资金;租约、断线、超时均不是未执行证据。 */
|
||||
@Service
|
||||
@lombok.extern.slf4j.Slf4j
|
||||
public class RentalTasks {
|
||||
private final RentalStore store;
|
||||
private final RentalChain chain;
|
||||
@@ -78,6 +79,8 @@ public class RentalTasks {
|
||||
funds.release(row);
|
||||
store.update("order", orderId, map("status", "CANCELLED"));
|
||||
orders.webhook(row, "CANCELLED", "ORDER:" + orderId + ":CANCELLED");
|
||||
log.info("租赁未派单超时释放 orderId={} orderType={} releasedUsdt={}",
|
||||
orderId, row.get("order_type"), row.get("sale_amount"));
|
||||
}
|
||||
return null;
|
||||
});
|
||||
@@ -98,6 +101,12 @@ public class RentalTasks {
|
||||
if (owner.equals(text(initial, "receiver_address"))) {
|
||||
continue;
|
||||
}
|
||||
if (RentalOnchain.isOrder(initial)) {
|
||||
if (dispatchOnchain(initial, candidate)) {
|
||||
return;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (chain.relation(
|
||||
text(initial, "network"),
|
||||
owner,
|
||||
@@ -209,6 +218,49 @@ public class RentalTasks {
|
||||
}
|
||||
}
|
||||
|
||||
/** TRX 任务独立占用钱包 TRX 执行槽,不占用质押资源,也不建立委托地址对及历史资源基线。 */
|
||||
private boolean dispatchOnchain(Map<String, Object> initial, Map<String, Object> candidate) {
|
||||
Map<String, Object> advertised = store.get("node", number(candidate, "node_id"), false);
|
||||
if (number(advertised, "transfer_enabled") != 1) {
|
||||
log.debug("地址上链候选未授权转账 orderId={} nodeId={}", initial.get("id"), advertised.get("id"));
|
||||
return false;
|
||||
}
|
||||
Map<String, Object> price;
|
||||
try {
|
||||
price = new com.fasterxml.jackson.databind.ObjectMapper().readValue(text(initial, "package_snapshot"), Map.class);
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalArgumentException("地址上链费用快照损坏");
|
||||
}
|
||||
// 仅作候选过滤,Agent 在签名前仍需重新检查余额、激活成本、费率及本地留存。
|
||||
long balance = chain.call(text(initial, "network"), "/wallet/getaccount",
|
||||
map("address", text(candidate, "address"), "visible", true)).path("balance").asLong();
|
||||
if (balance < Math.addExact(RentalOnchain.TRANSFER_SUN, number(price, "fee_limit_sun"))) {
|
||||
log.debug("地址上链候选余额不足 orderId={} walletId={} balanceSun={}", initial.get("id"), candidate.get("id"), balance);
|
||||
return false;
|
||||
}
|
||||
return Boolean.TRUE.equals(tx(() -> {
|
||||
Map<String, Object> order = store.get("order", number(initial, "id"), true);
|
||||
Map<String, Object> wallet = store.get("wallet", number(candidate, "id"), true);
|
||||
Map<String, Object> node = store.get("node", number(wallet, "node_id"), true);
|
||||
if (!"WAITING".equals(text(order, "status")) || !time(order.get("dispatch_deadline")).isAfter(Instant.now())
|
||||
|| !"ACTIVE".equals(text(wallet, "status")) || !"ACTIVE".equals(text(node, "status"))
|
||||
|| number(node, "transfer_enabled") != 1 || !"ONLINE".equals(text(node, "connection_status"))
|
||||
|| !time(node.get("heartbeat_expires_at")).isAfter(Instant.now())
|
||||
|| store.one("SELECT id FROM rent_task WHERE wallet_id=? AND resource_type='TRX' AND wallet_execution_slot=1",
|
||||
wallet.get("id")) != null) {
|
||||
return false;
|
||||
}
|
||||
Map<String, Object> supplier = funds.account(number(node, "user_id"), text(order, "network"));
|
||||
require("ACTIVE".equals(text(supplier, "status")), "供应商资金账户冻结");
|
||||
store.update("order", number(order, "id"), map("status", "EXECUTING", "wallet_id", wallet.get("id"),
|
||||
"supplier_user_id", node.get("user_id"), "supplier_account_id", supplier.get("id")));
|
||||
createTask(order, wallet, RentalOnchain.TASK_TYPE, RentalOnchain.TRANSFER_SUN, 0, 0, null);
|
||||
store.event("ORDER", number(order, "id"), "DISPATCH", 0, "固定1TRX转账,供应商获得本金补偿及服务费");
|
||||
log.info("地址上链派单 orderId={} walletId={} nodeId={} transferSun={}", order.get("id"), wallet.get("id"), node.get("id"), RentalOnchain.TRANSFER_SUN);
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
|
||||
private void createTask(
|
||||
Map<String, Object> order,
|
||||
Map<String, Object> wallet,
|
||||
@@ -236,7 +288,7 @@ public class RentalTasks {
|
||||
String taskNo = id("RT"), nonce = RentalCrypto.random();
|
||||
long signed = System.currentTimeMillis();
|
||||
java.time.LocalDateTime deadline =
|
||||
"DELEGATE".equals(type)
|
||||
!"RECLAIM".equals(type)
|
||||
? java.time.LocalDateTime.ofInstant(
|
||||
time(order.get("dispatch_deadline")), java.time.ZoneOffset.UTC)
|
||||
: after(300);
|
||||
@@ -278,7 +330,7 @@ public class RentalTasks {
|
||||
"total_weight",
|
||||
Long.toString(weight),
|
||||
"baseline_sun",
|
||||
"DELEGATE".equals(type) ? "0" : Long.toString(sun),
|
||||
!"RECLAIM".equals(type) ? "0" : Long.toString(sun),
|
||||
"conversion_block_height",
|
||||
text(wallet, "snapshot_block_height"),
|
||||
"delegate_lock",
|
||||
@@ -323,7 +375,7 @@ public class RentalTasks {
|
||||
"conversion_at",
|
||||
now(),
|
||||
"estimated_resource_quantity",
|
||||
quantity(sun, limit, weight),
|
||||
RentalOnchain.TASK_TYPE.equals(type) ? 0 : quantity(sun, limit, weight),
|
||||
"attempt_no",
|
||||
attempt,
|
||||
"active_slot",
|
||||
@@ -494,7 +546,7 @@ public class RentalTasks {
|
||||
"amount_sun",
|
||||
task.get("delegate_sun"),
|
||||
"baseline_sun",
|
||||
"DELEGATE".equals(text(task, "task_type"))
|
||||
!"RECLAIM".equals(text(task, "task_type"))
|
||||
? 0
|
||||
: task.get("delegate_sun"),
|
||||
"status",
|
||||
@@ -512,7 +564,7 @@ public class RentalTasks {
|
||||
"UNKNOWN".equals(kind) ? "UNKNOWN" : "BROADCASTED",
|
||||
"next_check_at",
|
||||
now()));
|
||||
if ("DELEGATE".equals(text(task, "task_type"))) {
|
||||
if (!"RECLAIM".equals(text(task, "task_type"))) {
|
||||
store.update(
|
||||
"order", number(order, "id"), map("status", "VERIFYING"));
|
||||
}
|
||||
@@ -538,6 +590,8 @@ public class RentalTasks {
|
||||
nodeId,
|
||||
"reason",
|
||||
"已认证节点事件,成功仍须独立链上核验"));
|
||||
log.info("节点任务事件 nodeId={} taskId={} orderId={} taskType={} event={} txPresent={}",
|
||||
nodeId, taskId, order.get("id"), task.get("task_type"), kind, !text(report, "tx_id").isEmpty());
|
||||
return null;
|
||||
});
|
||||
}
|
||||
@@ -580,6 +634,18 @@ public class RentalTasks {
|
||||
fields.put("evidence", orders.encode(proof));
|
||||
fields.put("confirmed_at", now());
|
||||
store.update("transaction", number(record, "id"), fields);
|
||||
if (RentalOnchain.TASK_TYPE.equals(text(current, "task_type"))) {
|
||||
// 原生转账不存在租期与回收;确认到账后仅扣一次冻结款并生成供应商待结算收入。
|
||||
funds.charge(order);
|
||||
store.update("task", taskId, map("status", "VERIFIED", "wallet_execution_slot", null));
|
||||
store.update("order", number(order, "id"), map("status", "SUCCESS", "reclaim_status", "NOT_REQUIRED",
|
||||
"service_started_at", proof.get("block_time"), "error_code", null));
|
||||
orders.webhook(order, "SUCCESS", "ORDER:" + order.get("id") + ":SUCCESS");
|
||||
store.event("TASK", taskId, "CHAIN_CONFIRMED", 0, "独立核验固定1TRX转账并扣款,无回收任务");
|
||||
log.info("地址上链核验扣款完成 orderId={} taskId={} network={} txId={} blockHeight={} chargedUsdt={}",
|
||||
order.get("id"), taskId, task.get("network"), record.get("tx_id"), proof.get("block_height"), order.get("sale_amount"));
|
||||
return null;
|
||||
}
|
||||
boolean delegate = "DELEGATE".equals(text(current, "task_type"));
|
||||
String prefix = text(current, "resource_type").toLowerCase(Locale.ROOT);
|
||||
Map<String, Object> barrier =
|
||||
@@ -679,7 +745,7 @@ public class RentalTasks {
|
||||
null,
|
||||
"reservation_status",
|
||||
delegate ? "RELEASED" : "NONE"));
|
||||
if (delegate) {
|
||||
if (delegate || RentalOnchain.TASK_TYPE.equals(text(task, "task_type"))) {
|
||||
boolean retry =
|
||||
number(task, "attempt_no") < 4
|
||||
&& time(order.get("dispatch_deadline")).isAfter(Instant.now());
|
||||
@@ -704,6 +770,8 @@ public class RentalTasks {
|
||||
map("reclaim_status", "WAITING", "next_reclaim_at", after(60)));
|
||||
}
|
||||
store.event("TASK", taskId, "SAFE_FAILURE", 0, "明确拒绝未构建或链上确认失败;未知不走本分支");
|
||||
log.warn("任务明确失败 taskId={} orderId={} taskType={} state={} attempt={},仅明确失败允许释放或重派",
|
||||
taskId, orderId, task.get("task_type"), state, task.get("attempt_no"));
|
||||
}
|
||||
|
||||
public void recoverResource(long orderId) {
|
||||
|
||||
Reference in new issue
Block a user