Skip to content

Segmentation fault in examples/03_send_transaction with middleware #37

Description

@ch4r10t33r

Bug Description

Segmentation fault crash when running examples/03_send_transaction at "Example 3: Transaction with Middleware". The crash occurs after successfully creating legacy and EIP-1559 transactions, specifically when attempting to use middleware functionality.

Error Output

Example 3: Transaction with Middleware
───────────────────────────────────────
zsh: segmentation fault  ./zig-out/examples/03_send_transaction

Successful Output Before Crash

The example successfully completes:

  • ✅ Wallet creation
  • ✅ Provider connection to Sepolia
  • ✅ Example 1: Legacy transaction creation
  • ✅ Example 2: EIP-1559 transaction creation
  • CRASH at Example 3: Transaction with Middleware

Steps to Reproduce

  1. Build the examples:

    zig build -Dexamples examples
  2. Run example 03:

    ./zig-out/examples/03_send_transaction
  3. Crash occurs at Example 3 (middleware section)

Expected Behavior

Should successfully demonstrate transaction creation with gas middleware without crashing.

Environment

  • Zig Version: 0.14.1
  • OS: macOS (darwin 23.6.0)
  • Architecture: ARM64 (Apple Silicon)
  • Network: Sepolia testnet

Code Context

Check examples/03_send_transaction.zig around Example 3:

// Example 3: Transaction with Middleware
std.debug.print("Example 3: Transaction with Middleware\n", .{});
std.debug.print("───────────────────────────────────────\n", .{});
{
    // Likely creating GasMiddleware and using it
    // Crash happens here when interacting with middleware
}

Potential Root Causes

  1. Middleware initialization issue - GasMiddleware may not be properly initialized
  2. Provider lifetime - Middleware holds reference to provider that's invalid
  3. u256 arithmetic issue - After migrating to native u256, some gas calculations might have issues
  4. Memory corruption - Pointer issue in middleware code

Related Code Areas

  • src/middleware/gas.zig - Gas middleware implementation
  • examples/03_send_transaction.zig - Failing example (around line 120-140)
  • Related to recent u256 migration where we changed:
    • .mulScalar()*
    • .add()+
    • .sub()-

Additional Context

  • This crash occurs after the u256 migration where arithmetic operations were changed
  • Other examples work fine:

Impact

  • Severity: High - Segmentation fault is a critical error
  • Scope: Affects middleware usage in examples (possibly production code too)
  • User Impact: Cannot use gas middleware functionality

Related Issues

Next Steps

  1. Review examples/03_send_transaction.zig lines around Example 3
  2. Check if GasMiddleware properly handles native u256 arithmetic
  3. Verify all gas calculation methods work with native u256
  4. Add null pointer checks in middleware
  5. Run under debugger to get exact crash location

Testing

After fix, verify with:

zig build -Dexamples examples
./zig-out/examples/03_send_transaction
# Should complete all examples without crash

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions