fix: 优化天河链确认策略
This commit is contained in:
@@ -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(),
|
||||
}),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user