Skip to content

feat: compare two contract versions, judged by the update rules (and ?url= links) - #2

Merged
QuantumExplorer merged 2 commits into
mainfrom
feat/compare-contracts
Sep 28, 2026
Merged

QuantumExplorer merged 2 commits into
mainfrom
feat/compare-contracts

Conversation

@QuantumExplorer

Copy link
Copy Markdown
Member

Basic explanation

What this does: Two additions to the visualizer.

  1. Compare mode: it draws two versions of a data contract as one diagram, so a reviewer of a contract-update pull request sees at a glance what the update adds, removes or changes. For each change it also says whether Dash Platform would refuse it if the new version were sent as an update of the old one. Many edits are not allowed after registration, like changing an index or adding a rule, and a refused update is a wasted transaction.
  2. ?url=: it opens any contract JSON file from a link. This was pushed to feat: read Platform 4.2 contracts (declared references and protocol 14 keywords) #1 after feat: read Platform 4.2 contracts (declared references and protocol 14 keywords) #1 had merged, so it never reached main; it is included here.

Value:

Risks: Low. The site stays read-only.

  • The verdicts follow the update rules written in the Dash Platform Book, not the platform's own validator, which the wasm SDK does not expose. A rule the book words differently from the code would give a wrong verdict. Every verdict links the rule it applies, so it can be checked.
  • The pull request loader uses GitHub's public API: 60 requests an hour per visitor without signing in, and a pull request takes three or four.

What changed

Compare mode

  • Compare in the toolbar, or a deep link, picks a base and a head. Each is a registered contract id (fetched from the toolbar's network), a link to a contract JSON file, an example:<key>, or empty for no contract.
  • From a GitHub pull request: paste the PR link and pick one of its changed JSON files. Contract files are listed first, and a PR with one contract file preselects it. Base is the file at the merge base, head at the PR head, as in GitHub's Files changed tab (src/github.ts).
  • Deep links: ?pr=<PR link>&file=<path> and ?base=<source>&head=<source> (plus network / devnet when a source is a contract id).
  • The diagram draws the union of both versions. Added parts are green (+), removed parts struck out in red (−) and changed parts amber (~), for document types, properties, indexes, property constraints, reference edges and external nodes. Unchanged types are dimmed, or hidden with hide unchanged types. A red ✕ marks every part a platform update would refuse.
  • The Changes panel lists every change as before → after, grouped by document type, with a verdict line per change. Clicking a change centers its type and opens it in the inspector, which shows the change on top. Refused only filters the list.

Verdicts (src/model/updateRules.ts)

Each change is judged by its keyword's On update rule in the book's Contract Keywords part:

  • Refused, with the consensus error the book names and a link to the rule: IncompatibleDocumentTypeSchemaError (10246), DocumentTypeUpdateError (40212), DataContractInvalidIndexDefinitionUpdateError (10217), DataContractInvalidRequiredFieldsUpdateError (10276), DataContractConfigUpdateError (40002), InvalidDataContractVersionError (10212), DataContractUpdateActionNotAllowedError (40004), IncompatibleDataContractSchemaError (10213), DataContractUpdatePermissionError (40003), DataContractIsReadonlyError (40001).
  • Check, where the schema text alone does not settle the outcome: a raised integer bound or a new enum value that may change how the integer is stored, and a changed schemaDefs entry that must stay compatible.
  • The direction rules are applied as the book states them. maxLength, maxItems, maximum and maxBytes may be raised or removed; minLength, minItems and minimum may be lowered or removed; enum may gain values; immutable may gain entries; required may gain only a property the same update adds with requiredSince equal to the new version. A typed array keeps its maxItems, and a byte array keeps its fixed length.
  • The version and readonly rules apply only when something changed, so identical versions report "the same".
  • Config defaults are filled in on both sides before comparing. A contract fetched through the SDK writes every config key and a file usually leaves defaults out; without this, a live contract compared with an identical file showed 6 false refusals.

Load a contract from a link (?url=)

  • ?url=<link>, or a link typed into the contract id box, fetches a contract JSON file and diagrams it like pasted JSON, without the SDK.
  • GitHub page links (github.com/<owner>/<repo>/blob|raw/<ref>/<path>) are fetched from raw.githubusercontent.com; other hosts must allow cross-origin reads. Only http(s) links are accepted, files over 5 MB are refused, and HTTP errors, CORS failures and non-JSON bodies each get a clear message.
  • The Vite dev server reads ?url as its own asset-import query and answered such page requests with 403. A dev-only plugin in vite.config.ts serves the app for them; static hosting (Pages) needs nothing.

Other

  • The model keeps each property, index and document type schema as written (written / schema), which the comparison reads. loadContractJson (in src/sdk/contract.ts) loads any source as JSON for both sides.

Before and after

Reviewing PastaPastaPasta/yappr#577 (contracts/pollr-contract.json):

  • Before: open both commits' copies of the 11 KB file on GitHub and read the JSON diff. Nothing says whether the platform would accept the new version as an update.
  • After: open ?pr=https://github.com/PastaPastaPasta/yappr/pull/577&file=contracts/pollr-contract.json. The poll card is outlined amber and shows a green + optionsContiguous row in its property constraints, with the rule written as a formula. The Changes panel says "As an update of the base, the platform would refuse one change". Under the change, it shows ✕ 10246 IncompatibleDocumentTypeSchemaError: A propertyConstraints rule may not be added, removed or changed on an existing type, with a link to the rule.

A registered contract against a proposed file, ?base=EG7RGfV8fDTayC2FyVr8HwdpJh3fXDbVztcfE94UmN88&head=example:moderation-charters&network=devnet&devnet=moutai:

  • Before this PR's config fix: 6 changes such as config.readonly: false → ∅, each refused with 40002.
  • After: "The two versions are the same."

Testing

  • npm test: 78 tests pass. They include:
    • the diff and its verdicts: 11 refusals with exact codes, from a marketplace variant with a changed ttl, an added index, an added rule, a lowered maxBytes, a changed refersTo, a removed property, a removed document type, a required property without requiredSince, a new type with a wrong requiredSince, a moderation list change and a skipped version;
    • allowed changes that produce no refusal;
    • key order and config defaults comparing equal;
    • the integer-bound check;
    • the PR loader against a fake API (merge base, fork head, added, removed and renamed files, URL encoding, rate limit);
    • link handling.
  • npm run typecheck, npm run build, npm run validate:examples: clean.
  • By hand in the browser:
    • feat(contracts): beta.5 re-cut — propertyConstraints, no document TTL PastaPastaPasta/yappr#577 through the deep link and through the dialog (list files, pick pollr). The social, pollr, storefront and blog diffs (9, 1, 3 and 1 new rules) match the PR description: social, pollr and storefront in the browser, all four through the diff engine. Clicking a change focuses it and fills the inspector.
    • A live moutai contract against an identical file reports no change.
    • Two unrelated examples show every type added or removed.
    • The production build, served statically under /contract-visualizer/, runs a PR comparison without downloading the SDK.
    • For ?url=: GitHub commit and branch links (a slash in the ref) through the dev server and the production build; a 404 and a host without CORS show their messages.

Notes

  • The exact verdict would come from DataContract::validate_update in rs-dpp. Exposing it in the wasm SDK (as DataContract.validateUpdate(newContract, platformVersion) or similar) would let this panel show the platform's own answer; that is a separate platform change.
  • The verdicts only matter for an update. A fresh registration of the head is not bound by them, and the panel says so.

🤖 Generated with Claude Code

QuantumExplorer and others added 2 commits September 28, 2026 08:02
?url=<link>, or a link typed into the contract id box, fetches a contract
JSON file and diagrams it like pasted JSON, without the SDK: a contract in a
pull request that is not registered yet opens from one shareable link.

- GitHub page links (github.com/<owner>/<repo>/blob|raw/<ref>/<path>) are
  fetched from raw.githubusercontent.com, since github.com pages are HTML
  and refuse cross-origin reads. Only http(s); files over 5 MB are refused;
  HTTP, CORS and non-JSON failures get their own message.
- The link lands in the input box and the page URL, and names PNG / SVG
  exports.
- The Vite dev server reads `?url` as its asset-import query and answered
  such page requests with 403; a dev-only plugin serves the app for them.
  Static hosting is unaffected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A Compare mode diagrams two versions of a contract as one: the union of
both, with added parts green, removed parts struck out in red and changed
parts amber, for document types, properties, indexes, property constraints
and reference edges; unchanged types are dimmed or hidden.

- Base and head are each a registered contract id, a link to a contract
  JSON file, an example, or none. A GitHub pull request link lists the PR's
  changed JSON files and compares one at the merge base against the head,
  as GitHub's Files changed tab does (public REST API, no sign-in).
- Deep links: ?pr=<PR link>&file=<path>, ?base=<source>&head=<source>.
- The Changes panel lists every change as before -> after, grouped by
  document type; clicking one centers its type and opens it in the
  inspector, which shows the change on top.
- Each change is judged by its keyword's "On update" rule in the Dash
  Platform Book: refused with the consensus error the book names
  (10246, 40212, 10217, 10276, 40002, 10212, ...) and a link to the rule,
  or flagged to check where the schema text alone does not settle it
  (integer storage width, schemaDefs compatibility). The version and
  readonly rules apply only when something changed. Config defaults are
  filled in on both sides so a fetched contract and a file compare equal.
- The model keeps each property, index and type schema as written, which
  the comparison reads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Sep 28, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: fd28eff6-63a6-4613-8f4e-6aadbd161648


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.

@QuantumExplorer
QuantumExplorer merged commit 32a9215 into main Sep 28, 2026
2 checks passed
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.

1 participant