feat(contracts): lazy pool state initialization with on-demand loading (#721) - #883
Merged
Smartdevs17 merged 1 commit intoAug 28, 2026
Conversation
Smartdevs17#721) Replace eager pool-state setup with a lazily-built, cached aggregate: - pool_state module: consolidated PoolStateSnapshot (interest, risk, liquidity, reserves) materialized on first access, memoised in short-lived storage keyed by (pool, global epoch). - On-demand loading: sub-components resolved only while building a snapshot; missing sub-config falls back to protocol defaults. - Invalidation: every relevant mutation (risk params, interest-rate config, reserve factor) bumps the global epoch; admin invalidate_pool_state forces a rebuild. Stale cache entries are ignored automatically because the epoch is part of the cache key. - Monitoring: hit/miss/rebuild/invalidation counters in persistent storage, exposed via get_pool_state_metrics. - New entrypoints: get_pool_state, is_pool_state_initialized, get_pool_state_epoch, get_pool_state_metrics, invalidate_pool_state. - API: StellarService.getPoolState lazily loads and caches snapshots keyed by on-chain epoch, with cache-hit-rate and load-latency metrics and per-pool cache invalidation. - Benchmarks: cold/warm/invalidation/metrics gas benchmarks for the lazy pool-state path.
|
Someone is attempting to deploy a commit to the smartdevs17's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@daniella-techie Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #721
Closes #714
Closes #715
Closes #716
Context
Pool state initialization was eager and slow — all pool state was loaded at startup.
What changed
stellar-lend/contracts/hello-world/src/pool_state.rs(new) — consolidatedPoolStateSnapshot(interest-rate, risk, liquidity, reserve components) that is materialized lazily on first access and memoised in short-lived (temporary) storage keyed by(pool, global epoch).initializetime; missing sub-config falls back to protocol defaults instead of erroring.set_risk_params,update_interest_rate_config,set_reserve_factor). Because the epoch is part of the cache key, stale entries are ignored automatically;invalidate_pool_state(admin) forces a rebuild for a pool.loadcall against one epoch, and the epoch is embedded in the returned value so callers can detect staleness. Snapshots are never partially mutated in place.get_pool_state_metrics.get_pool_state,is_pool_state_initialized,get_pool_state_epoch,get_pool_state_metrics,invalidate_pool_state.stellar-lend/contracts/hello-world/src/storage.rs—PoolStateKey/PoolStateTempKeystorage keys.api/src/services/stellar.service.ts—StellarService.getPoolState()lazily loads and caches snapshots keyed by the on-chain epoch, with cache-hit-rate + load-latency metrics (getPoolStateCacheMetrics) and per-pool cache invalidation (invalidatePoolStateCache). Warm reads are served from the local cache well within the <50ms target.stellar-lend/benchmarks/src/pool_state_benchmarks.rs(new) — cold / warm / invalidation / metrics gas benchmarks for the lazy pool-state path; registered in the benchmark runner.Acceptance criteria
get_pool_state_metrics/getPoolStateCacheMetrics)Related performance work (context only — not addressed in this PR)
#714 — Optimize liquidation gas costs with early exit and batched operations
Liquidation gas costs can be reduced through optimization. Current liquidation operations are gas-inefficient. Expected: early-exit optimizations for unprofitable liquidations, batched storage operations, gas-efficient data structures, gas regression testing in CI, gas optimization documentation, and gas benchmarks/comparison for liquidations. Scope:
stellar-lend/contracts/hello-world/src/liquidate.rs,stellar-lend/benchmarks/,.github/workflows/,docs/gas-optimization.md.#715 — Implement interest calculation caching with incremental updates
Interest calculations are performed on every query and recalculated on every access. Expected: interest calculation caching, incremental updates on state changes, cache invalidation on relevant events, interest calculation <10ms, cache hit-rate monitoring, and interest calculation benchmarks/documentation. Scope:
stellar-lend/contracts/hello-world/src/interest_rate.rs,stellar-lend/contracts/lending-interest/src/,api/src/services/redisCache.service.ts,stellar-lend/benchmarks/.#716 — Add transaction simulation cache for common pool operations
Transaction simulation is slow for repeated operations and is simulated from scratch each time. Expected: transaction simulation caching, cache key generation from inputs, cache invalidation on state changes, simulation performance <500ms, cache hit-rate monitoring, simulation accuracy validation, and simulation caching documentation. Scope:
api/src/services/stellar.service.ts,api/src/services/redisCache.service.ts,api/src/routes/,stellar-lend/benchmarks/..