Skip to content

Commit

Permalink
rebase & update test
Browse files Browse the repository at this point in the history
  • Loading branch information
ybensacq committed Sep 25, 2024
1 parent cef4b98 commit 12f479c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/network/protocol/messages/getdata.zig
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,18 @@ test "ok_full_flow_GetdataMessage" {
const inventory_items = [_]GetdataMessage.InventoryItem{
.{ .type = 1, .hash = [_]u8{0xab} ** 32 },
.{ .type = 2, .hash = [_]u8{0xcd} ** 32 },
.{ .type = 2, .hash = [_]u8{0xef} ** 32 },
};

const gd = GetdataMessage{
.inventory = inventory_items[0..],
};

// Serialize
const payload = try gd.serialize(allocator);
defer allocator.free(payload);

// Deserialize
const deserialized_gd = try GetdataMessage.deserializeSlice(allocator, payload);

// Test equality
try std.testing.expect(gd.eql(&deserialized_gd));

// Free allocated memory for deserialized inventory
Expand Down
39 changes: 39 additions & 0 deletions src/network/wire/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,45 @@ test "ok_send_mempool_message" {
}
}

test "ok_send_getdata_message" {
const Config = @import("../../config/config.zig").Config;
const ArrayList = std.ArrayList;
const test_allocator = std.testing.allocator;
const GetdataMessage = protocol.messages.GetdataMessage;

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

const inventory = try test_allocator.alloc(GetdataMessage.InventoryItem, 5);
defer test_allocator.free(inventory);

for (inventory) |*item| {
item.type = 1;
for (&item.hash) |*byte| {
byte.* = 0xab;
}
}

const message = GetdataMessage{
.inventory = inventory,
};

const writer = list.writer();
try sendMessage(test_allocator, writer, Config.PROTOCOL_VERSION, Config.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);

switch (received_message) {
.Getdata => |rm| {
try std.testing.expect(message.eql(&rm));
defer rm.deinit(test_allocator);
},
else => unreachable,
}
}

test "ok_send_getblocks_message" {
const Config = @import("../../config/config.zig").Config;
Expand Down

0 comments on commit 12f479c

Please sign in to comment.