Skip to content

Commit 6668151

Browse files
committed
fix(tempo): remember direct session fallback
1 parent 3c0500b commit 6668151

2 files changed

Lines changed: 43 additions & 37 deletions

File tree

src/tempo/session/client/Session.test.ts

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -531,69 +531,61 @@ describe('precompile client session', () => {
531531
},
532532
)
533533

534-
test('keeps a selected direct channel direct when a machine channel later appears', async () => {
535-
const directChannelId = Channel.computeId({
536-
...descriptor,
534+
test('keeps a direct fallback selected when a persisted machine route recovers', async () => {
535+
const machineDescriptor = getMachineDescriptor()
536+
const machineChannelId = Channel.computeId({
537+
...machineDescriptor,
538+
chainId,
539+
escrow: tip20ChannelEscrow,
540+
})
541+
const machineKey = channelKey({
537542
chainId,
538543
escrow: tip20ChannelEscrow,
544+
payee: machineDescriptor.payee,
545+
token: machineDescriptor.token,
539546
})
540547
const channelStore = createChannelStore()
541548
await channelStore.set({
542549
chainId,
543-
channelId: directChannelId,
550+
channelId: machineChannelId,
544551
cumulativeAmount: 100n,
545552
deposit: 1_000n,
546-
descriptor,
553+
descriptor: machineDescriptor,
547554
escrow: tip20ChannelEscrow,
548555
opened: true,
549556
})
557+
let currentClient = createMachineClient({ routeError: true })
550558
const method = session({
551559
account,
552560
channelStore,
553561
decimals: 0,
554-
getClient: () => machineClient,
562+
getClient: () => currentClient,
555563
})
556564
const challenge = () =>
557565
makeSessionChallenge({
558566
methodDetails: { escrowContract: tip20ChannelEscrow, machineTokenEnabled: true },
559567
})
560568

561-
expect(
562-
deserialize(await method.createCredential({ challenge: challenge(), context: {} })),
563-
).toMatchObject({
564-
action: 'voucher',
565-
channelId: directChannelId,
566-
cumulativeAmount: '200',
567-
})
568-
569-
const machineDescriptor = getMachineDescriptor()
570-
const machineChannelId = Channel.computeId({
571-
...machineDescriptor,
572-
chainId,
573-
escrow: tip20ChannelEscrow,
574-
})
575-
const machineKey = channelKey({
576-
chainId,
577-
escrow: tip20ChannelEscrow,
578-
payee: machineDescriptor.payee,
579-
token: machineDescriptor.token,
580-
})
581-
await channelStore.set({
582-
chainId,
583-
channelId: machineChannelId,
584-
cumulativeAmount: 100n,
585-
deposit: 1_000n,
586-
descriptor: machineDescriptor,
587-
escrow: tip20ChannelEscrow,
588-
opened: true,
569+
const opened = deserialize(
570+
await method.createCredential({ challenge: challenge(), context: {} }),
571+
)
572+
expect(opened).toMatchObject({
573+
action: 'open',
574+
descriptor: {
575+
operator: descriptor.operator,
576+
payee: descriptor.payee,
577+
token: descriptor.token,
578+
},
579+
cumulativeAmount: '100',
589580
})
581+
currentClient = machineClient
590582

591583
expect(
592584
deserialize(await method.createCredential({ challenge: challenge(), context: {} })),
593585
).toMatchObject({
594586
action: 'voucher',
595-
channelId: directChannelId,
596-
cumulativeAmount: '300',
587+
channelId: opened.channelId,
588+
cumulativeAmount: '200',
597589
})
598590
expect(await channelStore.get(machineKey)).toMatchObject({ cumulativeAmount: 100n })
599591
})

src/tempo/session/client/Session.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,20 @@ export function session(parameters: session.Parameters = {}) {
447447
// signatures succeed. Low-level Fetch opens remain staged until the
448448
// response acknowledges them; SessionManager owns its own boundary.
449449
const deferState = !!machineSession || (attempt !== undefined && plan.type === 'open')
450+
const trackedSink = {
451+
store: {
452+
get: (key: string) => store.get(key),
453+
async set(next: ChannelEntry) {
454+
await store.set(next)
455+
rememberRail(direct, resolved, next)
456+
},
457+
async delete(key: string) {
458+
await store.delete(key)
459+
rails.delete(direct.key)
460+
},
461+
},
462+
notifyUpdate: sink.notifyUpdate,
463+
}
450464
const credentialSink = deferState
451465
? {
452466
store: {
@@ -460,7 +474,7 @@ export function session(parameters: session.Parameters = {}) {
460474
pendingEntry = next
461475
},
462476
}
463-
: sink
477+
: trackedSink
464478
const feeToken = MachineTokenSession.resolveFeeToken({
465479
chainId: resolved.chainId,
466480
override: feeTokenParameter,

0 commit comments

Comments
 (0)