Skip to content

Commit

Permalink
fix code style and add missing test for headers message
Browse files Browse the repository at this point in the history
  • Loading branch information
ptisserand committed Oct 2, 2024
1 parent e867f9f commit fedba9d
Showing 1 changed file with 53 additions and 27 deletions.
80 changes: 53 additions & 27 deletions src/network/protocol/messages/headers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ pub const HeadersMessage = struct {
if (!std.meta.hasFn(@TypeOf(w), "writeAll")) @compileError("Expects r to have fn 'writeAll'.");
if (!std.meta.hasFn(@TypeOf(w), "writeByte")) @compileError("Expects r to have fn 'writeByte'.");
}
if (self.headers.len != 0) {
try CompactSizeUint.new(self.headers.len).encodeToWriter(w);
try CompactSizeUint.new(self.headers.len).encodeToWriter(w);

for (self.headers) |header| {
try header.serializeToWriter(w);
try w.writeByte(0);
}
for (self.headers) |header| {
try header.serializeToWriter(w);
try w.writeByte(0);
}
}

Expand Down Expand Up @@ -76,21 +74,17 @@ pub const HeadersMessage = struct {
if (!std.meta.hasFn(@TypeOf(r), "readByte")) @compileError("Expects r to have fn 'readByte'.");
}

var headers_message: Self = undefined;

const headers_count = try CompactSizeUint.decodeReader(r);

headers_message.headers = try allocator.alloc(BlockHeader, headers_count.value());
errdefer allocator.free(headers_message.headers);
var headers = try allocator.alloc(BlockHeader, headers_count.value());
errdefer allocator.free(headers);

var i: usize = 0;
while (i < headers_count.value()) : (i += 1) {
const header = try BlockHeader.deserializeReader(allocator, r);
headers_message.headers[i] = header;
_ = r.readByte();
for (0..headers_count.value()) |i| {
headers[i] = try BlockHeader.deserializeReader(r);
_ = try r.readByte();
}

return headers_message;
return Self{ .headers = headers };
}

/// Deserialize bytes into a `HeaderMessage`
Expand All @@ -100,16 +94,48 @@ pub const HeadersMessage = struct {
}

pub fn hintSerializedLen(self: Self) usize {
if (self.headers.len != 0) {
const headers_number_length = CompactSizeUint.new(self.headers.len).hint_encoded_len();
var headers_length: usize = 0;
for (self.headers) |_| {
headers_length += BlockHeader.serializedLen();
headers_length += 1; // 0 transactions
}
return headers_number_length + headers_length;
} else {
return 0;
}
const headers_number_length = CompactSizeUint.new(self.headers.len).hint_encoded_len();
const headers_length = self.headers.len * (BlockHeader.serializedLen() + 1);
return headers_number_length + headers_length;
}
};

// TESTS

test "ok_fullflow_headers_message" {
const allocator = std.testing.allocator;

{
// payload example from https://developer.bitcoin.org/reference/p2p_networking.html#headers
const payload = [_]u8{
0x01, // header count
// block header
0x02, 0x00, 0x00, 0x00, // block version: 2
0xb6, 0xff, 0x0b, 0x1b,
0x16, 0x80, 0xa2, 0x86,
0x2a, 0x30, 0xca, 0x44,
0xd3, 0x46, 0xd9, 0xe8,
0x91, 0x0d, 0x33, 0x4b, 0xeb, 0x48, 0xca, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // hash of previous block
0x9d, 0x10, 0xaa, 0x52, 0xee, 0x94, 0x93, 0x86, 0xca, 0x93, 0x85, 0x69, 0x5f, 0x04, 0xed, 0xe2,
0x70, 0xdd, 0xa2, 0x08, 0x10, 0xde, 0xcd, 0x12, 0xbc, 0x9b, 0x04, 0x8a, 0xaa, 0xb3, 0x14, 0x71, // merkle root
0x24, 0xd9, 0x5a, 0x54, // unix time (1415239972)
0x30, 0xc3, 0x1b, 0x18, // bits
0xfe, 0x9f, 0x08, 0x64, // nonce
// end of block header
0x00, // transaction count
};

var deserialized_msg = try HeadersMessage.deserializeSlice(allocator, &payload);
defer deserialized_msg.deinit(allocator);

try std.testing.expectEqual(1, deserialized_msg.headers.len);
try std.testing.expectEqual(2, deserialized_msg.headers[0].version);
try std.testing.expectEqual(1415239972, deserialized_msg.headers[0].timestamp);
try std.testing.expectEqual(1678286846, deserialized_msg.headers[0].nonce);

const serialized_payload = try deserialized_msg.serialize(allocator);
defer allocator.free(serialized_payload);

try std.testing.expect(std.mem.eql(u8, &payload, serialized_payload));
}
}

0 comments on commit fedba9d

Please sign in to comment.