Skip to content

Commit

Permalink
feat(ci) start correcting files to fit the new style guide (#361)
Browse files Browse the repository at this point in the history
* start correcting files to fit the new style

* continue formatting files

* remove non-style change

* some more refactors

* fix some try precedence issues
  • Loading branch information
Rexicon226 authored Nov 13, 2024
1 parent 46aacd5 commit 6aa5c3c
Show file tree
Hide file tree
Showing 46 changed files with 522 additions and 248 deletions.
183 changes: 67 additions & 116 deletions scripts/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,138 +117,82 @@ def unused_imports(args, files_to_check):
print("Total unused imports found:", total_lines_removed)
else:
print("Total unused imports removed:", total_lines_removed)

return total_lines_removed


files_excluded_from_line_length_check = [
"src/accountsdb/accounts_file.zig",
"src/accountsdb/bank.zig",
"src/accountsdb/cache.zig",
"src/accountsdb/db.zig",
"src/accountsdb/download.zig",
"src/accountsdb/fuzz_snapshot.zig",
"src/accountsdb/fuzz.zig",
"src/accountsdb/genesis_config.zig",
"src/accountsdb/index.zig",
"src/accountsdb/snapshots.zig",
"src/accountsdb/swiss_map.zig",
"src/accountsdb/sysvars.zig",
"src/benchmarks.zig",
"src/bincode/arraylist.zig",
"src/bincode/bincode.zig",
"src/bincode/bincode.zig",
"src/bincode/hashmap.zig",
"src/bincode/int.zig",
"src/bincode/list.zig",
"src/bincode/optional.zig",
"src/prometheus/metric.zig",
"src/prometheus/histogram.zig",
"src/prometheus/registry.zig",
"src/cmd/helpers.zig",
"src/geyser/core.zig",
"src/rpc/request.zig",
"src/bincode/shortvec.zig",
"src/bincode/varint.zig",
"src/bloom/bit_set.zig",
"src/common/lru.zig",
"src/common/merkle_tree.zig",
"src/fuzz.zig",
"src/geyser/main.zig",
"src/gossip/service.zig",
"src/ledger/database/rocksdb.zig",
"src/ledger/database/hashmap.zig",
"src/accountsdb/bank.zig",
"src/net/echo.zig",
"src/utils/types.zig",
"src/gossip/shards.zig",
"src/gossip/table.zig",
"src/bloom/bitvec.zig",
"src/bincode/int.zig",
"src/ledger/benchmarks.zig",
"src/ledger/cleanup_service.zig",
"src/ledger/database/hashmap.zig",
"src/ledger/database/interface.zig",
"src/ledger/database/rocksdb.zig",
"src/ledger/meta.zig",
"src/ledger/reader.zig",
"src/ledger/reed_solomon_table.zig",
"src/ledger/reed_solomon.zig",
"src/core/leader_schedule.zig",
"src/shred_collector/repair_message.zig",
"src/gossip/pull_request.zig",
"src/bincode/arraylist.zig",
"src/utils/thread.zig",
"src/utils/allocators.zig",
"src/gossip/message.zig",
"src/bloom/bloom.zig",
"src/bloom/bit_vec.zig",
"src/ledger/result_writer.zig",
"src/ledger/schema.zig",
"src/ledger/shred_inserter/merkle_root_checks.zig",
"src/ledger/shred_inserter/recovery.zig",
"src/time/time.zig",
"src/utils/tar.zig",
"src/utils/fmt.zig",
"src/gossip/fuzz_table.zig",
"src/bloom/bit_set.zig",
"src/accountsdb/sysvars.zig",
"src/ledger/shred_inserter/shred_inserter.zig",
"src/ledger/shred_inserter/slot_chaining.zig",
"src/accountsdb/swiss_map.zig",
"src/ledger/shred_inserter/working_state.zig",
"src/ledger/shred.zig",
"src/ledger/shredder.zig",
"src/ledger/test_shreds.zig",
"src/ledger/tests.zig",
"src/ledger/transaction_status.zig",
"src/net/echo.zig",
"src/net/net.zig",
"src/net/socket_utils.zig",
"src/prometheus/histogram.zig",
"src/prometheus/metric.zig",
"src/prometheus/registry.zig",
"src/rand/rand.zig",
"src/rpc/client.zig",
"src/rpc/client.zig",
"src/rpc/request.zig",
"src/rpc/request.zig",
"src/rpc/response.zig",
"src/shred_collector/repair_message.zig",
"src/accountsdb/fuzz_snapshot.zig",
"src/shred_collector/repair_service.zig",
"src/shred_collector/service.zig",
"src/shred_collector/shred_processor.zig",
"src/shred_collector/shred_receiver.zig",
"src/shred_collector/shred_tracker.zig",
"src/shred_collector/shred_verifier.zig",
"src/sync/channel.zig",
"src/sync/channel.zig",
"src/sync/mux.zig",
"src/sync/once_cell.zig",
"src/sync/ref.zig",
"src/sync/ref.zig",
"src/sync/thread_pool.zig",
"src/sync/thread_pool.zig",
"src/tests.zig",
"src/time/time.zig",
"src/trace/log.zig",
"src/transaction_sender/leader_info.zig",
"src/transaction_sender/mock_transfer_generator.zig",
"src/transaction_sender/mock_transfer_generator.zig",
"src/transaction_sender/service.zig",
"src/transaction_sender/service.zig",
"src/transaction_sender/transaction_info.zig",
"src/transaction_sender/transaction_pool.zig",
"src/gossip/fuzz_service.zig",
"src/sync/thread_pool.zig",
"src/transaction_sender/transaction_pool.zig",
"src/utils/allocators.zig",
"src/utils/collections.zig",
"src/utils/fmt.zig",
"src/utils/fmt.zig",
"src/utils/tar.zig",
"src/utils/tar.zig",
"src/utils/thread.zig",
"src/utils/thread.zig",
"src/utils/types.zig",
"src/bloom/bit_vec.zig",
"src/bloom/bitvec.zig",
"src/bloom/bloom.zig",
"src/cmd/cmd.zig",
"src/cmd/config.zig",
"src/cmd/helpers.zig",
"src/core/account.zig",
"src/core/epoch_schedule.zig",
"src/core/hard_forks.zig",
"src/core/leader_schedule.zig",
"src/core/shred.zig",
"src/gossip/ping_pong.zig",
"src/accountsdb/download.zig",
"src/accountsdb/fuzz.zig",
"src/ledger/reader.zig",
"src/rpc/client.zig",
"src/accountsdb/index.zig",
"src/ledger/cleanup_service.zig",
"src/ledger/reed_solomon.zig",
"src/benchmarks.zig",
"src/transaction_sender/mock_transfer_generator.zig",
"src/core/transaction.zig",
"src/crypto/base58.zig",
"src/geyser/core.zig",
"src/gossip/active_set.zig",
"src/net/net.zig",
"src/cmd/cmd.zig",
"src/bincode/bincode.zig",
"src/accountsdb/snapshots.zig",
"src/gossip/data.zig",
"src/gossip/dump_service.zig",
"src/gossip/fuzz_service.zig",
"src/gossip/fuzz_table.zig",
"src/gossip/message.zig",
"src/gossip/ping_pong.zig",
"src/gossip/pull_request.zig",
"src/gossip/pull_response.zig",
"src/gossip/service.zig",
"src/accountsdb/db.zig",
"src/rand/rand.zig",
"src/ledger/reed_solomon_table.zig",
"src/ledger/shred.zig",
"src/ledger/test_shreds.zig",
]


def line_length(args, files_to_check):
"""Enforces lines of code to be at most 100 characters long."""
unique_files = []

# map relating file paths to the number of lines that are too long
unique_files = {}

lines_found = 0

Expand All @@ -258,21 +202,28 @@ def line_length(args, files_to_check):
with open(path) as f:
lines = f.readlines()
for i, line in enumerate(lines):
# ignore comments. these are lines where the first non-whitespace characters
# are "//"
if line.strip().startswith("//"):
continue
if len(line) > MAX_LINE_LENGTH:
print(f"{path}:{i + 1} is too long: {len(line)}")
lines_found += 1
if path not in unique_files:
unique_files.append(path)
unique_files[path] = 1
else:
unique_files[path] += 1
print(line)

print("Files checked:", len(files_to_check))
print("Lines found:", lines_found)

for file in unique_files:
print(f'"{file}",')
# sorted_files = sorted(unique_files.items(), key=lambda x: x[1])
# for file, num_lines in sorted_files:
# print(f'"{file}",')

return lines_found


if __name__ == '__main__':
if __name__ == "__main__":
main()
10 changes: 8 additions & 2 deletions src/accountsdb/accounts_file.zig
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ pub const AccountInFile = struct {
}
}

pub fn toOwnedAccount(self: *const Self, allocator: std.mem.Allocator) std.mem.Allocator.Error!Account {
pub fn toOwnedAccount(
self: *const Self,
allocator: std.mem.Allocator,
) std.mem.Allocator.Error!Account {
const owned_data = try allocator.dupe(u8, self.data);
return .{
.data = owned_data,
Expand Down Expand Up @@ -312,7 +315,10 @@ pub const AccountFile = struct {

/// get account without reading data (a lot faster if the data field isnt used anyway)
/// (used when computing account hashes for snapshot validation)
pub fn getAccountHashAndLamports(self: *const Self, start_offset: usize) error{EOF}!struct { hash: *Hash, lamports: *u64 } {
pub fn getAccountHashAndLamports(
self: *const Self,
start_offset: usize,
) error{EOF}!struct { hash: *Hash, lamports: *u64 } {
var offset = start_offset;

offset += @sizeOf(AccountInFile.StorageInfo);
Expand Down
6 changes: 5 additions & 1 deletion src/accountsdb/cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ pub const AccountsCache = struct {

/// Makes use of the fact that CachedAccount needs to live on the heap & shares its lifetime
/// with .ref_count in order to allocate once instead of twice.
pub fn initCreate(allocator: std.mem.Allocator, account: Account, slot: Slot) error{OutOfMemory}!*CachedAccount {
pub fn initCreate(
allocator: std.mem.Allocator,
account: Account,
slot: Slot,
) error{OutOfMemory}!*CachedAccount {
const buf = try allocator.alignedAlloc(
u8,
@alignOf(CachedAccount),
Expand Down
4 changes: 2 additions & 2 deletions src/accountsdb/db.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1907,7 +1907,7 @@ pub const AccountsDB = struct {
// the queue if it deleted any accounts refs.
const dead_accounts_counter, var dead_accounts_counter_lg = self.dead_accounts_counter.writeWithLock();
defer dead_accounts_counter_lg.unlock();
const removed = dead_accounts_counter.fetchSwapRemove(slot) orelse unreachable;
const removed = dead_accounts_counter.fetchSwapRemove(slot).?;
std.debug.assert(removed.value == accounts_dead_count);
}
}
Expand Down Expand Up @@ -3150,7 +3150,7 @@ pub fn writeSnapshotTarWithFields(
try snapgen.writeAccountsDirHeader(archive_writer_counted);
const file_info_map = manifest.accounts_db_fields.file_map;
for (file_info_map.keys(), file_info_map.values()) |file_slot, file_info| {
const account_file = file_map.getPtr(file_info.id) orelse unreachable;
const account_file = file_map.getPtr(file_info.id).?;
std.debug.assert(account_file.id == file_info.id);
std.debug.assert(account_file.length == file_info.length);

Expand Down
35 changes: 30 additions & 5 deletions src/bincode/hashmap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,20 @@ pub fn hashMapFieldConfig(

var iter = data.iterator();
while (iter.next()) |entry| {
try writeFieldWithConfig(key_info, config.key, writer, entry.key_ptr.*, params);
try writeFieldWithConfig(value_info, config.value, writer, entry.value_ptr.*, params);
try writeFieldWithConfig(
key_info,
config.key,
writer,
entry.key_ptr.*,
params,
);
try writeFieldWithConfig(
value_info,
config.value,
writer,
entry.value_ptr.*,
params,
);
}
}

Expand All @@ -44,7 +56,8 @@ pub fn hashMapFieldConfig(
params: Params,
) anyerror!HashMapType {
const Size = if (hm_info.kind == .unordered) HashMapType.Size else usize;
const len = (try readIntAsLength(Size, reader, params)) orelse return error.HashMapTooBig;
const len = try readIntAsLength(Size, reader, params) orelse
return error.HashMapTooBig;

var data: HashMapType = switch (hm_info.management) {
.managed => HashMapType.init(allocator),
Expand All @@ -60,10 +73,22 @@ pub fn hashMapFieldConfig(
const key_field = std.meta.fieldInfo(HashMapType.KV, .key);
const value_field = std.meta.fieldInfo(HashMapType.KV, .value);
for (0..len) |_| {
const key = try readFieldWithConfig(allocator, reader, params, key_field, config.key);
const key = try readFieldWithConfig(
allocator,
reader,
params,
key_field,
config.key,
);
errdefer bincode.free(allocator, key);

const value = try readFieldWithConfig(allocator, reader, params, value_field, config.value);
const value = try readFieldWithConfig(
allocator,
reader,
params,
value_field,
config.value,
);
errdefer bincode.free(allocator, value);

const gop = data.getOrPutAssumeCapacity(key);
Expand Down
3 changes: 2 additions & 1 deletion src/bincode/list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pub fn valueEncodedAsSlice(
reader: anytype,
params: bincode.Params,
) anyerror!T {
const len = (try bincode.readIntAsLength(usize, reader, params)) orelse return Error.SingleElementSliceInvalidLength;
const len = (try bincode.readIntAsLength(usize, reader, params)) orelse
return Error.SingleElementSliceInvalidLength;
if (len != 1) return Error.SingleElementSliceInvalidLength;
if (config.deserializer) |deserialize| {
return try deserialize(allocator, reader, params);
Expand Down
6 changes: 5 additions & 1 deletion src/bincode/varint.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ pub fn VarIntConfig(comptime VarInt: type) bincode.FieldConfig(VarInt) {
try writer.writeByte(@as(u8, @intCast(v)));
}

pub fn deserialize(allocator: std.mem.Allocator, reader: anytype, params: bincode.Params) !VarInt {
pub fn deserialize(
allocator: std.mem.Allocator,
reader: anytype,
params: bincode.Params,
) !VarInt {
_ = params;
_ = allocator;

Expand Down
Loading

0 comments on commit 6aa5c3c

Please sign in to comment.