-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/message/mempool
- Loading branch information
Showing
8 changed files
with
750 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
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; | ||
|
||
/// GetaddrMessage represents the "getaddr" message | ||
/// | ||
/// https://developer.bitcoin.org/reference/p2p_networking.html#getaddr | ||
pub const GetaddrMessage = struct { | ||
// getaddr message do not contain any payload, thus there is no field | ||
|
||
pub inline fn name() *const [12]u8 { | ||
return protocol.CommandNames.GETADDR ++ [_]u8{0} ** 5; | ||
} | ||
|
||
pub fn checksum(self: GetaddrMessage) [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 GetaddrMessage, allocator: std.mem.Allocator) ![]u8 { | ||
_ = self; | ||
_ = allocator; | ||
return &.{}; | ||
} | ||
|
||
pub fn deserializeReader(allocator: std.mem.Allocator, r: anytype) !GetaddrMessage { | ||
_ = allocator; | ||
_ = r; | ||
return GetaddrMessage{}; | ||
} | ||
|
||
pub fn hintSerializedLen(self: GetaddrMessage) usize { | ||
_ = self; | ||
return 0; | ||
} | ||
}; | ||
|
||
// TESTS | ||
|
||
test "ok_full_flow_GetaddrMessage" { | ||
const allocator = std.testing.allocator; | ||
|
||
{ | ||
const msg = GetaddrMessage{}; | ||
|
||
const payload = try msg.serialize(allocator); | ||
defer allocator.free(payload); | ||
const deserialized_msg = try GetaddrMessage.deserializeReader(allocator, payload); | ||
_ = deserialized_msg; | ||
|
||
try std.testing.expect(payload.len == 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.