Skip to content

Commit 6717579

Browse files
Add blake3 support
1 parent 3390593 commit 6717579

File tree

4 files changed

+148
-2
lines changed

4 files changed

+148
-2
lines changed

cache/disk/disk_test.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,35 @@ func TestCacheExistingFiles(t *testing.T) {
416416
"raw/733e21b37cef883579a88183eed0d00cdeea0b59e1bcd77db6957f881c3a6b54",
417417
"raw.v2/73/733e21b37cef883579a88183eed0d00cdeea0b59e1bcd77db6957f881c3a6b54-123456789",
418418
},
419+
420+
{
421+
pb.DigestFunction_BLAKE3,
422+
"hej",
423+
"70514ff271364b5a63dc74edd2860b0bc54bdc3cc6b95bf4ea95554fdd08ba16",
424+
"cas/blake3/70514ff271364b5a63dc74edd2860b0bc54bdc3cc6b95bf4ea95554fdd08ba16",
425+
"cas.v2/blake3/79/70514ff271364b5a63dc74edd2860b0bc54bdc3cc6b95bf4ea95554fdd08ba16-3-123456789",
426+
},
427+
{
428+
pb.DigestFunction_BLAKE3,
429+
"världen",
430+
"a01628acbe112a59c4312ba1bcd8628170fe90284a84848da598ffdf9ca04d17",
431+
"cas/blake3/a01628acbe112a59c4312ba1bcd8628170fe90284a84848da598ffdf9ca04d17",
432+
"cas.v2/blake3/a0/a01628acbe112a59c4312ba1bcd8628170fe90284a84848da598ffdf9ca04d17-8-123456789",
433+
},
434+
{
435+
pb.DigestFunction_BLAKE3,
436+
"foo",
437+
"04e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9",
438+
"ac/blake3/04e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9",
439+
"ac.v2/blake3/04/04e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9-123456789",
440+
},
441+
{
442+
pb.DigestFunction_BLAKE3,
443+
"bar",
444+
"f2e897eed7d206cd855d441598fa521abc75aa96953e97c030c9612c30c1293d",
445+
"raw/blake3/f2e897eed7d206cd855d441598fa521abc75aa96953e97c030c9612c30c1293d",
446+
"raw.v2/blake3/f2/f2e897eed7d206cd855d441598fa521abc75aa96953e97c030c9612c30c1293d-123456789",
447+
},
419448
}
420449

421450
var err error
@@ -468,8 +497,8 @@ func TestCacheExistingFiles(t *testing.T) {
468497
origOnEvict(key, value)
469498
}
470499

471-
if testCache.lru.Len() != 4 {
472-
t.Fatal("expected four items in the cache, found", testCache.lru.Len())
500+
if testCache.lru.Len() != len(items) {
501+
t.Fatalf("expected %d items in the cache, found %d", len(items), testCache.lru.Len())
473502
}
474503

475504
// Adding new blobs should eventually evict the oldest (items[0]).

cache/hashing/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
33
go_library(
44
name = "go_default_library",
55
srcs = [
6+
"blake3.go",
67
"hashing.go",
78
"sha256.go",
89
],

cache/hashing/blake3.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package hashing
2+
3+
import (
4+
"encoding/hex"
5+
"errors"
6+
"fmt"
7+
"hash"
8+
"regexp"
9+
10+
pb "github.com/buchgr/bazel-remote/v2/genproto/build/bazel/remote/execution/v2"
11+
"lukechampine.com/blake3"
12+
)
13+
14+
func init() {
15+
hasher := &b3Hasher{}
16+
register(hasher)
17+
}
18+
19+
var b3Regex = regexp.MustCompile("^[a-f0-9]{64}$")
20+
21+
type b3Hasher struct{}
22+
23+
func (d *b3Hasher) New() hash.Hash {
24+
return blake3.New(d.Size(), nil)
25+
}
26+
27+
func (d *b3Hasher) Hash(data []byte) string {
28+
sum := blake3.Sum256(data)
29+
return hex.EncodeToString(sum[:])
30+
}
31+
32+
func (d *b3Hasher) DigestFunction() pb.DigestFunction_Value {
33+
return pb.DigestFunction_BLAKE3
34+
}
35+
36+
func (d *b3Hasher) Dir() string {
37+
return "blake3"
38+
}
39+
40+
func (d *b3Hasher) Empty() string {
41+
return "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262"
42+
}
43+
44+
func (d *b3Hasher) Size() int {
45+
return 32
46+
}
47+
48+
func (d *b3Hasher) Validate(value string) error {
49+
if d.Size()*2 != len(value) {
50+
return fmt.Errorf("Invalid blake3 hash length %d: expected %d", len(value), d.Size())
51+
}
52+
if !b3Regex.MatchString(value) {
53+
return errors.New("Malformed blake3 hash " + value)
54+
}
55+
return nil
56+
}
57+
58+
func (d *b3Hasher) ValidateDigest(hash string, size int64) error {
59+
if size == int64(0) {
60+
if hash == d.Empty() {
61+
return nil
62+
}
63+
return fmt.Errorf("Invalid zero-length %s hash", d.DigestFunction())
64+
}
65+
return d.Validate(hash)
66+
}

utils/resourcename/resourcename_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,24 @@ func TestParseReadResource(t *testing.T) {
183183
resourceName: "pretenduuid/compressed-blobs/IDENTITY/0123456789012345678901234567890123456789012345678901234567890123/42",
184184
expectError: true,
185185
},
186+
187+
// Contains digest function
188+
{
189+
"blobs/blake3/0123456789012345678901234567890123456789012345678901234567890123/42",
190+
"0123456789012345678901234567890123456789012345678901234567890123",
191+
42,
192+
casblob.Identity,
193+
pb.DigestFunction_BLAKE3,
194+
false,
195+
},
196+
{
197+
"compressed-blobs/zstd/blake3/0123456789012345678901234567890123456789012345678901234567890123/42",
198+
"0123456789012345678901234567890123456789012345678901234567890123",
199+
42,
200+
casblob.Zstandard,
201+
pb.DigestFunction_BLAKE3,
202+
false,
203+
},
186204
}
187205

188206
for _, tc := range tcs {
@@ -395,6 +413,38 @@ func TestParseWriteResource(t *testing.T) {
395413
},
396414

397415
// Contains digest function
416+
{
417+
"uploads/pretenduuid/blobs/blake3/0123456789012345678901234567890123456789012345678901234567890123/42",
418+
"0123456789012345678901234567890123456789012345678901234567890123",
419+
42,
420+
casblob.Identity,
421+
pb.DigestFunction_BLAKE3,
422+
false,
423+
},
424+
{
425+
"uploads/pretenduuid/compressed-blobs/zstd/blake3/0123456789012345678901234567890123456789012345678901234567890123/42",
426+
"0123456789012345678901234567890123456789012345678901234567890123",
427+
42,
428+
casblob.Zstandard,
429+
pb.DigestFunction_BLAKE3,
430+
false,
431+
},
432+
{
433+
"uploads/pretenduuid/blobs/blake3/0123456789012345678901234567890123456789012345678901234567890123/42/metadata",
434+
"0123456789012345678901234567890123456789012345678901234567890123",
435+
42,
436+
casblob.Identity,
437+
pb.DigestFunction_BLAKE3,
438+
false,
439+
},
440+
{
441+
"uploads/pretenduuid/compressed-blobs/zstd/blake3/0123456789012345678901234567890123456789012345678901234567890123/42/metadata",
442+
"0123456789012345678901234567890123456789012345678901234567890123",
443+
42,
444+
casblob.Zstandard,
445+
pb.DigestFunction_BLAKE3,
446+
false,
447+
},
398448
{
399449
resourceName: "uploads/pretenduuid/blobs/foo/0123456789012345678901234567890123456789012345678901234567890123/42",
400450
expectError: true,

0 commit comments

Comments
 (0)