Skip to content

fix: provisional PreviousTxn{Id,LedgerSeq} double threading#515

Merged
RichardAH merged 19 commits intoXahau:devfrom
sublimator:nd-debug-missing-previoustxnid-2025-06-05
Jul 8, 2025
Merged

fix: provisional PreviousTxn{Id,LedgerSeq} double threading#515
RichardAH merged 19 commits intoXahau:devfrom
sublimator:nd-debug-missing-previoustxnid-2025-06-05

Conversation

@sublimator
Copy link
Collaborator

@sublimator sublimator commented Jun 5, 2025

Fix missing PreviousTxnID in metadata

The issue was that PreviousTxnID and PreviousTxnLgrSeq fields were missing from the metadata of modified objects (discovered when investigating RippleState/trustline modifications). The root cause was that during provisional metadata generation, ApplyStateTable::threadItem() was modifying the original SLE's threading fields. When the same ApplyStateTable was reused for final metadata generation, the comparison between original and modified states didn't detect any change in these fields since both states had already been updated with the new values.

The fix implements a snapshot-and-restore mechanism in ApplyStateTable::threadItem() that:

  1. Saves the original PreviousTxnID/PreviousTxnLgrSeq values on the first call (during provisional metadata)
  2. Restores these original values before threading on subsequent calls (during final metadata)

This ensures the metadata correctly reflects which transaction previously modified the object. The fix is gated behind the new fixProvisionalDoubleThreading amendment to maintain backward compatibility.

Why the testTieBreaking test fails with fixProvisionalDoubleThreading enabled:

The testTieBreaking test in TxQ_test.cpp verifies the transaction queue's pseudo-random ordering mechanism. When transactions have the same fee, TxQ uses the formula txID XOR parentHash to
determine their order.

The critical insight is that the parent hash includes both the state tree AND the transaction tree (which contains metadata). When fixProvisionalDoubleThreading is enabled:

  1. Transaction metadata now correctly includes PreviousTxnID/PreviousTxnLgrSeq fields for modified RippleState objects
  2. This changes the transaction tree hash
  3. Which changes the parent ledger hash
  4. Which changes the XOR results used for transaction ordering
  5. Which breaks the test's hardcoded expectations about transaction order

The test was written with specific assumptions about which transactions would be included in which order. Since fixing the metadata bug fundamentally changes the pseudo-random ordering, the test
fails - not because the TxQ is broken, but because the test is brittle and depends on the exact ordering produced by the buggy metadata.

Solution: Rather than rewrite the test to work with both versions, we disabled the fixProvisionalDoubleThreading amendment for this specific test (testTieBreaking(all - fixProvisionalDoubleThreading)), allowing it to
continue validating the TxQ logic without being affected by the metadata fix.

@sublimator sublimator changed the title experiment: debug missing previoustxnid fix: provisional PreviousTxn{Id,LedgerSeq} double threading Jun 6, 2025
@sublimator sublimator force-pushed the nd-debug-missing-previoustxnid-2025-06-05 branch from a950be7 to 59ba335 Compare June 7, 2025 04:39
@sublimator sublimator force-pushed the nd-debug-missing-previoustxnid-2025-06-05 branch from 59ba335 to 7efa1d1 Compare June 7, 2025 04:43
@sublimator sublimator marked this pull request as ready for review July 1, 2025 04:07
sublimator and others added 6 commits July 1, 2025 11:37
This commit makes several small improvements to the implementation
of the `fixProvisionalDoubleThreading` amendment.

- The explicit call to `originalThreadingState_.clear()` is removed
  as it was superfluous. The `ApplyStateTable` is single-use per
  transaction, so its member map is destroyed along with it.
- A comment is added to the `originalThreadingState_` declaration
  to clarify this lifecycle assumption.
- The comment in `PreviousTxn_test.cpp` is updated to explicitly
  name the `fixProvisionalDoubleThreading` amendment for better
  readability.
- Minor test cleanup, including adjusting the log level and ensuring
  a final newline.
This commit improves the comment for the `fixProvisionalDoubleThreading`
logic within the `threadItem` function.

The new comment clarifies *why* the original SLE state is restored in a
single batch after the entire provisional metadata pass is complete,
rather than immediately within each `threadItem` call. It explicitly
notes that `threadItem` can be called multiple times on the same SLE
within a single pass, making an immediate restore incorrect. This
provides better context for future developers.
This commit refactors the `threadItem` function to simplify its
control flow.

The previous implementation had three separate branches that all
contained a call to `sle->thread()`. This change consolidates them
into a single, unconditional call. The conditional logic for saving
the original SLE state (when the `fixProvisionalDoubleThreading`
amendment is enabled) is now handled in a separate block before the
common threading logic, removing redundancy and making the function's
intent clearer.
tequdev
tequdev previously approved these changes Jul 6, 2025
Co-authored-by: tequ <git@tequ.dev>
tequdev
tequdev previously approved these changes Jul 7, 2025
- test both with and without fixProvisionalDoubleThreading amendment
- verify PreviousTxnID appears in metadata only when fix is enabled
- confirm SLE state is correct regardless of the bug
- check that PreviousTxnID never appears in PreviousFields
- validate account threading works properly in both cases
@sublimator
Copy link
Collaborator Author

sublimator commented Jul 8, 2025 via email

@RichardAH RichardAH merged commit d593f3b into Xahau:dev Jul 8, 2025
8 of 9 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.

3 participants