diff --git a/docs/rental/implementation.md b/docs/rental/implementation.md index 1098d62..005d2b2 100644 --- a/docs/rental/implementation.md +++ b/docs/rental/implementation.md @@ -149,6 +149,19 @@ Agent事件先落本地可靠队列,云端同事务以 `NODE:nodeId:eventId` 示例参数不代表当前链。内置实现从启用时开始保存真实确认状态,不会补造上线前或停机期间漏采的全网参数。**未配置历史服务,或内置服务缺少该高度参数/派单前基线时,可以确认真实委托,但订单转MANUAL_REVIEW、保留HELD资金并按原SUN回收,不自动记SUCCESS或供应商收入。** 内置区块尚未采集到交易高度则保持待核验并重试。交易已确认而交付不足、核验时已过期,同样人工隔离。可在确认回收DONE后通过受保护的resolve解冻。外部擅自变更委托关系会告警并转人工,不能任意释放关系占位。 +## 节点钱包管理 + +供应商节点详情提供单钱包卡片、TronLink 所有权认证、出租设置和换绑。 +`POST /rental/register/wallet` 在节点行锁内限制每节点一个地址;已有历史多钱包不会自动清理,页面提示联系管理员处理。 +`GET /rental/node/{id}` 仅返回本人节点及其钱包展示字段,不受全局列表前50条分页限制。 +出租设置复用 `POST /rental/wallet/{id}/policy`,可独立开启能量、带宽出租并设置留存本金;SUN 使用整数串传递,1 TRX = 1,000,000 SUN。 +开启出租不代替认证、审核、资源快照、本地授权和节点在线检查。 + +`POST /rental/wallet/{id}/rebind` 使用钱包绑定权限,并在事务中检查归属、单钱包、节点离线和资源预留。 +只允许相同网络的新 TRON 地址;新地址不能已被同网络其他记录绑定。 +换绑清空原所有权认证、链上权限、出租开关/留存策略和资源快照,恢复待审核状态;必须更新本地 Agent 配置并重新认证、审核和配置出租。 +存在任意历史或在途订单/任务的钱包禁止原地换绑,请为新地址注册新节点,避免改变历史资源所有者含义。 + ## 8. 验证与边界 具体命令、结果及尚未验收的外部条件见 [validation-implementation.md](validation-implementation.md)。 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalController.java b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalController.java index b253882..e34bc94 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalController.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalController.java @@ -184,6 +184,29 @@ public class RentalController { return AjaxResult.success(); } + /** 本人节点及绑定钱包详情,显式选列保护认证材料。 */ + @GetMapping("/node/{id}") + @PreAuthorize("@ss.hasPermi('rent:portal:view')") + public AjaxResult nodeDetail(@PathVariable long id) { + Map node = store.one("SELECT id,node_no,name,status,connection_status,heartbeat_at FROM rent_node WHERE id=? AND user_id=?", id, user()); + require(node != null, "节点不存在或无权访问"); + if (node.get("heartbeat_at") != null) { + node.put("heartbeat_at", time(node.get("heartbeat_at")).toEpochMilli()); + } + // 按节点查询而非在全量钱包的第一页过滤;显式选列不输出挑战或签名。 + List> wallets = store.rows("SELECT id,node_id,network,address,status,ownership_verified_at," + + "energy_enabled,bandwidth_enabled,energy_retain_sun,bandwidth_retain_sun FROM rent_wallet WHERE node_id=? ORDER BY id", id); + return AjaxResult.success(map("node", node, "wallets", wallets)); + } + + /** 本人离线节点换绑,旧钱包历史关联与新地址合法性在事务内校验。 */ + @PostMapping("/wallet/{id}/rebind") + @PreAuthorize("@ss.hasPermi('rent:wallet:bind')") + public AjaxResult rebindWallet(@PathVariable long id, @RequestBody Map body) { + provisioning.rebindWallet(id, body, user()); + return AjaxResult.success(); + } + /** 删除本人尚未完成所有权验证的待审核地址。 */ @DeleteMapping("/wallet/{id}") @PreAuthorize("@ss.hasPermi('rent:wallet:bind')") diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalProvisioning.java b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalProvisioning.java index 364831f..bff2f7a 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalProvisioning.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalProvisioning.java @@ -84,6 +84,9 @@ public class RentalProvisioning { require( number(node, "user_id") == actor && !"REVOKED".equals(text(node, "status")), "无节点权限"); + // 所有新增入口共用父节点行锁,串行校验,避免并发注册两个钱包。 + require(store.one("SELECT id FROM rent_wallet WHERE node_id=? LIMIT 1", nodeId) == null, + "一个租赁节点只能绑定一个钱包地址,请使用换绑功能"); String address = input(request, "address", 64); RentalOrders.address(address); row = @@ -157,6 +160,52 @@ public class RentalProvisioning { return wallet; } + /** + * 换绑仅适用于无历史业务引用的钱包,禁止原地改变历史订单/任务中的资源所有者。 + * 与派单一致先锁钱包再锁节点;离线且没有任何预留后才允许变更。 + * 新地址必须重新验证及审核,原地址的签名、快照、权限和出租策略全部清空。 + */ + @Transactional(rollbackFor = Exception.class) + public void rebindWallet(long id, Map request, long actor) { + Map wallet = ownedWallet(id, actor, true); + long nodeId = number(wallet, "node_id"); + Map node = store.get("node", nodeId, true); + require(!"REVOKED".equals(text(node, "status")), "节点已吊销,不能换绑"); + require("OFFLINE".equals(text(node, "connection_status")), "请先停止Agent,确认节点离线后换绑"); + require(store.rows("SELECT id FROM rent_wallet WHERE node_id=?", nodeId).size() == 1, + "节点存在多个旧绑定,请先联系管理员处理,禁止自动覆盖"); + require(number(wallet, "energy_reserved_sun") == 0 && number(wallet, "bandwidth_reserved_sun") == 0, + "钱包仍有资源预留,不能换绑"); + require(store.one("SELECT id FROM rent_order WHERE wallet_id=? LIMIT 1 FOR UPDATE", id) == null + && store.one("SELECT id FROM rent_task WHERE wallet_id=? LIMIT 1 FOR UPDATE", id) == null, + "钱包存在历史或在途订单/任务,不能换绑,请为新地址注册新节点"); + require(text(wallet, "network").equals(network(input(request, "network", 16))), + "换绑不能更改网络,请为其他网络注册新节点"); + String address = input(request, "address", 64); + RentalOrders.address(address); + require(!address.equals(text(wallet, "address")), "新地址不能与原地址相同"); + require(store.one("SELECT id FROM rent_wallet WHERE network=? AND address=?", text(wallet, "network"), address) == null, + "该网络的钱包地址已被绑定"); + Map changes = new LinkedHashMap<>(); + for (String field : Arrays.asList("permission_id", "ownership_method", "ownership_challenge", + "ownership_message", "ownership_signature", "ownership_verified_address", "challenge_issued_at", + "challenge_expires_at", "challenge_consumed_at", "ownership_verified_at", "ownership_evidence", + "snapshot_at", "snapshot_expires_at", "snapshot_block_height", "policy")) { + changes.put(field, null); + } + for (String field : Arrays.asList("energy_available", "bandwidth_available", "energy_enabled", "bandwidth_enabled", + "energy_delegatable_sun", "bandwidth_delegatable_sun", "energy_retain_sun", "bandwidth_retain_sun", + "total_energy_limit", "total_energy_weight", "total_bandwidth_limit", "total_bandwidth_weight", + "snapshot_min_block_height")) { + changes.put(field, 0L); + } + changes.put("address", address); + changes.put("status", "PENDING"); + store.update("wallet", id, changes); + store.event("WALLET", id, "REBIND", actor, + "换绑:" + text(wallet, "address") + " -> " + address + ";已清空验证、资源快照及出租设置"); + } + /** * 仅清理误添加的待验证地址,不删除已验证或已产生业务关联的钱包。 * 与所有权验证共用钱包行锁,保证验证与删除不能同时成功;审计独立保留。 diff --git a/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java b/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java index 36c70c6..429281f 100644 --- a/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java +++ b/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java @@ -65,6 +65,73 @@ public class RentalBusinessTest { assertEquals(1, store.rows("SELECT id FROM rent_event WHERE object_type='WALLET' AND object_id=? AND action='DELETE'", pending).size()); } + /** 同一节点新增必须串行校验;两个并发请求只能成功绑定一个地址。 */ + @Test + public void concurrentWalletRegistrationAllowsOnlyOne() throws Exception { + long freshNode = tx.execute(status -> rentalDeletion().create("node", map("name", "single-wallet"), 20)); + ExecutorService pool = Executors.newFixedThreadPool(2); + CountDownLatch start = new CountDownLatch(1); + try { + List> results = new ArrayList<>(); + for (String address : Arrays.asList(ADDRESS, "T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb")) { + results.add(pool.submit(() -> { + start.await(); + try { + tx.execute(status -> rentalDeletion().create("wallet", map("node_id", freshNode, + "network", "MAINNET", "address", address), 20)); + return true; + } catch (IllegalArgumentException expected) { return false; } + })); + } + start.countDown(); + int successes = 0; + for (Future result : results) { if (result.get(15, TimeUnit.SECONDS)) successes++; } + assertEquals(1, successes); + assertEquals(1, store.rows("SELECT id FROM rent_wallet WHERE node_id=?", freshNode).size()); + } finally { pool.shutdownNow(); } + } + + /** 换绑重置旧地址认证及资源策略,拒绝在线、越权、错误网络、重复地址及历史业务关联。 */ + @Test + public void walletRebindResetsProofAndRejectsUnsafeChanges() { + Map request = map("network", "NILE", "address", ADDRESS); + expectFailure(() -> tx.execute(status -> { rentalDeletion().rebindWallet(wallet, request, 20); return null; })); + store.update("node", node, map("connection_status", "OFFLINE")); + expectFailure(() -> tx.execute(status -> { rentalDeletion().rebindWallet(wallet, request, 10); return null; })); + expectFailure(() -> tx.execute(status -> { rentalDeletion().rebindWallet(wallet, map("network", "MAINNET", "address", ADDRESS), 20); return null; })); + expectFailure(() -> tx.execute(status -> { rentalDeletion().rebindWallet(wallet, map("network", "NILE", "address", OWNER), 20); return null; })); + store.update("wallet", wallet, map("energy_reserved_sun", 1)); + expectFailure(() -> tx.execute(status -> { rentalDeletion().rebindWallet(wallet, request, 20); return null; })); + store.update("wallet", wallet, map("energy_reserved_sun", 0, "ownership_challenge", "old-challenge")); + tx.execute(status -> { rentalDeletion().rebindWallet(wallet, request, 20); return null; }); + Map updated = store.get("wallet", wallet, false); + assertEquals(ADDRESS, text(updated, "address")); + assertEquals("PENDING", text(updated, "status")); + assertNull(updated.get("ownership_verified_at")); + assertNull(updated.get("ownership_challenge")); + assertNull(updated.get("snapshot_at")); + assertEquals(0, number(updated, "energy_enabled")); + assertEquals(0, number(updated, "energy_delegatable_sun")); + store.update("wallet", wallet, map("status", "ACTIVE", "ownership_verified_at", now())); + Map order = tx.execute(status -> orders.create(app, 10, map("network", "NILE", + "package_id", product, "resource_type", "ENERGY", "merchant_order_no", "rebind-history", "receiver_address", ADDRESS))); + store.update("order", number(order, "id"), map("wallet_id", wallet)); + expectFailure(() -> tx.execute(status -> { rentalDeletion().rebindWallet(wallet, map("network", "NILE", "address", OWNER), 20); return null; })); + assertEquals(ADDRESS, text(store.get("wallet", wallet, false), "address")); + } + + /** 出租策略保留 SUN 整数精度,不能越权或写入负数/小数。 */ + @Test + public void walletPolicyIsOwnedAndUsesExactSun() { + Map policy = map("energy_enabled", 1, "bandwidth_enabled", 0, + "energy_retain_sun", "9007199254740993", "bandwidth_retain_sun", "0"); + expectFailure(() -> tx.execute(status -> { rentalDeletion().policy(wallet, policy, 10); return null; })); + tx.execute(status -> { rentalDeletion().policy(wallet, policy, 20); return null; }); + assertEquals(9007199254740993L, number(store.get("wallet", wallet, false), "energy_retain_sun")); + policy.put("bandwidth_retain_sun", "-1"); + expectFailure(() -> tx.execute(status -> { rentalDeletion().policy(wallet, policy, 20); return null; })); + } + /** 在线和恢复连接时拒绝轮换且保留原凭证,离线后允许正常轮换。 */ @Test public void nodeCredentialRotationRequiresOffline() { @@ -99,6 +166,17 @@ public class RentalBusinessTest { tx.execute(status -> identity.issue("NODE", node, 20)); RentalController controller = new RentalController( store, null, identity, orders, funds, reviews, tasks, null, null); + expectFailure(() -> controller.nodeDetail(node)); + long ownNode = tx.execute(status -> rentalDeletion().create("node", map("name", "owned-detail"), 10)); + long ownWallet = tx.execute(status -> rentalDeletion().create("wallet", map("node_id", ownNode, + "network", "MAINNET", "address", ADDRESS), 10)); + store.update("wallet", ownWallet, map("ownership_challenge", "hidden-challenge", "ownership_signature", "hidden-signature")); + Map detail = (Map) controller.nodeDetail(ownNode).get("data"); + List> wallets = (List>) detail.get("wallets"); + assertEquals(1, wallets.size()); + assertEquals(ownWallet, number(wallets.get(0), "id")); + assertFalse(wallets.get(0).containsKey("ownership_challenge")); + assertFalse(wallets.get(0).containsKey("ownership_signature")); Map result = (Map) controller.appDetail(app).get("data"); Map appDetail = (Map) result.get("app"); assertEquals(time(store.get("app", app, false).get("created_at")).toEpochMilli(),