Commit Graph

1693 Commits

Author SHA1 Message Date
hailin 999d0389b3 refactor(trading): 价格数据源从 SharePool 切换到做市商 cashBalance
积分股价格公式中的"绿积分"(分子)本质上就是做市商的积分值余额,
SharePool 是多余的抽象层。此次改动将价格计算的数据源从独立的
SharePool 表切换到做市商的 TradingAccount.cashBalance。

主要变更:
- price.service.ts: 通过 PrismaService 直接读取做市商 cashBalance
  作为价格公式的分子(绿积分),移除 SharePoolRepository 依赖
- asset.service.ts: getMarketOverview() 同步切换数据源
- order.service.ts: 10%交易手续费从 sharePoolRepository.feeIn()
  改为 increment 回做市商的 TradingAccount,手续费自然留存在池中
- burn.service.ts: 移除 SharePool 初始化逻辑和依赖
- infrastructure.module.ts: 移除 SharePoolRepository 注册和导出
- seed.ts: 移除 SharePool 57.6亿初始化

价格公式、10%手续费扣除、销毁机制均保持不变,仅切换数据源。
SharePool 表和 repository 文件保留在磁盘上供历史数据参考。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 23:13:59 -08:00
hailin 14e70b56bb fix(c2c-bot): 水单完成时间转换为北京时间 (UTC+8)
问题:水单上的完成时间显示 UTC 时间(如 03:38:40),实际北京时间为 11:38:40。
原因:toISOString() 始终返回 UTC 时间。
修复:手动加 8 小时偏移量,将 UTC 转换为北京时间后再格式化。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 19:42:13 -08:00
hailin bf11a269a4 fix(c2c-bot): 水单移除交易哈希,仅保留在分类账中
水单图片不再显示交易哈希(txHash),减少对终端用户暴露的链上细节。
txHash 仍保留在 ProofData 接口和数据库中,供分类账和审计使用。
同时缩小 SVG 高度(480→420)并重新排列剩余字段。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 19:35:48 -08:00
hailin 0fe0f3b72a fix(c2c-bot): 水单显示优化:dUSDT→绿积分,收款地址→对方ID
水单图片调整:
- "支付金额 (dUSDT)" → "支付金额 (绿积分)"
- "收款地址"(显示 Kava 地址)→ "对方ID"(显示卖家账户 ID)
- ProofData 接口新增 sellerId 字段(来自 order.makerAccountSequence)
- sellerAddress 仍保留在数据结构中,供明细分类账使用

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 19:24:02 -08:00
hailin 7ee6d633c6 fix(kafka): 修复 MPC Kafka consumer 启动订阅失败后不重试的问题
问题现象:
- blockchain-service 启动时 Kafka consumer subscribe() 抛出
  "This server does not host this topic-partition" 错误
- 原实现只 try-catch 一次就放弃,consumer 永久失效
- 后续所有 MPC 签名结果都收不到,表现为 signing timeout 300s
- 需要手动 docker restart 才能恢复

根因分析:
- 服务启动时 Kafka broker 可能尚未完成 topic-partition 分配
  (特别是容器编排环境中服务启动顺序不确定)
- onModuleInit() 中的 connect/subscribe/run 是一次性调用
- KafkaJS 的 retry 配置只作用于内部操作,不覆盖初始连接流程

修复方案:
- 新增 connectWithRetry() 方法:指数退避重试(2s→4s→8s→...→60s)
  最多 10 次,总等待约 5 分钟
- 每次重试前断开连接清理状态,避免 KafkaJS 内部状态残留
- 监听 consumer CRASH 事件:当 KafkaJS 不自动重启时(restart=false)
  手动触发 connectWithRetry() 重连
- 新增 isShuttingDown 标志:防止 onModuleDestroy 时触发无意义的重连
- 同时修复 blockchain-service 和 identity-service 两个 consumer

影响范围:
- blockchain-service: 影响 MPC 签名结果接收(热钱包转账)
- identity-service: 影响 MPC 密钥生成结果接收(用户钱包创建)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 19:18:10 -08:00
hailin 2de4baf0af fix(c2c-bot): 安装中文字体解决水单图片乱码问题
Alpine Linux 容器没有中文字体,sharp 渲染 SVG 时中文显示为方块。
- Dockerfile: 安装 fontconfig + font-noto-cjk 并刷新字体缓存
- SVG 模板: font-family 改为 Noto Sans CJK SC

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 18:56:55 -08:00
hailin 21f51c5d84 fix(c2c-bot): 修复 sharp 导入方式,解决水单生成 is not a function 错误
NestJS 使用 CommonJS 模块系统,`import sharp from 'sharp'` 编译后
生成 `sharp_1.default` 调用,但 sharp 没有 default export。
改为 `import * as sharp from 'sharp'` 修复。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 18:47:38 -08:00
hailin eab61abace feat(c2c-bot): 顺序处理订单 + 自动生成付款水单图片
1. 顺序处理订单:
   - Scheduler 每个10s周期只处理1个卖单(原先最多10个)
   - 移除 for 循环,确保完成一个订单后再处理下一个
   - 分布式锁 TTL 从 8s 增加到 30s,留足链上转账时间

2. 付款水单自动生成:
   - 新增 PaymentProofService,使用 SVG 模板 + sharp 转 PNG
   - 水单包含:订单号、支付金额、交易哈希、收款地址、完成时间
   - Bot 完成转账后自动生成水单并调用 updatePaymentProof 更新订单
   - 水单生成失败不影响订单本身(try-catch 保护)

文件变更:
- package.json: 添加 sharp ^0.33.2 依赖
- c2c-bot.scheduler.ts: 限制每周期1单,增加锁时间
- payment-proof.service.ts: 新文件,SVG→PNG 水单生成服务
- application.module.ts: 注册 PaymentProofService
- c2c-bot.service.ts: 注入 PaymentProofService,步骤5生成水单

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 18:32:52 -08:00
hailin 7a1d438f84 fix(mpc): 去除 message_hash 的 0x 前缀,修复 MPC 签名 400 错误
MPC account-service 期望纯 hex 格式的 message_hash,不接受 0x 前缀。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 18:15:40 -08:00
hailin cb8c69788b fix(deploy): curl -s 改为 curl -sf,修复 HTTP 404 不被检测的问题
curl -s 只是静默输出,HTTP 404 仍然返回 exit code 0,导致就绪
检查在 Kafka Connect REST API 未初始化完时就通过了。
-f 标志让 curl 在 HTTP 4xx/5xx 时返回非零 exit code。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 10:30:37 -08:00
hailin d8ef156b5e fix(debezium): 修复 authorization connector 凭据和注册重试逻辑
- authorization-connector.json: 将硬编码的 debezium/debezium_password
  改为 ${POSTGRES_USER}/${POSTGRES_PASSWORD} 占位符,与其他 connector 一致
- register-connectors.sh: 添加 3 次重试逻辑,应对 Kafka Connect REST API
  初始化期间的间歇性 404

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 10:28:52 -08:00
hailin 5b3c391340 fix(infra): 添加 Zookeeper 数据持久化卷,修复 Kafka Cluster ID 冲突
Kafka 有持久化卷但 Zookeeper 没有,导致 down/up 后 ZK 生成新的
Cluster ID,与 Kafka meta.properties 中的旧 ID 冲突。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 10:25:14 -08:00
hailin d575287713 fix(deploy): 修复 deploy.sh Debezium connector 注册失败的问题
根因:就绪检查只检测 Jetty 根路径 /,但 Kafka Connect REST API
(/connectors) 需要更长时间初始化,导致注册时返回 404。

- deploy.sh: 就绪检查改为 /connectors,等待时间从 60s 增加到 120s
- deploy.sh: 删除 250 行硬编码的 connector 配置,改为调用
  register-connectors.sh(单一数据源,JSON 文件)
- register-connectors.sh: 同步修复就绪检查

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 10:22:04 -08:00
hailin 6f668d69bd fix(infra): 添加 Kafka 数据持久化卷,防止容器重建后 Debezium connector 丢失
根因:Kafka 没有配置数据卷,容器重建后 debezium_configs/offsets 等内部
topic 丢失,导致 connector 注册信息消失。新 snapshot 的 offset 与旧
processed_events 碰撞,CDC 数据被跳过。

- docker-compose.yml: 添加 kafka_data 命名卷挂载到 /var/lib/kafka/data
- register-connectors.sh: 添加环境变量替换、幂等注册、--force/--1.0/--2.0 参数

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 10:16:07 -08:00
hailin 0f3d03d832 fix(wallet-service): 修复提现失败时不写 UNFREEZE ledger 流水的 bug
问题描述:
withdrawal-status.handler.ts 的 executeWithdrawalFailed() 方法在处理
提现失败事件时,虽然正确地将冻结余额退回到可用余额(usdt_frozen → usdt_available),
但没有在 wallet_ledger_entries 表中写入 UNFREEZE 类型的流水记录,导致审计缺口。

对比 system-withdrawal-application.service.ts 中的同名方法 handleWithdrawalFailed(),
后者在退回余额时正确地写入了 UNFREEZE ledger entry。两者逻辑应保持一致。

影响范围:
所有通过 blockchain-service Kafka 事件触发的提现失败场景(包括链上提现和内部转账),
余额变动不会被记录到流水表中,无法审计追踪。

修复内容:
在 wallet_accounts 余额更新成功后,新增 tx.ledgerEntry.create() 调用,
写入 UNFREEZE 类型的流水记录,包含:
- amount: 实际退回金额(newAvailable - currentAvailable,兼容冻结不足的边缘情况)
- memo: "提现失败退款: {error}"
- payloadJson: toAddress, chainType, error 便于排查

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 09:13:40 -08:00
hailin 758babfdf8 fix(blockchain+trading): 修复做市商区块链充值无法入账的三个 bug
1. OutboxPublisherService 未注册到 NestJS 模块,导致充值确认事件永远
   停留在 outbox_events 表 PENDING 状态,无法发布到 Kafka
2. trading-service 消费者监听的 Kafka topic 和 event type 与发布端不匹配
   (blockchain.market_maker.deposits → mining_blockchain.market_maker.deposits)
3. 消费者只更新 market_maker_config 余额,但 API 从 trading_accounts 读取
   余额,导致入账后前端仍显示 0

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 03:30:30 -08:00
hailin 1f434f32fb fix(service-party-android): 修复导入钱包签名时 'party not registered' 错误
导入的钱包份额携带原始 keygen partyId,与设备自身 partyId 不同。
签名时用原始 partyId 订阅 session events,但该 ID 未在 message-router
注册,导致服务端返回 FAILED_PRECONDITION。

修复:签名前将导入份额的 partyId 也注册到 message-router(带 3 次重试),
注册失败则中断签名流程并提示用户;断线重连时自动恢复双 partyId 注册;
签名结束后清理额外注册。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 03:24:14 -08:00
hailin fe6c1b3fce fix(mining-admin-service): 修复 manual-mining 分页参数 NaN 问题
NestJS transform 会将缺失的 query 参数转为 NaN,
而 NaN ?? 1 仍然是 NaN。改用 Number(page) || 1 确保默认值生效。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 02:37:55 -08:00
hailin 3bce996dd3 fix(mining-admin-service): 修复 manual-mining 和 pending-contributions 内部调用 URL
这两个 service 调用 mining-service 时缺少 /api/v2/mining 前缀,
导致请求到达 mining-service 后 404。

修复前: ${miningServiceUrl}/admin/manual-mining/records
修复后: ${miningServiceUrl}/api/v2/mining/admin/manual-mining/records

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 02:24:00 -08:00
hailin 59efdb1f78 fix(mining-admin-web): 修复 API 请求超时和路由问题
问题:
- mining-admin-web 容器与后端服务在不同 Docker 网络, 无法通过公网 IP 回连 (hairpin NAT 失败)
- next.config.js 生产模式强制走 mapi.szaiai.com Kong 网关, 容器内无法访问
- client.ts 使用 NEXT_PUBLIC_API_URL 导致外部 URL 被 build 时内联到客户端包
- mining-service controller 有 mining/ 前缀, 直连模式 rewrite 丢失该前缀

修复:
- next.config.js: 改用 API_GATEWAY_URL 判断路由模式, 未设置则直连后端服务
- next.config.js: mining rewrite 保留 mining/ 前缀匹配 controller 路由
- client.ts: baseURL 固定为 /api, 所有请求统一走 Next.js rewrite 代理
- docker-compose.2.0.yml: 添加 TRADING_SERVICE_URL 和 MINING_SERVICE_URL 环境变量

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 01:14:19 -08:00
hailin d9d8f69562 feat(nginx): 添加 madmin.szaiai.com nginx 配置
mining-admin-web 前端代理,Next.js :3100。
API 由 Next.js SSR rewrite 转发到 mapi.szaiai.com (Kong)。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 00:01:37 -08:00
hailin 6443acf34a fix(deploy-mining): cdc-resnapshot 支持 standalone 模式远程操作 1.0 Debezium
standalone 模式下 source CDC connectors 在 1.0 服务器 (192.168.1.111:8084),
cdc-resnapshot 需要指向 1.0 Debezium 才能删除并重建 connector 触发全量快照。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 23:42:53 -08:00
hailin 0204f748e6 fix(debezium): outbox connector 配置支持 standalone 模式 (postgres-2)
- database.hostname 从硬编码 "postgres" 改为 ${DEBEZIUM_DB_HOST:-postgres}
- database.user/password 改为环境变量替换 ${POSTGRES_USER}/${POSTGRES_PASSWORD}
- standalone 模式设置 DEBEZIUM_DB_HOST=postgres-2
- shared 模式不设置,默认值 postgres 保持不变

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 23:01:45 -08:00
hailin 2a5b51aa8d fix(deploy-mining): ensure_kafka_image 输出到 stderr 避免被 $() 捕获
docker pull 在 group_info=$(run_kafka_cmd ...) 内执行时,stdout 被命令替换
捕获到变量中,屏幕无任何输出导致脚本看起来卡住。
- ensure_kafka_image 输出重定向到 stderr
- full_reset 开头显式预拉取镜像

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 22:42:42 -08:00
hailin 0156be8d25 fix(deploy-mining): 预拉取 Kafka CLI 镜像避免 timeout 期间拉取失败
standalone 模式下 run_kafka_cmd 使用 docker run confluentinc/cp-kafka:7.5.0,
首次运行时镜像未缓存导致 docker pull 被 timeout 30 截断,循环重试卡住。
添加 ensure_kafka_image() 在执行命令前先确保镜像已拉取。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 22:36:38 -08:00
hailin d53c2212a6 fix(deploy-mining): 修复 standalone 模式跨服务器 full-reset 卡住问题
- run_kafka_cmd 添加 timeout 30 防止跨服务器 Kafka 命令无限挂起
- CDC consumer offset 重置前先检查 group 是否存在,不存在则跳过
- 等待时间从 15s/20s 增加到 30s(匹配 KafkaJS 默认 sessionTimeout)
- max_retries 从 3 增加到 5,并输出实际 Kafka 错误信息
- 重试失败后 fallback 删除 consumer group(服务重启时自动重建)
- 同步修复 sync_reset() 和 full_reset() Step 3/Step 8 三处逻辑

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 22:33:28 -08:00
hailin 88ad3ab53d feat: 2.0 API 域名从 rwaapi.szaiai.com 迁移到 mapi.szaiai.com
- mining-admin-web: .env.production, next.config.js fallback URL
- mining-app: environment.dart baseUrl, update_config.dart checkUpdateUrl
- docker-compose.2.0.yml: mining-admin-service UPLOAD_BASE_URL

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 21:58:03 -08:00
hailin 981b11f746 feat(api-gateway): 添加 standalone 模式 (up2) 支持 2.0 服务与 Kong 同机部署
- 新增 kong-standalone.yml: 2.0 服务通过 host.docker.internal 访问本机端口
  1.0 服务仍走 192.168.1.111, CORS 已添加 mapi.szaiai.com
- 新增 docker-compose.standalone.yml: compose override 添加 extra_hosts
- deploy.sh 新增 up2/sync2 命令

用法:
  ./deploy.sh up2    # 启动 Kong (standalone)
  ./deploy.sh sync2  # 重新同步 standalone 配置

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 21:44:20 -08:00
hailin 42cf189749 feat(api-gateway): 添加 mapi.szaiai.com nginx+SSL 配置和 deploy.sh nginx 子命令
- 新增 nginx/mapi.szaiai.com.conf: 与 rwaapi.szaiai.com 同构的 SSL 反代配置
- deploy.sh 新增 nginx install/ssl 子命令,支持一键安装 nginx+证书
- 用法: sudo ./deploy.sh nginx install mapi.szaiai.com

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 21:35:00 -08:00
hailin ec73541fe1 refactor(mining-blockchain): 移除 mpc-service 依赖,改为 HTTP 直调 mpc-system
将 mining-blockchain-service 的 MPC 签名通信从 Kafka 事件驱动(经由 mpc-service 中转)
改为 HTTP 直接调用 mpc-system 的 account-service (port 4000)。

## 核心变更

### mpc-signing.client.ts (重写)
- 移除 EventPublisherService、MpcEventConsumerService 依赖和 pendingRequests Map
- 移除 OnModuleInit 中的 Kafka 事件注册
- 新增 HttpService (@nestjs/axios) + JwtService (@nestjs/jwt) 依赖注入
- 签名流程改为:
  1. POST /api/v1/mpc/sign → 创建签名会话 (snake_case: username, message_hash)
  2. GET /api/v1/mpc/sessions/{session_id} → 轮询结果 (每 2s, 最多 5 分钟)
- JWT 认证: 使用 MPC_JWT_SECRET (HS256) 生成 Bearer token,匹配 mpc-system 格式
- 所有公共接口不变 (signMessage, signMessageAsXxxMarketMaker, isConfigured, getXxxAddress 等)

### 删除的文件
- mpc-event-consumer.service.ts: Kafka MPC 事件消费者 (SigningCompleted/SessionFailed/KeygenCompleted)
- mpc-keygen-completed.handler.ts: Keygen 地址派生处理器 (不再由此服务处理)

### 模块更新
- infrastructure.module.ts: 移除 MpcEventConsumerService,新增 JwtModule.register({})
- kafka/index.ts: 移除 mpc-event-consumer.service 导出
- event-handlers/index.ts: 移除 mpc-keygen-completed.handler 导出

### 部署配置
- docker-compose.2.0.yml: 新增 MPC_ACCOUNT_SERVICE_URL 和 MPC_JWT_SECRET 环境变量
- deploy-mining.sh: standalone 模式新增 export MPC_ACCOUNT_SERVICE_URL (默认 http://192.168.1.111:4000)

## 不受影响的部分
- Erc20TransferService / MpcTransferInitializerService 调用方式不变
- EventPublisherService (用于其他事件) 不变
- WithdrawalEventConsumerService 不变
- mpc-system 本身零修改

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 20:53:35 -08:00
hailin c657bf6e2b revert: 撤回 mpc-service HTTP 调用方案,改为直接调用 mpc-system
Revert "fix(deploy): export MPC_SERVICE_URL in standalone mode"
Revert "refactor(mining-blockchain): replace Kafka MPC signing with HTTP direct calls"

将改为直接调用 mpc-system (Go TSS 后端) 的 API,跳过 mpc-service 中间层。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 20:33:39 -08:00
hailin 3102f3e7fb fix(deploy): export MPC_SERVICE_URL in standalone mode
In standalone mode, mpc-service runs on the 1.0 server (192.168.1.111),
not in the local Docker network. The default docker-compose value
http://mpc-service:3006 won't resolve. Export MPC_SERVICE_URL pointing
to the 1.0 server's IP so mining-blockchain-service can reach it.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 20:30:44 -08:00
hailin 1106a40ff1 refactor(mining-blockchain): replace Kafka MPC signing with HTTP direct calls
Remove Kafka event-driven MPC signing (publish/consume pattern) and replace
with HTTP POST + polling to mpc-service, matching identity-service approach.

- Rewrite mpc-signing.client.ts: POST /api/v1/mpc/sign + poll status
- Delete mpc-event-consumer.service.ts (no longer needed)
- Delete mpc-keygen-completed.handler.ts (keygen handled elsewhere)
- Add MPC_SERVICE_URL env var to docker-compose.2.0.yml

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 20:28:26 -08:00
hailin 0d47fadf59 feat(kafka): add EXTERNAL listener on port 9093 for LAN access
Add a third Kafka listener (EXTERNAL://0.0.0.0:9093) so that
2.0 standalone services on other servers (e.g. 192.168.1.10) can
connect to 1.0's Kafka at 192.168.1.111:9093.

Listeners now:
- PLAINTEXT (kafka:29092) - internal Docker network
- PLAINTEXT_HOST (localhost:9092) - host-local access
- EXTERNAL (192.168.1.111:9093) - LAN access for remote services

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 19:49:34 -08:00
hailin ee07b52af9 feat(mining-2.0): 支持 standalone 独立部署模式,2.0 服务可脱离 1.0 单独运行
## 背景
2.0 挖矿生态需要从 1.0 服务器(192.168.1.111)迁移到独立服务器(192.168.1.10),
实现物理隔离部署。2.0 拥有独立的 PostgreSQL/Redis,通过远程 Kafka 接收 1.0 CDC 数据。

## docker-compose.2.0.yml 变更

### 网络参数化
- 网络改为 external: true + name 参数化:
  `name: ${RWA_NETWORK_NAME:-services_rwa-network}`
- shared 模式复用 1.0 网络, standalone 模式使用独立 rwa-2-network

### 基础设施地址参数化 (所有 7 个 backend service)
- DATABASE_URL: @${POSTGRES_HOST:-postgres}:${POSTGRES_PORT:-5432}
- REDIS_HOST: ${REDIS_HOST:-redis}
- KAFKA_BROKERS: ${KAFKA_BROKERS:-kafka:29092}

### 新增 3 个 standalone profile 基础设施服务
- postgres-2: PostgreSQL 16, WAL logical, init-multiple-dbs.sh 自动建库
- redis-2: Redis 7, appendonly 持久化
- debezium-2: Debezium Connect 2.4, 连接远程 Kafka(192.168.1.111:9093)
  用于 2.0 内部 outbox 模式 (服务事件 → Kafka → mining-admin-service)

### 可选依赖
- 应用 service 添加 depends_on postgres-2/redis-2 with required: false
  (Docker Compose v2.20+, shared 模式下自动跳过)

## deploy-mining.sh 变更

### 部署模式切换
- DEPLOY_MODE 环境变量: shared(默认) / standalone
- load_env() 按模式设置基础设施地址和 COMPOSE_ARGS
- shared 模式不 export 变量,避免覆盖容器侧 Docker Compose 默认值

### Kafka 远程操作支持
- run_kafka_cmd() / run_kafka_cmd_stdin(): standalone 模式用临时容器执行 Kafka CLI
- 所有 Kafka 操作函数 (sync_reset, sync_status, cdc_resnapshot, full_reset) 已适配

### 基础设施生命周期管理
- infra_up / infra_down: 独立管理 standalone 基础设施
- services_up(): standalone 模式自动先启动基础设施
- services_down(): standalone 模式同时停止基础设施
- services_clean(): fallback 列表包含 standalone 容器名

### full_reset() standalone 适配
- standalone 模式跳过 1.0 source CDC 操作 (远程 Debezium 管理)
- standalone 使用 debezium_2_offsets offset topic
- 自动启动 debezium-2 后注册 outbox connectors

### 其他
- health_check(): standalone 模式检查本地 PG/Redis 容器 + 远程 Kafka
- show_help(): 新增 DEPLOY_MODE 说明和 infra-up/infra-down 命令
- print_header(): 显示当前 DEPLOY_MODE
- 所有 ~15 处 docker compose 调用统一使用 $COMPOSE_ARGS

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 19:32:03 -08:00
hailin f39fd52001 feat(c2c-bot): rename HOT_WALLET to C2C_BOT_WALLET and change dUSDT to 绿积分
- Rename env vars HOT_WALLET_ADDRESS/USERNAME to C2C_BOT_WALLET_ADDRESS/USERNAME
  in mining-blockchain-service to avoid conflict with existing hot wallet
- Update docker-compose.2.0.yml with new env var names
- Change all "dUSDT" labels to "绿积分" on C2C Bot admin page

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 08:40:45 -08:00
hailin be52ac979b fix(trading-service): add MINING_BLOCKCHAIN_SERVICE_URL to docker-compose
trading-service BlockchainClient defaults to http://localhost:3020 but
mining-blockchain-service runs on port 3026. Add the correct Docker
internal URL so the C2C Bot can reach the blockchain service.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 08:21:17 -08:00
hailin 8d7e5b17a1 fix(mining-admin-service): cast response.data to any for envelope unwrapping
TypeScript build fails because MiningServiceResponse type doesn't have
a 'data' property. The runtime response from mining-service wraps data
in a { success, data } envelope, so we need `as any` to access it.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 07:49:31 -08:00
hailin 7549b2b9a9 fix(mining-admin): 解包mining-service响应信封,取response.data.data
mining-service返回 { success, data: { ... } } 格式,
mining-admin-service需要取 .data.data 而非 .data,
否则前端读到的是信封对象而非实际数据,导致 TypeError。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 07:44:17 -08:00
hailin 48720d1846 feat(blockchain): 1.0 blockchain-service 同步添加 RPC 端点自动故障转移
与 mining-blockchain-service (2.0) 保持一致的故障转移方案:
当 RPC 端点连续失败超过 3 分钟后自动轮转到下一个备选端点。

新增文件:
- rpc-provider-manager.service.ts: RPC 故障转移管理器(与 2.0 相同逻辑)

修改文件:
- blockchain.config.ts: 新增 rpcUrls 配置字段
- chain-config.service.ts: 新增 rpcUrls 属性和 parseRpcUrls 解析方法
- domain.module.ts: 注册并导出 RpcProviderManager
- index.ts: 导出 RpcProviderManager
- evm-provider.adapter.ts: 委托 RpcProviderManager 获取 provider,
  所有方法通过 executeWithFailover 包裹
- erc20-transfer.service.ts: 移除本地 providers Map,改用 RpcProviderManager,
  新增 isRpcConnectionError 区分 RPC 网络错误与合约执行错误
- docker-compose.yml: blockchain-service 添加 KAVA_RPC_URLS 默认 4 个端点

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 07:36:02 -08:00
hailin b9e9bb6e4e fix(mining-admin): 修正调用mining-service的URL路径,补全/api/v2/mining前缀
mining-service有全局前缀api/v2和控制器前缀mining/admin,
但mining-admin-service的HTTP调用缺少这两个前缀导致404,
错误被catch静默吞掉,前端显示空数据。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 07:34:21 -08:00
hailin 3635369a8a feat(mining-blockchain): RPC端点自动故障转移,连续失败3分钟后切换备选节点
问题:Kava主网RPC (evm.kava.io) 偶发503,导致mining-blockchain-service
所有链上操作失败(转账、余额查询、区块扫描等)。

方案:新增RpcProviderManager单例服务,统一管理各链的JsonRpcProvider实例,
当某个RPC端点连续失败超过3分钟后自动轮转到下一个备选端点。

新增文件:
- rpc-provider-manager.service.ts: 核心故障转移管理器
  · 每条链维护 provider/urls/currentIndex/failureState
  · reportSuccess() 重置失败状态
  · reportFailure() 记录失败,>=3分钟触发 switchToNextUrl()
  · 轮转创建新 JsonRpcProvider,替换旧实例
  · 每30秒记录一次失败日志,避免日志刷屏

修改文件:
- blockchain.config.ts: 新增 rpcUrls 配置字段(KAVA_RPC_URLS/BSC_RPC_URLS)
- chain-config.service.ts: 解析逗号分隔的URL列表,回退到单个rpcUrl
- domain.module.ts: 注册并导出 RpcProviderManager
- index.ts: 导出 RpcProviderManager
- evm-provider.adapter.ts: 委托RpcProviderManager获取provider,
  所有公开方法通过executeWithFailover包裹,自动上报成功/失败
- erc20-transfer.service.ts: 移除本地providers Map,改用RpcProviderManager,
  新增isRpcConnectionError()区分RPC网络错误与合约执行错误
- docker-compose.2.0.yml: 添加KAVA_RPC_URLS默认4个端点
- .env.example: 添加KAVA_RPC_URLS配置说明

默认端点仍为 evm.kava.io,备选: evm.kava-rpc.com,
kava-evm-rpc.publicnode.com, rpc.ankr.com/kava_evm

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 07:06:31 -08:00
hailin ef663c0c08 fix(deploy): replace envsubst with eval heredoc for ${VAR:-default} support
GNU envsubst does not handle ${VAR:-default} bash syntax - it leaves
the entire expression unsubstituted. This caused CDC connector
registration to fail with "password authentication failed for user
${POSTGRES_USER:-rwa_user}".

Replace all envsubst calls with a substitute_env_vars() helper that
uses eval+heredoc, which natively supports bash default value syntax.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 06:22:01 -08:00
hailin ea3d256647 fix(deploy): 恢复connector JSON原样,仅在load_env中export变量修复envsubst
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 06:09:53 -08:00
hailin 5728953b41 fix(deploy): connector JSON改为硬编码凭据,与outbox connector保持一致
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 06:06:47 -08:00
hailin a4f3a8d3ab fix(deploy): 修复envsubst无法解析${VAR:-default}语法导致CDC connector注册失败
问题:
  commit 5b1f4c82 在 full-reset Step 9 中用 envsubst 替换 connector JSON 中的
  数据库凭据,但 envsubst(GNU gettext)不支持 bash 的 ${VAR:-default} 语法,
  导致 "${POSTGRES_USER:-rwa_user}" 被原样发送给 Debezium,认证失败。

修复:
  1. deploy-mining.sh load_env(): 默认值赋值改为 export,确保 envsubst 能访问
  2. connector JSON 文件: ${VAR:-default} 改为 ${VAR},默认值由脚本 export 提供

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 06:03:38 -08:00
hailin b9f803c5c8 fix(blockchain): 修复health check路径不匹配并配置C2C Bot热钱包
- app.config.ts: 添加apiPrefix读取API_PREFIX环境变量
- docker-compose: health check从api/v2改为api/v1(与trading-service一致)
- docker-compose: 配置HOT_WALLET默认值,解决"区块链服务不可用"和钱包地址不显示

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 05:46:58 -08:00
hailin c802519ec2 fix(c2c-bot): 调整purchaseOrder操作顺序,先扣余额再链上转账
问题:原流程先执行不可逆的链上dUSDT转账,再扣减卖家DB余额。
若转账成功但扣减失败(余额不足、DB异常),会导致:
- dUSDT已转出(链上不可回退)
- 订单仍为PENDING(可能被重复处理)
- 卖家余额未扣减

修复:
1. 将deductSellerBalance移至transferDusdt之前(可逆操作先行)
2. 链上转账失败时调用restoreSellerBalance回补余额
3. 新增restoreSellerBalance方法,失败时记录CRITICAL级别日志

修改后流程:扣余额(可逆) → 链上转账(不可逆) → 更新订单状态(DB)
任何步骤失败都不会导致资金损失。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 05:02:19 -08:00
hailin 251e37f772 feat(c2c-bot): 添加C2C Bot管理页面,支持运行时开关和热钱包查看
- 新增C2C Bot管理页面(mining-admin-web):Bot开关、热钱包余额/地址/QR码、统计、订单历史
- 新增admin API端点(trading-service):status/enable/disable/orders
- 重构Bot调度器enabled为Redis驱动,支持运行时开关(多实例安全)
- C2cOrderRepository新增findBotPurchasedOrders和getBotStats查询方法
- 侧边栏添加C2C Bot导航入口

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 04:47:28 -08:00
hailin bf772967f5 feat(mining): 批量补发30%分配到运营和总部账户,并添加交易筛选器
- 批量补发时将剩余30%分配到运营(12%)和总部(18%)系统账户
- SystemMiningAccountRepository.mine()支持referenceId/referenceType参数
- BatchMiningExecution新增operationAmount/headquartersAmount字段(含DB迁移)
- 三层架构(mining-service→admin-service→admin-web)全链路支持referenceType筛选
- 系统账户交易记录页面增加"全部/批量补发"筛选按钮

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 04:25:20 -08:00