Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Attribute, SignedBy } from "@akashnetwork/chain-sdk";
import { singleton } from "tsyringe";

import { ProviderCleanupService } from "@src/billing/services/provider-cleanup/provider-cleanup.service";
Expand Down Expand Up @@ -47,4 +48,9 @@ export class ProviderController {
async getProviderActiveLeasesGraphData(providerAddress: string) {
return await this.providerStatsService.getProviderActiveLeasesGraphData(providerAddress);
}

async getProvidersByAttributes(attributes: Attribute[], signatures?: Partial<SignedBy>) {
const hostUris = await this.providerService.getProvidersHostUriByAttributes(attributes, signatures);
return { data: hostUris };
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// TODO: replace this import with @akashnetwork/chain-sdk when it exports those types
import type { Attribute, SignedBy } from "@akashnetwork/chain-sdk";
import {
Provider,
Expand Down
49 changes: 49 additions & 0 deletions apps/api/src/provider/routes/providers/providers.router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { z } from "@hono/zod-openapi";
import type { TypedResponse } from "hono";
import { container } from "tsyringe";

Expand Down Expand Up @@ -126,3 +127,51 @@ providersRouter.openapi(activeLeasesGraphDataRoute, async function routeProvider

return c.json(graphData);
});

const repeatedValue = z.union([z.string(), z.array(z.string())]).transform(v => (Array.isArray(v) ? v : [v]));
const providersByAttributesRoute = createRoute({
method: "get",
path: "/v1/providers-host-uri",
tags: ["Providers"],
security: SECURITY_NONE,
cache: {
maxAge: 60 * 60
},
request: {
query: z.object({
attr: repeatedValue
.transform(v =>
v.map(pair => {
const separatorIndex = pair.indexOf(":");
return separatorIndex === -1 ? { key: pair, value: "" } : { key: pair.slice(0, separatorIndex), value: pair.slice(separatorIndex + 1) };
})
)
.optional()
.default([]),
signedByAllOf: repeatedValue.optional(),
signedByAnyOf: repeatedValue.optional()
})
},
responses: {
200: {
description: "Returns a list of provider host URIs matching the given attributes and signatures",
content: {
"application/json": {
schema: z.object({
data: z.array(z.string())
})
}
}
},
400: {
description: "Invalid request"
}
}
});

providersRouter.openapi(providersByAttributesRoute, async function routeGetProvidersByAttributes(c) {
const { attr, signedByAllOf, signedByAnyOf } = c.req.valid("query");
const signatures = signedByAllOf || signedByAnyOf ? { allOf: signedByAllOf ?? [], anyOf: signedByAnyOf ?? [] } : undefined;
const providerHostUris = await container.resolve(ProviderController).getProvidersByAttributes(attr, signatures);
return c.json(providerHostUris);
});
24 changes: 22 additions & 2 deletions apps/api/src/provider/services/provider/provider.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ProviderAttributesSchema } from "@akashnetwork/http-sdk";
import { faker } from "@faker-js/faker";
import { AxiosError } from "axios";
import { Ok } from "ts-results";
import { describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { mock } from "vitest-mock-extended";

import { cacheEngine } from "@src/caching/helpers";
Expand Down Expand Up @@ -508,13 +508,33 @@ describe(ProviderService.name, () => {
});
});

describe("getProvidersHostUriByAttributes", () => {
it("delegates call to provider repository", async () => {
const { service, providerRepository } = setup();

const attributes = [
{ key: "region", value: "us-west" },
{ key: "tier", value: "premium" }
];
const signatures = { allOf: ["auditor1", "auditor2"] };
const expectedHostUris = ["https://provider1.example.com:8443", "https://provider2.example.com:8443"];

providerRepository.getProvidersHostUriByAttributes.mockResolvedValue(expectedHostUris);

const result = await service.getProvidersHostUriByAttributes(attributes, signatures);

expect(providerRepository.getProvidersHostUriByAttributes).toHaveBeenCalledWith(attributes, signatures);
expect(result).toEqual(expectedHostUris);
});
});

function setup() {
const providerProxyService = mock<ProviderProxyService>();
const providerRepository = mock<ProviderRepository>();
const providerAttributesSchemaService = mock<ProviderAttributesSchemaService>();
const auditorsService = mock<AuditorService>();
const jwtTokenService = mock<ProviderJwtTokenService>({
generateJwtToken: jest.fn().mockResolvedValue(Ok("mock-jwt-token"))
generateJwtToken: vi.fn().mockResolvedValue(Ok("mock-jwt-token"))
});

const service = new ProviderService(providerProxyService, providerRepository, providerAttributesSchemaService, auditorsService, jwtTokenService);
Expand Down
5 changes: 5 additions & 0 deletions apps/api/src/provider/services/provider/provider.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Attribute, SignedBy } from "@akashnetwork/chain-sdk";
import { Provider, ProviderSnapshot, ProviderSnapshotNode, ProviderSnapshotNodeGPU } from "@akashnetwork/database/dbSchemas/akash";
import type { ProviderAttributesSchema } from "@akashnetwork/http-sdk";
import { AxiosError } from "axios";
Expand Down Expand Up @@ -263,4 +264,8 @@ export class ProviderService {
}))
};
}

async getProvidersHostUriByAttributes(attributes: Attribute[], signatures?: Partial<SignedBy>): Promise<string[]> {
return await this.providerRepository.getProvidersHostUriByAttributes(attributes, signatures);
}
}
Loading