Skip to content

Commit

Permalink
define a custom encoder to rlp-encode the block type at different forks
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com>
  • Loading branch information
gballet committed Jan 20, 2025
1 parent d723b7a commit dd3b4a2
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/types/block.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ pub const BlockHeader = struct {
allocator.free(self.extra_data);
self.* = undefined;
}

// defines a custom encoder so that fields that were added later on in the chain history
// are not included in the RLP when hashing.
pub fn encodeToRLP(self: *const BlockHeader, allocator: Allocator, list: *std.ArrayList(u8)) !void {
if (self.parent_beacon_root != null) {
return rlp.serialize(BlockHeader, allocator, self.*, list);
}
if (self.blob_gas_used != null) {
const tmp = .{ self.parent_hash, self.uncle_hash, self.fee_recipient, self.state_root, self.transactions_root, self.receipts_root, self.logs_bloom, self.difficulty, self.block_number, self.gas_limit, self.gas_used, self.timestamp, self.extra_data, self.prev_randao, self.nonce, self.base_fee_per_gas.?, self.withdrawals_root.?, self.blob_gas_used.?, self.excess_blob_gas.? };
return rlp.serialize(@TypeOf(tmp), allocator, tmp, list);
}
if (self.withdrawals_root != null) {
const tmp = .{ self.parent_hash, self.uncle_hash, self.fee_recipient, self.state_root, self.transactions_root, self.receipts_root, self.logs_bloom, self.difficulty, self.block_number, self.gas_limit, self.gas_used, self.timestamp, self.extra_data, self.prev_randao, self.nonce, self.base_fee_per_gas.?, self.withdrawals_root.? };
return rlp.serialize(@TypeOf(tmp), allocator, tmp, list);
}
if (self.base_fee_per_gas != null) {
const tmp = .{ self.parent_hash, self.uncle_hash, self.fee_recipient, self.state_root, self.transactions_root, self.receipts_root, self.logs_bloom, self.difficulty, self.block_number, self.gas_limit, self.gas_used, self.timestamp, self.extra_data, self.prev_randao, self.nonce, self.base_fee_per_gas.? };
return rlp.serialize(@TypeOf(tmp), allocator, tmp, list);
}
const tmp = .{ self.parent_hash, self.uncle_hash, self.fee_recipient, self.state_root, self.transactions_root, self.receipts_root, self.logs_bloom, self.difficulty, self.block_number, self.gas_limit, self.gas_used, self.timestamp, self.extra_data, self.prev_randao, self.nonce };
return rlp.serialize(@TypeOf(tmp), allocator, tmp, list);
}
};

pub const Block = struct {
Expand Down

0 comments on commit dd3b4a2

Please sign in to comment.