|
| 1 | +import test from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import Module from "node:module"; |
| 4 | +import { EventEmitter } from "node:events"; |
| 5 | + |
| 6 | +// ── Mock network clients ── |
| 7 | +let mockGetHandler: (url: string, options: any, callback: (res: any) => void) => any = () => { |
| 8 | + throw new Error("mockGetHandler not set"); |
| 9 | +}; |
| 10 | + |
| 11 | +const originalLoad = (Module as any)._load; |
| 12 | +(Module as any)._load = function patchedLoad(request: string, parent: unknown, isMain: boolean) { |
| 13 | + if (request === "http" || request === "https") { |
| 14 | + return { |
| 15 | + get: (url: string, options: any, callback: (res: any) => void) => { |
| 16 | + return mockGetHandler(url, options, callback); |
| 17 | + } |
| 18 | + }; |
| 19 | + } |
| 20 | + return originalLoad.apply(this, [request, parent, isMain]); |
| 21 | +}; |
| 22 | + |
| 23 | +// Now import the LedgerFollower |
| 24 | +import { LedgerFollower } from "../src/indexer/ledger_follower"; |
| 25 | + |
| 26 | +function createMockResponse(body: string) { |
| 27 | + const res = new EventEmitter() as any; |
| 28 | + process.nextTick(() => { |
| 29 | + res.emit("data", Buffer.from(body)); |
| 30 | + res.emit("end"); |
| 31 | + }); |
| 32 | + return res; |
| 33 | +} |
| 34 | + |
| 35 | +function createMockRequest() { |
| 36 | + const req = new EventEmitter() as any; |
| 37 | + req.destroy = () => {}; |
| 38 | + return req; |
| 39 | +} |
| 40 | + |
| 41 | +// ── Test Cases ── |
| 42 | + |
| 43 | +test("LedgerFollower uses dynamic pagination and batches query ranges correctly", async () => { |
| 44 | + let dbSavedLedger = 0; |
| 45 | + const mockPool = { |
| 46 | + query: async (queryText: string, params?: any[]) => { |
| 47 | + if (queryText.includes("SELECT last_processed_ledger")) { |
| 48 | + return { rowCount: 1, rows: [{ last_processed_ledger: "500" }] }; |
| 49 | + } |
| 50 | + if (queryText.includes("INSERT INTO indexer_state")) { |
| 51 | + dbSavedLedger = params?.[1]; |
| 52 | + return { rowCount: 1, rows: [] }; |
| 53 | + } |
| 54 | + return { rowCount: 0, rows: [] }; |
| 55 | + } |
| 56 | + } as any; |
| 57 | + |
| 58 | + // Set up mock HTTP response |
| 59 | + mockGetHandler = (url: string, options: any, callback: any) => { |
| 60 | + const req = createMockRequest(); |
| 61 | + if (url.includes("/ledgers?")) { |
| 62 | + callback(createMockResponse(JSON.stringify({ |
| 63 | + _embedded: { records: [{ sequence: 1000 }] } |
| 64 | + }))); |
| 65 | + } else if (url.includes("/soroban/events?")) { |
| 66 | + // Return 0 events |
| 67 | + callback(createMockResponse(JSON.stringify({ |
| 68 | + events: [] |
| 69 | + }))); |
| 70 | + } |
| 71 | + return req; |
| 72 | + }; |
| 73 | + |
| 74 | + const follower = new LedgerFollower(mockPool, { |
| 75 | + stellarRpcUrl: "https://mock-stellar.com", |
| 76 | + contractIds: ["mock-contract"], |
| 77 | + maxBlockLimit: 100, |
| 78 | + pollIntervalMs: 1000, |
| 79 | + }); |
| 80 | + |
| 81 | + // Manually set internal state instead of start() to avoid background loop |
| 82 | + (follower as any).lastProcessedLedger = 500; |
| 83 | + (follower as any).running = true; |
| 84 | + |
| 85 | + const result = await (follower as any).pollOnce(); |
| 86 | + |
| 87 | + assert.equal(result.processed, 0); |
| 88 | + assert.equal(result.hasMore, true); // Since 600 < 1000 |
| 89 | + assert.equal(follower.status.lastProcessedLedger, 600); // 500 + maxBlockLimit (100) |
| 90 | + assert.equal(dbSavedLedger, 600); // Sequence saved persistently to indexer_state |
| 91 | +}); |
| 92 | + |
| 93 | +test("LedgerFollower applies multiplicative decrease throttling when event density is high", async () => { |
| 94 | + let dbSavedLedger = 0; |
| 95 | + const mockPool = { |
| 96 | + query: async (queryText: string, params?: any[]) => { |
| 97 | + if (queryText.includes("SELECT last_processed_ledger")) { |
| 98 | + return { rowCount: 1, rows: [{ last_processed_ledger: "500" }] }; |
| 99 | + } |
| 100 | + if (queryText.includes("INSERT INTO indexer_state")) { |
| 101 | + dbSavedLedger = params?.[1]; |
| 102 | + return { rowCount: 1, rows: [] }; |
| 103 | + } |
| 104 | + return { rowCount: 0, rows: [] }; |
| 105 | + } |
| 106 | + } as any; |
| 107 | + |
| 108 | + // Mock high density events return: 600 events inside the batch of 100 ledgers |
| 109 | + mockGetHandler = (url: string, options: any, callback: any) => { |
| 110 | + const req = createMockRequest(); |
| 111 | + if (url.includes("/ledgers?")) { |
| 112 | + callback(createMockResponse(JSON.stringify({ |
| 113 | + _embedded: { records: [{ sequence: 1000 }] } |
| 114 | + }))); |
| 115 | + } else if (url.includes("/soroban/events?")) { |
| 116 | + const mockEvents = Array.from({ length: 600 }, (_, i) => ({ |
| 117 | + id: `event-${i}`, |
| 118 | + ledger: 550, |
| 119 | + contractId: "mock-contract", |
| 120 | + topic: ["test"], |
| 121 | + value: "value" |
| 122 | + })); |
| 123 | + callback(createMockResponse(JSON.stringify({ |
| 124 | + events: mockEvents |
| 125 | + }))); |
| 126 | + } |
| 127 | + return req; |
| 128 | + }; |
| 129 | + |
| 130 | + const follower = new LedgerFollower(mockPool, { |
| 131 | + stellarRpcUrl: "https://mock-stellar.com", |
| 132 | + contractIds: ["mock-contract"], |
| 133 | + maxBlockLimit: 100, |
| 134 | + highEventDensityThreshold: 5.0, // Throttles if density > 5.0 events/block |
| 135 | + pollIntervalMs: 1000, |
| 136 | + }); |
| 137 | + |
| 138 | + (follower as any).lastProcessedLedger = 500; |
| 139 | + (follower as any).running = true; |
| 140 | + |
| 141 | + await (follower as any).pollOnce(); |
| 142 | + |
| 143 | + // Halved from 100 to 50 |
| 144 | + assert.equal(follower.status.currentBatchSize, 50); |
| 145 | + assert.equal(follower.status.isThrottled, true); |
| 146 | + assert.equal(dbSavedLedger, 600); |
| 147 | +}); |
| 148 | + |
| 149 | +test("LedgerFollower applies additive increase recovery when event density is low", async () => { |
| 150 | + const mockPool = { |
| 151 | + query: async (queryText: string) => { |
| 152 | + if (queryText.includes("SELECT last_processed_ledger")) { |
| 153 | + return { rowCount: 1, rows: [{ last_processed_ledger: "500" }] }; |
| 154 | + } |
| 155 | + return { rowCount: 0, rows: [] }; |
| 156 | + } |
| 157 | + } as any; |
| 158 | + |
| 159 | + mockGetHandler = (url: string, options: any, callback: any) => { |
| 160 | + const req = createMockRequest(); |
| 161 | + if (url.includes("/ledgers?")) { |
| 162 | + callback(createMockResponse(JSON.stringify({ |
| 163 | + _embedded: { records: [{ sequence: 1000 }] } |
| 164 | + }))); |
| 165 | + } else if (url.includes("/soroban/events?")) { |
| 166 | + callback(createMockResponse(JSON.stringify({ events: [] }))); |
| 167 | + } |
| 168 | + return req; |
| 169 | + }; |
| 170 | + |
| 171 | + const follower = new LedgerFollower(mockPool, { |
| 172 | + stellarRpcUrl: "https://mock-stellar.com", |
| 173 | + contractIds: ["mock-contract"], |
| 174 | + maxBlockLimit: 100, |
| 175 | + pollIntervalMs: 1000, |
| 176 | + }); |
| 177 | + |
| 178 | + (follower as any).lastProcessedLedger = 500; |
| 179 | + (follower as any).running = true; |
| 180 | + |
| 181 | + // Set initial batch size to a low value to test additive increase |
| 182 | + (follower as any).currentBatchSize = 50; |
| 183 | + |
| 184 | + await (follower as any).pollOnce(); |
| 185 | + |
| 186 | + // Additively increased from 50 to 60 (+10) |
| 187 | + assert.equal(follower.status.currentBatchSize, 60); |
| 188 | + assert.equal(follower.status.isThrottled, false); |
| 189 | +}); |
0 commit comments