-
Notifications
You must be signed in to change notification settings - Fork 30
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
feat/blocktxn #154
Closed
Closed
feat/blocktxn #154
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
const std = @import("std"); | ||
const protocol = @import("../lib.zig"); | ||
|
||
const Sha256 = std.crypto.hash.sha2.Sha256; | ||
const BlockHeader = @import("../../../types/block_header.zig"); | ||
const Transaction = @import("../../../types/transaction.zig"); | ||
const CompactSizeUint = @import("bitcoin-primitives").types.CompatSizeUint; | ||
const genericChecksum = @import("lib.zig").genericChecksum; | ||
const genericDeserializeSlice = @import("lib.zig").genericDeserializeSlice; | ||
const genericSerialize = @import("lib.zig").genericSerialize; | ||
|
||
/// BlockTxnMessage represents the "BlockTxn" message | ||
/// | ||
/// https://developer.bitcoin.org/reference/p2p_networking.html#blocktxn | ||
pub const BlockTxnMessage = struct { | ||
block_hash: [32]u8, | ||
transactions: []Transaction, | ||
|
||
const Self = @This(); | ||
|
||
pub fn name() *const [12]u8 { | ||
return protocol.CommandNames.BLOCKTXN ++ [_]u8{0} ** 4; | ||
} | ||
|
||
/// Returns the message checksum | ||
pub fn checksum(self: *const Self) [4]u8 { | ||
return genericChecksum(self); | ||
} | ||
|
||
/// Free the allocated memory | ||
pub fn deinit(self: *const Self, allocator: std.mem.Allocator) void { | ||
allocator.free(self.transactions); | ||
} | ||
|
||
/// Serialize the message as bytes and write them to the Writer. | ||
pub fn serializeToWriter(self: *const Self, w: anytype) !void { | ||
comptime { | ||
if (!std.meta.hasFn(@TypeOf(w), "writeInt")) @compileError("Expects r to have fn 'writeInt'."); | ||
if (!std.meta.hasFn(@TypeOf(w), "writeAll")) @compileError("Expects r to have fn 'writeAll'."); | ||
} | ||
try w.writeAll(&self.block_hash); | ||
const indexes_count = CompactSizeUint.new(self.transactions.len); | ||
try indexes_count.encodeToWriter(w); | ||
for (self.transactions) |*index| { | ||
try index.encodeToWriter(w); | ||
} | ||
} | ||
/// Serialize a message as bytes and write them to the buffer. | ||
/// | ||
/// buffer.len must be >= than self.hintSerializedLen() | ||
pub fn serializeToSlice(self: *const Self, buffer: []u8) !void { | ||
var fbs = std.io.fixedBufferStream(buffer); | ||
try self.serializeToWriter(fbs.writer()); | ||
} | ||
|
||
/// Returns the hint of the serialized length of the message. | ||
pub fn hintSerializedLen(self: *const Self) usize { | ||
// 32 bytes for the block hash | ||
const fixed_length = 32; | ||
|
||
const indexes_count_length: usize = CompactSizeUint.new(self.transactions.len).hint_encoded_len(); | ||
|
||
var compact_indexes_length: usize = 0; | ||
for (self.transactions) |index| { | ||
compact_indexes_length += index.hint_encoded_len(); | ||
} | ||
|
||
const variable_length = indexes_count_length + compact_indexes_length; | ||
|
||
return fixed_length + variable_length; | ||
} | ||
|
||
pub fn deserializeReader(allocator: std.mem.Allocator, r: anytype) !Self { | ||
var blocktxn_message: Self = undefined; | ||
try r.readNoEof(&blocktxn_message.block_hash); | ||
|
||
const indexes_count = try Transaction.deserializeReader(allocator, r); | ||
blocktxn_message.transactions = try allocator.alloc(Transaction, indexes_count.value()); | ||
errdefer allocator.free(blocktxn_message.transactions); | ||
|
||
for (blocktxn_message.transactions) |*index| { | ||
index.* = try Transaction.deserializeReader(allocator, r); | ||
} | ||
|
||
return blocktxn_message; | ||
} | ||
|
||
pub fn new(block_hash: [32]u8, transactions: []Transaction) Self { | ||
return .{ | ||
.block_hash = block_hash, | ||
.transactions = transactions, | ||
}; | ||
} | ||
}; | ||
|
||
test "BlockTxnMessage serialization and deserialization" { | ||
const test_allocator = std.testing.allocator; | ||
|
||
const block_hash: [32]u8 = [_]u8{0} ** 32; | ||
const transaction = try test_allocator.alloc(Transaction, 1); | ||
transaction[0] = Transaction.init(test_allocator); | ||
const msg = BlockTxnMessage.new(block_hash, transaction); | ||
|
||
defer msg.deinit(test_allocator); | ||
|
||
const serialized = try msg.genericSerialize(test_allocator); | ||
defer test_allocator.free(serialized); | ||
|
||
const deserialized = try BlockTxnMessage.genericDeserializeSlice(test_allocator, serialized); | ||
defer deserialized.deinit(test_allocator); | ||
|
||
try std.testing.expectEqual(msg.block_hash, deserialized.block_hash); | ||
try std.testing.expectEqual(msg.transaction[0].value(), msg.transaction[0].value()); | ||
try std.testing.expectEqual(msg.hintSerializedLen(), 32 + 1 + 1); | ||
} |
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ const Sha256 = std.crypto.hash.sha2.Sha256; | |
pub const NotFoundMessage = @import("notfound.zig").NotFoundMessage; | ||
pub const SendHeadersMessage = @import("sendheaders.zig").SendHeadersMessage; | ||
pub const FilterLoadMessage = @import("filterload.zig").FilterLoadMessage; | ||
pub const BlockTxnMessage = @import("blocktxn.zig").BlockTxnMessage; | ||
pub const HeadersMessage = @import("headers.zig").HeadersMessage; | ||
pub const CmpctBlockMessage = @import("cmpctblock.zig").CmpctBlockMessage; | ||
|
||
|
@@ -38,12 +39,12 @@ pub const MessageTypes = enum { | |
notfound, | ||
sendheaders, | ||
filterload, | ||
blocktxn, | ||
getdata, | ||
headers, | ||
cmpctblock, | ||
}; | ||
|
||
|
||
pub const Message = union(MessageTypes) { | ||
version: VersionMessage, | ||
verack: VerackMessage, | ||
|
@@ -61,6 +62,7 @@ pub const Message = union(MessageTypes) { | |
notfound: NotFoundMessage, | ||
sendheaders: SendHeadersMessage, | ||
filterload: FilterLoadMessage, | ||
blocktxn: BlockTxnMessage, | ||
getdata: GetdataMessage, | ||
headers: HeadersMessage, | ||
cmpctblock: CmpctBlockMessage, | ||
|
@@ -83,6 +85,7 @@ pub const Message = union(MessageTypes) { | |
.notfound => |m| @TypeOf(m).name(), | ||
.sendheaders => |m| @TypeOf(m).name(), | ||
.filterload => |m| @TypeOf(m).name(), | ||
.blocktxn => |m| @TypeOf(m).name(), | ||
.getdata => |m| @TypeOf(m).name(), | ||
.headers => |m| @TypeOf(m).name(), | ||
.cmpctblock => |m| @TypeOf(m).name(), | ||
|
@@ -98,8 +101,11 @@ pub const Message = union(MessageTypes) { | |
.filteradd => |*m| m.deinit(allocator), | ||
.getdata => |*m| m.deinit(allocator), | ||
.cmpctblock => |*m| m.deinit(allocator), | ||
.sendheaders => {}, | ||
.filterload => {}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it can be removed, because managed in the else. |
||
.blocktxn => |*m| m.deinit(allocator), | ||
.headers => |*m| m.deinit(allocator), | ||
else => {} | ||
else => {}, | ||
} | ||
} | ||
|
||
|
@@ -121,6 +127,7 @@ pub const Message = union(MessageTypes) { | |
.notfound => |*m| m.checksum(), | ||
.sendheaders => |*m| m.checksum(), | ||
.filterload => |*m| m.checksum(), | ||
.blocktxn => |*m| m.checksum(), | ||
.getdata => |*m| m.checksum(), | ||
.headers => |*m| m.checksum(), | ||
.cmpctblock => |*m| m.checksum(), | ||
|
@@ -145,6 +152,7 @@ pub const Message = union(MessageTypes) { | |
.notfound => |m| m.hintSerializedLen(), | ||
.sendheaders => |m| m.hintSerializedLen(), | ||
.filterload => |*m| m.hintSerializedLen(), | ||
.blocktxn => |*m| m.hintSerializedLen(), | ||
.getdata => |m| m.hintSerializedLen(), | ||
.headers => |*m| m.hintSerializedLen(), | ||
.cmpctblock => |*m| m.hintSerializedLen(), | ||
|
@@ -194,4 +202,4 @@ pub fn genericDeserializeSlice(comptime T: type, allocator: std.mem.Allocator, b | |
const reader = fbs.reader(); | ||
|
||
return try T.deserializeReader(allocator, reader); | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you have to use deserialize from BlockTxnMessage not the generic one