Skip to content

Commit

Permalink
init fix for snapshot + status cache verification
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNineteen committed Jun 4, 2024
1 parent d009f73 commit 39a3d60
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
24 changes: 21 additions & 3 deletions src/accountsdb/db.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const AccountFileInfo = @import("../accountsdb/snapshots.zig").AccountFileInfo;
const AccountFile = @import("../accountsdb/accounts_file.zig").AccountFile;
const FileId = @import("../accountsdb/accounts_file.zig").FileId;
const AccountInFile = @import("../accountsdb/accounts_file.zig").AccountInFile;
const Blake3 = std.crypto.hash.Blake3;

const ThreadPool = @import("../sync/thread_pool.zig").ThreadPool;
const Task = ThreadPool.Task;
Expand Down Expand Up @@ -768,11 +769,28 @@ pub const AccountsDB = struct {
} orelse continue;
const result = try self.getAccountHashAndLamportsFromRef(max_slot_ref);

// only include non-zero lamport accounts (for full snapshots)
const lamports = result.lamports;
if (config == .FullAccountHash and lamports == 0) continue;
var account_hash = result.hash;
if (lamports == 0) {
switch (config) {
// for full snapshots, only include non-zero lamport accounts
.FullAccountHash => continue,
// zero-lamport accounts for incrementals = hash(pubkey)
.IncrementalAccountHash => {
var hasher = Blake3.init(.{});
hasher.update(&key.data);
hasher.final(&account_hash.data);
},
}
} else {
// hashes arent always stored correctly in snapshots
if (account_hash.order(&Hash.default()) == .eq) {
const account = try self.getAccountFromRef(max_slot_ref);
account_hash = account.hash(&key);
}
}

hashes.appendAssumeCapacity(result.hash);
hashes.appendAssumeCapacity(account_hash);
local_total_lamports += lamports;
}

Expand Down
13 changes: 10 additions & 3 deletions src/accountsdb/snapshots.zig
Original file line number Diff line number Diff line change
Expand Up @@ -705,10 +705,17 @@ pub const StatusCache = struct {
return error.SlotNotFoundInHistory;
}
}
for (slot_history.oldest()..slot_history.newest()) |slot| {
if (!slots_seen.contains(slot)) {
return error.SlotNotFoundInStatusCache;

var slots_checked: u32 = 0;
var slot = slot_history.newest();
while (slot >= slot_history.oldest() and slots_checked != MAX_CACHE_ENTRIES) {
if (slot_history.check(slot) == sysvars.SlotCheckResult.Found) {
slots_checked += 1;
if (!slots_seen.contains(slot)) {
return error.SlotNotFoundInStatusCache;
}
}
slot -= 1;
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/common/merkle_tree.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub const NestedHashTree = struct {
var i: usize = 0;
while (i < self.hashes.len) {
const nested_len = self.hashes[i].items.len;
if ((search_index + nested_len) > index) {
if (search_index + nested_len > index) {
const index_in_nested = index - search_index;
return &self.hashes[i].items[index_in_nested];
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/core/pubkey.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ pub const Pubkey = struct {
}

pub fn format(self: @This(), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
return writer.print("{s}", .{self.string()});
var dest: [44]u8 = undefined;
@memset(&dest, 0);
const written = encoder.encode(&self.data, &dest) catch return error.EncodingError;
return writer.print("{s}", .{dest[0..written]});
}

pub fn isDefault(self: *const Self) bool {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/tar.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn parallelUntarToFileSystem(
thread_pool.deinit();
}

logger.infof("using {d} threads to unpack snapshot\n", .{n_threads});
logger.infof("using {d} threads to unpack snapshot", .{n_threads});
const tasks = try UnTarTask.init(allocator, n_threads);
defer allocator.free(tasks);

Expand Down

0 comments on commit 39a3d60

Please sign in to comment.