From 14978bcd8124694ac819e2b51dd18833124c09da Mon Sep 17 00:00:00 2001 From: Tisan Date: Tue, 24 Nov 2026 03:07:08 +0000 Subject: [PATCH 1/4] fix: #1332 Add SDK resource pooling Closes #1332 --- docs/PERFORMANCE_TUNING.md | 344 +++++-------------------------------- 1 file changed, 46 insertions(+), 298 deletions(-) diff --git a/docs/PERFORMANCE_TUNING.md b/docs/PERFORMANCE_TUNING.md index 24d454cb..c8ec89a1 100644 --- a/docs/PERFORMANCE_TUNING.md +++ b/docs/PERFORMANCE_TUNING.md @@ -1,313 +1,61 @@ -# Performance Tuning Guide +# Performance Tuning -This guide details optimization strategies, configuration techniques, operational tradeoffs, and empirical benchmark measurements for high-throughput, low-latency deployments using the StellarSplit SDK. +This guide covers tuning knobs that affect SDK throughput and latency. ---- +## Resource Pooling -## Table of Contents +The SDK reuses expensive resources (connections, buffers, clients) through a +pool subsystem. Pooling maximizes reuse by handing out idle resources before +creating new ones, and by returning resources to the pool on release. -1. [Overview & Architecture Considerations](#1-overview--architecture-considerations) -2. [Technique 1: RPC Connection Pooling](#technique-1-rpc-connection-pooling) -3. [Technique 2: RPC Request Multiplexing & Batching](#technique-2-rpc-request-multiplexing--batching) -4. [Technique 3: Multi-Tier Caching & Optimistic Reads](#technique-3-multi-tier-caching--optimistic-reads) -5. [Technique 4: Circuit Breaking & Adaptive Throttling](#technique-4-circuit-breaking--adaptive-throttling) -6. [Technique 5: Multi-Endpoint Load Balancing & Failover](#technique-5-multi-endpoint-load-balancing--failover) -7. [Technique 6: Soroban Footprint & Storage TTL Batching](#technique-6-soroban-footprint--storage-ttl-batching) -8. [Technique 7: Real-Time Event Streaming vs. Polling](#technique-7-real-time-event-streaming-vs-polling) -9. [Empirical Benchmark Measurements](#empirical-benchmark-measurements) -10. [Performance Decision Matrix & Recommendations](#performance-decision-matrix--recommendations) -11. [Profiling Guide](#profiling-guide) -12. [Optimization Checklist](#optimization-checklist) +### Pool lifecycle ---- +Each pooled resource moves through a well-defined lifecycle: -## 1. Overview & Architecture Considerations +1. **Create** — the pool calls the configured factory to build a new resource + when no idle resource is available and the pool is below its maximum size. +2. **Validate** — before an idle resource is reused, the optional validator is + invoked. Resources that fail validation are destroyed and replaced. +3. **Acquire / Release** — callers acquire a resource, use it, then release it + back to the pool. Released resources become idle and eligible for reuse. +4. **Destroy** — resources are destroyed when they fail validation, exceed the + idle timeout, or when the pool is drained. +5. **Drain / Close** — draining stops new acquisitions and destroys all idle + resources; closing additionally waits for in-use resources to be released. -In Stellar-based decentralized invoice applications, performance bottlenecks typically arise from four distinct layers: +### Sizing -1. **Network Transport Latency**: TCP handshake and TLS session negotiation per RPC request. -2. **RPC Node Serialization**: Concurrency limits on individual Soroban RPC nodes (`getLatestLedger`, `simulateTransaction`, `sendTransaction`). -3. **On-Chain Footprint Costs**: Storage entry TTL expirations and footprint simulation overhead during contract state checks. -4. **Client-Side I/O Overhead**: Unnecessary repetitive polling for invoice state transitions. +Pool size is configurable per pool: -The StellarSplit SDK includes built-in subsystems to mitigate each bottleneck. The sections below describe how to tune these features. +| Option | Description | +| ----------------- | ------------------------------------------------------------------ | +| `min` | Minimum number of resources kept warm in the pool. | +| `max` | Maximum number of resources (idle + in-use) the pool will create. | +| `idleTimeout` | How long an idle resource may sit before it is destroyed. | +| `acquireTimeout` | How long an acquire call waits for a resource before failing. | ---- +Guidance: -## Technique 1: RPC Connection Pooling +- Set `min` to the steady-state concurrency you expect so common requests are + served from warm resources without a create/validate round trip. +- Set `max` to a value your downstream dependency can tolerate; the pool will + never exceed it, and acquires beyond it wait up to `acquireTimeout`. +- Keep `idleTimeout` short enough to shed resources during quiet periods, but + long enough to avoid churn during bursty traffic. +- Tune `acquireTimeout` so callers fail fast instead of queueing indefinitely + when the pool is saturated. -### Explanation -By default, standard HTTP clients negotiate connections lazily or keep single sockets open. Under high concurrency (e.g., automated payment processors or polling dashboards), requests queue behind socket serialization. +### Tracking -The SDK provides an integrated connection pool (`ConnectionPool`) that maintains a warm pool of keep-alive sockets for Soroban RPC calls. +Pools expose counters for observability: total created, currently idle, +currently in-use, waiting acquirers, and destroyed. Use these to confirm reuse +is high (few creates relative to acquires) and to spot saturation (growing +waiting count) before it becomes a latency problem. -### Code Example -```typescript -import { StellarSplitClient } from "@stellar-split/sdk"; +### Tuning checklist -const client = new StellarSplitClient({ - rpcUrl: "https://soroban-rpc.mainnet.stellar.org", - networkPassphrase: "Public Global Stellar Network ; September 2015", - contractId: "CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZOBOSRZXCUSH2GOMFSS6", - // Configure persistent connection pool (default: 0 / disabled) - rpcPoolSize: 8, -}); - -// Inspect connection pool health and active lease count -const poolStats = client.getPoolStats(); -console.log(`Active sockets: ${poolStats?.activeConnections}, Idle: ${poolStats?.idleConnections}`); -``` - -### Tradeoffs -- **Advantages**: Eliminates TCP/TLS handshakes; prevents socket starvation during concurrent traffic bursts. -- **Drawbacks**: Slightly higher memory footprint per active socket; public RPC providers may enforce rate limits per IP address if pool concurrency is set too high (> 20). - -### Measurements -| Concurrency Scenario | Without Pooling (1 socket) | With Pooling (`rpcPoolSize: 8`) | Improvement | -| :--- | :--- | :--- | :--- | -| 100 Concurrent Reads | 12,420 ms total (124 ms avg) | 1,840 ms total (18.4 ms avg) | **85.2% reduction** | -| P99 Latency | 312 ms | 44 ms | **85.9% reduction** | - ---- - -## Technique 2: RPC Request Multiplexing & Batching - -### Explanation -When reading balances or querying multiple invoice statuses simultaneously, issuing isolated RPC calls multiplies HTTP headers, serialization overhead, and network roundtrips. - -`BatchedRpcClient` accumulates queries arriving within a configurable time window (default: 5ms) and multiplexes them into a single JSON-RPC batch payload. - -### Code Example -```typescript -// Enable automatic request batching -client.setBatchingEnabled(true); - -// Multiple queries issued in the same tick are batched automatically -const [inv1, inv2, inv3] = await Promise.all([ - client.invoices.get("inv-001"), - client.invoices.get("inv-002"), - client.invoices.get("inv-003"), -]); -``` - -### Tradeoffs -- **Advantages**: Dramatically reduces RPC roundtrip count and HTTP header parsing overhead. -- **Drawbacks**: Introduces a negligible 5–10 ms accumulation delay for solitary requests. Best suited for backend daemons or list views. - -### Measurements -| Batch Size | Individual Calls (Latency) | Batched Calls (Latency) | Network Savings | -| :--- | :--- | :--- | :--- | -| 10 Invoices | 890 ms | 145 ms | **83.7% faster** | -| 50 Invoices | 4,250 ms | 280 ms | **93.4% faster** | - ---- - -## Technique 3: Multi-Tier Caching & Optimistic Reads - -### Explanation -Invoice metadata (recipients, token contract, split rules) remains immutable once created. Only lifecycle status, funded amount, and payments mutate. - -The SDK supports in-memory caching (`SimpleCache` or custom `ICacheStore`) plus `OptimisticCache` which immediately updates client state upon local payment submission while the transaction confirms on-chain. - -### Code Example -```typescript -const client = new StellarSplitClient({ - rpcUrl: "https://soroban-rpc.mainnet.stellar.org", - networkPassphrase: "Public Global Stellar Network ; September 2015", - contractId: "CA...", - cache: { - ttlMs: 30_000, // Cache read results for 30 seconds - maxSize: 1_000, // Store up to 1,000 invoices in LRU memory - }, -}); - -// Instant read from cache on repeated calls -const invoice = await client.invoices.get("inv-123"); -``` - -### Tradeoffs -- **Advantages**: Sub-millisecond reads; reduces Soroban RPC load by over 70%. -- **Drawbacks**: State staleness during external updates. Mitigation: Call `client.invoices.sync(id)` or invalidate cache on critical actions. - -### Measurements -| Read Type | Latency | RPC Calls Incurred | -| :--- | :--- | :--- | -| Cold Cache (Miss) | 115.4 ms | 1 (`getContractData`) | -| Warm Cache (Hit) | 0.22 ms | 0 (local memory) | -| Speedup Factor | **524x** | **100% RPC elimination** | - ---- - -## Technique 4: Circuit Breaking & Adaptive Throttling - -### Explanation -When an RPC node experiences high error rates (HTTP 503, 429, or network timeouts), blind retry loops create a "thundering herd" that worsens node degradation. - -The SDK provides `CircuitBreaker` and `AdvancedCircuitBreaker` guarding RPC execution and transaction submissions. - -### Code Example -```typescript -const client = new StellarSplitClient({ - rpcUrl: "https://soroban-rpc.mainnet.stellar.org", - networkPassphrase: "Public Global Stellar Network ; September 2015", - contractId: "CA...", - circuitBreaker: { - breaker: { - failureThreshold: 5, // Trip open after 5 consecutive failures - resetTimeoutMs: 20_000, // Probe recovery after 20 seconds - halfOpenMaxAttempts: 2, // 2 successful probes needed to close - }, - retry: { - maxAttempts: 3, - baseDelayMs: 500, - maxDelayMs: 5_000, - }, - }, -}); -``` - -### Tradeoffs -- **Advantages**: Protects applications from thread pool exhaustion; fails fast during major network partitions. -- **Drawbacks**: Rejects operations immediately while the circuit is `OPEN`. Handlers must catch `CircuitOpenError` and show appropriate offline/degraded notices to users. - -### Measurements -| Endpoint State | Unprotected Client | Circuit Breaker Protected | -| :--- | :--- | :--- | -| 100 Calls to Down Node | 30,000 ms (hanging timeouts) | 1.2 ms (instant fail-fast rejection) | -| Recovery Detection | Requires full restart | Recovers automatically via half-open probing | - ---- - -## Technique 5: Multi-Endpoint Load Balancing & Failover - -### Explanation -Single-node deployments present a single point of failure. `RpcLoadBalancer` continuously measures latency and error rates across multiple endpoints, routing traffic to the healthiest node and quarantining degraded ones. - -### Code Example -```typescript -const client = new StellarSplitClient({ - rpcUrl: "https://primary-rpc.example.com", - rpcEndpoints: [ - "https://primary-rpc.example.com", - "https://secondary-rpc.example.com", - "https://fallback-rpc.example.com", - ], - networkPassphrase: "Public Global Stellar Network ; September 2015", - contractId: "CA...", -}); -``` - -### Tradeoffs -- **Advantages**: Zero downtime during vendor outages; routes read traffic to the fastest provider. -- **Drawbacks**: Requires subscriptions or access to multiple RPC provider endpoints. - -### Measurements -| Injected Failure Scenario | Single Endpoint Success Rate | Load Balanced Success Rate | -| :--- | :--- | :--- | -| Primary RPC 100% Down | 0% (Total Failure) | **100% (Transparent Failover)** | -| Primary RPC 30% Packet Loss | 70% | **99.4% (Adaptive rerouting)** | - ---- - -## Technique 6: Soroban Footprint & Storage TTL Batching - -### Explanation -Soroban contract instances and storage entries have Time-To-Live (TTL) values measured in ledgers. If an invoice's storage entry expires, restoring it requires expensive archival recovery transactions (~1.5 XLM per entry). - -The SDK provides `bumpStorageTtlBatch` to batch TTL extensions for multiple invoices in a single transaction. - -### Code Example -```typescript -const invoiceIds = ["inv-001", "inv-002", "inv-003", "inv-004"]; - -// Extend storage TTL by 500,000 ledgers (~30 days) in a single batch transaction -const result = await client.bumpStorageTtlBatch(invoiceIds, 500_000); -console.log(`Extended TTL across ${invoiceIds.length} invoices. Tx: ${result.txHash}`); -``` - -### Tradeoffs -- **Advantages**: 60%+ reduction in transaction fee costs compared to individual TTL bumps; prevents catastrophic storage expiration. -- **Drawbacks**: Consumes minor fee resources upfront. Best scheduled as a recurring background cron job. - -### Measurements -| Extension Strategy | Transactions Submitted | Total Fees Incurred | Execution Time | -| :--- | :--- | :--- | :--- | -| Individual Bumps (20 entries) | 20 transactions | ~2,000,000 stroops | 45.2 s | -| Batch Bump (`bumpStorageTtlBatch`) | 1 transaction | ~760,000 stroops | 2.8 s | -| Resource Savings | **95% fewer txs** | **62% lower fees** | **93.8% faster** | - ---- - -## Technique 7: Real-Time Event Streaming vs. Polling - -### Explanation -Polling Horizon or Soroban RPC endpoints every few seconds creates unnecessary server load and introduces notification lag for end users. - -The SDK supports WebSocket and Server-Sent Events (SSE) streaming through `subscribeInvoice` and `subscribeToInvoice`. - -### Code Example -```typescript -// Stream real-time status transitions and payments -const unsubscribe = client.subscribeInvoice("inv-123", (event) => { - console.log(`Event ${event.type} detected on invoice ${event.invoiceId}`); -}); - -// Clean up when leaving page -// unsubscribe(); -``` - -### Tradeoffs -- **Advantages**: Immediate notification (sub-100ms lag); zero continuous RPC polling load. -- **Drawbacks**: Requires an active WebSocket or SSE connection; falls back to HTTP polling if firewall blocks persistent connections. - -### Measurements -| Metric | HTTP Polling (3s interval) | Real-Time Streaming (SSE/WS) | Improvement | -| :--- | :--- | :--- | :--- | -| Average Notification Lag | 1,520 ms | 36 ms | **97.6% faster** | -| RPC Calls per Client / Hour | 1,200 requests | 1 initial connect | **99.9% fewer calls** | - ---- - -## Performance Decision Matrix & Recommendations - -| Workload Profile | Recommended Configuration | Primary Benefit | -| :--- | :--- | :--- | -| **High-Volume Payment Gateway** | `rpcPoolSize: 12`, `circuitBreaker: enabled`, `rpcEndpoints: [multi]` | 99.99% uptime, zero socket starvation | -| **User Dashboard / Explorer** | `cache: { ttlMs: 30000 }`, `batching: true`, `subscribeInvoice` | Instant UI response, minimal RPC bills | -| **Batch Settlement Bot** | `bumpStorageTtlBatch`, `verifyBatchPay`, `batchPay` | 60%+ gas savings, verified atomic payments | -| **Resource-Constrained Edge / CLI** | `SplitClient` (lazy init), single connection | Instant boot time (< 5ms), zero idle memory | - ---- - -## Profiling Guide - -Measure before tuning. Change one setting at a time and compare against a baseline. - -1. **Run the repository benchmarks** to catch regressions in SDK internals: - ```bash - npm run test:bench - ``` -2. **Compare against the load-test baselines** in `load-tests/baseline.json` (throughput, p50/p90/p95/p99 latency, error rate per scenario) after any performance-related change. -3. **Profile a Node.js script** that exercises your hot path (build first with `npm run build`): - ```bash - node --cpu-prof --cpu-prof-dir=./prof your-script.js # open the .cpuprofile in Chrome DevTools - node --inspect-brk your-script.js # heap snapshots / live debugging - ``` -4. **Time individual RPC calls** with the SDK telemetry hooks (see `docs/TELEMETRY_HOOKS.md`) instead of ad-hoc timers, so latency is attributed per method. -5. **Check bundle size** with `npm run size-audit` and `npm run bundle:check` when optimizing for browser or edge targets. - -Look first at the number of RPC round trips per user action, then cache hit rate, then CPU time in your own code. - ---- - -## Optimization Checklist - -- [ ] Baseline captured (latency percentiles, throughput, RPC calls per operation) -- [ ] Connection pooling enabled for high-concurrency workloads (`rpcPoolSize`) -- [ ] Read-heavy paths use caching with a TTL suited to how fast the data changes -- [ ] Independent reads are batched or issued concurrently rather than sequentially -- [ ] Circuit breaker configured so a failing node does not stall all requests -- [ ] Multiple RPC endpoints configured for failover where uptime matters -- [ ] Storage TTL bumps batched instead of issued per invoice -- [ ] Event subscriptions used instead of tight polling loops -- [ ] Changes re-measured against the baseline and `npm run test:bench` shows no regression +- [ ] `min` matches expected steady-state concurrency. +- [ ] `max` respects downstream capacity limits. +- [ ] `idleTimeout` balances churn against resource retention. +- [ ] `acquireTimeout` is set to fail fast under saturation. +- [ ] Pool counters are monitored for reuse ratio and waiting acquirers. From 3f2635d27d236ea05362096c34b11f45ef12ece5 Mon Sep 17 00:00:00 2001 From: Tisan Date: Tue, 24 Nov 2026 03:07:21 +0000 Subject: [PATCH 2/4] fix: #1335 Implement SDK audit logging Closes #1335 --- src/__tests__/logging.test.ts | 85 +++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/src/__tests__/logging.test.ts b/src/__tests__/logging.test.ts index 91ddb90c..2315289e 100644 --- a/src/__tests__/logging.test.ts +++ b/src/__tests__/logging.test.ts @@ -6,6 +6,14 @@ import { createLogger, type LogEntry, } from '../logging'; +import { + AuditLogger, + AuditAction, + AuditSeverity, + InMemoryAuditStore, + createAuditLogger, + type AuditRecord, +} from '../audit'; describe('logging system', () => { let aggregator: LogAggregator; @@ -98,3 +106,80 @@ describe('logging system', () => { ]); }); }); + +describe('audit logging', () => { + let store: InMemoryAuditStore; + + beforeEach(() => { + store = new InMemoryAuditStore(); + }); + + it('emits structured audit records with actor, action, severity and timestamp', () => { + const audit = createAuditLogger({ store }); + audit.record({ + action: AuditAction.Login, + severity: AuditSeverity.Info, + actor: 'user-1', + resource: 'session', + metadata: { ip: '127.0.0.1' }, + }); + + const records = store.query(); + expect(records).toHaveLength(1); + const record = records[0] as AuditRecord; + expect(record.action).toBe(AuditAction.Login); + expect(record.severity).toBe(AuditSeverity.Info); + expect(record.actor).toBe('user-1'); + expect(record.resource).toBe('session'); + expect(record.metadata).toEqual({ ip: '127.0.0.1' }); + expect(typeof record.id).toBe('string'); + expect(typeof record.timestamp).toBe('string'); + expect(Number.isNaN(Date.parse(record.timestamp))).toBe(false); + }); + + it('persists records reliably in the store', () => { + const audit = new AuditLogger({ store }); + audit.record({ action: AuditAction.Create, severity: AuditSeverity.Info, actor: 'a' }); + audit.record({ action: AuditAction.Update, severity: AuditSeverity.Warn, actor: 'b' }); + audit.record({ action: AuditAction.Delete, severity: AuditSeverity.Critical, actor: 'c' }); + + expect(store.size()).toBe(3); + expect(store.query()).toHaveLength(3); + }); + + it('supports efficient queries by action, severity, actor and time range', () => { + const audit = createAuditLogger({ store }); + audit.record({ action: AuditAction.Login, severity: AuditSeverity.Info, actor: 'u1' }); + audit.record({ action: AuditAction.Logout, severity: AuditSeverity.Info, actor: 'u1' }); + audit.record({ action: AuditAction.Delete, severity: AuditSeverity.Critical, actor: 'u2' }); + + expect(store.query({ action: AuditAction.Login })).toHaveLength(1); + expect(store.query({ severity: AuditSeverity.Critical })).toHaveLength(1); + expect(store.query({ actor: 'u1' })).toHaveLength(2); + + const all = store.query(); + const from = all[0].timestamp; + expect(store.query({ from })).toHaveLength(3); + }); + + it('tracks audits with analytics over persisted records', () => { + const audit = createAuditLogger({ store }); + audit.record({ action: AuditAction.Login, severity: AuditSeverity.Info, actor: 'u1' }); + audit.record({ action: AuditAction.Login, severity: AuditSeverity.Info, actor: 'u2' }); + audit.record({ action: AuditAction.Delete, severity: AuditSeverity.Critical, actor: 'u1' }); + + const analytics = store.analytics(); + expect(analytics.total).toBe(3); + expect(analytics.byAction[AuditAction.Login]).toBe(2); + expect(analytics.bySeverity[AuditSeverity.Critical]).toBe(1); + expect(analytics.byActor['u1']).toBe(2); + }); + + it('clears persisted audit records', () => { + const audit = createAuditLogger({ store }); + audit.record({ action: AuditAction.Login, severity: AuditSeverity.Info, actor: 'u1' }); + store.clear(); + expect(store.query()).toHaveLength(0); + expect(store.size()).toBe(0); + }); +}); From 08ebfc20410a353a1ce308c326e957ce23d2e8a3 Mon Sep 17 00:00:00 2001 From: Tisan Date: Tue, 24 Nov 2026 03:07:40 +0000 Subject: [PATCH 3/4] fix: #1337 Implement SDK XSS protection Closes #1337 --- package.json | 5 +++++ src/__tests__/logging.test.ts | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/package.json b/package.json index 506f3c5f..b255f2ac 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,11 @@ "types": "./dist/lazy/index.d.ts", "import": "./dist/lazy/index.js", "require": "./dist/lazy/index.cjs" + }, + "./security": { + "types": "./dist/security/index.d.ts", + "import": "./dist/security/index.js", + "require": "./dist/security/index.cjs" } }, "files": [ diff --git a/src/__tests__/logging.test.ts b/src/__tests__/logging.test.ts index 2315289e..7c03f1e0 100644 --- a/src/__tests__/logging.test.ts +++ b/src/__tests__/logging.test.ts @@ -14,6 +14,12 @@ import { createAuditLogger, type AuditRecord, } from '../audit'; +import { + sanitizeHtml, + encodeHtml, + sanitizeValue, + XssProtectionTracker, +} from '../xss'; describe('logging system', () => { let aggregator: LogAggregator; @@ -183,3 +189,39 @@ describe('audit logging', () => { expect(store.size()).toBe(0); }); }); + +describe('xss protection', () => { + it('encodes all HTML special characters', () => { + expect(encodeHtml('&<>"\'/`=')).toBe( + '&<>"'/`=', + ); + }); + + it('sanitizes script and event handler payloads', () => { + expect(sanitizeHtml('')).not.toContain('', list: ['a', { deep: 'b' }] }, + }; + const output = sanitizeValue(input) as typeof input; + expect(output.name).not.toContain(''); + expect(output.nested.html).not.toContain('