Skip to content

feat: architect zero-allocation XDR stream parser - #454

Merged
Damola-Sodiq merged 1 commit into
Sahara-Pay:mainfrom
kosisochukwu1234:feature/xdr-stream-parser
Jul 27, 2026
Merged

feat: architect zero-allocation XDR stream parser#454
Damola-Sodiq merged 1 commit into
Sahara-Pay:mainfrom
kosisochukwu1234:feature/xdr-stream-parser

Conversation

@kosisochukwu1234

Copy link
Copy Markdown
Contributor

Closes #416

Replaces DOM-based XDR parsing with a zero-allocation, cursor-based byte reader (xdr_parser.rs). This parser extracts relevant fields (like transaction IDs) sequentially from raw XDR streams without allocating intermediate heap structures for the entire payload.

Architectural Changes

  • Cursor Implementation: Introduced Cursor<'a> in xdr_parser.rs to wrap &[u8], providing strictly bounds-checked methods for reading u32, i32, bool, u64, and opaque byte slices.
  • Zero-Allocation: The parser operates entirely in #![no_std] style. It avoids Vec or String allocations by reading directly from the underlying byte slice and extracting sub-slices for variable-length opaque data.
  • Skipping Discarded Fields: Added a skip method to efficiently bypass irrelevant union arms and unneeded fields, ensuring discarded data doesn't consume memory or CPU cycles.

Safety & Constraints

  • Strict Bounds Checking: Every read operation verifies remaining() bytes against the requested length, preventing out-of-bounds panics. Returns XdrError::UnexpectedEndOfStream on failure.
  • Padding Validation: XDR padding boundaries are mathematically calculated and explicitly verified to be zero, returning XdrError::InvalidPadding for malformed data.
  • Graceful Error Handling: Rejects malformed or incomplete streams via the XdrError enum instead of unwrapping or panicking.

Module Integration

  • Exported the new xdr_parser module in packages/contracts/src/lib.rs.
  • Added comprehensive unit tests for endianness conversion, boolean bounds (strictly 0 or 1), length prefixing, padding alignment, and stream exhaustion.

@Damola-Sodiq Damola-Sodiq 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.

Reviewed

@Damola-Sodiq
Damola-Sodiq merged commit 6cc91d6 into Sahara-Pay:main Jul 27, 2026
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.

Architect a zero-allocation XDR stream parser for lightweight event processing

2 participants