Skip to content

Commit c4902a4

Browse files
committed
Expose execution block hash calculation (#4326)
## Proposed Changes This is a light refactor of the execution layer's block hash calculation logic making it easier to use externally. e.g. in `eleel` (sigp/eleel#18). A static method is preferable to a method because the calculation doesn't actually need any data from `self`, and callers may want to compute block hashes without constructing an `ExecutionLayer` (`eleel` only constructs a simpler `Engine` struct).
1 parent 9326b3d commit c4902a4

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

beacon_node/execution_layer/src/block_hash.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ use types::{
1212
};
1313

1414
impl<T: EthSpec> ExecutionLayer<T> {
15-
/// Verify `payload.block_hash` locally within Lighthouse.
15+
/// Calculate the block hash of an execution block.
1616
///
17-
/// No remote calls to the execution client will be made, so this is quite a cheap check.
18-
pub fn verify_payload_block_hash(&self, payload: ExecutionPayloadRef<T>) -> Result<(), Error> {
19-
let _timer = metrics::start_timer(&metrics::EXECUTION_LAYER_VERIFY_BLOCK_HASH);
20-
17+
/// Return `(block_hash, transactions_root)`, where `transactions_root` is the root of the RLP
18+
/// transactions.
19+
pub fn calculate_execution_block_hash(
20+
payload: ExecutionPayloadRef<T>,
21+
) -> (ExecutionBlockHash, Hash256) {
2122
// Calculate the transactions root.
2223
// We're currently using a deprecated Parity library for this. We should move to a
2324
// better alternative when one appears, possibly following Reth.
@@ -46,7 +47,19 @@ impl<T: EthSpec> ExecutionLayer<T> {
4647

4748
// Hash the RLP encoding of the block header.
4849
let rlp_block_header = rlp_encode_block_header(&exec_block_header);
49-
let header_hash = ExecutionBlockHash::from_root(keccak256(&rlp_block_header));
50+
(
51+
ExecutionBlockHash::from_root(keccak256(&rlp_block_header)),
52+
rlp_transactions_root,
53+
)
54+
}
55+
56+
/// Verify `payload.block_hash` locally within Lighthouse.
57+
///
58+
/// No remote calls to the execution client will be made, so this is quite a cheap check.
59+
pub fn verify_payload_block_hash(&self, payload: ExecutionPayloadRef<T>) -> Result<(), Error> {
60+
let _timer = metrics::start_timer(&metrics::EXECUTION_LAYER_VERIFY_BLOCK_HASH);
61+
62+
let (header_hash, rlp_transactions_root) = Self::calculate_execution_block_hash(payload);
5063

5164
if header_hash != payload.block_hash() {
5265
return Err(Error::BlockHashMismatch {

0 commit comments

Comments
 (0)