Skip to content

Commit

Permalink
task.taskId is redundant. Rename to task.id
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr committed Sep 12, 2023
1 parent 2efba98 commit e12f4aa
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 92 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Task from "@divviup/dap";
const task = new Task({
type: "sum",
bits: 8,
taskId: "3XTBHxTtUAtI516GeXZsVIKjBPYVNIYmF94vEBb4jcY",
id: "3XTBHxTtUAtI516GeXZsVIKjBPYVNIYmF94vEBb4jcY",
leader: "http://localhost:8080",
helper: "http://localhost:8081",
timePrecisionSeconds: 3600,
Expand Down
4 changes: 2 additions & 2 deletions packages/dap/src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ export class PlaintextInputShare implements Encodable {

export class InputShareAad implements Encodable {
constructor(
public taskId: TaskId,
public id: TaskId,
public metadata: ReportMetadata,
public publicShare: Buffer,
) {}

encode(): Buffer {
return Buffer.concat([
this.taskId.encode(),
this.id.encode(),
this.metadata.encode(),
encodeOpaque32(this.publicShare),
]);
Expand Down
93 changes: 45 additions & 48 deletions packages/dap/src/task.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ function buildParams(): {
bits: number;
helper: string;
leader: string;
taskId: TaskId;
id: TaskId;
timePrecisionSeconds: number;
} {
return {
type: "sum",
bits: 16,
leader: "https://a.example.com/v1",
helper: "https://b.example.com/dap/",
taskId: TaskId.random(),
id: TaskId.random(),
timePrecisionSeconds: 1,
};
}
Expand All @@ -105,28 +105,28 @@ async function withHpkeConfigs<

describe("Task", () => {
describe("constructor variations", () => {
it("accepts a string taskId that is the base64url encoding of a taskId", () => {
it("accepts a string id that is the base64url encoding of a id", () => {
const task = new Task({
...buildParams(),
taskId: "3XTBHxTtUAtI516GeXZsVIKjBPYVNIYmF94vEBb4jcY",
id: "3XTBHxTtUAtI516GeXZsVIKjBPYVNIYmF94vEBb4jcY",
});
assert.equal(
task.taskId.toString(),
task.id.toString(),
"3XTBHxTtUAtI516GeXZsVIKjBPYVNIYmF94vEBb4jcY",
);
});

it("accepts a buffer taskId", () => {
it("accepts a buffer id", () => {
const task = new Task({
...buildParams(),
taskId: Buffer.from(
id: Buffer.from(
"3XTBHxTtUAtI516GeXZsVIKjBPYVNIYmF94vEBb4jcY",
"base64url",
),
});

assert.equal(
task.taskId.toString(),
task.id.toString(),
"3XTBHxTtUAtI516GeXZsVIKjBPYVNIYmF94vEBb4jcY",
);
});
Expand All @@ -137,7 +137,7 @@ describe("Task", () => {
buckets: [10, 20, 30],
helper: "http://helper",
leader: "http://leader",
taskId: "3XTBHxTtUAtI516GeXZsVIKjBPYVNIYmF94vEBb4jcY",
id: "3XTBHxTtUAtI516GeXZsVIKjBPYVNIYmF94vEBb4jcY",
timePrecisionSeconds: 3600,
});

Expand All @@ -150,7 +150,7 @@ describe("Task", () => {
bits: 8,
helper: "http://helper",
leader: "http://leader",
taskId: "3XTBHxTtUAtI516GeXZsVIKjBPYVNIYmF94vEBb4jcY",
id: "3XTBHxTtUAtI516GeXZsVIKjBPYVNIYmF94vEBb4jcY",
timePrecisionSeconds: 3600,
});

Expand All @@ -163,7 +163,7 @@ describe("Task", () => {
bits: 8,
helper: "http://helper",
leader: "http://leader",
taskId: "3XTBHxTtUAtI516GeXZsVIKjBPYVNIYmF94vEBb4jcY",
id: "3XTBHxTtUAtI516GeXZsVIKjBPYVNIYmF94vEBb4jcY",
timePrecisionSeconds: 3600,
});

Expand All @@ -189,13 +189,13 @@ describe("Task", () => {
await buildHpkeConfigList(),
await buildHpkeConfigList(),
];
const taskId = params.taskId.buffer.toString("base64url");
const id = params.id.buffer.toString("base64url");
const fetch = mockFetch({
[`https://a.example.com/v1/hpke_config?task_id=${taskId}`]: [
[`https://a.example.com/v1/hpke_config?task_id=${id}`]: [
await hpkeConfigResponse(hpkeConfig1),
],

[`https://b.example.com/dap/hpke_config?task_id=${taskId}`]: [
[`https://b.example.com/dap/hpke_config?task_id=${id}`]: [
await hpkeConfigResponse(hpkeConfig2),
],
});
Expand All @@ -213,13 +213,13 @@ describe("Task", () => {

it("throws an error if the status is not 200", async () => {
const params = buildParams();
const taskId = params.taskId.buffer.toString("base64url");
const id = params.id.buffer.toString("base64url");

const fetch = mockFetch({
[`https://a.example.com/v1/hpke_config?task_id=${taskId}`]: [
[`https://a.example.com/v1/hpke_config?task_id=${id}`]: [
{ status: 418 },
],
[`https://b.example.com/dap/hpke_config?task_id=${taskId}`]: [
[`https://b.example.com/dap/hpke_config?task_id=${id}`]: [
{ status: 500 },
],
});
Expand All @@ -245,15 +245,15 @@ describe("Task", () => {

it("throws an error if the content type is not correct", async () => {
const params = buildParams();
const taskId = params.taskId.buffer.toString("base64url");
const id = params.id.buffer.toString("base64url");
const fetch = mockFetch({
[`https://a.example.com/v1/hpke_config?task_id=${taskId}`]: [
[`https://a.example.com/v1/hpke_config?task_id=${id}`]: [
{
contentType: "application/text",
body: (await buildHpkeConfigList()).encode(),
},
],
[`https://b.example.com/dap/hpke_config?task_id=${taskId}`]: [
[`https://b.example.com/dap/hpke_config?task_id=${id}`]: [
{
contentType: "application/text",
body: (await buildHpkeConfigList()).encode(),
Expand All @@ -272,7 +272,7 @@ describe("Task", () => {
const privateKeys = [] as [CryptoKey, number][];
const task = new Task({
...buildParams(),
taskId: new TaskId(Buffer.alloc(32, 1)),
id: new TaskId(Buffer.alloc(32, 1)),
});
const kem = new DhkemP256HkdfSha256();
const kdf = KdfId.HkdfSha256;
Expand Down Expand Up @@ -301,7 +301,7 @@ describe("Task", () => {
);

const aad = Buffer.concat([
task.taskId.encode(),
task.id.encode(),
report.metadata.encode(),
encodeOpaque32(report.publicShare),
]);
Expand Down Expand Up @@ -367,12 +367,12 @@ describe("Task", () => {

it("fetches hpke configs if needed", async () => {
const params = buildParams();
const taskId = params.taskId.buffer.toString("base64url");
const id = params.id.buffer.toString("base64url");
const fetch = mockFetch({
[`https://a.example.com/v1/hpke_config?task_id=${taskId}`]: [
[`https://a.example.com/v1/hpke_config?task_id=${id}`]: [
await hpkeConfigResponse(),
],
[`https://b.example.com/dap/hpke_config?task_id=${taskId}`]: [
[`https://b.example.com/dap/hpke_config?task_id=${id}`]: [
await hpkeConfigResponse(),
],
});
Expand All @@ -386,9 +386,9 @@ describe("Task", () => {
describe("sending reports", () => {
it("can succeed", async () => {
const params = buildParams();
const taskId = params.taskId.buffer.toString("base64url");
const id = params.id.buffer.toString("base64url");
const fetch = mockFetch({
[`https://a.example.com/v1/tasks/${taskId}/reports`]: [{ status: 201 }],
[`https://a.example.com/v1/tasks/${id}/reports`]: [{ status: 201 }],
});
const task = await withHpkeConfigs(new Task(params));
task.fetch = fetch;
Expand All @@ -397,10 +397,7 @@ describe("Task", () => {
assert.equal(fetch.calls.length, 1);
const [[url, args]] = fetch.calls;
const request = new Request(url, args);
assert.equal(
request.url,
`https://a.example.com/v1/tasks/${taskId}/reports`,
);
assert.equal(request.url, `https://a.example.com/v1/tasks/${id}/reports`);
assert(!!args);
assert.deepEqual(args.body, report.encode());
assert.equal(request.method, "PUT");
Expand All @@ -423,15 +420,15 @@ describe("Task", () => {
describe("sending measurement", () => {
it("makes the correct number of http requests when all goes well", async () => {
const params = buildParams();
const taskId = params.taskId.buffer.toString("base64url");
const id = params.id.buffer.toString("base64url");
const fetch = mockFetch({
[`https://a.example.com/v1/hpke_config?task_id=${taskId}`]: [
[`https://a.example.com/v1/hpke_config?task_id=${id}`]: [
await hpkeConfigResponse(),
],
[`https://b.example.com/dap/hpke_config?task_id=${taskId}`]: [
[`https://b.example.com/dap/hpke_config?task_id=${id}`]: [
await hpkeConfigResponse(),
],
[`https://a.example.com/v1/tasks/${taskId}/reports`]: [{ status: 201 }],
[`https://a.example.com/v1/tasks/${id}/reports`]: [{ status: 201 }],
});

const task = new Task(params);
Expand All @@ -442,17 +439,17 @@ describe("Task", () => {

it("retries once if the configs were outdated", async () => {
const params = buildParams();
const taskId = params.taskId.buffer.toString("base64url");
const id = params.id.buffer.toString("base64url");
const fetch = mockFetch({
[`https://a.example.com/v1/hpke_config?task_id=${taskId}`]: [
[`https://a.example.com/v1/hpke_config?task_id=${id}`]: [
await hpkeConfigResponse(),
await hpkeConfigResponse(),
],
[`https://b.example.com/dap/hpke_config?task_id=${taskId}`]: [
[`https://b.example.com/dap/hpke_config?task_id=${id}`]: [
await hpkeConfigResponse(),
await hpkeConfigResponse(),
],
[`https://a.example.com/v1/tasks/${taskId}/reports`]: [
[`https://a.example.com/v1/tasks/${id}/reports`]: [
{
status: 400,
contentType: "application/problem+json",
Expand All @@ -464,7 +461,7 @@ describe("Task", () => {
detail:
"The message was generated using an outdated configuration.",
instance: "..",
taskid: params.taskId.toString(),
taskid: params.id.toString(),
}),
},
{},
Expand All @@ -479,19 +476,19 @@ describe("Task", () => {

it("does not retry more than once if the [refetched] configs were [still] outdated", async () => {
const params = buildParams();
const taskId = params.taskId.buffer.toString("base64url");
const id = params.id.buffer.toString("base64url");
const fetch = mockFetch({
[`https://a.example.com/v1/hpke_config?task_id=${taskId}`]: [
[`https://a.example.com/v1/hpke_config?task_id=${id}`]: [
await hpkeConfigResponse(),
await hpkeConfigResponse(),
],
[`https://b.example.com/dap/hpke_config?task_id=${taskId}`]: [
[`https://b.example.com/dap/hpke_config?task_id=${id}`]: [
await hpkeConfigResponse(),
await hpkeConfigResponse(),
],
[`https://a.example.com/v1/tasks/${taskId}/reports`]: [
outdatedConfigResponse(params.taskId),
outdatedConfigResponse(params.taskId),
[`https://a.example.com/v1/tasks/${id}/reports`]: [
outdatedConfigResponse(params.id),
outdatedConfigResponse(params.id),
],
});

Expand All @@ -506,7 +503,7 @@ describe("Task", () => {
});
});

function outdatedConfigResponse(taskId: TaskId): ResponseSpec {
function outdatedConfigResponse(id: TaskId): ResponseSpec {
return {
status: 400,
contentType: "application/problem+json",
Expand All @@ -516,7 +513,7 @@ function outdatedConfigResponse(taskId: TaskId): ResponseSpec {
status: 400,
detail: "The message was generated using an outdated configuration.",
instance: "..",
taskid: taskId.toString(),
taskid: id.toString(),
}),
};
}
Expand Down
26 changes: 12 additions & 14 deletions packages/dap/src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface ClientParameters {
either as a Buffer, a {@linkcode TaskId} or a base64url-encoded
string
**/
taskId: TaskId | Buffer | string;
id: TaskId | Buffer | string;
/**
the url of the leader aggregator, specified as either a string
or a {@linkcode URL}
Expand Down Expand Up @@ -103,7 +103,7 @@ export class Task<
Measurement extends VdafMeasurement<Spec>,
> {
#vdaf: ClientVdaf<Measurement>;
#taskId: TaskId;
#id: TaskId;
#aggregators: Aggregator[];
#timePrecisionSeconds: number;
#extensions: Extension[] = [];
Expand All @@ -118,7 +118,7 @@ export class Task<
*/
constructor(parameters: ClientParameters & Spec) {
this.#vdaf = vdafFromSpec(parameters) as ClientVdaf<Measurement>;
this.#taskId = taskIdFromDefinition(parameters.taskId);
this.#id = idFromDefinition(parameters.id);
this.#aggregators = aggregatorsFromParameters(parameters);
if (typeof parameters.timePrecisionSeconds !== "number") {
throw new Error("timePrecisionSeconds must be a number");
Expand Down Expand Up @@ -146,8 +146,8 @@ export class Task<

/** @internal */
//this exists for testing, and should not be considered part of the public api.
get taskId(): TaskId {
return this.#taskId;
get id(): TaskId {
return this.#id;
}

/**
Expand Down Expand Up @@ -177,7 +177,7 @@ export class Task<

const time = roundedTime(this.#timePrecisionSeconds, options?.timestamp);
const metadata = new ReportMetadata(reportId, time);
const aad = new InputShareAad(this.taskId, metadata, publicShare);
const aad = new InputShareAad(this.id, metadata, publicShare);
const ciphertexts = await Promise.all(
this.#aggregators.map((aggregator, i) =>
aggregator.seal(
Expand All @@ -200,9 +200,9 @@ export class Task<
async sendReport(report: Report) {
const body = report.encode();
const leader = this.#aggregators[0];
const taskId = this.#taskId.toString();
const id = this.#id.toString();
const response = await this.#fetch(
new URL(`tasks/${taskId}/reports`, leader.url).toString(),
new URL(`tasks/${id}/reports`, leader.url).toString(),
{
method: "PUT",
headers: { "Content-Type": CONTENT_TYPES.REPORT },
Expand Down Expand Up @@ -276,7 +276,7 @@ export class Task<
await Promise.all(
this.#aggregators.map(async (aggregator) => {
const url = new URL("hpke_config", aggregator.url);
url.searchParams.append("task_id", this.#taskId.toString());
url.searchParams.append("task_id", this.#id.toString());

const response = await this.#fetch(url.toString(), {
headers: { Accept: CONTENT_TYPES.HPKE_CONFIG_LIST },
Expand Down Expand Up @@ -314,11 +314,9 @@ function aggregatorsFromParameters({
return [Aggregator.leader(leader), Aggregator.helper(helper)];
}

function taskIdFromDefinition(
taskIdDefinition: Buffer | TaskId | string,
): TaskId {
if (taskIdDefinition instanceof TaskId) return taskIdDefinition;
else return new TaskId(taskIdDefinition);
function idFromDefinition(idDefinition: Buffer | TaskId | string): TaskId {
if (idDefinition instanceof TaskId) return idDefinition;
else return new TaskId(idDefinition);
}

function roundedTime(timePrecisionSeconds: number, date?: Date): number {
Expand Down
Loading

0 comments on commit e12f4aa

Please sign in to comment.