Skip to content
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

fix: conversion from callbackData bytes to l2BlockRangeAccepted uint256 #202

Merged
merged 13 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 51 additions & 13 deletions packages/linea-ens-resolver/contracts/L1Resolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract L1Resolver is
uint256 constant VERSIONABLE_HASHES_SLOT = 3;
uint256 constant VERSIONABLE_TEXTS_SLOT = 10;
// To check how old is the value/proof returned and is in the acceptable range
uint256 constant L2_BLOCK_RANGE_ACCEPTED = 86400;
uint256 constant ACCEPTED_L2_BLOCK_RANGE_LENGTH = 86400;
string public graphqlUrl;

event TargetSet(bytes name, address target);
Expand All @@ -63,6 +63,24 @@ contract L1Resolver is
* @param contractAddress Contract Address at which the deferred mutation should transact with.
*/
error StorageHandledByL2(uint256 chainId, address contractAddress);
error wrongl2BlockRangeLength(uint256 expected, uint256 actual);

/**
* @dev extra modifier added compared to the ENS initial implementation to make sure the
* l2 block range sent in the callbackData is not altered during the ccip-read flow
* when calling the EVMFetchTarget. It is not needed in the initial ENS implementation
* since the callbackData is not used.
*/
modifier callbackDataHasL2BlockRangeLength(bytes memory callbackData) {
uint256 acceptedL2BlockRangeLength = uint256(bytes32(callbackData));
if (acceptedL2BlockRangeLength != ACCEPTED_L2_BLOCK_RANGE_LENGTH) {
revert wrongl2BlockRangeLength(
ACCEPTED_L2_BLOCK_RANGE_LENGTH,
acceptedL2BlockRangeLength
);
}
_;
}

/**
* @param _verifier The chain verifier address
Expand Down Expand Up @@ -211,14 +229,19 @@ contract L1Resolver is
.element(COIN_TYPE_ETH)
.fetch(
this.addrCallback.selector,
abi.encode(L2_BLOCK_RANGE_ACCEPTED)
abi.encode(ACCEPTED_L2_BLOCK_RANGE_LENGTH)
); // recordVersions
}

function addrCallback(
bytes[] memory values,
bytes memory
) external pure returns (bytes memory) {
bytes memory callbackData
)
external
pure
callbackDataHasL2BlockRangeLength(callbackData)
returns (bytes memory)
{
return abi.encode(address(bytes20(values[1])));
}

Expand All @@ -237,14 +260,19 @@ contract L1Resolver is
.element(coinType)
.fetch(
this.addrCoinTypeCallback.selector,
abi.encode(L2_BLOCK_RANGE_ACCEPTED)
abi.encode(ACCEPTED_L2_BLOCK_RANGE_LENGTH)
);
}

function addrCoinTypeCallback(
bytes[] memory values,
bytes memory
) external pure returns (bytes memory) {
bytes memory callbackData
)
external
pure
callbackDataHasL2BlockRangeLength(callbackData)
returns (bytes memory)
{
return abi.encode(values[1]);
}

Expand All @@ -263,14 +291,19 @@ contract L1Resolver is
.element(key)
.fetch(
this.textCallback.selector,
abi.encode(L2_BLOCK_RANGE_ACCEPTED)
abi.encode(ACCEPTED_L2_BLOCK_RANGE_LENGTH)
);
}

function textCallback(
bytes[] memory values,
bytes memory
) external pure returns (bytes memory) {
bytes memory callbackData
)
external
pure
callbackDataHasL2BlockRangeLength(callbackData)
returns (bytes memory)
{
return abi.encode(string(values[1]));
}

Expand All @@ -287,14 +320,19 @@ contract L1Resolver is
.element(node)
.fetch(
this.contenthashCallback.selector,
abi.encode(L2_BLOCK_RANGE_ACCEPTED)
abi.encode(ACCEPTED_L2_BLOCK_RANGE_LENGTH)
);
}

function contenthashCallback(
bytes[] memory values,
bytes memory
) external pure returns (bytes memory) {
bytes memory callbackData
)
external
pure
callbackDataHasL2BlockRangeLength(callbackData)
returns (bytes memory)
{
return abi.encode(values[1]);
}

Expand Down
3 changes: 3 additions & 0 deletions packages/linea-ens-resolver/test/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ export const proofTest = {
export const extraDataTest =
"0x0000000000000000000000000b306bf915c4d645ff596e518faf3f9669b9701600000000000000000000000028f15b034f9744d43548ac64dce04ed77bdbd83200000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000120f470901a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000002000001ff000000000000000000000000000000000000000000000000000000000102200304ff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020e312102006c30788eb679837a67baf80dc450ee6bb8ee493b14b16250d3e4922000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000020e312102006c30788eb679837a67baf80dc450ee6bb8ee493b14b16250d3e49220000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000015180";

export const extraDataWithWrongL2BlockRangeLength =
"0x0000000000000000000000000b306bf915c4d645ff596e518faf3f9669b9701600000000000000000000000028f15b034f9744d43548ac64dce04ed77bdbd83200000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000120f470901a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000002000001ff000000000000000000000000000000000000000000000000000000000102200304ff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020e312102006c30788eb679837a67baf80dc450ee6bb8ee493b14b16250d3e4922000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000020e312102006c30788eb679837a67baf80dc450ee6bb8ee493b14b16250d3e49220000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000009999999";

export const wrongExtraData =
"0x0000000000000000000000000b306bf915c4d645ff596e518faf3f9669b9701600000000000000000000000028f15b034f9744d43548ac64dce04ed77bdbd83100000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000120f470901a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000002000001ff000000000000000000000000000000000000000000000000000000000102200304ff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020e312102006c30788eb679837a67baf80dc450ee6bb8ee493b14b16250d3e4922000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000020e312102006c30788eb679837a67baf80dc450ee6bb8ee493b14b16250d3e49220000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000015180";

Expand Down
46 changes: 36 additions & 10 deletions packages/linea-ens-resolver/test/testL1Resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
extraDataWithShortCallBackData,
retValueTest,
retValueLongTest,
extraDataWithWrongL2BlockRangeLength,
} from "./testData";
const labelhash = (label) => ethers.keccak256(ethers.toUtf8Bytes(label));
const encodeName = (name) => "0x" + packet.name.encode(name).toString("hex");
Expand All @@ -49,7 +50,7 @@ const EMPTY_BYTES32 =
const PROOF_ENCODING_PADDING =
"0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000044e0";

const ACCEPTED_BLOCK_RANGE_LENGTH = 86400;
const ACCEPTED_L2_BLOCK_RANGE_LENGTH = 86400;

type ethersObj = typeof ethersT &
Omit<HardhatEthersHelpers, "provider"> & {
Expand Down Expand Up @@ -444,7 +445,7 @@ describe("Crosschain Resolver", () => {
commands2Test,
constantsTest,
proofsEncoded,
ACCEPTED_BLOCK_RANGE_LENGTH
ACCEPTED_L2_BLOCK_RANGE_LENGTH
);
throw "Should have reverted";
} catch (error) {
Expand All @@ -457,7 +458,7 @@ describe("Crosschain Resolver", () => {
it("should not revert if the block number returned by the gateway is in the accepted block range", async () => {
await rollup.setCurrentStateRoot(blockNo, stateRoot);
await rollup.setCurrentStateRoot(
blockNo + ACCEPTED_BLOCK_RANGE_LENGTH - 10,
blockNo + ACCEPTED_L2_BLOCK_RANGE_LENGTH - 10,
stateRoot
);
let proofsEncoded = AbiCoder.defaultAbiCoder().encode(
Expand All @@ -480,7 +481,7 @@ describe("Crosschain Resolver", () => {
it("should revert if the block number returned by the gateway is not in the accepted block range", async () => {
await rollup.setCurrentStateRoot(blockNo, stateRoot);
await rollup.setCurrentStateRoot(
blockNo + ACCEPTED_BLOCK_RANGE_LENGTH + 10,
blockNo + ACCEPTED_L2_BLOCK_RANGE_LENGTH + 10,
stateRoot
);
let proofsEncoded = AbiCoder.defaultAbiCoder().encode(
Expand All @@ -505,7 +506,7 @@ describe("Crosschain Resolver", () => {
it("should not revert when callbackdata > 32 bytes", async () => {
await rollup.setCurrentStateRoot(blockNo, stateRoot);
await rollup.setCurrentStateRoot(
blockNo + ACCEPTED_BLOCK_RANGE_LENGTH - 10,
blockNo + ACCEPTED_L2_BLOCK_RANGE_LENGTH - 10,
stateRoot
);
let proofsEncoded = AbiCoder.defaultAbiCoder().encode(
Expand All @@ -529,7 +530,7 @@ describe("Crosschain Resolver", () => {
it("should revert when callbackdata < 32 bytes", async () => {
await rollup.setCurrentStateRoot(blockNo, stateRoot);
await rollup.setCurrentStateRoot(
blockNo + ACCEPTED_BLOCK_RANGE_LENGTH - 10,
blockNo + ACCEPTED_L2_BLOCK_RANGE_LENGTH - 10,
stateRoot
);
let proofsEncoded = AbiCoder.defaultAbiCoder().encode(
Expand All @@ -556,7 +557,7 @@ describe("Crosschain Resolver", () => {
it("should not revert when block sent by the gateway <= currentL2BlockNumber and currentL2BlockNumber <= ACCEPTED_BLOCK_RANGE_LENGTH", async () => {
const veryOldBlockNb = 1;
await rollup.setCurrentStateRoot(veryOldBlockNb, stateRoot);
await rollup.setCurrentStateRoot(ACCEPTED_BLOCK_RANGE_LENGTH, stateRoot);
await rollup.setCurrentStateRoot(ACCEPTED_L2_BLOCK_RANGE_LENGTH, stateRoot);
let proofsEncoded = AbiCoder.defaultAbiCoder().encode(
[
"uint256",
Expand All @@ -576,17 +577,17 @@ describe("Crosschain Resolver", () => {
});

it("should revert when block sent by the gateway > currentL2BlockNumber and currentL2BlockNumber <= ACCEPTED_BLOCK_RANGE_LENGTH", async () => {
const veryOldBlockNb = ACCEPTED_BLOCK_RANGE_LENGTH + 1;
const veryOldBlockNb = ACCEPTED_L2_BLOCK_RANGE_LENGTH + 1;
await rollup.setCurrentStateRoot(veryOldBlockNb, stateRoot);
await rollup.setCurrentStateRoot(ACCEPTED_BLOCK_RANGE_LENGTH, stateRoot);
await rollup.setCurrentStateRoot(ACCEPTED_L2_BLOCK_RANGE_LENGTH, stateRoot);
let proofsEncoded = AbiCoder.defaultAbiCoder().encode(
[
"uint256",
"tuple(bytes key, uint256 leafIndex, tuple(bytes value, bytes[] proofRelatedNodes) proof)",
"tuple(bytes32 key, uint256 leafIndex, tuple(bytes32 value, bytes[] proofRelatedNodes) proof, bool initialized)[]",
],
[
veryOldBlockNb + ACCEPTED_BLOCK_RANGE_LENGTH,
veryOldBlockNb + ACCEPTED_L2_BLOCK_RANGE_LENGTH,
proofTest.accountProof,
proofTest.storageProofs,
]
Expand Down Expand Up @@ -622,4 +623,29 @@ describe("Crosschain Resolver", () => {
expect(error.reason).to.equal("LineaProofHelper: wrong target");
}
});

it("should revert if the L2 block range length passed to the EVMFetchTarget has been modified", async () => {
// Set the block number and stateRoot to match the predefined proof in the proof test file
await rollup.setCurrentStateRoot(blockNo, stateRoot);
let proofsEncoded = AbiCoder.defaultAbiCoder().encode(
[
"uint256",
"tuple(bytes key, uint256 leafIndex, tuple(bytes value, bytes[] proofRelatedNodes) proof)",
"tuple(bytes32 key, uint256 leafIndex, tuple(bytes32 value, bytes[] proofRelatedNodes) proof, bool initialized)[]",
],
[blockNo, proofTest.accountProof, proofTest.storageProofs]
);
proofsEncoded = PROOF_ENCODING_PADDING + proofsEncoded.substring(2);
try {
await target.getStorageSlotsCallback(
proofsEncoded,
extraDataWithWrongL2BlockRangeLength
);
throw "Should have reverted";
} catch (error) {
expect(error.info.error.message).to.equal(
`VM Exception while processing transaction: reverted with custom error 'wrongl2BlockRangeLength(${ACCEPTED_L2_BLOCK_RANGE_LENGTH}, 161061273)'`
);
}
});
});
Loading