refactor(tvix/nix-compat/wire): express magics as u64

This allows using read_u64, write_u64, which is a bit easier to juggle
with.

Also, update names to align with the nix codebase, which makes it easier
to spot both the constant name as well as the value.
Leave the ASCII interpretation as a comment afterwards.

Change-Id: I0b9ab187acd22807e2785b0722aa4300dab37c51
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11378
Tested-by: BuildkiteCI
Reviewed-by: picnoir picnoir <picnoir@alternativebit.fr>
Autosubmit: flokli <flokli@flokli.de>
This commit is contained in:
Florian Klink 2024-04-08 11:31:41 +03:00 committed by clbot
parent fd749070e2
commit c05f90e611
2 changed files with 15 additions and 21 deletions

View file

@ -11,14 +11,11 @@ use crate::wire::primitive;
use super::bytes::read_string; use super::bytes::read_string;
// LE-encoded nixc on 64 bits. Because why not. pub static WORKER_MAGIC_1: u64 = 0x6e697863; // "nixc"
pub static MAGIC_HELLO: [u8; 8] = *b"cxin\0\0\0\0"; pub static WORKER_MAGIC_2: u64 = 0x6478696f; // "dxio"
// LE-encoded dxio on 64 bits. What's dxio? I have no clue. pub static STDERR_LAST: u64 = 0x616c7473; // "alts"
pub static MAGIC_HELLO_RESPONSE: [u8; 8] = *b"oixd\0\0\0\0"; /// Protocol version (1.35)
// LE-encoded protocol version. pub static PROTOCOL_VERSION: u64 = 1 << 8 | 35;
pub static PROTOCOL_VERSION: [u8; 8] = [0x23, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
pub static STDERR_LAST: u64 = 0x616c7473;
/// Max length of a Nix setting name/value. In bytes. /// Max length of a Nix setting name/value. In bytes.
/// ///

View file

@ -124,20 +124,17 @@ async fn perform_init_handshake<'a, R: 'a>(
where where
&'a mut R: AsyncReadExt + AsyncWriteExt + Unpin + std::fmt::Debug, &'a mut R: AsyncReadExt + AsyncWriteExt + Unpin + std::fmt::Debug,
{ {
let mut magic_hello = vec![0; 8]; let worker_magic_1 = primitive::read_u64(&mut conn).await?;
conn.read_exact(&mut magic_hello).await?;
debug!("Hello read"); debug!("Hello read");
if magic_hello != worker_protocol::MAGIC_HELLO { if worker_magic_1 != worker_protocol::WORKER_MAGIC_1 {
Err(anyhow!( Err(anyhow!(
"Invalid client hello received: {:?}, expected {:?}", "Invalid client hello received: {:?}, expected {:?}",
magic_hello, worker_magic_1,
worker_protocol::MAGIC_HELLO worker_protocol::WORKER_MAGIC_1
)) ))
} else { } else {
conn.write_all(&worker_protocol::MAGIC_HELLO_RESPONSE[..]) primitive::write_u64(&mut conn, worker_protocol::WORKER_MAGIC_2).await?;
.await?; primitive::write_u64(&mut conn, worker_protocol::PROTOCOL_VERSION).await?;
conn.write_all(&worker_protocol::PROTOCOL_VERSION[..])
.await?;
conn.flush().await?; conn.flush().await?;
debug!("Hello responded"); debug!("Hello responded");
let client_version = primitive::read_u64(&mut conn).await?; let client_version = primitive::read_u64(&mut conn).await?;
@ -198,12 +195,12 @@ mod integration_tests {
#[tokio::test] #[tokio::test]
async fn test_init_handshake() { async fn test_init_handshake() {
let mut test_conn = tokio_test::io::Builder::new() let mut test_conn = tokio_test::io::Builder::new()
.read(&worker_protocol::MAGIC_HELLO) .read(&worker_protocol::WORKER_MAGIC_1.to_le_bytes())
.write(&worker_protocol::MAGIC_HELLO_RESPONSE) .write(&worker_protocol::WORKER_MAGIC_2.to_le_bytes())
.write(&worker_protocol::PROTOCOL_VERSION) .write(&worker_protocol::PROTOCOL_VERSION.to_le_bytes())
// Let's say the client is in sync with the daemon // Let's say the client is in sync with the daemon
// protocol-wise // protocol-wise
.read(&worker_protocol::PROTOCOL_VERSION) .read(&worker_protocol::PROTOCOL_VERSION.to_le_bytes())
// cpu affinity // cpu affinity
.read(&vec![0; 8]) .read(&vec![0; 8])
// reservespace // reservespace