zap_transport: binary wire encoding for PaymentOffer / PaymentProof#21
Open
zap_transport: binary wire encoding for PaymentOffer / PaymentProof#21
Conversation
Adds switchboard/zap_transport.py — encode/decode PaymentOffer and
PaymentProof over the ZAP wire format (luxfi/zap, port 9999) as a
zero-allocation alternative to the existing JSON-in-HTTP-headers path.
Why: switchboard's HTTP/JSON transport is fine for HTTP/REST agents
but expensive for high-volume agent-to-agent traffic (every offer
parsed, allocated, copied). When two agents sit on the same Lux
network, ZAP lets them exchange offers/proofs with zero parse-time
allocation and ~10x smaller payloads.
zap_py is an optional dependency. If it isn't installed, the module
still imports cleanly and encode/decode raise ZapNotAvailable so
callers can fall back to the JSON path. Tests use pytest.importorskip
so the suite stays green either way.
Wire layout uses zap_py.StructBuilder so the schema is pinned and a
Go counterpart can mirror it exactly:
PaymentOffer:
scheme(u8) | chain_id(u64) | expires_at(u64, 0=null)
| recipient(address) | amount(bytes,uint256-be) | currency(text)
| description(text) | endpoint(text) | nonce(text)
PaymentProof:
chain_id(u64) | timestamp(u64) | payer(address) | tx_hash(hash32)
| amount(bytes,uint256-be) | nonce(text)
amount is a 32-byte big-endian uint256 rather than a uint64 so realistic
on-chain values aren't truncated.
11 tests cover: schema layout sanity, minimal+full offer roundtrip,
each PaymentScheme value, uint256-max amount, overflow/negative
rejection, proof roundtrip, invalid tx_hash length, ZapNotAvailable
gating, and a wire-size-vs-JSON sanity check.
Existing 40-test switchboard suite remains green. Two pre-existing
test files (test_nonce_manager.py, test_payment_protocol.py) had
collection-time syntax errors before this branch; they remain
unchanged.
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds
switchboard/zap_transport.py— encode/decodePaymentOfferandPaymentProofover the ZAP wire format (port 9999) as a zero-allocation alternative to the existing JSON-in-HTTP-headers path.Why
The HTTP/JSON transport is fine for HTTP/REST agents but expensive for high-volume agent-to-agent traffic — every offer is parsed, allocated, and copied. When two agents sit on the same Lux network, ZAP lets them exchange offers and proofs with zero parse-time allocation and ~10× smaller payloads (sanity-checked in
test_offer_wire_smaller_than_json).This is the first real consumer of
zap_py, the pure-stdlib Python port of the ZAP protocol. Validates the port end-to-end with a production switchboard data model.Wire layout
Schemas are declared with
zap_py.StructBuilderso a Go counterpart mirrors them exactly:amountis a 32-byte big-endian uint256 rather than auint64, so realistic on-chain values aren't truncated. Otherwise types match the existing JSON shape 1:1.Optional dependency
zap_pyis an optional dependency. If it isn't installed, the module imports cleanly andencode_offer/decode_offerraiseZapNotAvailableso callers fall back to the JSON path. Tests usepytest.importorskipso the suite stays green either way.Install path (until
luxfi-zapis on PyPI):pip install 'luxfi-zap @ git+https://github.com/luxfi/zap@main#subdirectory=python'Test plan
pytest tests/test_zap_transport.py -v— 11 passed with zap_py installedPaymentSchemevalueZapNotAvailablegating when zap_py is forced offpytest tests/test_x402_middleware.py tests/test_gas_budget.py tests/test_gas_tracker.py— 40 passed (existing suite still green)pytest tests/test_zap_transport.pywith zap_py uninstalled — module imports cleanly, suite skipstest_nonce_manager.py,test_payment_protocol.py) have collection-time syntax errors onmain, unrelated to this PR