Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions helpers/silent-payments/RustTransactionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ import { spScanTransactions, RustMatchedUTXO, RustBatchScanResult } from '../../
import { hexToUint8Array } from '../../modules/uint8array-extras';
import type { NetworkConfig } from '../../modules/network';

// Transactions per JSI call. The call is synchronous and holds the JS thread for its whole run, so taps,
// timers and JS-driven animation wait on it: a window of tens of thousands of transactions froze the
// app for seconds. At the ~30k tx/s in RUST_INTEGRATION.md, 1000 is ~35ms, short enough to go unnoticed.
export const SCAN_SLICE_SIZE = 1000;

// How long slices may run back to back before the scan yields. A yield costs up to a frame on iOS, where
// timers under 18ms wait for the next display frame (RCTTiming), so yielding after every slice left a
// fast device idle about as long as it scanned; a slow device's slice takes this long anyway.
export const SCAN_YIELD_AFTER_MS = 30;

// A macrotask: microtasks (and the setImmediate shim, built on them in bridgeless RN) all run before the
// runtime gets back to touches, timers and rendering.
const yieldToEventLoop = () => new Promise<void>(resolve => setTimeout(resolve, 0));

export class RustTransactionProcessor {
private scanPrivkeyHex: string;
/** Main spend pubkey plus the label-0 (change) spend pubkey — the scanner tries both. */
Expand Down Expand Up @@ -62,16 +76,43 @@ export class RustTransactionProcessor {
}

try {
const result: RustBatchScanResult = spScanTransactions(this.scanPrivkeyHex, this.spendPubkeysHex, transactions);
const matched: RustMatchedUTXO[] = [];
let transactionsScanned = 0;
let outputsScanned = 0;

let holdingSince = Date.now();
for (let start = 0; start < transactions.length; start += SCAN_SLICE_SIZE) {
if (start > 0) {
if (Date.now() - holdingSince >= SCAN_YIELD_AFTER_MS) {
await yieldToEventLoop();
holdingSince = Date.now();
}
// Thrown rather than returning what's found so far: once this resolves the caller marks the
// whole window scanned, so the slices not yet scanned would be skipped for good.
if (cancelScanCallback?.()) {
throw new Error('SCAN_CANCELLED');
}
}

const result: RustBatchScanResult = spScanTransactions(
this.scanPrivkeyHex,
this.spendPubkeysHex,
transactions.slice(start, start + SCAN_SLICE_SIZE),
);
transactionsScanned += result.transactionsScanned;
outputsScanned += result.outputsScanned;
matched.push(...result.matchedUtxos);
}

console.log(
`[RustTransactionProcessor] Scanned ${result.transactionsScanned} txs, ` +
`${result.outputsScanned} outputs, found ${result.matchedUtxos.length} matches`,
`[RustTransactionProcessor] Scanned ${transactionsScanned} txs, ${outputsScanned} outputs, found ${matched.length} matches`,
);

return result.matchedUtxos.map(utxo => this.convertToSilentPaymentUTXO(utxo));
} catch (error) {
console.error('[RustTransactionProcessor] Batch processing error:', error);
return matched.map(utxo => this.convertToSilentPaymentUTXO(utxo));
} catch (error: any) {
if (error?.message !== 'SCAN_CANCELLED') {
console.error('[RustTransactionProcessor] Batch processing error:', error);
}
throw error;
}
}
Expand Down
150 changes: 99 additions & 51 deletions modules/socks5Fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,66 @@ function parseUrl(url: string): { host: string; port: number; path: string } {
return { host, port, path };
}

// Returns complete: false whenever more bytes are needed - on a short/malformed chunk this must
// signal "not done yet" rather than returning the partial body, or a truncated read looks like success.
// Operates on the raw Buffer throughout - chunk sizes are byte counts, and indexing a UTF-8
// string instead would misalign on any multi-byte character in the body.
function decodeChunked(data: Buffer): { body: Buffer; complete: boolean } {
const chunks: Buffer[] = [];
let remaining = data;

while (remaining.length > 0) {
const lineEnd = remaining.indexOf('\r\n');
if (lineEnd === -1) return { body: Buffer.concat(chunks), complete: false };

const chunkSizeStr = remaining.subarray(0, lineEnd).toString('ascii').trim();
if (!chunkSizeStr) return { body: Buffer.concat(chunks), complete: false };

const chunkSize = parseInt(chunkSizeStr, 16);
if (isNaN(chunkSize)) return { body: Buffer.concat(chunks), complete: false };
if (chunkSize === 0) return { body: Buffer.concat(chunks), complete: true };

const chunkStart = lineEnd + 2;
if (remaining.length < chunkStart + chunkSize + 2) return { body: Buffer.concat(chunks), complete: false };
chunks.push(remaining.subarray(chunkStart, chunkStart + chunkSize));
remaining = remaining.subarray(chunkStart + chunkSize + 2); // +2 for trailing \r\n
}
interface ChunkedDecoder {
/** Takes the body's next bytes; true once the terminal zero-size chunk is in. */
push: (bytes: Buffer) => boolean;
body: () => Buffer;
}

// Decodes a chunked body as it arrives, looking at each byte once however the packets split it:
// re-decoding the whole body on every packet was quadratic in its size, on the JS thread. A
// short or malformed chunk never completes (a truncated read must not look like success), so the
// socket closing rejects it. Works on raw bytes throughout - chunk sizes are byte counts, and
// indexing a UTF-8 string instead would misalign on any multi-byte character in the body.
function createChunkedDecoder(): ChunkedDecoder {
const parts: Buffer[] = [];
let length = 0;
let sizeLine: Buffer = Buffer.alloc(0); // a size line split across packets
let owed = 0; // bytes of the current chunk still to come
let crlfOwed = 0; // the CRLF after a chunk's data, skipped unchecked
let state: 'size' | 'done' | 'stuck' = 'size';

return {
push(bytes: Buffer): boolean {
let rest = bytes;
while (rest.length > 0 && state === 'size') {
if (owed > 0) {
const taken = Math.min(owed, rest.length);
parts.push(rest.subarray(0, taken));
length += taken;
owed -= taken;
rest = rest.subarray(taken);
if (owed === 0) crlfOwed = 2;
continue;
}
if (crlfOwed > 0) {
const skipped = Math.min(crlfOwed, rest.length);
crlfOwed -= skipped;
rest = rest.subarray(skipped);
continue;
}

const line = sizeLine.length ? Buffer.concat([sizeLine, rest]) : rest;
const lineEnd = line.indexOf('\r\n');
if (lineEnd === -1) {
sizeLine = line;
// no real size line runs this long: don't keep buffering one
if (sizeLine.length > 1024) state = 'stuck';
break;
}

return { body: Buffer.concat(chunks), complete: false };
const sizeText = line.subarray(0, lineEnd).toString('ascii').trim();
const size = parseInt(sizeText, 16);
sizeLine = Buffer.alloc(0);
rest = line.subarray(lineEnd + 2);
if (!sizeText || isNaN(size) || size < 0) state = 'stuck';
else if (size === 0) state = 'done';
else owed = size;
}
return state === 'done';
},
body: () => Buffer.concat(parts, length),
};
}

/**
Expand Down Expand Up @@ -120,7 +154,14 @@ export function socks5Fetch(url: string, options: Socks5FetchOptions = {}): Prom
let phase: 'greeting' | 'connect' | 'http' = 'greeting';
let pending: Buffer = Buffer.alloc(0);

let httpBuffer: Buffer = Buffer.alloc(0);
// The response so far: the header bytes until their terminator, then the body as the packets that
// carried it, joined once at the end. Appending every packet to one growing Buffer copied the whole
// response again each time, quadratic in its size, on the JS thread.
let headerBytes: Buffer = Buffer.alloc(0);
let receivedBytes = 0;
const bodyParts: Buffer[] = [];
let bodyLength = 0;
let chunkedBody: ChunkedDecoder | null = null;
let headerEnd = -1;
let status = 0;
let statusText = '';
Expand All @@ -132,11 +173,11 @@ export function socks5Fetch(url: string, options: Socks5FetchOptions = {}): Prom
// while more bytes are still needed.
const parseHeadersIfNeeded = (): boolean => {
if (headerEnd !== -1) return true;
const idx = httpBuffer.indexOf('\r\n\r\n');
const idx = headerBytes.indexOf('\r\n\r\n');
if (idx === -1) return false;
headerEnd = idx;

const headerPart = httpBuffer.subarray(0, headerEnd).toString('utf-8');
const headerPart = headerBytes.subarray(0, headerEnd).toString('utf-8');
const statusLine = headerPart.split('\r\n')[0];
const statusMatch = statusLine.match(/HTTP\/[\d.]+\s+(\d+)\s*(.*)/);
status = statusMatch ? parseInt(statusMatch[1], 10) : 0;
Expand All @@ -153,6 +194,7 @@ export function socks5Fetch(url: string, options: Socks5FetchOptions = {}): Prom
}

isChunked = responseHeaders['transfer-encoding']?.includes('chunked') ?? false;
if (isChunked) chunkedBody = createChunkedDecoder();
const contentLengthHeader = responseHeaders['content-length'];
expectedBodyLength = contentLengthHeader ? parseInt(contentLengthHeader, 10) : null;
return true;
Expand All @@ -167,30 +209,30 @@ export function socks5Fetch(url: string, options: Socks5FetchOptions = {}): Prom
text: async () => bodyText,
});

// Resolves as soon as the body is verifiably complete (Content-Length reached, or the
// chunked terminator seen). Returns false when there's not yet enough to tell - including
// when there's no length info at all, in which case only the 'close' handler can finish it.
const tryFinalizeHttpResponse = (): boolean => {
if (!parseHeadersIfNeeded()) return false;
const bodyBuf = httpBuffer.subarray(headerEnd + 4);

if (isChunked) {
const decoded = decodeChunked(bodyBuf);
if (!decoded.complete) return false;
finish(() => resolve(buildResponse(decoded.body.toString('utf-8'))));
return true;
// Takes the body's next bytes, and resolves as soon as the body is verifiably complete
// (Content-Length reached, or the chunked terminator seen). With no length info at all, only the
// 'close' handler can finish it.
const receiveBody = (bytes: Buffer) => {
if (chunkedBody) {
if (chunkedBody.push(bytes)) {
const decoded = chunkedBody.body();
finish(() => resolve(buildResponse(decoded.toString('utf-8'))));
}
return;
}

if (expectedBodyLength !== null) {
if (bodyBuf.length < expectedBodyLength) return false;
finish(() => resolve(buildResponse(bodyBuf.subarray(0, expectedBodyLength as number).toString('utf-8'))));
return true;
if (bytes.length > 0) {
bodyParts.push(bytes);
bodyLength += bytes.length;
}
if (expectedBodyLength !== null && !(bodyLength < expectedBodyLength)) {
const declared = expectedBodyLength;
finish(() => resolve(buildResponse(Buffer.concat(bodyParts, bodyLength).subarray(0, declared).toString('utf-8'))));
}

return false;
};

client.on('data', (data: string | Buffer) => {
if (resolved) return;
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
pending = pending.length ? Buffer.from(Buffer.concat([pending, buf])) : buf;

Expand Down Expand Up @@ -266,18 +308,25 @@ export function socks5Fetch(url: string, options: Socks5FetchOptions = {}): Prom

client.write(Buffer.from(httpRequest));
} else if (phase === 'http') {
httpBuffer = httpBuffer.length ? Buffer.concat([httpBuffer, pending]) : pending;
const segment = pending;
pending = Buffer.alloc(0);

if (httpBuffer.length > MAX_RESPONSE_BYTES) {
receivedBytes += segment.length;
if (receivedBytes > MAX_RESPONSE_BYTES) {
finish(() => {
client.destroy();
reject(new Error(`SOCKS5 response exceeded ${MAX_RESPONSE_BYTES} byte limit`));
});
return;
}

tryFinalizeHttpResponse();
if (headerEnd !== -1) {
receiveBody(segment);
} else {
headerBytes = headerBytes.length ? Buffer.concat([headerBytes, segment]) : segment;
// the body's first bytes can arrive in the same packet as the end of the headers
if (parseHeadersIfNeeded()) receiveBody(headerBytes.subarray(headerEnd + 4));
}
}
}
});
Expand All @@ -298,8 +347,7 @@ export function socks5Fetch(url: string, options: Socks5FetchOptions = {}): Prom
reject(new Error('SOCKS5 response ended before the declared body length was received'));
return;
}
const bodyBuf = httpBuffer.subarray(headerEnd + 4);
resolve(buildResponse(bodyBuf.toString('utf-8')));
resolve(buildResponse(Buffer.concat(bodyParts, bodyLength).toString('utf-8')));
} catch (error) {
reject(new Error(`Failed to parse response: ${error instanceof Error ? error.message : String(error)}`));
}
Expand Down
Loading
Loading