Skip to content

Commit

Permalink
Make simple proofs for fixed values test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
makoto committed Apr 24, 2024
1 parent 1b14bb0 commit 109e06c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 73 deletions.
41 changes: 13 additions & 28 deletions scroll-gateway/src/ScrollProofService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable prettier/prettier */
import { EVMProofHelper, type IProofService } from '@ensdomains/evm-gateway';
import { AbiCoder, Contract, ethers, toBeHex, type AddressLike, } from 'ethers';
// import { AbiCoder, Contract, EventLog, ethers, toBeHex, type AddressLike, toNumber } from 'ethers';
import { AbiCoder, Contract, ethers, type AddressLike, } from 'ethers';
import { concat } from "ethers";

import rollupAbi from "./abi/rollupABI.js";
Expand All @@ -28,16 +27,12 @@ export class ScrollProofService implements IProofService<ScrollProvableBlock> {
l2RollupAddress: string,
cache: IBlockCache
) {
console.log('***', {
l1Provider,l2Provider,l2RollupAddress
})
this.l2Provider = l2Provider;
this.rollup = new Contract(
l2RollupAddress,
rollupAbi,
l1Provider
);
console.log({l1Provider, l2Provider})
this.helper = new EVMProofHelper(l2Provider);
this.cache = cache
}
Expand All @@ -63,47 +58,38 @@ export class ScrollProofService implements IProofService<ScrollProvableBlock> {
slots: bigint[]
): Promise<string> {
const searchUrl = 'https://sepolia-api-re.scroll.io/api/search';
const resp:any = await fetch(`${searchUrl}?keyword=${block.number}`)
const resp:any = await fetch(`${searchUrl}?keyword=${Number(block.number)}`)
const obj:any = await resp.json()
const batchIndex = obj.batch_index
const FOO_ADDRESS="0x94fbce7ca1a0152cfc99f90f4421d31cf356c896"
const EMPTY_ADDRESS="0x0000000000000000000000000000000000000000"
console.log(22, {batchIndex, address, slots, FOO_ADDRESS, EMPTY_ADDRESS, blockNumber:block.number})
console.log(23, [FOO_ADDRESS, [EMPTY_ADDRESS], toBeHex(block.number)])
const proof = await this.l2Provider.send("eth_getProof", [FOO_ADDRESS, [EMPTY_ADDRESS], toBeHex(block.number)]);
console.log(3, JSON.stringify(proof, null, 2))
const accountProof: Array<string> = proof.accountProof;
const storageProof: Array<string> = proof.storageProof[0].proof;
console.log(4, {accountProof})
console.log(5, {storageProof})
const proof = await this.helper.getProofs(Number(block.number), address, slots)
const accountProof: Array<string> = proof.stateTrieWitness;
const storageProof: Array<string> = proof.storageProofs[0];

const compressedProof = concat([
`0x${accountProof.length.toString(16).padStart(2, "0")}`,
...accountProof,
`0x${storageProof.length.toString(16).padStart(2, "0")}`,
...storageProof,
]);
console.log(6, {compressedProof})
// struct ScrollWitnessData {
// uint256 batchIndex;
// bytes32 storageKey;
// bytes compressedProof;
// }
const res:any = AbiCoder.defaultAbiCoder().encode(
[
'tuple(uint256 batchIndex, bytes32 storageKey, bytes compressedProof)',
'tuple(uint256 batchIndex, uint256 storageKey, bytes compressedProof)',
'tuple(bytes[] stateTrieWitness, bytes[][] storageProofs)',
],
[
{
batchIndex,
// storageKey:slots[0], // Check how to handle multiple storage
storageKey:EMPTY_ADDRESS,
storageKey:slots[0], // Check how to handle multiple storage
compressedProof
},
proof,
]
);
console.log(7, res)
console.log({
blockNumber:Number(block.number), address,
batchIndex, slots, compressedProof, proof,
res
})
return res;
}
/**
Expand All @@ -115,7 +101,6 @@ export class ScrollProofService implements IProofService<ScrollProvableBlock> {
if (!block) throw new Error('No block found');
return {
number: block.number
// number: block.number - 1
};
}
}
15 changes: 5 additions & 10 deletions scroll-verifier/contracts/ScrollVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface IScrollChainCommitmentVerifier {
address account,
bytes32 storageKey,
bytes calldata proof
) public view returns (bytes32 stateRoot, bytes32 storageValue);
) external view returns (bytes32 stateRoot, bytes32 storageValue);

function verifyStateCommitment(
uint256 batchIndex,
Expand Down Expand Up @@ -53,16 +53,11 @@ contract ScrollVerifier is IEVMVerifier {
address target,
bytes32[] memory commands,
bytes[] memory constants,
bytes memory compressedProof
bytes memory proof
) external view returns (bytes[] memory values) {
(ScrollWitnessData memory scrollData, StateProof memory stateProof) = abi.decode(proof, (ScrollWitnessData, StateProof));
require(
verifier.verifyStateCommitment(
scrollData.batchIndex, target, scrollData.storageKey, scrollData.compressedProof
),
"compressed proof mismatch"
)
(bytes32 storageValue) = verifier.verifyZkTrieProof(account, scrollData.storageKey, scrollData.compressedProof);
values = [storageValue];
(bytes32 stateRoot, bytes32 storageValue) = verifier.verifyZkTrieProof(target, scrollData.storageKey, scrollData.compressedProof);
values = new bytes[](1);
values[0] = abi.encodePacked(storageValue);
}
}
5 changes: 3 additions & 2 deletions scroll-verifier/deploy_l1/00_arb_verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import 'dotenv/config';
const GATEWAY_URLS = {
scrollDevnetL1: 'http://localhost:8089/{sender}/{data}.json',
goerli: 'https://scroll-gateway-worker.ens-cf.workers.dev/{sender}/{data}.json',
// Point to localhost if you want to test locally
// sepolia: 'http://localhost:8080/{sender}/{data}.json'
sepolia: 'https://scroll-sepolia-gateway-worker.ens-cf.workers.dev/{sender}/{data}.json',
};

const ROLLUP_ADDRESSES = {
goerli: '0x45e5cAea8768F42B385A366D3551Ad1e0cbFAb17',
sepolia: '0xd80810638dbDF9081b72C1B33c65375e807281C8',
sepolia: '0x64cb3A0Dcf43Ae0EE35C1C15edDF5F46D48Fa570',
};

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
Expand Down
38 changes: 12 additions & 26 deletions scroll-verifier/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ const DEPLOYER_PRIVATE_KEY =
const L1_PROVIDER_URL = process.env.L1_PROVIDER_URL || '';
const L1_ETHERSCAN_API_KEY = process.env.L1_ETHERSCAN_API_KEY;
const L2_ETHERSCAN_API_KEY = process.env.L2_ETHERSCAN_API_KEY;

console.log({
DEPLOYER_PRIVATE_KEY,
L1_PROVIDER_URL,
L1_ETHERSCAN_API_KEY,
L2_ETHERSCAN_API_KEY
})
const config: HardhatUserConfig = {
solidity: '0.8.19',
networks: {
Expand All @@ -30,31 +35,20 @@ const config: HardhatUserConfig = {
],
deploy: ['deploy_l2/'],
},
goerli: {
url: L1_PROVIDER_URL,
accounts: [DEPLOYER_PRIVATE_KEY],
deploy: ['deploy_l1/'],
companionNetworks: {
l2: 'scrollGoerli',
},
},
sepolia: {
url: L1_PROVIDER_URL,
accounts: [DEPLOYER_PRIVATE_KEY],
deploy: ['deploy_l1/'],
companionNetworks: {
l2: 'scrollSepolia',
},
},
scrollGoerli: {
url: 'https://rpc.goerli.scroll.gateway.fm',
accounts: [DEPLOYER_PRIVATE_KEY],
deploy: ['deploy_l2/'],
chainId: 11155111
},
scrollSepolia: {
url: 'https://sepolia-rollup.scroll.io/rpc',
url: 'https://sepolia-rpc.scroll.io',
accounts: [DEPLOYER_PRIVATE_KEY],
deploy: [ "deploy_l2/" ],
chainId: 534351
},
},
etherscan: {
Expand All @@ -65,20 +59,12 @@ const config: HardhatUserConfig = {
scrollSepolia: L2_ETHERSCAN_API_KEY,
},
customChains: [
{
network: 'scrollGoerli',
chainId: 421613,
urls: {
apiURL: 'https://api-goerli.scrollscan.dev/api',
browserURL: 'https://api-goerli.scrollscan.dev',
},
},
{
network: "scrollSepolia",
chainId: 421614,
chainId: 534351,
urls: {
apiURL: "https://api-sepolia.scrollscan.dev/api",
browserURL: "https://api-sepolia.scrollscan.dev"
apiURL: "https://api-sepolia.scrollscan.com/api",
browserURL: "https://sepolia.scrollscan.com"
}
}
],
Expand Down
2 changes: 1 addition & 1 deletion scroll-verifier/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.1.0",
"scripts": {
"build": "echo 'building scroll-verifier...' && bun hardhat compile",
"test": " hardhat test --network scrollDevnetL1",
"test": " hardhat test",
"clean": "rm -fr artifacts cache node_modules typechain-types",
"lint": "exit 0"
},
Expand Down
19 changes: 13 additions & 6 deletions scroll-verifier/test/testScrollVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,25 @@ describe('ScrollVerifier', () => {
// doesn't support CCIP-read.
provider = new ethers.BrowserProvider(hre.network.provider);
signer = await provider.getSigner(0);
// How to test against public testnet.
// 1. deploy l2 contract
// `L2_ETHERSCAN_API_KEY=$L2_ETHERSCAN_API_KEY DEPLOYER_PRIVATE_KEY=$DEPLOYER_PRIVATE_KEY L2_PROVIDER_URL=$L2_PROVIDER_URL npx hardhat deploy --network scrollSepolia`
// 2. deploy l1 contract
// 2.1 Modify GATEWAY_URLS on 00_scroll_verifier.ts to point to localhost
// 2.2 `L1_ETHERSCAN_API_KEY=$L1_ETHERSCAN_API_KEY DEPLOYER_PRIVATE_KEY=$DEPLOYER_PRIVATE_KEY L1_PROVIDER_URL=$L1_PROVIDER_URL ROLLUP_ADDRESS=$ROLLUP_ADDRESS npx hardhat deploy --network sepolia`
// 3. startup gateway server
// 3.0 `cd ../evm-gateway`
// 3.1 Add .dev.vars and add L1_PROVIDER_URL, L2_PROVIDER_URL, and L2_ROLLUP
// 3.2 `yarn dev`
// 4. run the test
// `DEPLOYER_PRIVATE_KEY=$DEPLOYER_PRIVATE_KEY L1_PROVIDER_URL=$L1_PROVIDER_URL ROLLUP_ADDRESS=$ROLLUP_ADDRESS yarn test --network sepolia`

//Rollup address according to sequencer config. Unfortunately, there is no endpoint to fetch it at runtime from the rollup.
//The address can be found at nitro-testnode-sequencer-1/config/deployment.json
const rollupAddress = process.env.ROLLUP_ADDRESS;
// When testing against Goerli, replace with this address
// const rollupAddress = '0x45e5cAea8768F42B385A366D3551Ad1e0cbFAb17';

const gateway = await makeScrollGateway(
(hre.network.config as any).url,
(hre.config.networks[hre.network.companionNetworks.l2] as any).url,
rollupAddress,
5,
rollupAddress
);
const server = new Server()
gateway.add(server)
Expand Down

0 comments on commit 109e06c

Please sign in to comment.