Repository navigation
feat(wasm-sdk): persist fetched data contracts and seed the next SDK from them - #4744
Conversation
…ts natively The protocol-version store (#4735) reached `localStorage` through `js_sys` directly. wasm-sdk is also compiled natively for its unit tests, where every wasm-bindgen import panics when called, so the three tests that build an unpinned SDK panicked in `WasmSdkBuilder::build`. Route all page storage through one `browser_storage` module: `js_sys::Reflect` on `globalThis.localStorage` for wasm32, a thread-local map everywhere else. The native fallback means the persistence paths run for real in unit tests, with each test thread seeing its own empty storage. Adds tests for the store's key gating and value filtering, and one native end-to-end test: an unpinned testnet builder boots at the floor, a verified response ratchets it and the observer persists the version, the next builder seeds from it, and a pinned builder ignores the store. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…from them
An app's first paint needs its contracts before its first proved document
query can be built or verified, and the SDK's trusted-context contract cache
lives in the instance only, so every page load refetched them: one round
trip (or one batched request) of 0.7 to 1.3 s ahead of any page data, for
about 50 KB of contract bytes that rarely change.
Keep what a fetch returns. `contract_store` persists each cached contract
under `dash-sdk.contracts.<network>` as one JSON map of contract id to
{version, bytes} through `browser_storage`, and `WasmSdkBuilder::build` seeds
the trusted context from it, so the first proved document query of a later
load needs no contract round trip. Every path that fills the cache persists:
`cache_contract`, the single-flight `fetch_contract_into_cache`, and the
state-transition side's `add_contract_to_context_cache`.
Staleness fails closed. Documents carry the `$contractVersion` they were
written under; `drop_stale_contract` drops the cache entry and its stored
copy when a returned document is stamped above the cached contract's version.
`getDocuments` and `getDocumentsWithProofInfo` then refetch the contract and
run the query once more; the composite surface drops the entry so the next
call refetches. `removeCachedContract` clears both. An entry this build
cannot deserialize is skipped, and the map is capped at 100 entries.
Persisted for mainnet and testnet only, like the protocol version: devnets
are re-cut and a contract id can outlive the chain that minted it; regtest is
local.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Warning Review limit reachedNext included review available in 32 minutes. View limit detailsLimit details: You’ve used the included review currently available. You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Review configuration: ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (6)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
🕓 Queued for automated review — 10th in line, estimated start in ~50 min (commit 814dcde)
|
#4743 merged as a squash, so its commit on this branch collided with its twin on v4.2-dev; resolved by keeping this branch's lib.rs and sdk.rs, which carry the fix plus the contract cache.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## v4.2-dev #4744 +/- ##
============================================
- Coverage 83.29% 83.08% -0.21%
============================================
Files 2839 2839
Lines 389582 390314 +732
============================================
- Hits 324486 324283 -203
- Misses 65096 66031 +935
🚀 New features to boost your workflow:
|
Issue being fixed or feature implemented
An app's first paint needs its data contracts before its first proved document query can be built or verified. The SDK's trusted-context contract cache lives in the instance only, so every page load refetches them: one round trip (or one batched
getDataContracts) of 0.7 to 1.3 s ahead of any page data, for about 50 KB of contract bytes that rarely change. Measured on the Yappr homepage against moutai, that preload is the largest single item between the trusted-context prefetch and the first document query.Stacked on #4743 (the
browser_storageseam); retarget tov4.2-devonce that merges.What was done?
contract_store. Persists each cached contract underdash-sdk.contracts.<network>as one JSON map of contract id to{version, bytes}(platform-serialized, base64) throughbrowser_storage.WasmSdkBuilder::build()seeds the trusted context from it when a trusted context is attached, so the first proved document query of a later load needs no contract round trip. Entries this build cannot deserialize are skipped; the map is capped at 100 entries per network.cache_contract(behindgetDataContractandgetDataContracts), the single-flightfetch_contract_into_cache(lazy fetches ahead of queries andrefreshContract), and the state-transition side'sadd_contract_to_context_cache.$contractVersionthey were written under.drop_stale_contractdrops the cache entry and its stored copy when a returned document is stamped above the cached contract's version.getDocumentsandgetDocumentsWithProofInfothen refetch the contract and run the query once more against the current layout; the composite surface drops the entry so the next call refetches.removeCachedContractclears both the cache and the stored copy.removeCachedContract.What this does not do: contracts are not revalidated proactively. An app that wants a background refresh can keep calling
getDataContractsafter first paint; it now also refreshes the store.How Has This Been Tested?
cargo test -p wasm-sdk --lib: 127 passed. New native tests, running against the thread-local storage fallback from fix(wasm-sdk): keep the protocol-version store off wasm-bindgen imports natively #4743:contract_store: only mainnet and testnet persist; store, load, replace and remove round-trip; undecodable entries are skipped and a garbage store is ignored; a full map refuses new ids but still replaces known ones.sdk: a contract cached by one SDK seeds the next SDK built for the network, and removal reaches the store; nothing persists without a trusted context or on devnets; a document stamped above the cached version drops the entry and its stored copy, while equal or unstamped versions do not.cargo clippy -p wasm-sdk --lib --testsandcargo clippy -p wasm-sdk --target wasm32-unknown-unknown: clean apart from the pre-existing unused import in rs-drive.Breaking Changes
None.
removeCachedContractnow also clears the persisted copy, which is what a caller removing a contract wants.Checklist:
For repository code-owners and collaborators only
🤖 Generated with Claude Code