Skip to content

Commit

Permalink
Merge branch 'main' into feat/message/mempool
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro committed Sep 20, 2024
2 parents cae11b0 + d070f88 commit 75910d9
Show file tree
Hide file tree
Showing 8 changed files with 744 additions and 406 deletions.
1 change: 1 addition & 0 deletions src/network/p2p.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub const P2P = struct {
// TODO: Implement the P2P network handler
// Initialize the listener
// const address = try net.Address.parseIp4("0.0.0.0", self.config.p2p_port);
// std.debug.panic("{any}", .{address});
// const stream = try net.tcpConnectToAddress(address);

// self.listener = net.Server{
Expand Down
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);
}
}
6 changes: 5 additions & 1 deletion src/network/protocol/messages/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@ const std = @import("std");
pub const VersionMessage = @import("version.zig").VersionMessage;
pub const VerackMessage = @import("verack.zig").VerackMessage;
pub const MempoolMessage = @import("mempool.zig").MempoolMessage;
pub const GetaddrMessage = @import("getaddr.zig").GetaddrMessage;

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

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

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

Expand All @@ -30,6 +33,7 @@ pub const Message = union(MessageTypes) {
.Version => |m| m.hintSerializedLen(),
.Verack => |m| m.hintSerializedLen(),
.Mempool => |m| m.hintSerializedLen(),
.Getaddr => |m| m.hintSerializedLen(),
};
}
};
9 changes: 7 additions & 2 deletions src/network/wire/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ 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)}
else if (std.mem.eql(u8, &command, protocol.messages.MempoolMessage.name()))
protocol.messages.Message{ .Mempool = try protocol.messages.MempoolMessage.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 @@ -136,6 +138,7 @@ test "ok_send_version_message" {
.Version => |rm| try std.testing.expect(message.eql(&rm)),
.Verack => unreachable,
.Mempool => unreachable,
.Getaddr => unreachable,
}
}

Expand All @@ -161,6 +164,7 @@ test "ok_send_verack_message" {
.Verack => {},
.Version => unreachable,
.Mempool => unreachable,
.Getaddr => unreachable,
}
}

Expand All @@ -186,6 +190,7 @@ test "ok_send_mempool_message" {
.Mempool => {},
.Verack => unreachable,
.Version => unreachable,
.Getaddr => unreachable,
}
}

Expand Down
Loading

0 comments on commit 75910d9

Please sign in to comment.