Skip to content

Commit

Permalink
Entryway: non-transactional createAccount (#2109)
Browse files Browse the repository at this point in the history
make create account non transactional
  • Loading branch information
dholms authored Feb 1, 2024
1 parent c7d95e9 commit 5f8f094
Showing 1 changed file with 46 additions and 23 deletions.
69 changes: 46 additions & 23 deletions packages/pds/src/api/com/atproto/server/createAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Database from '../../../../db'
import { didDocForSession } from './util'
import { getPdsEndpoint } from '../../../../pds-agents'
import { isThisPds } from '../../../proxy'
import { dbLogger as log } from '../../../../logger'

export default function (server: Server, ctx: AppContext) {
server.com.atproto.server.createAccount({
Expand Down Expand Up @@ -96,19 +97,6 @@ export default function (server: Server, ctx: AppContext) {
throw err
}

// Generate a real did with PLC
if (plcOp && !entrywayAssignedPds) {
try {
await ctx.plcClient.sendOperation(did, plcOp)
} catch (err) {
req.log.error(
{ didKey: ctx.plcRotationKey.did(), handle },
'failed to create did:plc',
)
throw err
}
}

// insert invite code use
if (ctx.cfg.invites.required && inviteCode) {
await ensureCodeIsAvailable(dbTxn, inviteCode, true)
Expand All @@ -132,6 +120,10 @@ export default function (server: Server, ctx: AppContext) {
.execute()
}

if (!entrywayAssignedPds) {
await repoTxn.createRepo(did, [], now)
}

const { access, refresh } = await ctx.services
.auth(dbTxn)
.createSession({
Expand All @@ -140,6 +132,15 @@ export default function (server: Server, ctx: AppContext) {
appPasswordName: null,
})

return {
did,
pdsDid: entrywayAssignedPds?.did ?? null,
accessJwt: access,
refreshJwt: refresh,
}
})

try {
if (entrywayAssignedPds) {
const agent = ctx.pdsAgents.get(entrywayAssignedPds.host)
await agent.com.atproto.server.createAccount({
Expand All @@ -149,17 +150,21 @@ export default function (server: Server, ctx: AppContext) {
recoveryKey: input.body.recoveryKey,
})
} else {
// Setup repo root
await repoTxn.createRepo(did, [], now)
}

return {
did,
pdsDid: entrywayAssignedPds?.did ?? null,
accessJwt: access,
refreshJwt: refresh,
assert(plcOp)
try {
await ctx.plcClient.sendOperation(did, plcOp)
} catch (err) {
req.log.error(
{ didKey: ctx.plcRotationKey.did(), handle },
'failed to create did:plc',
)
throw err
}
}
})
} catch (err) {
await cleanupUncreatedAccount(ctx, did)
throw err
}

const didDoc = await didDocForSession(ctx, result)

Expand Down Expand Up @@ -496,3 +501,21 @@ const randomIndexByWeight = (weights) => {
const rand = Math.random() * sum
return cumulative.findIndex((item) => item >= rand)
}

const cleanupUncreatedAccount = async (
ctx: AppContext,
did: string,
tries = 0,
) => {
if (tries > 3) return
try {
await Promise.all([
ctx.services.account(ctx.db).deleteAccount(did),
ctx.services.record(ctx.db).deleteForActor(did),
ctx.services.repo(ctx.db).deleteRepo(did),
])
} catch (err) {
log.error({ err, did, tries }, 'failed to clean up partially created user')
return cleanupUncreatedAccount(ctx, did, tries + 1)
}
}

0 comments on commit 5f8f094

Please sign in to comment.