Repository navigation
fix(dpp)!: compile document-schema patterns when first used - #5891
Conversation
At protocol version 15 a document type's native validator compiled every
`pattern` of its schema when it was built, on every path that builds one:
registration, the first document checked against the type, measuring a
contract's compiled size, and storing a type in its compiled form.
Building a validator now checks each pattern's syntax only, with the
regex-syntax AST parser configured as the regex crate configures it. Each
pattern is held in a private cell with its converted form and a OnceLock,
compiled under the regex crate's default size limit the first time a value
reaches it, and kept. The validator stays shared and immutable from outside;
the cell is its only interior mutability, and the compile is a pure function
of the converted pattern, so when it runs changes no verdict.
Registration compiles every pattern of the validator it builds, walking the
document schema and then each `$ref` target in key order, and refuses the
first that does not compile with the same refusal text as before. The
validator a registered type keeps therefore holds every pattern compiled.
A string reaching a pattern that parses but does not compile is refused with
a `JsonSchemaError` of keyword `format` and params `{"format":"regex"}`, the
violation protocol version 14 gives there. A `contains` element reaching one
matches nothing. Only a type registered through protocol version 14 can hold
such a pattern, in a value no registration check read as a schema that a
`$ref` alone reaches.
BREAKING CHANGE: at protocol version 15 (unreleased, not activated on any
network), changed in place in generation 0 of the validation-schema builder
and validator:
- a type holding a pattern that parses but does not compile keeps its
validation schema instead of being stored refused, and its documents get
protocol version 14's verdicts, with the first-use `format` violation
above; `document_type_validation_schema` returns its schema;
- a type holding both such a `$ref`-only pattern and another builder
refusal is refused, at registration and when built elsewhere, with the
other refusal rather than the pattern's.
For every type whose patterns all compile, the built schema, its stored
bytes and compiled size, and every verdict are unchanged.
Tests (rs-dpp, at the latest protocol version): the builder holds a pattern
that parses whether or not it compiles; a validator compiles a pattern only
when a value first reaches it and keeps the result; a string reaching a
pattern that does not compile gets the `format` violation, and a `contains`
element matches nothing; measuring a contract's compiled size compiles no
pattern; documents are checked alike whether the validator was kept from
registration, read back from the compiled form or built from an unvalidated
parse, with the ECMA escape rewrites applied on every path; a type
registered at protocol version 14 with such a pattern in a `const` reached
by a property or a `contains` `$ref` gets protocol version 14's verdicts at
15, also after a compiled-form read-back, while a pattern that does not
parse there still refuses every document; registration at 15 refuses the
`$ref`-only pattern; and every pattern that compiles also parses, over the
contract corpus and 5,000 generated patterns.
Test would have caught this in CI: ✖ before fix, ✔ after. With only the
implementation files restored from v5.1-dev (plus a test-only
`attempted_patterns` on the old validator, returning every pattern since it
compiled all of them when built, and the old `try_from_root_v0` return shape
in one test helper), 7 of the new tests fail:
should_compile_a_pattern_only_when_a_value_first_reaches_it,
should_refuse_a_string_reaching_a_pattern_that_does_not_compile,
should_match_no_element_against_a_contains_pattern_that_does_not_compile,
should_refuse_a_pattern_that_does_not_parse,
should_measure_the_compiled_size_without_compiling_a_pattern,
should_check_documents_alike_however_the_native_validator_was_built and
should_refuse_only_where_a_document_reaches_a_pattern_no_registration_compiled.
With this change all of them pass. The other new tests pin behaviour that
does not change and pass on both.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post |
|
✅ Final review complete — no blockers (commit 9336c87) · triage: critical |
thepastaclaw
left a comment
There was a problem hiding this comment.
Final validation — Phase 1 + Phase 2
Static verification of head 6603add found no blocking defects. One suggestion remains: cover and document a confirmed non-string contains acceptance change that falls outside the claimed PV14 verdict parity. Current Rust workspace, wasm32, Swift, Kotlin, and JS code checks pass; review/hygiene checks remain pending, and the one-commit base drift does not conflict with this change.
🟡 1 suggestion(s)
Approved: nothing here blocks the merge. The non-blocking findings are optional: fix them forward, in this PR or a follow-up, without waiting for another review round.
Review provenance
Source: reviewer 1: gpt-6.1-sol (agent: phase2-reviewer, role: general); reviewer 2: gpt-6.1-sol (agent: phase2-reviewer, role: architecture-layering); reviewer 3: gpt-6.1-sol (agent: phase2-reviewer, role: platform-versioning); reviewer 4: gpt-6.1-sol (agent: phase2-reviewer, role: rust-quality); reviewer 5: gpt-6.1-sol (agent: phase2-reviewer, role: security-auditor); reviewer 6: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 7: muse-spark-1.3-contributor (agent: phase1-reviewer, role: rust-quality); final verifier: gpt-6.1-sol (agent: sol-verifier, role: final-verifier)
- Triage:
criticalbygpt-6.1-sol(effort low) — The intricate changes to validation_schema/validate/v0/mod.rs and native_schema/mod.rs alter consensus-relevant document acceptance and contract registration by moving regex compilation to first use and changing how compilation failures become validation results. - Phase 1 reviewers:
muse-spark-1.3-contributor— general (completed, effort xhigh); agentphase1-reviewer,muse-spark-1.3-contributor— rust-quality (completed, effort xhigh); agentphase1-reviewer - Phase 1 model:
muse-spark-1.3-contributor— not quota-gated; passed overgemini-3.8-flash-high(antigravity quota lookup failed),glm-5.3-flash(not used above high effort; tier asks max) - Single stage: Phase 1 and Phase 2 reviewed this head side by side, with no blocker gate between them (triage tier)
- Fresh verifier:
gpt-6.1-sol— final-verifier; agentsol-verifier - Phase 2 reviewers:
gpt-6.1-sol— general (completed, effort xhigh); agentphase2-reviewer,gpt-6.1-sol— architecture-layering (completed, effort xhigh); agentphase2-reviewer,gpt-6.1-sol— platform-versioning (completed, effort xhigh); agentphase2-reviewer,gpt-6.1-sol— rust-quality (completed, effort xhigh); agentphase2-reviewer,gpt-6.1-sol— security-auditor (completed, effort xhigh); agentphase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review (PastaClaw). How to work through them:
- Verify each finding against the code; fix only what is real, as narrowly as the finding.
- If a finding is wrong, decline it on its thread in one or two sentences; the bot answers once and stops.
- To defer one, link a tracking issue on its thread (or reply asking the bot to file one).
- Suggestions don't block the merge, but most are worth fixing in this PR; judge each on its merits.
- Keep the PR's scope; don't grow it to satisfy hardening suggestions.
- Carried items were raised in an earlier round: read the thread first, and don't re-fix what you already fixed, declined or deferred there. Disputed and deferred items are already handled and are not listed.
In `packages/rs-dpp/src/data_contract/methods/validate_document/v2/tests.rs`:
- [SUGGESTION] packages/rs-dpp/src/data_contract/methods/validate_document/v2/tests.rs:746-750: Cover and document the non-string contains verdict change
The string-only target and items leave an additional PV14-to-PV15 acceptance change uncovered. Remove `type` from the referenced const target, change items to integers, and validate `{"codes":[1]}` with the same oversized pattern. In the pinned jsonschema fork, `RefValidator::is_valid` compiles the target before validating the element; compilation fails, so PV14 rejects the array with `contains`. The native validator evaluates string constraints only for strings, so the integer satisfies this target without attempting its pattern and PV15 accepts the array, including after compiled-form read-back. This is consistent with compiling only when a string reaches the pattern, but not with the PR description's blanket statement that these legacy types now receive PV14 verdicts. Add this case to the cross-version/read-back tests and explicitly document the additional acceptance change, or preserve the legacy rejection if verdict parity is intended.
|
Bots are done — your move: post |
…t does not compile
A `pattern` applies to strings alone. Through protocol version 14 the JSON
Schema validator compiled the schema a `$ref` reaches before checking any
value against it, so it refused every value reaching a schema holding a
pattern that does not compile. From protocol version 15 a value that is not
a string is checked against the rest of that schema alone. An integer
element therefore meets a `contains` whose `$ref` target holds nothing but
such a pattern, where protocol version 14 found no match.
The new test registers such a type at protocol version 14 and checks
`{"numbers":[1]}`: refused with `contains` at 14, accepted at 15, also after
a compiled-form read-back, with no pattern compiled. v15.rs note 6 now
states this difference alongside the string and `contains` verdicts.
This change adds a test and documentation only, with no behaviour change. On
v5.1-dev the test fails, because protocol version 15 refused the whole type
there.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post |
thepastaclaw
left a comment
There was a problem hiding this comment.
Re-review — Final validation — Phase 1 + Phase 2
The new head adds the cross-version/read-back regression and protocol documentation requested by the prior review; independent source inspection confirms both are present. No new actionable issues were found, and the lazy-compilation behavior remains confined to unreleased protocol version 15. This was a static review: current CI reports successful Rust workspace, wasm32, and other executed code checks, with PastaClaw review and PR Hygiene pending.
🔴 0 blocking | 🟡 0 suggestion(s) | 💬 0 nitpick(s)
Review provenance
Source: reviewer 1: gpt-6.1-sol (agent: phase2-reviewer, role: general); reviewer 2: gpt-6.1-sol (agent: phase2-reviewer, role: architecture-layering); reviewer 3: gpt-6.1-sol (agent: phase2-reviewer, role: platform-versioning); reviewer 4: gpt-6.1-sol (agent: phase2-reviewer, role: rust-quality); reviewer 5: gpt-6.1-sol (agent: phase2-reviewer, role: security-auditor); reviewer 6: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 7: muse-spark-1.3-contributor (agent: phase1-reviewer, role: rust-quality); final verifier: gpt-6.1-sol (agent: sol-verifier, role: final-verifier)
- Triage:
criticalbygpt-6.1-sol(effort low) — The intricate changes in validation_schema/validate/v0/mod.rs and validate/pattern.rs alter consensus-visible document acceptance by deferring regex compilation and changing failures for reached patterns, including contains matching. - Phase 1 reviewers:
muse-spark-1.3-contributor— general (completed, effort xhigh); agentphase1-reviewer,muse-spark-1.3-contributor— rust-quality (completed, effort xhigh); agentphase1-reviewer - Phase 1 model:
muse-spark-1.3-contributor— not quota-gated; passed overgemini-3.8-flash-high(antigravity quota lookup failed),glm-5.3-flash(not used above high effort; tier asks max) - Single stage: Phase 1 and Phase 2 reviewed this head side by side, with no blocker gate between them (triage tier)
- Fresh verifier:
gpt-6.1-sol— final-verifier; agentsol-verifier - Phase 2 reviewers:
gpt-6.1-sol— general (completed, effort xhigh); agentphase2-reviewer,gpt-6.1-sol— architecture-layering (completed, effort xhigh); agentphase2-reviewer,gpt-6.1-sol— platform-versioning (completed, effort xhigh); agentphase2-reviewer,gpt-6.1-sol— rust-quality (completed, effort xhigh); agentphase2-reviewer,gpt-6.1-sol— security-auditor (completed, effort xhigh); agentphase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify the current code and confirm that no unresolved issues remain.
No unresolved findings remain from the prior review on this head.
|
Bots are done — your move: post |
Issue being fixed or feature implemented
At protocol version 15 a document type's native validator (
DocumentSchemaValidator, #5871) compiled everypatternof the type's schema when it was built. A validator is built on several paths besides registration: the first document checked against the type, measuring a contract's compiled size, and storing a type in its compiled form. Each of these compiled every pattern of the type, including ones no document would reach.This PR builds validators with a syntax check only and compiles each pattern the first time a value reaches it. Registration still compiles every pattern before it accepts a type.
What was done?
All changes are in rs-dpp, in generation 0 of the validation-schema builder and validator. Protocol version 15 is the only version that selects them.
ValidationSchemageneration 0 checks apattern's syntax with the regex-syntax AST parser and no longer compiles it. A pattern that does not parse is refused with the same text as before.try_from_root_v0andValidationSchema::buildno longer return a map of compiled regexes.validator_regex_parsesnow sets out the parser configuration explicitly: nest limit 250, no octal, whitespace not ignored, no empty{,n}minimum. These are the values regex 1.12.4 and regex-automata 0.4.14 pass to regex-syntax 0.8.11 today. A pattern the crate compiles therefore always parses, and a test checks that.DocumentSchemaValidatorholds each pattern in a privatePatternCell: the converted pattern plus aOnceLock<Option<Regex>>.with_patternsdrops its regex argument. It checks the syntax of every pattern the schema holds, whether the schema came from the builder or from a compiled-form read-back.&selfand shared. The cell is its only interior mutability.Debugprints only the pattern, soDebugoutput does not change with compile state.LazyRegex(contested indexes) is untouched.JsonSchemaError: keywordformat, params{"format":"regex"}, message"<pattern>" is not a "regex". This is the violation protocol version 14 gives there. Incontainsmatching, such a pattern matches no element. Neither case can produce aProtocolError.check_validation_schemacompiles every pattern of the validator it built. It walks the document schema first, then each$reftarget in key order, and refuses the first failure with the same text as before, adding the "(reached through$ref…)" suffix for a pattern inside a reference. The validator a registered type keeps holds every pattern compiled.StringConstraints::pattern,validator_regex,validator_regex_parses,with_patterns, the native-schema module docs, and notes 6–8 inv15.rs.In-place changes to shipped generations
None. No edited generation is selected by a protocol version that has been released as stable or activated on a network.
Protocol version 15 (unreleased, not activated on testnet or mainnet) changed in place:
validation_schema0 and the generation-0 native validator, selected only throughCONTRACT_VERSIONS_V7(used only byv15.rs). The same holds forvalidate_document2,check_document_type_schema1 and contract storage format 2. Protocol versions 14 and earlier build no native validator.patternthat parses but does not compile used to be stored with its validation schema refused, and every document of it was refused (InvalidContractStructure). It now keeps its validation schema, and its documents get protocol version 14's verdicts.formatviolation above.containsarray whose element reaches it failscontains.$refreaches before checking any value against it, and so refused every value reaching it. 15 checks a value that is not a string against the rest of that schema alone. For example, an integer element now meets acontainswhose$reftarget holds only such a pattern, where 14 found no match.$refalone reaches. None of the corpus contracts does.document_type_validation_schemareturns the schema for such a type instead of its refusal.$ref-only pattern and another builder refusal, the refusal is now the other one, at registration and when the type is built elsewhere. Both refusals areInvalidContractStructure.ContractCompiledSizeValidationcharge and every document verdict are the same.How Has This Been Tested?
New rs-dpp tests, at
PlatformVersion::latest()unless noted:should_refuse_a_pattern_that_does_not_parse: the builder refuses look-around with the same text, and holds a pattern too large for either size limit without compiling it.should_compile_a_pattern_only_when_a_value_first_reaches_it: no pattern is compiled when the validator is built; each one is compiled when a value first reaches it, and kept.should_refuse_a_string_reaching_a_pattern_that_does_not_compileandshould_match_no_element_against_a_contains_pattern_that_does_not_compile: the first-use verdicts.should_compile_a_pattern_with_its_ecma_escapes_rewritten:\ddoes not match U+0663 and\cAmatches U+0001.should_measure_the_compiled_size_without_compiling_a_pattern:validate_compiled_sizeon an unvalidated parse compiles no pattern and leaves the type with its validation schema.should_check_documents_alike_however_the_native_validator_was_built: a validator kept from registration, one read back from the compiled form, and one built from an unvalidated parse give identical results on matching, non-matching andcontainsdocuments.should_refuse_only_where_a_document_reaches_a_pattern_no_registration_compiled: a type registered atPlatformVersion::get(14)with such a pattern in aconstreached by a property or acontains$refgets the same verdicts at 15 as at 14, including after a compiled-form read-back.should_match_a_contains_element_no_pattern_applies_to_where_protocol_version_14_did_not: the non-string difference above, at 14, at 15 and after a compiled-form read-back.should_refuse_every_document_of_a_type_holding_a_pattern_that_does_not_parse: the non-parsing variant stays refused.should_refuse_at_registration_a_pattern_that_does_not_compile: registration at 15 refuses the$ref-only pattern.should_parse_every_pattern_that_compiles: every pattern that compiles also parses, over the mainnet and testnet contract corpus and over 5,000 seeded generated patterns.Red → green: with only the implementation files restored from
v5.1-dev, 7 of the new tests fail. The restore keeps a test-onlyattempted_patternsshim, which returns every pattern because the old validator compiled them all, and the oldtry_from_root_v0return shape in one test helper. With this PR all of them pass. The other new tests pin unchanged behaviour and pass on both.Ran locally:
cargo test -p dpp --all-features --lib --testsRUST_MIN_STACK=4194304 cargo test -p drive-abci --lib, which includes theContractCompiledSizeValidationfee pinscargo clippy -p dpp -p platform-version --all-targets --all-features -- --no-deps -D warnings, also with dpp's default features and withvalidationalonecargo fmt --allBreaking Changes
Consensus-visible at protocol version 15 only, which is unreleased and not activated: items (a)–(d) above. Protocol versions 14 and earlier are unaffected.
Checklist:
structure.rs, regeneratedgrovedb-structure.json, and checked the structure viewer link posted on this pull requestFor repository code-owners and collaborators only
🤖 Generated with Claude Code
PR Hygiene ·
9336c87/self-reviewedWhen every merge requirement is met, the
PR Hygienecheck passes. Reviewer limits do not block merging; other required GitHub checks and protections still apply.Summary by CodeRabbit