Skip to content

feat(wasm-sdk): persist fetched data contracts and seed the next SDK from them - #4744

Merged
QuantumExplorer merged 3 commits into
v4.2-devfrom
feat/wasm-sdk-persist-contracts
Sep 14, 2026
Merged

QuantumExplorer merged 3 commits into
v4.2-devfrom
feat/wasm-sdk-persist-contracts

Conversation

@QuantumExplorer

Copy link
Copy Markdown
Member

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_storage seam); retarget to v4.2-dev once that merges.

What was done?

  • contract_store. Persists each cached contract under dash-sdk.contracts.<network> as one JSON map of contract id to {version, bytes} (platform-serialized, base64) through browser_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.
  • Every cache fill persists. cache_contract (behind getDataContract and getDataContracts), the single-flight fetch_contract_into_cache (lazy fetches ahead of queries and refreshContract), 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 against the current layout; the composite surface drops the entry so the next call refetches. removeCachedContract clears both the cache and the stored copy.
  • Scope. Persisted for mainnet and testnet only, like the protocol version: devnets are re-cut and a contract id (a hash of owner and nonce) can outlive the chain that minted it; regtest is local. Devnets keep the in-memory cache.
  • The evo-sdk README documents the store, the staleness rule and removeCachedContract.

What this does not do: contracts are not revalidated proactively. An app that wants a background refresh can keep calling getDataContracts after 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 --tests and cargo clippy -p wasm-sdk --target wasm32-unknown-unknown: clean apart from the pre-existing unused import in rs-drive.

Breaking Changes

None. removeCachedContract now also clears the persisted copy, which is what a caller removing a contract wants.

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have added "!" to the title and described breaking changes in the corresponding section if my code contains any
  • I have made corresponding changes to the documentation if needed

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

🤖 Generated with Claude Code

QuantumExplorer and others added 2 commits September 14, 2026 22:22
…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]>
@coderabbitai

coderabbitai Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 32 minutes.

Check out review usage here.

View limit details

Limit 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.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: c1fd4b45-6c85-47ae-b429-c5f76c1bc7bd

📥 Commits

Reviewing files that changed from the base of the PR and between 23611ac and 814dcde.

📒 Files selected for processing (6)
  • packages/js-evo-sdk/README.md
  • packages/wasm-sdk/src/contract_store.rs
  • packages/wasm-sdk/src/lib.rs
  • packages/wasm-sdk/src/queries/composite_document.rs
  • packages/wasm-sdk/src/queries/document.rs
  • packages/wasm-sdk/src/sdk.rs

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@thepastaclaw

thepastaclaw commented Sep 14, 2026 •

Copy link
Copy Markdown
Collaborator

🕓 Queued for automated review — 10th in line, estimated start in ~50 min (commit 814dcde)
Estimated review time once started: ~10 min (two-phase automated review; median of recent runs).

  • Request priority review — tick this box and the review moves to the front of the queue.

Base automatically changed from fix/wasm-sdk-storage-native-guard to v4.2-dev September 14, 2026 15:35
@github-actions github-actions Bot added this to the v4.2.0 milestone Sep 14, 2026
#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.
@QuantumExplorer
QuantumExplorer merged commit cfe7862 into v4.2-dev Sep 14, 2026
6 checks passed
@QuantumExplorer
QuantumExplorer deleted the feat/wasm-sdk-persist-contracts branch September 14, 2026 15:43
@codecov

codecov Bot commented Sep 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.08%. Comparing base (23611ac) to head (814dcde).
⚠️ Report is 1 commits behind head on v4.2-dev.

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     
Components Coverage Δ
dpp 83.67% <ø> (+<0.01%) ⬆️
drive 84.10% <ø> (-0.26%) ⬇️
drive-abci 85.96% <ø> (-0.10%) ⬇️
sdk ∅ <ø> (∅)
dapi-client ∅ <ø> (∅)
platform-version ∅ <ø> (∅)
platform-value 70.53% <ø> (ø)
platform-wallet ∅ <ø> (∅)
drive-proof-verifier 35.10% <ø> (-4.03%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants