fix: 出证连接失败时停止查询并允许重试

This commit is contained in:
Nixevol
2026-11-15 13:56:44 +08:00
parent e17ebe53df
commit 72e99daa7b
6 changed files with 51 additions and 13 deletions
@@ -50,6 +50,19 @@ describe('Consumer certificate lifecycle', () => {
await service.process(row);
expect(submit).not.toHaveBeenCalled();
});
it('a refused connection on submit fails instead of querying an unsent deposit', async () => {
const { service, row, prisma } = fixture();
const cause = Object.assign(new Error('connect ECONNREFUSED 127.0.0.1:443'), { code: 'ECONNREFUSED' });
const failure = new TypeError('fetch failed');
(failure as { cause?: unknown }).cause = cause;
jest.spyOn(DepositProvider.prototype, 'submit').mockRejectedValue(failure);
await service.process(row);
expect(prisma.blockchainProof.updateMany.mock.calls[1][0].data).toMatchObject({
certificateState: 'FAILED',
certificateError: '暂时联系不上存证服务,证书没有生成。',
certificateNextCheck: null,
});
});
it('a submission timeout changes to query-only, preserving the exact operation id', async () => {
const { service, row, prisma } = fixture();
const submit = jest.spyOn(DepositProvider.prototype, 'submit').mockRejectedValue(new Error('timeout'));
@@ -19,6 +19,19 @@ interface CertificateRequest {
const ACTIVE_STATES = ['PENDING', 'SUBMITTING', 'WAITING', 'CHECKING'];
const MAX_CHECKS = 8;
const UNREACHABLE_MESSAGE = '暂时联系不上存证服务,证书没有生成。';
function depositUnreachable(error: unknown) {
const texts: string[] = [];
let current: unknown = error;
for (let depth = 0; depth < 3 && current instanceof Error; depth += 1) {
texts.push(current.message);
const code = (current as { code?: unknown }).code;
if (typeof code === 'string') texts.push(code);
current = (current as { cause?: unknown }).cause;
}
return /ECONNREFUSED|ENOTFOUND|EAI_AGAIN|EHOSTUNREACH|ECONNRESET/.test(texts.join(' '));
}
@Injectable()
export class DepositCertificateService {
@@ -46,10 +59,12 @@ export class DepositCertificateService {
const certificate = request && proof.certificateState === 'READY'
? presentStoredCertificate(proof.certificateUrl, request.environment, request.address, request.text, await this.settings.get('certificateAuthorMode'), artist)
: null;
const status = proof.certificateState === 'READY' && !certificate ? 'FAILED' : proof.certificateState;
return {
status: proof.certificateState === 'READY' && !certificate ? 'FAILED' : proof.certificateState,
status,
generatedAt: proof.certificateGeneratedAt,
certificate: certificate && request ? { ...certificate, environment: request.environment } : certificate,
message: status === 'READY' ? null : proof.certificateError,
};
}
@@ -63,7 +78,8 @@ export class DepositCertificateService {
const { code, proof } = await this.eligible(shortCode);
if (proof.certificateState === 'READY') return this.view(proof, code.artwork.artist);
const settings = await this.settings.getAllRaw();
if (proof.certificateState !== 'NONE') {
const retryUnreachable = proof.certificateState === 'FAILED' && !proof.certificateUrl && proof.certificateError === UNREACHABLE_MESSAGE;
if (proof.certificateState !== 'NONE' && !retryUnreachable) {
// An uncertain submission is never submitted again. User action only resumes queries.
if (proof.certificateState === 'UNKNOWN' && (!proof.certificateNextCheck || proof.certificateNextCheck <= new Date())) {
await this.prisma.blockchainProof.updateMany({
@@ -103,10 +119,10 @@ export class DepositCertificateService {
// A single conditional write claims this item; concurrent callers cannot
// replace the winner's frozen evidence or operation id. No daily quota.
await this.prisma.blockchainProof.updateMany({
where: { id: proof.id, certificateState: 'NONE', status: ProofStatus.ONCHAIN },
where: { id: proof.id, certificateState: retryUnreachable ? 'FAILED' : 'NONE', status: ProofStatus.ONCHAIN },
data: {
certificateState: 'PENDING', certificateRequest: request as unknown as Prisma.InputJsonObject,
certificateRequestedAt: now, certificateNextCheck: now,
certificateError: null, certificateRequestedAt: now, certificateNextCheck: now,
},
});
return this.view(await this.prisma.blockchainProof.findUniqueOrThrow({ where: { id: proof.id } }), code.artwork.artist);
@@ -184,9 +200,13 @@ export class DepositCertificateService {
certificateNextCheck: new Date(Date.now() + 30_000) });
} catch (error) {
const rejected = submit && !accepted && error instanceof DepositRejected;
await finish({ certificateState: rejected ? 'FAILED' : attempts >= MAX_CHECKS ? 'UNKNOWN' : 'WAITING',
certificateAttempts: attempts, certificateError: rejected ? '存证提交被拒绝,请联系平台管理员' : '远端结果暂未确认,后续仅查询原存证',
certificateNextCheck: new Date(Date.now() + 30_000) });
const unreachable = submit && !accepted && depositUnreachable(error);
await finish({
certificateState: rejected || unreachable ? 'FAILED' : attempts >= MAX_CHECKS ? 'UNKNOWN' : 'WAITING',
certificateAttempts: attempts,
certificateError: rejected ? '存证提交被拒绝,请联系平台管理员' : unreachable ? UNREACHABLE_MESSAGE : '远端结果暂未确认,后续仅查询原存证',
certificateNextCheck: rejected || unreachable ? null : new Date(Date.now() + 30_000),
});
}
}
}