Skip to content

Commit

Permalink
fix: concurrent provider certs validation
Browse files Browse the repository at this point in the history
  • Loading branch information
stalniy committed Jan 31, 2025
1 parent b7e647a commit f345de0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 4 additions & 2 deletions apps/provider-proxy/src/services/CertificateValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ export class CertificateValidator {

return this.knownCertificatesCache.get(key);
} finally {
this.locks[key].release();
delete this.locks[key];
if (this.locks[key]) {
this.locks[key].release();
delete this.locks[key];
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions apps/provider-proxy/test/services/CertificateValidator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,23 @@ describe(CertificateValidator.name, () => {
expect(result.ok).toBe(true);
});

it("fetches provider certificate only once for concurrent validation of the same certificate", async () => {
const { cert } = createX509CertPair({
validFrom: new Date(),
validTo: new Date(Date.now() + ONE_MINUTE),
commonName: "akash1rk090a6mq9gvm0h6ljf8kz8mrxglwwxsk4srxh",
serialNumber: "177831BE7F249E66"
});
const getCertificate = jest.fn(() => Promise.resolve(cert));
const validator = setup({ getCertificate });

const results = await Promise.all([validator.validate(cert, "mainnet", "provider"), validator.validate(cert, "mainnet", "provider")]);

expect(getCertificate).toHaveBeenCalledTimes(1);
expect(results[0].ok).toBe(true);
expect(results[1].ok).toBe(true);
});

function setup(params?: Params) {
return new CertificateValidator(
() => params?.now ?? Date.now(),
Expand Down

0 comments on commit f345de0

Please sign in to comment.