Skip to content

Commit

Permalink
Update base58 dependency and related code
Browse files Browse the repository at this point in the history
  • Loading branch information
InKryption committed Feb 3, 2025
1 parent a14a5a5 commit 2433828
Show file tree
Hide file tree
Showing 29 changed files with 326 additions and 372 deletions.
34 changes: 17 additions & 17 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ pub fn build(b: *Build) void {
// Dependencies
const dep_opts = .{ .target = target, .optimize = optimize };

const base58_dep = b.dependency("base58-zig", dep_opts);
const base58_module = base58_dep.module("base58-zig");
const base58_dep = b.dependency("base58", dep_opts);
const base58_mod = base58_dep.module("base58");

const zig_network_dep = b.dependency("zig-network", dep_opts);
const zig_network_module = zig_network_dep.module("network");
const zig_network_mod = zig_network_dep.module("network");

const zig_cli_dep = b.dependency("zig-cli", dep_opts);
const zig_cli_module = zig_cli_dep.module("zig-cli");
const zig_cli_mod = zig_cli_dep.module("zig-cli");

const zstd_dep = b.dependency("zstd", dep_opts);
const zstd_mod = zstd_dep.module("zstd");
Expand All @@ -72,9 +72,9 @@ pub fn build(b: *Build) void {

sig_mod.addOptions("build-options", build_options);

sig_mod.addImport("zig-network", zig_network_module);
sig_mod.addImport("base58-zig", base58_module);
sig_mod.addImport("zig-cli", zig_cli_module);
sig_mod.addImport("zig-network", zig_network_mod);
sig_mod.addImport("base58", base58_mod);
sig_mod.addImport("zig-cli", zig_cli_mod);
sig_mod.addImport("zstd", zstd_mod);
switch (blockstore_db) {
.rocksdb => sig_mod.addImport("rocksdb", rocksdb_mod),
Expand All @@ -100,9 +100,9 @@ pub fn build(b: *Build) void {
sig_exe.linkLibC();
sig_exe.root_module.addOptions("build-options", build_options);

sig_exe.root_module.addImport("base58-zig", base58_module);
sig_exe.root_module.addImport("zig-cli", zig_cli_module);
sig_exe.root_module.addImport("zig-network", zig_network_module);
sig_exe.root_module.addImport("base58", base58_mod);
sig_exe.root_module.addImport("zig-cli", zig_cli_mod);
sig_exe.root_module.addImport("zig-network", zig_network_mod);
sig_exe.root_module.addImport("zstd", zstd_mod);
sig_exe.root_module.addImport("lsquic", lsquic_mod);
sig_exe.root_module.addImport("ssl", ssl_mod);
Expand Down Expand Up @@ -138,8 +138,8 @@ pub fn build(b: *Build) void {
unit_tests_exe.linkLibC();
unit_tests_exe.root_module.addOptions("build-options", build_options);

unit_tests_exe.root_module.addImport("base58-zig", base58_module);
unit_tests_exe.root_module.addImport("zig-network", zig_network_module);
unit_tests_exe.root_module.addImport("base58", base58_mod);
unit_tests_exe.root_module.addImport("zig-network", zig_network_mod);
unit_tests_exe.root_module.addImport("zstd", zstd_mod);
switch (blockstore_db) {
.rocksdb => unit_tests_exe.root_module.addImport("rocksdb", rocksdb_mod),
Expand Down Expand Up @@ -171,8 +171,8 @@ pub fn build(b: *Build) void {
fuzz_exe.linkLibC();
fuzz_exe.root_module.addOptions("build-options", build_options);

fuzz_exe.root_module.addImport("base58-zig", base58_module);
fuzz_exe.root_module.addImport("zig-network", zig_network_module);
fuzz_exe.root_module.addImport("base58", base58_mod);
fuzz_exe.root_module.addImport("zig-network", zig_network_mod);
fuzz_exe.root_module.addImport("zstd", zstd_mod);
switch (blockstore_db) {
.rocksdb => fuzz_exe.root_module.addImport("rocksdb", rocksdb_mod),
Expand Down Expand Up @@ -205,8 +205,8 @@ pub fn build(b: *Build) void {
benchmark_exe.linkLibC();
benchmark_exe.root_module.addOptions("build-options", build_options);

benchmark_exe.root_module.addImport("base58-zig", base58_module);
benchmark_exe.root_module.addImport("zig-network", zig_network_module);
benchmark_exe.root_module.addImport("base58", base58_mod);
benchmark_exe.root_module.addImport("zig-network", zig_network_mod);
benchmark_exe.root_module.addImport("zstd", zstd_mod);
benchmark_exe.root_module.addImport("prettytable", pretty_table_mod);
switch (blockstore_db) {
Expand Down Expand Up @@ -238,7 +238,7 @@ pub fn build(b: *Build) void {
install_step.dependOn(&geyser_reader_exe.step);

geyser_reader_exe.root_module.addImport("sig", sig_mod);
geyser_reader_exe.root_module.addImport("zig-cli", zig_cli_module);
geyser_reader_exe.root_module.addImport("zig-cli", zig_cli_mod);

if (!no_bin) {
const geyser_reader_install = b.addInstallArtifact(geyser_reader_exe, .{});
Expand Down
8 changes: 4 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
"src",
},
.dependencies = .{
.@"base58-zig" = .{
.url = "https://github.com/Syndica/base58-zig/archive/e1001fbe8b41eed36d81e37931ada66b784e51dc.tar.gz",
.hash = "12206e5050a03cd9dcb896781de0cf541081488006532675371653f61d00c1f27433",
},
.@"zig-network" = .{
.url = "https://github.com/ikskuh/zig-network/archive/d4506cd1950de8a6e6d3f28aed68a0b4f64e0b44.tar.gz",
.hash = "122080b2675fa7fdd7410af5f62942dec99e0ea766827c8c6506e6672b575fb0a50e",
Expand Down Expand Up @@ -39,5 +35,9 @@
.url = "https://github.com/dying-will-bullet/prettytable-zig/archive/46b6ad9b5970def35fa43c9613cd244f28862fa9.tar.gz",
.hash = "122098d444c9c7112c66481e7655bb5389829c67e04b280a029200545e1971187443",
},
.base58 = .{
.url = "git+https://github.com/Syndica/base58-zig#e6337e7eb3dc7ca77cad4c3b2fe4a34357bd50ea",
.hash = "122092353c0b494c2a4968c10a527dc90d6e89eaac48637fda1a0af3efd948bf4ee3",
},
},
}
22 changes: 10 additions & 12 deletions src/accountsdb/snapshots.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const std = @import("std");
const zstd = @import("zstd");
const sig = @import("../sig.zig");
const base58 = @import("base58-zig");
const base58 = @import("base58");

const bincode = sig.bincode;

Expand Down Expand Up @@ -349,7 +349,7 @@ test "deserialize VoteState.node_pubkey" {
90, 174, 158, 6, 199, 179, 134, 194, 112, 248, 166, 232, 144, 253, 128, 249, 67, 118,
} ++ .{0} ** 1586 ++ .{ 31, 0, 0, 0, 0, 0, 0, 0, 1 } ++ .{0} ** 24;
const vote_state = try bincode.readFromSlice(undefined, VoteState, &bytes, .{});
const expected_pubkey = try Pubkey.fromString("55abJrqFnjm7ZRB1noVdh7BzBe3bBSMFT3pt16mw6Vad");
const expected_pubkey = try Pubkey.parseBase58String("55abJrqFnjm7ZRB1noVdh7BzBe3bBSMFT3pt16mw6Vad");
try std.testing.expect(expected_pubkey.equals(&vote_state.node_pubkey));
}

Expand Down Expand Up @@ -2219,7 +2219,7 @@ pub const FullSnapshotFileInfo = struct {

pub const SnapshotArchiveNameStr = SnapshotArchiveNameFmtSpec.BoundedArrayValue(.{
.slot = std.math.maxInt(Slot),
.hash = sig.utils.fmt.boundedString(&(Hash{ .data = .{255} ** 32 }).base58String()),
.hash = "1" ** Hash.BASE58_MAX_SIZE,
});

pub fn snapshotArchiveName(self: FullSnapshotFileInfo) SnapshotArchiveNameStr {
Expand Down Expand Up @@ -2301,11 +2301,10 @@ pub const FullSnapshotFileInfo = struct {
return error.MissingHash;
}

const str_max_len = Hash.base58_max_encoded_size;
const end_max = @max(filename.len, start + str_max_len + 1);
const str_max_len = Hash.BASE58_MAX_SIZE;
const end_max = @min(filename.len, start + str_max_len + 1);
const filename_truncated = filename[0..end_max];
// TODO: accessing it this way is dirty, the base58 API should be improved
const alphabet = &base58.Alphabet.DEFAULT.encode;
const alphabet = std.mem.asBytes(&base58.Table.BITCOIN.alphabet);
const end = std.mem.indexOfNonePos(u8, filename_truncated, start + 1, alphabet) orelse
filename_truncated.len;

Expand Down Expand Up @@ -2347,7 +2346,7 @@ pub const IncrementalSnapshotFileInfo = struct {
pub const SnapshotArchiveNameStr = SnapshotArchiveNameFmtSpec.BoundedArrayValue(.{
.base_slot = std.math.maxInt(Slot),
.slot = std.math.maxInt(Slot),
.hash = sig.utils.fmt.boundedString(&(Hash{ .data = .{255} ** 32 }).base58String()),
.hash = "1" ** Hash.BASE58_MAX_SIZE,
});

pub fn snapshotArchiveName(self: IncrementalSnapshotFileInfo) SnapshotArchiveNameStr {
Expand Down Expand Up @@ -2458,11 +2457,10 @@ pub const IncrementalSnapshotFileInfo = struct {
return error.MissingHash;
}

const str_max_len = Hash.base58_max_encoded_size;
const end_max = @max(filename.len, start + str_max_len + 1);
const str_max_len = Hash.BASE58_MAX_SIZE;
const end_max = @min(filename.len, start + str_max_len + 1);
const filename_truncated = filename[0..end_max];
// TODO: accessing it this way is dirty, the base58 API should be improved
const alphabet = &base58.Alphabet.DEFAULT.encode;
const alphabet = std.mem.asBytes(&base58.Table.BITCOIN.alphabet);
const end = std.mem.indexOfNonePos(u8, filename_truncated, start + 1, alphabet) orelse
filename_truncated.len;

Expand Down
16 changes: 8 additions & 8 deletions src/accountsdb/sysvars.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ pub const SlotCheckResult = enum { Future, TooOld, Found, NotFound };
///
/// Analogous to [SysvarCache](https://github.com/anza-xyz/agave/blob/ebd063eb79c6e2f14da660ccfc90f1d4c0b7db1f/program-runtime/src/sysvar_cache.rs#L28)
pub const IDS = struct {
pub const clock = Pubkey.fromString("SysvarC1ock11111111111111111111111111111111") catch unreachable;
pub const epoch_schedule = Pubkey.fromString("SysvarEpochSchedu1e111111111111111111111111") catch unreachable;
pub const epoch_rewards = Pubkey.fromString("SysvarEpochRewards1111111111111111111111111") catch unreachable;
pub const rent = Pubkey.fromString("SysvarRent111111111111111111111111111111111") catch unreachable;
pub const slot_hashes = Pubkey.fromString("SysvarS1otHashes111111111111111111111111111") catch unreachable;
pub const slot_history = Pubkey.fromString("SysvarS1otHistory11111111111111111111111111") catch unreachable;
pub const stake_history = Pubkey.fromString("SysvarStakeHistory1111111111111111111111111") catch unreachable;
pub const last_restart_slot = Pubkey.fromString("SysvarLastRestartS1ot1111111111111111111111") catch unreachable;
pub const clock = Pubkey.parseBase58String("SysvarC1ock11111111111111111111111111111111") catch unreachable;
pub const epoch_schedule = Pubkey.parseBase58String("SysvarEpochSchedu1e111111111111111111111111") catch unreachable;
pub const epoch_rewards = Pubkey.parseBase58String("SysvarEpochRewards1111111111111111111111111") catch unreachable;
pub const rent = Pubkey.parseBase58String("SysvarRent111111111111111111111111111111111") catch unreachable;
pub const slot_hashes = Pubkey.parseBase58String("SysvarS1otHashes111111111111111111111111111") catch unreachable;
pub const slot_history = Pubkey.parseBase58String("SysvarS1otHistory11111111111111111111111111") catch unreachable;
pub const stake_history = Pubkey.parseBase58String("SysvarStakeHistory1111111111111111111111111") catch unreachable;
pub const last_restart_slot = Pubkey.parseBase58String("SysvarLastRestartS1ot1111111111111111111111") catch unreachable;
};

/// Analogous to [Clock](https://github.com/anza-xyz/agave/blob/fc2a8794be2526e9fd6cdbc9b304c055b2d9cc57/sdk/program/src/clock.rs#L180)
Expand Down
2 changes: 1 addition & 1 deletion src/cmd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,7 @@ fn getTrustedValidators(allocator: std.mem.Allocator) !?std.ArrayList(Pubkey) {
);
for (current_config.gossip.trusted_validators) |trusted_validator_str| {
trusted_validators.?.appendAssumeCapacity(
try Pubkey.fromString(trusted_validator_str),
try Pubkey.parseBase58String(trusted_validator_str),
);
}
}
Expand Down
67 changes: 33 additions & 34 deletions src/core/hash.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const std = @import("std");
const sig = @import("../sig.zig");
const base58 = @import("base58");

const BASE58_ENDEC = base58.Table.BITCOIN;
const Sha256 = std.crypto.hash.sha2.Sha256;
const Slot = sig.core.time.Slot;
const Allocator = std.mem.Allocator;

pub const SlotAndHash = struct {
slot: Slot,
Expand All @@ -27,73 +28,71 @@ pub const SlotAndHash = struct {
};

pub const Hash = extern struct {
data: [size]u8,
data: [SIZE]u8,

pub const size = 32;
pub const SIZE = 32;

pub const ZEROES: Hash = .{ .data = .{0} ** size };

const base58 = sig.crypto.base58.Base58Sized(size);

pub fn fromSizedSlice(data: *const [size]u8) Hash {
var hash: Hash = undefined;
@memcpy(&hash.data, data);
return hash;
}
pub const ZEROES: Hash = .{ .data = .{0} ** SIZE };

pub fn generateSha256Hash(bytes: []const u8) Hash {
var data: [size]u8 = undefined;
var data: [SIZE]u8 = undefined;
Sha256.hash(bytes, &data, .{});
return .{ .data = data };
}

pub fn extendAndHash(id: Hash, val: []const u8) Hash {
pub fn extendAndHash(self: Hash, val: []const u8) Hash {
var hasher = Sha256.init(.{});
hasher.update(&id.data);
hasher.update(&self.data);
hasher.update(val);
return .{ .data = hasher.finalResult() };
}

pub fn eql(self: Hash, other: Hash) bool {
const xx: @Vector(size, u8) = self.data;
const yy: @Vector(size, u8) = other.data;
const xx: @Vector(SIZE, u8) = self.data;
const yy: @Vector(SIZE, u8) = other.data;
return @reduce(.And, xx == yy);
}

pub fn order(a: *const Hash, b: *const Hash) std.math.Order {
for (a.data, b.data) |a_byte, b_byte| {
if (a_byte > b_byte) return .gt;
if (a_byte < b_byte) return .lt;
}
return .eq;
pub fn order(self: *const Hash, other: *const Hash) std.math.Order {
return for (self.data, other.data) |a_byte, b_byte| {
if (a_byte > b_byte) break .gt;
if (a_byte < b_byte) break .lt;
} else .eq;
}

pub fn parseBase58String(str: []const u8) error{InvalidHash}!Hash {
return .{ .data = base58.decode(str) catch return error.InvalidHash };
if (str.len > BASE58_MAX_SIZE) return error.InvalidHash;
var encoded: std.BoundedArray(u8, BASE58_MAX_SIZE) = .{};
encoded.appendSliceAssumeCapacity(str);

if (@inComptime()) @setEvalBranchQuota(str.len * str.len * str.len);
const decoded = BASE58_ENDEC.decodeBounded(BASE58_MAX_SIZE, encoded) catch {
return error.InvalidHash;
};

if (decoded.len != SIZE) return error.InvalidHash;
return .{ .data = decoded.constSlice()[0..SIZE].* };
}

pub const base58_max_encoded_size = base58.max_encoded_size;
pub const Base58String = std.BoundedArray(u8, base58_max_encoded_size);
pub const BASE58_MAX_SIZE = base58.encodedMaxSize(SIZE);
pub const Base58String = std.BoundedArray(u8, BASE58_MAX_SIZE);
pub fn base58String(self: Hash) Base58String {
return base58.encode(self.data);
return BASE58_ENDEC.encodeArray(SIZE, self.data);
}

pub fn format(
self: Hash,
comptime _: []const u8,
_: std.fmt.FormatOptions,
writer: anytype,
) !void {
return base58.format(self.data, writer);
}

pub fn base58EncodeAlloc(self: Hash, allocator: Allocator) Allocator.Error![]const u8 {
return base58.encodeAlloc(self.data, allocator);
) @TypeOf(writer).Error!void {
const str = self.base58String();
return writer.writeAll(str.constSlice());
}

/// Intended to be used in tests.
pub fn initRandom(random: std.Random) Hash {
var data: [size]u8 = undefined;
var data: [SIZE]u8 = undefined;
random.bytes(&data);
return .{ .data = data };
}
Expand Down
31 changes: 17 additions & 14 deletions src/core/leader_schedule.zig
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub const LeaderSchedule = struct {

var rpc_leader_iter = leader_to_slots.iterator();
while (rpc_leader_iter.next()) |entry| {
const key = try Pubkey.fromString(entry.key_ptr.*);
const key = try Pubkey.parseBase58String(entry.key_ptr.*);
for (entry.value_ptr.*) |slot| {
leaders[leaders_index] = .{ .slot = slot, .key = key };
leaders_index += 1;
Expand Down Expand Up @@ -251,7 +251,7 @@ pub const LeaderSchedule = struct {
start_slot = slot;
}
const node_str = nextNonEmpty(&word_iter) orelse return error.MissingPubkey;
try slot_leaders.append(try Pubkey.fromString(node_str));
try slot_leaders.append(try Pubkey.parseBase58String(node_str));
}

return .{
Expand Down Expand Up @@ -287,7 +287,10 @@ test "leaderSchedule calculation matches agave" {
const slot_leaders = try LeaderSchedule.fromStakedNodes(std.testing.allocator, 123, 321, &staked_nodes.unmanaged);
defer std.testing.allocator.free(slot_leaders);
for (slot_leaders, 0..) |slot_leader, i| {
try std.testing.expect((try Pubkey.fromString(generated_leader_schedule[i])).equals(&slot_leader));
try std.testing.expectEqual(
slot_leader,
try Pubkey.parseBase58String(generated_leader_schedule[i]),
);
}
}

Expand All @@ -308,17 +311,17 @@ test "parseLeaderSchedule writeLeaderSchedule happy path roundtrip" {
\\
;
const expected_nodes = [_]Pubkey{
try Pubkey.fromString("Fd7btgySsrjuo25CJCj7oE7VPMyezDhnx7pZkj2v69Nk"),
try Pubkey.fromString("Fd7btgySsrjuo25CJCj7oE7VPMyezDhnx7pZkj2v69Nk"),
try Pubkey.fromString("Fd7btgySsrjuo25CJCj7oE7VPMyezDhnx7pZkj2v69Nk"),
try Pubkey.fromString("Fd7btgySsrjuo25CJCj7oE7VPMyezDhnx7pZkj2v69Nk"),
try Pubkey.fromString("GBuP6xK2zcUHbQuUWM4gbBjom46AomsG8JzSp1bzJyn8"),
try Pubkey.fromString("GBuP6xK2zcUHbQuUWM4gbBjom46AomsG8JzSp1bzJyn8"),
try Pubkey.fromString("GBuP6xK2zcUHbQuUWM4gbBjom46AomsG8JzSp1bzJyn8"),
try Pubkey.fromString("GBuP6xK2zcUHbQuUWM4gbBjom46AomsG8JzSp1bzJyn8"),
try Pubkey.fromString("DWvDTSh3qfn88UoQTEKRV2JnLt5jtJAVoiCo3ivtMwXP"),
try Pubkey.fromString("DWvDTSh3qfn88UoQTEKRV2JnLt5jtJAVoiCo3ivtMwXP"),
try Pubkey.fromString("DWvDTSh3qfn88UoQTEKRV2JnLt5jtJAVoiCo3ivtMwXP"),
try Pubkey.parseBase58String("Fd7btgySsrjuo25CJCj7oE7VPMyezDhnx7pZkj2v69Nk"),
try Pubkey.parseBase58String("Fd7btgySsrjuo25CJCj7oE7VPMyezDhnx7pZkj2v69Nk"),
try Pubkey.parseBase58String("Fd7btgySsrjuo25CJCj7oE7VPMyezDhnx7pZkj2v69Nk"),
try Pubkey.parseBase58String("Fd7btgySsrjuo25CJCj7oE7VPMyezDhnx7pZkj2v69Nk"),
try Pubkey.parseBase58String("GBuP6xK2zcUHbQuUWM4gbBjom46AomsG8JzSp1bzJyn8"),
try Pubkey.parseBase58String("GBuP6xK2zcUHbQuUWM4gbBjom46AomsG8JzSp1bzJyn8"),
try Pubkey.parseBase58String("GBuP6xK2zcUHbQuUWM4gbBjom46AomsG8JzSp1bzJyn8"),
try Pubkey.parseBase58String("GBuP6xK2zcUHbQuUWM4gbBjom46AomsG8JzSp1bzJyn8"),
try Pubkey.parseBase58String("DWvDTSh3qfn88UoQTEKRV2JnLt5jtJAVoiCo3ivtMwXP"),
try Pubkey.parseBase58String("DWvDTSh3qfn88UoQTEKRV2JnLt5jtJAVoiCo3ivtMwXP"),
try Pubkey.parseBase58String("DWvDTSh3qfn88UoQTEKRV2JnLt5jtJAVoiCo3ivtMwXP"),
};
// const expected_start = 270864000;

Expand Down
Loading

0 comments on commit 2433828

Please sign in to comment.