Skip to content

primitives%feat: implement {Addr,Service,NetInfo}{V1,V2}, define surrounding traits (Net{Addr,Info}), define Checkable, add synthetic vectors, correct impl. errors#12

Merged
kwvg merged 33 commits into
dashpay:developfrom
kwvg:netaddr
Jun 22, 2026

Conversation

@kwvg

@kwvg kwvg commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Motivation

Due to a hard dependency on std in rust-bitcoin for its Addr types, we've had to implement our own equivalent types, which proved to be more involved as unlike Bitcoin, Dash embeds addresses (and services, which is an address paired with a port) in its transaction data, making it no longer just a P2P concern but consensus-critical for on-chain data.

This pull request implements the semantics to more closely match what is expected by validation routines in Dash Core, alongside synthetic test vectors to validate extended addresses in preparation for a V24 fork.

Additional Information

  • Depends on sdk%refac: consolidate exports, types, add utf8 serde codec to process domain names, preserve unknown values, drop legacy macro #11
  • Display and FromStr form a lossless round-trip for all address and service types across both versions. FromStr for V1 types explicitly rejects .onion and .b32.i2p with NetAddrError::AddrTooNew.
  • Version interconversion is provided as From<{Addr,Service}V1> for {Addr,Service}V2 (infallible upgrade) and TryFrom<{Addr,Service}V2> for {Addr,Service}V1. A type_cvrt! macro in dash-types generates both the owned and borrowed From/TryFrom impls from a single closure.
  • Payload Checkable impls (ProRegTx, ProUpServTx) now delegate to the type-level check() calls and NITrait queries instead of reimplementing validation inline. ProTxInvalid::NetInfoInvalid now includes the underlying NIError.

Breaking Changes

  • ExtendedNetInfo has been renamed to NetInfoV2.
  • NetInfoEntry has been renamed to NIEntry.
  • NetInfoPurpose has been renamed to NIPurpose.
  • I2P variant on AddrV2 has been renamed to I2p.
  • AddrV2 has changed to a fixed-size enum. Field access via .network and .addr must use .network() and .bytes() methods.
  • NIEntryCode replaces the bare 0x01/0x02 literals in codec paths with a typed enum (Service, Domain, Unknown(u8)).
  • NIEntry::Invalid has been removed. Unknown entry types now fail at decode time.

Corrections

  • MAX_ENTRIES corrected from 8 to 4.
  • NIEntry::Service corrected to ServiceV2 from ServiceV1.

How Has This Been Tested?

cargo fmt --check
cargo test --features full,_internal
./contrib/lint_all.py

Checklist

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional tests
  • I have made corresponding changes to the documentation (note: N/A)
  • I have assigned this pull request to a milestone (for repository code-owners and collaborators only)

@kwvg kwvg added this to the 0.1 milestone Jun 22, 2026
@kwvg kwvg self-assigned this Jun 22, 2026
@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 82ed5e5c-3f82-4471-9daf-61ba9d26234f

📥 Commits

Reviewing files that changed from the base of the PR and between 711e9da and f9c577f.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (17)
  • pkgs/p2p_core/src/msg/addr.rs
  • pkgs/p2p_core/tests/addrv2.rs
  • pkgs/primitives/Cargo.toml
  • pkgs/primitives/corpus/proregtx.json5
  • pkgs/primitives/corpus/proupservtx.json5
  • pkgs/primitives/src/lib.rs
  • pkgs/primitives/src/payload/mod.rs
  • pkgs/primitives/src/payload/proregtx.rs
  • pkgs/primitives/src/payload/proupservtx.rs
  • pkgs/primitives/src/prelude.rs
  • pkgs/primitives/src/types/addrv1.rs
  • pkgs/primitives/src/types/addrv2.rs
  • pkgs/primitives/src/types/mod.rs
  • pkgs/primitives/src/types/netaddr.rs
  • pkgs/primitives/src/types/netinfo.rs
  • pkgs/primitives/src/types/util.rs
  • pkgs/types/src/lib.rs
✅ Files skipped from review due to trivial changes (3)
  • pkgs/primitives/Cargo.toml
  • pkgs/primitives/src/prelude.rs
  • pkgs/primitives/corpus/proregtx.json5
🚧 Files skipped from review as they are similar to previous changes (14)
  • pkgs/types/src/lib.rs
  • pkgs/primitives/src/lib.rs
  • pkgs/primitives/src/types/mod.rs
  • pkgs/p2p_core/src/msg/addr.rs
  • pkgs/primitives/src/types/util.rs
  • pkgs/p2p_core/tests/addrv2.rs
  • pkgs/primitives/src/payload/proupservtx.rs
  • pkgs/primitives/src/payload/mod.rs
  • pkgs/primitives/src/payload/proregtx.rs
  • pkgs/primitives/src/types/netaddr.rs
  • pkgs/primitives/src/types/addrv1.rs
  • pkgs/primitives/corpus/proupservtx.json5
  • pkgs/primitives/src/types/addrv2.rs
  • pkgs/primitives/src/types/netinfo.rs

📝 Walkthrough

Walkthrough

Replaces the struct-based AddrV2 and macro-based AddrV1 with explicit typed enums and a new [u8;16] type respectively. Introduces NetworkType, NetAddr trait, NetAddrError, is_bad_port, NIPurpose/NIEntry/NetInfoV2 replacing legacy netinfo types, Base32/hex utilities, and a type_cvrt! macro. Updates ProRegTx and ProUpServTx codec/validation paths, rewires public re-exports, and extends corpus test fixtures.

Changes

Network Address Types Overhaul

Layer / File(s) Summary
NetworkType, NetAddr trait, NetAddrError, is_bad_port, encoding utilities, and type_cvrt! macro
pkgs/primitives/src/types/netaddr.rs, pkgs/primitives/src/types/util.rs, pkgs/types/src/lib.rs
Defines NetworkType enum with BIP155 u8 codec, NetAddrError structured variants, the NetAddr trait with RFC-range classification default methods and is_routable(), is_bad_port(), unpadded Base32 and lowercase hex encode/decode helpers with RFC 4648 validation, and the type_cvrt! macro generating paired owned/reference From/TryFrom impls.
AddrV2 typed enum: codec, validation, display/parse, ServiceV2
pkgs/primitives/src/types/addrv2.rs
Replaces AddrV2 { network, addr: Vec<u8> } struct with a typed enum (Ipv4([u8;4]), Ipv6([u8;16]), TorV3([u8;32]), I2p([u8;32]), Cjdns([u8;16]), Unknown). Rewires BIP155 codec with IPv6→Cjdns mapping for 0xfc prefix, replaces generic validation with variant-specific checks, overhauls Display/FromStr (Tor v3 SHA3-256 checksum + base32, I2P base32, unknown hex), updates ServiceV2::Checkable with I2P port-must-be-zero rule, and expands tests.
AddrV1 explicit type: validation, NetAddr, display/parse, ServiceV1, conversions
pkgs/primitives/src/types/addrv1.rs
Replaces macro-based ADDRv1 with explicit AddrV1(pub [u8;16]). Adds RFC3849/null/broadcast Checkable, NetAddr implementation with IPv4-mapped byte rules, Display/FromStr with Tor/I2P suffix rejection (AddrTooNew), TryFrom<AddrV2> conversion, ServiceV1 Checkable/Display/FromStr, split_service_str bracket-aware helper, TryFrom<ServiceV2> for ServiceV1, and expanded tests covering roundtrips, classification, and conversion failures.
NIPurpose, NIEntry, NIError, NetInfoV2, check_domain, NetInfoV1, and NITrait
pkgs/primitives/src/types/netinfo.rs
Replaces NetInfoPurpose/NetInfoEntry/ExtendedNetInfo with NIPurpose, NIEntryCode, NIEntry (carrying ServiceV2 or Domain { name, port }), NIError, and NetInfoV2 with full structural Checkable (duplicate purpose/addr:port detection, purpose↔entry-type compatibility, per-group size limits). Adds check_domain RFC 1035-style validation with reserved TLD rejection. Introduces NetInfoV1 legacy wrapper and unified NITrait dispatch over NetInfo::Legacy/Extended. Expands test suite.
ProRegTx and ProUpServTx codec/validation updated for new netinfo types
pkgs/primitives/src/payload/mod.rs, pkgs/primitives/src/payload/proregtx.rs, pkgs/primitives/src/payload/proupservtx.rs
Updates ProRegTx and ProUpServTx decode to use NetInfoV2::decode/NetInfoV1 wrapping, encode to write directly without an intermediate Vec<u8>, and check with net_info.is_empty() + per-variant delegation. Rewrites check_sptx_netinfo to call ext.check() and ext.has_entries(NIPurpose) with version-gated platform rules. Changes ProTxInvalid::NetInfoInvalid from unit variant to struct variant carrying NIError.
Module re-exports, prelude, p2p adapter, dev-dep, and corpus fixtures
pkgs/primitives/src/types/mod.rs, pkgs/primitives/src/lib.rs, pkgs/primitives/src/prelude.rs, pkgs/primitives/Cargo.toml, pkgs/p2p_core/src/msg/addr.rs, pkgs/p2p_core/tests/addrv2.rs, pkgs/primitives/corpus/proregtx.json5, pkgs/primitives/corpus/proupservtx.json5
Declares netaddr/util modules and replaces old ExtendedNetInfo/NetInfoEntry/NetInfoPurpose re-exports with NI*/NetInfoV1/NetInfoV2; adds ToString to prelude; adds hex-literal dev-dependency; updates AddrV2Entry::fmt to call .network() method and adapts p2p tests to use AddrV2 enum variants; adds synthetic ExtAddr corpus entries (IPv4-only, dual-stack, empty, mixed address types) for both proregtx and proupservtx.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: implementing address, service, and network info types with validation and trait definitions.
Description check ✅ Passed The description is comprehensively related to the changeset, explaining motivation, implementation details, breaking changes, corrections, and testing approach.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Jun 22, 2026

Copy link
Copy Markdown

Warning

This pull request may have conflicts, please coordinate with the authors of these pull requests.

Potential conflicts

kwvg added 26 commits June 23, 2026 03:15
@kwvg

kwvg commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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