Skip to content

Commit

Permalink
More Lemonade instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
robknight committed Nov 13, 2024
1 parent e37dd16 commit 59586a4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -781,45 +781,50 @@ export class LemonadePipeline implements BasePipeline {
email: string,
identityCommitment: string
): Promise<EdDSATicketPCD[]> {
// Load atom-backed tickets
const relevantTickets = await this.db.loadByEmail(this.id, email);
return traced(LOG_NAME, "getTicketsForEmail", async () => {
// Load atom-backed tickets

// Load check-in data
const checkIns = await this.checkinDB.getByTicketIds(
client,
this.id,
relevantTickets.map((ticket) => ticket.id)
);
const checkInsById = _.keyBy(checkIns, (checkIn) => checkIn.ticketId);
const relevantTickets = await this.db.loadByEmail(this.id, email);

// Convert atoms to ticket data
const ticketDatas = relevantTickets.map((t) => {
if (checkInsById[t.id]) {
t.checkinDate = checkInsById[t.id].timestamp;
}
return this.atomToTicketData(t, identityCommitment);
});
// Load manual tickets from the definition
const manualTickets = this.getManualTicketsForEmail(email);
// Convert manual tickets to ticket data and add to array
ticketDatas.push(
...(await Promise.all(
manualTickets.map((manualTicket) =>
this.manualTicketToTicketData(
client,
manualTicket,
identityCommitment
)
// Load check-in data
const checkIns = await traced(LOG_NAME, "get checkins", async () =>
this.checkinDB.getByTicketIds(
client,
this.id,
relevantTickets.map((ticket) => ticket.id)
)
))
);
);
const checkInsById = _.keyBy(checkIns, (checkIn) => checkIn.ticketId);

// Turn ticket data into PCDs
const tickets = await Promise.all(
ticketDatas.map((t) => this.getOrGenerateTicket(t))
);
// Convert atoms to ticket data
const ticketDatas = relevantTickets.map((t) => {
if (checkInsById[t.id]) {
t.checkinDate = checkInsById[t.id].timestamp;
}
return this.atomToTicketData(t, identityCommitment);
});
// Load manual tickets from the definition
const manualTickets = this.getManualTicketsForEmail(email);
// Convert manual tickets to ticket data and add to array
ticketDatas.push(
...(await Promise.all(
manualTickets.map((manualTicket) =>
this.manualTicketToTicketData(
client,
manualTicket,
identityCommitment
)
)
))
);

return tickets;
// Turn ticket data into PCDs
const tickets = await Promise.all(
ticketDatas.map((t) => this.getOrGenerateTicket(t))
);

return tickets;
});
}

private async issueLemonadeTicketPCDs(
Expand Down Expand Up @@ -893,8 +898,11 @@ export class LemonadePipeline implements BasePipeline {
});
}

const ticketPCDs = await Promise.all(
tickets.map((t) => EdDSATicketPCDPackage.serialize(t))
const ticketPCDs = await traced(
LOG_NAME,
"serialize tickets",
async () =>
Promise.all(tickets.map((t) => EdDSATicketPCDPackage.serialize(t)))
);

ticketActions.push({
Expand All @@ -903,54 +911,13 @@ export class LemonadePipeline implements BasePipeline {
pcds: ticketPCDs
});

const contactsFolder = `${this.definition.options.feedOptions.feedFolder}/contacts`;
const contacts = (
await Promise.all(
emails.map((e) => this.getReceivedContactsForEmail(client, e.email))
)
).flat();
const contactActions: PCDAction[] = [
{
type: PCDActionType.DeleteFolder,
folder: contactsFolder,
recursive: true
},
{
type: PCDActionType.ReplaceInFolder,
folder: contactsFolder,
pcds: contacts
}
];

const badgeFolder = `${this.definition.options.feedOptions.feedFolder}/badges`;

const badges = (
await Promise.all(
emails.map((e) => this.getReceivedBadgesForEmail(client, e.email))
)
).flat();
const badgeActions: PCDAction[] = [
{
type: PCDActionType.DeleteFolder,
folder: badgeFolder,
recursive: true
},
{
type: PCDActionType.ReplaceInFolder,
folder: badgeFolder,
pcds: badges
}
];

traceFlattenedObject(span, {
pcds_issued: tickets.length + badges.length + contacts.length,
tickets_issued: tickets.length,
badges_issued: badges.length,
contacts_issued: contacts.length
pcds_issued: tickets.length,
tickets_issued: tickets.length
});

return {
actions: [...ticketActions, ...contactActions, ...badgeActions]
actions: [...ticketActions]
};
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { LRUCache } from "lru-cache";
import { Pool } from "postgres-pool";
import { loadZupassEdDSAPublicKey } from "../../issuanceService";
import { traced } from "../../telemetryService";

/**
* Manages server-side verification of credential PCDs.
Expand Down Expand Up @@ -62,26 +63,35 @@ export class CredentialSubservice {
public async verifyAndExpectZupassEmail(
credential: Credential
): Promise<VerifiedCredential> {
const verifiedCredential = await this.verify(credential);
return traced(
"CredentialSubservice",
"verifyAndExpectZupassEmail",
async () => {
const verifiedCredential = await this.verify(credential);

if (!verifiedCredential.emails || verifiedCredential.emails.length === 0) {
throw new VerificationError("Missing Email PCDs");
}
if (
!verifiedCredential.emails ||
verifiedCredential.emails.length === 0
) {
throw new VerificationError("Missing Email PCDs");
}

for (const signedEmail of verifiedCredential.emails) {
const { email, semaphoreId, signer } = signedEmail;
for (const signedEmail of verifiedCredential.emails) {
const { email, semaphoreId, signer } = signedEmail;

if (!email || !semaphoreId) {
throw new VerificationError("Missing email PCD in credential");
}
if (!verifiedCredential.authKey && !this.isZupassPublicKey(signer)) {
throw new VerificationError(
`Email PCD not signed by Zupass. expected ${this.zupassPublicKey} but got ${signer}`
);
}
}
if (!email || !semaphoreId) {
throw new VerificationError("Missing email PCD in credential");
}
if (!verifiedCredential.authKey && !this.isZupassPublicKey(signer)) {
throw new VerificationError(
`Email PCD not signed by Zupass. expected ${this.zupassPublicKey} but got ${signer}`
);
}
}

return { ...verifiedCredential, emails: verifiedCredential.emails };
return { ...verifiedCredential, emails: verifiedCredential.emails };
}
);
}

/**
Expand Down

0 comments on commit 59586a4

Please sign in to comment.