Skip to content

Commit 18a2f0f

Browse files
author
Ubuntu
committed
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 2e9f7e8 commit 18a2f0f

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,
@@ -286,11 +280,13 @@ pub fn build(b: *Build) !void {
286280
const gh_table = b.createModule(.{ .root_source_file = generateTable(b) });
287281

288282
const sqlite_mod = genSqlite(b, config.target, config.optimize);
283+
const bzip2_mod = genBzip2(b, config.target, config.optimize);
289284

290285
// zig fmt: off
291286
const imports: []const Build.Module.Import = &.{
292287
.{ .name = "base58", .module = base58_mod },
293288
.{ .name = "build-options", .module = build_options.createModule() },
289+
.{ .name = "bzip2", .module = bzip2_mod },
294290
.{ .name = "httpz", .module = httpz_mod },
295291
.{ .name = "lsquic", .module = lsquic_mod },
296292
.{ .name = "poseidon", .module = poseidon_mod },
@@ -321,7 +317,6 @@ pub fn build(b: *Build) !void {
321317
.optimize = config.optimize,
322318
.imports = imports,
323319
});
324-
sig_mod.linkLibrary(bzip2_lib);
325320

326321
switch (config.ledger_db) {
327322
.rocksdb => sig_mod.addImport("rocksdb", rocksdb_mod),
@@ -343,7 +338,6 @@ pub fn build(b: *Build) !void {
343338
});
344339
sig_exe.root_module.addObject(memcpy);
345340
sig_exe.root_module.addImport("cli", cli_mod);
346-
sig_exe.linkLibrary(bzip2_lib);
347341

348342
// make sure pyroscope's got enough info to profile
349343
sig_exe.build_id = .fast;
@@ -368,7 +362,6 @@ pub fn build(b: *Build) !void {
368362
});
369363
unit_tests_exe.root_module.addObject(memcpy);
370364
unit_tests_exe.root_module.addImport("cli", cli_mod);
371-
unit_tests_exe.linkLibrary(bzip2_lib);
372365
switch (config.ledger_db) {
373366
.rocksdb => unit_tests_exe.root_module.addImport("rocksdb", rocksdb_mod),
374367
.hashmap => {},
@@ -548,6 +541,47 @@ fn genSqlite(
548541
return mod;
549542
}
550543

544+
fn genBzip2(
545+
b: *Build,
546+
target: std.Build.ResolvedTarget,
547+
optimize: std.builtin.OptimizeMode,
548+
) *Build.Module {
549+
const dep = b.dependency("bzip2", .{});
550+
551+
const lib = b.addLibrary(.{
552+
.name = "bz",
553+
.linkage = .static,
554+
.root_module = b.createModule(.{
555+
.target = target,
556+
.optimize = optimize,
557+
}),
558+
});
559+
lib.addCSourceFiles(.{
560+
.root = dep.path("."),
561+
.files = &.{
562+
"bzlib.c",
563+
"blocksort.c",
564+
"compress.c",
565+
"crctable.c",
566+
"decompress.c",
567+
"huffman.c",
568+
"randtable.c",
569+
},
570+
});
571+
lib.linkLibC();
572+
573+
const translate_c = b.addTranslateC(.{
574+
.root_source_file = dep.path("bzlib.h"),
575+
.target = target,
576+
.optimize = optimize,
577+
.link_libc = true,
578+
});
579+
const mod = translate_c.createModule();
580+
mod.linkLibrary(lib);
581+
582+
return mod;
583+
}
584+
551585
/// Reference/inspiration: https://kristoff.it/blog/improving-your-zls-experience/
552586
fn disableEmitBin(b: *Build) void {
553587
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
@@ -103,7 +103,7 @@ pub fn main() !void {
103103
current_config.metrics_port = cmd.metrics_port;
104104
current_config.log_file = cmd.log_file;
105105
current_config.tee_logs = cmd.tee_logs;
106-
current_config.validator_dir = cmd.validator_dir;
106+
current_config.validator_dir = try std.fs.realpathAlloc(gpa, cmd.validator_dir);
107107

108108
// If no subcommand was provided, print a friendly header and help information.
109109
const subcmd = cmd.subcmd orelse {
@@ -1219,7 +1219,7 @@ fn ensureGenesis(
12191219
&.{ cfg.validator_dir, "genesis.bin" },
12201220
);
12211221
errdefer allocator.free(existing_path);
1222-
if (!std.meta.isError(std.fs.accessAbsolute(existing_path, .{}))) {
1222+
if (!std.meta.isError(std.fs.cwd().access(existing_path, .{}))) {
12231223
logger.info().logf("Using existing genesis file: {s}", .{existing_path});
12241224
return existing_path;
12251225
}

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)