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

Adding filter clear message #129

Merged
merged 3 commits into from
Sep 27, 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
54 changes: 54 additions & 0 deletions src/network/protocol/messages/filterclear.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const std = @import("std");
const protocol = @import("../lib.zig");

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

pub inline fn name() *const [12]u8 {
return protocol.CommandNames.FILTERCLEAR;
}

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

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

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

// TESTS

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

{
const msg = FilterClearMessage{};

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

try std.testing.expect(payload.len == 0);
}
}
8 changes: 8 additions & 0 deletions src/network/protocol/messages/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub const GetblocksMessage = @import("getblocks.zig").GetblocksMessage;
pub const PingMessage = @import("ping.zig").PingMessage;
pub const PongMessage = @import("pong.zig").PongMessage;
pub const FeeFilterMessage = @import("feefilter.zig").FeeFilterMessage;
pub const FilterClearMessage = @import("filterclear.zig").FilterClearMessage;

pub const MessageTypes = enum {
version,
Expand All @@ -17,6 +18,7 @@ pub const MessageTypes = enum {
ping,
pong,
feefilter,
filterclear,
};

pub const Message = union(MessageTypes) {
Expand All @@ -28,6 +30,7 @@ pub const Message = union(MessageTypes) {
ping: PingMessage,
pong: PongMessage,
feefilter: FeeFilterMessage,
filterclear: FilterClearMessage,

pub fn name(self: Message) *const [12]u8 {
return switch (self) {
Expand All @@ -39,6 +42,7 @@ pub const Message = union(MessageTypes) {
.ping => |m| @TypeOf(m).name(),
.pong => |m| @TypeOf(m).name(),
.feefilter => |m| @TypeOf(m).name(),
.filterclear => |m| @TypeOf(m).name(),
};
}

Expand All @@ -52,8 +56,10 @@ pub const Message = union(MessageTypes) {
.ping => {},
.pong => {},
.feefilter => {},
.filterclear => {},
}
}

pub fn checksum(self: Message) [4]u8 {
return switch (self) {
.version => |m| m.checksum(),
Expand All @@ -64,6 +70,7 @@ pub const Message = union(MessageTypes) {
.ping => |m| m.checksum(),
.pong => |m| m.checksum(),
.feefilter => |m| m.checksum(),
.filterclear => |m| m.checksum(),
};
}

Expand All @@ -77,6 +84,7 @@ pub const Message = union(MessageTypes) {
.ping => |m| m.hintSerializedLen(),
.pong => |m| m.hintSerializedLen(),
.feefilter => |m| m.hintSerializedLen(),
.filterclear => |m| m.hintSerializedLen(),
};
}
};
2 changes: 2 additions & 0 deletions src/network/wire/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ pub fn receiveMessage(
protocol.messages.Message{ .ping = try protocol.messages.PingMessage.deserializeReader(allocator, r) }
else if (std.mem.eql(u8, &command, protocol.messages.PongMessage.name()))
protocol.messages.Message{ .pong = try protocol.messages.PongMessage.deserializeReader(allocator, r) }
else if (std.mem.eql(u8, &command, protocol.messages.FilterClearMessage.name()))
protocol.messages.Message{ .filterclear = try protocol.messages.FilterClearMessage.deserializeReader(allocator, r) }
else {
try r.skipBytes(payload_len, .{}); // Purge the wire
return error.UnknownMessage;
Expand Down
Loading