Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getaddr message #111

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/network/protocol/messages/getaddr.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const std = @import("std");
const native_endian = @import("builtin").target.cpu.arch.endian();
const protocol = @import("../lib.zig");

const ServiceFlags = protocol.ServiceFlags;

const Endian = std.builtin.Endian;
const Sha256 = std.crypto.hash.sha2.Sha256;

const CompactSizeUint = @import("bitcoin-primitives").types.CompatSizeUint;

/// GetaddrMessage represents the "getaddr" message
///
/// https://developer.bitcoin.org/reference/p2p_networking.html#getaddr
pub const GetaddrMessage = struct {
// getaddr message do not contain any payload, thus there is no field

pub inline fn name() *const [12]u8 {
return protocol.CommandNames.GETADDR ++ [_]u8{0} ** 5;
}

pub fn checksum(self: GetaddrMessage) [4]u8 {
_ = self;
// If payload is empty, the checksum is always 0x5df6e0e2 (SHA256(SHA256("")))
return [4]u8{ 0x5d, 0xf6, 0xe0, 0xe2 };
}

/// Serialize a message as bytes and return them.
pub fn serialize(self: *const GetaddrMessage, allocator: std.mem.Allocator) ![]u8 {
_ = self;
_ = allocator;
return &.{};
}

pub fn deserializeReader(allocator: std.mem.Allocator, r: anytype) !GetaddrMessage {
_ = allocator;
_ = r;
return GetaddrMessage{};
}

pub fn hintSerializedLen(self: GetaddrMessage) usize {
_ = self;
return 0;
}
};

// TESTS

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

{
const msg = GetaddrMessage{};

const payload = try msg.serialize(allocator);
defer allocator.free(payload);
const deserialized_msg = try GetaddrMessage.deserializeReader(allocator, payload);
_ = deserialized_msg;

try std.testing.expect(payload.len == 0);
}
}
7 changes: 6 additions & 1 deletion src/network/protocol/messages/lib.zig
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
const std = @import("std");
pub const VersionMessage = @import("version.zig").VersionMessage;
pub const VerackMessage = @import("verack.zig").VerackMessage;
pub const GetaddrMessage = @import("getaddr.zig").GetaddrMessage;

pub const MessageTypes = enum { Version, Verack };
pub const MessageTypes = enum { Version, Verack, Getaddr };

pub const Message = union(MessageTypes) {
Version: VersionMessage,
Verack: VerackMessage,
Getaddr: GetaddrMessage,

pub fn deinit(self: Message, allocator: std.mem.Allocator) void {
switch (self) {
.Version => |m| m.deinit(allocator),
.Verack => {},
.Getaddr => {},
}
}
pub fn checksum(self: Message) [4]u8 {
return switch (self) {
.Version => |m| m.checksum(),
.Verack => |m| m.checksum(),
.Getaddr => |m| m.checksum(),
};
}

pub fn hintSerializedLen(self: Message) usize {
return switch (self) {
.Version => |m| m.hintSerializedLen(),
.Verack => |m| m.hintSerializedLen(),
.Getaddr => |m| m.hintSerializedLen(),
};
}
};
10 changes: 7 additions & 3 deletions src/network/wire/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ pub fn receiveMessage(allocator: std.mem.Allocator, r: anytype) !protocol.messag
const checksum = try r.readBytesNoEof(4);

// Read payload
const message: protocol.messages.Message = if (std.mem.eql(u8, &command, protocol.messages.VersionMessage.name()))
protocol.messages.Message{ .Version = try protocol.messages.VersionMessage.deserializeReader(allocator, r)}
const message: protocol.messages.Message = if (std.mem.eql(u8, &command, protocol.messages.VersionMessage.name()))
protocol.messages.Message{ .Version = try protocol.messages.VersionMessage.deserializeReader(allocator, r) }
else if (std.mem.eql(u8, &command, protocol.messages.VerackMessage.name()))
protocol.messages.Message{ .Verack = try protocol.messages.VerackMessage.deserializeReader(allocator, r)}
protocol.messages.Message{ .Verack = try protocol.messages.VerackMessage.deserializeReader(allocator, r) }
else if (std.mem.eql(u8, &command, protocol.messages.GetaddrMessage.name()))
protocol.messages.Message{ .Getaddr = try protocol.messages.GetaddrMessage.deserializeReader(allocator, r) }
else
return error.InvalidCommand;
errdefer message.deinit(allocator);
Expand Down Expand Up @@ -133,6 +135,7 @@ test "ok_send_version_message" {
switch (received_message) {
.Version => |rm| try std.testing.expect(message.eql(&rm)),
.Verack => unreachable,
.Getaddr => unreachable,
}
}

Expand All @@ -157,6 +160,7 @@ test "ok_send_verack_message" {
switch (received_message) {
.Verack => {},
.Version => unreachable,
.Getaddr => unreachable,
}
}

Expand Down
Loading