You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Can cheaply parse leading fields whose lengths are fixed.
/// @notice Efficiently extracts one of the first 7 fields from an RLP-encoded Ethereum block header./// @dev Optimized for extracting one of the first 7 fields (parentHash, ommersHash, etc.)/// from a canonical RLP-encoded block header using precomputed offsets./// Much faster and cheaper than generic RLP decoding for these fields.function decodeLeadingField(bytescalldataheader, uint256field) internalpurereturns (bytesmemory) {
unchecked {
// Field 0: parentHash (32 bytes)// Field 1: ommersHash (32 bytes)// Field 2: beneficiary (20 bytes)// Field 3: stateRoot (32 bytes)// Field 4: transactionsRoot (32 bytes)// Field 5: receiptsRoot (32 bytes)// Field 6: logsBloom (259 bytes)uint8[7] memory offsets = [1, 34, 67, 88, 121, 154, 186];
uint16[7] memory lengths = [32, 32, 20, 32, 32, 32, 259];
uint256 start =1+ (uint8(header[0]) - (LIST_LONG_START -1)) + offsets[field];
return header[start:start + lengths[field]];
}
}
Activity
0xClandestine commentedon Jul 4, 2025
This throws stack-too-deep for obvious reasons...
Vectorized commentedon Jul 7, 2025
Good request.
Thank you OZ once again for the reference!
0xClandestine commentedon Jul 18, 2025
Can cheaply parse leading fields whose lengths are fixed.
[-]Rlp decoding + MPT proofs[/-][+]✨ Rlp decoding + MPT proofs[/+]