Skip to content

Commit b1f83bb

Browse files
Ubuntuyewman
authored andcommitted
refactor(build): bzip2 using translate-c
- Replace @cImport with bzip2 mod via translate-c - Simplify genesis_download to use new bzip2 module import - Fix path handling in cmd.zig and config.zig for validator-dir
1 parent 252cfad commit b1f83bb

File tree

5 files changed

+59
-26
lines changed

5 files changed

+59
-26
lines changed

build.zig

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,6 @@ pub fn build(b: *Build) !void {
242242
.optimize = config.optimize,
243243
}).module("zstd");
244244

245-
const bzip2_dep = b.dependency("bzip2", .{
246-
.target = config.target,
247-
.optimize = config.optimize,
248-
});
249-
const bzip2_lib = bzip2_dep.artifact("bz");
250-
251245
const ssl_mod = lsquic_dep.builder.dependency("boringssl", .{
252246
.target = config.target,
253247
.optimize = config.optimize,
@@ -288,11 +282,13 @@ pub fn build(b: *Build) !void {
288282
const gh_table = b.createModule(.{ .root_source_file = generateTable(b) });
289283

290284
const sqlite_mod = genSqlite(b, config.target, config.optimize);
285+
const bzip2_mod = genBzip2(b, config.target, config.optimize);
291286

292287
// zig fmt: off
293288
const imports: []const Build.Module.Import = &.{
294289
.{ .name = "base58", .module = base58_mod },
295290
.{ .name = "build-options", .module = build_options.createModule() },
291+
.{ .name = "bzip2", .module = bzip2_mod },
296292
.{ .name = "httpz", .module = httpz_mod },
297293
.{ .name = "lsquic", .module = lsquic_mod },
298294
.{ .name = "poseidon", .module = poseidon_mod },
@@ -323,7 +319,6 @@ pub fn build(b: *Build) !void {
323319
.optimize = config.optimize,
324320
.imports = imports,
325321
});
326-
sig_mod.linkLibrary(bzip2_lib);
327322

328323
switch (config.ledger_db) {
329324
.rocksdb => sig_mod.addImport("rocksdb", rocksdb_mod),
@@ -345,7 +340,6 @@ pub fn build(b: *Build) !void {
345340
});
346341
sig_exe.root_module.addObject(memcpy);
347342
sig_exe.root_module.addImport("cli", cli_mod);
348-
sig_exe.linkLibrary(bzip2_lib);
349343

350344
// make sure pyroscope's got enough info to profile
351345
sig_exe.build_id = .fast;
@@ -370,7 +364,6 @@ pub fn build(b: *Build) !void {
370364
});
371365
unit_tests_exe.root_module.addObject(memcpy);
372366
unit_tests_exe.root_module.addImport("cli", cli_mod);
373-
unit_tests_exe.linkLibrary(bzip2_lib);
374367
switch (config.ledger_db) {
375368
.rocksdb => unit_tests_exe.root_module.addImport("rocksdb", rocksdb_mod),
376369
.hashmap => {},
@@ -550,6 +543,47 @@ fn genSqlite(
550543
return mod;
551544
}
552545

546+
fn genBzip2(
547+
b: *Build,
548+
target: std.Build.ResolvedTarget,
549+
optimize: std.builtin.OptimizeMode,
550+
) *Build.Module {
551+
const dep = b.dependency("bzip2", .{});
552+
553+
const lib = b.addLibrary(.{
554+
.name = "bz",
555+
.linkage = .static,
556+
.root_module = b.createModule(.{
557+
.target = target,
558+
.optimize = optimize,
559+
}),
560+
});
561+
lib.addCSourceFiles(.{
562+
.root = dep.path("."),
563+
.files = &.{
564+
"bzlib.c",
565+
"blocksort.c",
566+
"compress.c",
567+
"crctable.c",
568+
"decompress.c",
569+
"huffman.c",
570+
"randtable.c",
571+
},
572+
});
573+
lib.linkLibC();
574+
575+
const translate_c = b.addTranslateC(.{
576+
.root_source_file = dep.path("bzlib.h"),
577+
.target = target,
578+
.optimize = optimize,
579+
.link_libc = true,
580+
});
581+
const mod = translate_c.createModule();
582+
mod.linkLibrary(lib);
583+
584+
return mod;
585+
}
586+
553587
/// Reference/inspiration: https://kristoff.it/blog/improving-your-zls-experience/
554588
fn disableEmitBin(b: *Build) void {
555589
for (b.install_tls.step.dependencies.items) |*install_step_dep| {

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
.hash = "N-V-__8AAH-mpwB7g3MnqYU-ooUBF1t99RP27dZ9addtMVXD",
5555
},
5656
.bzip2 = .{
57-
.url = "git+https://github.com/allyourcodebase/bzip2?ref=1.0.8#d7668d5ee5eb55489c35632ecf0b0107c7a77569",
58-
.hash = "bzip2-1.0.8-m7CPz2QGAADdAt97rbHaz6ekIt864HgHs9rZ3fsYF5Fy",
57+
.url = "https://github.com/libarchive/bzip2/archive/refs/tags/bzip2-1.0.8.zip",
58+
.hash = "N-V-__8AANChDwAITDmkzIqB-9m_iaOXSquhghM2-AqDFcX8",
5959
},
6060
},
6161
}

src/cmd.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub fn main() !void {
104104
current_config.metrics_port = cmd.metrics_port;
105105
current_config.log_file = cmd.log_file;
106106
current_config.tee_logs = cmd.tee_logs;
107-
current_config.validator_dir = cmd.validator_dir;
107+
current_config.validator_dir = try std.fs.realpathAlloc(gpa, cmd.validator_dir);
108108

109109
// If no subcommand was provided, print a friendly header and help information.
110110
const subcmd = cmd.subcmd orelse {
@@ -1328,7 +1328,7 @@ fn ensureGenesis(
13281328
&.{ cfg.validator_dir, "genesis.bin" },
13291329
);
13301330
errdefer allocator.free(existing_path);
1331-
if (!std.meta.isError(std.fs.accessAbsolute(existing_path, .{}))) {
1331+
if (!std.meta.isError(std.fs.cwd().access(existing_path, .{}))) {
13321332
logger.info().logf("Using existing genesis file: {s}", .{existing_path});
13331333
return existing_path;
13341334
}

src/config.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub const Cmd = struct {
6161
if (std.mem.eql(u8, param_value, sig.VALIDATOR_DIR ++ default_suffix)) {
6262
return try std.fs.path.join(allocator, &.{ self.validator_dir, default_suffix });
6363
}
64-
return param_value;
64+
return try allocator.dupe(u8, param_value);
6565
}
6666
};
6767

src/core/genesis_download.zig

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
const std = @import("std");
88
const sig = @import("../sig.zig");
9-
const bzlib_c = @cImport({
10-
@cInclude("bzlib.h");
11-
});
9+
const bzip2 = @import("bzip2");
1210

1311
const Allocator = std.mem.Allocator;
1412

@@ -93,12 +91,12 @@ fn downloadGenesisArchive(
9391
/// Decompresses bz2 data using libbz2
9492
fn decompressBz2(allocator: Allocator, compressed_data: []const u8) DownloadError![]u8 {
9593
// Start with an estimate of 10x compression ratio
96-
var dest_len: c_uint = @intCast(@min(compressed_data.len * 10, 500 * 1024 * 1024));
94+
var dest_len: u32 = @intCast(@min(compressed_data.len * 10, 500 * 1024 * 1024));
9795
var decompressed = allocator.alloc(u8, dest_len) catch return error.OutOfMemory;
9896
errdefer allocator.free(decompressed);
9997

10098
while (true) {
101-
const result = bzlib_c.BZ2_bzBuffToBuffDecompress(
99+
const result = bzip2.BZ2_bzBuffToBuffDecompress(
102100
decompressed.ptr,
103101
&dest_len,
104102
@constCast(@ptrCast(compressed_data.ptr)),
@@ -108,21 +106,22 @@ fn decompressBz2(allocator: Allocator, compressed_data: []const u8) DownloadErro
108106
);
109107

110108
switch (result) {
111-
bzlib_c.BZ_OK => {
109+
bzip2.BZ_OK => {
112110
// Shrink to actual size
113111
if (dest_len < decompressed.len) {
114-
decompressed = allocator.realloc(decompressed, dest_len) catch decompressed;
112+
decompressed = allocator.realloc(decompressed, dest_len) catch
113+
return error.OutOfMemory;
115114
}
116-
return decompressed[0..dest_len];
115+
return decompressed;
117116
},
118-
bzlib_c.BZ_OUTBUFF_FULL => {
119-
// Need more space, double the buffer
120-
allocator.free(decompressed);
117+
bzip2.BZ_OUTBUFF_FULL => {
121118
dest_len = dest_len * 2;
122119
if (dest_len > 1024 * 1024 * 1024) { // 1GB limit
123120
return error.Bz2DecompressError;
124121
}
125-
decompressed = allocator.alloc(u8, dest_len) catch return error.OutOfMemory;
122+
decompressed = allocator.realloc(decompressed, dest_len) catch {
123+
return error.OutOfMemory;
124+
};
126125
},
127126
else => return error.Bz2DecompressError,
128127
}

0 commit comments

Comments
 (0)