refactor(tvix/castore): use Directory struct separate from proto one

This uses our own data type to deal with Directories in the castore model.

It makes some undesired states unrepresentable, removing the need for conversions and checking in various places:

 - In the protobuf, blake3 digests could have a wrong length, as proto doesn't know fixed-size fields. We now use `B3Digest`, which makes cloning cheaper, and removes the need to do size-checking everywhere.
 - In the protobuf, we had three different lists for `files`, `symlinks` and `directories`. This was mostly a protobuf size optimization, but made interacting with them a bit awkward. This has now been replaced with a list of enums, and convenience iterators to get various nodes, and add new ones.

Change-Id: I7b92691bb06d77ff3f58a5ccea94a22c16f84f04
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12057
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
Yureka 2024-07-29 14:34:50 +02:00 committed by yuka
parent 5d3f3158d6
commit 3ca0b53840
53 changed files with 1429 additions and 1377 deletions

View file

@ -9,8 +9,9 @@ use std::io;
use std::sync::Arc;
use tokio::io::sink;
use tvix_castore::blobservice::BlobService;
use tvix_castore::directoryservice::DirectoryService;
use tvix_castore::proto as castorepb;
use tvix_castore::directoryservice::{
DirectoryNode, DirectoryService, FileNode, Node, SymlinkNode,
};
#[rstest]
#[tokio::test]
@ -22,10 +23,9 @@ async fn single_symlink(
write_nar(
&mut buf,
&castorepb::node::Node::Symlink(castorepb::SymlinkNode {
name: "doesntmatter".into(),
target: "/nix/store/somewhereelse".into(),
}),
&Node::Symlink(
SymlinkNode::new("doesntmatter".into(), "/nix/store/somewhereelse".into()).unwrap(),
),
// don't put anything in the stores, as we don't actually do any requests.
blob_service,
directory_service,
@ -45,12 +45,15 @@ async fn single_file_missing_blob(
) {
let e = write_nar(
sink(),
&castorepb::node::Node::File(castorepb::FileNode {
name: "doesntmatter".into(),
digest: HELLOWORLD_BLOB_DIGEST.clone().into(),
size: HELLOWORLD_BLOB_CONTENTS.len() as u64,
executable: false,
}),
&Node::File(
FileNode::new(
"doesntmatter".into(),
HELLOWORLD_BLOB_DIGEST.clone(),
HELLOWORLD_BLOB_CONTENTS.len() as u64,
false,
)
.unwrap(),
),
// the blobservice is empty intentionally, to provoke the error.
blob_service,
directory_service,
@ -90,12 +93,15 @@ async fn single_file_wrong_blob_size(
// Test with a root FileNode of a too big size
let e = write_nar(
sink(),
&castorepb::node::Node::File(castorepb::FileNode {
name: "doesntmatter".into(),
digest: HELLOWORLD_BLOB_DIGEST.clone().into(),
size: 42, // <- note the wrong size here!
executable: false,
}),
&Node::File(
FileNode::new(
"doesntmatter".into(),
HELLOWORLD_BLOB_DIGEST.clone(),
42, // <- note the wrong size here!
false,
)
.unwrap(),
),
blob_service.clone(),
directory_service.clone(),
)
@ -112,12 +118,15 @@ async fn single_file_wrong_blob_size(
// Test with a root FileNode of a too small size
let e = write_nar(
sink(),
&castorepb::node::Node::File(castorepb::FileNode {
name: "doesntmatter".into(),
digest: HELLOWORLD_BLOB_DIGEST.clone().into(),
size: 2, // <- note the wrong size here!
executable: false,
}),
&Node::File(
FileNode::new(
"doesntmatter".into(),
HELLOWORLD_BLOB_DIGEST.clone(),
2, // <- note the wrong size here!
false,
)
.unwrap(),
),
blob_service,
directory_service,
)
@ -153,12 +162,15 @@ async fn single_file(
write_nar(
&mut buf,
&castorepb::node::Node::File(castorepb::FileNode {
name: "doesntmatter".into(),
digest: HELLOWORLD_BLOB_DIGEST.clone().into(),
size: HELLOWORLD_BLOB_CONTENTS.len() as u64,
executable: false,
}),
&Node::File(
FileNode::new(
"doesntmatter".into(),
HELLOWORLD_BLOB_DIGEST.clone(),
HELLOWORLD_BLOB_CONTENTS.len() as u64,
false,
)
.unwrap(),
),
blob_service,
directory_service,
)
@ -196,11 +208,14 @@ async fn test_complicated(
write_nar(
&mut buf,
&castorepb::node::Node::Directory(castorepb::DirectoryNode {
name: "doesntmatter".into(),
digest: DIRECTORY_COMPLICATED.digest().into(),
size: DIRECTORY_COMPLICATED.size(),
}),
&Node::Directory(
DirectoryNode::new(
"doesntmatter".into(),
DIRECTORY_COMPLICATED.digest(),
DIRECTORY_COMPLICATED.size(),
)
.unwrap(),
),
blob_service.clone(),
directory_service.clone(),
)
@ -211,11 +226,14 @@ async fn test_complicated(
// ensure calculate_nar does return the correct sha256 digest and sum.
let (nar_size, nar_digest) = calculate_size_and_sha256(
&castorepb::node::Node::Directory(castorepb::DirectoryNode {
name: "doesntmatter".into(),
digest: DIRECTORY_COMPLICATED.digest().into(),
size: DIRECTORY_COMPLICATED.size(),
}),
&Node::Directory(
DirectoryNode::new(
"doesntmatter".into(),
DIRECTORY_COMPLICATED.digest(),
DIRECTORY_COMPLICATED.size(),
)
.unwrap(),
),
blob_service,
directory_service,
)