budgets.test.ts (7765B)
1 import assert from 'node:assert/strict'; 2 import { test } from 'node:test'; 3 import { generateSecretKey, finalizeEvent } from 'applesauce-core/helpers'; 4 import { validateRelayPolicy } from '../../src/lib/config/relays.ts'; 5 import { 6 PUBLIC_INGRESS_BUDGETS, 7 PUBLIC_REQUEST_BUDGETS 8 } from '../../src/lib/config/budgets.ts'; 9 import { 10 createPublicScheduler, 11 createPublicRun, 12 openPublicRequest, 13 publicRunSnapshot, 14 publicRequestScopeSnapshot, 15 publicSchedulerSnapshot, 16 closePublicScheduler, 17 type RequestClock, 18 type RequestKind 19 } from '../../src/lib/nostr/request-scope.ts'; 20 import { 21 createExtensionScheduler, 22 runExtensionAction, 23 callExtension, 24 markExtensionWaitExpired, 25 extensionSchedulerSnapshot, 26 type ExtensionAction 27 } from '../../src/lib/nostr/extension-scheduler.ts'; 28 import type { PublicPoolMessage } from '../../src/lib/nostr/exports.ts'; 29 const origin = 'wss://aggregate.example.org'; 30 const policy = validateRelayPolicy( 31 JSON.stringify({ 32 schemaVersion: 1, 33 public: [{ origin, read: true, write: false, nip50: false }], 34 inbox: [], 35 postingEnabled: false, 36 messagingEnabled: false, 37 operatorDenylist: [] 38 }) 39 ); 40 assert.ok(policy); 41 function channels() { 42 let now = 0; 43 const timers = new Map<symbol, () => void>(); 44 const clock: RequestClock = { 45 now: () => now, 46 schedule(callback) { 47 const id = Symbol(); 48 timers.set(id, callback); 49 return () => { 50 timers.delete(id); 51 }; 52 } 53 }; 54 const scheduler = createPublicScheduler(clock); 55 const run = createPublicRun(scheduler, policy!); 56 let closed = 0, 57 verified = 0; 58 const sinks: ((message: PublicPoolMessage) => void)[] = []; 59 const kinds: RequestKind[] = [ 60 'search', 61 'head', 62 'deletion', 63 'profile', 64 'search', 65 'head' 66 ]; 67 const requests = kinds.map((kind) => 68 openPublicRequest( 69 run, 70 kind, 71 (next) => { 72 sinks.push(next); 73 return () => { 74 closed++; 75 }; 76 }, 77 () => { 78 verified++; 79 } 80 ) 81 ); 82 return { 83 scheduler, 84 run, 85 requests, 86 sinks, 87 timers, 88 counts: () => ({ closed, verified }), 89 advance(value: number) { 90 now = value; 91 }, 92 close: () => closePublicScheduler(scheduler) 93 }; 94 } 95 await test('aggregate primary and auxiliary bytes, rejects and duplicate costs stop all six scopes while an outstanding extension call remains occupied', async () => { 96 const key = generateSecretKey(), 97 f = channels(), 98 extension = createExtensionScheduler(); 99 const capture = { owner: null, session: Symbol(), operation: Symbol() }; 100 let action: ExtensionAction | undefined, 101 release: (() => void) | undefined, 102 invoked = 0; 103 const held = new Promise<void>((resolve) => { 104 release = resolve; 105 }); 106 const pending = runExtensionAction( 107 extension, 108 capture, 109 () => true, 110 async (value) => { 111 action = value; 112 return callExtension(value, 'decrypt', () => { 113 invoked++; 114 return held; 115 }); 116 } 117 ); 118 try { 119 assert.equal(invoked, 1); 120 assert.ok(action); 121 assert.throws( 122 () => 123 openPublicRequest( 124 f.run, 125 'profile', 126 () => { 127 throw Error('seventh effect'); 128 }, 129 () => {} 130 ), 131 /public_request_concurrency_limit/u 132 ); 133 const good = finalizeEvent( 134 { 135 kind: 30402, 136 created_at: 1700000000, 137 tags: [['d', 'unicode']], 138 content: '😀é'.repeat(20) 139 }, 140 key 141 ); 142 const bad = { ...good, sig: '0'.repeat(128) }; 143 const event = (candidate: typeof good): PublicPoolMessage => ({ 144 type: 'EVENT', 145 id: 'bounded', 146 from: origin, 147 event: candidate 148 }); 149 f.sinks[0](event(good)); 150 f.sinks[1](event(good)); 151 f.sinks[2](event(bad)); 152 assert.equal(f.counts().verified, 2); 153 const first = publicRunSnapshot(f.run).ingress; 154 assert.equal(first.deliveries, 3); 155 assert.ok( 156 first.chargedBytes > new TextEncoder().encode(good.content).length * 3 157 ); 158 assert.equal(first.stopped, false); 159 const rawOversize = { 160 ...good, 161 content: '😀'.repeat(PUBLIC_INGRESS_BUDGETS.eventBytes / 4 + 1) 162 }; 163 for (let count = 0; !publicRunSnapshot(f.run).ingress.stopped; count++) { 164 assert.ok(count < 33, 'finite actual accounting reaches 8MiB'); 165 f.sinks[count % f.sinks.length](event(rawOversize)); 166 } 167 const final = publicRunSnapshot(f.run); 168 assert.equal( 169 final.ingress.chargedBytes, 170 PUBLIC_INGRESS_BUDGETS.processedBytes 171 ); 172 assert.equal(final.ingress.stopped, true); 173 assert.equal(final.activeRequests, 0); 174 assert.deepEqual(publicSchedulerSnapshot(f.scheduler), { 175 activeRequests: 0, 176 closed: false 177 }); 178 assert.equal(f.counts().closed, 6); 179 assert.equal(f.timers.size, 0); 180 for (const sink of f.sinks) sink(event(good)); 181 assert.deepEqual(publicRunSnapshot(f.run).ingress, final.ingress); 182 markExtensionWaitExpired(action); 183 assert.deepEqual(extensionSchedulerSnapshot(extension), { 184 state: 'active', 185 pending: 'decrypt', 186 phase: 'wait_expired' 187 }); 188 const blocked = await runExtensionAction( 189 extension, 190 capture, 191 () => true, 192 () => { 193 invoked++; 194 return Promise.resolve(undefined); 195 } 196 ); 197 assert.equal(blocked.status, 'busy'); 198 assert.equal(invoked, 1); 199 release!(); 200 const settled = await pending; 201 assert.equal(settled.status, 'wait_expired'); 202 assert.deepEqual(extensionSchedulerSnapshot(extension), { state: 'idle' }); 203 console.log( 204 JSON.stringify({ 205 fixture: 206 'HCP132_LOGICAL_APPLICATION_COMBINED_NO_PREALLOCATION_OR_LIVE_OPERATOR_CLAIM', 207 public: final.ingress, 208 requestsClosed: f.counts().closed, 209 controlled_slot_calls: invoked 210 }) 211 ); 212 } finally { 213 release?.(); 214 await pending; 215 f.close(); 216 key.fill(0); 217 } 218 }); 219 await test('all primary and auxiliary callbacks enforce captured ten and fifteen second deadlines even when timer dispatch is delayed', () => { 220 const f = channels(), 221 key = generateSecretKey(); 222 try { 223 assert.equal( 224 publicRunSnapshot(f.run).deadline, 225 PUBLIC_REQUEST_BUDGETS.runMilliseconds 226 ); 227 assert.ok( 228 f.requests.every( 229 (request) => 230 publicRequestScopeSnapshot(request).deadline === 231 PUBLIC_REQUEST_BUDGETS.requestMilliseconds 232 ) 233 ); 234 f.advance(PUBLIC_REQUEST_BUDGETS.requestMilliseconds); 235 const good = finalizeEvent( 236 { 237 kind: 30402, 238 created_at: 1700000000, 239 tags: [['d', 'deadline']], 240 content: 'bounded' 241 }, 242 key 243 ); 244 for (const sink of f.sinks) 245 sink({ type: 'EVENT', id: 'late', from: origin, event: good }); 246 assert.equal(f.counts().verified, 0); 247 assert.equal(f.counts().closed, 6); 248 assert.equal(f.timers.size, 0); 249 assert.equal(publicRunSnapshot(f.run).ingress.deliveries, 0); 250 assert.ok( 251 f.requests.every( 252 (request) => publicRequestScopeSnapshot(request).state === 'deadline' 253 ) 254 ); 255 f.advance(PUBLIC_REQUEST_BUDGETS.runMilliseconds - 1); 256 const late = openPublicRequest( 257 f.run, 258 'profile', 259 () => () => {}, 260 () => {} 261 ); 262 assert.equal( 263 publicRequestScopeSnapshot(late).deadline, 264 PUBLIC_REQUEST_BUDGETS.runMilliseconds 265 ); 266 f.advance(PUBLIC_REQUEST_BUDGETS.runMilliseconds); 267 assert.equal(publicRunSnapshot(f.run).active, false); 268 assert.throws( 269 () => 270 openPublicRequest( 271 f.run, 272 'search', 273 () => () => {}, 274 () => {} 275 ), 276 /public_run_inactive/u 277 ); 278 } finally { 279 f.close(); 280 key.fill(0); 281 } 282 });