Skip to content

Conversation

@evanlinjin
Copy link
Member

@evanlinjin evanlinjin commented Sep 12, 2025

Breaking Changes

This PR completely redesigns the request/response API to be simpler and more maintainable.

Summary

Replaces the complex macro-based request/response system with straightforward enums, improving code clarity and reducing complexity.

Changes

Request System

  • ✅ Replace macro-generated request types with a single Request enum
  • ✅ Inline all request parameters directly in enum variants
  • ✅ Remove pending_request.rs and batch_request.rs (no longer needed)

Response System

  • ✅ Create unified Response enum for all response types
  • ✅ Split ambiguous response types for compile-time clarity:
    • HeaderRespHeaderResp (simple) and HeaderWithProofResp (with merkle proof)
    • HeadersRespHeadersResp (simple) and HeadersWithCheckpointResp (with checkpoint)
  • ✅ Auto-detect response type in from_json() based on fields present

Error Handling

  • ✅ Add clean ClientError enum with just 3 variants: SendError, ServerError, Cancelled
  • ✅ Remove complex nested error types from client methods

Client API

  • ✅ Direct methods for each request type on both AsyncClient and BlockingClient
  • ✅ Type-safe return types (e.g., header() returns HeaderResp, header_with_proof() returns HeaderWithProofResp)
  • ✅ Consistent error handling across all methods

Testing

  • ✅ Fix blocking client tests to use proper synchronization
  • ✅ Remove sleep-based waiting in favor of env.wait_until_electrum_sees_block()
  • ✅ Add proper assertions and clean shutdown

Motivation

The previous macro-based system was:

  • Hard to understand and debug
  • Poor IDE support (no go-to-definition, autocomplete issues)
  • Overly complex for the relatively simple Electrum protocol

The new enum-based approach:

  • Simple and explicit
  • Great IDE support
  • Easy exhaustive matching
  • Clear request→response relationships

Migration Guide

// Old (macro-generated types)
let req = HeaderRequest { height: 100, cp_height: None };
let resp = client.request(req).await?;

// New (enum-based)
let resp = client.header(100).await?;  // Returns HeaderResp
// or
let resp = client.header_with_proof(100, 50).await?;  // Returns HeaderWithProofResp

Closes #6

BREAKING CHANGE: Request/Response API completely redesigned

- Replace macro-based system with simple Request/Response enums
- Inline all request parameters directly in enum variants
- Split HeaderResp into HeaderResp/HeaderWithProofResp
- Split HeadersResp into HeadersResp/HeadersWithCheckpointResp
- Add clean ClientError enum for all client methods
- Fix blocking client tests to use proper synchronization
@evanlinjin evanlinjin changed the title Simplify. Turn enum-based. refactor!: Simplify request/response system with enum-based approach Sep 12, 2025
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.

1 participant