Skip to content

fix(contract): prefer the exact event match in parseEvent - #1900

Merged
quietbits merged 5 commits into
mainfrom
issue-1818
Nov 24, 2026
Merged

quietbits merged 5 commits into
mainfrom
issue-1818

Conversation

@quietbits

@quietbits quietbits commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor
  • Fixed Spec.parseEvent() and the generated bindings' parseEvent(), which returned an earlier declaration with fields dropped when a later declaration fit the event — e.g. { amount, memo } parsed as Mint { amount } when Mint came first, and the topics [evt, from, to] parsed as Evt { from } when Evt came before EvtTo { from, to }
  • Preferred a declaration whose topics and data have exactly the shape that soroban-sdk emits: one topic for each prefix topic and topic-list param, every map key is a symbol that names a data param, one vec element per data param, and void data for a single value with no data params
  • Treated an undeclared trailing topic as a loose fit, so SAC events with the SEP-11 asset topic still parse. If a later declaration declares that topic, it wins, because the event bytes are the same for both (documented in the parseEvent docs and pinned by a test)
  • Kept a loose fit as the fallback when no declaration fits exactly, so older bindings still read an event from a newer contract version that adds fields (the first loose fit wins and the extra values are ignored, as on main)
  • Rejected a map with a missing key unless the param is Option, () or Val — the sparse map drops only void values, so {} no longer parses as Mint with no amount (a Val field can hold void, so it joins the issue's Option and ())
  • Decoded the loose fits only after no exact fit decodes — decoding each one when found cost +52–55% on a collision, and deferred it is within noise of a strict-only version
  • Checked the prefix before the topic count and read the event's topics once per call, so a declaration that does not match costs less. A trailing-topic event must still scan every declaration for an exact fit: a SAC transfer is ×1.08 and a 50-event spec is ×2.04; an exact match on the first declaration is unchanged (10 rotated rounds, A/A control ~1.00)
  • Checked older soroban-sdk releases (v23.0.0, v23.4.0, v25.0.0): they write a dense map with every key, which counts as an exact fit
  • Updated the malformed-data test, which expected a map with a non-symbol key to match with the required field dropped — that assertion pinned the bug
  • Updated the bindings comment on optional map fields: they are now looser than parseEvent needs. Tightening the generated types is a follow-up (Generated event interfaces mark required map fields as optional #1920)
  • Left two limits: when nothing fits exactly, declaration order decides, so { amount, memo, extra } returns Mint and [1, 2, 3] returns Evt before Evt2; and Spec.scValToNative lets a number through an Address or String type, so a numeric trailing topic can fill a typed topic param of a later declaration (follow-up: Spec.scValToNative passes a number through Address and String types #1919)
  • Did not sort declarations by size (suggested in review): with sparse maps, Mint { amount } and MintMemo { amount, memo: None } emit the same bytes, so a sort would always pick MintMemo, and it would change the winner for events that already match exactly

Closes #1818

@github-project-automation github-project-automation Bot moved this to Backlog (Not Ready) in DevX Nov 13, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Duplicate data-param names can make an event declaration incorrectly match a map while silently dropping an unmatched field, requiring distinct-name counting and a regression test.

1 open issue
What changed in this PR

Tightens contract-event matching in Spec.parseEvent(), which generated clients also use, to skip declarations whose data shape does not fit rather than return partial results.

Changes:

  • Checks map key coverage, exact vector length, and void data for single-value declarations with no data parameters.
  • Preserves sparse-map omissions for Option, () and Val, plus dense-map compatibility.
  • Adds regression tests and documents when parsing now returns undefined.
File Description
test/​unit/​spec/​event_spec.test.ts Adds shape-validation and fallback tests; updates the malformed-map expectation.
src/​contract/​spec.ts Documents stricter event-data matching.
src/​contract/​event_spec.ts Adds data-shape checks during candidate matching.
docs/​reference/​contracts-client.md Updates generated API documentation and source links.
CHANGELOG.md Records the fix and its compatibility impact.

Review effort: Balanced

Comment thread src/contract/event_spec.ts Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the contract-event matching changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@quietbits
quietbits requested a review from Ryang-21 November 13, 2026 16:45
@Ryang-21

Copy link
Copy Markdown
Contributor

This removes behavior we intended. Only one of the four rules is a pure bug fix.

The missing-key rule is right. Mint with no amount should never have matched, and the old comment already limited missing keys to void fields. Keep it.

The other three rules (undeclared map key, longer vec, non-void data on a single value with no data params) resolve collisions by rejecting the declaration. They also reject it when there is no collision, where the partial parse was the intended result. The CHANGELOG entry says so: data from a newer contract version now returns undefined. In practice, a contract upgrade that adds a field to an event makes every consumer on older generated bindings stop receiving that event, with no error. Before, they kept getting it with the fields they knew about. The host's sparse map reader also ignores extra keys, so the lenient reading matches upstream.

Both goals can be met. Run the declarations with the strict rules first. If none fits, run them again with the old lenient rules for these three cases, and keep the missing-key rule in both passes. Then:

  • { amount, memo } returns MintMemo, { info: 5 } returns PingWithInfo, and [1, 2] returns Evt2, as in this PR.
  • { amount, extra } returns Mint { amount } and [1, 2, 3] returns Evt2 { a, b }, as on main.

The second pass runs only when no declaration fits strictly, so the common path should cost the same as this PR. Was the two-pass version you measured this design, or a pre-check pass inside the loop?

If we take this, the "returns undefined when no declaration lists every key" test and the vec(1, 2, 3) assertion flip back, and the TSDoc and CHANGELOG should say that undeclared keys and extra elements are accepted when no declaration fits exactly.

@quietbits quietbits changed the title fix(contract): skip event declarations that do not fit the data fix(contract): prefer the event declaration that fits the data exactly Nov 13, 2026
@quietbits quietbits changed the title fix(contract): prefer the event declaration that fits the data exactly fix(contract): prefer the exact event match in parseEvent Nov 13, 2026
@Ryang-21

Copy link
Copy Markdown
Contributor

Should we consider sorting the declarations instead of the exact/loose split? Sort by prefix topics, then topic param count, then data param count, biggest first, and take the first one that matches.

That way we always try the best fit first and fall back to smaller declarations with the same prefix. It also covers the cases the split misses, like two declarations that differ only in topic params (Evt vs EvtTo), or a Val single value shadowing a map. Too-big declarations already fail to match (min topic count, short vec, missing map key), so nothing else needs to change for it to work.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block approval of the event-parsing update.

No open issues 🎉

Review effort: Balanced

@quietbits
quietbits merged commit fbb3d9c into main Nov 24, 2026
17 checks passed
@quietbits
quietbits deleted the issue-1818 branch November 24, 2026 19:07
@github-project-automation github-project-automation Bot moved this from Backlog (Not Ready) to Done in DevX Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Spec.parseEvent() matches an event declaration when the data does not fit its params

3 participants