mirror of
https://github.com/luxfi/node.git
synced 2026-10-30 15:24:05 +00:00
The P-chain, the Z-chain and the F-chain join the Q and X chains: what each puts on a wire is stated once in chains/schema, and zapgen writes the accessors. Five schemas, five emitted modules, and one runtime under all of them — chains/rust/zap, also emitted. The five hand-written copies of that runtime are gone: 4,056 lines that were the same reader written out five times, three of them byte-identical and two variants nobody had reconciled. WHAT THE VARIANTS DISAGREED ABOUT, and it was not cosmetic. Every hand copy defaulted `Builder::new` to wire version 2; the emitted runtime keeps `new` at version 1 and names `new_v2` for the other. So the swap silently moved byte 4 of every message the P-chain wrote outside a generated builder, and the warp differential caught it against the bytes Go wrote — a one-byte divergence in a header nobody reads by hand. Every P-chain writer now says new_v2, which is what it always wrote. THE SCHEMA WAS PROVED BEFORE THE TABLE WAS DELETED. A test asserted every offset and every size in pchain.zap against the constant the chain had been using — 27 structs, every field — and it passed before a line was removed. What replaces it now compares schema to schema: every kind's envelope is Base's envelope, checked field by field against the emitted constants, so `read_envelope` reading any transaction through Base's accessors is a thing that is checked rather than a convention to remember. THE BYTES DID NOT MOVE, and every chain says so against the corpus the Go chain generated: 68 P transactions and 4 P blocks, 19 X transactions and 6 X blocks, 12 Q blocks, 22 Z blocks, 43 F transactions, each re-encoded from the values that were read and compared to Go's hex. Those are every vector the corpus expects to parse; the rest are the deliberately malformed ones, and they are still refused. `make chains` is 725 vectors, 4 implementations, 13 evaluators, AGREED on every compared field, exit 0, nothing under NOT ANSWERED. Two fields of one name and a struct named but not declared are now refused by zapgen's front end, once, for every backend. The P-chain hit the first: an envelope's owner-address POOL and the owner group a transaction hands ownership TO were both called OwnerAddrs, and the emitted Rust had the same struct member twice. They are named apart now — NewOwner on a chain ownership transfer, NetworkOwner on a network being created. WHAT IS STILL HAND-WRITTEN, and it is the P-chain only: warp.rs and warpmsg.rs (the warp envelope and its payloads), genesis.rs, persist.rs (the disk state), and components.rs (the fx primitives it spends). Those are real remaining offsets and they are next. The fx primitives belong in neither chain's schema — the X-chain makes them and the P-chain spends them, so xchain.zap and a P-chain statement of the same TransferOutput would be two statements of one format. Lifting them into a shared fx.zap needs an import across schemas that zapgen does not have; the attempt is what found the undeclared-struct hole, and the refusal now says so at the schema rather than in emitted Rust. 378 platformvm, 290 xvm, 110 quantumvm, 150 zkvm and 97 fhevm unit tests, the five corpus byte tests, the golden vectors and the differentials all pass.
55 lines
1.9 KiB
Rust
55 lines
1.9 KiB
Rust
// SPDX-License-Identifier: BSD-3-Clause-Eco
|
|
// Copyright (C) 2019-2026, Lux Industries Inc. All rights reserved.
|
|
|
|
//! Every Z vector in the shared corpus, written back byte for byte.
|
|
//!
|
|
//! The corpus was generated by the Go chain, so a vector's hex is what Go
|
|
//! wrote. Reading one and writing it again has to reproduce it exactly: this
|
|
//! chain's writer and Go's are the same writer or they are not. That is what
|
|
//! makes the corpus a check on the WRITE path and not only on the read path —
|
|
//! a reader can agree about every field of bytes it could never have produced.
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use lux_zkvm::wire;
|
|
|
|
fn corpus() -> String {
|
|
let p = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
.join("../../../conformance/corpus/vectors.tsv");
|
|
std::fs::read_to_string(&p).unwrap_or_else(|e| panic!("{}: {e}", p.display()))
|
|
}
|
|
|
|
fn unhex(s: &str) -> Option<Vec<u8>> {
|
|
if s == "-" || s.len() % 2 != 0 {
|
|
return None;
|
|
}
|
|
(0..s.len() / 2)
|
|
.map(|i| u8::from_str_radix(&s[i * 2..i * 2 + 2], 16).ok())
|
|
.collect()
|
|
}
|
|
|
|
#[test]
|
|
fn every_z_vector_is_written_back_exactly_as_it_arrived() {
|
|
let text = corpus();
|
|
let mut blocks = 0;
|
|
for line in text.lines() {
|
|
if line.is_empty() || line.starts_with('#') {
|
|
continue;
|
|
}
|
|
let f: Vec<&str> = line.split('\t').collect();
|
|
if f.len() < 5 || f[0] != "V" || f[2] != "Z" || f[3] != "block" {
|
|
continue;
|
|
}
|
|
let id = f[1];
|
|
let Some(bytes) = unhex(f[4]) else { continue };
|
|
let Ok(body) = wire::read_block(&bytes) else {
|
|
continue;
|
|
};
|
|
assert_eq!(wire::write_block(&body), bytes, "{id}: block bytes moved");
|
|
blocks += 1;
|
|
}
|
|
// A corpus that stopped naming Z vectors would pass every assertion above.
|
|
assert!(blocks > 0, "no Z block vector was read back");
|
|
println!("{blocks} Z blocks written back byte for byte");
|
|
}
|