Skip to content

Add --hint option to stellar-xdr generate#544

Open
leighmcculloch wants to merge 4 commits into
mainfrom
generate-arbitrary-hint
Open

Add --hint option to stellar-xdr generate#544
leighmcculloch wants to merge 4 commits into
mainfrom
generate-arbitrary-hint

Conversation

@leighmcculloch

@leighmcculloch leighmcculloch commented Jun 11, 2026

Copy link
Copy Markdown
Member

What

The stellar-xdr generate arbitrary command gains a repeatable --hint <STRING> option, paired with a --hint-attempts cap, that keeps generating arbitrary values until one whose JSON representation contains every supplied hint is found and then emits that value in whatever --output format 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 arbitrary over 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 --output format, 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:

cargo install --locked --git https://github.com/stellar/rs-stellar-xdr --rev fe02d01319bb86133443d00af5d7a19d00e19315 --features cli stellar-xdr

Run:

stellar-xdr generate arbitrary --type LedgerKey --output json --hint trust_line --hint asset

Close #472

@leighmcculloch leighmcculloch marked this pull request as ready for review June 18, 2026 07:08
Copilot AI review requested due to automatic review settings June 18, 2026 07:08
@leighmcculloch leighmcculloch changed the title Add --hint option to generate arbitrary Add --hint option to stellar-xdr generate Jun 18, 2026
@leighmcculloch leighmcculloch enabled auto-merge June 18, 2026 07:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() and generate() and adds a HintNotFound error 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);
}
}
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.

In the cli add support for hinting towards specific outputs when using generate arbitrary

2 participants