Add --hint option to stellar-xdr generate#544
Open
leighmcculloch wants to merge 4 commits into
Open
Conversation
--hint option to stellar-xdr generate
Contributor
There was a problem hiding this comment.
Pull request overview
Adds hint-based targeting to the stellar-xdr generate arbitrary CLI command by repeatedly generating arbitrary values until the value’s JSON contains all provided substrings (or a configured attempt limit is reached), addressing the workflow described in #472.
Changes:
- Introduces repeatable
--hint <STRING>and--hint-attempts <N>CLI options to filter generated values by substring matches on their JSON representation. - Refactors generation into
generate_one()andgenerate()and adds aHintNotFounderror when the attempt limit is exceeded. - Adds unit tests for single-hint, multi-hint, and not-found scenarios.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+136
to
+142
| for _ in 0..self.hint_attempts { | ||
| let v = Self::generate_one(type_)?; | ||
| let json = serde_json::to_string(&v)?; | ||
| if self.hint.iter().all(|hint| json.contains(hint)) { | ||
| return Ok(v); | ||
| } | ||
| } |
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.
What
The
stellar-xdr generate arbitrarycommand gains a repeatable--hint <STRING>option, paired with a--hint-attemptscap, that keeps generating arbitrary values until one whose JSON representation contains every supplied hint is found and then emits that value in whatever--outputformat was requested, erroring out if no match turns up within the attempt limit.Why
Generating a particular ledger key, a specific variant of a complex union or enum, or a value with some sub-shape was only possible by running
generate arbitraryover and over by hand and eyeballing the output until the random value happened to land on the wanted construction, as described in #472. Putting the generation in a loop that retries until the rendered JSON contains the caller-supplied hints delivers most of that targeting value without the cost of building and maintaining a dedicated DSL for specifying sub-types or sub-values, and accepting several hints lets a value be narrowed by more than one substring at once.Known limitations
The hints are always matched against the value's JSON representation regardless of the
--outputformat, since JSON is the only stable, human-readable rendering in which type and variant names appear, so hints filter on JSON field and variant names even when the emitted output is base64 or text. The search requires all supplied hints to be present in the same value and is bounded by--hint-attempts(default 20,000); an impossible or misspelled hint, or a combination that never co-occurs, runs to that limit before failing.Try it
Install:
Run:
Close #472