Skip to content

Commit

Permalink
refactor(evm): rm check on the number of block header fields within p…
Browse files Browse the repository at this point in the history
…roveAncestralBlockHashes
  • Loading branch information
allemanfredi committed Sep 27, 2024
1 parent a4646a1 commit 2c0a882
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 28 deletions.
7 changes: 0 additions & 7 deletions packages/evm/contracts/adapters/BlockHashAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,14 @@ abstract contract BlockHashAdapter is IBlockHashAdapter, Adapter {
function proveAncestralBlockHashes(uint256 chainId, bytes[] memory blockHeaders) external {
for (uint256 i = 0; i < blockHeaders.length; i++) {
RLPReader.RLPItem memory blockHeaderRLP = RLPReader.toRlpItem(blockHeaders[i]);

if (!blockHeaderRLP.isList()) revert InvalidBlockHeaderRLP();

RLPReader.RLPItem[] memory blockHeaderContent = blockHeaderRLP.toList();

// A block header should have between 15 and 17 elements (baseFee and withdrawalsRoot have been added later)
if (blockHeaderContent.length < 15 || blockHeaderContent.length > 17)
revert InvalidBlockHeaderLength(blockHeaderContent.length);

bytes32 blockParent = bytes32(blockHeaderContent[0].toUint());
uint256 blockNumber = uint256(blockHeaderContent[8].toUint());

bytes32 blockHash = keccak256(blockHeaders[i]);
bytes32 storedBlockHash = getHash(chainId, blockNumber);

if (blockHash != storedBlockHash) revert ConflictingBlockHeader(blockNumber, blockHash, storedBlockHash);

_storeHash(chainId, blockNumber - 1, blockParent);
Expand Down
1 change: 0 additions & 1 deletion packages/evm/contracts/interfaces/IAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pragma solidity ^0.8.0;
*/
interface IAdapter {
error ConflictingBlockHeader(uint256 blockNumber, bytes32 blockHash, bytes32 storedBlockHash);
error InvalidBlockHeaderLength(uint256 length);
error InvalidBlockHeaderRLP();

/**
Expand Down
20 changes: 0 additions & 20 deletions packages/evm/test/adapters/02_Adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,5 @@ describe("Adapter", function () {
"InvalidBlockHeaderRLP",
)
})

it("Reverts if block proof doesn't match valid block header lengths", async function () {
const { adapter, blocks } = await setup()
const blockHeaderContents = RLP.decode(blockRLP(blocks[0]))
const blockHeaderTooShortContents = blockHeaderContents.slice(0, 14)
const blockHeaderTooShort = RLP.encode(blockHeaderTooShortContents)

// Block header RLP contains too few elements
await expect(adapter.proveAncestralBlockHashes(CHAIN_ID, [blockHeaderTooShort]))
.to.revertedWithCustomError(adapter, "InvalidBlockHeaderLength")
.withArgs(blockHeaderTooShortContents.length)

const blockHeaderTooLongContents = blockHeaderContents.concat([adapter.address, adapter.address])
const blockHeaderTooLong = RLP.encode(blockHeaderTooLongContents)

// Block header RLP contains too many elements
await expect(adapter.proveAncestralBlockHashes(CHAIN_ID, [blockHeaderTooLong]))
.to.revertedWithCustomError(adapter, "InvalidBlockHeaderLength")
.withArgs(blockHeaderTooLongContents.length)
})
})
})

0 comments on commit 2c0a882

Please sign in to comment.