From 12f479c022ac05cb6b199a5e0fb59ab6009f9f96 Mon Sep 17 00:00:00 2001 From: Yannick Bensacq Date: Wed, 25 Sep 2024 19:28:20 +0200 Subject: [PATCH] rebase & update test --- src/network/protocol/messages/getdata.zig | 4 +-- src/network/wire/lib.zig | 39 +++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/network/protocol/messages/getdata.zig b/src/network/protocol/messages/getdata.zig index 119a7b3..1e82bbe 100644 --- a/src/network/protocol/messages/getdata.zig +++ b/src/network/protocol/messages/getdata.zig @@ -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 diff --git a/src/network/wire/lib.zig b/src/network/wire/lib.zig index 9e034a5..cbc7b9b 100644 --- a/src/network/wire/lib.zig +++ b/src/network/wire/lib.zig @@ -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;