retry-send.ts (6904B)
1 import { makeFixture as makeRecoveryFixture } from './resume-preparation.ts'; 2 import { 3 validateRelayPolicy, 4 readRelayPolicy 5 } from '../../../src/lib/config/relays.ts'; 6 import { createInboxRoutePlan } from '../../../src/lib/messaging/inbox-routing.ts'; 7 import { 8 createPrivateSession, 9 closePrivateSession 10 } from '../../../src/lib/runtime/private-session.ts'; 11 import { 12 getPrivatePool, 13 closePrivatePool 14 } from '../../../src/lib/nostr/private-pool.ts'; 15 import { 16 resumeEncryptedPreparation, 17 qualifyRecoveredPrivatePair, 18 resumePreparationSnapshot, 19 resumeRecoveredRumor, 20 stopResumePreparation 21 } from '../../../src/lib/messaging/resume-preparation.ts'; 22 import { rumorPlanSnapshot } from '../../../src/lib/messaging/rumor-plan.ts'; 23 import { decodePrivateRecord } from '../../../src/lib/persistence/private-records.ts'; 24 import { 25 capturePrivateRetry, 26 runPrivateRetry, 27 stopPrivateRetry, 28 privateRetrySnapshot, 29 type PrivateRetry 30 } from '../../../src/lib/messaging/retry-send.ts'; 31 import { 32 pairedDeliveryAcknowledgementSnapshot, 33 commitPairedDeliveryReceipt, 34 type PairedDeliveryAcknowledgement 35 } from '../../../src/lib/persistence/private-sends.ts'; 36 export async function makeFixture(initialise = true) { 37 const f = await makeRecoveryFixture(initialise); 38 const policy = validateRelayPolicy( 39 JSON.stringify({ 40 ...readRelayPolicy(f.context.policy), 41 messagingEnabled: true 42 }) 43 ); 44 if (!policy) throw Error('invalid private fixture policy'); 45 const plan = createInboxRoutePlan( 46 policy, 47 f.owner, 48 f.peer, 49 f.context.own, 50 f.context.other 51 ); 52 if (!plan) throw Error('missing private route plan'); 53 const context = { ...f.context, policy, plan }; 54 const session = await createPrivateSession( 55 f.identity, 56 'reviewed_private_session' 57 ); 58 if (!session) throw Error('missing genuine private session'); 59 const pool = getPrivatePool(session, policy, [ 60 'wss://peer.example.org', 61 'wss://archive.example.org' 62 ]); 63 if (!pool) throw Error('missing private pool'); 64 const resume = f.capture(); 65 let receipt: PairedDeliveryAcknowledgement | undefined, 66 retry: PrivateRetry | undefined; 67 let restoreFault = () => {}, 68 injected = 0; 69 async function prepare() { 70 const recovered = await resumeEncryptedPreparation( 71 resume, 72 context, 73 'reviewed_private_resume' 74 ); 75 if (!['prepared', 'already_prepared'].includes(recovered.status)) 76 return recovered; 77 const qualified = await qualifyRecoveredPrivatePair( 78 resume, 79 context, 80 'reviewed_stored_private_retry' 81 ); 82 if ('receipt' in qualified) receipt = qualified.receipt; 83 return { status: qualified.status }; 84 } 85 function capture(review: unknown = 'reviewed_private_retry') { 86 retry = receipt 87 ? capturePrivateRetry( 88 f.repository, 89 session!, 90 receipt, 91 context, 92 pool!, 93 review 94 ) 95 : undefined; 96 return !!retry; 97 } 98 return { 99 ...f, 100 async row() { 101 const value = await f.row(); 102 if (!value) throw Error('missing actual paired row'); 103 return value; 104 }, 105 prepare, 106 capture, 107 run: (review: unknown = 'reviewed_private_retry') => 108 retry 109 ? runPrivateRetry(retry, review) 110 : Promise.resolve({ status: 'invalid' as const }), 111 snapshot: () => retry && privateRetrySnapshot(retry), 112 proof: () => receipt && pairedDeliveryAcknowledgementSnapshot(receipt), 113 recovered: () => ({ 114 snapshot: resumePreparationSnapshot(resume), 115 rumor: !!rumorPlanSnapshot(resumeRecoveredRumor(resume)!) 116 }), 117 stop: () => { 118 if (retry) stopPrivateRetry(retry); 119 }, 120 clearRecovery: () => stopResumePreparation(resume), 121 countsValue: () => f.counts(), 122 async controlledArchiveSuccessor() { 123 if (!receipt) throw Error('missing genuine custody'); 124 const row = (await f.row())?.record; 125 if (!row) throw Error('missing actual ciphertext'); 126 // Controlled metadata race, not a claim of an additional relay ACK. 127 return commitPairedDeliveryReceipt( 128 f.repository, 129 receipt, 130 JSON.stringify({ 131 actionId: '12345678-1234-4234-8234-123456789abe', 132 role: 'self_archive', 133 origin: 'wss://archive.example.org', 134 attempt: 1, 135 eventId: row.self.eventId, 136 status: 'accepted', 137 observedAtMilliseconds: 100, 138 readbackWire: null 139 }) 140 ); 141 }, 142 async injectUnacknowledgedSuccessBetweenReads() { 143 const stored = await f.row(); 144 if (!stored?.record.peerArtifact || !stored.record.deliveryPlan) 145 throw Error('missing real paired record'); 146 const row = stored.record; 147 const facts = [ 148 row.deliveryPlan!.routes.peer, 149 row.deliveryPlan!.routes.archive 150 ].flatMap((route) => 151 route.targets.map((origin) => ({ 152 actionId: '12345678-1234-4234-8234-123456789abf', 153 role: route.role, 154 origin, 155 attempt: 1, 156 eventId: 157 route.role === 'peer' 158 ? row.peerArtifact!.eventId 159 : row.self.eventId, 160 status: 'accepted', 161 observedAtMilliseconds: 100, 162 readbackWire: null 163 })) 164 ); 165 const wire = JSON.stringify({ 166 ...row, 167 revision: row.revision + 1, 168 receipts: [...(row.receipts ?? []), ...facts] 169 }); 170 if (!decodePrivateRecord(wire, f.owner, f.command).ok) 171 throw Error('invalid controlled race row'); 172 const native = Reflect.get<IDBIndex, 'openCursor'>( 173 IDBIndex.prototype, 174 'openCursor' 175 ); 176 IDBIndex.prototype.openCursor = function ( 177 ...args: Parameters<IDBIndex['openCursor']> 178 ) { 179 const request = native.apply(this, args); 180 if (this.objectStore.name === 'private_sends' && injected === 0) { 181 injected++; 182 // Queue real RW behind first genuine verification and ahead of the 183 // next load. No opaque acknowledgement is renewed by this mutation. 184 const transaction = this.objectStore.transaction.db.transaction( 185 ['private_sends'], 186 'readwrite' 187 ); 188 transaction 189 .objectStore('private_sends') 190 .put({ owner: f.owner, id: f.command, wire }); 191 } 192 return request; 193 }; 194 restoreFault = () => { 195 IDBIndex.prototype.openCursor = native; 196 }; 197 return { 198 originalWire: stored.wire, 199 changedWire: wire, 200 revision: row.revision 201 }; 202 }, 203 injected: () => injected, 204 clearFault() { 205 restoreFault(); 206 restoreFault = () => {}; 207 }, 208 close() { 209 restoreFault(); 210 if (retry) stopPrivateRetry(retry); 211 stopResumePreparation(resume); 212 closePrivatePool(pool); 213 closePrivateSession(session); 214 f.close(); 215 } 216 }; 217 }