Skip to content

Commit

Permalink
Merge branch 'zig-bitcoin:main' into op-sha1
Browse files Browse the repository at this point in the history
  • Loading branch information
supreme2580 committed Sep 20, 2024
2 parents d626178 + 35eb160 commit 2bdf249
Show file tree
Hide file tree
Showing 12 changed files with 1,063 additions and 475 deletions.
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
.hash = "1220f9e1eb744c8dc2750c1e6e1ceb1c2d521bedb161ddead1a6bb772032e576d74a",
},
.@"bitcoin-primitives" = .{
.url = "git+https://github.com/zig-bitcoin/bitcoin-primitives#4d179bb3027dbc35a99a56938c05008b62e4bf7e",
.hash = "1220a65f6105a79c9347449d2553e7abf965b3f61fa883478954d861e824631d5396",
.url = "git+https://github.com/zig-bitcoin/bitcoin-primitives#3743701d398b35af80826b729b6eb12d3e8e8df9",
.hash = "12204e7aa2c42049440faf891e80cd7bc85f64b4aacdcda75e891ca52787f267342c",
},
},
.paths = .{
Expand Down
1 change: 1 addition & 0 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub const wallet = @import("wallet/wallet.zig");
pub const miner = @import("miner/miner.zig");
pub const node = @import("node/node.zig");
pub const script = @import("script/lib.zig");
pub const wire = @import("network/wire/lib.zig");

test {
@import("std").testing.refAllDeclsRecursive(@This());
Expand Down
23 changes: 5 additions & 18 deletions src/network/protocol/lib.zig
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
pub const message = @import("./message.zig");
pub const messages = @import("./messages/lib.zig");

/// Protocol version
pub const PROTOCOL_VERSION: i32 = 70015;

/// Known network ids
pub const NetworkMagicBytes = struct {
pub const MAINNET: [4]u8 = 0xd9b4bef9;
pub const BitcoinNetworkId = struct {
pub const MAINNET: [4]u8 = .{ 0xd9, 0xb4, 0xbe, 0xf9 };
pub const REGTEST: [4]u8 = 0xdab5bffa;
pub const TESTNET3: [4]u8 = 0x0709110b;
pub const SIGNET: [4]u8 = 0x40cf030a;
};

/// Protocol version
pub const PROTOCOL_VERSION: i32 = 70015;

/// Network services
pub const ServiceFlags = struct {
pub const NODE_NETWORK: u64 = 0x1;
Expand All @@ -22,18 +21,6 @@ pub const ServiceFlags = struct {
pub const NODE_NETWORK_LIMITED: u64 = 0x0400;
};

/// An IpV6 address
pub const IpV6Address = struct {
ip: [8]u16, // represented in big endian
port: u16, // represented in system native endian
};

/// NetworkAddress represents a network address
pub const NetworkAddress = struct {
services: u64,
address: IpV6Address,
};

pub const CommandNames = struct {
pub const VERSION = "version";
pub const VERACK = "verack";
Expand Down
42 changes: 0 additions & 42 deletions src/network/protocol/message.zig

This file was deleted.

29 changes: 29 additions & 0 deletions src/network/protocol/messages/lib.zig
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
const std = @import("std");
pub const VersionMessage = @import("version.zig").VersionMessage;
pub const VerackMessage = @import("verack.zig").VerackMessage;

pub const MessageTypes = enum { Version, Verack };

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

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

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

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

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

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

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

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

};

// TESTS

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

{
const msg = VerackMessage{};

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

try std.testing.expect(payload.len == 0);
}
}
Loading

0 comments on commit 2bdf249

Please sign in to comment.