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

Feat/message/mempool #106

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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 MempoolMessage = @import("mempool.zig").MempoolMessage;

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

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

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

pub fn hintSerializedLen(self: Message) usize {
return switch (self) {
.Version => |m| m.hintSerializedLen(),
.Verack => |m| m.hintSerializedLen(),
.Mempool => |m| m.hintSerializedLen(),
};
}
};
57 changes: 57 additions & 0 deletions src/network/protocol/messages/mempool.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

const std = @import("std");
const native_endian = @import("builtin").target.cpu.arch.endian();
const protocol = @import("../lib.zig");

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

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

pub fn checksum(self: MempoolMessage) [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 MempoolMessage, allocator: std.mem.Allocator) ![]u8 {
_ = self;
_ = allocator;
return &.{};
}

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

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

};

// TESTS

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

{
const msg = MempoolMessage{};

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

try std.testing.expect(payload.len == 0);
}
}
29 changes: 29 additions & 0 deletions src/network/wire/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ pub fn receiveMessage(allocator: std.mem.Allocator, r: anytype) !protocol.messag
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
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,
.Mempool => unreachable,
}
}

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

test "ok_send_mempool_message" {
const ArrayList = std.ArrayList;
const test_allocator = std.testing.allocator;
const MempoolMessage = protocol.messages.MempoolMessage;

var list: std.ArrayListAligned(u8, null) = ArrayList(u8).init(test_allocator);
defer list.deinit();

const message = MempoolMessage{};

const writer = list.writer();
try sendMessage(test_allocator, writer, protocol.PROTOCOL_VERSION, protocol.BitcoinNetworkId.MAINNET, message);
var fbs: std.io.FixedBufferStream([]u8) = std.io.fixedBufferStream(list.items);
const reader = fbs.reader();

const received_message = try receiveMessage(test_allocator, reader);
defer received_message.deinit(test_allocator);

switch (received_message) {
.Mempool => {},
.Verack => unreachable,
.Version => unreachable,
}
}

Expand Down
Loading