Skip to content

Add dataclass encoding - #108

Open
zjkmxy wants to merge 7 commits into
named-data:masterfrom
zjkmxy:dev2
Open

zjkmxy wants to merge 7 commits into
named-data:masterfrom
zjkmxy:dev2

Conversation

@zjkmxy

@zjkmxy zjkmxy commented Aug 17, 2026 •

Copy link
Copy Markdown
Member

Ref: #97

New wire format is longer but with advantage:

  • Built-in dataclass support, such as =, repr and asdict. Also, models can be easily encoded into JSON and other formats for human/subagents to read.
  • Built-in typing annotation. Type checker now can correctly understand field types.
  • Faster than v1. Using type hints cache, v2 is about 80% of the time in v1 for encoding and decoding.

Not so good points:

  • v2 typing is longer than v1.
  • Signature representation is still ugly.
  • Annotation handling could change with interpreters.
  • bytes field could actually hold memoryview. Need further effort to improve.

I plan to replace old model with the new encoding model in a future breaking change. This diff adds new method as extra files for people to review.

zjkmxy and others added 4 commits September 6, 2026 17:58
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The new codec and application stack contain unresolved decoding, buffer sizing, reply-contract, and LP framing bugs.

Review effort: Balanced
Findings: 2 High severity · 5 Medium severity

Open (7)
What changed in this PR

Adds a parallel dataclass-based TLV encoding stack and migrates selected applications, commands, and examples to it.

Changes:

  • Implements dataclass TLV encoding, parsing, maps, signatures, and schema caching.
  • Adds dataclass packet, NDNLP, security, NFD management, and application modules.
  • Migrates CLI tools/examples and adds compatibility and integration tests.
File Description
tests/​misc/​security_v2_2_test.py Tests dataclass certificate and SafeBag support.
tests/​misc/​nfd_mgmt_2_test.py Tests NFD management interoperability.
tests/​integration/​app_v2_2_test.py Exercises the dataclass-based application stack.
tests/​encoding/​tlv_model_v2_test.py Tests the new TLV codec.
tests/​encoding/​ndnlp_v2_2_test.py Tests dataclass NDNLP handling.
tests/​encoding/​ndn_format_0_3_2_test.py Tests packet encoding and signatures.
src/​ndn/​transport/​nfd_registerer.py Adds dataclass management registration.
src/​ndn/​security/​tpm/​tpm_osx_keychain.py Formats key names in errors.
src/​ndn/​encoding/​tlv_model_v2.py Implements dataclass TLV serialization.
src/​ndn/​encoding/​ndnlp_v2_2.py Adds dataclass NDNLP models.
src/​ndn/​encoding/​ndn_format_0_3_2.py Adds dataclass packet models.
src/​ndn/​encoding/​__init__.py Exports the new TLV API.
src/​ndn/​bin/​sec/​utils.py Uses dataclass security constants.
src/​ndn/​bin/​sec/​cmd_sign_cert.py Migrates certificate signing.
src/​ndn/​bin/​sec/​cmd_import_cert.py Migrates certificate importing.
src/​ndn/​bin/​sec/​cmd_get_signreq.py Migrates signing requests.
src/​ndn/​bin/​sec/​cmd_get_childitem.py Migrates certificate display.
src/​ndn/​bin/​nfdc/​utils.py Uses the new application stack.
src/​ndn/​bin/​nfdc/​cmd_set_strategy.py Migrates strategy commands.
src/​ndn/​bin/​nfdc/​cmd_remove_strategy.py Migrates strategy removal.
src/​ndn/​bin/​nfdc/​cmd_remove_route.py Migrates route removal.
src/​ndn/​bin/​nfdc/​cmd_remove_face.py Migrates face removal.
src/​ndn/​bin/​nfdc/​cmd_new_route.py Migrates route creation.
src/​ndn/​bin/​nfdc/​cmd_new_face.py Migrates face creation.
src/​ndn/​bin/​nfdc/​cmd_get_strategy.py Migrates strategy queries.
src/​ndn/​bin/​nfdc/​cmd_get_status.py Migrates status queries.
src/​ndn/​bin/​nfdc/​cmd_get_route.py Migrates route queries.
src/​ndn/​bin/​nfdc/​cmd_get_face.py Migrates face queries.
src/​ndn/​appv2_2.py Adds the dataclass-based application implementation.
src/​ndn/​app_support/​security_v2_2.py Adds dataclass security models.
src/​ndn/​app_support/​nfd_mgmt_2.py Adds dataclass NFD management models.
src/​ndn/​app_support/​light_versec/​checker.py Uses dataclass certificate parsing.
src/​ndn/​app_support/​light_versec/​binary.py Converts LVS models to dataclasses.
examples/​dpdk_experimental/​udp_producer.py Migrates the DPDK producer.
examples/​dpdk_experimental/​udp_consumer.py Migrates the DPDK consumer.
examples/​appv2/​keychain_cert/​keychain_register.py Migrates keychain registration.
examples/​appv2/​keychain_cert/​fetch_certificate.py Migrates certificate fetching.
examples/​appv2/​forwarding_hint/​producer.py Migrates the forwarding-hint producer.
examples/​appv2/​forwarding_hint/​consumer.py Migrates the forwarding-hint consumer.
examples/​appv2/​basic_packets/​producer.py Migrates the basic producer.
examples/​appv2/​basic_packets/​consumer.py Migrates the basic consumer.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/ndn/appv2_2.py
Comment on lines +437 to +439
frag_l = len(data)
lp_l = len(pt_wire) + enc.get_tl_num_size(ndnlp.LpTypeNumber.FRAGMENT) + enc.get_tl_num_size(frag_l)
wire_l = enc.get_tl_num_size(ndnlp.LpTypeNumber.LP_PACKET) + enc.get_tl_num_size(lp_l) + lp_l
Comment on lines +452 to +454
if fixed_len is not None:
n = fixed_len
elif val <= 0xFF:
Comment thread src/ndn/appv2_2.py
Comment on lines +262 to +263
data = lp_pkt.fragment
typ, _ = enc.parse_tl_num(data)
Comment thread src/ndn/appv2_2.py
Comment on lines +366 to +369
if pit_token is None:
self._put_raw_packet(data)
else:
self._put_raw_packet_with_pit_token(data, pit_token)
Comment on lines +546 to +548
if kind == 'model':
inner_markers: dict = {}
length = _encoded_length_model(val, inner_markers)
Comment on lines +881 to +882
length, sz_l = parse_tl_num(mv, offset)
offset += sz_l
Comment on lines +924 to +930
_val_typ, _sz_t2 = parse_tl_num(mv, offset)
offset += _sz_t2
length, _sz_l2 = parse_tl_num(mv, offset)
offset += _sz_l2

val = _parse_value(f'{fname}[{idx}#v]', spec.val,
mv, offset, length, offset_btl, ignore_critical)
@zjkmxy
zjkmxy marked this pull request as ready for review September 27, 2026 20:12
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.

2 participants