open-standards/smart-byte/smart-byte-rs/crates/smart-byte-ilp/tests/packet_roundtrip.rs
Transaction Science 8180ee8c71 ingest wave D: 3 crates · op-fraud-graph + smart-byte-ilp + eoc-quantize
OpenPay (1 crate, 2487 LOC, 33 tests)
  op-fraud-graph    Graph-based fraud detection. EntityKind (card/email/
                    device/IP/address/phone/BIN+last4/account) with
                    canonicalization + hashed EntityKey + billing↔shipping
                    mismatch detection. Adjacency-list FraudGraph with
                    idempotent upsert + edge-merge. Double-hashing Bloom
                    filter (capacity+FPR-sized). Union-find connected
                    components. PageRank with dangling-sink handling.
                    Louvain community detection. Shared-instrument hub
                    rings. Sliding ring-buffer velocity counters. Synthetic
                    identity heuristics (freshness + heavy-use + mail-drop).
                    Structuring / smurfing / mule-chain DFS. EntityRisk +
                    TransactionRisk + RiskBand combiner.

Smart Byte (1 crate, ~1844 LOC, 44 tests)
  smart-byte-ilp    Interledger Protocol v4. OER codec (length-determinant,
                    var-octet-string, BE u32/u64). Prepare/Fulfill/Reject
                    packet roundtrip with all 19 IL-RFC-27 reject codes.
                    SHA-256 Condition + Fulfillment with constant-time
                    verify. ILP address validator (7 schemes, 1-128 char
                    segments, 1023-byte cap, segment-aligned prefix match).
                    STREAM (15 frame types). BTP. SPSP payment-pointer
                    resolver. Open Payments API. Longest-prefix routing
                    Connector with fixed-point quote conversion + balance +
                    rate-limit checks.

EOC (1 crate, 1921 LOC, 36 tests)
  eoc-quantize      Quantization primitives. int8 (symmetric/asymmetric
                    with zero-inclusion fix), int4 (GPTQ + AWQ group quant),
                    int2 (BitNet), fp8 (E4M3/E5M2), nf4 (QLoRA). GGUF v3
                    header parse. safetensors read sketch. Calibration
                    datasets + per-channel scales. KV-cache quant. Energy
                    tradeoff curves (joules-per-token across schemes).

Co-Authored-By: Claude Opus 4.7 <[email protected]>
2026-05-24 16:10:10 -04:00

105 lines
3 KiB
Rust

//! Encode every ILPv4 packet kind, decode it back, and verify the wire
//! shape matches what we expect.
use smart_byte_ilp::{
Address, Condition, Fulfill, Fulfillment, IlpPacket, Prepare, Reject, RejectCode, Result,
};
#[test]
fn prepare_wire_starts_with_type_12() -> Result<()> {
let f = Fulfillment::new([5u8; 32]);
let pkt = IlpPacket::Prepare(Prepare {
amount: 12_345,
expires_at: *b"20260524180000000",
condition: f.condition(),
destination: Address::parse("g.us.bank.alice")?,
data: b"payload".to_vec(),
});
let wire = pkt.encode();
assert_eq!(wire[0], 12);
let back = IlpPacket::decode(&wire)?;
assert_eq!(pkt, back);
Ok(())
}
#[test]
fn fulfill_wire_starts_with_type_13() -> Result<()> {
let pkt = IlpPacket::Fulfill(Fulfill {
fulfillment: Fulfillment::new([7u8; 32]),
data: vec![],
});
let wire = pkt.encode();
assert_eq!(wire[0], 13);
let back = IlpPacket::decode(&wire)?;
assert_eq!(pkt, back);
Ok(())
}
#[test]
fn reject_wire_starts_with_type_14() -> Result<()> {
let pkt = IlpPacket::Reject(Reject {
code: RejectCode::T04InsufficientLiquidity,
triggered_by: "g.us.bank".into(),
message: "out of pennies".into(),
data: vec![0xde, 0xad, 0xbe, 0xef],
});
let wire = pkt.encode();
assert_eq!(wire[0], 14);
let back = IlpPacket::decode(&wire)?;
assert_eq!(pkt, back);
Ok(())
}
#[test]
fn large_prepare_uses_long_form_length() -> Result<()> {
let f = Fulfillment::new([0u8; 32]);
let pkt = IlpPacket::Prepare(Prepare {
amount: 1,
expires_at: *b"20260524180000000",
condition: f.condition(),
destination: Address::parse("g.us.bank")?,
data: vec![0xa5; 300],
});
let wire = pkt.encode();
assert_eq!(wire[0], 12);
// Long-form length-determinant: high bit set on the second byte.
assert!(wire[1] >= 0x80);
let back = IlpPacket::decode(&wire)?;
assert_eq!(pkt, back);
Ok(())
}
#[test]
fn truncated_input_errors() {
let f = Fulfillment::new([1u8; 32]);
let pkt = IlpPacket::Prepare(Prepare {
amount: 1,
expires_at: *b"20260524180000000",
condition: f.condition(),
destination: Address::parse("g.us.bank").unwrap(),
data: vec![],
});
let wire = pkt.encode();
let truncated = &wire[..wire.len() - 5];
assert!(IlpPacket::decode(truncated).is_err());
}
#[test]
fn condition_round_trips_through_prepare() -> Result<()> {
let secret = Fulfillment::new([0xaa; 32]);
let cond: Condition = secret.condition();
let pkt = IlpPacket::Prepare(Prepare {
amount: 1,
expires_at: *b"20260524180000000",
condition: cond,
destination: Address::parse("g.us.bank.alice")?,
data: vec![],
});
let wire = pkt.encode();
let back = IlpPacket::decode(&wire)?;
match back {
IlpPacket::Prepare(p) => assert_eq!(p.condition, cond),
_ => panic!("not a prepare"),
}
Ok(())
}