fix: 优化天河链确认策略

This commit is contained in:
Nixevol
2026-07-06 19:43:00 +08:00
parent 628a5a4465
commit 7480e4d256
9 changed files with 107 additions and 22 deletions
@@ -78,4 +78,23 @@ export class AttestationService {
await this.queue.add('onchain', { proofId });
return { success: true };
}
async confirm(proofId: number) {
const proof = await this.prisma.blockchainProof.findUnique({ where: { id: proofId } });
if (!proof) throw new BusinessException(ErrorCode.NOT_FOUND, '存证记录不存在', 404);
if (proof.status === ProofStatus.ONCHAIN) return { success: true };
if (!proof.txHash) throw new BusinessException(ErrorCode.BAD_REQUEST, '缺少交易哈希,无法重新确认');
await this.prisma.blockchainProof.update({
where: { id: proofId },
data: {
status: ProofStatus.CONFIRMING,
rawResponse: {
...(proof.rawResponse && typeof proof.rawResponse === 'object' ? (proof.rawResponse as object) : {}),
manualConfirmQueuedAt: new Date().toISOString(),
},
},
});
await this.queue.add('confirm-onchain', { proofId, action: 'confirm', confirmAttempt: 1, manual: true });
return { success: true };
}
}
@@ -23,4 +23,11 @@ export class BlockchainController {
retry(@Param('id', ParseIntPipe) id: number) {
return this.attestation.retry(id);
}
@Post('blockchain-proofs/:id/confirm')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '重新查询链上确认结果' })
confirm(@Param('id', ParseIntPipe) id: number) {
return this.attestation.confirm(id);
}
}
+24 -18
View File
@@ -10,10 +10,12 @@ interface OnchainJobData {
proofId: number;
action?: 'attest' | 'confirm';
confirmAttempt?: number;
manual?: boolean;
}
const CONFIRM_DELAY_MS = 30_000;
const MAX_CONFIRM_ATTEMPTS = 20;
const FIRST_CONFIRM_DELAY_MS = 30_000;
const SECOND_CONFIRM_DELAY_MS = 60_000;
const MAX_AUTO_CONFIRM_ATTEMPTS = 2;
function requestMeta(raw: unknown): Record<string, unknown> | undefined {
if (!raw || typeof raw !== 'object') return undefined;
@@ -43,7 +45,7 @@ export class OnchainProcessor extends WorkerHost {
async process(job: Job<OnchainJobData>): Promise<void> {
if (job.data.action === 'confirm') {
await this.confirm(job.data.proofId, job.data.confirmAttempt || 1);
await this.confirm(job.data.proofId, job.data.confirmAttempt || 1, Boolean(job.data.manual));
return;
}
await this.attest(job.data.proofId);
@@ -74,7 +76,7 @@ export class OnchainProcessor extends WorkerHost {
},
});
if (result.confirmationRequired) {
await this.scheduleConfirm(proofId, 1);
await this.scheduleConfirm(proofId, 1, FIRST_CONFIRM_DELAY_MS);
this.logger.log(`存证 ${proofId} 已提交链上,等待确认`);
} else {
this.logger.log(`存证 ${proofId} 上链成功`);
@@ -96,9 +98,10 @@ export class OnchainProcessor extends WorkerHost {
}
}
private async confirm(proofId: number, attempt: number): Promise<void> {
private async confirm(proofId: number, attempt: number, manual = false): Promise<void> {
const proof = await this.prisma.blockchainProof.findUnique({ where: { id: proofId } });
if (!proof || proof.status !== ProofStatus.CONFIRMING) return;
if (!proof || proof.status === ProofStatus.ONCHAIN) return;
if (proof.status !== ProofStatus.CONFIRMING && !manual) return;
if (!proof.txHash) {
await this.markConfirmFailed(proofId, proof.rawResponse, '缺少交易哈希,无法确认链上结果', attempt);
return;
@@ -130,39 +133,42 @@ export class OnchainProcessor extends WorkerHost {
return;
}
await this.recordWaiting(proofId, proof.rawResponse, result.raw, attempt);
if (attempt >= MAX_CONFIRM_ATTEMPTS) {
await this.markConfirmFailed(proofId, proof.rawResponse, '超过最大确认次数,仍未查询到链上区块信息', attempt);
await this.recordWaiting(proofId, proof.rawResponse, result.raw, attempt, manual);
if (manual || attempt >= MAX_AUTO_CONFIRM_ATTEMPTS) {
this.logger.warn(`存证 ${proofId} 尚未查询到链上确认信息,等待手动重新确认`);
return;
}
await this.scheduleConfirm(proofId, attempt + 1);
await this.scheduleConfirm(proofId, attempt + 1, SECOND_CONFIRM_DELAY_MS);
} catch (e) {
const message = (e as Error).message;
if (attempt >= MAX_CONFIRM_ATTEMPTS) {
await this.markConfirmFailed(proofId, proof.rawResponse, message, attempt);
if (manual || attempt >= MAX_AUTO_CONFIRM_ATTEMPTS) {
await this.recordWaiting(proofId, proof.rawResponse, { error: message }, attempt, manual);
return;
}
await this.recordWaiting(proofId, proof.rawResponse, { error: message }, attempt);
await this.scheduleConfirm(proofId, attempt + 1);
await this.recordWaiting(proofId, proof.rawResponse, { error: message }, attempt, manual);
await this.scheduleConfirm(proofId, attempt + 1, SECOND_CONFIRM_DELAY_MS);
}
}
private scheduleConfirm(proofId: number, confirmAttempt: number) {
private scheduleConfirm(proofId: number, confirmAttempt: number, delay: number, manual = false) {
return this.queue.add(
'confirm-onchain',
{ proofId, action: 'confirm', confirmAttempt } satisfies OnchainJobData,
{ delay: CONFIRM_DELAY_MS },
{ proofId, action: 'confirm', confirmAttempt, manual } satisfies OnchainJobData,
{ delay },
);
}
private recordWaiting(proofId: number, raw: unknown, confirmation: Record<string, unknown>, attempt: number) {
private recordWaiting(proofId: number, raw: unknown, confirmation: Record<string, unknown>, attempt: number, manual: boolean) {
return this.prisma.blockchainProof.update({
where: { id: proofId },
data: {
status: ProofStatus.CONFIRMING,
rawResponse: jsonObject({
...rawObject(raw),
confirmation,
confirmationAttempts: attempt,
confirmationManualRequired: manual || attempt >= MAX_AUTO_CONFIRM_ATTEMPTS,
lastConfirmMode: manual ? 'manual' : 'auto',
lastConfirmCheckedAt: new Date().toISOString(),
}),
},
@@ -39,6 +39,7 @@ function actionOf(method: string, path: string): string {
if (path.includes('/download')) return 'download';
if (path.includes('/upload')) return 'upload';
if (path.includes('/audit')) return 'audit';
if (path.includes('/blockchain-proofs') && path.includes('/confirm')) return 'onchain';
if (path.includes('/onchain')) return 'onchain';
if (path.includes('/backup')) return 'backup';
if (path.includes('/restore')) return 'restore';